@imaduddin/fastbrain 1.0.3 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@imaduddin/fastbrain",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "Raw-JAVASCRIPT Based Matirx and AI LLM Package called fastBrain.",
5
5
  "keywords": [
6
6
  "ai",
@@ -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.totalLoss))[0],
185
+ (await this.gpu.readBuffer(4, this.loss)),
215
186
 
216
187
  acc: async () =>
217
- (await this.gpu.readBuffer(4, this.totalAcc))[0],
218
-
219
- clear: async () => {
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
 
@@ -24,7 +24,10 @@ export class model {
24
24
  dirname: string,
25
25
  vocabFile: string
26
26
  })
27
- async forward(input: number[], target: number[]): Promise<{
27
+ getLoss : Function
28
+ getAcc: Function
29
+ clearLoss : Function
30
+ async forward(input: number[]): Promise<{
28
31
  read: function(): Promise<Float32Array>,
29
32
  mainBuffer: GPUBuffer,
30
33
  isDestroyed: function(): Boolean,
@@ -36,4 +39,5 @@ export class model {
36
39
  isDestroyed: function(): Boolean,
37
40
  destroy: function(): void
38
41
  }, target: number[], lr : number)
42
+ async Save() : Promise<void>
39
43
  }
@@ -5,7 +5,7 @@ import { Softmax } from "../Softmax/index.js"
5
5
  import { Tokenizer } from "../Tokenizer/index.js"
6
6
  import { join } from 'path'
7
7
  import { TransformerBlock } from "../TransformerBlock/index.js"
8
- import {OperationManager} from '../OperationManager.js'
8
+ import { OperationManager } from '../OperationManager.js'
9
9
  export class model {
10
10
  tokenizer
11
11
  embedding
@@ -46,7 +46,7 @@ export class model {
46
46
  })
47
47
  }
48
48
  }
49
- async forward(input, target) {
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,22 +58,29 @@ 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
- const {acc,clear,loss} = await this.softmax.crossEntropy(probs, target)
62
- this.getAcc = acc;
63
- this.getLoss = loss;
64
- this.clearLoss = clear;
61
+
65
62
  return probs
66
63
  }
67
- async backward(x, target, lr){
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--) {
72
- dInput = await this.blocks[i].backward(dInput, lr, encoder)
70
+ dInput = await this.blocks[i].backward(dInput, lr, encoder)
73
71
  };
74
72
  await this.embedding.backward(dInput, lr)
75
73
  await this.PosEmbedding.backward(dInput, lr)
76
74
  await this.op.submitQueue(encoder)
77
-
75
+ return { acc, loss }
76
+
77
+ }
78
+ async Save() {
79
+ await this.PosEmbedding.Save()
80
+ await this.embedding.Save()
81
+ await this.linear.Save()
82
+ for (let i = this.blocks.length - 1; i >= 0; i--) {
83
+ await this.blocks[i].Save()
84
+ };
78
85
  }
79
86
  }