@imaduddin/fastbrain 1.1.1-patch → 2.0.0
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 +14 -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 -82
- 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
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Buffer, GPUManager } from '@imaduddin/webgpu-manager';
|
|
2
|
+
export declare class Embedding {
|
|
3
|
+
device: GPUDevice;
|
|
4
|
+
embeddingSize: number;
|
|
5
|
+
vocabSize: number;
|
|
6
|
+
configs: {
|
|
7
|
+
filename: string;
|
|
8
|
+
};
|
|
9
|
+
vectors: Float32Array;
|
|
10
|
+
vectorsBuffer: Buffer | undefined;
|
|
11
|
+
gpu: GPUManager | undefined;
|
|
12
|
+
param: Buffer | undefined;
|
|
13
|
+
paramBackward: Buffer | undefined;
|
|
14
|
+
tokenBuffer: Buffer | undefined;
|
|
15
|
+
output: Buffer | undefined;
|
|
16
|
+
forwardShader: GPUComputePipeline | undefined;
|
|
17
|
+
backwardPipline: GPUComputePipeline | undefined;
|
|
18
|
+
constructor(device: GPUDevice, embeddingSize: number, vocabSize: number, configs: {
|
|
19
|
+
filename: string;
|
|
20
|
+
});
|
|
21
|
+
forward(input: number[], encoderShared: GPUCommandEncoder | undefined): Promise<Buffer | undefined>;
|
|
22
|
+
backward(dInput: Buffer, lr: number, encoderShared: GPUCommandEncoder | undefined): Promise<void>;
|
|
23
|
+
Save(): Promise<void>;
|
|
24
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { GetVectorsForFile, SaveFileVector, initRandomVector } from '../fileManager.js';
|
|
2
|
+
import { GPUManager } from '@imaduddin/webgpu-manager';
|
|
3
|
+
export class Embedding {
|
|
4
|
+
device;
|
|
5
|
+
embeddingSize;
|
|
6
|
+
vocabSize;
|
|
7
|
+
configs;
|
|
8
|
+
vectors;
|
|
9
|
+
vectorsBuffer;
|
|
10
|
+
gpu;
|
|
11
|
+
param;
|
|
12
|
+
paramBackward;
|
|
13
|
+
tokenBuffer;
|
|
14
|
+
output;
|
|
15
|
+
forwardShader;
|
|
16
|
+
backwardPipline;
|
|
17
|
+
constructor(device, embeddingSize, vocabSize, configs) {
|
|
18
|
+
this.device = device;
|
|
19
|
+
this.embeddingSize = embeddingSize;
|
|
20
|
+
this.vocabSize = vocabSize;
|
|
21
|
+
this.configs = configs;
|
|
22
|
+
if (!this.configs) {
|
|
23
|
+
this.vectors = initRandomVector(embeddingSize * vocabSize);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
if (!this.configs.filename) {
|
|
27
|
+
this.vectors = initRandomVector(embeddingSize * vocabSize);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
const fileVec = GetVectorsForFile(configs.filename, embeddingSize * vocabSize, false, embeddingSize, vocabSize);
|
|
31
|
+
if (!fileVec) {
|
|
32
|
+
this.vectors = initRandomVector(embeddingSize * vocabSize);
|
|
33
|
+
SaveFileVector(this.configs.filename, this.vectors);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
this.vectors = fileVec;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
this.gpu = new GPUManager(device, {
|
|
41
|
+
shaderDir: "./shaders"
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
async forward(input, encoderShared) {
|
|
45
|
+
let token = new Uint32Array(input);
|
|
46
|
+
let encoder = encoderShared ? encoderShared : await this.gpu?.CreateEncoder();
|
|
47
|
+
if (!this.vectorsBuffer) {
|
|
48
|
+
this.vectorsBuffer = await this.gpu?.createBuffer(this.vectors, "storage");
|
|
49
|
+
this.forwardShader = await this.gpu?.getPiplineShader("embedding/forward.wgsl");
|
|
50
|
+
}
|
|
51
|
+
if (!this.param)
|
|
52
|
+
this.param = await this.gpu?.createBuffer(new Uint32Array([this.embeddingSize, this.vocabSize]), "uniform");
|
|
53
|
+
if (!this.tokenBuffer)
|
|
54
|
+
this.tokenBuffer = await this.gpu?.createBuffer(token, "storage");
|
|
55
|
+
if (this.tokenBuffer?.buffer.size !== token.byteLength) {
|
|
56
|
+
this.tokenBuffer?.destroy();
|
|
57
|
+
this.tokenBuffer = await this.gpu?.createBuffer(token, "storage");
|
|
58
|
+
}
|
|
59
|
+
this.tokenBuffer?.changeData(token);
|
|
60
|
+
if (!this.output)
|
|
61
|
+
this.output = await this.gpu?.createBuffer(new Float32Array(this.embeddingSize * input.length), "storage");
|
|
62
|
+
if (this.output?.buffer.size !== input.length * this.embeddingSize * 4 || this.output.isDestroy()) {
|
|
63
|
+
this.output?.destroy();
|
|
64
|
+
this.output = await this.gpu?.createBuffer(new Float32Array(this.embeddingSize * input.length), "storage");
|
|
65
|
+
}
|
|
66
|
+
const bindGroup = await this.gpu?.createBindGroup(this.forwardShader, [this.vectorsBuffer, this.tokenBuffer, this.output, this.param], 0);
|
|
67
|
+
await this.gpu?.RunPipline(encoder, this.forwardShader, bindGroup, Math.ceil(this.embeddingSize / 64), Math.ceil(input.length / 4));
|
|
68
|
+
if (!encoderShared)
|
|
69
|
+
await this.gpu?.SumbitEncoder(encoder);
|
|
70
|
+
return this.output;
|
|
71
|
+
}
|
|
72
|
+
async backward(dInput, lr, encoderShared) {
|
|
73
|
+
const encoder = encoderShared ? encoderShared : await this.gpu?.CreateEncoder();
|
|
74
|
+
const tokenCount = dInput.buffer.size / (this.embeddingSize * 4);
|
|
75
|
+
if (!this.backwardPipline) {
|
|
76
|
+
this.backwardPipline = await this.gpu?.getPiplineShader("embedding/backward.wgsl");
|
|
77
|
+
}
|
|
78
|
+
if (!this.paramBackward) {
|
|
79
|
+
this.paramBackward = await this.gpu?.createBuffer(new Float32Array([this.embeddingSize, this.vocabSize, lr]), "uniform");
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
this.paramBackward.changeData(new Float32Array([this.embeddingSize, this.vocabSize, lr]));
|
|
83
|
+
}
|
|
84
|
+
const bindGroup = await this.gpu?.createBindGroup(this.backwardPipline, [this.vectorsBuffer, this.tokenBuffer, dInput, this.paramBackward], 0);
|
|
85
|
+
await this.gpu?.RunPipline(encoder, this.backwardPipline, bindGroup, Math.ceil(this.embeddingSize / 64), Math.ceil(tokenCount / 4));
|
|
86
|
+
if (!encoderShared)
|
|
87
|
+
await this.gpu?.SumbitEncoder(encoder);
|
|
88
|
+
}
|
|
89
|
+
async Save() {
|
|
90
|
+
let vec = await this.vectorsBuffer?.read();
|
|
91
|
+
await SaveFileVector(this.configs.filename, vec);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Linear } from "../Linear/index.js";
|
|
2
|
+
import { Gelu as Relu } from '../Gelu/index.js';
|
|
3
|
+
import { Buffer, GPUManager } from '@imaduddin/webgpu-manager';
|
|
4
|
+
export declare class FFN {
|
|
5
|
+
device: GPUDevice;
|
|
6
|
+
dimSize: number;
|
|
7
|
+
configs: {
|
|
8
|
+
dirname: string;
|
|
9
|
+
};
|
|
10
|
+
linear1: Linear | undefined;
|
|
11
|
+
linear2: Linear | undefined;
|
|
12
|
+
activation: Relu | undefined;
|
|
13
|
+
gpu: GPUManager | undefined;
|
|
14
|
+
constructor(device: GPUDevice, dimSize: number, configs: {
|
|
15
|
+
dirname: string;
|
|
16
|
+
});
|
|
17
|
+
forward(input: Buffer, encoderShared: GPUCommandEncoder | undefined): Promise<Buffer | undefined>;
|
|
18
|
+
backward(outputGradient: Buffer, lr: number, encoderShared: GPUCommandEncoder | undefined): Promise<Buffer | undefined>;
|
|
19
|
+
Save(): Promise<void>;
|
|
20
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Linear } from "../Linear/index.js";
|
|
2
|
+
import { Gelu as Relu } from '../Gelu/index.js';
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { GPUManager } from '@imaduddin/webgpu-manager';
|
|
5
|
+
export class FFN {
|
|
6
|
+
device;
|
|
7
|
+
dimSize;
|
|
8
|
+
configs;
|
|
9
|
+
linear1;
|
|
10
|
+
linear2;
|
|
11
|
+
activation;
|
|
12
|
+
gpu;
|
|
13
|
+
constructor(device, dimSize, configs) {
|
|
14
|
+
this.device = device;
|
|
15
|
+
this.dimSize = dimSize;
|
|
16
|
+
this.configs = configs;
|
|
17
|
+
this.linear1 = new Linear(device, dimSize, dimSize * 4, {
|
|
18
|
+
filename: [join(configs.dirname, "LINEAR_EXPAND_FFN_WEIGHTS_1.bin"), join(configs.dirname, "LINEAR_EXPAND_FFN_BIAS_1.bin")]
|
|
19
|
+
});
|
|
20
|
+
this.linear2 = new Linear(device, dimSize * 4, dimSize, {
|
|
21
|
+
filename: [join(configs.dirname, "LINEAR_EXPAND_FFN_WEIGHTS_2.bin"), join(configs.dirname, "LINEAR_EXPAND_FFN_BIAS_2.bin")]
|
|
22
|
+
});
|
|
23
|
+
this.activation = new Relu(device, dimSize * 4);
|
|
24
|
+
this.gpu = new GPUManager(device, {
|
|
25
|
+
shaderDir: "./shaders"
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
async forward(input, encoderShared) {
|
|
29
|
+
const encoder = encoderShared ? encoderShared : await this.gpu?.CreateEncoder();
|
|
30
|
+
const hidden1 = await this.linear1?.forward(input, encoder);
|
|
31
|
+
const hiddenActivation = await this.activation?.forward(hidden1, encoder);
|
|
32
|
+
const hidden2 = await this.linear2?.forward(hiddenActivation, encoder);
|
|
33
|
+
if (!encoderShared) {
|
|
34
|
+
await this.gpu?.SumbitEncoder(encoder);
|
|
35
|
+
}
|
|
36
|
+
return hidden2;
|
|
37
|
+
}
|
|
38
|
+
async backward(outputGradient, lr, encoderShared) {
|
|
39
|
+
const encoder = encoderShared ? encoderShared : await this.gpu?.CreateEncoder();
|
|
40
|
+
const dActivation = await this.linear2?.backward(outputGradient, lr, encoder);
|
|
41
|
+
const dHidden2 = await this.activation?.backward(dActivation, encoder);
|
|
42
|
+
const dHidden1 = await this.linear1?.backward(dHidden2, lr, encoder);
|
|
43
|
+
await this.linear2?.updateParameter(outputGradient, lr, encoder);
|
|
44
|
+
await this.linear1?.updateParameter(dHidden2, lr, encoder);
|
|
45
|
+
return dHidden1;
|
|
46
|
+
}
|
|
47
|
+
async Save() {
|
|
48
|
+
await this.linear1?.Save();
|
|
49
|
+
await this.linear2?.Save();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Buffer, GPUManager } from "@imaduddin/webgpu-manager";
|
|
2
|
+
export declare class Gelu {
|
|
3
|
+
device: GPUDevice;
|
|
4
|
+
hiddenSize: number;
|
|
5
|
+
input: Buffer | undefined;
|
|
6
|
+
output: Buffer | undefined;
|
|
7
|
+
outputGrad: Buffer | undefined;
|
|
8
|
+
params: Buffer | undefined;
|
|
9
|
+
forwardShader: GPUComputePipeline | undefined;
|
|
10
|
+
backwardShader: GPUComputePipeline | undefined;
|
|
11
|
+
gpu: GPUManager | undefined;
|
|
12
|
+
constructor(device: GPUDevice, hiddenSize: number);
|
|
13
|
+
forward(input: Buffer, encoderShared: GPUCommandEncoder | undefined): Promise<Buffer | undefined>;
|
|
14
|
+
backward(gradient: Buffer, encoderShared: GPUCommandEncoder | undefined): Promise<Buffer | undefined>;
|
|
15
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { GPUManager } from "@imaduddin/webgpu-manager";
|
|
2
|
+
export class Gelu {
|
|
3
|
+
device;
|
|
4
|
+
hiddenSize;
|
|
5
|
+
input;
|
|
6
|
+
output;
|
|
7
|
+
outputGrad;
|
|
8
|
+
params;
|
|
9
|
+
forwardShader;
|
|
10
|
+
backwardShader;
|
|
11
|
+
gpu;
|
|
12
|
+
constructor(device, hiddenSize) {
|
|
13
|
+
this.device = device;
|
|
14
|
+
this.hiddenSize = hiddenSize;
|
|
15
|
+
this.gpu = new GPUManager(device, {
|
|
16
|
+
shaderDir: "./shaders/relu"
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
async forward(input, encoderShared) {
|
|
20
|
+
let tokenCount = input.buffer.size / (this.hiddenSize * 4);
|
|
21
|
+
this.input = input;
|
|
22
|
+
if (!this.output || this.output.buffer.size !== input.buffer.size) {
|
|
23
|
+
if (this.output)
|
|
24
|
+
this.output.destroy();
|
|
25
|
+
this.output = await this.gpu?.createBuffer(new Float32Array(this.hiddenSize * tokenCount), "storage");
|
|
26
|
+
}
|
|
27
|
+
if (!this.forwardShader) {
|
|
28
|
+
this.forwardShader = await this.gpu?.getPiplineShader("forward.wgsl");
|
|
29
|
+
this.backwardShader = await this.gpu?.getPiplineShader("backward.wgsl");
|
|
30
|
+
}
|
|
31
|
+
if (!this.params) {
|
|
32
|
+
this.params = await this.gpu?.createBuffer(new Uint32Array([this.hiddenSize, tokenCount]), "uniform");
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
await this.params.changeData(new Uint32Array([this.hiddenSize, tokenCount]));
|
|
36
|
+
}
|
|
37
|
+
let encoder = encoderShared ? encoderShared : await this.gpu?.CreateEncoder();
|
|
38
|
+
const bindGroup = await this.gpu?.createBindGroup(this.forwardShader, [this.input, this.output, this.params], 0);
|
|
39
|
+
await this.gpu?.RunPipline(encoder, this.forwardShader, bindGroup, Math.ceil(this.hiddenSize / 16), Math.ceil(tokenCount / 16));
|
|
40
|
+
if (!encoderShared)
|
|
41
|
+
await this.gpu?.SumbitEncoder(encoder);
|
|
42
|
+
return this.output;
|
|
43
|
+
}
|
|
44
|
+
async backward(gradient, encoderShared) {
|
|
45
|
+
let tokenCount = gradient.buffer.size / (this.hiddenSize * 4);
|
|
46
|
+
let encoder = encoderShared ? encoderShared : await this.gpu?.CreateEncoder();
|
|
47
|
+
if (!this.outputGrad || this.outputGrad.buffer.size !== gradient.buffer.size) {
|
|
48
|
+
if (this.outputGrad)
|
|
49
|
+
this.outputGrad.destroy();
|
|
50
|
+
this.outputGrad = await this.gpu?.createBuffer(new Float32Array(gradient.buffer.size / 4), "storage");
|
|
51
|
+
}
|
|
52
|
+
const bindGroup = await this.gpu?.createBindGroup(this.backwardShader, [this.input, this.outputGrad, gradient, this.params], 0);
|
|
53
|
+
await this.gpu?.RunPipline(encoder, this.backwardShader, bindGroup, Math.ceil(this.hiddenSize / 16), Math.ceil(tokenCount / 16));
|
|
54
|
+
if (!encoderShared)
|
|
55
|
+
await this.gpu?.SumbitEncoder(encoder);
|
|
56
|
+
return this.outputGrad;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { GPUManager, Buffer } from "@imaduddin/webgpu-manager";
|
|
2
|
+
export declare class Linear {
|
|
3
|
+
device: GPUDevice;
|
|
4
|
+
embeddingSize: number;
|
|
5
|
+
vocabSize: number;
|
|
6
|
+
configs: {
|
|
7
|
+
filename: string[];
|
|
8
|
+
};
|
|
9
|
+
bias: Float32Array | undefined;
|
|
10
|
+
weights: Float32Array | undefined;
|
|
11
|
+
wBuffer: Buffer | undefined;
|
|
12
|
+
bBuffer: Buffer | undefined;
|
|
13
|
+
gpu: GPUManager | undefined;
|
|
14
|
+
paramsForward: Buffer | undefined;
|
|
15
|
+
paramsBackward: Buffer | undefined;
|
|
16
|
+
output: Buffer | undefined;
|
|
17
|
+
forwardPipline: GPUComputePipeline | undefined;
|
|
18
|
+
backwardPipline: GPUComputePipeline | undefined;
|
|
19
|
+
weightUpdate: GPUComputePipeline | undefined;
|
|
20
|
+
biasUpdate: GPUComputePipeline | undefined;
|
|
21
|
+
gradOut: Buffer | undefined;
|
|
22
|
+
input: Buffer | undefined;
|
|
23
|
+
constructor(device: GPUDevice, embeddingSize: number, vocabSize: number, configs: {
|
|
24
|
+
filename: string[];
|
|
25
|
+
});
|
|
26
|
+
getParameter(): void;
|
|
27
|
+
forward(input: Buffer, en: GPUCommandEncoder | undefined): Promise<Buffer | undefined>;
|
|
28
|
+
backward(outputGradient: Buffer, lr: number, encoderShared: GPUCommandEncoder | undefined): Promise<Buffer | undefined>;
|
|
29
|
+
updateParameter(outputGradient: Buffer, lr: number, encoderShared: GPUCommandEncoder | undefined): Promise<Buffer | undefined>;
|
|
30
|
+
Save(): Promise<void>;
|
|
31
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { GPUManager } from "@imaduddin/webgpu-manager";
|
|
2
|
+
import { GetVectorsForFile, SaveFileVector, initRandomVector, getParameter } from '../fileManager.js';
|
|
3
|
+
export class Linear {
|
|
4
|
+
device;
|
|
5
|
+
embeddingSize;
|
|
6
|
+
vocabSize;
|
|
7
|
+
configs;
|
|
8
|
+
bias;
|
|
9
|
+
weights;
|
|
10
|
+
wBuffer;
|
|
11
|
+
bBuffer;
|
|
12
|
+
gpu;
|
|
13
|
+
paramsForward;
|
|
14
|
+
paramsBackward;
|
|
15
|
+
output;
|
|
16
|
+
forwardPipline;
|
|
17
|
+
backwardPipline;
|
|
18
|
+
weightUpdate;
|
|
19
|
+
biasUpdate;
|
|
20
|
+
gradOut;
|
|
21
|
+
input;
|
|
22
|
+
constructor(device, embeddingSize, vocabSize, configs) {
|
|
23
|
+
this.device = device;
|
|
24
|
+
this.embeddingSize = embeddingSize;
|
|
25
|
+
this.vocabSize = vocabSize;
|
|
26
|
+
this.configs = configs;
|
|
27
|
+
if (!this.configs) {
|
|
28
|
+
this.weights = initRandomVector(embeddingSize * vocabSize);
|
|
29
|
+
this.bias = initRandomVector(vocabSize);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
if (!this.configs.filename) {
|
|
33
|
+
this.weights = initRandomVector(embeddingSize * vocabSize);
|
|
34
|
+
this.bias = initRandomVector(vocabSize);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
const fileVec = GetVectorsForFile(configs.filename[0], embeddingSize * vocabSize, false, embeddingSize, vocabSize);
|
|
38
|
+
const fileBias = GetVectorsForFile(configs.filename[1], vocabSize, true, embeddingSize, vocabSize);
|
|
39
|
+
if (!fileVec) {
|
|
40
|
+
this.weights = initRandomVector(embeddingSize * vocabSize);
|
|
41
|
+
this.bias = initRandomVector(vocabSize);
|
|
42
|
+
SaveFileVector(this.configs.filename[0], this.weights);
|
|
43
|
+
SaveFileVector(this.configs.filename[1], this.bias);
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
this.weights = fileVec;
|
|
47
|
+
this.bias = fileBias;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
this.gpu = new GPUManager(device, {
|
|
52
|
+
shaderDir: "./shaders"
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
getParameter() {
|
|
56
|
+
getParameter();
|
|
57
|
+
}
|
|
58
|
+
async forward(input, en) {
|
|
59
|
+
const encoder = en ? en : await this.gpu?.CreateEncoder();
|
|
60
|
+
this.input = input;
|
|
61
|
+
if (!this.wBuffer || !this.bBuffer) {
|
|
62
|
+
this.wBuffer = await this.gpu?.createBuffer(this.weights, "storage");
|
|
63
|
+
this.bBuffer = await this.gpu?.createBuffer(this.bias, "storage");
|
|
64
|
+
this.forwardPipline = await this.gpu?.getPiplineShader("linear/forward.wgsl");
|
|
65
|
+
}
|
|
66
|
+
let tokenCount = input.buffer.size / (this.embeddingSize * 4);
|
|
67
|
+
if (!this.paramsForward) {
|
|
68
|
+
this.paramsForward = await this.gpu?.createBuffer(new Uint32Array([this.embeddingSize, this.vocabSize, tokenCount]), "uniform");
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
await this.paramsForward.changeData(new Uint32Array([this.embeddingSize, this.vocabSize, tokenCount]));
|
|
72
|
+
}
|
|
73
|
+
if (!this.output || this.output.buffer.size !== tokenCount * this.vocabSize * 4) {
|
|
74
|
+
if (this.output)
|
|
75
|
+
this.output?.destroy();
|
|
76
|
+
this.output = await this.gpu?.createBuffer(new Float32Array(this.vocabSize * tokenCount), "storage");
|
|
77
|
+
}
|
|
78
|
+
const bindGroup = await this.gpu?.createBindGroup(this.forwardPipline, [input, this.wBuffer, this.bBuffer, this.output, this.paramsForward], 0);
|
|
79
|
+
await this.gpu?.RunPipline(encoder, this.forwardPipline, bindGroup, this.vocabSize, tokenCount);
|
|
80
|
+
if (!en)
|
|
81
|
+
await this.gpu?.SumbitEncoder(encoder);
|
|
82
|
+
return this.output;
|
|
83
|
+
}
|
|
84
|
+
async backward(outputGradient, lr, encoderShared) {
|
|
85
|
+
let tokenCount = outputGradient.buffer.size / (4 * this.vocabSize);
|
|
86
|
+
let encoder = encoderShared ? encoderShared : await this.gpu?.CreateEncoder();
|
|
87
|
+
if (!this.weightUpdate) {
|
|
88
|
+
this.weightUpdate = await this.gpu?.getPiplineShader("linear/updateWeights.wgsl");
|
|
89
|
+
this.biasUpdate = await this.gpu?.getPiplineShader("linear/updateBias.wgsl");
|
|
90
|
+
this.backwardPipline = await this.gpu?.getPiplineShader("linear/ComputeDinput.wgsl");
|
|
91
|
+
}
|
|
92
|
+
if (!this.paramsBackward) {
|
|
93
|
+
this.paramsBackward = await this.gpu?.createBuffer(new Float32Array([this.embeddingSize, this.vocabSize, tokenCount, lr]), "uniform");
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
await this.paramsBackward.changeData(new Float32Array([this.embeddingSize, this.vocabSize, tokenCount, lr]));
|
|
97
|
+
}
|
|
98
|
+
if (!this.gradOut || this.gradOut.buffer.size !== tokenCount * this.embeddingSize * 4) {
|
|
99
|
+
if (this.gradOut)
|
|
100
|
+
this.gradOut.destroy();
|
|
101
|
+
this.gradOut = await this.gpu?.createBuffer(new Float32Array(this.embeddingSize * tokenCount), "storage");
|
|
102
|
+
}
|
|
103
|
+
const bindGroup2 = await this.gpu?.createBindGroup(this.backwardPipline, [this.wBuffer, outputGradient, this.gradOut, this.paramsBackward], 0);
|
|
104
|
+
await this.gpu?.RunPipline(encoder, this.backwardPipline, bindGroup2, tokenCount, this.embeddingSize);
|
|
105
|
+
if (!encoderShared)
|
|
106
|
+
await this.gpu?.SumbitEncoder(encoder);
|
|
107
|
+
return this.gradOut;
|
|
108
|
+
}
|
|
109
|
+
async updateParameter(outputGradient, lr, encoderShared) {
|
|
110
|
+
let tokenCount = outputGradient.buffer.size / (4 * this.vocabSize);
|
|
111
|
+
let encoder = encoderShared ? encoderShared : await this.gpu?.CreateEncoder();
|
|
112
|
+
if (!this.weightUpdate) {
|
|
113
|
+
this.weightUpdate = await this.gpu?.getPiplineShader("linear/updateWeights.wgsl");
|
|
114
|
+
this.biasUpdate = await this.gpu?.getPiplineShader("linear/updateBias.wgsl");
|
|
115
|
+
}
|
|
116
|
+
if (!this.paramsBackward) {
|
|
117
|
+
this.paramsBackward = await this.gpu?.createBuffer(new Float32Array([this.embeddingSize, this.vocabSize, tokenCount, lr]), "uniform");
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
await this.paramsBackward.changeData(new Float32Array([this.embeddingSize, this.vocabSize, tokenCount, lr]));
|
|
121
|
+
}
|
|
122
|
+
const bindGroup = await this.gpu?.createBindGroup(this.weightUpdate, [this.wBuffer, outputGradient, this.paramsBackward, this.input], 0);
|
|
123
|
+
const bindGroup1 = await this.gpu?.createBindGroup(this.biasUpdate, [this.bBuffer, outputGradient, this.paramsBackward], 0);
|
|
124
|
+
await this.gpu?.RunPipline(encoder, this.weightUpdate, bindGroup, Math.ceil(this.embeddingSize / 16), Math.ceil(this.vocabSize / 32));
|
|
125
|
+
await this.gpu?.RunPipline(encoder, this.biasUpdate, bindGroup1, Math.ceil(this.vocabSize / 32));
|
|
126
|
+
if (!encoderShared)
|
|
127
|
+
await this.gpu?.SumbitEncoder(encoder);
|
|
128
|
+
return this.gradOut;
|
|
129
|
+
}
|
|
130
|
+
async Save() {
|
|
131
|
+
const bias = await this.bBuffer?.read();
|
|
132
|
+
const weights = await this.wBuffer?.read();
|
|
133
|
+
SaveFileVector(this.configs.filename[0], weights);
|
|
134
|
+
SaveFileVector(this.configs.filename[1], bias);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { GPUManager, Buffer } from "@imaduddin/webgpu-manager";
|
|
2
|
+
export declare class RMSNorm {
|
|
3
|
+
device: GPUDevice;
|
|
4
|
+
embeddingSize: number;
|
|
5
|
+
rms: Buffer | undefined;
|
|
6
|
+
input: Buffer | undefined;
|
|
7
|
+
dInput: Buffer | undefined;
|
|
8
|
+
params: Buffer | undefined;
|
|
9
|
+
output: Buffer | undefined;
|
|
10
|
+
forwardShader: GPUComputePipeline | undefined;
|
|
11
|
+
backwardShader: GPUComputePipeline | undefined;
|
|
12
|
+
gpu: GPUManager | undefined;
|
|
13
|
+
constructor(device: GPUDevice, embeddingSize: number);
|
|
14
|
+
forward(input: Buffer, encoderShared: GPUCommandEncoder | undefined): Promise<Buffer | undefined>;
|
|
15
|
+
backward(outputGradient: Buffer, encoderShared: GPUCommandEncoder | undefined): Promise<Buffer | undefined>;
|
|
16
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { GPUManager } from "@imaduddin/webgpu-manager";
|
|
2
|
+
export class RMSNorm {
|
|
3
|
+
device;
|
|
4
|
+
embeddingSize;
|
|
5
|
+
rms;
|
|
6
|
+
input;
|
|
7
|
+
dInput;
|
|
8
|
+
params;
|
|
9
|
+
output;
|
|
10
|
+
forwardShader;
|
|
11
|
+
backwardShader;
|
|
12
|
+
gpu;
|
|
13
|
+
constructor(device, embeddingSize) {
|
|
14
|
+
this.device = device;
|
|
15
|
+
this.embeddingSize = embeddingSize;
|
|
16
|
+
this.gpu = new GPUManager(device, {
|
|
17
|
+
shaderDir: "./shaders/rmsnorm"
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
async forward(input, encoderShared) {
|
|
21
|
+
this.input = input;
|
|
22
|
+
let tokenCount = input.buffer.size / (this.embeddingSize * 4);
|
|
23
|
+
if (!this.rms || this.rms.buffer.size !== tokenCount * 4) {
|
|
24
|
+
if (this.rms)
|
|
25
|
+
this.rms.destroy();
|
|
26
|
+
this.rms = await this.gpu?.createBuffer(new Float32Array(tokenCount), "storage");
|
|
27
|
+
}
|
|
28
|
+
if (!this.output || this.output.buffer.size !== tokenCount * this.embeddingSize * 4) {
|
|
29
|
+
if (this.output)
|
|
30
|
+
this.output.destroy();
|
|
31
|
+
if (this.dInput)
|
|
32
|
+
this.dInput.destroy();
|
|
33
|
+
this.output = await this.gpu?.createBuffer(new Float32Array(tokenCount * this.embeddingSize), "storage");
|
|
34
|
+
this.dInput = await this.gpu?.createBuffer(new Float32Array(tokenCount * this.embeddingSize), "storage");
|
|
35
|
+
}
|
|
36
|
+
if (!this.params) {
|
|
37
|
+
this.params = await this.gpu?.createBuffer(new Uint32Array([this.embeddingSize, tokenCount, 0]), "uniform");
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
await this.params.changeData(new Uint32Array([this.embeddingSize, tokenCount, 0]));
|
|
41
|
+
}
|
|
42
|
+
if (!this.forwardShader) {
|
|
43
|
+
this.forwardShader = await this.gpu?.getPiplineShader("forward.wgsl");
|
|
44
|
+
this.backwardShader = await this.gpu?.getPiplineShader("backward.wgsl");
|
|
45
|
+
}
|
|
46
|
+
const encoder = encoderShared ? encoderShared : await this.gpu?.CreateEncoder();
|
|
47
|
+
const bindGroup = await this.gpu?.createBindGroup(this.forwardShader, [this.input, this.output, this.rms, this.params], 0);
|
|
48
|
+
await this.gpu?.RunPipline(encoder, this.forwardShader, bindGroup, tokenCount);
|
|
49
|
+
if (!encoderShared) {
|
|
50
|
+
this.gpu?.SumbitEncoder(encoder);
|
|
51
|
+
}
|
|
52
|
+
return this.output;
|
|
53
|
+
}
|
|
54
|
+
async backward(outputGradient, encoderShared) {
|
|
55
|
+
const tokenCount = outputGradient.buffer.size / (this.embeddingSize * 4);
|
|
56
|
+
const encoder = encoderShared ? encoderShared : await this.gpu?.CreateEncoder();
|
|
57
|
+
const bindGroup = await this.gpu?.createBindGroup(this.backwardShader, [this.input, this.rms, outputGradient, this.dInput, this.params], 0);
|
|
58
|
+
await this.gpu?.RunPipline(encoder, this.backwardShader, bindGroup, tokenCount);
|
|
59
|
+
if (!encoderShared)
|
|
60
|
+
await this.gpu?.SumbitEncoder(encoder);
|
|
61
|
+
return this.dInput;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Buffer, GPUManager } from "@imaduddin/webgpu-manager";
|
|
2
|
+
export declare class Relu {
|
|
3
|
+
device: GPUDevice;
|
|
4
|
+
hiddenSize: number;
|
|
5
|
+
input: Buffer | undefined;
|
|
6
|
+
output: Buffer | undefined;
|
|
7
|
+
outputGrad: Buffer | undefined;
|
|
8
|
+
params: Buffer | undefined;
|
|
9
|
+
forwardShader: GPUComputePipeline | undefined;
|
|
10
|
+
backwardShader: GPUComputePipeline | undefined;
|
|
11
|
+
gpu: GPUManager | undefined;
|
|
12
|
+
constructor(device: GPUDevice, hiddenSize: number);
|
|
13
|
+
forward(input: Buffer, encoderShared: GPUCommandEncoder | undefined): Promise<Buffer | undefined>;
|
|
14
|
+
backward(gradient: Buffer, encoderShared: GPUCommandEncoder | undefined): Promise<Buffer | undefined>;
|
|
15
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { GPUManager } from "@imaduddin/webgpu-manager";
|
|
2
|
+
export class Relu {
|
|
3
|
+
device;
|
|
4
|
+
hiddenSize;
|
|
5
|
+
input;
|
|
6
|
+
output;
|
|
7
|
+
outputGrad;
|
|
8
|
+
params;
|
|
9
|
+
forwardShader;
|
|
10
|
+
backwardShader;
|
|
11
|
+
gpu;
|
|
12
|
+
constructor(device, hiddenSize) {
|
|
13
|
+
this.device = device;
|
|
14
|
+
this.hiddenSize = hiddenSize;
|
|
15
|
+
this.gpu = new GPUManager(device, {
|
|
16
|
+
shaderDir: "./shaders/relu"
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
async forward(input, encoderShared) {
|
|
20
|
+
let tokenCount = input.buffer.size / (this.hiddenSize * 4);
|
|
21
|
+
this.input = input;
|
|
22
|
+
if (!this.output || this.output.buffer.size !== input.buffer.size) {
|
|
23
|
+
if (this.output)
|
|
24
|
+
this.output.destroy();
|
|
25
|
+
this.output = await this.gpu?.createBuffer(new Float32Array(this.hiddenSize * tokenCount), "storage");
|
|
26
|
+
}
|
|
27
|
+
if (!this.forwardShader) {
|
|
28
|
+
this.forwardShader = await this.gpu?.getPiplineShader("forward.wgsl");
|
|
29
|
+
this.backwardShader = await this.gpu?.getPiplineShader("backward.wgsl");
|
|
30
|
+
}
|
|
31
|
+
if (!this.params) {
|
|
32
|
+
this.params = await this.gpu?.createBuffer(new Uint32Array([this.hiddenSize, tokenCount]), "uniform");
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
await this.params.changeData(new Uint32Array([this.hiddenSize, tokenCount]));
|
|
36
|
+
}
|
|
37
|
+
let encoder = encoderShared ? encoderShared : await this.gpu?.CreateEncoder();
|
|
38
|
+
const bindGroup = await this.gpu?.createBindGroup(this.forwardShader, [this.input, this.output, this.params], 0);
|
|
39
|
+
await this.gpu?.RunPipline(encoder, this.forwardShader, bindGroup, Math.ceil(this.hiddenSize / 16), Math.ceil(tokenCount / 16));
|
|
40
|
+
if (!encoderShared)
|
|
41
|
+
await this.gpu?.SumbitEncoder(encoder);
|
|
42
|
+
return this.output;
|
|
43
|
+
}
|
|
44
|
+
async backward(gradient, encoderShared) {
|
|
45
|
+
let tokenCount = gradient.buffer.size / (this.hiddenSize * 4);
|
|
46
|
+
let encoder = encoderShared ? encoderShared : await this.gpu?.CreateEncoder();
|
|
47
|
+
if (!this.outputGrad || this.outputGrad.buffer.size !== gradient.buffer.size) {
|
|
48
|
+
if (this.outputGrad)
|
|
49
|
+
this.outputGrad.destroy();
|
|
50
|
+
this.outputGrad = await this.gpu?.createBuffer(new Float32Array(gradient.buffer.size / 4), "storage");
|
|
51
|
+
}
|
|
52
|
+
const bindGroup = await this.gpu?.createBindGroup(this.backwardShader, [this.input, this.outputGrad, gradient, this.params], 0);
|
|
53
|
+
await this.gpu?.RunPipline(encoder, this.backwardShader, bindGroup, Math.ceil(this.hiddenSize / 16), Math.ceil(tokenCount / 16));
|
|
54
|
+
if (!encoderShared)
|
|
55
|
+
await this.gpu?.SumbitEncoder(encoder);
|
|
56
|
+
return this.outputGrad;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { Linear } from "../Linear/index.js";
|
|
2
|
+
import { GPUManager, Buffer } from "@imaduddin/webgpu-manager";
|
|
3
|
+
export declare class SelfAttention {
|
|
4
|
+
device: GPUDevice;
|
|
5
|
+
embeddingSize: number;
|
|
6
|
+
configs: {
|
|
7
|
+
dirname: string;
|
|
8
|
+
};
|
|
9
|
+
query: Linear | undefined;
|
|
10
|
+
value: Linear | undefined;
|
|
11
|
+
key: Linear | undefined;
|
|
12
|
+
v: Buffer | undefined;
|
|
13
|
+
q: Buffer | undefined;
|
|
14
|
+
weights: Buffer | undefined;
|
|
15
|
+
k: Buffer | undefined;
|
|
16
|
+
gpu: GPUManager | undefined;
|
|
17
|
+
params: Buffer | undefined;
|
|
18
|
+
Softparams: Buffer | undefined;
|
|
19
|
+
Softbackparams: Buffer | undefined;
|
|
20
|
+
Transparams: Buffer | undefined;
|
|
21
|
+
matrixMat: GPUComputePipeline | undefined;
|
|
22
|
+
transposeShader: GPUComputePipeline | undefined;
|
|
23
|
+
addShader: GPUComputePipeline | undefined;
|
|
24
|
+
softmaxShader: GPUComputePipeline | undefined;
|
|
25
|
+
softmaxBackShader: GPUComputePipeline | undefined;
|
|
26
|
+
qkT: Buffer | undefined;
|
|
27
|
+
transPosedKey: Buffer | undefined;
|
|
28
|
+
output: Buffer | undefined;
|
|
29
|
+
qKTShader: GPUComputePipeline | undefined;
|
|
30
|
+
dQ: Buffer | undefined;
|
|
31
|
+
dK: Buffer | undefined;
|
|
32
|
+
dV: Buffer | undefined;
|
|
33
|
+
dWeights: Buffer | undefined;
|
|
34
|
+
dScores: Buffer | undefined;
|
|
35
|
+
dInput: Buffer | undefined;
|
|
36
|
+
addParams: Buffer | undefined;
|
|
37
|
+
constructor(device: GPUDevice, embeddingSize: number, configs: {
|
|
38
|
+
dirname: string;
|
|
39
|
+
});
|
|
40
|
+
softmaxBackward(weights: Buffer, dWeigths: Buffer, output: Buffer, tokenCount: number, enocder: GPUCommandEncoder): Promise<void>;
|
|
41
|
+
MatMut(a: Buffer, b: Buffer, output: Buffer, M: number, N: number, K: number, encoderS: GPUCommandEncoder, isqK: boolean): Promise<void>;
|
|
42
|
+
transpose(a: Buffer, output: Buffer, M: number, N: number, encoder: GPUCommandEncoder): Promise<void>;
|
|
43
|
+
forward(input: Buffer): Promise<Buffer | undefined>;
|
|
44
|
+
Save(): Promise<void>;
|
|
45
|
+
add3(a: Buffer, b: Buffer, c: Buffer, out: Buffer, size: number, encoder: GPUCommandEncoder): Promise<void>;
|
|
46
|
+
backward(outputGradient: Buffer, lr: number): Promise<Buffer | undefined>;
|
|
47
|
+
}
|