@mineproness/tensorloop 1.3.4 → 1.3.7
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/index.js +3 -1
- package/package.json +1 -1
- package/readme.md +1 -1
- package/src/GELU/index.d.ts +6 -0
- package/src/GELU/index.js +43 -0
- package/src/SelfAttention/index.d.ts +1 -2
package/index.js
CHANGED
|
@@ -7,6 +7,7 @@ import {tools} from './src/tools/index.js'
|
|
|
7
7
|
import {ReLU} from './src/ReLU/index.js'
|
|
8
8
|
import {PositionEmbedding as PE} from './src/PositionEmbedding/index.js'
|
|
9
9
|
import {SelfAttention as ST} from './src/SelfAttention/index.js'
|
|
10
|
+
import {GELU as GU} from './src/GELU/index.js'
|
|
10
11
|
export const Tokenizer = tokenizer
|
|
11
12
|
export const Embedding = embeddding2
|
|
12
13
|
export const Linear = li2
|
|
@@ -15,4 +16,5 @@ export const CrossEntropy = crossEntropy
|
|
|
15
16
|
export const Tools = tools
|
|
16
17
|
export const ReLu = ReLU
|
|
17
18
|
export const PositionEmbedding = PE
|
|
18
|
-
export const SelfAttention = ST
|
|
19
|
+
export const SelfAttention = ST
|
|
20
|
+
export const GELU = GU
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -8,7 +8,7 @@ A Library that Supply AI Tools and Large Language model Development tools like E
|
|
|
8
8
|
- Linear Weights and Bias.
|
|
9
9
|
- Softmax forward and Backward.
|
|
10
10
|
- CrossEntroy for Loss Calculation.
|
|
11
|
-
- Relu for Stable Layer.
|
|
11
|
+
- Relu and GELU for Stable Layer.
|
|
12
12
|
- Tokenizer for convert into tokens.
|
|
13
13
|
- Self attention
|
|
14
14
|
- Position Embedding
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export default class GELU {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.inputs = []
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
forward(input, index = 0) {
|
|
7
|
+
this.inputs[index] = new Float32Array(input)
|
|
8
|
+
const output = new Float32Array(input.length)
|
|
9
|
+
const k = Math.sqrt(2 / Math.PI)
|
|
10
|
+
for (let i = 0; i < input.length; i++) {
|
|
11
|
+
const x = input[i]
|
|
12
|
+
const u = k * (x + 0.044715 * x * x * x)
|
|
13
|
+
output[i] = 0.5 * x * (1 + Math.tanh(u))
|
|
14
|
+
}
|
|
15
|
+
return output
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
backward(dOutput, index = 0) {
|
|
19
|
+
const input = this.inputs[index]
|
|
20
|
+
if (!input) throw new Error(`No GELU cache for index ${index}`);
|
|
21
|
+
const grad = new Float32Array(dOutput.length)
|
|
22
|
+
const k = Math.sqrt(2 / Math.PI)
|
|
23
|
+
for (let i = 0; i < dOutput.length; i++) {
|
|
24
|
+
const x = input[i]
|
|
25
|
+
const x3 = x * x * x
|
|
26
|
+
const u = k * (x + 0.044715 * x3)
|
|
27
|
+
const t = Math.tanh(u)
|
|
28
|
+
const sech2 = 1 - t * t
|
|
29
|
+
const duDx = k * (1 + 3 * 0.044715 * x * x)
|
|
30
|
+
const geluGrad =
|
|
31
|
+
0.5 * (1 + t) +
|
|
32
|
+
0.5 * x * sech2 * duDx
|
|
33
|
+
|
|
34
|
+
grad[i] = dOutput[i] * geluGrad
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return grad
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
ClearInputCache() {
|
|
41
|
+
this.inputs = []
|
|
42
|
+
}
|
|
43
|
+
}
|