@mineproness/tensorloop 2.1.3 → 2.1.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mineproness/tensorloop",
3
- "version": "2.1.3",
3
+ "version": "2.1.4",
4
4
  "description": "Raw-JAVASCRIPT Based Matirx and AI LLM Package called Tensorloop.",
5
5
  "keywords": [
6
6
  "ai",
@@ -5,8 +5,7 @@ export class PositionEmbedding{
5
5
  filename: string
6
6
  }
7
7
  })
8
- forward(position: number | number[]) : Float32Array[] | Float32Array[][]
9
- backward(pos: number , inputGradient: number[] | Float32Array[], learingRate: number) : void
8
+ forward(vectors: Float32Array[]) :Float32Array[]
9
+ backward(inputGradient: Float32Array[], learingRate: number) : void
10
10
  Save() : void;
11
- addPos(vectors : Float32Array[][]) : Float32Array
12
11
  }
@@ -1,7 +1,7 @@
1
1
  import fs, { existsSync } from 'fs'
2
2
  import { LoadVectors, SaveVectors } from '../GetConfigs.js'
3
3
  // import { config } from 'process'
4
- import { GPUEmbedding } from '../gpiInit.js'
4
+ import { GPUEmbedding, GPUPosition } from '../gpiInit.js'
5
5
 
6
6
  function GenerateVectors(embeddingSize, vocabSize) {
7
7
  const vector = []
@@ -31,39 +31,18 @@ export class PositionEmbedding {
31
31
  }
32
32
  } else {
33
33
  this.vectors = GenerateVectors(embeddingSize, vocabSize)
34
- this.gpu = new GPUEmbedding(this.vectors, vocabSize, embeddingSize, 1)
34
+ this.gpu = new GPUPosition(this.vectors, embeddingSize, vocabSize)
35
35
  }
36
36
  }
37
- forward(token) {
38
- if (Array.isArray(token)) {
39
- return token.map((e) => {
40
- return this.vectors[e]
41
- })
42
- } else {
43
-
44
- return this.vectors[token]
45
- }
37
+ forward(vec) {
38
+ return this.gpu.forward(vec)
46
39
  }
47
- backward(token, inputGradient, learingRate) {
48
- for (let a = 0; a < this.vectors[token].length; a++) {
49
- this.vectors[token][a] -= learingRate * inputGradient[a]
50
- }
40
+ backward(inputGradient, learingRate) {
41
+ this.gpu.backward(inputGradient, learingRate)
51
42
  }
52
43
  Save() {
53
- SaveVectors(this.vectors, this.configs.save.filename)
54
- }
55
- addPos(xEM) {
56
- const out = []
57
-
58
- for (let pos = 0; pos < xEM.length; pos++) {
59
- const tokenVector = xEM[pos]
60
- const posVector = this.forward([pos])[0]
61
- const x = new Float32Array(xEM[0].length)
62
- for (let a = 0; a < tokenVector.length; a++) {
63
- x[a] = tokenVector[a] + posVector[a]
64
- }
65
- out.push(x)
66
- }
67
- return out
44
+ const save = this.gpu.Save()
45
+ SaveVectors(save, this.configs.save.filename)
46
+
68
47
  }
69
48
  }
@@ -70,25 +70,6 @@ export class Tokenizer {
70
70
  }
71
71
  }
72
72
  }
