Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions tfjs-backend-webgpu/src/backend_webgpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,6 @@ export class WebGPUBackend extends KernelBackend {
const uniformData = new Int32Array(dimUniforms);
const uniforms = this.makeUniforms(uniformData);

const key =
webgpu_program.makeShaderKey(program, bufferShapes.map(d => d.length));
const inputsData = inputs.map((input: Tensor, i: number) => {
this.uploadToGPU(input.dataId);

Expand All @@ -471,6 +469,9 @@ export class WebGPUBackend extends KernelBackend {
};
});
this.uploadToGPU(output.dataId);
const bufferTypes = inputsData.map(d => d.dtype).concat(output.dtype);
const key =
webgpu_program.makeShaderKey(program, bufferShapes, bufferTypes);
const {bindGroupLayout, pipeline} = this.getAndSavePipeline(key, () => {
return webgpu_program.compileProgram(
this.glslang, this.device, program, inputsData, output, uniforms);
Expand Down
9 changes: 4 additions & 5 deletions tfjs-backend-webgpu/src/kernels/webgpu_program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* =============================================================================
*/

import {DataType, Tensor} from '@tensorflow/tfjs-core';
import {DataType, Rank, ShapeMap, Tensor} from '@tensorflow/tfjs-core';
import {Glslang} from '@webgpu/glslang/dist/web-devel/glslang.onefile';

import * as shader_preprocessor from '../shader_preprocessor';
Expand Down Expand Up @@ -121,11 +121,10 @@ export const compileProgram =
return {bindGroupLayout, pipeline};
};

// TODO: Consider uploading shape info as vec4s regardless of rank to reduce
// recompilation.
export function makeShaderKey(program: WebGPUProgram, ranks: number[]): string {
export function makeShaderKey<R extends Rank>(program: WebGPUProgram,
shapes: Array<ShapeMap[R]>, types: string[]): string {
const key = (program.workGroupSize ? program.workGroupSize.join(',') : '') +
ranks.join(',') +
shapes.join(',') + types.join(',') + program.variableNames.join(',') +
(program.shaderKey ? program.shaderKey : program.userCode);
return key;
}