-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmatmul_packedbuffer.ts
More file actions
189 lines (158 loc) · 6.13 KB
/
Copy pathmatmul_packedbuffer.ts
File metadata and controls
189 lines (158 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import {Glslang} from '@webgpu/glslang/dist/web-devel/glslang.onefile';
import {BufferOp} from './buffer';
export class MatmulPackedBufferOp extends BufferOp {
workGroupSize: [number, number, number];
workPerThread: number;
constructor(
device: GPUDevice, glslang: Glslang,
firstMatrix: Float32Array|Uint32Array,
secondMatrix: Float32Array|Uint32Array, shape: Uint32Array,
workPerThread = 1) {
super(device, glslang);
const TS = 16;
const TS_Y = 16;
this.workGroupSize = [TS, TS_Y, 1];
this.workPerThread = workPerThread;
this.compile(firstMatrix, secondMatrix, shape, this.getShader());
}
executeSync() {
const result =
this.compileAndRunSync(this.workGroupSize, this.workPerThread);
return result;
}
// Experimental. DO not USE!
async execute(
firstMatrix: Float32Array|Uint32Array,
secondMatrix: Float32Array|Uint32Array, shape: Uint32Array, mode = 0) {
const result = await this.compileAndRun(this.workGroupSize);
return result;
}
private getShader() {
// Compute shader code (GLSL)
// https://github.com/tensorflow/tfjs/blob/master/tfjs-backend-webgpu/src/kernels/matmul_packed_webgpu.ts
const computeShaderCode = `#version 450
layout(local_size_x = ${this.workGroupSize[0]}, local_size_y = ${
this.workGroupSize[1]}, local_size_z = 1) in;
/* TODO.
layout(std140, set = 0, binding = 0) uniform Uniforms {
ivec3 aShape; ivec3 bShape; ivec3 outShape;
};
*/
layout(set = 0, binding = 0) uniform Uniforms {
int inputWidth;
int inputHeight;
int filterWidth;
int filterHeight;
int outputWidth;
int outputHeight;
};
layout(std430, set = 0, binding = 1) readonly buffer ssbA {
float A[];
};
layout(std430, set = 0, binding = 2) readonly buffer ssbB {
float B[];
};
layout(std430, set = 0, binding = 3) writeonly buffer ssbOut {
float result[];
};
void setOutput(int flatIndex, float value) {
result[flatIndex] = value;
}
// TODO.
int dimAOuter = inputWidth; // aShape[1];
int dimInner = filterWidth; // aShape[2];
int dimBOuter = outputWidth;// bShape[2];
float mm_readA(int row, int col);
float mm_readB(int row, int col);
void mm_write(int row, int col, float value);
void mm_matMul(int dimAOuter, int dimInner, int dimBOuter);
const int RowPerThread = ${this.workPerThread};
const int ColPerThread = ${this.workPerThread};
const int TileAOuter = int(gl_WorkGroupSize.y) * RowPerThread;
const int TileBOuter = int(gl_WorkGroupSize.x) * ColPerThread;
const int TileInner = TileAOuter > TileBOuter ? TileAOuter : TileBOuter;
shared float mm_Asub[TileAOuter][TileInner];
shared float mm_Bsub[TileInner][TileBOuter];
void mm_matMul(int dimAOuter, int dimInner, int dimBOuter) {
int tileRow = int(gl_LocalInvocationID.y) * RowPerThread;
int tileCol = int(gl_LocalInvocationID.x) * ColPerThread;
int globalRow = int(gl_GlobalInvocationID.y) * RowPerThread;
int globalCol = int(gl_GlobalInvocationID.x) * ColPerThread;
int numTiles = (dimInner - 1) / TileInner + 1;
float acc[RowPerThread][ColPerThread];
float ACached;
float BCached[ColPerThread];
// Without this initialization strange values show up in acc.
for (int innerRow = 0; innerRow < RowPerThread; innerRow++) {
for (int innerCol = 0; innerCol < ColPerThread; innerCol++) {
acc[innerRow][innerCol] = 0.0;
}
}
const int ColPerThreadA = TileInner / int(gl_WorkGroupSize.x);
int tileColA = int(gl_LocalInvocationID.x) * ColPerThreadA;
const int RowPerThreadB = TileInner / int(gl_WorkGroupSize.y);
int tileRowB = int(gl_LocalInvocationID.y) * RowPerThreadB;
// Loop over shared dimension.
for (int t = 0; t < numTiles; t++) {
// Load one tile of A into local memory.
for (int innerRow = 0; innerRow < RowPerThread; innerRow++) {
for (int innerCol = 0; innerCol < ColPerThreadA; innerCol++) {
int inputRow = tileRow + innerRow;
int inputCol = tileColA + innerCol;
mm_Asub[inputRow][inputCol] = mm_readA(
globalRow + innerRow,
t * TileInner + inputCol);
}
}
// Load one tile of B into local memory.
for (int innerRow = 0; innerRow < RowPerThreadB; innerRow++) {
for (int innerCol = 0; innerCol < ColPerThread; innerCol++) {
int inputRow = tileRowB + innerRow;
int inputCol = tileCol + innerCol;
mm_Bsub[inputRow][inputCol] = mm_readB(
t * TileInner + inputRow,
globalCol + innerCol);;
}
}
barrier();
// Compute acc values for a single thread.
for (int k = 0; k < TileInner; k++) {
for (int inner = 0; inner < ColPerThread; inner++) {
BCached[inner] = mm_Bsub[k][tileCol + inner];
}
for (int innerRow = 0; innerRow < RowPerThread; innerRow++) {
ACached = mm_Asub[tileRow + innerRow][k];
for (int innerCol = 0; innerCol < ColPerThread; innerCol++) {
acc[innerRow][innerCol] += ACached * BCached[innerCol];
}
}
}
barrier();
}
for (int innerRow = 0; innerRow < RowPerThread; innerRow++) {
for (int innerCol = 0; innerCol < ColPerThread; innerCol++) {
if ((globalCol + innerCol) < dimBOuter &&
(globalRow + innerRow) < dimAOuter) {
mm_write(globalRow + innerRow,
globalCol + innerCol,
acc[innerRow][innerCol]);
}
}
}
}
float mm_readA(int row, int col) {
return A[row * dimInner + col];
}
float mm_readB(int row, int col) {
return B[row * dimBOuter + col];
}
void mm_write(int row, int col, float value) {
setOutput(row * dimBOuter + col, value);
}
void main() {
mm_matMul(dimAOuter, dimInner, dimBOuter);
}
`;
return computeShaderCode;
}
}