mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-05-16 21:00:14 +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.
62 lines
1.8 KiB
TypeScript
62 lines
1.8 KiB
TypeScript
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
// Licensed under the MIT License.
|
|
|
|
import {
|
|
CpuPinnedConstructorParameters,
|
|
GpuBufferConstructorParameters,
|
|
TextureConstructorParameters,
|
|
} from './tensor-factory.js';
|
|
import { Tensor } from './tensor-impl.js';
|
|
|
|
/**
|
|
* calculate size from dims.
|
|
*
|
|
* @param dims the dims array. May be an illegal input.
|
|
*/
|
|
export const calculateSize = (dims: readonly unknown[]): number => {
|
|
let size = 1;
|
|
for (let i = 0; i < dims.length; i++) {
|
|
const dim = dims[i];
|
|
if (typeof dim !== 'number' || !Number.isSafeInteger(dim)) {
|
|
throw new TypeError(`dims[${i}] must be an integer, got: ${dim}`);
|
|
}
|
|
if (dim < 0) {
|
|
throw new RangeError(`dims[${i}] must be a non-negative integer, got: ${dim}`);
|
|
}
|
|
size *= dim;
|
|
}
|
|
return size;
|
|
};
|
|
|
|
/**
|
|
* implementation of Tensor.reshape()
|
|
*/
|
|
export const tensorReshape = (tensor: Tensor, dims: readonly number[]): Tensor => {
|
|
switch (tensor.location) {
|
|
case 'cpu':
|
|
return new Tensor(tensor.type, tensor.data, dims);
|
|
case 'cpu-pinned':
|
|
return new Tensor({
|
|
location: 'cpu-pinned',
|
|
data: tensor.data as CpuPinnedConstructorParameters['data'],
|
|
type: tensor.type as CpuPinnedConstructorParameters['type'],
|
|
dims,
|
|
});
|
|
case 'texture':
|
|
return new Tensor({
|
|
location: 'texture',
|
|
texture: tensor.texture,
|
|
type: tensor.type as TextureConstructorParameters['type'],
|
|
dims,
|
|
});
|
|
case 'gpu-buffer':
|
|
return new Tensor({
|
|
location: 'gpu-buffer',
|
|
gpuBuffer: tensor.gpuBuffer,
|
|
type: tensor.type as GpuBufferConstructorParameters['type'],
|
|
dims,
|
|
});
|
|
default:
|
|
throw new Error(`tensorReshape: tensor location ${tensor.location} is not supported`);
|
|
}
|
|
};
|