Skip to content

[Bug] python.contrib.test_onnx.test_resize numerical accuracy issue #12567

Description

@leandron

While working to enable Integration tests in AArch64 with ONNX and Torch installed (which is currently a blind spot in our CI, see #12529), I'm seeing python.contrib.test_onnx.test_resize failing with an accuracy error. According to some tests locally, the issue reproduces consistently in x86 and AArch64.

I'm creating this issue so that we can investigate the numerical accuracy issue separately, but for now, I'm marking this test as an xfail.

Steps to reproduce:

  1. Starting from a ci_cpu image, run the ONNX installation script (https://github.com/apache/tvm/blob/989e5a11285503716c2033f4e56f1bba6b6d00c7/docker/install/ubuntu_install_onnx.sh)
  2. Run the test python.contrib.test_onnx.test_resize

This is the error message I see:

def test_resize():
        """Resize unit test."""
    
        def verify_resize(dshape, outsize, method, coord_trans, rounding_method, dtype="float32"):
            x = relay.var("x", relay.ty.TensorType(dshape, dtype))
            y = relay.image.resize2d(
                x,
                outsize,
                None,
                layout="NCHW",
                method=method,
                coordinate_transformation_mode=coord_trans,
                rounding_method=rounding_method,
            )
            func = relay.Function([x], y)
            x_data = np.random.uniform(size=dshape).astype(dtype)
            verify_results(func, [x_data], "test_resize", rtol=1e-4, atol=1e-4)
    
        method = ["nearest_neighbor", "linear", "cubic"]
        coord_trans = ["half_pixel", "align_corners", "asymmetric"]
        rounding_method = ["round", "floor", "ceil"]
    
        isize = (1, 3, 480, 640)
    
        # Downsample
        osize = (240, 320)
        for i in method:
            for j in coord_trans:
                for k in rounding_method:
                    if (i == "nearest_neighbor" and j == "align_corners") or (
                        i == "cubic" and j in ["half_pixel", "align_corners"]
                    ):
                        continue
                    verify_resize(isize, osize, method=i, coord_trans=j, rounding_method=k)
    
        # Upsample
        osize = (960, 1280)
        for i in method:
            for j in coord_trans:
                for k in rounding_method:
                    if (i == "nearest_neighbor" and j == "align_corners") or (i == "cubic"):
                        continue
>                   verify_resize(isize, osize, method=i, coord_trans=j, rounding_method=k)

tests/python/contrib/test_onnx.py:700: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
tests/python/contrib/test_onnx.py:674: in verify_resize
    verify_results(func, [x_data], "test_resize", rtol=1e-4, atol=1e-4)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

relay_func = fn (%x: Tensor[(1, 3, 480, 640), float32]) {
  image.resize2d(%x, size=[960, 1280], roi=[0f, 0f, 0f, 0f], method="nearest_neighbor", coordinate_transformation_mode="asymmetric")
}
indata = [array([[[[0.07992046, 0.10136255, 0.00943371, ..., 0.6083984 ,
          0.80598825, 0.8000824 ],
         [0.6850799...],
         [0.47949922, 0.5546733 , 0.77905625, ..., 0.5419391 ,
          0.09121019, 0.5444949 ]]]], dtype=float32)]
test_name = 'test_resize', rtol = 0.0001, atol = 0.0001, is_dyn = False

    def verify_results(relay_func, indata, test_name, rtol=1e-7, atol=0, is_dyn=False):
        relay_results = run_relay(relay_func, indata, is_dyn)
        onnx_results = run_onnx(func_to_onnx(relay_func, test_name), indata)
    
        for relay_res, onnx_res in zip(relay_results, onnx_results):
>           np.testing.assert_allclose(relay_res, onnx_res, rtol=rtol, atol=atol)
E           AssertionError: 
E           Not equal to tolerance rtol=0.0001, atol=0.0001
E           
E           Mismatched elements: 2760564 / 3686400 (74.9%)
E           Max absolute difference: 0.99967563
E           Max relative difference: 5434130.5
E            x: array([[[[0.07992 , 0.07992 , 0.101363, ..., 0.805988, 0.800082,
E                     0.800082],
E                    [0.07992 , 0.07992 , 0.101363, ..., 0.805988, 0.800082,...
E            y: array([[[[0.07992 , 0.101363, 0.101363, ..., 0.800082, 0.800082,
E                     0.800082],
E                    [0.68508 , 0.172607, 0.172607, ..., 0.060676, 0.060676,...

tests/python/contrib/test_onnx.py:69: AssertionError

cc @ashutosh-arm

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions