@mineproness/tensorloop 1.5.4 → 1.5.6

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.
Binary file
Binary file
Binary file
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mineproness/tensorloop",
3
- "version": "1.5.4",
3
+ "version": "1.5.6",
4
4
  "description": "Raw-JAVASCRIPT Based Matirx and AI LLM Package called Tensorloop.",
5
5
  "keywords": [
6
6
  "ai",
package/src/GPU.js CHANGED
@@ -18,150 +18,115 @@ export function splitMatrix(matrix, chunksSizes = gl.getParameter(gl.MAX_TEXTURE
18
18
  return { split: true, data: chunks }
19
19
 
20
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++) {
21
+ export class GPULinear {
22
+ cacheLF = [];
23
+ cacheLBIas = [];
24
+ cacheLBWeights = [];
25
+ cacheLBInputGradent = [];
26
+ // texures
27
+ Bias = [];
28
+ Weights = []
29
+ InitKernel(weights, bias, size) {
30
+ const splitedWeights = splitMatrix(weights)
31
+ const splitedBias = splitMatrix(bias)
32
+ this.Weights = splitedWeights.data
33
+ this.Bias = splitedBias.data
34
+ for (let a = 0; a < splitedWeights.data.length; a++) {
35
+ this.cacheLF[a] = gpu.createKernel(function (input, bias, weights) {
36
+ let sum = bias[this.thread.x]
37
+ for (let a = 0; a < this.constants.inputLength; a++) {
38
+ sum += input[a] * weights[this.thread.x][a]
39
+ }
40
+ return sum
41
+ }).setConstants({
42
+ inputLength: splitedWeights.data[a][0].length
43
+ }).setArgumentTypes({
44
+ input: 'Array',
45
+ bias: 'Array',
46
+ weights: 'Array'
47
+ }).setOutput([splitedWeights.data[a].length])
48
+ this.cacheLBIas[a] = gpu.createKernel(function (
49
+ bias,
50
+ outputGradient,
51
+ learningRate
52
+ ) {
58
53
  const grad = Math.max(
59
54
  -1,
60
- Math.min(1, outputGradient[a])
55
+ Math.min(1, outputGradient[this.thread.x])
61
56
  )
62
57
 
63
- sum += grad * weights[a][this.thread.x]
64
- }
65
-
66
- return sum
67
- })
68
- .setConstants({
69
- outputSize: splitedWeights.data[a].length
58
+ return bias[this.thread.x] - learningRate * grad
59
+ }, {
60
+ pipeline: true,
61
+ immutable: true,
62
+ output: [splitedWeights.data[a].length]
70
63
  })
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
64
+ this.cacheLBInputGradent[a] = gpu.createKernel(function (
65
+ weights,
66
+ outputGradient
67
+ ) {
68
+ let sum = 0
69
+
70
+ for (let a = 0; a < this.constants.outputSize; a++) {
71
+ const grad = Math.max(
72
+ -1,
73
+ Math.min(1, outputGradient[a])
74
+ )
75
+
76
+ sum += grad * weights[a][this.thread.x]
77
+ }
78
+
79
+ return sum
80
+ }, {
81
+ constants: { outputSize: splitedWeights.data[a].length },
82
+ output: [size]
83
+ })
84
+
85
+ this.cacheLBWeights[a] = gpu.createKernel(function (
86
+ weights,
87
+ outputGradient,
88
+ input,
89
+ learningRate
90
+ ) {
91
+ const grad = Math.max(
92
+ -1,
93
+ Math.min(1, outputGradient[this.thread.y])
94
+ )
119
95
 
120
- // Gradient belonging to this output/weight chunk
121
- const gradientChunk = out.slice(
122
- gradientOffset,
123
- gradientOffset + chunkOutputSize
124
- )
96
+ return weights[this.thread.y][this.thread.x]
97
+ - learningRate * input[this.thread.x] * grad
98
+ }, {
99
+ pipeline: true,
100
+ immutable: true,
101
+ output: [size, splitedWeights.data[a].length]
102
+ })
103
+ }
125
104
 
126
- gradientOffset += chunkOutputSize
127
105
 
128
- // Bias update
129
- const newBias = cacheLBIas[a](
130
- biasChunk,
131
- gradientChunk,
132
- learningRate
133
- )
134
106
 
135
- biasOut.set(newBias, offsetBias)
136
- offsetBias += newBias.length
137
107
 
138
- // Input gradient
139
- const outputGra = cacheLBInputGradent[a](
140
- weightChunk,
141
- gradientChunk
142
- )
108
+ }
109
+ CalculateLinearForward(input) {
110
+ const result = []
111
+ let offset = 0;
112
+ for (let a = 0; a < this.Weights.length; a++) {
113
+ const output = this.cacheLF[a](input, this.Bias[a], this.Weights[a])
114
+ result.push(...output)
143
115
 
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
116
  }
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
-
117
+ return result
160
118
  }
119
+ CalculateBackward(out, learningRate, input) {
120
+ let inputGradient = new Float32Array(input.length).fill(0)
121
+ for (let a = 0; a < this.Weights.length; a++) {
122
+ const outGradient = this.cacheLBInputGradent[a](this.Weights[a], out)
123
+ for (let i = 0; i < outGradient.length; i++) {
124
+ inputGradient[i] += outGradient[i]
125
+ }
126
+ this.Weights[a] = this.cacheLBWeights[a](this.Weights[a], out, input, learningRate)
127
+ this.Bias[a] = this.cacheLBIas[a](this.Bias[a], out, learningRate)
161
128
 
162
- return {
163
- inputGradient,
164
- biasOut,
165
- weightsOut
129
+ }
130
+ return inputGradient
166
131
  }
167
132
  }
@@ -1,6 +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
+ import { GPULinear } from '../GPU.js'
4
4
  function GenerateWeightsBias(embeddingSize, vocabSize) {
5
5
  const vectors = []
6
6
  const Bias = new Float32Array(vocabSize)
@@ -19,6 +19,7 @@ export class Linear {
19
19
  Bias = []
20
20
  configs;
21
21
  input = [];
22
+ gpu;
22
23
  constructor(embeddingSize, vocabSize, configs) {
23
24
  if (configs && configs.save) {
24
25
  this.configs = configs
@@ -39,7 +40,8 @@ export class Linear {
39
40
  this.Weights = vectors;
40
41
  this.Bias = Bias;
41
42
  }
42
- InitKernel(this.Weights, embeddingSize)
43
+ this.gpu = new GPULinear()
44
+ this.gpu.InitKernel(this.Weights, this.Bias, embeddingSize)
43
45
  }
44
46
  Save() {
45
47
  SaveVectors(this.Weights, this.configs.save.filename[0])
@@ -47,14 +49,12 @@ export class Linear {
47
49
  }
48
50
  forward(input) {
49
51
  this.input.push(input)
50
- return CalculateLinearForward(this.Weights, this.Bias, input)
52
+ return this.gpu.CalculateLinearForward(this.Weights, this.Bias, input)
51
53
  }
52
54
  backward(outputGradient, learningRate, idx) {
53
55
  const input = this.input[idx];
54
56
  const weights = this.Weights
55
- const {biasOut,inputGradient, weightsOut} = CalculateBackward(weights, this.Bias, outputGradient, learningRate, input)
56
- this.Weights = weightsOut;
57
- this.Bias = biasOut
57
+ const inputGradient = this.gpu.CalculateBackward(outputGradient, learningRate, input)
58
58
  return inputGradient
59
59
  }
60
60
  ClearInputCache(){