@mineproness/tensorloop 2.1.11 → 2.1.41

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/index.js CHANGED
@@ -8,6 +8,7 @@ import {SelfAttention as ST} from './src/SelfAttention/index.js'
8
8
  import {GELU as GU} from './src/GELU/index.js'
9
9
  import {FFN as ffn} from './src/FFN/index.js'
10
10
  import { TransformerBlock as TB} from './src/TransformerBlock/index.js'
11
+ import Model from "./src/Model/index.js";
11
12
  export const Tokenizer = tokenizer
12
13
  export const Embedding = embeddding2
13
14
  export const Linear = li2
@@ -17,4 +18,5 @@ export const PositionEmbedding = PE
17
18
  export const SelfAttention = ST
18
19
  export const GELU = GU
19
20
  export const FFN = ffn
20
- export const TransformerBlock = TB
21
+ export const TransformerBlock = TB
22
+ export const model = Model
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mineproness/tensorloop",
3
- "version": "2.1.11",
3
+ "version": "2.1.41",
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
  }
@@ -50,6 +50,8 @@ export class SelfAttention {
50
50
  dInput[a][b] = dq[a][b] + dk[a][b] + dv[a][b]
51
51
  }
52
52
  }
53
+ dWeights.delete()
54
+ dScores.delete()
53
55
  return dInput
54
56
  }
55
57
  Save() {
@@ -1,5 +1,5 @@
1
1
  export class Softmax{
2
2
  forward(xLi : number[]) : number
3
- backward(target : number[]) : Float32Array[]
4
- crossEntropy(dLogits: Float32Array[], target: number[])
3
+ backward(probs: Float32Array[], target : number[]) : Float32Array[]
4
+ crossEntropy(probs: Float32Array[], target: number[])
5
5
  }
@@ -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;
@@ -261,12 +257,14 @@ export class GPUSoftmax {
261
257
  const maxLogits = this.cacheMaxCache(logits)
262
258
  const sumes = this.cacheSumCalculate(maxLogits, logits)
263
259
  const splitedLogits = split2DByColumns(logits)
264
- const result = Array.from({ length: this.contextSize }, () => [])
260
+ const result = Array.from({ length: this.contextSize }, () => new Float32Array(logits[0].length))
261
+ let offset = 0
265
262
  for (let i = 0; i < splitedLogits.length; i++) {
266
263
  const out = this.cacheSF[i](splitedLogits[i], sumes, maxLogits)
267
264
  for (let a = 0; a < this.contextSize; a++) {
268
- result[a].push(...out[a])
265
+ result[a].set(out[a], offset)
269
266
  }
267
+ offset += out[0].length
270
268
  }
271
269
  maxLogits.delete()
272
270
  sumes.delete()
@@ -274,19 +272,19 @@ export class GPUSoftmax {
274
272
  }
275
273
  backward(probs, target) {
276
274
  const splitedProbs = split2DByColumns(probs)
277
- const result = Array.from({ length: this.contextSize }, () => [])
275
+ const result = Array.from({ length: this.contextSize }, () => new Float32Array(probs[0].length))
276
+ let offset = 0;
278
277
  for (let i = 0; i < this.cacheSB.length; i++) {
279
278
  const out = this.cacheSB[i](splitedProbs[i], target)
280
279
  for (let a = 0; a < this.contextSize; a++) {
281
- result[a].push(...out[a])
280
+ result[a].set(out[a], offset)
282
281
  }
282
+ offset += out[0].length
283
283
  }
284
284
  return result
285
285
  }
286
286
  crossEntropy(probs, target) {
287
- const losses = this.calculateLoss(probs, target)
288
- const loss = this.avgLoss(losses)
289
- losses.delete()
287
+ const loss = this.avgLoss(probs, target)
290
288
  return loss
291
289
  }
292
290
 
@@ -718,4 +716,44 @@ export class GPUAttention {
718
716
  }
719
717
  });
720
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
+ }
721
759
  }