mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-05-18 21:21:17 +00:00
### Description
See
454996d496
for manual changes (excluded auto-generated formatting changes)
### Why
Because the toolsets for old clang-format is out-of-date. This reduces
the development efficiency.
- The NPM package `clang-format` is already in maintenance mode. not
updated since 2 years ago.
- The VSCode extension for clang-format is not maintained for a while,
and a recent Node.js security update made it not working at all in
Windows.
No one in community seems interested in fixing those.
Choose Prettier as it is the most popular TS/JS formatter.
### How to merge
It's easy to break the build:
- Be careful of any new commits on main not included in this PR.
- Be careful that after this PR is merged, other PRs that already passed
CI can merge.
So, make sure there is no new commits before merging this one, and
invalidate js PRs that already passed CI, force them to merge to latest.
84 lines
3 KiB
TypeScript
84 lines
3 KiB
TypeScript
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
// Licensed under the MIT License.
|
|
|
|
import { env } from 'onnxruntime-common';
|
|
|
|
import { DataType } from '../../../wasm-common';
|
|
import { ComputeContext, ProgramInfo, ProgramUniform } from '../types';
|
|
|
|
import {
|
|
createTensorShapeVariables,
|
|
outputVariable,
|
|
ShaderHelper,
|
|
UniformDataElementType,
|
|
UniformsArrayType,
|
|
} from './common';
|
|
|
|
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) {
|
|
throw new Error("Range these inputs' contents are invalid.");
|
|
}
|
|
};
|
|
|
|
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;
|
|
const programUniforms: ProgramUniform[] = [
|
|
{ type: DataType.uint32, data: outputSize },
|
|
{ type: dataType, data: start },
|
|
{ type: dataType, data: delta },
|
|
...createTensorShapeVariables(outputShape),
|
|
];
|
|
|
|
const getShaderSource = (shaderHelper: ShaderHelper) => {
|
|
const output = outputVariable('output', dataType, outputShape.length);
|
|
const wgslType = output.type.value;
|
|
const uniforms: UniformsArrayType = [
|
|
{ name: 'outputSize', type: 'u32' },
|
|
{ name: 'start', type: wgslType as UniformDataElementType },
|
|
{ name: 'delta', type: wgslType as UniformDataElementType },
|
|
];
|
|
return `
|
|
${shaderHelper.registerUniforms(uniforms).declareVariables(output)}
|
|
${shaderHelper.mainStart()}
|
|
${shaderHelper.guardAgainstOutOfBoundsWorkgroupSizes('uniforms.outputSize')}
|
|
output[global_idx] = uniforms.start + ${wgslType}(global_idx) * uniforms.delta;
|
|
}`;
|
|
};
|
|
|
|
return {
|
|
name: 'Range',
|
|
shaderCache: { hint: `${dataType}` },
|
|
getShaderSource,
|
|
getRunData: () => ({
|
|
outputs: [{ dims: outputShape, dataType }],
|
|
dispatchGroup: { x: Math.ceil(outputSize / 64 /* workgroup size */) },
|
|
programUniforms,
|
|
}),
|
|
};
|
|
};
|
|
|
|
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);
|
|
}
|
|
|
|
context.compute(createRangeProgramInfo(start, limit, delta, context.inputs[0].dataType), { inputs: [] });
|
|
};
|