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

Roialign fix and half_pixel mode support #3482

Open
wants to merge 61 commits into
base: develop
Choose a base branch
from
Open

Conversation

bpickrel
Copy link
Collaborator

@bpickrel bpickrel commented Sep 26, 2024

Fix bugs in the implementation of ROIAlign operation which were found when attempting to run it with the half_pixel coordinate conversion mode, to include more thorough tests. Some bugs are mode-specific and some are not.

The ROIAlign operation was first proposed in a paper at https://arxiv.org/abs/1703.06870v3 which introduced the Mask R-CNN model. It was a variant of the ROIPool operation which was found to give significantly better accuracy. In the implementations in Torch, Onnxruntime, and Migraphx, ROIPool and ROIAlign are implemented in the same op. with different choices for the mode attribute, with output_half_pixel for ROIPool and half_pixel for ROIAlign; thus, there is no ROIAlign op without fixing the half_pixel mode.

Note, by the way, that these same coordinate conversion modes are also attributes of the Resize op.

MIGraphX uses the Onnxruntime implementation of ROIAlign as its functional specification and should give identical results.

This change is prerequisite for torch-migraphx PR #143 but does not close it.

bpickrel and others added 30 commits July 17, 2024 22:23
…code. Tests need to be completed, including updating generated onnx test files.
…_half_pixel_verify_test for first roi but fails for second
@bpickrel
Copy link
Collaborator Author

bpickrel commented Oct 9, 2024

Reviewers: This PR isn't quite ready for review as I'm still working on the GPU implementation. I'm just opening it to activate the Jenkins testing.

Copy link
Contributor

@spolifroni-amd spolifroni-amd left a comment

Choose a reason for hiding this comment

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

There's a small wording change but it's neither a big change nor a mandatory one.

@bpickrel bpickrel marked this pull request as draft October 10, 2024 21:20
@bpickrel bpickrel marked this pull request as ready for review October 14, 2024 17:15
@bpickrel
Copy link
Collaborator Author

This PR is finally ready to begin review. Note that I left a "todo" about recalculating the indexing order in the Reference op. If we come up with a way to iterate through the array in correct order the first time through, great. But getting the order right to this point (just to obtain correct test values) has been very time-consuming and if a fix doesn't present itself, it may be better to stick with the workaround.

Copy link
Contributor

@shivadbhavsar shivadbhavsar left a comment

Choose a reason for hiding this comment

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

Looks fine, just a few small things. I haven't been able to fully wrap my head around all the math in the ref and gpu impl, the index changes look reasonable. Do we have a way to directly test against ORT (without maunally extracting gold outputs)? If so, I think it would be worthwhile to add a few more tests comparing with ORT

EXPECT(p == prog);

// Opset 12 program
Copy link
Contributor

Choose a reason for hiding this comment

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

I suggest making this a separate test so if there is a failure you know which one of the two opsets had the problem

tools/build_and_test_onnxrt.sh Outdated Show resolved Hide resolved
test/verify/test_roialign_nondefault.cpp Outdated Show resolved Hide resolved
@migraphx-bot
Copy link
Collaborator

Test Batch Rate new
4c4846
Rate old
b73def
Diff Compare
torchvision-resnet50 64 3,260.10 3,257.93 0.07%
torchvision-resnet50_fp16 64 6,997.72 6,992.99 0.07%
torchvision-densenet121 32 2,431.22 2,432.26 -0.04%
torchvision-densenet121_fp16 32 4,069.01 4,038.39 0.76%
torchvision-inceptionv3 32 1,638.16 1,638.89 -0.04%
torchvision-inceptionv3_fp16 32 2,760.41 2,761.69 -0.05%
cadene-inceptionv4 16 775.87 776.39 -0.07%
cadene-resnext64x4 16 808.62 811.37 -0.34%
slim-mobilenet 64 7,530.01 7,532.73 -0.04%
slim-nasnetalarge 64 211.48 211.42 0.03%
slim-resnet50v2 64 3,502.80 3,507.25 -0.13%
bert-mrpc-onnx 8 1,150.20 1,147.76 0.21%
bert-mrpc-tf 1 463.63 469.91 -1.34%
pytorch-examples-wlang-gru 1 421.33 514.96 -18.18% 🔴
pytorch-examples-wlang-lstm 1 382.12 386.61 -1.16%
torchvision-resnet50_1 1 787.04 772.05 1.94%
cadene-dpn92_1 1 417.30 398.73 4.66% 🔆
cadene-resnext101_1 1 382.44 383.67 -0.32%
onnx-taau-downsample 1 343.17 342.33 0.25%
dlrm-criteoterabyte 1 33.33 33.33 -0.01%
dlrm-criteoterabyte_fp16 1 52.73 52.70 0.06%
agentmodel 1 8,040.01 8,056.20 -0.20%
unet_fp16 2 58.84 58.92 -0.14%
resnet50v1_fp16 1 948.15 950.32 -0.23%
resnet50v1_int8 1 979.13 1,000.02 -2.09%
bert_base_cased_fp16 64 1,171.83 1,169.24 0.22%
bert_large_uncased_fp16 32 363.50 363.69 -0.05%
bert_large_fp16 1 200.64 198.89 0.88%
distilgpt2_fp16 16 2,203.66 2,203.09 0.03%
yolov5s 1 544.88 540.85 0.74%
tinyllama 1 43.44 43.43 0.01%
vicuna-fastchat 1 173.75 170.64 1.82%
whisper-tiny-encoder 1 418.08 418.21 -0.03%
whisper-tiny-decoder 1 434.02 426.10 1.86%

