onnxruntime/js/common/lib
Yulong Wang e5ca3f3dcb
[js/api] introducing IO binding for tensor (#16452)
[//]: # (## Work In Progress. Feedbacks are welcome!)

### Description
This PR adds a few properties, methods and factories to Tensor type to
support IO-binding feature. This will allow user to create tensor from
GPU/CPU bound data without a force transferring of data between CPU and
GPU.

This change is a way to resolve #15312

### Change Summary
1. Add properties to `Tensor` type:
a. `location`: indicating where the data is sitting. valid values are
`cpu`, `cpu-pinned`, `texture`, `gpu-buffer`.
b. `texture`: sit side to `data`, a readonly property of `WebGLTexture`
type. available only when `location === 'texture'`
c. `gpuBuffer`: sit side to `data`, a readonly property of `GPUBuffer`
type. available only when `location === 'gpu-buffer'`

2. Add methods to `Tensor` type (usually dealing with inference
outputs):
- async function `getData()` allows user to download data from GPU to
CPU manually.
- function `dispose()` allows user to release GPU resources manually.

3. Add factories for creating `Tensor` instances:
    a. `fromTexture()` to create a WebGL texture bound tensor data
    b. `fromGpuBuffer()` to create a WebGPUBuffer bound tensor data
    c. `fromPinnedBuffer()` to create a tensor using a CPU pinned buffer

### Examples:

create tensors from texture and pass to inference session as inputs
```js
// when create session, specify we prefer 'image_output:0' to be stored on GPU as texture
const session = await InferenceSession.create('./my_model.onnx', {
  executionProviders: [ 'webgl' ],
  preferredOutputLocation: { 'image_output:0': 'texture' }
});

...

const myImageTexture = getTexture(); // user's function to get a texture
const myFeeds = { input0: Tensor.fromTexture(myImageTexture, { width: 224, height: 224 }) }; // shape [1, 224, 224, 4], RGBA format.
const results = await session.run(myFeeds);
const myOutputTexture = results['image_output:0'].texture;
```
2023-08-29 12:58:26 -07:00
..
backend-impl.ts [js/common] allow import onnxruntime-common as ESM and CJS (#15772) 2023-06-12 12:05:11 -07:00
backend.ts [js/common] allow import onnxruntime-common as ESM and CJS (#15772) 2023-06-12 12:05:11 -07:00
env-impl.ts [js/common] allow import onnxruntime-common as ESM and CJS (#15772) 2023-06-12 12:05:11 -07:00
env.ts [js/api] introducing IO binding for tensor (#16452) 2023-08-29 12:58:26 -07:00
index.ts [js/common] allow import onnxruntime-common as ESM and CJS (#15772) 2023-06-12 12:05:11 -07:00
inference-session-impl.ts [js/common] allow import onnxruntime-common as ESM and CJS (#15772) 2023-06-12 12:05:11 -07:00
inference-session.ts [js/api] introducing IO binding for tensor (#16452) 2023-08-29 12:58:26 -07:00
onnx-value.ts [js/api] introducing IO binding for tensor (#16452) 2023-08-29 12:58:26 -07:00
tensor-conversion-impl.ts [js/common] allow import onnxruntime-common as ESM and CJS (#15772) 2023-06-12 12:05:11 -07:00
tensor-conversion.ts [js/common] allow import onnxruntime-common as ESM and CJS (#15772) 2023-06-12 12:05:11 -07:00
tensor-factory-impl.ts [js/api] introducing IO binding for tensor (#16452) 2023-08-29 12:58:26 -07:00
tensor-factory.ts [js/api] introducing IO binding for tensor (#16452) 2023-08-29 12:58:26 -07:00
tensor-impl-type-mapping.ts [js/api] introducing IO binding for tensor (#16452) 2023-08-29 12:58:26 -07:00
tensor-impl.ts [js/api] introducing IO binding for tensor (#16452) 2023-08-29 12:58:26 -07:00
tensor-utils-impl.ts [js/api] introducing IO binding for tensor (#16452) 2023-08-29 12:58:26 -07:00
tensor-utils.ts [js/common] allow import onnxruntime-common as ESM and CJS (#15772) 2023-06-12 12:05:11 -07:00
tensor.ts [js/api] introducing IO binding for tensor (#16452) 2023-08-29 12:58:26 -07:00
version.ts [js] add API that allows to get package version (#16207) 2023-06-09 16:18:53 -07:00