2023-09-30 09:05:32 +00:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
|
// Licensed under the MIT License.
|
|
|
|
|
|
2024-08-14 23:51:22 +00:00
|
|
|
import { env } from 'onnxruntime-common';
|
2023-09-30 09:05:32 +00:00
|
|
|
|
2024-08-14 23:51:22 +00:00
|
|
|
import { DataType } from '../../../wasm-common';
|
|
|
|
|
import { ComputeContext, ProgramInfo, ProgramUniform } from '../types';
|
2023-09-30 09:05:32 +00:00
|
|
|
|
2024-08-14 23:51:22 +00:00
|
|
|
import {
|
|
|
|
|
createTensorShapeVariables,
|
|
|
|
|
outputVariable,
|
|
|
|
|
ShaderHelper,
|
|
|
|
|
UniformDataElementType,
|
|
|
|
|
UniformsArrayType,
|
|
|
|
|
} from './common';
|
2023-09-30 09:05:32 +00:00
|
|
|
|
|
|
|
|
const validateInputsContent = (start: number, limit: number, delta: number): void => {
|
|
|
|
|
const sameStartLimit = start === limit;
|
|
|
|
|
const increasingRangeNegativeStep = start < limit && delta < 0;
|
|
|
|
|
const decreasingRangePositiveStep = start > limit && delta > 0;
|
|
|
|
|
|
|
|
|
|
if (sameStartLimit || increasingRangeNegativeStep || decreasingRangePositiveStep) {
|
2024-08-14 23:51:22 +00:00
|
|
|
throw new Error("Range these inputs' contents are invalid.");
|
2023-09-30 09:05:32 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-10-10 07:31:12 +00:00
|
|
|
const createRangeProgramInfo = (start: number, limit: number, delta: number, dataType: DataType): ProgramInfo => {
|
|
|
|
|
const numElements = Math.abs(Math.ceil((limit - start) / delta));
|
|
|
|
|
const outputShape: number[] = [numElements];
|
|
|
|
|
const outputSize = numElements;
|
2024-01-09 17:33:57 +00:00
|
|
|
const programUniforms: ProgramUniform[] = [
|
2024-08-14 23:51:22 +00:00
|
|
|
{ type: DataType.uint32, data: outputSize },
|
|
|
|
|
{ type: dataType, data: start },
|
|
|
|
|
{ type: dataType, data: delta },
|
|
|
|
|
...createTensorShapeVariables(outputShape),
|
2024-01-09 17:33:57 +00:00
|
|
|
];
|
2023-09-30 09:05:32 +00:00
|
|
|
|
2024-01-09 17:33:57 +00:00
|
|
|
const getShaderSource = (shaderHelper: ShaderHelper) => {
|
|
|
|
|
const output = outputVariable('output', dataType, outputShape.length);
|
|
|
|
|
const wgslType = output.type.value;
|
|
|
|
|
const uniforms: UniformsArrayType = [
|
2024-08-14 23:51:22 +00:00
|
|
|
{ name: 'outputSize', type: 'u32' },
|
|
|
|
|
{ name: 'start', type: wgslType as UniformDataElementType },
|
|
|
|
|
{ name: 'delta', type: wgslType as UniformDataElementType },
|
2024-01-09 17:33:57 +00:00
|
|
|
];
|
|
|
|
|
return `
|
|
|
|
|
${shaderHelper.registerUniforms(uniforms).declareVariables(output)}
|
2023-09-30 09:05:32 +00:00
|
|
|
${shaderHelper.mainStart()}
|
2024-01-09 17:33:57 +00:00
|
|
|
${shaderHelper.guardAgainstOutOfBoundsWorkgroupSizes('uniforms.outputSize')}
|
|
|
|
|
output[global_idx] = uniforms.start + ${wgslType}(global_idx) * uniforms.delta;
|
2023-09-30 09:05:32 +00:00
|
|
|
}`;
|
2024-01-09 17:33:57 +00:00
|
|
|
};
|
|
|
|
|
|
2023-10-10 07:31:12 +00:00
|
|
|
return {
|
|
|
|
|
name: 'Range',
|
2024-08-14 23:51:22 +00:00
|
|
|
shaderCache: { hint: `${dataType}` },
|
2023-10-10 07:31:12 +00:00
|
|
|
getShaderSource,
|
2024-01-09 17:33:57 +00:00
|
|
|
getRunData: () => ({
|
2024-08-14 23:51:22 +00:00
|
|
|
outputs: [{ dims: outputShape, dataType }],
|
|
|
|
|
dispatchGroup: { x: Math.ceil(outputSize / 64 /* workgroup size */) },
|
|
|
|
|
programUniforms,
|
|
|
|
|
}),
|
2023-10-10 07:31:12 +00:00
|
|
|
};
|
|
|
|
|
};
|
2023-09-30 09:05:32 +00:00
|
|
|
|
|
|
|
|
export const range = (context: ComputeContext): void => {
|
|
|
|
|
let start = 0;
|
|
|
|
|
let limit = 0;
|
|
|
|
|
let delta = 0;
|
|
|
|
|
if (context.inputs[0].dataType === DataType.int32) {
|
|
|
|
|
start = context.inputs[0].getInt32Array()[0];
|
|
|
|
|
limit = context.inputs[1].getInt32Array()[0];
|
|
|
|
|
delta = context.inputs[2].getInt32Array()[0];
|
|
|
|
|
} else if (context.inputs[0].dataType === DataType.float) {
|
|
|
|
|
start = context.inputs[0].getFloat32Array()[0];
|
|
|
|
|
limit = context.inputs[1].getFloat32Array()[0];
|
|
|
|
|
delta = context.inputs[2].getFloat32Array()[0];
|
|
|
|
|
}
|
|
|
|
|
if (env.webgpu.validateInputContent) {
|
|
|
|
|
validateInputsContent(start, limit, delta);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-14 23:51:22 +00:00
|
|
|
context.compute(createRangeProgramInfo(start, limit, delta, context.inputs[0].dataType), { inputs: [] });
|
2023-09-30 09:05:32 +00:00
|
|
|
};
|