1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
| // Copyright (c) Microsoft Corporation. All rights reserved.
| // Licensed under the MIT License.
|
| import assert from 'assert/strict';
| import { Tensor } from 'onnxruntime-common';
|
| import {
| ALL_NUMERICAL_TYPES,
| assertIsArrayOf,
| BIGINT_TYPES,
| NUMBER_COMPATIBLE_NUMERICAL_TYPES,
| TensorAny,
| } from '../common.js';
|
| describe('Tensor Constructor Tests - check types', () => {
| for (const [type, typedArrayConstructor, canBeInferredFromType] of ALL_NUMERICAL_TYPES) {
| it(`[${type}] new Tensor(type, typedArray, dims): "tensor.type" should match type passed in`, () => {
| const tensor = new Tensor(type, new typedArrayConstructor(4), [2, 2]);
| assert.equal(tensor.type, type, `tensor.type should be '${type}'`);
| });
|
| it(`[${type}] new Tensor(type, typedArray, dims): "tensor.data" should be instance of expected typed array`, () => {
| const tensor = new Tensor(type, new typedArrayConstructor(4), [2, 2]);
| assert(
| tensor.data instanceof typedArrayConstructor,
| `tensor.data should be an instance of '${typedArrayConstructor.name}'`,
| );
| });
|
| if (canBeInferredFromType) {
| it(`[${type}] new Tensor(typedArray, dims): "tensor.type" should match inferred type`, () => {
| const tensor = new Tensor(new typedArrayConstructor(4), [2, 2]);
| assert.equal(tensor.type, type, `tensor.type should be '${type}'`);
| });
| }
|
| it(`[${type}] new Tensor(type, {}, dims): expect to throw because data is invalid`, () => {
| assert.throws(() => new TensorAny(type, {}, [2, 2]), TypeError);
| });
|
| it(`[${type}] new Tensor(type, arrayBuffer, dims): expect to throw because data is invalid`, () => {
| assert.throws(() => new TensorAny(type, new ArrayBuffer(100), [2, 2]), TypeError);
| });
| }
|
| for (const [type] of NUMBER_COMPATIBLE_NUMERICAL_TYPES) {
| it(`[${type}] new Tensor(type, numbers, dims): tensor can be constructed from number array`, () => {
| const tensor = new Tensor(type, [1, 2, 3, 4], [2, 2]);
| assert.equal(tensor.type, type, `tensor.type should be '${type}'`);
| });
| }
|
| for (const [type] of BIGINT_TYPES) {
| it(`[${type}] new Tensor(type, numbers, dims): tensor can be constructed from number array`, () => {
| const tensor = new Tensor(type, [1, 2, 3, 4], [2, 2]);
| assert.equal(tensor.type, type, `tensor.type should be '${type}'`);
| });
|
| it(`[${type}] new Tensor(type, bigints, dims): tensor can be constructed from bigint array`, () => {
| const tensor = new Tensor(type, [1n, 2n, 3n, 4n], [2, 2]);
| assert.equal(tensor.type, type, `tensor.type should be '${type}'`);
| });
| }
|
| it('[string] new Tensor(\'string\', strings, dims): "tensor.type" should match type passed in', () => {
| const tensor = new Tensor('string', ['a', 'b', 'c', 'd'], [2, 2]);
| assert.equal(tensor.type, 'string', "tensor.type should be 'string'");
| });
|
| it('[string] new Tensor(strings, dims): "tensor.data" should match inferred type', () => {
| const tensor = new Tensor(['a', 'b', 'c', 'd'], [2, 2]);
| assert.equal(tensor.type, 'string', "tensor.type should be 'string'");
| });
|
| it('[string] new Tensor(\'string\', strings, dims): "tensor.data" should be a string array', () => {
| const tensor = new Tensor('string', ['a', 'b', 'c', 'd'], [2, 2]);
| assertIsArrayOf(tensor.data, 'string');
| });
|
| it('[bool] new Tensor(\'bool\', booleans, dims): "tensor.type" should match type passed in', () => {
| const tensor = new Tensor('bool', [true, false, true, false], [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
| const tensor = new Tensor('uint8', uint8ClampedArray, [2]);
| assert.equal(tensor.type, 'uint8', "tensor.type should be 'uint8'");
| });
|
| 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('[bool] new Tensor(booleans, dims): "tensor.data" should match inferred type', () => {
| const tensor = new Tensor([true, false, true, false], [2, 2]);
| assert.equal(tensor.type, 'bool', "tensor.type should be 'bool'");
| });
|
| it('[bool] new Tensor(\'bool\', booleans, dims): "tensor.data" should be a boolean array', () => {
| const tensor = new Tensor('bool', [true, false, true, false], [2, 2]);
| assert(tensor.data instanceof Uint8Array, "tensor.data should be an instance of 'Uint8Array'");
| });
|
| it("[badtype] new Tensor('a', numbers, dims): expect to throw because 'a' is an invalid type", () => {
| assert.throws(() => new TensorAny('a', [1, 2, 3, 4], [2, 2]), TypeError);
| });
| });
|
|