-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtensor.go
266 lines (253 loc) · 9.31 KB
/
tensor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package llmgo
type tensor struct {
data []float32
dims []int
}
// TODO: make this better
func (t tensor) Data() []float32 {
return t.data
}
func newTensor(data []float32, dims ...int) (tensor, int) {
s := 1
for _, d := range dims {
s *= d
}
if s > len(data) {
panic("dimensions larger than supplied data")
}
ss := min(s, len(data))
return tensor{
data: data[:ss],
dims: dims,
}, ss
}
func (t tensor) size() int {
size := 1
for _, dim := range t.dims {
size *= dim
}
return size
}
func (t tensor) index(idx ...int) tensor {
// 1. Error Handling (Partially Adjusted)
if len(idx) > len(t.dims) {
panic("Too many indices for tensor dimensions")
}
for i, dim := range idx {
if dim < 0 || dim >= t.dims[i] {
panic("Index out of bounds")
}
}
// 2. Calculate Linear Index (Partially Adjusted)
linearIndex := idx[0]
stride := t.size()
for i := 1; i < len(idx); i++ {
stride /= t.dims[i]
linearIndex += idx[i] * stride
}
// 3. Adjust Dimensions and Return Sub-Tensor
newDims := t.dims[len(idx):] // Keep remaining dimensions
end := linearIndex + t.subTensorSize(newDims) // Size based on remaining dimensions
return tensor{
data: t.data[linearIndex:end],
dims: newDims,
}
}
// Helper function to calculate the size of a sub-tensor
func (t tensor) subTensorSize(idx []int) int {
subTensorSize := 1
for _, dim := range t.dims[len(idx):] {
subTensorSize *= dim
}
return subTensorSize
}
// ParameterTensors are the parameters of the model
type ParameterTensors struct {
Memory []float32
WordTokEmbed tensor // (V, C) - Word/Token Embedding weights (Vocabulary size, Embedding dimension)
WordPosEmbed tensor // (maxT, C) - Positional Embedding weights (Maximum Sequence length, Embedding dimension)
LayerNorm1W tensor // (L, C) - Weights for Layer Normalization 1 (Number of layers, Embedding dimension)
LayerNorm1B tensor // (L, C) - Biases for Layer Normalization 1
QueryKeyValW tensor // (L, 3*C, C) - Attention QKV weights (Layers, 3 * Embedding dimension, Embedding dimension)
QueryKeyValB tensor // (L, 3*C) - Attention QKV biases
AttProjW tensor // (L, C, C) - Attention projection weights (Layers, Embedding dimension, Embedding dimension)
AttProjB tensor // (L, C) - Attention projection biases
Layer2NormW tensor // (L, C) - Weights for Layer Normalization 2
Layer2NormB tensor // (L, C) - Biases for Layer Normalization 2
FeedFwdW tensor // (L, 4*C, C) - Feed-forward layer weights (Layers, 4 * Embedding Dimension, Embedding Dimension)
FeedFwdB tensor // (L, 4*C) - Feed-forward layer biases
FeedFwdProjW tensor // (L, C, 4*C) - Feed-forward projection weights
FeedFwdProjB tensor // (L, C)- Feed-forward projection biases
LayerFinNormW tensor // (C) - Final layer normalization weights
LayerFinNormB tensor // (C) - Final layer normalization biases
}
func newParameterTensors(V, C, maxSeqLen, L int) ParameterTensors {
var tensor ParameterTensors
tensor.Init(V, C, maxSeqLen, L)
return tensor
}
func (tensor *ParameterTensors) Len() int {
return len(tensor.Memory)
}
// Init initialises the ParameterTensors with specific sizes for each tensor based on the model architecture.
func (tensor *ParameterTensors) Init(V, C, maxSeqLen, L int) {
tensor.Memory = make([]float32,
V*C+ // WordTokEmbed
maxSeqLen*C+ // WordPosEmbed
L*C+ // LayerNorm1W
L*C+ // LayerNorm1B
L*3*C*C+ // QueryKeyValW
L*3*C+ // QueryKeyValB
L*C*C+ // AttProjW
L*C+ // AttProjB
L*C+ // Layer2NormW
L*C+ // Layer2NormB
L*4*C*C+ // FeedFwdW
L*4*C+ // FeedFwdB
L*C*4*C+ // FeedFwdProjW
L*C+ // FeedFwdProjB
C+ // LayerFinNormW
C, // LayerFinNormB
)
var ptr int
memPtr := tensor.Memory
tensor.WordTokEmbed, ptr = newTensor(memPtr, V, C)
memPtr = memPtr[ptr:]
tensor.WordPosEmbed, ptr = newTensor(memPtr, maxSeqLen, C)
memPtr = memPtr[ptr:]
tensor.LayerNorm1W, ptr = newTensor(memPtr, L, C)
memPtr = memPtr[ptr:]
tensor.LayerNorm1B, ptr = newTensor(memPtr, L, C)
memPtr = memPtr[ptr:]
tensor.QueryKeyValW, ptr = newTensor(memPtr, L, 3*C, C)
memPtr = memPtr[ptr:]
tensor.QueryKeyValB, ptr = newTensor(memPtr, L, 3*C)
memPtr = memPtr[ptr:]
tensor.AttProjW, ptr = newTensor(memPtr, L, C, C)
memPtr = memPtr[ptr:]
tensor.AttProjB, ptr = newTensor(memPtr, L, C)
memPtr = memPtr[ptr:]
tensor.Layer2NormW, ptr = newTensor(memPtr, L, C)
memPtr = memPtr[ptr:]
tensor.Layer2NormB, ptr = newTensor(memPtr, L, C)
memPtr = memPtr[ptr:]
tensor.FeedFwdW, ptr = newTensor(memPtr, L, 4*C, C)
memPtr = memPtr[ptr:]
tensor.FeedFwdB, ptr = newTensor(memPtr, L, 4*C)
memPtr = memPtr[ptr:]
tensor.FeedFwdProjW, ptr = newTensor(memPtr, L, C, 4*C)
memPtr = memPtr[ptr:]
tensor.FeedFwdProjB, ptr = newTensor(memPtr, L, C)
memPtr = memPtr[ptr:]
tensor.LayerFinNormW, ptr = newTensor(memPtr, C)
memPtr = memPtr[ptr:]
tensor.LayerFinNormB, ptr = newTensor(memPtr, C)
memPtr = memPtr[ptr:]
if len(memPtr) != 0 {
panic("something went real bad here")
}
}
// ActivationTensors
type ActivationTensors struct {
Memory []float32
Encoded tensor // (B, T, C) - Initial encoded input representations (Batch size, Sequence length, Embedding dimension)
Layer1Act tensor // (L, B, T, C) - Activations after Layer Normalization 1
LayerNorm1Mean tensor // (L, B, T) - Mean values for Layer Normalization 1
LayerNorm1Rstd tensor // (L, B, T) - Reciprocal of standard deviation for Layer Normalization 1
QueryKeyVal tensor // (L, B, T, 3*C) - Combined Query, Key, Value representations for attention
AttentionInter tensor // (L, B, T, C) - Intermediate attention-like result
PreAttention tensor // (L, B, NH, T, T) - Pre-attention scores
Attention tensor // (L, B, NH, T, T) - Normalized attention weights (Number of layers, Batch size, Number of Attention Heads, Sequence length, Sequence length)
AttentionProj tensor // (L, B, T, C) - Projected attention outputs
Residual2 tensor // (L, B, T, C) - Residual connection after attention
LayerNorm2Act tensor // (L, B, T, C) - Activations after Layer Normalization 2
LayerNorm2Mean tensor // (L, B, T) - Mean values for Layer Normalization 2
LayerNorm2Rstd tensor // (L, B, T) - Reciprocal of standard deviation for Layer Normalization 2
FeedForward tensor // (L, B, T, 4*C) - Intermediate Feed-Forward Network activations
FeedForwardGelu tensor // (L, B, T, 4*C) - FeedForward activations after applying GELU (non-linearity)
FeedForwardProj tensor // (L, B, T, C) - Projected output of the Feed-Forward Network
Residual3 tensor // (L, B, T, C) - Residual connection after Feed-Forward Network
LayerNormFinal tensor // (B, T, C) - Final activations after Layer Normalization
LayerNormFinalMean tensor // (B, T) - Mean values for final Layer Normalization
LayerNormFinalStd tensor // (B, T) - Reciprocal of standard deviation for final Layer Normalization
Logits tensor // (B, T, V) - Raw output scores (before softmax)
Probabilities tensor // (B, T, V) - Softmax probabilities over the vocabulary
Losses tensor // (B, T) - Loss values per token in the batch
}
func (tensor *ActivationTensors) Init(B, C, T, L, NH, V int) {
tensor.Memory = make([]float32,
B*T*C+
L*B*T*C+
L*B*T+
L*B*T+
L*B*T*C*3+
L*B*T*C+
L*B*NH*T*T+
L*B*NH*T*T+
L*B*T*C+
L*B*T*C+
L*B*T*C+
L*B*T+
L*B*T+
L*B*T*C*4+
L*B*T*C*4+
L*B*T*C+
L*B*T*C+
B*T*C+
B*T+
B*T+
B*T*V+
B*T*V+
B*T)
var ptr int
memPtr := tensor.Memory
tensor.Encoded, ptr = newTensor(memPtr, B, T, C)
memPtr = memPtr[ptr:]
tensor.Layer1Act, ptr = newTensor(memPtr, L, B, T, C)
memPtr = memPtr[ptr:]
tensor.LayerNorm1Mean, ptr = newTensor(memPtr, L, B, T)
memPtr = memPtr[ptr:]
tensor.LayerNorm1Rstd, ptr = newTensor(memPtr, L, B, T)
memPtr = memPtr[ptr:]
tensor.QueryKeyVal, ptr = newTensor(memPtr, L, B, T, C*3)
memPtr = memPtr[ptr:]
tensor.AttentionInter, ptr = newTensor(memPtr, L, B, T, C)
memPtr = memPtr[ptr:]
tensor.PreAttention, ptr = newTensor(memPtr, L, B, NH, T, T)
memPtr = memPtr[ptr:]
tensor.Attention, ptr = newTensor(memPtr, L, B, NH, T, T)
memPtr = memPtr[ptr:]
tensor.AttentionProj, ptr = newTensor(memPtr, L, B, T, C)
memPtr = memPtr[ptr:]
tensor.Residual2, ptr = newTensor(memPtr, L, B, T, C)
memPtr = memPtr[ptr:]
tensor.LayerNorm2Act, ptr = newTensor(memPtr, L, B, T, C)
memPtr = memPtr[ptr:]
tensor.LayerNorm2Mean, ptr = newTensor(memPtr, L, B, T)
memPtr = memPtr[ptr:]
tensor.LayerNorm2Rstd, ptr = newTensor(memPtr, L, B, T)
memPtr = memPtr[ptr:]
tensor.FeedForward, ptr = newTensor(memPtr, L, B, T, C*4)
memPtr = memPtr[ptr:]
tensor.FeedForwardGelu, ptr = newTensor(memPtr, L, B, T, C*4)
memPtr = memPtr[ptr:]
tensor.FeedForwardProj, ptr = newTensor(memPtr, L, B, T, C)
memPtr = memPtr[ptr:]
tensor.Residual3, ptr = newTensor(memPtr, L, B, T, C)
memPtr = memPtr[ptr:]
tensor.LayerNormFinal, ptr = newTensor(memPtr, B, T, C)
memPtr = memPtr[ptr:]
tensor.LayerNormFinalMean, ptr = newTensor(memPtr, B, T)
memPtr = memPtr[ptr:]
tensor.LayerNormFinalStd, ptr = newTensor(memPtr, B, T)
memPtr = memPtr[ptr:]
tensor.Logits, ptr = newTensor(memPtr, B, T, V)
memPtr = memPtr[ptr:]
tensor.Probabilities, ptr = newTensor(memPtr, B, T, V)
memPtr = memPtr[ptr:]
tensor.Losses, ptr = newTensor(memPtr, B, T)
memPtr = memPtr[ptr:]
if len(memPtr) != 0 {
panic("something went real bad here")
}
}