@imaduddin/fastbrain 1.0.0 → 1.0.1
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/Embedding/index.js +1 -2
- package/src/Linear/index.js +0 -1
- package/src/RMSNorm/index.js +42 -43
- package/src/Relu/index.js +0 -2
- package/src/Softmax/index.js +0 -1
- package/src/TransformerBlock/index.js +2 -2
package/package.json
CHANGED
package/src/Embedding/index.js
CHANGED
|
@@ -63,7 +63,6 @@ export class Embedding {
|
|
|
63
63
|
this.outputBuffer.destroy()
|
|
64
64
|
this.outputBuffer = await this.gpu.ManageBuffer(Float32Array.BYTES_PER_ELEMENT * this.embeddingSize * input.length);
|
|
65
65
|
}
|
|
66
|
-
await this.outputBuffer.count()
|
|
67
66
|
const bind = await this.device.createBindGroup({
|
|
68
67
|
layout: await this.forwardPipline.getBindGroupLayout(0),
|
|
69
68
|
entries: [
|
|
@@ -87,7 +86,7 @@ export class Embedding {
|
|
|
87
86
|
]
|
|
88
87
|
})
|
|
89
88
|
await this.gpu.RunPipline(encoder, this.forwardPipline, bind, Math.ceil((this.embeddingSize * input.length) / 256))
|
|
90
|
-
if(!encoderMain) this.device.queue.submit([encoder.finish()])
|
|
89
|
+
if(!encoderMain) await this.device.queue.submit([encoder.finish()])
|
|
91
90
|
return this.outputBuffer
|
|
92
91
|
|
|
93
92
|
}
|
package/src/Linear/index.js
CHANGED
|
@@ -83,7 +83,6 @@ export class Linear {
|
|
|
83
83
|
this.outputBuffer.destroy()
|
|
84
84
|
this.outputBuffer = await this.gpu.ManageBuffer(4 * this.vocabSize * tokenCount)
|
|
85
85
|
}
|
|
86
|
-
await this.outputBuffer.count()
|
|
87
86
|
const bind = await this.device.createBindGroup({
|
|
88
87
|
layout: await this.forwardPipline.getBindGroupLayout(0),
|
|
89
88
|
entries: [
|
package/src/RMSNorm/index.js
CHANGED
|
@@ -11,6 +11,11 @@ export class RMSNorm {
|
|
|
11
11
|
gamma;
|
|
12
12
|
gammabuffer;
|
|
13
13
|
configs;
|
|
14
|
+
rmsReduction;
|
|
15
|
+
output;
|
|
16
|
+
gradientOutput;
|
|
17
|
+
input;
|
|
18
|
+
device
|
|
14
19
|
constructor(device, embeddingSize, configs) {
|
|
15
20
|
this.configs = configs
|
|
16
21
|
this.op = new OperationManager(device);
|
|
@@ -27,70 +32,64 @@ export class RMSNorm {
|
|
|
27
32
|
this.updateGamma = this.op.getShaderPipline("./src/shaders/Norm/updateGamma.wgsl", {
|
|
28
33
|
embeddingSize: embeddingSize
|
|
29
34
|
})
|
|
35
|
+
this.device = device;
|
|
30
36
|
if (configs) {
|
|
31
|
-
const
|
|
32
|
-
if (!
|
|
37
|
+
const gammaFile = getVector(configs.save.filename, embeddingSize);
|
|
38
|
+
if (!gammaFile) {
|
|
33
39
|
this.gamma = new Float32Array(embeddingSize).fill(1);
|
|
34
40
|
SaveVector(configs.save.filename, this.gamma)
|
|
35
41
|
} else {
|
|
36
|
-
this.gamma =
|
|
42
|
+
this.gamma = gammaFile
|
|
37
43
|
}
|
|
38
44
|
} else {
|
|
39
45
|
this.gamma = new Float32Array(embeddingSize).fill(1)
|
|
40
46
|
}
|
|
41
47
|
}
|
|
42
|
-
async forward(
|
|
43
|
-
this.input = input;
|
|
48
|
+
async forward(vec, encoderMain) {
|
|
44
49
|
if (!this.gammabuffer) {
|
|
45
|
-
this.gammabuffer = await this.op.createBuffer(
|
|
50
|
+
this.gammabuffer = await this.op.createBuffer(this.embeddingSize * 4);
|
|
46
51
|
await this.op.WriteBuffer(this.gammabuffer, this.gamma);
|
|
47
52
|
}
|
|
48
|
-
|
|
49
|
-
if (!this.
|
|
50
|
-
if (this.
|
|
51
|
-
|
|
52
|
-
this.
|
|
53
|
+
const context = vec.size / (this.embeddingSize * 4)
|
|
54
|
+
if (!this.rmsReduction) this.rmsReduction = await this.op.createBuffer(context * 4);
|
|
55
|
+
if (!this.output) this.output = await this.op.ManageBuffer(vec.size)
|
|
56
|
+
if (this.rmsReduction.size !== context * 4) {
|
|
57
|
+
this.rmsReduction.destroy()
|
|
58
|
+
this.rmsReduction = await this.op.createBuffer(4 * context)
|
|
53
59
|
}
|
|
54
|
-
if (
|
|
55
|
-
if (this.output.size !== 4 * this.embeddingSize * context || this.output.isDestroyed() || this.output.size == 0) {
|
|
60
|
+
if (this.output.size !== vec.size) {
|
|
56
61
|
this.output.destroy()
|
|
57
|
-
this.output = await this.op.ManageBuffer(
|
|
62
|
+
this.output = await this.op.ManageBuffer(vec.size)
|
|
58
63
|
}
|
|
59
|
-
await this.
|
|
60
|
-
const
|
|
61
|
-
const
|
|
62
|
-
await this.op.RunPipline(encoder, this.reductionShader,
|
|
63
|
-
|
|
64
|
-
await this.op.
|
|
65
|
-
|
|
64
|
+
const encoder = encoderMain ? encoderMain : await this.op.CreateEncoder()
|
|
65
|
+
const bind1 = await this.op.createBindGroup(this.reductionShader, 0, [vec.mainBuffer, this.rmsReduction])
|
|
66
|
+
const bind2 = await this.op.createBindGroup(this.forwardShader, 0, [this.rmsReduction, vec.mainBuffer, this.output.mainBuffer, this.gammabuffer])
|
|
67
|
+
await this.op.RunPipline(encoder, this.reductionShader, bind1, context)
|
|
68
|
+
await this.op.RunPipline(encoder, this.forwardShader, bind2, Math.ceil((this.embeddingSize * context) / 64))
|
|
69
|
+
if (!encoderMain) await this.op.submitQueue(encoder);
|
|
70
|
+
this.input = vec;
|
|
66
71
|
return this.output
|
|
72
|
+
|
|
67
73
|
}
|
|
68
|
-
async backward(
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
throw new Error("RMSNorm.backward: rms buffer is gone");
|
|
74
|
-
}
|
|
75
|
-
if (!this.gradOut) this.gradOut = await this.op.ManageBuffer(outputGradient.size);
|
|
76
|
-
if (this.gradOut.size !== outputGradient.size || this.gradOut.isDestroyed() || this.gradOut.size == 0) {
|
|
77
|
-
this.gradOut.destroy()
|
|
78
|
-
this.gradOut = await this.op.ManageBuffer(outputGradient.size)
|
|
74
|
+
async backward(outGradient, lr, encoderMain) {
|
|
75
|
+
const contextSize = outGradient.size / (this.embeddingSize * 4);
|
|
76
|
+
if (!this.gradientOutput) this.gradientOutput = await this.op.ManageBuffer(this.embeddingSize * 4 * contextSize)
|
|
77
|
+
if (this.gradientOutput.size !== outGradient.size) {
|
|
78
|
+
this.gradientOutput = await this.op.ManageBuffer(this.embeddingSize * 4 * contextSize)
|
|
79
79
|
}
|
|
80
|
-
await this.
|
|
81
|
-
if (!this.params) this.params = await this.op.createBuffer(4 * 2)
|
|
80
|
+
if (!this.params) this.params = await this.op.createBuffer(4 * 2);
|
|
82
81
|
await this.op.WriteBuffer(this.params, new Float32Array([lr, 0]))
|
|
83
|
-
const
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
await this.op.
|
|
87
|
-
await this.op.RunPipline(encoder, this.updateGamma,
|
|
88
|
-
|
|
89
|
-
return this.
|
|
82
|
+
const encoder = encoderMain ? encoderMain : this.op.CreateEncoder()
|
|
83
|
+
const bind1 = await this.op.createBindGroup(this.backwardShader, 0, [this.input.mainBuffer, outGradient.mainBuffer, this.rmsReduction, this.gammabuffer, this.gradientOutput.mainBuffer])
|
|
84
|
+
await this.op.RunPipline(encoder, this.backwardShader, bind1, contextSize);
|
|
85
|
+
const bind2 = await this.op.createBindGroup(this.updateGamma, 0, [this.input.mainBuffer, outGradient.mainBuffer, this.rmsReduction, this.gammabuffer, this.params])
|
|
86
|
+
await this.op.RunPipline(encoder, this.updateGamma, bind2, this.embeddingSize);
|
|
87
|
+
if (!encoderMain) this.op.submitQueue(encoder);
|
|
88
|
+
return this.gradientOutput
|
|
90
89
|
}
|
|
91
90
|
async Save() {
|
|
92
|
-
const
|
|
93
|
-
SaveVector(this.configs.save.filename,
|
|
91
|
+
const gpuGamma = await this.op.readBuffer(4 * this.embeddingSize, this.gammabuffer);
|
|
92
|
+
SaveVector(this.configs.save.filename, gpuGamma)
|
|
94
93
|
}
|
|
95
94
|
|
|
96
95
|
}
|
package/src/Relu/index.js
CHANGED
|
@@ -27,7 +27,6 @@ export class Relu {
|
|
|
27
27
|
this.output.destroy()
|
|
28
28
|
this.output = await this.gpu.ManageBuffer(input.mainBuffer.size);
|
|
29
29
|
}
|
|
30
|
-
await this.output.count()
|
|
31
30
|
if (this.oldParam[1] !== tokenCount) {
|
|
32
31
|
this.oldParam = new Uint32Array([this.embeddingSize, tokenCount, 0])
|
|
33
32
|
await this.gpu.WriteBuffer(this.paramBuffer, this.oldParam)
|
|
@@ -71,7 +70,6 @@ export class Relu {
|
|
|
71
70
|
this.gradOut.destroy();
|
|
72
71
|
this.gradOut = await this.gpu.ManageBuffer(inputGradient.size)
|
|
73
72
|
}
|
|
74
|
-
await this.gradOut.count()
|
|
75
73
|
if (this.oldParam[1] !== tokenCount) {
|
|
76
74
|
this.oldParam = new Uint32Array([this.embeddingSize, tokenCount, 0])
|
|
77
75
|
await this.gpu.WriteBuffer(this.paramBuffer, this.oldParam)
|
package/src/Softmax/index.js
CHANGED
|
@@ -42,7 +42,6 @@ export class Softmax {
|
|
|
42
42
|
this.output.destroy()
|
|
43
43
|
this.output = await this.gpu.ManageBuffer(logits.size)
|
|
44
44
|
}
|
|
45
|
-
await this.output.count()
|
|
46
45
|
const bind = await this.device.createBindGroup({
|
|
47
46
|
layout: await this.forwardPipline.getBindGroupLayout(0),
|
|
48
47
|
entries: [
|
|
@@ -45,10 +45,10 @@ export class TransformerBlock {
|
|
|
45
45
|
|
|
46
46
|
async forward(input, encoderMain) {
|
|
47
47
|
const count = input.size / 4;
|
|
48
|
-
const norm1 = await this.rmsnorm.forward(input);
|
|
48
|
+
const norm1 = await this.rmsnorm.forward(input, encoderMain);
|
|
49
49
|
const att = await this.attention.forward(norm1, encoderMain)
|
|
50
50
|
const residual1 = await this.rFor1.add(att, input, encoderMain)
|
|
51
|
-
const norm2 = await this.rmsnorm2.forward(residual1)
|
|
51
|
+
const norm2 = await this.rmsnorm2.forward(residual1, encoderMain)
|
|
52
52
|
const f = await this.ffn.forward(norm2, encoderMain)
|
|
53
53
|
const residual2 = await this.rFor2.add(f, residual1, encoderMain)
|
|
54
54
|
return residual2;
|