@imaduddin/fastbrain 1.0.4 → 1.0.5
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 +4 -43
- package/src/model/index.d.ts +1 -1
- package/src/model/index.js +5 -6
package/package.json
CHANGED
package/src/Softmax/index.js
CHANGED
|
@@ -145,22 +145,6 @@ export class Softmax {
|
|
|
145
145
|
this.acc = await this.gpu.createBuffer(4);
|
|
146
146
|
}
|
|
147
147
|
|
|
148
|
-
if (!this.totalAcc) {
|
|
149
|
-
this.totalAcc = await this.gpu.createBuffer(4);
|
|
150
|
-
await this.gpu.WriteBuffer(
|
|
151
|
-
this.totalAcc,
|
|
152
|
-
new Float32Array([0])
|
|
153
|
-
);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
if (!this.totalLoss) {
|
|
157
|
-
this.totalLoss = await this.gpu.createBuffer(4);
|
|
158
|
-
await this.gpu.WriteBuffer(
|
|
159
|
-
this.totalLoss,
|
|
160
|
-
new Float32Array([0])
|
|
161
|
-
);
|
|
162
|
-
}
|
|
163
|
-
|
|
164
148
|
await this.gpu.WriteBuffer(
|
|
165
149
|
this.targetBuffer,
|
|
166
150
|
new Uint32Array(target)
|
|
@@ -178,12 +162,6 @@ export class Softmax {
|
|
|
178
162
|
[this.paramsForward, probs.mainBuffer, this.acc, this.targetBuffer]
|
|
179
163
|
);
|
|
180
164
|
|
|
181
|
-
const bindGroup3 = await this.gpu.createBindGroup(
|
|
182
|
-
this.totalCalculate,
|
|
183
|
-
0,
|
|
184
|
-
[this.loss, this.acc, this.totalLoss, this.totalAcc]
|
|
185
|
-
);
|
|
186
|
-
|
|
187
165
|
await this.gpu.RunPipline(
|
|
188
166
|
encoder,
|
|
189
167
|
this.lossPipline,
|
|
@@ -198,35 +176,18 @@ export class Softmax {
|
|
|
198
176
|
1
|
|
199
177
|
);
|
|
200
178
|
|
|
201
|
-
await this.gpu.RunPipline(
|
|
202
|
-
encoder,
|
|
203
|
-
this.totalCalculate,
|
|
204
|
-
bindGroup3,
|
|
205
|
-
1
|
|
206
|
-
);
|
|
207
|
-
|
|
208
179
|
if (!encoderMain) {
|
|
209
180
|
await this.gpu.submitQueue(encoder);
|
|
210
181
|
}
|
|
211
182
|
|
|
212
183
|
return {
|
|
213
184
|
loss: async () =>
|
|
214
|
-
(await this.gpu.readBuffer(4, this.
|
|
185
|
+
(await this.gpu.readBuffer(4, this.loss)),
|
|
215
186
|
|
|
216
187
|
acc: async () =>
|
|
217
|
-
(await this.gpu.readBuffer(4, this.
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
await this.gpu.WriteBuffer(
|
|
221
|
-
this.totalLoss,
|
|
222
|
-
new Float32Array([0])
|
|
223
|
-
);
|
|
224
|
-
|
|
225
|
-
await this.gpu.WriteBuffer(
|
|
226
|
-
this.totalAcc,
|
|
227
|
-
new Float32Array([0])
|
|
228
|
-
);
|
|
229
|
-
}
|
|
188
|
+
(await this.gpu.readBuffer(4, this.acc))[0],
|
|
189
|
+
|
|
190
|
+
|
|
230
191
|
};
|
|
231
192
|
}
|
|
232
193
|
|
package/src/model/index.d.ts
CHANGED
|
@@ -27,7 +27,7 @@ export class model {
|
|
|
27
27
|
getLoss : Function
|
|
28
28
|
getAcc: Function
|
|
29
29
|
clearLoss : Function
|
|
30
|
-
async forward(input: number[]
|
|
30
|
+
async forward(input: number[]): Promise<{
|
|
31
31
|
read: function(): Promise<Float32Array>,
|
|
32
32
|
mainBuffer: GPUBuffer,
|
|
33
33
|
isDestroyed: function(): Boolean,
|
package/src/model/index.js
CHANGED
|
@@ -46,7 +46,7 @@ export class model {
|
|
|
46
46
|
})
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
|
-
async forward(input
|
|
49
|
+
async forward(input) {
|
|
50
50
|
const encoder = await this.op.CreateEncoder()
|
|
51
51
|
if (input.length > this.contextSize) throw new Error("The input get large then ContextSize")
|
|
52
52
|
let vec = await this.embedding.forward(input, encoder)
|
|
@@ -58,14 +58,12 @@ export class model {
|
|
|
58
58
|
let logits = await this.linear.forward(vec, encoder)
|
|
59
59
|
let probs = await this.softmax.forward(logits, encoder)
|
|
60
60
|
await this.op.submitQueue(encoder)
|
|
61
|
-
|
|
62
|
-
this.getAcc = acc;
|
|
63
|
-
this.getLoss = loss;
|
|
64
|
-
this.clearLoss = clear;
|
|
61
|
+
|
|
65
62
|
return probs
|
|
66
63
|
}
|
|
67
64
|
async backward(x, target, lr) {
|
|
68
65
|
const encoder = await this.op.CreateEncoder()
|
|
66
|
+
const { acc, loss } = await this.softmax.crossEntropy(probs, target, encoder)
|
|
69
67
|
let dLogits = await this.softmax.backward(x, target, encoder)
|
|
70
68
|
let dInput = await this.linear.backward(dLogits, lr, encoder)
|
|
71
69
|
for (let i = this.blocks.length - 1; i >= 0; i--) {
|
|
@@ -74,6 +72,7 @@ export class model {
|
|
|
74
72
|
await this.embedding.backward(dInput, lr)
|
|
75
73
|
await this.PosEmbedding.backward(dInput, lr)
|
|
76
74
|
await this.op.submitQueue(encoder)
|
|
75
|
+
return { acc, loss }
|
|
77
76
|
|
|
78
77
|
}
|
|
79
78
|
async Save() {
|
|
@@ -81,7 +80,7 @@ export class model {
|
|
|
81
80
|
await this.embedding.Save()
|
|
82
81
|
await this.linear.Save()
|
|
83
82
|
for (let i = this.blocks.length - 1; i >= 0; i--) {
|
|
84
|
-
|
|
83
|
+
await this.blocks[i].Save()
|
|
85
84
|
};
|
|
86
85
|
}
|
|
87
86
|
}
|