@mineproness/tensorloop 1.5.3 → 1.5.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": "1.5.3",
3
+ "version": "1.5.4",
4
4
  "description": "Raw-JAVASCRIPT Based Matirx and AI LLM Package called Tensorloop.",
5
5
  "keywords": [
6
6
  "ai",
@@ -19,5 +19,8 @@
19
19
  "repository": {
20
20
  "type": "git",
21
21
  "url": "https://github.com/mineproness-org/tensorloop.git"
22
+ },
23
+ "dependencies": {
24
+ "gpu.js": "^2.24.0"
22
25
  }
23
26
  }
package/src/FFN/index.js CHANGED
@@ -50,5 +50,6 @@ export class FFN {
50
50
  this.linear1.ClearInputCache()
51
51
  this.linear2.ClearInputCache()
52
52
  this.hidden.ClearInputCache()
53
+
53
54
  }
54
55
  }
package/src/GPU.js ADDED
@@ -0,0 +1,167 @@
1
+ import { GPU } from "gpu.js";
2
+
3
+ const gpu = new GPU({
4
+ mode: "gpu"
5
+ })
6
+ const kernel = gpu.createKernel(function () {
7
+ return 1
8
+ }).setOutput([1])
9
+
10
+ kernel()
11
+ const gl = kernel.context
12
+
13
+ export function splitMatrix(matrix, chunksSizes = gl.getParameter(gl.MAX_TEXTURE_SIZE)) {
14
+ const chunks = []
15
+ for (let a = 0; a < matrix.length; a += chunksSizes) {
16
+ chunks.push(matrix.slice(a, a + chunksSizes))
17
+ }
18
+ return { split: true, data: chunks }
19
+
20
+ }
21
+
22
+ const cacheLF = []
23
+ const cacheLBIas = []
24
+ const cacheLBWeights = []
25
+ const cacheLBInputGradent = []
26
+ export function InitKernel(weights, size) {
27
+ const splitedWeights = splitMatrix(weights)
28
+ for (let a = 0; a < splitedWeights.data.length; a++) {
29
+ cacheLF[a] = gpu.createKernel(function (input, bias, weights) {
30
+ let sum = bias[this.thread.x]
31
+ for (let a = 0; a < this.constants.inputLength; a++) {
32
+ sum += input[a] * weights[this.thread.x][a]
33
+ }
34
+ return sum
35
+ }).setConstants({
36
+ inputLength: splitedWeights.data[a].length
37
+ }).setOutput([splitedWeights.data[a].length])
38
+ cacheLBIas[a] = gpu.createKernel(function (
39
+ bias,
40
+ outputGradient,
41
+ learningRate
42
+ ) {
43
+ const grad = Math.max(
44
+ -1,
45
+ Math.min(1, outputGradient[this.thread.x])
46
+ )
47
+
48
+ return bias[this.thread.x] - learningRate * grad
49
+ })
50
+ .setOutput([splitedWeights.data[a].length])
51
+ cacheLBInputGradent[a] = gpu.createKernel(function (
52
+ weights,
53
+ outputGradient
54
+ ) {
55
+ let sum = 0
56
+
57
+ for (let a = 0; a < this.constants.outputSize; a++) {
58
+ const grad = Math.max(
59
+ -1,
60
+ Math.min(1, outputGradient[a])
61
+ )
62
+
63
+ sum += grad * weights[a][this.thread.x]
64
+ }
65
+
66
+ return sum
67
+ })
68
+ .setConstants({
69
+ outputSize: splitedWeights.data[a].length
70
+ })
71
+ .setOutput([size])
72
+ cacheLBWeights[a] = gpu.createKernel(function (
73
+ weights,
74
+ outputGradient,
75
+ input,
76
+ learningRate
77
+ ) {
78
+ const grad = Math.max(
79
+ -1,
80
+ Math.min(1, outputGradient[this.thread.y])
81
+ )
82
+
83
+ return weights[this.thread.y][this.thread.x]
84
+ - learningRate * input[this.thread.x] * grad
85
+ })
86
+ .setOutput([size, splitedWeights.data[a].length])
87
+ }
88
+
89
+
90
+ }
91
+ export function CalculateLinearForward(weights, bias, input) {
92
+ const splitedBias = splitMatrix(bias)
93
+ const splitedWeights = splitMatrix(weights)
94
+ const result = new Float32Array(weights.length)
95
+ let offset = 0;
96
+ for (let a = 0; a < splitedBias.data.length; a++) {
97
+ const output = cacheLF[a](input, splitedBias.data[a], splitedWeights.data[a])
98
+ result.set(output, offset)
99
+ offset += output.length
100
+ }
101
+ return result
102
+ }
103
+
104
+ export function CalculateBackward(weights, bias, out, learningRate, input) {
105
+ const splitedBias = splitMatrix(bias)
106
+ const splitedWeights = splitMatrix(weights)
107
+ const inputGradient = new Float32Array(input.length)
108
+ const biasOut = new Float32Array(bias.length)
109
+ const weightsOut = []
110
+ let offsetBias = 0
111
+ let offsetWeights = 0
112
+ let gradientOffset = 0
113
+
114
+ for (let a = 0; a < splitedWeights.data.length; a++) {
115
+ const weightChunk = splitedWeights.data[a]
116
+ const biasChunk = splitedBias.data[a]
117
+
118
+ const chunkOutputSize = weightChunk.length
119
+
120
+ // Gradient belonging to this output/weight chunk
121
+ const gradientChunk = out.slice(
122
+ gradientOffset,
123
+ gradientOffset + chunkOutputSize
124
+ )
125
+
126
+ gradientOffset += chunkOutputSize
127
+
128
+ // Bias update
129
+ const newBias = cacheLBIas[a](
130
+ biasChunk,
131
+ gradientChunk,
132
+ learningRate
133
+ )
134
+
135
+ biasOut.set(newBias, offsetBias)
136
+ offsetBias += newBias.length
137
+
138
+ // Input gradient
139
+ const outputGra = cacheLBInputGradent[a](
140
+ weightChunk,
141
+ gradientChunk
142
+ )
143
+
144
+ // ADD this chunk's gradient to the same input gradient
145
+ for (let i = 0; i < outputGra.length; i++) {
146
+ inputGradient[i] += outputGra[i]
147
+ }
148
+
149
+ // Weight update
150
+ const newWeights = cacheLBWeights[a](
151
+ weightChunk,
152
+ gradientChunk,
153
+ input,
154
+ learningRate
155
+ )
156
+
157
+ // Copy chunk into final Float32Array
158
+ weightsOut.push(...newWeights)
159
+
160
+ }
161
+
162
+ return {
163
+ inputGradient,
164
+ biasOut,
165
+ weightsOut
166
+ }
167
+ }
@@ -1,5 +1,6 @@
1
1
  import { LoadBias, LoadVectors, SaveBias, SaveVectors } from '../GetConfigs.js'
