@mineproness/tensorloop 1.4.6 → 1.4.8
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 +1 -1
- package/src/LayerNorm/index.js +26 -10
- package/src/TransformerBlock/index.js +28 -48
package/package.json
CHANGED
package/src/LayerNorm/index.js
CHANGED
|
@@ -49,22 +49,24 @@ export class LayerNorm {
|
|
|
49
49
|
const invStd = cache.invStd;
|
|
50
50
|
const size = input.length;
|
|
51
51
|
const dInput = new Float32Array(size);
|
|
52
|
+
const normalized = new Float32Array(size);
|
|
52
53
|
let sumDNorm = 0;
|
|
53
54
|
let sumDNormX = 0;
|
|
54
|
-
const normalized = new Float32Array(size);
|
|
55
55
|
for (let i = 0; i < size; i++) {
|
|
56
|
-
normalized[i] =
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
const
|
|
61
|
-
|
|
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
62
|
|
|
63
|
-
|
|
64
|
-
|
|
63
|
+
sumDNorm += dNorm;
|
|
64
|
+
sumDNormX +=
|
|
65
|
+
dNorm * normalized[i];
|
|
65
66
|
}
|
|
66
67
|
for (let i = 0; i < size; i++) {
|
|
67
|
-
const dNorm =
|
|
68
|
+
const dNorm =
|
|
69
|
+
dOutput[i] * this.gamma[i];
|
|
68
70
|
dInput[i] =
|
|
69
71
|
(invStd / size) *
|
|
70
72
|
(
|
|
@@ -73,6 +75,20 @@ export class LayerNorm {
|
|
|
73
75
|
normalized[i] * sumDNormX
|
|
74
76
|
);
|
|
75
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
|
+
|
|
76
92
|
return dInput;
|
|
77
93
|
}
|
|
78
94
|
|
|
@@ -18,62 +18,42 @@ export class TransformerBlock {
|
|
|
18
18
|
normal1[a] = this.norm1.forward(input[a], a)
|
|
19
19
|
}
|
|
20
20
|
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
21
|
const normal2 = []
|
|
29
|
-
for (let a = 0; a <
|
|
30
|
-
normal2[a] = this.norm2.forward(
|
|
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
|
-
}
|
|
22
|
+
for (let a = 0; a < attention.length; a++) {
|
|
23
|
+
normal2[a] = this.norm2.forward(attention[a], a)
|
|
39
24
|
}
|
|
40
|
-
return
|
|
25
|
+
return this.ffn.forward(normal2)
|
|
41
26
|
}
|
|
42
27
|
backward(dOutput, learningRate) {
|
|
43
|
-
const dFFN =
|
|
44
|
-
|
|
28
|
+
const dFFN =
|
|
29
|
+
this.ffn.backward(dOutput, learningRate)
|
|
30
|
+
|
|
31
|
+
const dAttentionInput = []
|
|
32
|
+
|
|
45
33
|
for (let a = 0; a < dFFN.length; a++) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
for (let i = 0; i < dOutput[a].length; i++) {
|
|
53
|
-
dResidual1[a][i] =
|
|
54
|
-
dOutput[a][i] + dNorm2[i]
|
|
55
|
-
}
|
|
34
|
+
dAttentionInput[a] =
|
|
35
|
+
this.norm2.backward(
|
|
36
|
+
dFFN[a],
|
|
37
|
+
learningRate,
|
|
38
|
+
a
|
|
39
|
+
)
|
|
56
40
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
for (let a = 0; a < dAttention.length; a++) {
|
|
63
|
-
dInputFromAttention[a] = this.norm1.backward(
|
|
64
|
-
dAttention[a],
|
|
65
|
-
learningRate,
|
|
66
|
-
a
|
|
41
|
+
|
|
42
|
+
const dAttention =
|
|
43
|
+
this.selfAttention.backward(
|
|
44
|
+
dAttentionInput,
|
|
45
|
+
learningRate
|
|
67
46
|
)
|
|
68
|
-
|
|
47
|
+
|
|
69
48
|
const dInput = []
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
49
|
+
|
|
50
|
+
for (let a = 0; a < dAttention.length; a++) {
|
|
51
|
+
dInput[a] =
|
|
52
|
+
this.norm1.backward(
|
|
53
|
+
dAttention[a],
|
|
54
|
+
learningRate,
|
|
55
|
+
a
|
|
56
|
+
)
|
|
77
57
|
}
|
|
78
58
|
|
|
79
59
|
return dInput
|