[Node.js] Fix float16 tensor input support#27327
Open
ingyukoh wants to merge 2 commits intomicrosoft:mainfrom
Open
[Node.js] Fix float16 tensor input support#27327ingyukoh wants to merge 2 commits intomicrosoft:mainfrom
ingyukoh wants to merge 2 commits intomicrosoft:mainfrom
Conversation
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>
Contributor
There was a problem hiding this comment.
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_arrayconstant for older Node.js versions to ensure compatibility - Update tensor validation logic to accept both
Uint16ArrayandFloat16Arrayfor 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
napi_float16_arrayfor older Node.js versions (added in Node.js 23/N-API v10)Uint16ArrayandFloat16Arrayfor float16 tensors in validationRoot cause: The validation in
tensor_helper.cconly acceptedUint16Arrayfor float16tensors, rejecting the newer
Float16Arraytype.Test Plan
Uint16Arrayfor float16 tensors (all Node.js versions)Float16Arrayfor float16 tensors (Node.js 22+)