@mineproness/tensorloop 1.4.8 → 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 +1 -1
- package/src/TransformerBlock/index.js +5 -45
- package/src/LayerNorm/index.d.ts +0 -22
- package/src/LayerNorm/index.js +0 -98
package/package.json
CHANGED
|
@@ -1,70 +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 normal2 = []
|
|
22
|
-
for (let a = 0; a < attention.length; a++) {
|
|
23
|
-
normal2[a] = this.norm2.forward(attention[a], a)
|
|
24
|
-
}
|
|
25
12
|
return this.ffn.forward(normal2)
|
|
26
13
|
}
|
|
27
14
|
backward(dOutput, learningRate) {
|
|
28
|
-
const dFFN =
|
|
29
|
-
this.ffn.backward(dOutput, learningRate)
|
|
30
|
-
|
|
31
|
-
const dAttentionInput = []
|
|
32
|
-
|
|
33
|
-
for (let a = 0; a < dFFN.length; a++) {
|
|
34
|
-
dAttentionInput[a] =
|
|
35
|
-
this.norm2.backward(
|
|
36
|
-
dFFN[a],
|
|
37
|
-
learningRate,
|
|
38
|
-
a
|
|
39
|
-
)
|
|
40
|
-
}
|
|
41
|
-
|
|
42
15
|
const dAttention =
|
|
43
|
-
this.
|
|
44
|
-
dAttentionInput,
|
|
45
|
-
learningRate
|
|
46
|
-
)
|
|
47
|
-
|
|
48
|
-
const dInput = []
|
|
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
|
-
)
|
|
57
|
-
}
|
|
16
|
+
this.ffn.backward(dOutput, learningRate)
|
|
58
17
|
|
|
59
|
-
return
|
|
18
|
+
return this.selfAttention.backward(
|
|
19
|
+
dAttention,
|
|
20
|
+
learningRate
|
|
21
|
+
)
|
|
60
22
|
}
|
|
61
23
|
clearInputCache() {
|
|
62
24
|
this.ffn.ClearInputCache()
|
|
63
25
|
this.selfAttention.key.ClearInputCache()
|
|
64
26
|
this.selfAttention.query.ClearInputCache()
|
|
65
27
|
this.selfAttention.value.ClearInputCache()
|
|
66
|
-
this.norm1.ClearInputCache()
|
|
67
|
-
this.norm2.ClearInputCache()
|
|
68
28
|
}
|
|
69
29
|
Save() {
|
|
70
30
|
this.selfAttention.Save()
|
package/src/LayerNorm/index.d.ts
DELETED
|
@@ -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
|
-
}
|
package/src/LayerNorm/index.js
DELETED
|
@@ -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
|
-
}
|