Tensor construction fails for the majority of TypedArray types
controversial opened this issue · comments
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 TypedArray
s, (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
Hi, @controversial
Thank you for bringing this issue to our attention and I was able to reproduce the behavior on my end as well. It appears there may be a discrepancy between the TensorFlow.js documentation and the currently supported TypedArrays
for tensor construction. While the documentation lists broader compatibility with all TypedArrays
but at the moment seems like it only support (Uint8Array, Uint8ClampedArray, Int32Array, Float32Array)
for tensor construction so most probably this issue will be considered as feature request if TensorFlow.js does not support remaining TypedArrays
for tensor construction and I'll discuss this issue in our internal meeting and will update you soon on this. thank you for your valuable time and efforts bringing this issue to our attention
Here is output log for reference :
(base) gaikwadrahul-macbookpro:test-8257 gaikwadrahul$ node index.js
- failure: Int8Array
+ success: Uint8Array
+ success: Uint8ClampedArray
- failure: Int16Array
- failure: Uint16Array
+ success: Int32Array
- failure: Uint32Array
+ success: Float32Array
- failure: Float64Array
(base) gaikwadrahul-macbookpro:test-8257 gaikwadrahul$
Thank you for your understanding and patience.