@@ -51,13 +51,16 @@ export class Tensor implements TensorInterface {
51
51
*/
52
52
constructor (
53
53
type : TensorType ,
54
- data : TensorDataType | readonly string [ ] | readonly number [ ] | readonly boolean [ ] ,
54
+ data : TensorDataType | Uint8ClampedArray | readonly string [ ] | readonly number [ ] | readonly boolean [ ] ,
55
55
dims ?: readonly number [ ] ,
56
56
) ;
57
57
/**
58
58
* Construct a new CPU tensor object from the given data and dims. Type is inferred from data.
59
59
*/
60
- constructor ( data : TensorDataType | readonly string [ ] | readonly boolean [ ] , dims ?: readonly number [ ] ) ;
60
+ constructor (
61
+ data : TensorDataType | Uint8ClampedArray | readonly string [ ] | readonly boolean [ ] ,
62
+ dims ?: readonly number [ ] ,
63
+ ) ;
61
64
/**
62
65
* Construct a new tensor object from the pinned CPU data with the given type and dims.
63
66
*
@@ -90,12 +93,13 @@ export class Tensor implements TensorInterface {
90
93
arg0 :
91
94
| TensorType
92
95
| TensorDataType
96
+ | Uint8ClampedArray
93
97
| readonly string [ ]
94
98
| readonly boolean [ ]
95
99
| CpuPinnedConstructorParameters
96
100
| TextureConstructorParameters
97
101
| GpuBufferConstructorParameters ,
98
- arg1 ?: TensorDataType | readonly number [ ] | readonly string [ ] | readonly boolean [ ] ,
102
+ arg1 ?: TensorDataType | Uint8ClampedArray | readonly number [ ] | readonly string [ ] | readonly boolean [ ] ,
99
103
arg2 ?: readonly number [ ] ,
100
104
) {
101
105
// perform one-time check for BigInt/Float16Array support
@@ -216,6 +220,12 @@ export class Tensor implements TensorInterface {
216
220
}
217
221
} else if ( arg1 instanceof typedArrayConstructor ) {
218
222
data = arg1 ;
223
+ } else if ( arg1 instanceof Uint8ClampedArray ) {
224
+ if ( arg0 === 'uint8' ) {
225
+ data = Uint8Array . from ( arg1 ) ;
226
+ } else {
227
+ throw new TypeError ( `A Uint8ClampedArray tensor's data must be type of uint8` ) ;
228
+ }
219
229
} else {
220
230
throw new TypeError ( `A ${ type } tensor's data must be type of ${ typedArrayConstructor } ` ) ;
221
231
}
@@ -243,6 +253,9 @@ export class Tensor implements TensorInterface {
243
253
} else {
244
254
throw new TypeError ( `Invalid element type of data array: ${ firstElementType } .` ) ;
245
255
}
256
+ } else if ( arg0 instanceof Uint8ClampedArray ) {
257
+ type = 'uint8' ;
258
+ data = Uint8Array . from ( arg0 ) ;
246
259
} else {
247
260
// get tensor type from TypedArray
248
261
const mappedType = NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP . get (
0 commit comments