2023-08-18 17:07:21 +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 { ComputeContext, ProgramInfo } from '../types';
|
2023-08-18 17:07:21 +00:00
|
|
|
|
2024-08-14 23:51:22 +00:00
|
|
|
import { createTensorShapeVariables, inputVariable, outputVariable, ShaderHelper } from './common';
|
2023-08-18 17:07:21 +00:00
|
|
|
|
|
|
|
|
const getRepeats = (repeatsTensorView: TensorView): readonly number[] =>
|
2024-08-14 23:51:22 +00:00
|
|
|
Array.from(repeatsTensorView.getBigInt64Array(), Number);
|
2023-08-18 17:07:21 +00:00
|
|
|
|
|
|
|
|
const validateInputs = (inputs: readonly TensorView[]): void => {
|
|
|
|
|
if (!inputs || inputs.length !== 2) {
|
|
|
|
|
throw new Error('Tile requires 2 inputs.');
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-14 23:51:22 +00:00
|
|
|
if (
|
|
|
|
|
inputs[0].dataType !== DataType.float &&
|
|
|
|
|
inputs[0].dataType !== DataType.float16 &&
|
|
|
|
|
inputs[0].dataType !== DataType.int32 &&
|
|
|
|
|
inputs[0].dataType !== DataType.uint32
|
|
|
|
|
) {
|
2024-05-24 16:59:13 +00:00
|
|
|
throw new Error('Tile only support float, float16, int32, and uint32 data types');
|
2023-08-18 17:07:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (inputs[1].dataType !== DataType.int64) {
|
|
|
|
|
throw new Error('Tile `repeats` input should be of int64 data type');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (inputs[1].dims.length !== 1) {
|
|
|
|
|
throw new Error('Tile `repeats` input should be 1-D');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const repeats: readonly number[] = getRepeats(inputs[1]);
|
|
|
|
|
|
|
|
|
|
if (repeats.length !== inputs[0].dims.length) {
|
|
|
|
|
throw new Error('Tile `repeats` input should have same number of elements as rank of input data tensor');
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getOutputShape = (inputShape: readonly number[], repeats: readonly number[]): readonly number[] => {
|
|
|
|
|
const outputShape: number[] = [];
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < inputShape.length; ++i) {
|
|
|
|
|
outputShape.push(inputShape[i] * repeats[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return outputShape;
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-13 16:43:37 +00:00
|
|
|
export const createTileProgramInfo = (inputs: readonly TensorView[], shape?: number[]): ProgramInfo => {
|
2023-10-10 07:31:12 +00:00
|
|
|
const inputShape = inputs[0].dims;
|
2024-05-13 16:43:37 +00:00
|
|
|
const repeats: readonly number[] = shape == null ? getRepeats(inputs[1]) : shape;
|
2023-10-10 07:31:12 +00:00
|
|
|
const outputShape = getOutputShape(inputShape, repeats);
|
|
|
|
|
const outputSize = ShapeUtil.size(outputShape);
|
2023-08-18 17:07:21 +00:00
|
|
|
|
2023-10-10 07:31:12 +00:00
|
|
|
const dataType = inputs[0].dataType;
|
2023-12-12 04:58:52 +00:00
|
|
|
const input = inputVariable('input', dataType, inputShape.length);
|
|
|
|
|
const output = outputVariable('output', dataType, outputShape.length);
|
2023-08-18 17:07:21 +00:00
|
|
|
|
2023-10-10 07:31:12 +00:00
|
|
|
const getShaderSource = (shaderHelper: ShaderHelper) => `
|
2023-08-18 17:07:21 +00:00
|
|
|
const inputShape = ${input.indices(...inputShape)};
|
2023-12-12 04:58:52 +00:00
|
|
|
${shaderHelper.registerUniform('output_size', 'u32').declareVariables(input, output)}
|
2023-08-18 17:07:21 +00:00
|
|
|
${shaderHelper.mainStart()}
|
2023-12-12 04:58:52 +00:00
|
|
|
${shaderHelper.guardAgainstOutOfBoundsWorkgroupSizes('uniforms.output_size')}
|
|
|
|
|
let output_indices = ${output.offsetToIndices('global_idx')};
|
|
|
|
|
var input_indices: ${input.type.indices};
|
2023-08-18 17:07:21 +00:00
|
|
|
for (var i = 0; i < ${inputShape.length}; i++) {
|
2023-12-12 04:58:52 +00:00
|
|
|
let input_dim_i = ${input.indicesGet('uniforms.input_shape', 'i')};
|
|
|
|
|
let input_dim_value = ${output.indicesGet('output_indices', 'i')} % input_dim_i;
|
2023-08-18 17:07:21 +00:00
|
|
|
|
2023-12-12 04:58:52 +00:00
|
|
|
${input.indicesSet('input_indices', 'i', 'input_dim_value')}
|
2023-08-18 17:07:21 +00:00
|
|
|
}
|
2023-12-12 04:58:52 +00:00
|
|
|
${output.setByOffset('global_idx', input.getByIndices('input_indices'))}
|
2023-08-18 17:07:21 +00:00
|
|
|
}`;
|
|
|
|
|
|
2023-10-10 07:31:12 +00:00
|
|
|
return {
|
|
|
|
|
name: 'Tile',
|
2024-08-14 23:51:22 +00:00
|
|
|
shaderCache: { hint: `${repeats}`, inputDependencies: ['rank'] },
|
2023-10-10 07:31:12 +00:00
|
|
|
getRunData: () => ({
|
2024-08-14 23:51:22 +00:00
|
|
|
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, outputShape),
|
|
|
|
|
],
|
2023-10-10 07:31:12 +00:00
|
|
|
}),
|
|
|
|
|
getShaderSource,
|
|
|
|
|
};
|
|
|
|
|
};
|
2023-08-18 17:07:21 +00:00
|
|
|
|
|
|
|
|
export const tile = (context: ComputeContext): void => {
|
|
|
|
|
validateInputs(context.inputs);
|
2024-08-14 23:51:22 +00:00
|
|
|
context.compute(createTileProgramInfo(context.inputs), { inputs: [0] });
|
2023-08-18 17:07:21 +00:00
|
|
|
};
|