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

wgsl: Convert quantizeToI32/U32 to use Math.trunc #3120

Merged
merged 1 commit into from
Nov 1, 2023

Conversation

zoddicus
Copy link
Contributor

Another small bump (~5%) to be gained through using a builtin instead of trampolining through a TypedArray.


Requirements for PR author:

  • All missing test coverage is tracked with "TODO" or .unimplemented().
  • New helpers are /** documented */ and new helper files are found in helper_index.txt.
  • Test behaves as expected in a WebGPU implementation. (If not passing, explain above.)

Requirements for reviewer sign-off:

  • Tests are properly located in the test tree.
  • Test descriptions allow a reader to "read only the test plans and evaluate coverage completeness", and accurately reflect the test code.
  • Tests provide complete coverage (including validation control cases). Missing coverage MUST be covered by TODOs.
  • Helpers and types promote readability and maintainability.

When landing this PR, be sure to make any necessary issue status updates.

@zoddicus zoddicus added enhancement New feature or request wgsl labels Oct 31, 2023
@zoddicus zoddicus self-assigned this Oct 31, 2023
if (num <= kValue.i32.negative.min) {
return kValue.i32.negative.min;
}
return Math.round(num);
Copy link
Contributor

Choose a reason for hiding this comment

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

Despite the function's comment, quantizeToI32 wasn't rounding to closest, but was rounding towards zero:

const F = 0.7;

const quantizeToI32Data = new Int32Array(new ArrayBuffer(4));
quantizeToI32Data[0] = F;
var message = `${quantizeToI32Data[0]}, ${Math.round(F)}`;

console.log(message)

This is a change of behaviour. Have you checked that all the places that call this expect the new behaviour?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed these to use Math.trunc to replicate the behaviour. I have run all of the tests under webgpu:shader,execution,expression,* and the unittests, so I think this should be fine.

Another small bump (~5%) to be gained through using a builtin instead
of trampolining through a TypedArray.
@zoddicus zoddicus force-pushed the optimizeQuantizeIntegers branch from a82d059 to 0b436a0 Compare November 1, 2023 02:21
@zoddicus zoddicus changed the title wgsl: Convert quantizeToI32/U32 to used Math.round wgsl: Convert quantizeToI32/U32 to use Math.trunc Nov 1, 2023
@zoddicus zoddicus requested a review from ben-clayton November 1, 2023 02:23
@jiangzhaoming
Copy link
Contributor

The spec said In other words, floating point to integer conversion rounds toward zero, then saturates in the target type.

On the other hand, if all tests are passed no matter using Math.round or Math.trunc, does it imply that some tests on this fp-int conversion is missing?

Copy link
Contributor

@ben-clayton ben-clayton left a comment

Choose a reason for hiding this comment

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

Approving this change, but I agree with:

On the other hand, if all tests are passed no matter using Math.round or Math.trunc, does it imply that some tests on this fp-int conversion is missing?

It sounds like we could do with some more unit tests, and probably some more CTS cases.

@zoddicus
Copy link
Contributor Author

zoddicus commented Nov 1, 2023

Looking at the call sites of these functions, since it has been awhile since I worked with this code. They are only being used as filtering/cleanup of initial input values, i.e. I want you to run CTS on this set of numbers, and these functions are used to fix up the values to be the correct type of integer. They are not being used to adjust things internally in the framework to make sure that results comply with the WGSL rounding rules.

This is why the specific rounding rule doesn't affect if the test is passing, changing it just changes the set of inputs being tested on, not the expected result for a specific value.

@zoddicus zoddicus merged commit ab09ed4 into gpuweb:main Nov 1, 2023
2 checks passed
@zoddicus zoddicus deleted the optimizeQuantizeIntegers branch November 1, 2023 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request wgsl
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants