onnxruntime/js/web/lib/onnxjs/backends/webgl/ops/sum.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

73 lines
2.2 KiB
TypeScript

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { Tensor } from '../../../tensor';
import { getGlsl } from '../glsl-source';
import { WebGLInferenceHandler } from '../inference-handler';
import { ProgramInfo, ProgramMetadata, TextureType } from '../types';
export const sum = (inferenceHandler: WebGLInferenceHandler, inputs: Tensor[]): Tensor[] => {
validateInputs(inputs);
const sumProgramMetadata = {
name: 'Sum',
inputNames: inputs.map((_v, i) => `X${i}`),
inputTypes: new Array(inputs.length).fill(TextureType.unpacked),
};
const output = inferenceHandler.run(
{ ...sumProgramMetadata, get: () => createSumProgramInfo(inferenceHandler, inputs, sumProgramMetadata) },
inputs,
);
return [output];
};
const createSumProgramInfo = (
inferenceHandler: WebGLInferenceHandler,
inputs: Tensor[],
sumProgramMetadata: ProgramMetadata,
): ProgramInfo => {
const glsl = getGlsl(inferenceHandler.session.backend.glContext.version);
const outputShape = inputs[0].dims.slice();
const sumLine = inputs.map((_v, i) => `${glsl.texture2D}(X${i},TexCoords)`).join(' + ');
const shaderSource = `
void main() {
vec4 result = ${sumLine};
${glsl.output} = result;
}
`;
return {
...sumProgramMetadata,
output: { dims: outputShape, type: inputs[0].type, textureType: TextureType.unpacked },
hasMain: true,
shaderSource,
};
};
const validateInputs = (inputs: Tensor[]): void => {
if (!inputs || inputs.length === 0) {
throw new Error('Sum requires inputs.');
}
const length = inputs[0].dims.length;
for (let i = 1; i < inputs.length; i++) {
if (length !== inputs[i].dims.length) {
throw new Error('Input shapes are mismatched.');
}
for (let j = 0; j < length; j++) {
if (inputs[0].dims[j] !== inputs[i].dims[j]) {
throw new Error('Input shapes are not matched.');
}
}
}
if (inputs[0].type !== 'float32' && inputs[0].type !== 'float64') {
throw new Error('Invalid input type.');
}
for (let i = 1; i < inputs.length; i++) {
if (inputs[0].type !== inputs[i].type) {
throw new Error('Input types are not matched.');
}
}
};