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

[Test][Topi] Avoid depending on f32 rounding behavior for crop_and_divide tests #13773

Merged
merged 4 commits into from
Apr 5, 2023

Conversation

Lunderberg
Copy link
Contributor

@Lunderberg Lunderberg commented Jan 12, 2023

The crop_and_resize operator uses floating-point arithmetic to determine whether an index is within a view-box. This can cause the use of extrapolation_value to depend on target-dependent rounding differences.

For example, this issue was initially noticed on Vulkan during debugging of #13530, and was the result of computing 0.2*223.0 + 0.8*223.0 < 223.0. If all intermediates are cast to float32, the left-hand side evaluates to 223.00002. If intermediates are kept at a higher precision, the left-hand side evaluates to 223.0.

The floating-point indexing can't be removed, because the operator must match the API defined by TensorFlow's operator implementation. The TensorFlow documentation for CropAndResize does not specify behavior in these cases, nor do the current TensorFlow unit tests check cases of rounding error. Since the TensorFlow unit tests only use binary fractions for the boxes argument, which largely avoids the rounding issue, this commit updates the TVM unit tests to avoid depending on floating-point precision.

@tvm-bot
Copy link
Collaborator

tvm-bot commented Jan 12, 2023

Thanks for contributing to TVM! Please refer to the contributing guidelines https://tvm.apache.org/docs/contribute/ for useful information and tips. Please request code reviews from Reviewers by @-ing them in a comment.

  • No users to tag found in teams: test, topi See #10317 for details

Generated by tvm-bot

@Lunderberg Lunderberg force-pushed the crop_and_resize_binary_fraction_box branch from fa50f8d to 70aaff7 Compare January 25, 2023 14:15
@Lunderberg
Copy link
Contributor Author

Failure wasn't reproducible locally with 1k repetitions, each with a different random input. Re-running CI with a fixed seed for random input data.

@Lunderberg
Copy link
Contributor Author

The errors in the i386 test look like they are due to the same type of rounding issue that this PR was intended to avoid. That is, when an input pixel falls exactly on the edge of a bounding box, whether the extrapolation_value is used depends entirely on the numerical rounding error. Rather than trying to reproduce exact errors, updating the PR such that the tests have some epsilon different applied to the bounding boxes.

Lunderberg and others added 4 commits March 31, 2023 09:52
The `crop_and_resize` operator uses floating-point arithmetic to
determine whether an index is within a view-box.  This can cause the
use of `extrapolation_value` to depend on target-dependent rounding
differences.

For example, this issue was initially noticed on Vulkan during
debugging of apache#13530, and was the
result of computing `0.2*223.0 + 0.8*223.0 < 223.0`.  If all
intermediates are cast to float32, the left-hand side evaluates to
`223.00002`.  If intermediates are kept at a higher precision, the
left-hand side evaluates to `223.0`.

The floating-point indexing can't be removed, because the operator
must match the API defined by TensorFlow's operator implementation.
The TensorFlow documentation for
[`CropAndResize`](https://www.tensorflow.org/api_docs/cc/class/tensorflow/ops/crop-and-resize)
does not specify behavior in these cases, nor do the current
TensorFlow unit tests check cases of rounding error.  Since the
TensorFlow unit tests only use binary fractions for the `boxes`
argument, which largely avoids the rounding issue, this commit updates
the TVM unit tests to avoid depending on floating-point precision.
This mimics the example usage of `tf.image.crop_and_resize`, whose API
this operator is intended to follow.  Using any boxes with edges
representable as integer fractions has the potential for the in-bounds
check to be impacted by rounding error (e.g. `0.2*x + 0.8*x < x`).
Unfortunately, there's no way to remove this possibility altogether
without changing the API, such as accepting the box location as an
integer fraction, rather than a `float32`, but that would break
compatibility.

To avoid the risk of a flaky unit test based on the specific random
boxes used, the PRNG is seeded prior to generation.
@Lunderberg Lunderberg force-pushed the crop_and_resize_binary_fraction_box branch from 0166f49 to aad6f0e Compare March 31, 2023 15:04
@Lunderberg Lunderberg changed the title [Test][Topi] Use binary fractions for crop_and_divide unit tests [Test][Topi] Avoid depending on f32 rounding behavior for crop_and_divide tests Apr 5, 2023
@masahi masahi merged commit 6caf085 into apache:main Apr 5, 2023
@Lunderberg Lunderberg deleted the crop_and_resize_binary_fraction_box branch April 5, 2023 19:16
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.

3 participants