Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[js/webgpu] Refactor createTensorShapeVariables #18883

Merged
merged 1 commit into from
Feb 2, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,7 @@ export const createConv2DMatMulProgramInfo =
{type: DataType.int32, data: attributes.strides}, {type: DataType.int32, data: attributes.dilations}
];
appendActivationUniformsData(attributes, programUniforms);
programUniforms.push(
...createTensorShapeVariables(inputs[0].dims), ...createTensorShapeVariables(inputs[1].dims));
programUniforms.push(...createTensorShapeVariables(inputs[0].dims, inputs[1].dims));
const inputDependencies: ProgramInputTensorInfoDependency[] = ['rank', 'rank'];
if (hasBias) {
programUniforms.push(...createTensorShapeVariables(inputs[2].dims));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,7 @@ export const createConv2DTransposeMatMulProgramInfo =
{type: DataType.int32, data: pads}
];
appendActivationUniformsData(attributes, programUniforms);
programUniforms.push(
...createTensorShapeVariables(inputs[0].dims), ...createTensorShapeVariables(inputs[1].dims));
programUniforms.push(...createTensorShapeVariables(inputs[0].dims, inputs[1].dims));

const inputDependencies: ProgramInputTensorInfoDependency[] = ['rank', 'rank'];
if (hasBias) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ export const createConvTranspose2DProgramInfo =
{type: DataType.uint32, data: filterDims}, {type: DataType.uint32, data: dilations},
{type: DataType.uint32, data: effectiveFilterDims}, {type: DataType.int32, data: pads},
{type: DataType.uint32, data: inputChannelsPerGroup}, {type: DataType.uint32, data: outputChannelsPerGroup},
...createTensorShapeVariables(inputs[0].dims), ...createTensorShapeVariables(inputs[1].dims)
...createTensorShapeVariables(inputs[0].dims, inputs[1].dims)
];
if (hasBias) {
programUniforms.push(...createTensorShapeVariables(inputs[2].dims));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,7 @@ export const createMatmulProgramInfo =
{type: DataType.int32, data: dimInner}
];
appendActivationUniformsData(activationAttributes, programUniforms);
programUniforms.push(
...createTensorShapeVariables(outerDims), ...createTensorShapeVariables(aShapeTemp),
...createTensorShapeVariables(bShapeTemp));
programUniforms.push(...createTensorShapeVariables(outerDims, aShapeTemp, bShapeTemp));
const inputDependencies: ProgramInputTensorInfoDependency[] = ['rank', 'rank'];

const hasBias = inputs.length > 2;
Expand Down
4 changes: 1 addition & 3 deletions js/web/lib/wasm/jsep/webgpu/ops/binary-op.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,7 @@ const createBinaryOpProgramInfo =
dispatchGroup: {x: Math.ceil(outputSize / 64 /* workgroup size */ / 4 /* component size */)},
programUniforms: [
{type: DataType.uint32, data: Math.ceil(ShapeUtil.size(outputShape) / 4)},
...createTensorShapeVariables(a.dims),
...createTensorShapeVariables(b.dims),
...createTensorShapeVariables(outputShape),
...createTensorShapeVariables(a.dims, b.dims, outputShape)
],
}),
};
Expand Down
13 changes: 10 additions & 3 deletions js/web/lib/wasm/jsep/webgpu/ops/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,16 @@ export const tensorTypeToWsglValueType = (type: DataType, components: 1|2|3|4 =
return typeof mappedType === 'string' ? mappedType : mappedType[1];
};

export const createTensorShapeVariables = (dims: readonly number[]): ProgramUniform[] => dims.length === 0 ?
[] :
[{type: DataType.uint32, data: dims}, {type: DataType.uint32, data: ShapeUtil.computeStrides(dims)}];
export const createTensorShapeVariables = (...dims: ReadonlyArray<readonly number[]>): ProgramUniform[] => {
const programUniforms: ProgramUniform[] = [];
dims.forEach(dim => {
if (dim.length !== 0) {
programUniforms.push(
{type: DataType.uint32, data: dim}, {type: DataType.uint32, data: ShapeUtil.computeStrides(dim)});
}
});
return programUniforms;
};

