mmdetection
mmdetection copied to clipboard
[Bug] use ASP from apex.contrib.sparsity to sparsify the model
Prerequisite
- [X] I have searched Issues and Discussions but cannot get the expected help.
- [X] I have read the FAQ documentation but cannot get the expected help.
- [X] The bug has not been fixed in the latest version (master) or latest version (3.x).
Task
I have modified the scripts/configs, or I'm working on my own tasks/models/datasets.
Branch
master branch https://github.com/open-mmlab/mmdetection
Environment
sys.platform: linux Python: 3.8.12 (default, Oct 12 2021, 13:49:34) [GCC 7.5.0] CUDA available: True GPU 0: NVIDIA GeForce RTX 3090 CUDA_HOME: /usr/local/cuda NVCC: Build cuda_11.2.r11.2/compiler.29618528_0 GCC: gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0 PyTorch: 1.10.0+cu113 PyTorch compiling details: PyTorch built with:
- GCC 7.3
- C++ Version: 201402
- Intel(R) Math Kernel Library Version 2020.0.0 Product Build 20191122 for Intel(R) 64 architecture applications
- Intel(R) MKL-DNN v2.2.3 (Git Hash 7336ca9f055cf1bfa13efb658fe15dc9b41f0740)
- OpenMP 201511 (a.k.a. OpenMP 4.5)
- LAPACK is enabled (usually provided by MKL)
- NNPACK is enabled
- CPU capability usage: AVX2
- CUDA Runtime 11.3
- 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_70,code=sm_70;-gencode;arch=compute_75,code=sm_75;-gencode;arch=compute_80,code=sm_80;-gencode;arch=compute_86,code=sm_86
- CuDNN 8.2
- Magma 2.5.2
- Build settings: BLAS_INFO=mkl, BUILD_TYPE=Release, CUDA_VERSION=11.3, CUDNN_VERSION=8.2.0, CXX_COMPILER=/opt/rh/devtoolset-7/root/usr/bin/c++, CXX_FLAGS= -Wno-deprecated -fvisibility-inlines-hidden -DUSE_PTHREADPOOL -fopenmp -DNDEBUG -DUSE_KINETO -DUSE_FBGEMM -DUSE_QNNPACK -DUSE_PYTORCH_QNNPACK -DUSE_XNNPACK -DSYMBOLICATE_MOBILE_DEBUG_HANDLE -DEDGE_PROFILER_USE_KINETO -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, LAPACK_INFO=mkl, PERF_WITH_AVX=1, PERF_WITH_AVX2=1, PERF_WITH_AVX512=1, TORCH_VERSION=1.10.0, USE_CUDA=ON, USE_CUDNN=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.11.0+cu113 OpenCV: 4.7.0 MMCV: 1.4.0 MMCV Compiler: GCC 7.3 MMCV CUDA Compiler: 11.3 MMDetection: 2.28.1+c14dd6c
Reproduces the problem - code sample
The train_sparsify.py is exactly the same as train.py file in tools, adding a few lines i mmdetection/mmdet/api/train.py in the next section.
python tools/train_sparsify.py /vol_fast/pytorch_quantization/mmdetection/configs/yolox/yolox_s_8x8_300e_coco.py --work-dir /vol_fast/pytorch_quantization/mmdetection/yolox_s_prune --resume-from /vol_fast/pytorch_quantization/mmdetection/yolox_s_8x8_300e_coco_20211121_095711-4592a793.pth --gpu-id 0
Reproduces the problem - command or script
In mmdetection/mmdet/apis/train.py, starting around line 168, after optimizer = build_optimizer(model, cfg.optimizer)
, add:
# import apex sparsity
try:
from apex.contrib.sparsity import ASP
except ImportError:
raise RuntimeError("Failed to iimport ASP, Please install Apex")
sparse_flag = 1
if sparse_flag:
print("*************** preparing for sparsifying ***************")
ASP.prune_trained_model(model, optimizer)
Then run the command above
Reproduces the problem - error message
[print_raw_fx_graph] Meet the fatal fault when trying to symbolic trace the model with Torch.FX
Traceback (most recent call last):
File "tools/train_sparsify.py", line 252, in <module>
main()
File "tools/train_sparsify.py", line 241, in main
train_detector(
File "/vol_fast/pytorch_quantization/mmdetection/mmdet/apis/train.py", line 179, in train_detector
ASP.prune_trained_model(model, optimizer)
File "/opt/conda/envs/mmdeploy/lib/python3.8/site-packages/apex/contrib/sparsity/asp.py", line 303, in prune_trained_model
cls.compute_sparse_masks()
File "/opt/conda/envs/mmdeploy/lib/python3.8/site-packages/apex/contrib/sparsity/asp.py", line 224, in compute_sparse_masks
offline_permutation_fx_graph, success_in_build_offline_permutation_graph = Permutation.build_offline_permutation_graph(cls.__model.module, dump_fx_graph=cls.__save_permutation_graph, save_dumped_fx_graph=os.path.join(cls.__permutation_output_dir, 'model_offline_permutation_graph.json'))
File "/opt/conda/envs/mmdeploy/lib/python3.8/site-packages/apex/contrib/sparsity/permutation_lib.py", line 241, in build_offline_permutation_graph
fx_graph, success_in_build_fx_graph = cls.build_fx_graph(model, dump_fx_graph=dump_fx_graph, save_dumped_fx_graph=save_dumped_fx_graph)
File "/opt/conda/envs/mmdeploy/lib/python3.8/site-packages/apex/contrib/sparsity/permutation_lib.py", line 836, in build_fx_graph
graph_module = cls.print_raw_fx_graph(model, print_tabular=True)
File "/opt/conda/envs/mmdeploy/lib/python3.8/site-packages/apex/contrib/sparsity/permutation_lib.py", line 896, in print_raw_fx_graph
symbolic_traced : torch.fx.GraphModule = symbolic_trace(model)
File "/opt/conda/envs/mmdeploy/lib/python3.8/site-packages/torch/fx/_symbolic_trace.py", line 907, in symbolic_trace
graph = tracer.trace(root, concrete_args)
File "/opt/conda/envs/mmdeploy/lib/python3.8/site-packages/torch/fx/_symbolic_trace.py", line 615, in trace
self.create_node('output', 'output', (self.create_arg(fn(*args)),), {},
File "/opt/conda/envs/mmdeploy/lib/python3.8/site-packages/mmcv/runner/fp16_utils.py", line 94, in new_func
if not isinstance(args[0], torch.nn.Module):
TypeError: 'YOLOX' object is not subscriptable
Additional information
Maybe the model I put into is wrong? the fx can't trace the model
MMDetection 2.x does not support fx tracing. We supported this feature for single-stage model in the 3.x version. Please try the 3.x branch https://github.com/open-mmlab/mmdetection/tree/3.x .
MMDetection 2.x does not support fx tracing. We supported this feature for single-stage model in the 3.x version. Please try the 3.x branch https://github.com/open-mmlab/mmdetection/tree/3.x .
what's the root cause of MMDetection 2.x not supporting fx tracing
I am able to trace the model's backbone, neck, and head separately, but fails when attempting to trace the whole model in MMDet 2.28. I mean why TypeError: 'YOLOX' object is not subscriptable
occurs. Any insights into the root cause would be greatly appreciated. Thanks in advance!