LLM.C · TENSOR FUNDAMENTALS
Tensor 内存与视图
从线性 storage 出发,理解 shape、stride、reshape、transpose 和 broadcast。
当前章节Tensor MemoryIndex · View · Broadcast
03 · TENSOR MEMORY LAB
张量视图与地址动画
同一块线性 storage,如何被 shape、stride 和 broadcasting 解释成不同逻辑张量。
通用公式
offset = storage_offset + Σ index[d] × stride[d]逻辑 tensorshape [2,3,4]
stride [12,4,1]当前坐标A[0,0,0]
0×12 + 0×4 + 0×1 = 0[0,0,0]0
[0,0,1]1
[0,0,2]2
[0,0,3]3
[0,1,0]4
[0,1,1]5
[0,1,2]6
[0,1,3]7
[0,2,0]8
[0,2,1]9
[0,2,2]10
[0,2,3]11
[1,0,0]12
[1,0,1]13
[1,0,2]14
[1,0,3]15
[1,1,0]16
[1,1,1]17
[1,1,2]18
[1,1,3]19
[1,2,0]20
[1,2,1]21
[1,2,2]22
[1,2,3]23
1
dim 0: 0 × 12+2
dim 1: 0 × 4+3
dim 2: 0 × 1+4
offset 0float data[ ]offset 000x1000
offset 110x1004
offset 220x1008
offset 330x100c
offset 440x1010
offset 550x1014
offset 660x1018
offset 770x101c
offset 880x1020
offset 990x1024
offset 10100x1028
offset 11110x102c
offset 12120x1030
offset 13130x1034
offset 14140x1038
offset 15150x103c
offset 16160x1040
offset 17170x1044
offset 18180x1048
offset 19190x104c
offset 20200x1050
offset 21210x1054
offset 22220x1058
offset 23230x105c
1 / 24