torch-mlir icon indicating copy to clipboard operation
torch-mlir copied to clipboard

[ONNX] Add support for onnx.Resize with nearest_mode "round_prefer_floor" and "round_prefer_ceil"

Open flemairen6 opened this issue 1 year ago • 6 comments

Currently the torchOnnxToTorch pass seems to only support nearest mode with floor.

flemairen6 avatar Jun 05 '24 14:06 flemairen6

Assigned to @aldesilv

vivekkhandelwal1 avatar Jun 05 '24 16:06 vivekkhandelwal1

Thanks for working on it. What about ceil mode?

ttjost avatar Jun 06 '24 08:06 ttjost

https://github.com/llvm/torch-mlir/pull/3421

aldesilv avatar Jun 07 '24 18:06 aldesilv

The referenced PR just got merged. Here is a status update, so please let us know if any of the currently unsupported features are needed by your team. @flemairen6

I'm going to do some E2E testing and put up a report today to see if we are running into any numerical issues with the supported configurations below.

Resize Status:

Currently Supported:

  • [X] mode == "nearest": &: nearest_mode is one of "floor", "ceil", "round_prefer_floor", "round_prefer_ceil" &: coordinate_transformation_mode == "asymmetric" &: 1,2, or 3 spatial dims

  • [X] mode == "linear": &: coordinate_transformation_mode is one of "align_corners", "asymmetric", "pytorch_half_pixel", "half_pixel" &: 2 spatial dims.

Currently Unsupported:

  • [ ] coordinate_transformation_mode == "tf_crop_and_resize"
  • [ ] coordinate_transformation_mode == "half_pixel_symmetric". Note: It looks like we aren't producing a match failure in this case, so we should add support for mode = "linear" and coordTfMode = "half_pixel_symmetric" as soon as possible. @aldesilv Do you want to take a look? Edit: see comment below.
  • [ ] mode = "linear" with 1 or 3 spatial dims.
  • [ ] mode = "nearest" with coordinate_transformation_mode != "asymmetric".

zjgarvey avatar Jun 07 '24 19:06 zjgarvey

A few notes after doing some e2e testing via the iree_tests:

  1. all nearest_modes that are ceil-related have some issues along the back-edge where ceil would result in an index > (input_size - 1). E.G.:
_______________________________________________________________________________________________ IREE compile and run: test_resize_upsample_sizes_nearest_round_prefer_ceil_asymmetric::cpu_llvm_sync_test ________________________________________________________________________________________________
Error invoking iree-run-module
Error code: 1
Stderr diagnostics:

Stdout diagnostics:
EXEC @test_resize_upsample_sizes_nearest_round_prefer_ceil_asymmetric
[FAILED] result[0]: element at index 7 (5) does not match the expected (4); expected that the view is equal to contents of a view of 1x1x8x8xf32
  expected:
1x1x8x8xf32=[[[1 2 2 3 3 4 4 4][5 6 6 7 7 8 8 8][5 6 6 7 7 8 8 8][9 10 10 11 11 12 12 12][9 10 10 11 11 12 12 12][13 14 14 15 15 16 16 16][13 14 14 15 15 16 16 16][13 14 14 15 15 16 16 16]]]
  actual:
1x1x8x8xf32=[[[1 2 2 3 3 4 4 5][5 6 6 7 7 8 8 9][5 6 6 7 7 8 8 9][9 10 10 11 11 12 12 13][9 10 10 11 11 12 12 13][13 14 14 15 15 16 16 0][13 14 14 15 15 16 16 0][0 0 0 1.35926E-43 1.35926E-43 0 0 1.4013E-45]]]

Compiled with:
  cd /home/zjgar/code/SHARK-TestSuite/iree_tests/onnx/node/generated/test_resize_upsample_sizes_nearest_round_prefer_ceil_asymmetric && iree-compile model.mlir --iree-hal-target-backends=llvm-cpu -o model_cpu_llvm_sync_test.vmfb

Run with:
  cd /home/zjgar/code/SHARK-TestSuite/iree_tests/onnx/node/generated/test_resize_upsample_sizes_nearest_round_prefer_ceil_asymmetric && iree-run-module --module=model_cpu_llvm_sync_test.vmfb --device=local-sync --flagfile=test_data_flags.txt
  1. there seems to be some numerical error with a linear test using align_corners:
______________________________________________________________________________________________________ IREE compile and run: test_resize_downsample_scales_linear_align_corners::cpu_llvm_sync_test ______________________________________________________________________________________________________
Error invoking iree-run-module
Error code: 1
Stderr diagnostics:

Stdout diagnostics:
EXEC @test_resize_downsample_scales_linear_align_corners
[FAILED] result[0]: element at index 1 (4) does not match the expected (3.14286); expected that the view is equal to contents of a view of 1x1x1x2xf32
  expected:
1x1x1x2xf32=[[[1 3.14286]]]
  actual:
1x1x1x2xf32=[[[1 4]]]

Compiled with:
  cd /home/zjgar/code/SHARK-TestSuite/iree_tests/onnx/node/generated/test_resize_downsample_scales_linear_align_corners && iree-compile model.mlir --iree-hal-target-backends=llvm-cpu -o model_cpu_llvm_sync_test.vmfb

Run with:
  cd /home/zjgar/code/SHARK-TestSuite/iree_tests/onnx/node/generated/test_resize_downsample_scales_linear_align_corners && iree-run-module --module=model_cpu_llvm_sync_test.vmfb --device=local-sync --flagfile=test_data_flags.txt
  1. coordTfMode half_pixel_symmetric indeed causes a crash, so this should be fixed.

zjgarvey avatar Jun 08 '24 01:06 zjgarvey

The referenced PR #3443 addresses these two issues:

  1. all nearest_modes that are ceil-related have some issues along the back-edge where ceil would result in an index > (input_size - 1). E.G.:
  1. coordTfMode half_pixel_symmetric indeed causes a crash, so this should be fixed.

I'm still looking into the align_corners issue, since it seems like our calculation "does what it should" in this case. I'll try doing some more testing to confirm whether this test is giving the correct golden output.

Other updates:

#3441 Adds support for nearest mode with coordinate_transformation_mode = half_pixel (thanks @mgehre !)

zjgarvey avatar Jun 10 '24 15:06 zjgarvey