@mineproness/tensorloop 2.1.71 → 2.1.73
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/package.json
CHANGED
package/src/RMSNorm/index.js
CHANGED
|
@@ -49,7 +49,7 @@ export class RMSNorm {
|
|
|
49
49
|
const output = await this.op.createBuffer(input.size);
|
|
50
50
|
const bindGroup = await this.op.createBindGroup(this.reductionShader, 0, [input, outputReduce])
|
|
51
51
|
const encoder = await this.op.CreateEncoder()
|
|
52
|
-
await this.op.RunPipline(encoder, this.reductionShader, bindGroup,
|
|
52
|
+
await this.op.RunPipline(encoder, this.reductionShader, bindGroup, context)
|
|
53
53
|
const bindGroup1 = await this.op.createBindGroup(this.forwardShader, 0, [outputReduce, input, output, this.gammabuffer])
|
|
54
54
|
await this.op.RunPipline(encoder, this.forwardShader, bindGroup1, Math.ceil((input.size / 4) / 64));
|
|
55
55
|
await this.op.submitQueue(encoder);
|
|
@@ -68,7 +68,7 @@ export class RMSNorm {
|
|
|
68
68
|
const bindGroup = await this.op.createBindGroup(this.backwardShader, 0, [this.input, outputGradient, this.rms, this.gammabuffer, output]);
|
|
69
69
|
const bindGroup2 = await this.op.createBindGroup(this.updateGamma, 0, [this.input, outputGradient, this.rms, this.gammabuffer, params]);
|
|
70
70
|
const encoder = await this.op.CreateEncoder()
|
|
71
|
-
await this.op.RunPipline(encoder, this.backwardShader, bindGroup, Math.ceil((this.input.size /
|
|
71
|
+
await this.op.RunPipline(encoder, this.backwardShader, bindGroup, Math.ceil((this.input.size) / (this.embeddingSize * 4)))
|
|
72
72
|
await this.op.RunPipline(encoder, this.updateGamma, bindGroup2, this.embeddingSize)
|
|
73
73
|
await this.op.submitQueue(encoder)
|
|
74
74
|
if (this.prevOut) this.prevOut.destroy()
|
package/src/Softmax/index.js
CHANGED
|
@@ -111,7 +111,7 @@ export class Softmax {
|
|
|
111
111
|
}
|
|
112
112
|
]
|
|
113
113
|
})
|
|
114
|
-
await this.gpu.RunPipline(encoder, this.backwardPipline, bind,
|
|
114
|
+
await this.gpu.RunPipline(encoder, this.backwardPipline, bind, tokenCount)
|
|
115
115
|
this.device.queue.submit([encoder.finish()])
|
|
116
116
|
probs.destroy()
|
|
117
117
|
targetBuffer.destroy()
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
+
|
|
1
2
|
@group(0) @binding(0)
|
|
2
3
|
var<storage, read> input: array<f32>;
|
|
3
4
|
|
|
4
5
|
@group(0) @binding(1)
|
|
5
6
|
var<storage, read> gradOutput: array<f32>;
|
|
6
|
-
|
|
7
7
|
@group(0) @binding(2)
|
|
8
|
-
var<storage, read>
|
|
8
|
+
var<storage, read> sumSquares: array<f32>;
|
|
9
9
|
|
|
10
10
|
@group(0) @binding(3)
|
|
11
11
|
var<storage, read> gamma: array<f32>;
|
|
@@ -13,30 +13,53 @@ var<storage, read> gamma: array<f32>;
|
|
|
13
13
|
@group(0) @binding(4)
|
|
14
14
|
var<storage, read_write> gradInput: array<f32>;
|
|
15
15
|
|
|
16
|
-
var<workgroup> partial: array<f32,
|
|
16
|
+
var<workgroup> partial: array<f32, 64>;
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
const D: u32 = %embeddingSize%u;
|
|
19
|
+
const EPSILON: f32 = 1e-6;
|
|
20
|
+
@compute @workgroup_size(64)
|
|
19
21
|
fn main(@builtin(local_invocation_index) lid: u32, @builtin(workgroup_id) wid: vec3<u32>) {
|
|
20
|
-
let D = %embeddingSize%u;
|
|
21
22
|
let token = wid.x;
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
let
|
|
26
|
-
let r =
|
|
27
|
-
|
|
23
|
+
if (token >= arrayLength(&sumSquares)) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
let base = token * D;
|
|
27
|
+
let r = sqrt(sumSquares[token] / f32(D) + EPSILON);
|
|
28
|
+
var localSum = 0.0;
|
|
29
|
+
var d = lid;
|
|
30
|
+
while (d < D) {
|
|
31
|
+
let idx = base + d;
|
|
32
|
+
localSum += gradOutput[idx] * gamma[d] * input[idx];
|
|
33
|
+
d += 64u;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
partial[lid] = localSum;
|
|
28
37
|
workgroupBarrier();
|
|
29
|
-
var stride =
|
|
38
|
+
var stride = 32u;
|
|
30
39
|
loop {
|
|
31
40
|
if (stride == 0u) {
|
|
32
41
|
break;
|
|
33
42
|
}
|
|
43
|
+
|
|
34
44
|
if (lid < stride) {
|
|
35
45
|
partial[lid] += partial[lid + stride];
|
|
36
46
|
}
|
|
47
|
+
|
|
37
48
|
workgroupBarrier();
|
|
38
49
|
stride /= 2u;
|
|
39
50
|
}
|
|
40
51
|
let dot = partial[0];
|
|
41
|
-
|
|
52
|
+
d = lid;
|
|
53
|
+
while (d < D) {
|
|
54
|
+
let idx = base + d;
|
|
55
|
+
let x = input[idx];
|
|
56
|
+
let dy = gradOutput[idx];
|
|
57
|
+
let g = gamma[d];
|
|
58
|
+
|
|
59
|
+
gradInput[idx] =
|
|
60
|
+
(dy * g) / r
|
|
61
|
+
- (x * dot) / (f32(D) * r * r * r);
|
|
62
|
+
|
|
63
|
+
d += 64u;
|
|
64
|
+
}
|
|
42
65
|
}
|
|
@@ -1,20 +1,23 @@
|
|
|
1
|
+
|
|
1
2
|
@group(0) @binding(0)
|
|
2
3
|
var<storage, read> input: array<f32>;
|
|
3
4
|
|
|
4
5
|
@group(0) @binding(1)
|
|
5
6
|
var<storage, read_write> out: array<f32>;
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const embeddingSize : u32 = %embeddingSize%u;
|
|
8
|
+
var<workgroup> partial: array<f32, 64>;
|
|
9
|
+
const D: u32 = %embeddingSize%u;
|
|
11
10
|
@compute @workgroup_size(64)
|
|
12
11
|
fn main(@builtin(local_invocation_index) lid: u32, @builtin(workgroup_id) wid: vec3<u32>) {
|
|
13
|
-
let
|
|
14
|
-
let index = wid.x * dim + lid;
|
|
12
|
+
let base = wid.x * D;
|
|
15
13
|
var sum = 0.0;
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
var d = lid;
|
|
15
|
+
while (d < D) {
|
|
16
|
+
let idx = base + d;
|
|
17
|
+
if (idx < arrayLength(&input)) {
|
|
18
|
+
sum += input[idx] * input[idx];
|
|
19
|
+
}
|
|
20
|
+
d += 64u;
|
|
18
21
|
}
|
|
19
22
|
partial[lid] = sum;
|
|
20
23
|
workgroupBarrier();
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
|
|
1
2
|
@group(0) @binding(0)
|
|
2
3
|
var<storage, read> input: array<f32>;
|
|
3
4
|
|
|
@@ -5,7 +6,7 @@ var<storage, read> input: array<f32>;
|
|
|
5
6
|
var<storage, read> gradOutput: array<f32>;
|
|
6
7
|
|
|
7
8
|
@group(0) @binding(2)
|
|
8
|
-
var<storage, read>
|
|
9
|
+
var<storage, read> sumSquares: array<f32>;
|
|
9
10
|
|
|
10
11
|
@group(0) @binding(3)
|
|
11
12
|
var<storage, read_write> gamma: array<f32>;
|
|
@@ -13,49 +14,40 @@ var<storage, read_write> gamma: array<f32>;
|
|
|
13
14
|
@group(0) @binding(4)
|
|
14
15
|
var<storage, read> params: array<f32>;
|
|
15
16
|
|
|
16
|
-
var<workgroup> partial: array<f32,
|
|
17
|
+
var<workgroup> partial: array<f32, 64>;
|
|
17
18
|
|
|
19
|
+
const D: u32 = %embeddingSize%u;
|
|
20
|
+
const EPSILON: f32 = 1e-6;
|
|
18
21
|
@compute @workgroup_size(64)
|
|
19
22
|
fn main(@builtin(local_invocation_index) lid: u32, @builtin(workgroup_id) wid: vec3<u32>) {
|
|
20
|
-
let D = %embeddingSize%u;
|
|
21
23
|
let dim = wid.x;
|
|
22
|
-
|
|
23
24
|
if (dim >= D) {
|
|
24
25
|
return;
|
|
25
26
|
}
|
|
26
|
-
|
|
27
|
-
let T = arrayLength(&rms);
|
|
27
|
+
let T = arrayLength(&sumSquares);
|
|
28
28
|
let lr = params[0];
|
|
29
|
-
|
|
30
29
|
var sum = 0.0;
|
|
31
30
|
var token = lid;
|
|
32
|
-
|
|
33
31
|
while (token < T) {
|
|
34
32
|
let idx = token * D + dim;
|
|
35
|
-
|
|
36
|
-
sum += gradOutput[idx] * input[idx] /
|
|
37
|
-
|
|
33
|
+
let r = sqrt(sumSquares[token] / f32(D) + EPSILON);
|
|
34
|
+
sum += gradOutput[idx] * input[idx] / r;
|
|
38
35
|
token += 64u;
|
|
39
36
|
}
|
|
40
|
-
|
|
41
37
|
partial[lid] = sum;
|
|
42
38
|
workgroupBarrier();
|
|
43
|
-
|
|
44
39
|
var stride = 32u;
|
|
45
40
|
loop {
|
|
46
41
|
if (stride == 0u) {
|
|
47
42
|
break;
|
|
48
43
|
}
|
|
49
|
-
|
|
50
44
|
if (lid < stride) {
|
|
51
45
|
partial[lid] += partial[lid + stride];
|
|
52
46
|
}
|
|
53
|
-
|
|
54
47
|
workgroupBarrier();
|
|
55
48
|
stride /= 2u;
|
|
56
49
|
}
|
|
57
50
|
if (lid == 0u) {
|
|
58
|
-
|
|
59
|
-
gamma[dim] -= lr * gradGamma;
|
|
51
|
+
gamma[dim] -= lr * partial[0];
|
|
60
52
|
}
|
|
61
53
|
}
|