onnxruntime/js/web/lib/onnxjs/backends/webgl/ops/split.ts
Yulong Wang abdc31de40
[js] change default formatter for JavaScript/TypeScript from clang-format to Prettier (#21728)
### 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.
2024-08-14 16:51:22 -07:00

110 lines
3.3 KiB
TypeScript

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { AttributeWithCacheKey, createAttributeWithCacheKey } from '../../../attribute-with-cache-key';
import { Graph } from '../../../graph';
import { OperatorImplementation, OperatorInitialization } from '../../../operators';
import { Tensor } from '../../../tensor';
import { ShapeUtil, SplitUtil } from '../../../util';
import { WebGLInferenceHandler } from '../inference-handler';
import { ProgramInfo, TextureType } from '../types';
export interface SplitAttributes extends AttributeWithCacheKey {
readonly axis: number;
readonly split: number[];
readonly numOutputs: number;
}
const splitProgramMetadata = {
name: 'Split',
inputNames: ['A'],
inputTypes: [TextureType.unpacked],
};
export const split: OperatorImplementation<SplitAttributes> = (
inferenceHandler: WebGLInferenceHandler,
inputs: Tensor[],
attributes: SplitAttributes,
): Tensor[] => {
validateInputs(inputs);
const axis = ShapeUtil.normalizeAxis(attributes.axis, inputs[0].dims.length);
const count = getProgramCount(inferenceHandler, inputs, axis, attributes);
const output: Tensor[] = [];
for (let i = 0; i < count; ++i) {
output.push(
inferenceHandler.run(
{
...splitProgramMetadata,
cacheHint: `${attributes.cacheKey};${i}`,
get: () => createSplitProgramInfo(inferenceHandler, inputs[0], attributes, axis, i),
},
inputs,
),
);
}
return output;
};
export const parseSplitAttributes: OperatorInitialization<SplitAttributes> = (node: Graph.Node): SplitAttributes => {
const axis = node.attributes.getInt('axis', 0);
const split = node.attributes.getInts('split', []);
const numOutputs = node.outputs.length;
return createAttributeWithCacheKey({ axis, split, numOutputs });
};
const getProgramCount = (
_inferenceHandler: WebGLInferenceHandler,
inputs: Tensor[],
axis: number,
attributes: SplitAttributes,
): number => {
const [, offsets] = SplitUtil.splitShape(inputs[0].dims, axis, attributes.split, attributes.numOutputs);
return offsets.length;
};
const createSplitProgramInfo = (
_inferenceHandler: WebGLInferenceHandler,
input: Tensor,
attributes: SplitAttributes,
axis: number,
index: number,
): ProgramInfo => {
const [shapes, offsets] = SplitUtil.splitShape(input.dims, axis, attributes.split, attributes.numOutputs);
const offset = offsets[index];
const outputShape = shapes[index];
const rank = outputShape.length;
const shaderSource = `
float process(int indices[${rank}]) {
indices[${axis}] += ${offset};
return _A(indices);
}
`;
return {
...splitProgramMetadata,
cacheHint: `${attributes.cacheKey}:${index}`,
output: { dims: outputShape, type: input.type, textureType: TextureType.unpacked },
shaderSource,
};
};
const validateInputs = (inputs: Tensor[]): void => {
if (!inputs || inputs.length !== 1) {
throw new Error('Split requires one input.');
}
if (
inputs[0].type !== 'int8' &&
inputs[0].type !== 'uint8' &&
inputs[0].type !== 'int16' &&
inputs[0].type !== 'uint16' &&
inputs[0].type !== 'int32' &&
inputs[0].type !== 'uint32' &&
inputs[0].type !== 'float32' &&
inputs[0].type !== 'float64' &&
inputs[0].type !== 'bool'
) {
throw new Error('Invalid input type.');
}
};