-
Notifications
You must be signed in to change notification settings - Fork 3.6k
TypeScript: Allow constructing float16 tensors using Float16Array #26742
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
This change updates the TypeScript definitions to allow constructing float16 tensors using Float16Array in environments where it is available. Runtime behavior remains unchanged (float16 is still represented as Uint16Array). - Introduce GlobalFloat16Array helper type to safely detect Float16Array without requiring global polyfills. - Add type-specific and inferred constructor overloads for float16. - No changes to runtime logic or public C APIs. This resolves compile-time errors when passing Float16Array to the Tensor constructor in the onnxruntime-web package.
|
@microsoft-github-policy-service agree |
|
I think you could reuse the onnxruntime/js/common/lib/type-helper.ts Line 27 in 790018d
|
|
Updated to use |
js/common/lib/tensor.ts
Outdated
|
|
||
| // Helper type: resolves to the instance type of `Float16Array` if it exists in the global scope, | ||
| // or `never` otherwise. Uses the shared TryGetGlobalType helper. | ||
| export type GlobalFloat16Array = TryGetGlobalType<'Float16Array', never>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it needs to be exported / part of the public API, just a local helper.
| export type GlobalFloat16Array = TryGetGlobalType<'Float16Array', never>; | |
| type GlobalFloat16Array = TryGetGlobalType<'Float16Array', never>; |
js/common/lib/tensor.ts
Outdated
| */ | ||
| new ( | ||
| type: 'float16', | ||
| data: Tensor.DataTypeMap['float16'] | GlobalFloat16Array | readonly number[], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, wouldn't it be easier to inline this helper into the DataTypeMap?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated DataTypeMap.float16 to inline TryGetGlobalType<'Float16Array'> and map it to the instance type via prototype, so Float16Array instances are accepted where supported without changing runtime behavior.
|
Thanks for working on this! Just to be clear, I'm not a maintainer on this repo, we'll need for one of them to review too. |
Fixes #26741
This change updates the TypeScript definitions to allow constructing
float16tensors usingFloat16Arrayin environments where it is available. Runtime behavior remains unchanged (float16is still represented internally asUint16Array).GlobalFloat16Arrayhelper type to safely detectFloat16Arraywithout requiring global polyfills.float16.This resolves compile-time errors when passing
Float16Arrayto theTensorconstructor in theonnxruntime-webpackage.Description
This PR enhances the TypeScript typings for
float16tensors within the ONNX Runtime JavaScript API:GlobalFloat16Array, a conditional utility type that resolves to the instance type ofFloat16Arrayonly when available.Uint16Array(existing behavior),Float16Array(new behavior, when supported by the JS environment),readonly number[].new Tensor(new Float16Array(...)).Float16Arraywithout encountering type errors.Internally, ONNX Runtime continues to treat
float16data asUint16Array, so runtime behavior is unchanged.Motivation and Context
Modern JavaScript runtimes (browsers and Node versions) have begun introducing native
Float16Arraysupport. Developers using ONNX Runtime in TypeScript projects may attempt to constructfloat16tensors using: