Skip to content

[Node.js] Fix float16 tensor input support#27327

Open
ingyukoh wants to merge 2 commits intomicrosoft:mainfrom
ingyukoh:fix-nodejs-float16-tensor-input
Open

[Node.js] Fix float16 tensor input support#27327
ingyukoh wants to merge 2 commits intomicrosoft:mainfrom
ingyukoh:fix-nodejs-float16-tensor-input

Conversation

@ingyukoh
Copy link
Contributor

Accept both Uint16Array and Float16Array for float16 tensors in the Node.js binding. Float16Array is a newer JavaScript type (ES2024) that N-API supports as napi_float16_array (type 11) in Node.js 23+.

For older Node.js versions, define napi_float16_array to ensure compatibility when users pass Float16Array data.

Summary

Fixes #26791 - Users cannot pass float16 input tensors from JavaScript.

Changes:

  • Define napi_float16_array for older Node.js versions (added in Node.js 23/N-API v10)
  • Accept both Uint16Array and Float16Array for float16 tensors in validation

Root cause: The validation in tensor_helper.cc only accepted Uint16Array for float16
tensors, rejecting the newer Float16Array type.

Test Plan

  • Test with Uint16Array for float16 tensors (all Node.js versions)
  • Test with Float16Array for float16 tensors (Node.js 22+)

Accept both Uint16Array and Float16Array for float16 tensors in the
Node.js binding. Float16Array is a newer JavaScript type (ES2024) that
N-API supports as napi_float16_array (type 11) in Node.js 23+.

For older Node.js versions, define napi_float16_array to ensure
compatibility when users pass Float16Array data.

Fixes microsoft#26791

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes an issue where users could not pass float16 input tensors from JavaScript in the Node.js binding. The problem was that the validation logic only accepted Uint16Array for float16 tensors, but users may also pass the newer Float16Array type (added in ES2024 and supported in Node.js 23+).

Changes:

  • Define napi_float16_array constant for older Node.js versions to ensure compatibility
  • Update tensor validation logic to accept both Uint16Array and Float16Array for float16 tensors
  • Update error message to mention Float16Array support

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Address review comment: the "or Float16Array" text in the type
mismatch error was shown for all tensor types. Now it only appears
when the expected type is float16.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] [Node.js] No way to pass float16 input tensors

1 participant