Skip to content
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

[js/common] allows using Uint16Array as data for float16 tensor #23827

Merged
merged 3 commits into from
Mar 3, 2025

Conversation

fs-eire
Copy link
Contributor

@fs-eire fs-eire commented Feb 26, 2025

Description

Resolve #23817

Motivation and Context

Copy link
Contributor

@fdwr fdwr left a comment

Choose a reason for hiding this comment

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

Thanks Yulong.

@fs-eire
Copy link
Contributor Author

fs-eire commented Feb 27, 2025

@Honry @xenova Could you please help to check if this PR fixes the problem? It should work but I don't have a chance to test it E2E.

Artifacts can be downloaded at https://dev.azure.com/onnxruntime/onnxruntime/_build/results?buildId=1629021&view=artifacts&pathAsName=false&type=publishedArtifacts

@Honry
Copy link
Contributor

Honry commented Feb 27, 2025

@fs-eire, verified, thanks for the quick fix.

Copy link
Contributor

@xenova xenova left a comment

Choose a reason for hiding this comment

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

Thanks for the fix! For transformers.js, we'll also default to Float16Array if available: #23817

@Honry
Copy link
Contributor

Honry commented Feb 27, 2025

@fs-eire, hold on, this still breaks demos that read the outputs as Uint16Array. Now you always save the output to Float16Array if Float16Array is available, however existing demos still treat it as Uint16Array.

@fdwr
Copy link
Contributor

fdwr commented Feb 27, 2025

@fs-eire, hold on, this still breaks demos that read the outputs as Uint16Array. Now you always save the output to Float16Array if Float16Array is available, however existing demos still treat it as Uint16Array.

Ooh, that's more complicated. How does ORT know what the output type should be? It could look at the input types to deduce the output types (e.g. if there were uint16-as-float16 input tensors, then presumably any output float16 tensors should be uint16 too), but it wouldn't be fully robust (e.g. say you had a model that took int32 inputs and float16-as-uint16 outputs, which there is no input hint to know the output type). Well, it might be a good enough transient heuristic for now.

@Honry
Copy link
Contributor

Honry commented Feb 27, 2025

Ooh, that's more complicated. How does ORT know what the output type should be? It could look at the input types to deduce the output types (e.g. if there were uint16-as-float16 input tensors, then presumably any output float16 tensors should be uint16 too), but it wouldn't be fully robust (e.g. say you had a model that took int32 inputs and float16-as-uint16 outputs, which there is no input hint to know the output type). Well, it might be a good enough transient heuristic for now.

Exactly, it’s difficult to know what the output type should be. Users have to add API check to determine if Float16Array should be used.

@fs-eire
Copy link
Contributor Author

fs-eire commented Feb 27, 2025

Ooh, that's more complicated. How does ORT know what the output type should be? It could look at the input types to deduce the output types (e.g. if there were uint16-as-float16 input tensors, then presumably any output float16 tensors should be uint16 too), but it wouldn't be fully robust (e.g. say you had a model that took int32 inputs and float16-as-uint16 outputs, which there is no input hint to know the output type). Well, it might be a good enough transient heuristic for now.

Exactly, it’s difficult to know what the output type should be. Users have to add API check to determine if Float16Array should be used.

as explained by @fdwr, I think there is no way to do this inside ORT

to fix this problem, you need to modify the application code. a simple way is that for all float16 output, you always create a Uint16Array wrapper:

if (myTensor.type === 'float16') {
   myData = new Uint16Array(myTensor.data.buffer, myTensor.data.byteOffset, myTensor.data.length);
}

@fs-eire
Copy link
Contributor Author

fs-eire commented Feb 27, 2025

@Honry Please let me know if you are OK with this change or we need further discussion

@Honry
Copy link
Contributor

Honry commented Feb 28, 2025

@Honry Please let me know if you are OK with this change or we need further discussion

Make sense, and we should remind end users to change their application code if need.

@fdwr
Copy link
Contributor

fdwr commented Feb 28, 2025

@Honry Please let me know if you are OK with this change or we need further discussion

Make sense, and we should remind end users to change their application code if need.

Sounds worth adding to the breaking changes section: https://github.com/microsoft/webnn-developer-preview?tab=readme-ov-file#breaking-changes

Copy link
Contributor

@Honry Honry left a comment

Choose a reason for hiding this comment

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

👍

@fs-eire fs-eire merged commit 1872527 into main Mar 3, 2025
93 of 95 checks passed
@fs-eire fs-eire deleted the fs-eire/js-support-f16-uint16array branch March 3, 2025 21:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Web] Shall we accept Uint16Array for 'float16' if Float16Array is available
5 participants