/**
* A helper function to get maximum vector size for specified data length
Expand Down
8 changes: 2 additions & 6 deletions js/web/lib/wasm/jsep/webgpu/ops/conv-grouped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ export const createGroupedConvProgramInfo =
{type: DataType.uint32, data: outputChannelsPerGroup}
];
appendActivationUniformsData(attributes, programUniforms);
programUniforms.push(
...createTensorShapeVariables(xShape), ...createTensorShapeVariables(wShape),
...createTensorShapeVariables(outputShape));
programUniforms.push(...createTensorShapeVariables(xShape, wShape, outputShape));
const inputDependencies: ProgramInputTensorInfoDependency[] = ['rank', 'rank'];
if (hasBias) {
programUniforms.push(...createTensorShapeVariables(inputs[2].dims));
Expand Down Expand Up @@ -134,9 +132,7 @@ export const createGroupedConvVectorizeProgramInfo =
{type: DataType.int32, data: [attributes.pads[0], attributes.pads[1]]}
];
appendActivationUniformsData(attributes, programUniforms);
programUniforms.push(
...createTensorShapeVariables(xShape), ...createTensorShapeVariables(wShape),
...createTensorShapeVariables(outputShapeInShader));
programUniforms.push(...createTensorShapeVariables(xShape, wShape, outputShapeInShader));
const xNumber = (outputNumber - 1) * attributes.strides[1] + wShape[1];
const getShaderSource = (shaderHelper: ShaderHelper) => {
const output = outputVariable('output', inputs[0].dataType, outputShapeInShader.length, components);
Expand Down
2 changes: 1 addition & 1 deletion js/web/lib/wasm/jsep/webgpu/ops/cumsum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const createCumsumProgramInfo =
dispatchGroup: {x: Math.ceil(outputSize / 64 /* workgroup size */)},
programUniforms: [
{type: DataType.uint32, data: outputSize}, {type: DataType.int32, data: axis},
...createTensorShapeVariables(inputShape), ...createTensorShapeVariables(inputShape)
...createTensorShapeVariables(inputShape, inputShape)
]

}),
Expand Down
6 changes: 2 additions & 4 deletions js/web/lib/wasm/jsep/webgpu/ops/expand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,8 @@ const createExpandProgramInfo = (inputs: readonly TensorView[]): ProgramInfo =>
${assignment}`;
};

const programUniforms: ProgramUniform[] = [
{type: DataType.uint32, data: outputSize}, ...createTensorShapeVariables(inputShape),
...createTensorShapeVariables(outputShape)
];
const programUniforms: ProgramUniform[] =
[{type: DataType.uint32, data: outputSize}, ...createTensorShapeVariables(inputShape, outputShape)];
return {
name: 'Expand',
shaderCache: {hint: `${outputShape.length}`, inputDependencies: ['rank']},
Expand Down
4 changes: 1 addition & 3 deletions js/web/lib/wasm/jsep/webgpu/ops/gather-elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ const createGatherElementsProgramInfo =
{type: DataType.uint32, data: outputSize}, {type: DataType.int32, data: axisDimLimit},
{type: DataType.uint32, data: axis}
];
programUniforms.push(...createTensorShapeVariables(inputShape));
programUniforms.push(...createTensorShapeVariables(indicesShape));
programUniforms.push(...createTensorShapeVariables(outputShape));
programUniforms.push(...createTensorShapeVariables(inputShape, indicesShape, outputShape));
const inputDependencies: ProgramInputTensorInfoDependency[] = ['rank', 'rank'];

// int64 indices would be treated as little endian i32 with assumption they fall in i32 limits
Expand Down
3 changes: 1 addition & 2 deletions js/web/lib/wasm/jsep/webgpu/ops/gather.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ const createGatherProgramInfo = (inputs: readonly TensorView[], attributes: Gath

const programUniforms: ProgramUniform[] = [
{type: DataType.uint32, data: outputSize}, {type: DataType.int32, data: axisDimLimit},
{type: DataType.uint32, data: axis}, ...createTensorShapeVariables(inputs[0].dims),
...createTensorShapeVariables(inputs[1].dims), ...createTensorShapeVariables(outputShape)
{type: DataType.uint32, data: axis}, ...createTensorShapeVariables(inputs[0].dims, inputs[1].dims, outputShape)
];

const getShaderSource = (shaderHelper: ShaderHelper) => {
Expand Down
2 changes: 1 addition & 1 deletion js/web/lib/wasm/jsep/webgpu/ops/instance-norm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const createInstanceNormProgramInfo =
const inputDependencies: ProgramInputTensorInfoDependency[] = ['rank', 'type', 'type'];
const programUniforms: ProgramUniform[] =
[{type: DataType.uint32, data: normSize}, {type: DataType.uint32, data: normPackedSize}];
programUniforms.push(...createTensorShapeVariables(inputShape), ...createTensorShapeVariables(inputShape));
programUniforms.push(...createTensorShapeVariables(inputShape, inputShape));

const getShaderSource = (shaderHelper: ShaderHelper) => {
const x = inputVariable('x', inputs[0].dataType, inputShape.length, components);
Expand Down
4 changes: 1 addition & 3 deletions js/web/lib/wasm/jsep/webgpu/ops/matmul.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ export const createNaiveMatmulProgramInfo =
{type: DataType.uint32, data: K}
];
appendActivationUniformsData(activationAttributes, programUniforms);
programUniforms.push(
...createTensorShapeVariables(outerDims), ...createTensorShapeVariables(aShape),
...createTensorShapeVariables(bShape));
programUniforms.push(...createTensorShapeVariables(outerDims, aShape, bShape));
if (hasBias) {
programUniforms.push(...createTensorShapeVariables(inputs[2].dims));
}
Expand Down
2 changes: 1 addition & 1 deletion js/web/lib/wasm/jsep/webgpu/ops/pad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const createPadProgramInfo = (inputs: readonly TensorView[], attributes: PadAttr
programUniforms.push({type: inputs[0].dataType, data: attributes.value});
}

programUniforms.push(...createTensorShapeVariables(inputs[0].dims), ...createTensorShapeVariables(outputShape));
programUniforms.push(...createTensorShapeVariables(inputs[0].dims, outputShape));
const inputDependencies: ProgramInputTensorInfoDependency[] = ['rank'];

const getShaderSource = (shaderHelper: ShaderHelper) => {
Expand Down
4 changes: 2 additions & 2 deletions js/web/lib/wasm/jsep/webgpu/ops/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ const createAveragePoolProgramInfo =
}
const [programUniforms, uniforms, hasPads, pwStartEndNotZero, phStartEndNotZero] =
getUniformAndPadInfo(outputShape, adjustedAttributes);
programUniforms.push(...createTensorShapeVariables(input.dims), ...createTensorShapeVariables(outputShape));
programUniforms.push(...createTensorShapeVariables(input.dims, outputShape));
const inputDependencies: ProgramInputTensorInfoDependency[] = ['rank'];
return {
name,
Expand Down Expand Up @@ -370,7 +370,7 @@ const createMaxPoolProgramInfo =
const inputDependencies: ProgramInputTensorInfoDependency[] = ['rank'];
const [programUniforms, uniforms, hasPads, pwStartEndNotZero, phStartEndNotZero] =
getUniformAndPadInfo(outputShape, adjustedAttributes);
programUniforms.push(...createTensorShapeVariables(input.dims), ...createTensorShapeVariables(outputShape));
programUniforms.push(...createTensorShapeVariables(input.dims, outputShape));
return {
name,
shaderCache:
Expand Down
6 changes: 2 additions & 4 deletions js/web/lib/wasm/jsep/webgpu/ops/reduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,8 @@ export const createReduceProgramInfo =
getRunData: () => ({
outputs: [{dims: outputShape, dataType: outputDataType}],
dispatchGroup: {x: Math.ceil(outputSize / 64 /* workgroup size */)},
programUniforms: [
{type: DataType.uint32, data: outputSize}, ...createTensorShapeVariables(inputShape),
...createTensorShapeVariables(outputShape)
]
programUniforms:
[{type: DataType.uint32, data: outputSize}, ...createTensorShapeVariables(inputShape, outputShape)]
}),
};
};
Expand Down
7 changes: 2 additions & 5 deletions js/web/lib/wasm/jsep/webgpu/ops/resize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -642,11 +642,8 @@ const createResizeProgramInfo =
outputs: [{dims: outputShape, dataType: inputTensor.dataType}],
dispatchGroup: {x: Math.ceil(outputSize / 64 /* workgroup size */)},
programUniforms: [
{type: DataType.uint32, data: outputSize},
{type: DataType.float, data: scales},
{type: DataType.float, data: roi},
...createTensorShapeVariables(inputShape),
...createTensorShapeVariables(outputShape),
{type: DataType.uint32, data: outputSize}, {type: DataType.float, data: scales},
{type: DataType.float, data: roi}, ...createTensorShapeVariables(inputShape, outputShape)
]
})
};
Expand Down
2 changes: 1 addition & 1 deletion js/web/lib/wasm/jsep/webgpu/ops/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ const createSliceProgramInfo = (inputs: readonly TensorView[], attributes: Slice
const programUniforms: ProgramUniform[] = [
{type: DataType.uint32, data: outputSize}, {type: DataType.uint32, data: starts},
{type: DataType.int32, data: signs}, {type: DataType.uint32, data: steps},
...createTensorShapeVariables(inputs[0].dims), ...createTensorShapeVariables(outputShape)
...createTensorShapeVariables(inputs[0].dims, outputShape)
];

const getShaderSource = (shaderHelper: ShaderHelper) => `
Expand Down
5 changes: 2 additions & 3 deletions js/web/lib/wasm/jsep/webgpu/ops/split.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ const createSplitProgramInfo = (inputs: readonly TensorView[], attributes: Split
outputs[i] = outputVariable(`output${i}`, dataType, outputShape);
outputsTensorInfo.push({dims: outputShapes[i], dataType: inputs[0].dataType});
}
programUniforms.push({type: DataType.uint32, data: sizeInSplitAxis});
programUniforms.push(...createTensorShapeVariables(inputShape));
outputShapes.forEach((outputShape) => programUniforms.push(...createTensorShapeVariables(outputShape)));
programUniforms.push(
{type: DataType.uint32, data: sizeInSplitAxis}, ...createTensorShapeVariables(inputShape, ...outputShapes));
const getShaderSource = (shaderHelper: ShaderHelper) => `
${
shaderHelper.registerUniform('input_size', 'u32')
Expand Down
6 changes: 2 additions & 4 deletions js/web/lib/wasm/jsep/webgpu/ops/tile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,8 @@ export const createTileProgramInfo = (inputs: readonly TensorView[]): ProgramInf
getRunData: () => ({
outputs: [{dims: outputShape, dataType: inputs[0].dataType}],
dispatchGroup: {x: Math.ceil(outputSize / 64 /* workgroup size */)},
programUniforms: [
{type: DataType.uint32, data: outputSize}, ...createTensorShapeVariables(inputs[0].dims),
...createTensorShapeVariables(outputShape)
],
programUniforms:
[{type: DataType.uint32, data: outputSize}, ...createTensorShapeVariables(inputs[0].dims, outputShape)],
}),
getShaderSource,
};
Expand Down
7 changes: 2 additions & 5 deletions js/web/lib/wasm/jsep/webgpu/ops/transpose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,8 @@ export const createTransposeProgramInfo = (inputTensor: TensorView, permAttr: nu
return {
outputs: [{dims: outputShape, dataType: inputs[0].dataType}],
dispatchGroup: {x: Math.ceil(outputSize / 64 /* workgroup size */)},
programUniforms: [
{type: DataType.uint32, data: outputSize},
...createTensorShapeVariables(inputs[0].dims),
...createTensorShapeVariables(outputShape),
],
programUniforms:
[{type: DataType.uint32, data: outputSize}, ...createTensorShapeVariables(inputs[0].dims, outputShape)],
};
},
getShaderSource,
Expand Down
7 changes: 2 additions & 5 deletions js/web/lib/wasm/jsep/webgpu/ops/where.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,8 @@ const createWhereOpProgramInfo = (inputs: readonly TensorView[]): ProgramInfo =>
getRunData: () => ({
outputs: [{dims: outputShape, dataType: outputDataType}],
dispatchGroup: {x: Math.ceil(outputSize / 64 /* workgroup size */ / 4 /* vec size */)},
programUniforms: [
{type: DataType.uint32, data: vecSize}, ...createTensorShapeVariables(dimsC),
...createTensorShapeVariables(dimsA), ...createTensorShapeVariables(dimsB),
...createTensorShapeVariables(outputShape)
],
programUniforms:
[{type: DataType.uint32, data: vecSize}, ...createTensorShapeVariables(dimsC, dimsA, dimsB, outputShape)],
}),
};
};
Expand Down
Loading