@imaduddin/fastbrain 1.0.0 → 1.0.2

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@imaduddin/fastbrain",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Raw-JAVASCRIPT Based Matirx and AI LLM Package called fastBrain.",
5
5
  "keywords": [
6
6
  "ai",
@@ -21,7 +21,7 @@
21
21
  "url": "https://github.com/imaduddin/fastBrain.git"
22
22
  },
23
23
  "dependencies": {
24
- "webgpu": "^0.6.0"
24
+ "webgpu": "^0.6.1"
25
25
  },
26
26
  "allowScripts": {
27
27
  "webgpu@0.6.0": true
@@ -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
  }
@@ -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: [
@@ -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 ga = getVector(configs.save.filename, embeddingSize);
32
- if (!ga) {
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 = ga
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(input) {
43
- this.input = input;
48
+ async forward(vec, encoderMain) {
44
49
  if (!this.gammabuffer) {
45
- this.gammabuffer = await this.op.createBuffer(4 * this.embeddingSize);
50
+ this.gammabuffer = await this.op.createBuffer(this.embeddingSize * 4);
46
51
  await this.op.WriteBuffer(this.gammabuffer, this.gamma);
47
52
  }
48
- let context = input.size / (this.embeddingSize * 4)
49
- if (!this.rms) this.rms = await this.op.createBuffer(4 * context);
50
- if (this.rms.size !== 4 * context) {
51
- this.rms.destroy()
52
- this.rms = await this.op.createBuffer(4 * context);
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 (!this.output) this.output = await this.op.ManageBuffer(input.size);
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(input.size)
62
+ this.output = await this.op.ManageBuffer(vec.size)
58
63
  }
59
- await this.output.count()
60
- const bindGroup = await this.op.createBindGroup(this.reductionShader, 0, [input.mainBuffer, this.rms])
61
- const encoder = await this.op.CreateEncoder()
62
- await this.op.RunPipline(encoder, this.reductionShader, bindGroup, context)
63
- const bindGroup1 = await this.op.createBindGroup(this.forwardShader, 0, [this.rms, input.mainBuffer, this.output.mainBuffer, this.gammabuffer])
64
- await this.op.RunPipline(encoder, this.forwardShader, bindGroup1, Math.ceil((input.size / 4) / 64));
65
- await this.op.submitQueue(encoder);
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(outputGradient, lr) {
69
- if (!this.input || !this.input.mainBuffer || this.input.isDestroyed?.()) {
70
- throw new Error("RMSNorm.backward: input buffer is gone");
71
- }
72
- if (!this.rms || this.rms.isDestroyed?.()) {
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.gradOut.count()
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 bindGroup = await this.op.createBindGroup(this.backwardShader, 0, [this.input.mainBuffer, outputGradient.mainBuffer, this.rms, this.gammabuffer, this.gradOut.mainBuffer]);
84
- const bindGroup2 = await this.op.createBindGroup(this.updateGamma, 0, [this.input.mainBuffer, outputGradient.mainBuffer, this.rms, this.gammabuffer, this.params]);
85
- const encoder = await this.op.CreateEncoder()
86
- await this.op.RunPipline(encoder, this.backwardShader, bindGroup, Math.ceil((this.input.size) / (this.embeddingSize * 4)))
87
- await this.op.RunPipline(encoder, this.updateGamma, bindGroup2, Math.ceil(this.embeddingSize / 64))
88
- await this.op.submitQueue(encoder)
89
- return this.gradOut
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 read = await this.op.readBuffer(4 * this.embeddingSize, this.gammabuffer);
93
- SaveVector(this.configs.save.filename, read)
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)
@@ -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;