@mineproness/tensorloop 1.4.7 → 1.4.9

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.4.7",
3
+ "version": "1.4.9",
4
4
  "description": "The most powerfull Machine Learning Library for AI.",
5
5
  "keywords": [
6
6
  "ai",
@@ -1,90 +1,30 @@
1
1
  import { SelfAttention } from '../SelfAttention/index.js'
2
2
  import { FFN } from '../FFN/index.js'
3
- import { LayerNorm } from '../LayerNorm/index.js'
4
3
  export class TransformerBlock {
5
- norm1;
6
- norm2;
7
4
  ffn;
8
5
  selfAttention;
9
6
  constructor(embeddingSize, configs) {
10
- this.norm1 = new LayerNorm(embeddingSize)
11
- this.norm2 = new LayerNorm(embeddingSize)
12
7
  this.ffn = new FFN(embeddingSize, "gelu", configs)
13
8
  this.selfAttention = new SelfAttention(embeddingSize, configs)
14
9
  }
15
10
  forward(input) {
16
- const normal1 = []
17
- for (let a = 0; a < input.length; a++) {
18
- normal1[a] = this.norm1.forward(input[a], a)
19
- }
20
11
  const attention = this.selfAttention.forward(normal1)
21
- const residual1 = []
22
- for (let a = 0; a < input.length; a++) {
23
- residual1[a] = new Float32Array(input[0].length)
24
- for (let i = 0; i < input[0].length; i++) {
25
- residual1[a][i] = input[a][i] + attention[a][i]
26
- }
27
- }
28
- const normal2 = []
29
- for (let a = 0; a < input.length; a++) {
30
- normal2[a] = this.norm2.forward(residual1[a], a)
31
- }
32
- const ffnOutput = this.ffn.forward(normal2)
33
- const output = []
34
- for (let g = 0; g < residual1.length; g++) {
35
- output[g] = new Float32Array(residual1[0].length)
36
- for (let a = 0; a < output[0].length; a++) {
37
- output[g][a] = residual1[g][a] + ffnOutput[g][a]
38
- }
39
- }
40
- return output
12
+ return this.ffn.forward(normal2)
41
13
  }
42
14
  backward(dOutput, learningRate) {
43
- const dFFN = this.ffn.backward(dOutput, learningRate)
44
- const dResidual1 = []
45
- for (let a = 0; a < dFFN.length; a++) {
46
- const dNorm2 = this.norm2.backward(
47
- dFFN[a],
48
- learningRate,
49
- a
50
- )
51
- dResidual1[a] = new Float32Array(dOutput[a].length)
52
- for (let i = 0; i < dOutput[a].length; i++) {
53
- dResidual1[a][i] =
54
- dOutput[a][i] + dNorm2[i]
55
- }
56
- }
57
- const dAttention = this.selfAttention.backward(
58
- dResidual1,
15
+ const dAttention =
16
+ this.ffn.backward(dOutput, learningRate)
17
+
18
+ return this.selfAttention.backward(
19
+ dAttention,
59
20
  learningRate
60
21
  )
61
- const dInputFromAttention = []
62
- for (let a = 0; a < dAttention.length; a++) {
63
- dInputFromAttention[a] = this.norm1.backward(
64
- dAttention[a],
65
- learningRate,
66
- a
67
- )
68
- }
69
- const dInput = []
70
- for (let a = 0; a < dOutput.length; a++) {
71
- dInput[a] = new Float32Array(dOutput[a].length)
72
- for (let i = 0; i < dOutput[a].length; i++) {
73
- dInput[a][i] =
74
- dOutput[a][i] +
75
- dInputFromAttention[a][i]
76
- }
77
- }
78
-
79
- return dInput
80
22
  }
81
23
  clearInputCache() {
82
24
  this.ffn.ClearInputCache()
83
25
  this.selfAttention.key.ClearInputCache()
84
26
  this.selfAttention.query.ClearInputCache()
85
27
  this.selfAttention.value.ClearInputCache()
86
- this.norm1.ClearInputCache()
87
- this.norm2.ClearInputCache()
88
28
  }
89
29
  Save() {
90
30
  this.selfAttention.Save()
@@ -1,22 +0,0 @@
1
- export class LayerNorm {
2
- gamma: Float32Array;
3
- beta: Float32Array;
4
- eps: number;
5
- cache: Array<{
6
- input: Float32Array;
7
- mean: number;
8
- invStd: number;
9
- }>;
10
-
11
- constructor(embeddingSize: number, eps?: number);
12
-
13
- forward(input: Float32Array, index?: number): Float32Array;
14
-
15
- backward(
16
- dOutput: Float32Array,
17
- learningRate: number,
18
- index?: number
19
- ): Float32Array;
20
-
21
- ClearInputCache(): void;
22
- }
@@ -1,98 +0,0 @@
1
- export class LayerNorm {
2
- gamma;
3
- beta;
4
- eps;
5
- cache;
6
-
7
- constructor(embeddingSize, eps = 1e-5) {
8
- this.gamma = new Float32Array(embeddingSize);
9
- this.beta = new Float32Array(embeddingSize);
10
-
11
- this.gamma.fill(1);
12
- this.beta.fill(0);
13
-
14
- this.eps = eps;
15
- this.cache = [];
16
- }
17
-
18
- forward(input, index = 0) {
19
- const size = input.length;
20
- let mean = 0;
21
- for (let i = 0; i < size; i++) {
22
- mean += input[i];
23
- }
24
- mean /= size;
25
- let variance = 0;
26
- for (let i = 0; i < size; i++) {
27
- const diff = input[i] - mean;
28
- variance += diff * diff;
29
- }
30
- variance /= size;
31
- const invStd = 1 / Math.sqrt(variance + this.eps);
32
- const output = new Float32Array(size);
33
- for (let i = 0; i < size; i++) {
34
- const normalized = (input[i] - mean) * invStd;
35
- output[i] = normalized * this.gamma[i] + this.beta[i];
36
- }
37
- this.cache[index] = {
38
- input: new Float32Array(input),
39
- mean,
40
- invStd
41
- };
42
- return output;
43
- }
44
-
45
- backward(dOutput, learningRate, index = 0) {
46
- const cache = this.cache[index];
47
- const input = cache.input;
48
- const mean = cache.mean;
49
- const invStd = cache.invStd;
50
- const size = input.length;
51
- const dInput = new Float32Array(size);
52
- const normalized = new Float32Array(size);
53
- let sumDNorm = 0;
54
- let sumDNormX = 0;
55
- for (let i = 0; i < size; i++) {
56
- normalized[i] =
57
- (input[i] - mean) * invStd;
58
- }
59
- for (let i = 0; i < size; i++) {
60
- const dNorm =
61
- dOutput[i] * this.gamma[i];
62
-
63
- sumDNorm += dNorm;
64
- sumDNormX +=
65
- dNorm * normalized[i];
66
- }
67
- for (let i = 0; i < size; i++) {
68
- const dNorm =
69
- dOutput[i] * this.gamma[i];
70
- dInput[i] =
71
- (invStd / size) *
72
- (
73
- size * dNorm -
74
- sumDNorm -
75
- normalized[i] * sumDNormX
76
- );
77
- }
78
- for (let i = 0; i < size; i++) {
79
- const dGamma =
80
- dOutput[i] * normalized[i];
81
-
82
- const dBeta =
83
- dOutput[i];
84
-
85
- this.gamma[i] -=
86
- learningRate * dGamma;
87
-
88
- this.beta[i] -=
89
- learningRate * dBeta;
90
- }
91
-
92
- return dInput;
93
- }
94
-
95
- ClearInputCache() {
96
- this.cache = [];
97
- }
98
- }