@mineproness/tensorloop 2.2.2 → 2.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/Softmax/index.js +8 -72
package/package.json
CHANGED
package/src/Softmax/index.js
CHANGED
|
@@ -113,81 +113,17 @@ export class Softmax {
|
|
|
113
113
|
}
|
|
114
114
|
async crossEntropy(probs, target) {
|
|
115
115
|
const encoder = this.device.createCommandEncoder()
|
|
116
|
-
|
|
117
116
|
const tokenCount = probs.size / (this.vocabSize * 4)
|
|
118
117
|
const targetBuffer = await this.gpu.createBuffer(target.length * Float32Array.BYTES_PER_ELEMENT)
|
|
119
|
-
|
|
120
|
-
|
|
118
|
+
if(!this.loss) this.loss = await this.gpu.createBuffer(4 * tokenCount)
|
|
119
|
+
if(!this.acc) this.acc = await this.gpu.createBuffer(4 * 1)
|
|
121
120
|
await this.gpu.WriteBuffer(targetBuffer, new Uint32Array(target))
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
buffer: probs.mainBuffer
|
|
129
|
-
}
|
|
130
|
-
},
|
|
131
|
-
{
|
|
132
|
-
binding: 1,
|
|
133
|
-
resource: {
|
|
134
|
-
buffer: targetBuffer
|
|
135
|
-
}
|
|
136
|
-
}, {
|
|
137
|
-
binding: 2,
|
|
138
|
-
resource: {
|
|
139
|
-
buffer: loss
|
|
140
|
-
}
|
|
141
|
-
}, {
|
|
142
|
-
binding: 3,
|
|
143
|
-
resource: {
|
|
144
|
-
buffer: this.paramsForward
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
},
|
|
148
|
-
]
|
|
149
|
-
})
|
|
150
|
-
await this.gpu.RunPipline(encoder, this.lossPipline, bind, Math.ceil((tokenCount / 4)))
|
|
151
|
-
const outputLoss = await this.gpu.createBuffer(4)
|
|
152
|
-
const reduceBind = await this.gpu.createBindGroup(this.reduceFn, 0, [loss, outputLoss, this.paramsForward])
|
|
153
|
-
await this.gpu.RunPipline(encoder, this.reduceFn, reduceBind, 1);
|
|
154
|
-
const bind2 = await this.device.createBindGroup({
|
|
155
|
-
layout: await this.accPipline.getBindGroupLayout(0),
|
|
156
|
-
entries: [
|
|
157
|
-
{
|
|
158
|
-
binding: 0,
|
|
159
|
-
resource: {
|
|
160
|
-
buffer: this.paramsForward
|
|
161
|
-
}
|
|
162
|
-
},
|
|
163
|
-
{
|
|
164
|
-
binding: 1,
|
|
165
|
-
resource: {
|
|
166
|
-
buffer: probs.mainBuffer
|
|
167
|
-
}
|
|
168
|
-
}, {
|
|
169
|
-
binding: 3,
|
|
170
|
-
resource: {
|
|
171
|
-
buffer: targetBuffer
|
|
172
|
-
}
|
|
173
|
-
},
|
|
174
|
-
{
|
|
175
|
-
binding: 2,
|
|
176
|
-
resource: {
|
|
177
|
-
buffer: acc
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
]
|
|
181
|
-
})
|
|
182
|
-
await this.gpu.RunPipline(encoder, this.accPipline, bind2, 1)
|
|
183
|
-
this.device.queue.submit([encoder.finish()])
|
|
184
|
-
targetBuffer.destroy()
|
|
185
|
-
loss.destroy()
|
|
186
|
-
const lossCpu = await this.gpu.readBuffer(outputLoss.size, outputLoss)
|
|
187
|
-
const accCPU = await this.gpu.readBuffer(acc.size, acc)
|
|
188
|
-
acc.destroy()
|
|
189
|
-
outputLoss.destroy()
|
|
190
|
-
return { loss: lossCpu[0], acc: accCPU[0] }
|
|
121
|
+
const bindGroup1 = await this.gpu.createBindGroup(this.lossPipline, 0, [probs.mainBuffer, targetBuffer, this.loss, this.paramsForward])
|
|
122
|
+
const bindGroup2 = await this.gpu.createBindGroup(this.accPipline, 0 , [this.paramsForward, probs.mainBuffer, targetBuffer, this.acc])
|
|
123
|
+
await this.gpu.RunPipline(encoder, this.lossPipline, bindGroup1, Math.ceil((tokenCount / 4)))
|
|
124
|
+
await this.gpu.RunPipline(encoder, this.accPipline, bindGroup2, 1)
|
|
125
|
+
await this.gpu.submitQueue(encoder)
|
|
126
|
+
return {loss: await this.gpu.readBuffer(this.loss.size, this.loss), acc: (await this.gpu.readBuffer(this.acc.size, this.acc))[0]}
|
|
191
127
|
}
|
|
192
128
|
|
|
193
129
|
}
|