Skip to content

Commit

Permalink
[Test][Topi] Use binary fractions for crop_and_divide unit tests
Browse files Browse the repository at this point in the history
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`](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.
  • Loading branch information
Lunderberg committed Jan 11, 2023
1 parent 68c917d commit fa50f8d
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions tests/python/relay/test_op_level5.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,14 @@ def test_crop_and_resize(self, target, dev, executor_kind, layout, interpolate_m

if layout == "NHWC":
img_shape = (10, 224, 224, 3)
boxes = np.array([[0.1, 0.2, 0.8, 0.7], [0.2, 0, 1, 0.6]]).astype("float32")
boxes = np.array([[0.125, 0.25, 0.8125, 0.71875], [0.25, 0, 1, 0.625]]).astype(
"float32"
)
box_indices = np.array([1, 0]).astype("int32")
crop_size = np.array([20, 30]).astype("int32")
elif layout == "NCHW":
img_shape = (5, 3, 255, 255)
boxes = np.array([[0, 0, 1, 1], [0.2, 0.1, 1, 0.9]]).astype("float32")
boxes = np.array([[0, 0, 1, 1], [0.25, 0.125, 1, 0.9375]]).astype("float32")
box_indices = np.array([0, 1]).astype("int32")
crop_size = np.array([30, 30]).astype("int32")
else:
Expand Down

0 comments on commit fa50f8d

Please sign in to comment.