73
- return Validate(output.filter(e=> e.trim().length > 0)).join(" ").replaceAll(/\s+([,.;'{}`!~\"?])/g, "$1")
73
+ return output.filter(e=> e.trim().length > 0).join(" ").replaceAll(/\s+([,.;'{}`!~\"?])/g, "$1")
74
74
  }
75
- }
76
-
77
-
78
- function Validate(output = ["hello", "guys"]) {
79
- const bestOutput = []
80
- output.map((e, idx) => {
81
- if (idx == 0) {
82
- bestOutput[idx] = e[0].toLocaleUpperCase() + e.slice(1, e.length)
83
- } else {
84
- if (bestOutput[idx - 1] == ":" || bestOutput[idx - 1] == ".") {
85
-
86
- bestOutput[idx] = e[0].toLocaleUpperCase() + e.slice(1, e.length)
87
- } else {
88
- bestOutput[idx] = e
89
- }
90
- }
91
-
92
- })
93
- return bestOutput
94
75
  }
package/src/gpiInit.js CHANGED
@@ -194,23 +194,19 @@ export class GPUSoftmax {
194
194
  cacheSF = [];
195
195
  cacheSB = []
196
196
  contextSize;
197
- calculateLoss;
198
197
  avgLoss;
199
198
  constructor(logits, contextSize) {
200
199
  this.contextSize = contextSize;
201
- this.avgLoss = gpu.createKernel(function (losses) {
200
+ this.avgLoss = gpu.createKernel(function (probs, target) {
202
201
  let sum = 0;
203
202
  for (let i = 0; i < this.constants.contextSize; i++) {
204
- sum += losses[i]
203
+ sum += -Math.log(probs[i][target[i]] + 1e-15)
205
204
  }
206
205
  return sum / this.constants.contextSize
207
206
  }, {
208
207
  output: [1],
209
208
  constants: { contextSize }
210
209
  })
211
- this.calculateLoss = gpu.createKernel(function (probs, target) {
212
- return -Math.log(probs[this.thread.x][target[this.thread.x]] + 1e-9)
213
- }, { output: [contextSize], immutable: true, pipeline: true })
214
210
  this.cacheSumCalculate = gpu.createKernel(function (maxLogits, logits) {
215
211
  const maxLogit = maxLogits[this.thread.x]
216
212
  let sum = 0;
@@ -266,7 +262,7 @@ export class GPUSoftmax {
266
262
  for (let i = 0; i < splitedLogits.length; i++) {
267
263
  const out = this.cacheSF[i](splitedLogits[i], sumes, maxLogits)
268
264
  for (let a = 0; a < this.contextSize; a++) {
269
- result[a].set(out[a] , offset)
265
+ result[a].set(out[a], offset)
270
266
  }
271
267
  offset += out[0].length
272
268
  }
@@ -288,9 +284,7 @@ export class GPUSoftmax {
288
284
  return result
289
285
  }
290
286
  crossEntropy(probs, target) {
291
- const losses = this.calculateLoss(probs, target)
292
- const loss = this.avgLoss(losses)
293
- losses.delete()
287
+ const loss = this.avgLoss(probs, target)
294
288
  return loss
295
289
  }
296
290
 
@@ -722,4 +716,44 @@ export class GPUAttention {
722
716
  }
723
717
  });
724
718
  }
719
+ }
720
+
721
+
722
+ export class GPUPosition {
723
+ cachePF;
724
+ cachePB;
725
+ posVectors
726
+ constructor(posVectors, embeddingSize, contextSize) {
727
+ const vramKernel = gpu.createKernel(function (posVec) {
728
+ return posVec[this.thread.y][this.thread.x]
729
+ }, {
730
+ output: [embeddingSize, contextSize],
731
+ immutable: true,
732
+ pipeline: true
733
+ })
734
+ this.posVectors = vramKernel(posVectors)
735
+ this.cachePF = gpu.createKernel(function (posVectors, vectors) {
736
+ return vectors[this.thread.y][this.thread.x] + posVectors[this.thread.y][this.thread.x]
737
+ }, {
738
+ output: [embeddingSize, contextSize]
739
+ })
740
+ this.cachePB = gpu.createKernel(function (outGradient, learingRate, oldVec) {
741
+ return oldVec[this.thread.y][this.thread.x] - learingRate * outGradient[this.thread.y][this.thread.x]
742
+ }, {
743
+ output: [embeddingSize, contextSize],
744
+ immutable: true,
745
+ pipeline: true,
746
+ })
747
+ }
748
+ forward(vectors){
749
+ return this.cachePF(this.posVectors, vectors)
750
+ }
751
+ backward(outGradient, learingRate){
752
+ const oldVec = this.posVectors;
753
+ this.posVectors = this.cachePB(outGradient, learingRate, oldVec)
754
+ oldVec.delete()
755
+ }
756
+ Save(){
757
+ return this.posVectors.toArray()
758
+ }
725
759
  }