onnxruntime/js/web/lib/wasm/jsep/tensor-view.ts
Yulong Wang 9aafbe3feb
[js/web] revise TensorView (#17473)
### Description

This change:
- removes the unused `Tensor` types declared in
/js/web/lib/wasm/jsep/tensor.ts
- removes duplicated util functions in  /js/web/lib/wasm/jsep/tensor.ts
- renames /js/web/lib/wasm/jsep/**tensor.ts** to
/js/web/lib/wasm/jsep/**tensor-view.ts** and update corresponding
references. It was kind of confusing that we have multiple `Tensor`
types defined in different places also we have multiple `tensor.ts`
source files.

This is one of the prerequisites for supporting IO binding for WebGPU
buffer in onnxruntime-web.

list of prerequisites PRs:
https://github.com/microsoft/onnxruntime/pull/17465
https://github.com/microsoft/onnxruntime/pull/17469
https://github.com/microsoft/onnxruntime/pull/17470
https://github.com/microsoft/onnxruntime/pull/17472
https://github.com/microsoft/onnxruntime/pull/17473 (this one)
2023-09-14 21:14:44 -07:00

39 lines
1.1 KiB
TypeScript

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import {Tensor} from 'onnxruntime-common';
import {tensorTypeToTypedArrayConstructor} from '../wasm-common';
export const createView = (dataBuffer: ArrayBuffer, type: Tensor.Type): Int32Array|Uint32Array|BigInt64Array|
BigUint64Array|Uint8Array|Float32Array|Float64Array|Int8Array|Int16Array|Uint16Array =>
new (tensorTypeToTypedArrayConstructor(type))(dataBuffer);
/**
* a TensorView does not own the data.
*/
export interface TensorView {
readonly data: number;
readonly dataType: number;
readonly dims: readonly number[];
/**
* get a Float32Array data view of the tensor data. tensor data must be on CPU.
*/
getFloat32Array(): Float32Array;
/**
* get a BigInt64Array data view of the tensor data. tensor data must be on CPU.
*/
getBigInt64Array(): BigInt64Array;
/**
* get a Int32Array data view of the tensor data. tensor data must be on CPU.
*/
getInt32Array(): Int32Array;
/**
* create a new tensor view with the same data but different dimensions.
*/
reshape(newDims: readonly number[]): TensorView;
}