This build is not recommended to merge 🔴

@migraphx-bot
Copy link
Collaborator


     ✅ bert-mrpc-onnx: PASSED: MIGraphX meets tolerance

     ✅ bert-mrpc-tf: PASSED: MIGraphX meets tolerance

     ✅ pytorch-examples-wlang-gru: PASSED: MIGraphX meets tolerance

     ✅ pytorch-examples-wlang-lstm: PASSED: MIGraphX meets tolerance

     ✅ torchvision-resnet50_1: PASSED: MIGraphX meets tolerance

     ✅ cadene-dpn92_1: PASSED: MIGraphX meets tolerance

     ✅ cadene-resnext101_1: PASSED: MIGraphX meets tolerance

     ✅ dlrm-criteoterabyte: PASSED: MIGraphX meets tolerance

     ✅ agentmodel: PASSED: MIGraphX meets tolerance

     ✅ unet: PASSED: MIGraphX meets tolerance

     ✅ resnet50v1: PASSED: MIGraphX meets tolerance

     ✅ bert_base_cased_fp16: PASSED: MIGraphX meets tolerance

🔴bert_large_uncased_fp16: FAILED: MIGraphX is not within tolerance - check verbose output


     ✅ bert_large: PASSED: MIGraphX meets tolerance

     ✅ yolov5s: PASSED: MIGraphX meets tolerance

     ✅ tinyllama: PASSED: MIGraphX meets tolerance

     ✅ vicuna-fastchat: PASSED: MIGraphX meets tolerance

     ✅ whisper-tiny-encoder: PASSED: MIGraphX meets tolerance

     ✅ whisper-tiny-decoder: PASSED: MIGraphX meets tolerance

     ✅ distilgpt2_fp16: PASSED: MIGraphX meets tolerance

mm->add_return({r});

return p;
}
};

template struct test_roialign_half_pixel<migraphx::shape::float_type>;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Is there any point in adding other data types for the half_pixel (i.e. the "real" ROIAlign) test?


pp["rois"] = migraphx::argument(srois, rois_data.data());
pp["batch_ind"] = migraphx::argument(sbi, bi_data.data());
pp["y"] = migraphx::argument(s, data.data());
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's not clear to me whether Y, the output, is even needed for this test. What role does it play?

Copy link
Contributor

Choose a reason for hiding this comment

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

Likely because roialign_half_pixel_test.onnx expects it as input. I am not sure why its configured like that. Maybe it needs an allocated buffer to write the output to?
I am not very familiar with the verify onnx tests, I would check if this is common in other tests also

@@ -41,7 +41,7 @@ TEST_CASE(roialign_test)
{{"coordinate_transformation_mode", "output_half_pixel"},
{"spatial_scale", 2.0f},
{"output_height", 5},
{"output_width", 5},
{"output_width", 3},
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This particular change was a big deal, btw, since the old code appeared to work fine until I gave it an output_height and output_width that were not the same.

migraphx::shape srois{migraphx::shape::float_type, {2, 4}};
std::vector<float> rois_data = {1.1, 0.73, 1.7, 1.13, 1.1, 0.73, 2.6, 1.13};
migraphx::shape sbi{migraphx::shape::int64_type, {2}}; // batch_index
std::vector<int64_t> bi_data = {0, 1};
Copy link
Contributor

Choose a reason for hiding this comment

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

for good measure, can you add a tests with the following cases:

  • Repeated batch indices
  • Missing batch indices (ie. not all batch items are computed on)
  • Number of ROIs != batch_size

You can probably just create one test case to get all these. Make the input batch_size 3 and the batch_indices something like {1,2,2,1} (and hence the rois shape will be {4,4})
Would be good to have a gpu verify test for this same case too just to be sure gpu impl matches

Copy link
Contributor

@spolifroni-amd spolifroni-amd left a comment

Choose a reason for hiding this comment

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

A whitespace in the table needs to be removed.

@@ -697,7 +697,7 @@ Operator Support Matrix
| | | | functions are |
| | | | not enabled |
+--------------------------+-----------+-----------------+------------------------------+
| RoiAlign || FP8, FP16, | |
| RoiAlign || FP8, FP16, | |
Copy link
Contributor

Choose a reason for hiding this comment

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

The extra whitespace at the end of this row is causing the table to be improperly formatted and not appear on the doc page.

Suggested change
| RoiAlign | ✅ | FP8, FP16, | |
| RoiAlign | ✅ | FP8, FP16, | |

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.

5 participants