2023-08-28 16:55:25 +00:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
|
// Licensed under the MIT License.
|
|
|
|
|
|
2024-08-14 23:51:22 +00:00
|
|
|
import { DataType } from '../../../wasm-common';
|
|
|
|
|
import { TensorView } from '../../tensor-view';
|
|
|
|
|
import { ShapeUtil } from '../../util';
|
|
|
|
|
import { AttributeWithCacheKey, createAttributeWithCacheKey } from '../attribute-with-cache-key';
|
|
|
|
|
import { ComputeContext, ProgramInfo, ProgramInputTensorInfoDependency, ProgramUniform } from '../types';
|
2023-08-28 16:55:25 +00:00
|
|
|
|
2024-08-14 23:51:22 +00:00
|
|
|
import { createTensorShapeVariables, inputVariable, outputVariable, ShaderHelper } from './common';
|
2023-08-28 16:55:25 +00:00
|
|
|
|
|
|
|
|
export interface GatherElementsAttributes extends AttributeWithCacheKey {
|
|
|
|
|
axis: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const validateInputs = (inputs: readonly TensorView[]): void => {
|
|
|
|
|
if (!inputs || inputs.length !== 2) {
|
|
|
|
|
throw new Error('GatherElements requires 2 inputs.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (inputs[0].dims.length < 1) {
|
|
|
|
|
throw new Error('GatherElements requires that the data input be rank >= 1.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (inputs[0].dims.length !== inputs[1].dims.length) {
|
|
|
|
|
throw new Error(`GatherElements requires that the data input and
|
|
|
|
|
indices input tensors be of same rank.`);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-14 23:51:22 +00:00
|
|
|
const createGatherElementsProgramInfo = (
|
|
|
|
|
inputs: readonly TensorView[],
|
|
|
|
|
attributes: GatherElementsAttributes,
|
|
|
|
|
): ProgramInfo => {
|
|
|
|
|
const inputShape = inputs[0].dims;
|
|
|
|
|
const inputOutputDataType = inputs[0].dataType;
|
|
|
|
|
const inputRank = inputShape.length;
|
|
|
|
|
|
|
|
|
|
const indicesShape = inputs[1].dims;
|
|
|
|
|
const indicesDataType = inputs[1].dataType;
|
|
|
|
|
const axis = ShapeUtil.normalizeAxis(attributes.axis, inputRank);
|
|
|
|
|
const axisDimLimit = inputShape[axis];
|
|
|
|
|
|
|
|
|
|
const outputShape = indicesShape.slice(0);
|
|
|
|
|
const outputSize = ShapeUtil.size(outputShape);
|
|
|
|
|
|
|
|
|
|
const input = inputVariable('input', inputOutputDataType, inputRank);
|
|
|
|
|
const indices = inputVariable('indicesInput', indicesDataType, indicesShape.length);
|
|
|
|
|
const output = outputVariable('output', inputOutputDataType, outputShape.length);
|
|
|
|
|
|
|
|
|
|
const programUniforms: ProgramUniform[] = [
|
|
|
|
|
{ type: DataType.uint32, data: outputSize },
|
|
|
|
|
{ type: DataType.int32, data: axisDimLimit },
|
|
|
|
|
{ type: DataType.uint32, data: axis },
|
|
|
|
|
];
|
|
|
|
|
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
|
|
|
|
|
// That assumption is safe as it's not possible to allocate >2gb buffer for input tensor
|
|
|
|
|
// Input data will be treated as u32 or two u32 for 8-byte tensors
|
|
|
|
|
const getShaderSource = (shaderHelper: ShaderHelper) => `
|
|
|
|
|
${shaderHelper
|
|
|
|
|
.registerUniform('outputSize', 'u32')
|
|
|
|
|
.registerUniform('axisDimLimit', 'i32')
|
|
|
|
|
.registerUniform('axis', 'u32')
|
|
|
|
|
.declareVariables(input, indices, output)}
|
2023-08-28 16:55:25 +00:00
|
|
|
${shaderHelper.mainStart()}
|
2023-12-05 17:19:53 +00:00
|
|
|
${shaderHelper.guardAgainstOutOfBoundsWorkgroupSizes('uniforms.outputSize')}
|
2023-08-28 16:55:25 +00:00
|
|
|
|
|
|
|
|
let outputIndices = ${output.offsetToIndices('global_idx')};
|
|
|
|
|
|
|
|
|
|
var idx = ${indices.getByOffset('global_idx')};
|
|
|
|
|
if (idx < 0) {
|
2023-12-05 17:19:53 +00:00
|
|
|
idx = idx + uniforms.axisDimLimit;
|
2023-08-28 16:55:25 +00:00
|
|
|
}
|
2023-12-05 17:19:53 +00:00
|
|
|
var inputIndices = ${input.type.indices}(outputIndices);
|
|
|
|
|
${input.indicesSet('inputIndices', 'uniforms.axis', 'u32(idx)')};
|
|
|
|
|
let value = ${input.getByIndices('inputIndices')};
|
2023-08-28 16:55:25 +00:00
|
|
|
|
2023-12-05 17:19:53 +00:00
|
|
|
${output.setByOffset('global_idx', 'value')};
|
2023-08-28 16:55:25 +00:00
|
|
|
}`;
|
|
|
|
|
|
2024-08-14 23:51:22 +00:00
|
|
|
return {
|
|
|
|
|
name: 'GatherElements',
|
|
|
|
|
shaderCache: { inputDependencies },
|
|
|
|
|
getRunData: () => ({
|
|
|
|
|
outputs: [{ dims: outputShape, dataType: inputs[0].dataType }],
|
|
|
|
|
dispatchGroup: { x: Math.ceil(outputSize / 64 /* workgroup size */) },
|
|
|
|
|
programUniforms,
|
|
|
|
|
}),
|
|
|
|
|
getShaderSource,
|
|
|
|
|
};
|
|
|
|
|
};
|
2023-08-28 16:55:25 +00:00
|
|
|
|
|
|
|
|
export const parseGatherElementsAttributes = (attributes: Record<string, unknown>): GatherElementsAttributes =>
|
2024-08-14 23:51:22 +00:00
|
|
|
createAttributeWithCacheKey({ axis: attributes.axis as number });
|
2023-08-28 16:55:25 +00:00
|
|
|
|
|
|
|
|
export const gatherElements = (context: ComputeContext, attributes: GatherElementsAttributes): void => {
|
|
|
|
|
const inputs = context.inputs;
|
|
|
|
|
validateInputs(inputs);
|
2023-10-10 07:31:12 +00:00
|
|
|
context.compute(createGatherElementsProgramInfo(context.inputs, attributes));
|
2023-08-28 16:55:25 +00:00
|
|
|
};
|