forked from tensorflow/tfjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshader_preprocessor.ts
428 lines (367 loc) · 11.9 KB
/
shader_preprocessor.ts
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/**
* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
import {backend_util, DataType, util} from '@tensorflow/tfjs-core';
import {symbolicallyComputeStrides} from './shader_util';
export function getCoordsDataType(rank: number): string {
if (rank <= 1) {
return 'int';
} else if (rank === 2) {
return 'ivec2';
} else if (rank === 3) {
return 'ivec3';
} else if (rank === 4) {
return 'ivec4';
} else {
throw Error(`GPU for rank ${rank} is not yet supported`);
}
}
type GLSLDataType = 'float'|'int';
function mapToGlslTypes(type: DataType): GLSLDataType|DataType {
if (type === 'float32') {
return 'float';
}
if (type === 'int32') {
return 'int';
}
return type;
}
interface ProgramParams {
dispatchLayout: {x: number[], y?: number[], z?: number[]};
workGroupSize?: [number, number, number];
variableNames: string[];
uniforms?: string;
needsShapesUniforms: boolean;
userCode: string;
}
export interface InputInfo {
dtype: DataType;
shape: number[];
name: string;
}
export function makeShader(
inputInfo: InputInfo[], outputData: {dtype: DataType, shape: number[]},
program: ProgramParams): string {
const prefixSnippets: string[] = [];
if (program.workGroupSize != null) {
prefixSnippets.push(`
layout (local_size_x = ${program.workGroupSize[0]},
local_size_y = ${program.workGroupSize[1]},
local_size_z = ${program.workGroupSize[2]}) in;
`);
}
// Output buffer.
prefixSnippets.push(`
layout(std430, set = 0, binding = 0) writeonly buffer ssbOut {
${mapToGlslTypes(outputData.dtype)} result[];
};
`);
program.variableNames.forEach((x, i) => {
prefixSnippets.push(`
layout(std430, set = 0, binding = ${1 + i}) readonly buffer ssb${x} {
${mapToGlslTypes(inputInfo[i].dtype)} ${x}[];
};
`);
});
let uniformDeclaration = '';
if (program.needsShapesUniforms) {
program.variableNames.forEach((x, i) => {
uniformDeclaration += `${getCoordsDataType(inputInfo[i].shape.length)} ${
x.charAt(0).toLowerCase() + x.slice(1)}Shape; `;
});
uniformDeclaration +=
`${getCoordsDataType(outputData.shape.length)} outShape; `;
}
if (program.uniforms) {
uniformDeclaration += program.uniforms;
}
if (!(program.uniforms || program.needsShapesUniforms)) {
const sources = [
SHADER_PREFIX, prefixSnippets.join('\n'),
getSetOutputSnippet(
outputData.shape.length, outputData.dtype,
program.needsShapesUniforms),
program.userCode
];
const source = sources.join('\n');
return source;
}
prefixSnippets.push(`
layout(std140, set = 0, binding = ${
1 + program.variableNames.length}) uniform Uniforms {
${uniformDeclaration}
};
`);
const [getOutputCoords, dispatchLayoutRank] =
generateGetOutputCoords(program.dispatchLayout);
const getCoords = generateGetCoordsFromFlatIndex(outputData.shape);
const sources = [
SHADER_PREFIX, prefixSnippets.join('\n'), SAMPLING_SNIPPETS,
getOutputCoords, getCoords,
getSetOutputSnippet(
outputData.shape.length, outputData.dtype, program.needsShapesUniforms)
];
if (dispatchLayoutRank === outputData.shape.length) {
// Input sampling snippet is only meaningful when the output isn't getting
// implicitly reshaped (like it does in conv2d_matmul).
const inputSamplingSnippet =
inputInfo.map(x => getInputSamplingSnippet(x, outputData.shape))
.join('\n');
sources.push(inputSamplingSnippet);
}
sources.push(program.userCode);
const source = sources.join('\n');
return source;
}
const SHADER_PREFIX = `#version 450
int idiv(int a, int b, float sign) {
int res = a / b;
int mod = a % b;
if (sign < 0. && mod != 0) {
res -= 1;
}
return res;
}
// Checks whether coordinates lie within the bounds of the shape.
bool coordsInBounds(ivec4 coord, ivec4 shape) {
return all(greaterThanEqual(coord, ivec4(0))) &&
all(lessThan(coord, shape));
}
bool coordsInBounds(ivec3 coord, ivec3 shape) {
return all(greaterThanEqual(coord, ivec3(0))) &&
all(lessThan(coord, shape));
}
bool coordsInBounds(ivec2 coord, ivec2 shape) {
return all(greaterThanEqual(coord, ivec2(0))) &&
all(lessThan(coord, shape));
}
`;
const SAMPLING_SNIPPETS = `
int getFlatIndex(int coord, int shape) {
return coord;
}
int getFlatIndex(ivec2 coords, ivec2 shape) {
return int(dot(coords, ivec2(shape.y, 1.)));
}
int getFlatIndex(ivec3 coords, ivec3 shape) {
return int(dot(coords, ivec3(shape.y * shape.z, shape.z, 1.)));
}
int getFlatIndex(ivec4 coords, ivec4 shape) {
return int(dot(coords, ivec4(
shape.y * shape.z * shape.w, shape.z * shape.w, shape.w, 1.)));
}
`;
function getSetOutputSnippet(
outRank: number, outBufferType: DataType,
needsShapesUniforms: boolean): string {
const glslType = mapToGlslTypes(outBufferType);
let snippet = `void setOutput(int flatIndex, float value) {
result[flatIndex] = ${
glslType === 'int' ? 'int(value)' :
(glslType === 'bool' ? 'bool(value)' : 'value')};
}
void setOutput(int flatIndex, int value) {
result[flatIndex] = ${
glslType === 'float' ? 'float(value)' :
(glslType === 'bool' ? 'bool(value)' : 'value')};
}`;
if (!needsShapesUniforms) {
return snippet;
}
if (outRank >= 2) {
const dims = ['d0', 'd1', 'd2', 'd3'].slice(0, outRank);
const type = getCoordsDataType(outRank);
snippet += `
void setOutput(${dims.map(d => `int ${d}`).join(', ')}, float value) {
int flatIndex = getFlatIndex(${type}(${dims.join(', ')}), outShape);
setOutput(flatIndex, value);
}
void setOutput(${dims.map(d => `int ${d}`).join(', ')}, int value) {
int flatIndex = getFlatIndex(${type}(${dims.join(', ')}), outShape);
setOutput(flatIndex, value);
}
`;
}
return snippet;
}
function getInputSamplingSnippet(
inInfo: InputInfo, outShape: number[]): string {
let res = getSamplerFromInInfo(inInfo);
const inShape = inInfo.shape;
if (inShape.length <= outShape.length) {
res += getSamplerAtOutputCoords(inInfo, outShape);
}
return res;
}
function getSamplerFromInInfo(inInfo: InputInfo): string {
const texName = inInfo.name;
const rank = inInfo.shape.length;
const type = getCoordsDataType(rank);
const funcName = 'get' + texName.charAt(0).toUpperCase() + texName.slice(1);
const dims = ['d0', 'd1', 'd2', 'd3'].slice(0, rank);
const inputs = dims.map(d => `int ${d}`).join(', ');
if (rank < 1) {
return `
float ${funcName}() {
return ${texName}[0];
}
`;
}
return `
float ${funcName}(${inputs}) {
return float(${texName}[getFlatIndex(${type}(${dims.join(',')}),
${texName.charAt(0).toLowerCase() + texName.slice(1)}Shape)]);
}
`;
}
function getSamplerAtOutputCoords(
inInfo: InputInfo, outShape: number[]): string {
const texName = inInfo.name;
const texFuncSnippet = texName.charAt(0).toUpperCase() + texName.slice(1);
const funcName = 'get' + texFuncSnippet + 'AtOutCoords';
const inRank = inInfo.shape.length;
const outRank = outShape.length;
const type = getCoordsDataType(outRank);
const broadcastDims = backend_util.getBroadcastDims(inInfo.shape, outShape);
const rankDiff = outRank - inRank;
let coordsSnippet = '';
if (inRank === 0) {
return `
float ${funcName}() {
return get${texFuncSnippet}();
}
float ${funcName}(${type} coords) {
return get${texFuncSnippet}();
}
`;
} else {
if (outRank < 2 && broadcastDims.length >= 1) {
coordsSnippet = 'coords = 0;';
} else {
coordsSnippet =
broadcastDims.map(d => `coords[${d + rankDiff}] = 0;`).join('\n');
}
}
let unpackedCoordsSnippet = '';
if (outRank < 2 && inRank > 0) {
unpackedCoordsSnippet = 'coords';
} else {
if (outRank > 1) {
const coordsType = getCoordsDataType(inRank);
const coordsValues =
inInfo.shape.map((s, i) => `coords[${i + rankDiff}]`).join(', ');
unpackedCoordsSnippet = `${coordsType}(${coordsValues})`;
} else {
unpackedCoordsSnippet = 'coords';
}
}
return `
float ${funcName}() {
${type} coords = getOutputCoords();
${coordsSnippet}
return float(${texName}[getFlatIndex(${unpackedCoordsSnippet}, ${
texName.charAt(0).toLowerCase() + texName.slice(1)}Shape)]);
}
float ${funcName}(${type} coords) {
${coordsSnippet}
return float(${texName}[getFlatIndex(${unpackedCoordsSnippet}, ${
texName.charAt(0).toLowerCase() + texName.slice(1)}Shape)]);
}
`;
}
/**
* Generates getOutputCoords() function that computes output coordinates from
* dispatch geometry to reduce arithmetic.
*/
function generateGetOutputCoords(
dispatchLayout: {x: number[], y?: number[], z?: number[]}):
[string, number] {
const {x, y = [], z = []} = dispatchLayout;
let gatherDimensionsStr = '';
const dims = [x, y, z];
let rank = 0;
for (let i = 0; i < dims.length; i++) {
const arr = dims[i];
if (arr.length === 0) {
continue;
}
rank += arr.length;
if (arr.length === 1) {
gatherDimensionsStr += `int d${arr[0]} =
int(gl_GlobalInvocationID[${i}]);`;
} else {
const strides = symbolicallyComputeStrides(arr, 'outShape');
gatherDimensionsStr += `int index${i} =
int(gl_GlobalInvocationID[${i}]);`;
for (let j = 0; j < strides.length; j++) {
gatherDimensionsStr += `int d${arr[j]} = index${i} / ${strides[j]};`;
if (j === strides.length - 1) {
gatherDimensionsStr += `int d${arr[j + 1]} = ` +
`index${i} - d${arr[j]} * ${strides[j]};`;
} else {
gatherDimensionsStr += `index${i} -= d${arr[j]} * ${strides[j]};`;
}
}
}
}
const dimensions = [];
for (let i = 0; i < rank; i++) {
dimensions.push(`d${i}`);
}
const dtype = getCoordsDataType(rank);
let snippet = `${dtype} getOutputCoords() {
${gatherDimensionsStr}
`;
if (dimensions.length === 0) {
snippet += `return ${dtype}(0);}`;
} else {
snippet += `return ${dtype}(${dimensions.join(',')});}`;
}
return [snippet, rank];
}
/**
* Derives logical coordinates from a flat index. Performs integer division with
* each stride and decrements the index until the index equals the final
* dimension coordinate.
*/
function generateGetCoordsFromFlatIndex(shape: number[]): string {
const rank = shape.length;
if (rank <= 1) {
return `int getCoordsFromFlatIndex(int index) {return index; }`;
}
const strides = util.computeStrides(shape);
const dtype = getCoordsDataType(rank);
const coords: string[] = [];
for (let i = 0; i < rank; i++) {
coords.push(`d${i}`);
}
const snippet =
strides
.map((stride, i) => {
const line1 = `int ${coords[i]} = index / ${stride}`;
const line2 = i === strides.length - 1 ?
`int ${coords[i + 1]} = index - ${coords[i]} * ${stride}` :
`index -= ${coords[i]} * ${stride}`;
return `${line1}; ${line2};`;
})
.join('');
return `
${dtype} getCoordsFromFlatIndex(int index) {
${snippet}
return ${dtype}(${coords.join(',')});
}
`;
}