@imaduddin/fastbrain 1.1.1 → 2.0.1
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/dist/Embedding/index.d.ts +24 -0
- package/dist/Embedding/index.js +93 -0
- package/dist/FFN/index.d.ts +20 -0
- package/dist/FFN/index.js +51 -0
- package/dist/Gelu/index.d.ts +15 -0
- package/dist/Gelu/index.js +58 -0
- package/dist/Linear/index.d.ts +31 -0
- package/dist/Linear/index.js +136 -0
- package/dist/RMSNorm/index.d.ts +16 -0
- package/dist/RMSNorm/index.js +63 -0
- package/dist/Relu/index.d.ts +15 -0
- package/dist/Relu/index.js +58 -0
- package/dist/SelfAttention/index.d.ts +47 -0
- package/dist/SelfAttention/index.js +186 -0
- package/dist/Softmax/index.d.ts +18 -0
- package/dist/Softmax/index.js +96 -0
- package/dist/Tokenizer/index.d.ts +15 -0
- package/dist/Tokenizer/index.js +58 -0
- package/dist/TransformerBlock/index.d.ts +21 -0
- package/dist/TransformerBlock/index.js +43 -0
- package/dist/fileManager.d.ts +6 -0
- package/dist/fileManager.js +34 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +10 -0
- package/dist/residualConnection/index.d.ts +9 -0
- package/dist/residualConnection/index.js +25 -0
- package/package.json +20 -3
- package/shaders/add.wgsl +22 -0
- package/shaders/embedding/backward.wgsl +36 -0
- package/shaders/embedding/forward.wgsl +35 -0
- package/shaders/linear/ComputeDinput.wgsl +72 -0
- package/shaders/linear/forward.wgsl +63 -0
- package/shaders/linear/updateBias.wgsl +33 -0
- package/shaders/linear/updateWeights.wgsl +38 -0
- package/shaders/relu/backward.wgsl +43 -0
- package/shaders/relu/forward.wgsl +28 -0
- package/shaders/rmsnorm/backward.wgsl +54 -0
- package/shaders/rmsnorm/forward.wgsl +51 -0
- package/shaders/selfAttention/SB.wgsl +30 -0
- package/shaders/selfAttention/add3.wgsl +27 -0
- package/shaders/selfAttention/matMul.wgsl +63 -0
- package/shaders/selfAttention/qK.wgsl +66 -0
- package/shaders/selfAttention/transpose.wgsl +24 -0
- package/shaders/softmax/backward.wgsl +36 -0
- package/shaders/softmax/crossEntropy.wgsl +36 -0
- package/shaders/softmax/forward.wgsl +68 -0
- package/src/Embedding/index.ts +81 -0
- package/src/FFN/index.ts +47 -0
- package/src/Gelu/index.ts +53 -0
- package/src/Linear/index.ts +122 -0
- package/src/RMSNorm/index.ts +56 -0
- package/src/SelfAttention/index.ts +177 -0
- package/src/Softmax/index.ts +82 -0
- package/src/Tokenizer/index.ts +60 -0
- package/src/TransformerBlock/index.ts +41 -0
- package/src/fileManager.ts +46 -0
- package/src/index.ts +10 -0
- package/src/residualConnection/index.ts +24 -0
- package/tsconfig.json +27 -0
- package/index.js +0 -22
- package/src/Embedding/index.d.ts +0 -21
- package/src/Embedding/index.js +0 -140
- package/src/FFN/index.d.ts +0 -23
- package/src/FFN/index.js +0 -45
- package/src/Linear/index.d.ts +0 -31
- package/src/Linear/index.js +0 -196
- package/src/OperationManager.js +0 -169
- package/src/PositionEmbedding/index.d.ts +0 -30
- package/src/PositionEmbedding/index.js +0 -70
- package/src/RMSNorm/index.d.ts +0 -30
- package/src/RMSNorm/index.js +0 -91
- package/src/Relu/index.d.ts +0 -25
- package/src/Relu/index.js +0 -108
- package/src/SelfAttention/index.d.ts +0 -28
- package/src/SelfAttention/index.js +0 -138
- package/src/Softmax/index.d.ts +0 -26
- package/src/Softmax/index.js +0 -195
- package/src/Tokenizer/index.d.ts +0 -12
- package/src/Tokenizer/index.js +0 -164
- package/src/Tokenizer/vocabs.json +0 -19618
- package/src/TransformerBlock/index.d.ts +0 -28
- package/src/TransformerBlock/index.js +0 -78
- package/src/getVec.js +0 -30
- package/src/gpu.js +0 -0
- package/src/model/index.d.ts +0 -43
- package/src/model/index.js +0 -83
- package/src/residualConnection/index.js +0 -31
- package/src/shaders/CalculateAcc.wgsl +0 -50
- package/src/shaders/CalculateLoss.wgsl +0 -33
- package/src/shaders/ComputeDInput.wgsl +0 -46
- package/src/shaders/EB.wgsl +0 -33
- package/src/shaders/EF.wgsl +0 -25
- package/src/shaders/LB.wgsl +0 -71
- package/src/shaders/LF.wgsl +0 -60
- package/src/shaders/Norm/rmsBackward.wgsl +0 -65
- package/src/shaders/Norm/rmsForward.wgsl +0 -30
- package/src/shaders/Norm/sumSquare.wgsl +0 -38
- package/src/shaders/Norm/updateGamma.wgsl +0 -54
- package/src/shaders/PB.wgsl +0 -25
- package/src/shaders/PF.wgsl +0 -24
- package/src/shaders/RB.wgsl +0 -37
- package/src/shaders/RF.wgsl +0 -33
- package/src/shaders/ReductionFunction.wgsl +0 -28
- package/src/shaders/SB.wgsl +0 -41
- package/src/shaders/SF.wgsl +0 -75
- package/src/shaders/SoftmaxBackward.wgsl +0 -31
- package/src/shaders/add.wgsl +0 -30
- package/src/shaders/attentionShader/CompueFinalForwardOutput.wgsl +0 -29
- package/src/shaders/attentionShader/ComputeDA.wgsl +0 -30
- package/src/shaders/attentionShader/ComputeDV.wgsl +0 -37
- package/src/shaders/attentionShader/ComputeK.wgsl +0 -30
- package/src/shaders/attentionShader/ComputedQ.wgsl +0 -31
- package/src/shaders/attentionShader/attentionSocres.wgsl +0 -40
- package/src/shaders/attentionShader/sumGradient.wgsl +0 -24
- package/src/shaders/calculateTotal.wgsl +0 -26
package/src/Linear/index.js
DELETED
|
@@ -1,196 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import fs from 'fs'
|
|
3
|
-
import { buffer } from "stream/consumers";
|
|
4
|
-
import { SaveVector, getVector } from '../getVec.js'
|
|
5
|
-
import { OperationManager } from "../OperationManager.js";
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
function GenerateVectors(embeddingSize, vocabSize) {
|
|
10
|
-
const vec = new Float32Array(embeddingSize * vocabSize).map(() => (Math.random() * 2 - 1) * 0.02)
|
|
11
|
-
return vec
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function GenerateBias(vocabSize) {
|
|
15
|
-
const vec = new Float32Array(vocabSize)
|
|
16
|
-
return vec
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export class Linear {
|
|
20
|
-
device;
|
|
21
|
-
embeddingSize;
|
|
22
|
-
vocabSize;
|
|
23
|
-
configs;
|
|
24
|
-
bias;
|
|
25
|
-
weights;
|
|
26
|
-
forwardPipline;
|
|
27
|
-
configs;
|
|
28
|
-
input;
|
|
29
|
-
wBuffer;
|
|
30
|
-
bBuffer;
|
|
31
|
-
gpu;
|
|
32
|
-
paramForward;
|
|
33
|
-
oldParam;
|
|
34
|
-
constructor(device, embeddingSize, vocabSize, configs) {
|
|
35
|
-
this.configs = configs
|
|
36
|
-
this.embeddingSize = embeddingSize;
|
|
37
|
-
this.vocabSize = vocabSize;
|
|
38
|
-
this.configs = configs;
|
|
39
|
-
this.device = device
|
|
40
|
-
let total = embeddingSize * vocabSize;
|
|
41
|
-
if (configs && configs.save) {
|
|
42
|
-
const w = getVector(configs.save.filename[0], total);
|
|
43
|
-
const b = getVector(configs.save.filename[1], vocabSize, true)
|
|
44
|
-
if (!w && !b) {
|
|
45
|
-
this.bias = GenerateBias(vocabSize)
|
|
46
|
-
this.weights = GenerateVectors(embeddingSize, vocabSize)
|
|
47
|
-
SaveVector(configs.save.filename[0], this.weights)
|
|
48
|
-
SaveVector(configs.save.filename[1], this.bias)
|
|
49
|
-
} else {
|
|
50
|
-
this.bias = b;
|
|
51
|
-
this.weights = w;
|
|
52
|
-
}
|
|
53
|
-
} else {
|
|
54
|
-
this.bias = GenerateBias(vocabSize)
|
|
55
|
-
this.weights = GenerateVectors(embeddingSize, vocabSize)
|
|
56
|
-
}
|
|
57
|
-
this.gpu = new OperationManager(device);
|
|
58
|
-
this.forwardPipline = this.gpu.getShaderPipline("./src/shaders/LF.wgsl")
|
|
59
|
-
this.backwardPipline = this.gpu.getShaderPipline("./src/shaders/LB.wgsl")
|
|
60
|
-
this.dInputBackward = this.gpu.getShaderPipline("./src/shaders/ComputeDInput.wgsl")
|
|
61
|
-
}
|
|
62
|
-
async forward(vec, encoderMain) {
|
|
63
|
-
const encoder = encoderMain ? encoderMain : this.device.createCommandEncoder()
|
|
64
|
-
const tokenCount = vec.mainBuffer.size / (this.embeddingSize * 4)
|
|
65
|
-
if (!this.wBuffer) {
|
|
66
|
-
|
|
67
|
-
this.wBuffer = await this.gpu.createBuffer(this.embeddingSize * this.vocabSize * 4)
|
|
68
|
-
await this.gpu.WriteBuffer(this.wBuffer, this.weights)
|
|
69
|
-
this.bBuffer = await this.gpu.createBuffer(this.vocabSize * 4)
|
|
70
|
-
await this.gpu.WriteBuffer(this.bBuffer, this.bias)
|
|
71
|
-
this.paramForward = await this.gpu.createUniFormBuffer(4 * 4)
|
|
72
|
-
this.paramBackward = await this.gpu.createUniFormBuffer(16)
|
|
73
|
-
}
|
|
74
|
-
if (!this.oldParam) {
|
|
75
|
-
this.oldParam = new Uint32Array([this.embeddingSize, this.vocabSize, 0, 0])
|
|
76
|
-
}
|
|
77
|
-
if (this.oldParam[2] !== tokenCount) {
|
|
78
|
-
await this.gpu.WriteBuffer(this.paramForward, new Uint32Array([this.embeddingSize, this.vocabSize, tokenCount, 0]))
|
|
79
|
-
this.oldParam = new Uint32Array([this.embeddingSize, this.vocabSize, tokenCount, 0]);
|
|
80
|
-
}
|
|
81
|
-
if (!this.outputBuffer) this.outputBuffer = await this.gpu.ManageBuffer(4 * this.vocabSize * tokenCount)
|
|
82
|
-
if (this.outputBuffer.mainBuffer.size !== 4 * this.vocabSize * tokenCount || this.outputBuffer.isDestroyed()) {
|
|
83
|
-
this.outputBuffer.destroy()
|
|
84
|
-
this.outputBuffer = await this.gpu.ManageBuffer(4 * this.vocabSize * tokenCount)
|
|
85
|
-
}
|
|
86
|
-
const bind = await this.device.createBindGroup({
|
|
87
|
-
layout: await this.forwardPipline.getBindGroupLayout(0),
|
|
88
|
-
entries: [
|
|
89
|
-
{
|
|
90
|
-
binding: 0,
|
|
91
|
-
resource: this.wBuffer
|
|
92
|
-
},
|
|
93
|
-
{
|
|
94
|
-
binding: 1,
|
|
95
|
-
resource: this.outputBuffer.mainBuffer
|
|
96
|
-
},
|
|
97
|
-
{
|
|
98
|
-
binding: 2,
|
|
99
|
-
resource: vec.mainBuffer
|
|
100
|
-
},
|
|
101
|
-
{
|
|
102
|
-
binding: 3,
|
|
103
|
-
resource: this.bBuffer
|
|
104
|
-
},
|
|
105
|
-
{
|
|
106
|
-
binding: 4,
|
|
107
|
-
resource: this.paramForward
|
|
108
|
-
}
|
|
109
|
-
]
|
|
110
|
-
})
|
|
111
|
-
await this.gpu.RunPipline(encoder, this.forwardPipline, bind, Math.ceil(this.vocabSize / 16), Math.ceil(tokenCount / 4))
|
|
112
|
-
|
|
113
|
-
if(!encoderMain) this.device.queue.submit([encoder.finish()])
|
|
114
|
-
this.input = vec;
|
|
115
|
-
return this.outputBuffer
|
|
116
|
-
}
|
|
117
|
-
async backward(dOutput, learningRate, encoderMain) {
|
|
118
|
-
const encoder = encoderMain ? encoderMain : this.device.createCommandEncoder()
|
|
119
|
-
const tokenCount = dOutput.size / (this.vocabSize * 4)
|
|
120
|
-
const data = new ArrayBuffer(16);
|
|
121
|
-
const view = new DataView(data);
|
|
122
|
-
view.setUint32(0, this.vocabSize, true);
|
|
123
|
-
view.setUint32(4, tokenCount, true);
|
|
124
|
-
view.setFloat32(8, learningRate, true);
|
|
125
|
-
view.setUint32(12, this.embeddingSize, true);
|
|
126
|
-
await this.gpu.WriteBuffer(this.paramBackward, data);
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
if (!this.gradientBuffer) this.gradientBuffer = await this.gpu.ManageBuffer(this.embeddingSize * tokenCount * 4);
|
|
130
|
-
if (this.gradientBuffer.size !== 4 * this.embeddingSize * tokenCount || this.gradientBuffer.isDestroyed()) {
|
|
131
|
-
this.gradientBuffer.destroy()
|
|
132
|
-
this.gradientBuffer = await this.gpu.ManageBuffer(this.embeddingSize * tokenCount * 4)
|
|
133
|
-
|
|
134
|
-
}
|
|
135
|
-
await this.gradientBuffer.count()
|
|
136
|
-
const bind = await this.device.createBindGroup({
|
|
137
|
-
layout: await this.backwardPipline.getBindGroupLayout(0),
|
|
138
|
-
entries: [
|
|
139
|
-
{
|
|
140
|
-
binding: 0,
|
|
141
|
-
resource: this.bBuffer
|
|
142
|
-
},
|
|
143
|
-
{
|
|
144
|
-
binding: 1,
|
|
145
|
-
resource: this.wBuffer
|
|
146
|
-
},
|
|
147
|
-
{
|
|
148
|
-
binding: 2,
|
|
149
|
-
resource: dOutput.mainBuffer
|
|
150
|
-
},
|
|
151
|
-
{
|
|
152
|
-
binding: 3,
|
|
153
|
-
resource: this.input.mainBuffer
|
|
154
|
-
},
|
|
155
|
-
{
|
|
156
|
-
binding: 4,
|
|
157
|
-
resource: this.paramBackward
|
|
158
|
-
}
|
|
159
|
-
]
|
|
160
|
-
})
|
|
161
|
-
const bind2 = await this.device.createBindGroup({
|
|
162
|
-
layout: await this.dInputBackward.getBindGroupLayout(0),
|
|
163
|
-
entries: [
|
|
164
|
-
{
|
|
165
|
-
binding: 0,
|
|
166
|
-
resource: this.wBuffer
|
|
167
|
-
},
|
|
168
|
-
{
|
|
169
|
-
binding: 1,
|
|
170
|
-
resource: dOutput.mainBuffer
|
|
171
|
-
},
|
|
172
|
-
{
|
|
173
|
-
binding: 3,
|
|
174
|
-
resource: this.paramBackward
|
|
175
|
-
},
|
|
176
|
-
{
|
|
177
|
-
binding: 4,
|
|
178
|
-
resource: this.gradientBuffer.mainBuffer
|
|
179
|
-
}
|
|
180
|
-
]
|
|
181
|
-
})
|
|
182
|
-
await this.gpu.RunPipline(encoder, this.dInputBackward, bind2, Math.ceil((this.embeddingSize * tokenCount) / 64));
|
|
183
|
-
await this.gpu.RunPipline(encoder, this.backwardPipline, bind, this.vocabSize);
|
|
184
|
-
if(!encoderMain) await this.device.queue.submit([encoder.finish()])
|
|
185
|
-
return this.gradientBuffer
|
|
186
|
-
|
|
187
|
-
}
|
|
188
|
-
async Save() {
|
|
189
|
-
const b = await this.gpu.readBuffer(this.bBuffer.size, this.bBuffer);
|
|
190
|
-
const w = await this.gpu.readBuffer(this.wBuffer.size, this.wBuffer);
|
|
191
|
-
SaveVector(this.configs.save.filename[0], w)
|
|
192
|
-
SaveVector(this.configs.save.filename[1], b)
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
}
|
package/src/OperationManager.js
DELETED
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
import fs from 'fs'
|
|
2
|
-
import path, { join, dirname, format } from 'path';
|
|
3
|
-
import { fileURLToPath } from 'url';
|
|
4
|
-
|
|
5
|
-
const __dirname = dirname(fileURLToPath(import.meta.url))
|
|
6
|
-
|
|
7
|
-
export class OperationManager {
|
|
8
|
-
device;
|
|
9
|
-
matrixMatSHhaders;
|
|
10
|
-
params;
|
|
11
|
-
addShader;
|
|
12
|
-
constructor(device) {
|
|
13
|
-
this.device = device;
|
|
14
|
-
this.addShader = this.getShaderPipline("./src/shaders/add.wgsl")
|
|
15
|
-
}
|
|
16
|
-
async createBuffer(size) {
|
|
17
|
-
return this.device.createBuffer({
|
|
18
|
-
size: size,
|
|
19
|
-
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC | GPUBufferUsage.STORAGE
|
|
20
|
-
})
|
|
21
|
-
}
|
|
22
|
-
async createUniFormBuffer(size) {
|
|
23
|
-
return this.device.createBuffer({
|
|
24
|
-
size,
|
|
25
|
-
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC | GPUBufferUsage.UNIFORM
|
|
26
|
-
})
|
|
27
|
-
}
|
|
28
|
-
async readBuffer(size, buffer) {
|
|
29
|
-
const readBuffer = this.device.createBuffer({
|
|
30
|
-
size: size,
|
|
31
|
-
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
|
|
32
|
-
})
|
|
33
|
-
const encoder = this.device.createCommandEncoder()
|
|
34
|
-
encoder.copyBufferToBuffer(buffer, 0, readBuffer, 0, size);
|
|
35
|
-
this.device.queue.submit([await encoder.finish()])
|
|
36
|
-
await readBuffer.mapAsync(GPUMapMode.READ);
|
|
37
|
-
const mapped = readBuffer.getMappedRange();
|
|
38
|
-
const result = new Float32Array(
|
|
39
|
-
mapped.slice(0)
|
|
40
|
-
);
|
|
41
|
-
readBuffer.unmap()
|
|
42
|
-
readBuffer.destroy()
|
|
43
|
-
return result
|
|
44
|
-
}
|
|
45
|
-
getShaderPipline(filename, value = { "embedding": 123 }, entryPoint = "main") {
|
|
46
|
-
let readShaderCode = fs.readFileSync(path.join(__dirname, `../${filename}`), "utf-8");
|
|
47
|
-
if (value) {
|
|
48
|
-
for (const a in value) {
|
|
49
|
-
readShaderCode = readShaderCode.replaceAll(`%${a}%`, value[a])
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
const compiledKernel = this.device.createShaderModule({
|
|
53
|
-
code: readShaderCode
|
|
54
|
-
});
|
|
55
|
-
const compiledPipLine = this.device.createComputePipeline({
|
|
56
|
-
layout: "auto",
|
|
57
|
-
compute: {
|
|
58
|
-
module: compiledKernel,
|
|
59
|
-
entryPoint: entryPoint
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
return compiledPipLine;
|
|
63
|
-
}
|
|
64
|
-
async WriteBuffer(buffer, data) {
|
|
65
|
-
this.device.queue.writeBuffer(buffer, 0, data)
|
|
66
|
-
}
|
|
67
|
-
async RunPipline(encoder, pipline, binding, dispatchWorkgroups, y = 1, z = 1) {
|
|
68
|
-
const pass = encoder.beginComputePass();
|
|
69
|
-
pass.setPipeline(pipline)
|
|
70
|
-
pass.setBindGroup(0, binding)
|
|
71
|
-
pass.dispatchWorkgroups(dispatchWorkgroups, y, z)
|
|
72
|
-
pass.end()
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
async MakeACopyBuffer(encoder2, sourceBuffer) {
|
|
76
|
-
const encoder = encoder2 ? encoder2 : await this.CreateEncoder()
|
|
77
|
-
const buffer = this.device.createBuffer({
|
|
78
|
-
size: sourceBuffer.size,
|
|
79
|
-
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC | GPUBufferUsage.STORAGE
|
|
80
|
-
})
|
|
81
|
-
encoder.copyBufferToBuffer(sourceBuffer, 0, buffer, 0, sourceBuffer.size);
|
|
82
|
-
if (!encoder2) await this.submitQueue(encoder)
|
|
83
|
-
return buffer
|
|
84
|
-
}
|
|
85
|
-
CreateEncoder() {
|
|
86
|
-
return this.device.createCommandEncoder()
|
|
87
|
-
}
|
|
88
|
-
createBindGroup(shader, groupNum, ent) {
|
|
89
|
-
|
|
90
|
-
return this.device.createBindGroup({
|
|
91
|
-
layout: shader.getBindGroupLayout(groupNum),
|
|
92
|
-
entries: ent.map((e, x) => {
|
|
93
|
-
return {
|
|
94
|
-
binding: x,
|
|
95
|
-
resource: { buffer: e}
|
|
96
|
-
}
|
|
97
|
-
})
|
|
98
|
-
})
|
|
99
|
-
}
|
|
100
|
-
async submitQueue(encoder) {
|
|
101
|
-
this.device.queue.submit([encoder.finish()])
|
|
102
|
-
}
|
|
103
|
-
async add(a, b, size) {
|
|
104
|
-
if (!this.params) {
|
|
105
|
-
this.params = await this.createUniFormBuffer(4 * 4)
|
|
106
|
-
|
|
107
|
-
}
|
|
108
|
-
const encoder = await this.CreateEncoder()
|
|
109
|
-
const output = await this.ManageBuffer(a.size);
|
|
110
|
-
const bind = await this.createBindGroup(this.addShader, 0, [a, b, output.mainBuffer, this.params]);
|
|
111
|
-
await this.WriteBuffer(this.params, new Uint32Array([size, 0]))
|
|
112
|
-
await this.RunPipline(encoder, this.addShader, bind, Math.ceil(size / 64));
|
|
113
|
-
await this.submitQueue(encoder)
|
|
114
|
-
// a.destroy()
|
|
115
|
-
// b.destroy()
|
|
116
|
-
return output
|
|
117
|
-
}
|
|
118
|
-
ManageBuffer(size) {
|
|
119
|
-
let buffer = this.device.createBuffer({
|
|
120
|
-
size,
|
|
121
|
-
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC | GPUBufferUsage.STORAGE
|
|
122
|
-
})
|
|
123
|
-
let isDestroy = false;
|
|
124
|
-
let ret = {
|
|
125
|
-
id: Math.floor(Math.random() * 9999),
|
|
126
|
-
mainBuffer: buffer,
|
|
127
|
-
reuse: 0,
|
|
128
|
-
destroy: function () {
|
|
129
|
-
isDestroy = true
|
|
130
|
-
this.mainBuffer.destroy()
|
|
131
|
-
},
|
|
132
|
-
isDestroyed: function () {
|
|
133
|
-
return isDestroy
|
|
134
|
-
},
|
|
135
|
-
read: async () => {
|
|
136
|
-
return await this.readBuffer(buffer.size, buffer)
|
|
137
|
-
},
|
|
138
|
-
write: async (data) => {
|
|
139
|
-
if (ret.reuse == 1000) {
|
|
140
|
-
await ret.mainBuffer.destroy()
|
|
141
|
-
ret.mainBuffer = this.device.createBuffer({
|
|
142
|
-
size: ret.size,
|
|
143
|
-
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC | GPUBufferUsage.STORAGE
|
|
144
|
-
})
|
|
145
|
-
ret.reuse = 0;
|
|
146
|
-
}
|
|
147
|
-
await this.WriteBuffer(buffer, data)
|
|
148
|
-
ret.reuse++;
|
|
149
|
-
|
|
150
|
-
},
|
|
151
|
-
size: size,
|
|
152
|
-
count: async () => {
|
|
153
|
-
if (ret.reuse == 1000) {
|
|
154
|
-
await ret.mainBuffer.destroy()
|
|
155
|
-
ret.mainBuffer = this.device.createBuffer({
|
|
156
|
-
size: ret.size,
|
|
157
|
-
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC | GPUBufferUsage.STORAGE
|
|
158
|
-
})
|
|
159
|
-
ret.reuse = 0;
|
|
160
|
-
}
|
|
161
|
-
ret.reuse++;
|
|
162
|
-
|
|
163
|
-
},
|
|
164
|
-
|
|
165
|
-
}
|
|
166
|
-
return ret
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
export class PositionEmbedding {
|
|
2
|
-
constructor(device: GPUBuffer, embedding: number, contextSize: number, config: {
|
|
3
|
-
save: {
|
|
4
|
-
filename: string
|
|
5
|
-
}
|
|
6
|
-
});
|
|
7
|
-
async forward(input: {
|
|
8
|
-
read: function(): Promise<Float32Array>,
|
|
9
|
-
mainBuffer: GPUBuffer,
|
|
10
|
-
isDestroyed: function(): Boolean,
|
|
11
|
-
destroy: function(): void
|
|
12
|
-
}): Promise<{
|
|
13
|
-
read: function(): Promise<Float32Array>,
|
|
14
|
-
mainBuffer: GPUBuffer,
|
|
15
|
-
isDestroyed: function(): Boolean,
|
|
16
|
-
destroy: function(): void
|
|
17
|
-
}>
|
|
18
|
-
async Save(): Promise<void>
|
|
19
|
-
async backward(inputGradient: {
|
|
20
|
-
read: function(): Promise<Float32Array>,
|
|
21
|
-
mainBuffer: GPUBuffer,
|
|
22
|
-
isDestroyed: function(): Boolean,
|
|
23
|
-
destroy: function(): void
|
|
24
|
-
}, lr: number): Promise<{
|
|
25
|
-
read: function(): Promise<Float32Array>,
|
|
26
|
-
mainBuffer: GPUBuffer,
|
|
27
|
-
isDestroyed: function(): Boolean,
|
|
28
|
-
destroy: function(): void
|
|
29
|
-
}>
|
|
30
|
-
}
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import { OperationManager } from "../OperationManager.js"
|
|
2
|
-
import { getVector, SaveVector } from "../getVec.js"
|
|
3
|
-
function GenerateVectors(embeddingSize, contextSize) {
|
|
4
|
-
const vec = new Float32Array(embeddingSize * contextSize).map(() => (Math.random() * 2 - 1) * 0.02)
|
|
5
|
-
return vec
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export class PositionEmbedding {
|
|
9
|
-
vectors;
|
|
10
|
-
vecBuffer;
|
|
11
|
-
gpu;
|
|
12
|
-
embeddingSize;
|
|
13
|
-
contextSize;
|
|
14
|
-
params;
|
|
15
|
-
forwardShader;
|
|
16
|
-
backwardShader;
|
|
17
|
-
configs;
|
|
18
|
-
constructor(device, embeddingSize, contextSize, configs) {
|
|
19
|
-
this.embeddingSize = embeddingSize;
|
|
20
|
-
const total = embeddingSize * contextSize;
|
|
21
|
-
this.configs = configs;
|
|
22
|
-
this.contextSize = contextSize;
|
|
23
|
-
if (configs && configs.save) {
|
|
24
|
-
const vec = getVector(configs.save.filename, total);
|
|
25
|
-
if (vec) {
|
|
26
|
-
this.vectors = vec;
|
|
27
|
-
|
|
28
|
-
} else {
|
|
29
|
-
this.vectors = GenerateVectors(embeddingSize, contextSize)
|
|
30
|
-
SaveVector(this.configs.save.filename, this.vectors)
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
} else {
|
|
34
|
-
this.vectors = GenerateVectors(embeddingSize, contextSize)
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
this.gpu = new OperationManager(device)
|
|
38
|
-
this.forwardShader = this.gpu.getShaderPipline("./src/shaders/PF.wgsl")
|
|
39
|
-
this.backwardShader = this.gpu.getShaderPipline("./src/shaders/PB.wgsl")
|
|
40
|
-
}
|
|
41
|
-
async forward(input, en2) {
|
|
42
|
-
const encoder = en2 ? en2 : this.gpu.CreateEncoder()
|
|
43
|
-
if (!this.params) this.params = await this.gpu.createUniFormBuffer(4 * 4)
|
|
44
|
-
if (!this.vecBuffer) {
|
|
45
|
-
this.vecBuffer = await this.gpu.createBuffer(Float32Array.BYTES_PER_ELEMENT * this.contextSize * this.embeddingSize);
|
|
46
|
-
await this.gpu.WriteBuffer(this.vecBuffer, this.vectors)
|
|
47
|
-
}
|
|
48
|
-
if(!this.output) this.output = await this.gpu.ManageBuffer(input.size);
|
|
49
|
-
if(this.output.size !== input.size || this.output.isDestroyed()){
|
|
50
|
-
this.output.destroy()
|
|
51
|
-
this.output = await this.gpu.ManageBuffer(input.size)
|
|
52
|
-
}
|
|
53
|
-
const size = Math.ceil((input.size / 4) / 64);
|
|
54
|
-
const bindGroup = await this.gpu.createBindGroup(this.forwardShader, 0, [this.vecBuffer, input.mainBuffer, this.output.mainBuffer])
|
|
55
|
-
await this.gpu.RunPipline(encoder, this.forwardShader, bindGroup, size);
|
|
56
|
-
if(!en2) await this.gpu.submitQueue(encoder);
|
|
57
|
-
return this.output
|
|
58
|
-
}
|
|
59
|
-
async backward(outputGradient, lr, en2){
|
|
60
|
-
const encoder = en2 ? en2 : await this.gpu.CreateEncoder()
|
|
61
|
-
await this.gpu.WriteBuffer(this.params, new Float32Array([lr,0]))
|
|
62
|
-
const bindGroup = await this.gpu.createBindGroup(this.backwardShader, 0, [this.vecBuffer, outputGradient.mainBuffer, this.params])
|
|
63
|
-
await this.gpu.RunPipline(encoder, this.backwardShader, bindGroup, Math.ceil((outputGradient.size / 4) / 64))
|
|
64
|
-
if(!en2) await this.gpu.submitQueue(encoder);
|
|
65
|
-
}
|
|
66
|
-
async Save() {
|
|
67
|
-
const vec = await this.gpu.readBuffer(Float32Array.BYTES_PER_ELEMENT * this.contextSize * this.embeddingSize, this.vecBuffer)
|
|
68
|
-
SaveVector(this.configs.save.filename, vec)
|
|
69
|
-
}
|
|
70
|
-
}
|
package/src/RMSNorm/index.d.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
export class RMSNorm {
|
|
2
|
-
constructor(device: GPUAdapter, embeddingSize: number, {
|
|
3
|
-
save: {
|
|
4
|
-
filename: string
|
|
5
|
-
}
|
|
6
|
-
});
|
|
7
|
-
async forward(input: {
|
|
8
|
-
read: function(): Promise<Float32Array>,
|
|
9
|
-
mainBuffer: GPUBuffer,
|
|
10
|
-
isDestroyed: function(): Boolean,
|
|
11
|
-
destroy: function(): void
|
|
12
|
-
}): Promise<{
|
|
13
|
-
read: function(): Promise<Float32Array>,
|
|
14
|
-
mainBuffer: GPUBuffer,
|
|
15
|
-
isDestroyed: function(): Boolean,
|
|
16
|
-
destroy: function(): void
|
|
17
|
-
}>
|
|
18
|
-
async backward(outputGradient: {
|
|
19
|
-
read: function(): Promise<Float32Array>,
|
|
20
|
-
mainBuffer: GPUBuffer,
|
|
21
|
-
isDestroyed: function(): Boolean,
|
|
22
|
-
destroy: function(): void
|
|
23
|
-
}): Promise<{
|
|
24
|
-
read: function(): Promise<Float32Array>,
|
|
25
|
-
mainBuffer: GPUBuffer,
|
|
26
|
-
isDestroyed: function(): Boolean,
|
|
27
|
-
destroy: function(): void
|
|
28
|
-
}>
|
|
29
|
-
async Save(): Promise<void>
|
|
30
|
-
}
|
package/src/RMSNorm/index.js
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import { OperationManager } from '../OperationManager.js'
|
|
2
|
-
import fs from 'fs'
|
|
3
|
-
import { SaveVector, getVector } from '../getVec.js'
|
|
4
|
-
|
|
5
|
-
export class RMSNorm {
|
|
6
|
-
embeddingSize;
|
|
7
|
-
reductionShader;
|
|
8
|
-
forwardShader;
|
|
9
|
-
backwardShader;
|
|
10
|
-
op;
|
|
11
|
-
gamma;
|
|
12
|
-
gammabuffer;
|
|
13
|
-
configs;
|
|
14
|
-
rmsReduction;
|
|
15
|
-
output;
|
|
16
|
-
gradientOutput;
|
|
17
|
-
input;
|
|
18
|
-
device
|
|
19
|
-
constructor(device, embeddingSize, configs) {
|
|
20
|
-
this.configs = configs
|
|
21
|
-
this.op = new OperationManager(device);
|
|
22
|
-
this.embeddingSize = embeddingSize;
|
|
23
|
-
this.reductionShader = this.op.getShaderPipline("./src/shaders/Norm/sumSquare.wgsl", {
|
|
24
|
-
embeddingSize: embeddingSize,
|
|
25
|
-
})
|
|
26
|
-
this.forwardShader = this.op.getShaderPipline("./src/shaders/Norm/rmsForward.wgsl", {
|
|
27
|
-
embeddingSize: embeddingSize
|
|
28
|
-
})
|
|
29
|
-
this.backwardShader = this.op.getShaderPipline("./src/shaders/Norm/rmsBackward.wgsl", {
|
|
30
|
-
embeddingSize: embeddingSize
|
|
31
|
-
})
|
|
32
|
-
this.updateGamma = this.op.getShaderPipline("./src/shaders/Norm/updateGamma.wgsl", {
|
|
33
|
-
embeddingSize: embeddingSize
|
|
34
|
-
})
|
|
35
|
-
this.device = device;
|
|
36
|
-
if (configs) {
|
|
37
|
-
const gammaFile = getVector(configs.save.filename, embeddingSize);
|
|
38
|
-
if (!gammaFile) {
|
|
39
|
-
this.gamma = new Float32Array(embeddingSize).fill(1);
|
|
40
|
-
SaveVector(configs.save.filename, this.gamma)
|
|
41
|
-
} else {
|
|
42
|
-
this.gamma = gammaFile
|
|
43
|
-
}
|
|
44
|
-
} else {
|
|
45
|
-
this.gamma = new Float32Array(embeddingSize).fill(1)
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
async forward(vec, en) {
|
|
49
|
-
if (!this.gammabuffer) {
|
|
50
|
-
this.gammabuffer = await this.op.createBuffer(this.embeddingSize * 4);
|
|
51
|
-
await this.op.WriteBuffer(this.gammabuffer, this.gamma);
|
|
52
|
-
}
|
|
53
|
-
const context = vec.size / (this.embeddingSize * 4)
|
|
54
|
-
if (!this.rmsReduction) this.rmsReduction = await this.op.createBuffer(context * 4);
|
|
55
|
-
if (!this.output) this.output = await this.op.ManageBuffer(vec.size)
|
|
56
|
-
if (this.rmsReduction.size !== context * 4) {
|
|
57
|
-
this.rmsReduction.destroy()
|
|
58
|
-
this.rmsReduction = await this.op.createBuffer(4 * context)
|
|
59
|
-
}
|
|
60
|
-
if (this.output) this.output.destroy();
|
|
61
|
-
this.output = await this.op.ManageBuffer(vec.size)
|
|
62
|
-
const encoder = en ? en : await this.op.CreateEncoder()
|
|
63
|
-
const bind1 = await this.op.createBindGroup(this.reductionShader, 0, [vec.mainBuffer, this.rmsReduction])
|
|
64
|
-
const bind2 = await this.op.createBindGroup(this.forwardShader, 0, [this.rmsReduction, vec.mainBuffer, this.output.mainBuffer, this.gammabuffer])
|
|
65
|
-
await this.op.RunPipline(encoder, this.reductionShader, bind1, context)
|
|
66
|
-
await this.op.RunPipline(encoder, this.forwardShader, bind2, Math.ceil((this.embeddingSize * context) / 64))
|
|
67
|
-
if(!en) await this.op.submitQueue(encoder);
|
|
68
|
-
this.input = vec;
|
|
69
|
-
return this.output
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
async backward(outGradient, lr, en) {
|
|
73
|
-
const contextSize = outGradient.size / (this.embeddingSize * 4);
|
|
74
|
-
if (this.gradientOutput) this.gradientOutput.destroy()
|
|
75
|
-
this.gradientOutput = await this.op.ManageBuffer(this.embeddingSize * 4 * contextSize)
|
|
76
|
-
if (!this.params) this.params = await this.op.createUniFormBuffer(4 * 2);
|
|
77
|
-
await this.op.WriteBuffer(this.params, new Float32Array([lr, 0]))
|
|
78
|
-
const encoder = en ? en : this.op.CreateEncoder()
|
|
79
|
-
const bind1 = await this.op.createBindGroup(this.backwardShader, 0, [this.input.mainBuffer, outGradient.mainBuffer, this.rmsReduction, this.gammabuffer, this.gradientOutput.mainBuffer])
|
|
80
|
-
await this.op.RunPipline(encoder, this.backwardShader, bind1, contextSize);
|
|
81
|
-
const bind2 = await this.op.createBindGroup(this.updateGamma, 0, [this.input.mainBuffer, outGradient.mainBuffer, this.rmsReduction, this.gammabuffer, this.params])
|
|
82
|
-
await this.op.RunPipline(encoder, this.updateGamma, bind2, this.embeddingSize);
|
|
83
|
-
if(!en) this.op.submitQueue(encoder);
|
|
84
|
-
return this.gradientOutput
|
|
85
|
-
}
|
|
86
|
-
async Save() {
|
|
87
|
-
const gpuGamma = await this.op.readBuffer(4 * this.embeddingSize, this.gammabuffer);
|
|
88
|
-
SaveVector(this.configs.save.filename, gpuGamma)
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
}
|
package/src/Relu/index.d.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
export class Relu {
|
|
2
|
-
constructor(device: GPUAdapter, embeddingSize: number);
|
|
3
|
-
async forward(input: {
|
|
4
|
-
read: function(): Promise<Float32Array>,
|
|
5
|
-
mainBuffer: GPUBuffer,
|
|
6
|
-
isDestroyed: function(): Boolean,
|
|
7
|
-
destroy: function(): void
|
|
8
|
-
}): Promise<{
|
|
9
|
-
read: function(): Promise<Float32Array>,
|
|
10
|
-
mainBuffer: GPUBuffer,
|
|
11
|
-
isDestroyed: function(): Boolean,
|
|
12
|
-
destroy: function(): void
|
|
13
|
-
}>
|
|
14
|
-
async backward(outGradinet: {
|
|
15
|
-
read: function(): Promise<Float32Array>,
|
|
16
|
-
mainBuffer: GPUBuffer,
|
|
17
|
-
isDestroyed: function(): Boolean,
|
|
18
|
-
destroy: function(): void
|
|
19
|
-
}): Promise<{
|
|
20
|
-
read: function(): Promise<Float32Array>,
|
|
21
|
-
mainBuffer: GPUBuffer,
|
|
22
|
-
isDestroyed: function(): Boolean,
|
|
23
|
-
destroy: function(): void
|
|
24
|
-
}>
|
|
25
|
-
}
|