Repository: pytorch
Issue: torch.jit.save gives error - RuntimeError: Could not export Python function call 'NMSop'. Remove calls to Python functions before export. Did you forget to add @script or @script_method annotation? If this is a nn.ModuleList, add it to __constants__
Labels: oncall: jit

## 🐛 Bug

Model trace works fine but when saving the traced model, it gives an error

## To Reproduce

Steps to reproduce the behavior:

I trained a mmdetection model and for deployment I need to trace the model and then save the traced model.  When saving the traced model, it gives an error.
Issue on mmdetection [repo](https://github.com/open-mmlab/mmdetection/issues/5381)
```
with open('work_dirs/torchscript_artificant.pth', 'wb') as f:
    torch.jit.save(model, f)
```

<!-- If you have a code sample, error messages, stack traces, please provide it here as well -->

Error traceback
If applicable, paste the error trackback here.

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-16-13663be733d8> in <module>()
      1 with open('work_dirs/torchscript_artificant.pth', 'wb') as f:
----> 2     torch.jit.save(model, f)

/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/torch/jit/_serialization.py in save(m, f, _extra_files)
     81         m.save(f, _extra_files=_extra_files)
     82     else:
---> 83         ret = m.save_to_buffer(_extra_files=_extra_files)
     84         f.write(ret)
     85 

/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/torch/jit/_script.py in save_to_buffer(self, *args, **kwargs)
    505 
    506         def save_to_buffer(self, *args, **kwargs):
--> 507             return self._c.save_to_buffer(*args, **kwargs)
    508 
    509         def get_debug_state(self, *args, **kwargs):

RuntimeError: 
Could not export Python function call 'NMSop'. Remove calls to Python functions before export. Did you forget to add @script or @script_method annotation? If this is a nn.ModuleList, add it to __constants__:
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/mmcv/ops/nms.py(144): nms
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/mmcv/utils/misc.py(310): new_func
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/mmcv/ops/nms.py(279): batched_nms
/home/ubuntu/clip/mmdetection/mmdet/models/dense_heads/rpn_head.py(234): _get_bboxes
/home/ubuntu/clip/mmdetection/mmdet/models/dense_heads/anchor_head.py(585): get_bboxes
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/mmcv/runner/fp16_utils.py(182): new_func
/home/ubuntu/clip/mmdetection/mmdet/models/dense_heads/rpn_test_mixin.py(36): simple_test_rpn
/home/ubuntu/clip/mmdetection/mmdet/models/detectors/two_stage.py(200): simple_test
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/torch/nn/modules/module.py(709): _slow_forward
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/torch/nn/modules/module.py(725): _call_impl
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/torch/jit/_trace.py(940): trace_module
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/torch/jit/_trace.py(742): trace
<ipython-input-15-f13edadbd344>(1): <module>
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/IPython/core/interactiveshell.py(2878): run_code
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/IPython/core/interactiveshell.py(2818): run_ast_nodes
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/IPython/core/interactiveshell.py(2714): run_cell
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/ipykernel/zmqshell.py(539): run_cell
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/ipykernel/ipkernel.py(302): do_execute
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/tornado/gen.py(162): _fake_ctx_run
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/tornado/gen.py(234): wrapper
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/ipykernel/kernelbase.py(538): execute_request
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/tornado/gen.py(162): _fake_ctx_run
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/tornado/gen.py(234): wrapper
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/ipykernel/kernelbase.py(261): dispatch_shell
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/tornado/gen.py(162): _fake_ctx_run
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/tornado/gen.py(234): wrapper
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/ipykernel/kernelbase.py(358): process_one
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/tornado/gen.py(775): run
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/tornado/gen.py(162): _fake_ctx_run
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/tornado/gen.py(814): inner
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/tornado/ioloop.py(741): _run_callback
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/tornado/ioloop.py(688): <lambda>
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/asyncio/events.py(145): _run
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/asyncio/base_events.py(1434): _run_once
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/asyncio/base_events.py(422): run_forever
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/tornado/platform/asyncio.py(199): start
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/ipykernel/kernelapp.py(612): start
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/traitlets/config/application.py(664): launch_instance
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/site-packages/ipykernel_launcher.py(16): <module>
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/runpy.py(85): _run_code
/home/ubuntu/anaconda3/envs/clip/lib/python3.6/runpy.py(193): _run_module_as_main

## Expected behavior

Saved artifact

## Environment

Please copy and paste the output from our
[environment collection script](https://raw.githubusercontent.com/pytorch/pytorch/master/torch/utils/collect_env.py)
(or fill out the checklist below manually).

You can get the script and run it with:
```
GPU 0: Tesla K80
CUDA_HOME: /usr/local/cuda
NVCC: Build cuda_11.0_bu.TC445_37.28845127_0
GCC: gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0 
PyTorch: 1.7.1
PyTorch compiling details: PyTorch built with:
  - GCC 7.3
  - C++ Version: 201402
  - Intel(R) Math Kernel Library Version 2020.0.2 Product Build 20200624 for Intel(R) 64 architecture applications
  - Intel(R) MKL-DNN v1.6.0 (Git Hash 5ef631a030a6f73131c77892041042805a06064f)
  - OpenMP 201511 (a.k.a. OpenMP 4.5)
  - NNPACK is enabled
  - CPU capability usage: AVX2
  - CUDA Runtime 11.0
  - NVCC architecture flags: -gencode;arch=compute_37,code=sm_37;-gencode;arch=compute_50,code=sm_50;-gencode;arch=compute_60,code=sm_60;-gencode;arch=compute_61,code=sm_61;-gencode;arch=compute_70,code=sm_70;-gencode;arch=compute_75,code=sm_75;-gencode;arch=compute_80,code=sm_80;-gencode;arch=compute_37,code=compute_37
  - CuDNN 8.0.5
  - Magma 2.5.2
  - Build settings: BLAS=MKL, BUILD_TYPE=Release, CXX_FLAGS= -Wno-deprecated -fvisibility-inlines-hidden -DUSE_PTHREADPOOL -fopenmp -DNDEBUG -DUSE_FBGEMM -DUSE_QNNPACK -DUSE_PYTORCH_QNNPACK -DUSE_XNNPACK -DUSE_VULKAN_WRAPPER -O2 -fPIC -Wno-narrowing -Wall -Wextra -Werror=return-type -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-unused-result -Wno-unused-local-typedefs -Wno-strict-overflow -Wno-strict-aliasing -Wno-error=deprecated-declarations -Wno-stringop-overflow -Wno-psabi -Wno-error=pedantic -Wno-error=redundant-decls -Wno-error=old-style-cast -fdiagnostics-color=always -faligned-new -Wno-unused-but-set-variable -Wno-maybe-uninitialized -fno-math-errno -fno-trapping-math -Werror=format -Wno-stringop-overflow, PERF_WITH_AVX=1, PERF_WITH_AVX2=1, PERF_WITH_AVX512=1, USE_CUDA=ON, USE_EXCEPTION_PTR=1, USE_GFLAGS=OFF, USE_GLOG=OFF, USE_MKL=ON, USE_MKLDNN=ON, USE_MPI=OFF, USE_NCCL=ON, USE_NNPACK=ON, USE_OPENMP=ON,

TorchVision: 0.8.2
OpenCV: 4.5.2
MMCV: 1.2.7
MMCV Compiler: GCC 7.5
MMCV CUDA Compiler: 11.0
MMDetection: 2.11.0+ca95937

```

 - PyTorch Version (e.g., 1.0):
 - OS (e.g., Linux):
 - How you installed PyTorch (`conda`, `pip`, source):
 - Build command you used (if compiling from source):
 - Python version:
 - CUDA/cuDNN version:
 - GPU models and configuration:
 - Any other relevant information:

## Additional context

<!-- Add any other context about the problem here. -->


cc @gmagogsfm