@mineproness/tensorloop 1.4.7 → 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/TransformerBlock/index.js +28 -48
package/package.json
CHANGED
|
@@ -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
|