Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds support for Uint8ClampedArray #21985

Merged
merged 22 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions js/common/lib/tensor-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ export class Tensor implements TensorInterface {
}
} else if (arg1 instanceof typedArrayConstructor) {
data = arg1;
} else if (arg1 instanceof Uint8ClampedArray) {
data = Uint8Array.from(arg1);
} else {
throw new TypeError(`A ${type} tensor's data must be type of ${typedArrayConstructor}`);
}
Expand Down Expand Up @@ -243,6 +245,9 @@ export class Tensor implements TensorInterface {
} else {
throw new TypeError(`Invalid element type of data array: ${firstElementType}.`);
}
} else if (arg0 instanceof Uint8ClampedArray) {
type = "uint8";
data = Uint8Array.from(arg0);
} else {
// get tensor type from TypedArray
const mappedType = NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.get(
Expand Down
4 changes: 2 additions & 2 deletions js/common/lib/tensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ interface TypedTensorBase<T extends Tensor.Type> {
export declare namespace Tensor {
interface DataTypeMap {
float32: Float32Array;
uint8: Uint8Array;
uint8: Uint8Array | Uint8ClampedArray;
prathikr marked this conversation as resolved.
Show resolved Hide resolved
int8: Int8Array;
uint16: Uint16Array;
int16: Int16Array;
Expand Down Expand Up @@ -243,7 +243,7 @@ export interface TensorConstructor extends TensorFactory {
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: Uint8Array, dims?: readonly number[]): TypedTensor<'uint8'>;
new (data: Uint8Array | Uint8ClampedArray, dims?: readonly number[]): TypedTensor<'uint8'>;

/**
* Construct a new uint16 tensor object from the given data and dims.
Expand Down
11 changes: 11 additions & 0 deletions js/common/test/type-tests/tensor/create-new-uint8.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import * as ort from 'onnxruntime-common';

// construct from Uint8ClampedArray
//
// {type-tests}|pass
new ort.Tensor(new Uint8ClampedArray(1))
prathikr marked this conversation as resolved.
Show resolved Hide resolved


15 changes: 12 additions & 3 deletions js/common/test/unit-tests/tensor/constructor-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,18 @@ describe('Tensor Constructor Tests - check types', () => {
assert.equal(tensor.type, 'bool', "tensor.type should be 'bool'");
});

it("[bool] new Tensor('bool', uint8Array, dims): tensor can be constructed from Uint8Array", () => {
const tensor = new Tensor('bool', new Uint8Array([1, 0, 1, 0]), [2, 2]);
assert.equal(tensor.type, 'bool', "tensor.type should be 'bool'");
it("[uint8] new Tensor(uint8ClampedArray, dims): uint8 tensor can be constructed from Uint8ClampedArray", () => {
const uint8ClampedArray = new Uint8ClampedArray(2);
uint8ClampedArray[0] = 0;
uint8ClampedArray[1] = 256; // clamped
assert.equal(uint8ClampedArray[1], 255, "uint8ClampedArray[1] should be clamped to 255");
prathikr marked this conversation as resolved.
Show resolved Hide resolved
const tensor = new Tensor('uint8', uint8ClampedArray, [2]);
prathikr marked this conversation as resolved.
Show resolved Hide resolved
assert.equal(tensor.type, 'uint8', "tensor.type should be 'uint8'");
});

it("[clamp] new Tensor('bool', uint8Array, dims): tensor can be constructed from Uint8Array", () => {
prathikr marked this conversation as resolved.
Show resolved Hide resolved
const tensor = new Tensor('bool', new Uint8Array([1, 0, 1, 0]), [2, 2]);
assert.equal(tensor.type, 'bool', "tensor.type should be 'bool'");
});

it('[bool] new Tensor(booleans, dims): "tensor.data" should match inferred type', () => {
Expand Down
Loading