@mineproness/tensorloop 1.5.4 → 1.5.5
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 +125 -133
- package/src/Linear/index.js +6 -4
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/package.json
CHANGED
package/src/GPU.js
CHANGED
|
@@ -18,150 +18,142 @@ 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
|
-
return bias[this.thread.x] - learningRate * grad
|
|
49
|
-
})
|
|
50
|
-
.setOutput([splitedWeights.data[a].length])
|
|
51
|
-
cacheLBInputGradent[a] = gpu.createKernel(function (
|
|
52
|
-
weights,
|
|
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
|
+
InitKernel(weights, size) {
|
|
27
|
+
const splitedWeights = splitMatrix(weights)
|
|
28
|
+
for (let a = 0; a < splitedWeights.data.length; a++) {
|
|
29
|
+
this.cacheLF[a] = gpu.createKernel(function (input, bias, weights) {
|
|
30
|
+
let sum = bias[this.thread.x]
|
|
31
|
+
for (let a = 0; a < this.constants.inputLength; a++) {
|
|
32
|
+
sum += input[a] * weights[this.thread.x][a]
|
|
33
|
+
}
|
|
34
|
+
return sum
|
|
35
|
+
}).setConstants({
|
|
36
|
+
inputLength: splitedWeights.data[a][0].length
|
|
37
|
+
}).setArgumentTypes({
|
|
38
|
+
input: 'Array',
|
|
39
|
+
bias: 'Array',
|
|
40
|
+
weights: 'Array'
|
|
41
|
+
}).setOutput([splitedWeights.data[a].length])
|
|
42
|
+
this.cacheLBIas[a] = gpu.createKernel(function (
|
|
43
|
+
bias,
|
|
44
|
+
outputGradient,
|
|
45
|
+
learningRate
|
|
46
|
+
) {
|
|
58
47
|
const grad = Math.max(
|
|
59
48
|
-1,
|
|
60
|
-
Math.min(1, outputGradient[
|
|
49
|
+
Math.min(1, outputGradient[this.thread.x])
|
|
61
50
|
)
|
|
62
51
|
|
|
63
|
-
|
|
64
|
-
}
|
|
52
|
+
return bias[this.thread.x] - learningRate * grad
|
|
53
|
+
})
|
|
54
|
+
.setOutput([splitedWeights.data[a].length])
|
|
55
|
+
this.cacheLBInputGradent[a] = gpu.createKernel(function (
|
|
56
|
+
weights,
|
|
57
|
+
outputGradient
|
|
58
|
+
) {
|
|
59
|
+
let sum = 0
|
|
60
|
+
|
|
61
|
+
for (let a = 0; a < this.constants.outputSize; a++) {
|
|
62
|
+
const grad = Math.max(
|
|
63
|
+
-1,
|
|
64
|
+
Math.min(1, outputGradient[a])
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
sum += grad * weights[a][this.thread.x]
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return sum
|
|
71
|
+
})
|
|
72
|
+
.setConstants({
|
|
73
|
+
outputSize: splitedWeights.data[a].length
|
|
74
|
+
})
|
|
75
|
+
.setOutput([size])
|
|
76
|
+
this.cacheLBWeights[a] = gpu.createKernel(function (
|
|
77
|
+
weights,
|
|
78
|
+
outputGradient,
|
|
79
|
+
input,
|
|
80
|
+
learningRate
|
|
81
|
+
) {
|
|
82
|
+
const grad = Math.max(
|
|
83
|
+
-1,
|
|
84
|
+
Math.min(1, outputGradient[this.thread.y])
|
|
85
|
+
)
|
|
65
86
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
.setConstants({
|
|
69
|
-
outputSize: splitedWeights.data[a].length
|
|
87
|
+
return weights[this.thread.y][this.thread.x]
|
|
88
|
+
- learningRate * input[this.thread.x] * grad
|
|
70
89
|
})
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
weights,
|
|
74
|
-
outputGradient,
|
|
75
|
-
input,
|
|
76
|
-
learningRate
|
|
77
|
-
) {
|
|
78
|
-
const grad = Math.max(
|
|
79
|
-
-1,
|
|
80
|
-
Math.min(1, outputGradient[this.thread.y])
|
|
81
|
-
)
|
|
82
|
-
|
|
83
|
-
return weights[this.thread.y][this.thread.x]
|
|
84
|
-
- learningRate * input[this.thread.x] * grad
|
|
85
|
-
})
|
|
86
|
-
.setOutput([size, splitedWeights.data[a].length])
|
|
87
|
-
}
|
|
90
|
+
.setOutput([size, splitedWeights.data[a].length])
|
|
91
|
+
}
|
|
88
92
|
|
|
89
93
|
|
|
90
|
-
}
|
|
91
|
-
export function CalculateLinearForward(weights, bias, input) {
|
|
92
|
-
const splitedBias = splitMatrix(bias)
|
|
93
|
-
const splitedWeights = splitMatrix(weights)
|
|
94
|
-
const result = new Float32Array(weights.length)
|
|
95
|
-
let offset = 0;
|
|
96
|
-
for (let a = 0; a < splitedBias.data.length; a++) {
|
|
97
|
-
const output = cacheLF[a](input, splitedBias.data[a], splitedWeights.data[a])
|
|
98
|
-
result.set(output, offset)
|
|
99
|
-
offset += output.length
|
|
100
|
-
}
|
|
101
|
-
return result
|
|
102
|
-
}
|
|
103
94
|
|
|
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
|
|
119
|
-
|
|
120
|
-
// Gradient belonging to this output/weight chunk
|
|
121
|
-
const gradientChunk = out.slice(
|
|
122
|
-
gradientOffset,
|
|
123
|
-
gradientOffset + chunkOutputSize
|
|
124
|
-
)
|
|
125
|
-
|
|
126
|
-
gradientOffset += chunkOutputSize
|
|
127
|
-
|
|
128
|
-
// Bias update
|
|
129
|
-
const newBias = cacheLBIas[a](
|
|
130
|
-
biasChunk,
|
|
131
|
-
gradientChunk,
|
|
132
|
-
learningRate
|
|
133
|
-
)
|
|
134
|
-
|
|
135
|
-
biasOut.set(newBias, offsetBias)
|
|
136
|
-
offsetBias += newBias.length
|
|
137
|
-
|
|
138
|
-
// Input gradient
|
|
139
|
-
const outputGra = cacheLBInputGradent[a](
|
|
140
|
-
weightChunk,
|
|
141
|
-
gradientChunk
|
|
142
|
-
)
|
|
143
|
-
|
|
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
|
-
}
|
|
148
95
|
|
|
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
|
-
|
|
160
96
|
}
|
|
97
|
+
CalculateLinearForward(weights, bias, input) {
|
|
98
|
+
// console.log(input)
|
|
99
|
+
const splitedBias = splitMatrix(bias)
|
|
100
|
+
const splitedWeights = splitMatrix(weights)
|
|
101
|
+
const result = new Float32Array(weights.length)
|
|
102
|
+
let offset = 0;
|
|
103
|
+
for (let a = 0; a < splitedBias.data.length; a++) {
|
|
104
|
+
const output = this.cacheLF[a](input, splitedBias.data[a], splitedWeights.data[a])
|
|
105
|
+
result.set(output, offset)
|
|
106
|
+
offset += output.length
|
|
107
|
+
}
|
|
108
|
+
return result
|
|
109
|
+
}
|
|
110
|
+
CalculateBackward(weights, bias, out, learningRate, input) {
|
|
111
|
+
const splitedBias = splitMatrix(bias)
|
|
112
|
+
const splitedWeights = splitMatrix(weights)
|
|
113
|
+
const inputGradient = new Float32Array(input.length)
|
|
114
|
+
const biasOut = new Float32Array(bias.length)
|
|
115
|
+
const weightsOut = []
|
|
116
|
+
let offsetBias = 0
|
|
117
|
+
let offsetWeights = 0
|
|
118
|
+
let gradientOffset = 0
|
|
119
|
+
|
|
120
|
+
for (let a = 0; a < splitedWeights.data.length; a++) {
|
|
121
|
+
const weightChunk = splitedWeights.data[a]
|
|
122
|
+
const biasChunk = splitedBias.data[a]
|
|
123
|
+
const chunkOutputSize = weightChunk.length
|
|
124
|
+
const gradientChunk = out.slice(
|
|
125
|
+
gradientOffset,
|
|
126
|
+
gradientOffset + chunkOutputSize
|
|
127
|
+
)
|
|
128
|
+
gradientOffset += chunkOutputSize
|
|
129
|
+
const newBias = this.cacheLBIas[a](
|
|
130
|
+
biasChunk,
|
|
131
|
+
gradientChunk,
|
|
132
|
+
learningRate
|
|
133
|
+
)
|
|
134
|
+
biasOut.set(newBias, offsetBias)
|
|
135
|
+
offsetBias += newBias.length
|
|
136
|
+
const outputGra = this.cacheLBInputGradent[a](
|
|
137
|
+
weightChunk,
|
|
138
|
+
gradientChunk
|
|
139
|
+
)
|
|
140
|
+
for (let i = 0; i < outputGra.length; i++) {
|
|
141
|
+
inputGradient[i] += outputGra[i]
|
|
142
|
+
}
|
|
143
|
+
const newWeights = this.cacheLBWeights[a](
|
|
144
|
+
weightChunk,
|
|
145
|
+
gradientChunk,
|
|
146
|
+
input,
|
|
147
|
+
learningRate
|
|
148
|
+
)
|
|
149
|
+
weightsOut.push(...newWeights)
|
|
161
150
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return {
|
|
154
|
+
inputGradient,
|
|
155
|
+
biasOut,
|
|
156
|
+
weightsOut
|
|
157
|
+
}
|
|
166
158
|
}
|
|
167
159
|
}
|
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, embeddingSize)
|
|
43
45
|
}
|
|
44
46
|
Save() {
|
|
45
47
|
SaveVectors(this.Weights, this.configs.save.filename[0])
|
|
@@ -47,12 +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 {biasOut,inputGradient, weightsOut} = CalculateBackward(weights, this.Bias, outputGradient, learningRate, input)
|
|
57
|
+
const {biasOut,inputGradient, weightsOut} = this.gpu.CalculateBackward(weights, this.Bias, outputGradient, learningRate, input)
|
|
56
58
|
this.Weights = weightsOut;
|
|
57
59
|
this.Bias = biasOut
|
|
58
60
|
return inputGradient
|