2
2
  import { existsSync } from 'fs'
3
+ import { CalculateBackward, CalculateLinearForward, InitKernel } from '../GPU.js'
3
4
  function GenerateWeightsBias(embeddingSize, vocabSize) {
4
5
  const vectors = []
5
6
  const Bias = new Float32Array(vocabSize)
@@ -38,6 +39,7 @@ export class Linear {
38
39
  this.Weights = vectors;
39
40
  this.Bias = Bias;
40
41
  }
42
+ InitKernel(this.Weights, embeddingSize)
41
43
  }
42
44
  Save() {
43
45
  SaveVectors(this.Weights, this.configs.save.filename[0])
@@ -45,29 +47,14 @@ export class Linear {
45
47
  }
46
48
  forward(input) {
47
49
  this.input.push(input)
48
- const output = new Float32Array(this.Weights.length)
49
- for (let a = 0; a < this.Weights.length; a++) {
50
- let sum = this.Bias[a]
51
- for (let t = 0; t < input.length; t++) {
52
- sum += input[t] * this.Weights[a][t]
53
- }
54
- output[a] = sum
55
- }
56
- return output
50
+ return CalculateLinearForward(this.Weights, this.Bias, input)
57
51
  }
58
52
  backward(outputGradient, learningRate, idx) {
59
53
  const input = this.input[idx];
60
54
  const weights = this.Weights
61
- const inputGradient = new Float32Array(input.length)
62
- for (let a = 0; a < weights.length; a++) {
63
- const gran = Math.max(-1, Math.min(1, outputGradient[a]))
64
- const w = weights[a]
65
- for (let at = 0; at < input.length; at++) {
66
- inputGradient[at] += gran * w[at]
67
- w[at] -= learningRate * input[at] * gran
68
- }
69
- this.Bias[a] -= learningRate * gran
70
- }
55
+ const {biasOut,inputGradient, weightsOut} = CalculateBackward(weights, this.Bias, outputGradient, learningRate, input)
56
+ this.Weights = weightsOut;
57
+ this.Bias = biasOut
71
58
  return inputGradient
72
59
  }
73
60
  ClearInputCache(){