Skip to content

Tensor construction fails for the majority of TypedArray types #8257

@controversial

Description

@controversial

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow.js): yes
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): macOs 14.4.1 (23E224) / Darwin 23.4.0 arm64 arm
  • TensorFlow.js installed from (npm or script link): npm
  • TensorFlow.js version (use command below): 4.18.0
  • Browser version: Node.js v20.12.2
  • Tensorflow.js Converter Version: 4.18.0

Describe the current behavior

Only some types of TypedArrays can be passed to tf.tensor:

import tf from '@tensorflow/tfjs-node';

const test = (arr) => {
  try {
    tf.tensor(arr);
    console.log('+ success:', arr.constructor.name);
  } catch (e) {
    console.error('- failure:', arr.constructor.name);
  }
}

test(new Int8Array([1, 2, 3]));
test(new Uint8Array([1, 2, 3]));
test(new Uint8ClampedArray([1, 2, 3]));
test(new Int16Array([1, 2, 3]));
test(new Uint16Array([1, 2, 3]));
test(new Int32Array([1, 2, 3]));
test(new Uint32Array([1, 2, 3]));
test(new Float32Array([1, 2, 3]));
test(new Float64Array([1, 2, 3]));

Output:

- failure: Int8Array
+ success: Uint8Array
+ success: Uint8ClampedArray
- failure: Int16Array
- failure: Uint16Array
+ success: Int32Array
- failure: Uint32Array
+ success: Float32Array
- failure: Float64Array

The error for the failing arrays is:

/Users/luke/Developer/misc/tfjs-node-test/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:5710
        throw new Error('values passed to tensor(values) must be a number/boolean/string or ' +
              ^

Error: values passed to tensor(values) must be a number/boolean/string or an array of numbers/booleans/strings, or a TypedArray
    at makeTensor (/Users/luke/Developer/misc/tfjs-node-test/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:5710:15)
    at Object.tensor (/Users/luke/Developer/misc/tfjs-node-test/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:5936:12)
    at test (file:///Users/luke/Developer/misc/tfjs-node-test/test.js:5:8)
    at file:///Users/luke/Developer/misc/tfjs-node-test/test.js:15:1
    at ModuleJob.run (node:internal/modules/esm/module_job:222:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)
    at async loadESM (node:internal/process/esm_loader:28:7)
    at async handleMainPromise (node:internal/modules/run_main:113:12)

Describe the expected behavior

Per the docs:

value “Can be nested array of numbers, or a flat array, or a TypedArray, or a WebGLData object, or a WebGPUData object”

All TypedArrays, (listed on the MDN page that’s linked in the TensorFlow.js docs), should be valid for the tf.tensor constructor.

Standalone code to reproduce the issue

See above

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions