[maskrcnn] use stable sort in mask rcnn caffe2 ops (#70510)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/70510
Pull Request resolved: https://github.com/facebookresearch/detectron2/pull/3838
Pull Request resolved: https://github.com/facebookresearch/mobile-vision/pull/58
Pull Request resolved: https://github.com/fairinternal/detectron2/pull/567
D32694315 changes the implementation of sorting in NMS to stable sort. While the C2 operators are using non-stable sort. This causes test failure such as:
- mobile-vision/d2go/tests:fb_test_meta_arch_rcnn - test_export_caffe2 (d2go.tests.fb.test_meta_arch_rcnn.TestFBNetV2MaskRCNNFP32) (architecture: x86_64, buildmode: dev-nosan, buildsystem: buck, compiler: clang, sanitizer: none) https://www.internalfb.com/intern/testinfra/diagnostics/7318349463675961.562949999530318.1640814509/
- mobile-vision/d2go/tests:fb_test_meta_arch_rcnn - test_export_torchscript_mobile_c2_ops (d2go.tests.fb.test_meta_arch_rcnn.TestFBNetV2MaskRCNNFP32) (architecture: x86_64, buildmode: dev-nosan, buildsystem: buck, compiler: clang, sanitizer: none) https://www.internalfb.com/intern/testinfra/diagnostics/7318349463675961.844424980844724.1640814504/
To illustrate, in the failed test_export_caffe2 test, the inputs of BoxWithNMSLimit are:
```
(Pdb) ws.FetchBlob("246")
array([[0.01234568, 0.01234568, 0.01234568, 0.01234568, 0.01234568,
0.01234568, 0.01234568, 0.01234568, 0.01234568, 0.01234568,
0.01234568, 0.01234568, 0.01234568, 0.01234568, 0.01234568,
0.01234568, 0.01234568, 0.01234568, 0.01234568, 0.01234568,
0.01234568, 0.01234568, 0.01234568, 0.01234568, 0.01234568,
0.01234568, 0.01234568, 0.01234568, 0.01234568, 0.01234568,
0.01234568, 0.01234568, 0.01234568, 0.01234568, 0.01234568,
0.01234568, 0.01234568, 0.01234568, 0.01234568, 0.01234568,
0.01234568, 0.01234568, 0.01234568, 0.01234568, 0.01234568,
0.01234568, 0.01234568, 0.01234568, 0.01234568, 0.01234568,
0.01234568, 0.01234568, 0.01234568, 0.01234568, 0.01234568,
0.01234568, 0.01234568, 0.01234568, 0.01234568, 0.01234568,
0.01234568, 0.01234568, 0.01234568, 0.01234568, 0.01234568,
0.01234568, 0.01234568, 0.01234568, 0.01234568, 0.01234568,
0.01234568, 0.01234568, 0.01234568, 0.01234568, 0.01234568,
0.01234568, 0.01234568, 0.01234568, 0.01234568, 0.01234568,
0.01234568]], dtype=float32)
(Pdb) ws.FetchBlob("248")
array([[ 0., 0., 10., 20., 0., 0., 10., 20., 0., 0., 10., 20., 0.,
0., 10., 20., 0., 0., 10., 20., 0., 0., 10., 20., 0., 0.,
10., 20., 0., 0., 10., 20., 0., 0., 10., 20., 0., 0., 10.,
20., 0., 0., 10., 20., 0., 0., 10., 20., 0., 0., 10., 20.,
0., 0., 10., 20., 0., 0., 10., 20., 0., 0., 10., 20., 0.,
0., 10., 20., 0., 0., 10., 20., 0., 0., 10., 20., 0., 0.,
10., 20., 0., 0., 10., 20., 0., 0., 10., 20., 0., 0., 10.,
20., 0., 0., 10., 20., 0., 0., 10., 20., 0., 0., 10., 20.,
0., 0., 10., 20., 0., 0., 10., 20., 0., 0., 10., 20., 0.,
0., 10., 20., 0., 0., 10., 20., 0., 0., 10., 20., 0., 0.,
10., 20., 0., 0., 10., 20., 0., 0., 10., 20., 0., 0., 10.,
20., 0., 0., 10., 20., 0., 0., 10., 20., 0., 0., 10., 20.,
0., 0., 10., 20., 0., 0., 10., 20., 0., 0., 10., 20., 0.,
0., 10., 20., 0., 0., 10., 20., 0., 0., 10., 20., 0., 0.,
10., 20., 0., 0., 10., 20., 0., 0., 10., 20., 0., 0., 10.,
20., 0., 0., 10., 20., 0., 0., 10., 20., 0., 0., 10., 20.,
0., 0., 10., 20., 0., 0., 10., 20., 0., 0., 10., 20., 0.,
0., 10., 20., 0., 0., 10., 20., 0., 0., 10., 20., 0., 0.,
10., 20., 0., 0., 10., 20., 0., 0., 10., 20., 0., 0., 10.,
20., 0., 0., 10., 20., 0., 0., 10., 20., 0., 0., 10., 20.,
0., 0., 10., 20., 0., 0., 10., 20., 0., 0., 10., 20., 0.,
0., 10., 20., 0., 0., 10., 20., 0., 0., 10., 20., 0., 0.,
10., 20., 0., 0., 10., 20., 0., 0., 10., 20., 0., 0., 10.,
20., 0., 0., 10., 20., 0., 0., 10., 20., 0., 0., 10., 20.,
0., 0., 10., 20., 0., 0., 10., 20.]], dtype=float32)
(Pdb) ws.FetchBlob("249")
array([1.], dtype=float32)
```
This contains 81 boxes (representing 81 classes) with equal score, stable sort will return the class id 0; while the non-stable sort returns class id 50.
This diff changes the sorting to stable sort for BoxWithNMSLimit op.
Test Plan:
The D2 (https://github.com/pytorch/pytorch/commit/401a6b682b920bb3f6ba9b74e8e46d388c9cc5eb)Go's tests can pass after this change.
```
buck test mode/dev-nosan //mobile-vision/d2go/tests:fb_test_meta_arch_rcnn -- --run-disabled
```
https://www.internalfb.com/intern/testinfra/testrun/4785074687594820
Reviewed By: newstzpz
Differential Revision: D33355251
fbshipit-source-id: 9f3fc230b852a5e43f0e3cb8fa9093cbaf53e8b6