@mineproness/tensorloop 1.5.4 → 1.5.6
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/model/ffnBias.bin +0 -0
- package/model/ffnBias1.bin +0 -0
- package/model/ffnWeights.bin +0 -0
- package/model/ffnWeights1.bin +0 -0
- package/package.json +1 -1
- package/src/GPU.js +96 -131
- package/src/Linear/index.js +6 -6
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/package.json
CHANGED
package/src/GPU.js
CHANGED
|
@@ -18,150 +18,115 @@ export function splitMatrix(matrix, chunksSizes = gl.getParameter(gl.MAX_TEXTURE
|
|
|
18
18
|
return { split: true, data: chunks }
|
|
19
19
|
|
|
20
20
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
outputGradient
|
|
54
|
-
) {
|
|
55
|
-
let sum = 0
|
|
56
|
-
|
|
57
|
-
for (let a = 0; a < this.constants.outputSize; a++) {
|
|
21
|
+
export class GPULinear {
|
|
22
|
+
cacheLF = [];
|
|
23
|
+
cacheLBIas = [];
|
|
24
|
+
cacheLBWeights = [];
|
|
25
|
+
cacheLBInputGradent = [];
|
|
26
|
+
// texures
|
|
27
|
+
Bias = [];
|
|
28
|
+
Weights = []
|
|
29
|
+
InitKernel(weights, bias, size) {
|
|
30
|
+
const splitedWeights = splitMatrix(weights)
|
|
31
|
+
const splitedBias = splitMatrix(bias)
|
|
32
|
+
this.Weights = splitedWeights.data
|
|
33
|
+
this.Bias = splitedBias.data
|
|
34
|
+
for (let a = 0; a < splitedWeights.data.length; a++) {
|
|
35
|
+
this.cacheLF[a] = gpu.createKernel(function (input, bias, weights) {
|
|
36
|
+
let sum = bias[this.thread.x]
|
|
37
|
+
for (let a = 0; a < this.constants.inputLength; a++) {
|
|
38
|
+
sum += input[a] * weights[this.thread.x][a]
|
|
39
|
+
}
|
|
40
|
+
return sum
|
|
41
|
+
}).setConstants({
|
|
42
|
+
inputLength: splitedWeights.data[a][0].length
|
|
43
|
+
}).setArgumentTypes({
|
|
44
|
+
input: 'Array',
|
|
45
|
+
bias: 'Array',
|
|
46
|
+
weights: 'Array'
|
|
47
|
+
}).setOutput([splitedWeights.data[a].length])
|
|
48
|
+
this.cacheLBIas[a] = gpu.createKernel(function (
|
|
49
|
+
bias,
|
|
50
|
+
outputGradient,
|
|
51
|
+
learningRate
|
|
52
|
+
) {
|
|
58
53
|
const grad = Math.max(
|
|
59
54
|
-1,
|
|
60
|
-
Math.min(1, outputGradient[
|
|
55
|
+
Math.min(1, outputGradient[this.thread.x])
|
|
61
56
|
)
|
|
62
57
|
|
|
63
|
-
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
.setConstants({
|
|
69
|
-
outputSize: splitedWeights.data[a].length
|
|
58
|
+
return bias[this.thread.x] - learningRate * grad
|
|
59
|
+
}, {
|
|
60
|
+
pipeline: true,
|
|
61
|
+
immutable: true,
|
|
62
|
+
output: [splitedWeights.data[a].length]
|
|
70
63
|
})
|
|
71
|
-
.
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export function CalculateBackward(weights, bias, out, learningRate, input) {
|
|
105
|
-
const splitedBias = splitMatrix(bias)
|
|
106
|
-
const splitedWeights = splitMatrix(weights)
|
|
107
|
-
const inputGradient = new Float32Array(input.length)
|
|
108
|
-
const biasOut = new Float32Array(bias.length)
|
|
109
|
-
const weightsOut = []
|
|
110
|
-
let offsetBias = 0
|
|
111
|
-
let offsetWeights = 0
|
|
112
|
-
let gradientOffset = 0
|
|
113
|
-
|
|
114
|
-
for (let a = 0; a < splitedWeights.data.length; a++) {
|
|
115
|
-
const weightChunk = splitedWeights.data[a]
|
|
116
|
-
const biasChunk = splitedBias.data[a]
|
|
117
|
-
|
|
118
|
-
const chunkOutputSize = weightChunk.length
|
|
64
|
+
this.cacheLBInputGradent[a] = gpu.createKernel(function (
|
|
65
|
+
weights,
|
|
66
|
+
outputGradient
|
|
67
|
+
) {
|
|
68
|
+
let sum = 0
|
|
69
|
+
|
|
70
|
+
for (let a = 0; a < this.constants.outputSize; a++) {
|
|
71
|
+
const grad = Math.max(
|
|
72
|
+
-1,
|
|
73
|
+
Math.min(1, outputGradient[a])
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
sum += grad * weights[a][this.thread.x]
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return sum
|
|
80
|
+
}, {
|
|
81
|
+
constants: { outputSize: splitedWeights.data[a].length },
|
|
82
|
+
output: [size]
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
this.cacheLBWeights[a] = gpu.createKernel(function (
|
|
86
|
+
weights,
|
|
87
|
+
outputGradient,
|
|
88
|
+
input,
|
|
89
|
+
learningRate
|
|
90
|
+
) {
|
|
91
|
+
const grad = Math.max(
|
|
92
|
+
-1,
|
|
93
|
+
Math.min(1, outputGradient[this.thread.y])
|
|
94
|
+
)
|
|
119
95
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
96
|
+
return weights[this.thread.y][this.thread.x]
|
|
97
|
+
- learningRate * input[this.thread.x] * grad
|
|
98
|
+
}, {
|
|
99
|
+
pipeline: true,
|
|
100
|
+
immutable: true,
|
|
101
|
+
output: [size, splitedWeights.data[a].length]
|
|
102
|
+
})
|
|
103
|
+
}
|
|
125
104
|
|
|
126
|
-
gradientOffset += chunkOutputSize
|
|
127
105
|
|
|
128
|
-
// Bias update
|
|
129
|
-
const newBias = cacheLBIas[a](
|
|
130
|
-
biasChunk,
|
|
131
|
-
gradientChunk,
|
|
132
|
-
learningRate
|
|
133
|
-
)
|
|
134
106
|
|
|
135
|
-
biasOut.set(newBias, offsetBias)
|
|
136
|
-
offsetBias += newBias.length
|
|
137
107
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
)
|
|
108
|
+
}
|
|
109
|
+
CalculateLinearForward(input) {
|
|
110
|
+
const result = []
|
|
111
|
+
let offset = 0;
|
|
112
|
+
for (let a = 0; a < this.Weights.length; a++) {
|
|
113
|
+
const output = this.cacheLF[a](input, this.Bias[a], this.Weights[a])
|
|
114
|
+
result.push(...output)
|
|
143
115
|
|
|
144
|
-
// ADD this chunk's gradient to the same input gradient
|
|
145
|
-
for (let i = 0; i < outputGra.length; i++) {
|
|
146
|
-
inputGradient[i] += outputGra[i]
|
|
147
116
|
}
|
|
148
|
-
|
|
149
|
-
// Weight update
|
|
150
|
-
const newWeights = cacheLBWeights[a](
|
|
151
|
-
weightChunk,
|
|
152
|
-
gradientChunk,
|
|
153
|
-
input,
|
|
154
|
-
learningRate
|
|
155
|
-
)
|
|
156
|
-
|
|
157
|
-
// Copy chunk into final Float32Array
|
|
158
|
-
weightsOut.push(...newWeights)
|
|
159
|
-
|
|
117
|
+
return result
|
|
160
118
|
}
|
|
119
|
+
CalculateBackward(out, learningRate, input) {
|
|
120
|
+
let inputGradient = new Float32Array(input.length).fill(0)
|
|
121
|
+
for (let a = 0; a < this.Weights.length; a++) {
|
|
122
|
+
const outGradient = this.cacheLBInputGradent[a](this.Weights[a], out)
|
|
123
|
+
for (let i = 0; i < outGradient.length; i++) {
|
|
124
|
+
inputGradient[i] += outGradient[i]
|
|
125
|
+
}
|
|
126
|
+
this.Weights[a] = this.cacheLBWeights[a](this.Weights[a], out, input, learningRate)
|
|
127
|
+
this.Bias[a] = this.cacheLBIas[a](this.Bias[a], out, learningRate)
|
|
161
128
|
|
|
162
|
-
|
|
163
|
-
inputGradient
|
|
164
|
-
biasOut,
|
|
165
|
-
weightsOut
|
|
129
|
+
}
|
|
130
|
+
return inputGradient
|
|
166
131
|
}
|
|
167
132
|
}
|
package/src/Linear/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { LoadBias, LoadVectors, SaveBias, SaveVectors } from '../GetConfigs.js'
|
|
2
2
|
import { existsSync } from 'fs'
|
|
3
|
-
import {
|
|
3
|
+
import { GPULinear } from '../GPU.js'
|
|
4
4
|
function GenerateWeightsBias(embeddingSize, vocabSize) {
|
|
5
5
|
const vectors = []
|
|
6
6
|
const Bias = new Float32Array(vocabSize)
|
|
@@ -19,6 +19,7 @@ export class Linear {
|
|
|
19
19
|
Bias = []
|
|
20
20
|
configs;
|
|
21
21
|
input = [];
|
|
22
|
+
gpu;
|
|
22
23
|
constructor(embeddingSize, vocabSize, configs) {
|
|
23
24
|
if (configs && configs.save) {
|
|
24
25
|
this.configs = configs
|
|
@@ -39,7 +40,8 @@ export class Linear {
|
|
|
39
40
|
this.Weights = vectors;
|
|
40
41
|
this.Bias = Bias;
|
|
41
42
|
}
|
|
42
|
-
|
|
43
|
+
this.gpu = new GPULinear()
|
|
44
|
+
this.gpu.InitKernel(this.Weights, this.Bias, embeddingSize)
|
|
43
45
|
}
|
|
44
46
|
Save() {
|
|
45
47
|
SaveVectors(this.Weights, this.configs.save.filename[0])
|
|
@@ -47,14 +49,12 @@ export class Linear {
|
|
|
47
49
|
}
|
|
48
50
|
forward(input) {
|
|
49
51
|
this.input.push(input)
|
|
50
|
-
return CalculateLinearForward(this.Weights, this.Bias, input)
|
|
52
|
+
return this.gpu.CalculateLinearForward(this.Weights, this.Bias, input)
|
|
51
53
|
}
|
|
52
54
|
backward(outputGradient, learningRate, idx) {
|
|
53
55
|
const input = this.input[idx];
|
|
54
56
|
const weights = this.Weights
|
|
55
|
-
const
|
|
56
|
-
this.Weights = weightsOut;
|
|
57
|
-
this.Bias = biasOut
|
|
57
|
+
const inputGradient = this.gpu.CalculateBackward(outputGradient, learningRate, input)
|
|
58
58
|
return inputGradient
|
|
59
59
|
}
|
|
60
60
|
ClearInputCache(){
|