@mineproness/tensorloop 1.5.3 → 1.5.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.
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.3",
3
+ "version": "1.5.5",
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,159 @@
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
+ export class GPULinear {
22
+ cacheLF = [];
23
+ cacheLBIas = [];
24
+ cacheLBWeights = [];
25
+ cacheLBInputGradent = [];
26
+ InitKernel(weights, size) {
27
+ const splitedWeights = splitMatrix(weights)
28
+ for (let a = 0; a < splitedWeights.data.length; a++) {
29
+ this.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][0].length
37
+ }).setArgumentTypes({
38
+ input: 'Array',
39
+ bias: 'Array',
40
+ weights: 'Array'
41
+ }).setOutput([splitedWeights.data[a].length])
42
+ this.cacheLBIas[a] = gpu.createKernel(function (
43
+ bias,
44
+ outputGradient,
45
+ learningRate
46
+ ) {
47
+ const grad = Math.max(
48
+ -1,
49
+ Math.min(1, outputGradient[this.thread.x])
50
+ )
51
+
52
+ return bias[this.thread.x] - learningRate * grad
53
+ })
54
+ .setOutput([splitedWeights.data[a].length])
55
+ this.cacheLBInputGradent[a] = gpu.createKernel(function (
56
+ weights,
57
+ outputGradient
58
+ ) {
59
+ let sum = 0
60
+
61
+ for (let a = 0; a < this.constants.outputSize; a++) {
62
+ const grad = Math.max(
63
+ -1,
64
+ Math.min(1, outputGradient[a])
65
+ )
66
+
67
+ sum += grad * weights[a][this.thread.x]
68
+ }
69
+
70
+ return sum
71
+ })
72
+ .setConstants({
73
+ outputSize: splitedWeights.data[a].length
74
+ })
75
+ .setOutput([size])
76
+ this.cacheLBWeights[a] = gpu.createKernel(function (
77
+ weights,
78
+ outputGradient,
79
+ input,
80
+ learningRate
81
+ ) {
82
+ const grad = Math.max(
83
+ -1,
84
+ Math.min(1, outputGradient[this.thread.y])
85
+ )
86
+
87
+ return weights[this.thread.y][this.thread.x]
88
+ - learningRate * input[this.thread.x] * grad
89
+ })
90
+ .setOutput([size, splitedWeights.data[a].length])
91
+ }
92
+
93
+
94
+
95
+
96
+ }
97
+ CalculateLinearForward(weights, bias, input) {
98
+ // console.log(input)
99
+ const splitedBias = splitMatrix(bias)
100
+ const splitedWeights = splitMatrix(weights)
101
+ const result = new Float32Array(weights.length)
102
+ let offset = 0;
103
+ for (let a = 0; a < splitedBias.data.length; a++) {
104
+ const output = this.cacheLF[a](input, splitedBias.data[a], splitedWeights.data[a])
105
+ result.set(output, offset)
106
+ offset += output.length
107
+ }
108
+ return result
109
+ }
110
+ CalculateBackward(weights, bias, out, learningRate, input) {
111
+ const splitedBias = splitMatrix(bias)
112
+ const splitedWeights = splitMatrix(weights)
113
+ const inputGradient = new Float32Array(input.length)
114
+ const biasOut = new Float32Array(bias.length)
115
+ const weightsOut = []
116
+ let offsetBias = 0
117
+ let offsetWeights = 0
118
+ let gradientOffset = 0
119
+
120
+ for (let a = 0; a < splitedWeights.data.length; a++) {
121
+ const weightChunk = splitedWeights.data[a]
122
+ const biasChunk = splitedBias.data[a]
123
+ const chunkOutputSize = weightChunk.length
124
+ const gradientChunk = out.slice(
125
+ gradientOffset,
126
+ gradientOffset + chunkOutputSize
127
+ )
128
+ gradientOffset += chunkOutputSize
129
+ const newBias = this.cacheLBIas[a](
130
+ biasChunk,
131
+ gradientChunk,
132
+ learningRate
133
+ )
134
+ biasOut.set(newBias, offsetBias)
135
+ offsetBias += newBias.length
136
+ const outputGra = this.cacheLBInputGradent[a](
137
+ weightChunk,
138
+ gradientChunk
139
+ )
140
+ for (let i = 0; i < outputGra.length; i++) {
141
+ inputGradient[i] += outputGra[i]
142
+ }
143
+ const newWeights = this.cacheLBWeights[a](
144
+ weightChunk,
145
+ gradientChunk,
146
+ input,
147
+ learningRate
148
+ )
149
+ weightsOut.push(...newWeights)
150
+
151
+ }
152
+
153
+ return {
154
+ inputGradient,
155
+ biasOut,
156
+ weightsOut
157
+ }
158
+ }
159
+ }
@@ -1,5 +1,6 @@
1
1
  import { LoadBias, LoadVectors, SaveBias, SaveVectors } from '../GetConfigs.js'
2
2
  import { existsSync } from 'fs'
3
+ import { GPULinear } from '../GPU.js'
3
4
  function GenerateWeightsBias(embeddingSize, vocabSize) {
4
5
  const vectors = []
5
6
  const Bias = new Float32Array(vocabSize)
@@ -18,6 +19,7 @@ export class Linear {
18
19
  Bias = []
19
20
  configs;
20
21
  input = [];
22
+ gpu;
21
23
  constructor(embeddingSize, vocabSize, configs) {
22
24
  if (configs && configs.save) {
23
25
  this.configs = configs
@@ -38,6 +40,8 @@ export class Linear {
38
40
  this.Weights = vectors;
39
41
  this.Bias = Bias;
40
42
  }
43
+ this.gpu = new GPULinear()
44
+ this.gpu.InitKernel(this.Weights, embeddingSize)
41
45
  }
42
46
  Save() {
43
47
  SaveVectors(this.Weights, this.configs.save.filename[0])
@@ -45,29 +49,14 @@ export class Linear {
45
49
  }
46
50
  forward(input) {
47
51
  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
52
+ return this.gpu.CalculateLinearForward(this.Weights, this.Bias, input)
57
53
  }
58
54
  backward(outputGradient, learningRate, idx) {
59
55
  const input = this.input[idx];
60
56
  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
- }
57
+ const {biasOut,inputGradient, weightsOut} = this.gpu.CalculateBackward(weights, this.Bias, outputGradient, learningRate, input)
58
+ this.Weights = weightsOut;
59
+ this.Bias = biasOut
71
60
  return inputGradient
72
61
  }
73
62
  ClearInputCache(){