quic / aimet

AIMET is a library that provides advanced quantization and compression techniques for trained neural network models.
https://quic.github.io/aimet-pages/index.html
Other
2.09k stars 374 forks source link

yolov5 pytorch models issue #1625

Open Sanath1998 opened 1 year ago

Sanath1998 commented 1 year ago

Hi team,

Actually, I had trained yolov5 for custom object detection model and I had carried out compression techniques and it worked for me. But quantization on yolov5 models is throwing error ---------------------- ERROR LOG ---------------------- raise TraceError('symbolically traced variables cannot be used as inputs to control flow') torch.fx.proxy.TraceError: symbolically traced variables cannot be used as inputs to control flow

This is happening when I'am preparing the yolov5 model for Quantization Simulation.

Could you please tell if yolov5 models are supported for AIMET quantization or is there any specific reason for such kind of errors.

quic-akhobare commented 1 year ago

Hi @quic-hitameht - Could you please give some examples of how we got symbolic tracing to work for Yolo v5. Just a couple examples might be sufficient.

Sanath1998 commented 1 year ago

Hi @quic-hitameht - Could you please give some examples of how we got symbolic tracing to work for Yolo v5. Just a couple examples might be sufficient.

Yes that will be more beneficial for me to get hands on those models. Or is there any tutorial for doing the customization on yolov5 models itself.

quic-hitameht commented 1 year ago

Hi @Sanath1998 Regarding the traceback you shared, there are few things AIMET model preparer API (and torch FX)can not handle today and user should slightly refactor the model definition if the model has following:

1) Dynamic control flow – If model definition has loops or if-else where logic depends on the input of your model. 2) If the model has non-torch functions (such as python built-in function "len" for example) 3) Tensor constructors (e.g. torch.zeros, torch.arange etc) arguments should not be inferred dynamically.

Workaround for such cases are: 1) Factor out the not traceable logic into a function and wrap using torch.fx.wrap API. You can check this example here: https://github.com/quic/aimet/blob/develop/TrainingExtensions/torch/test/python/test_model_preparer.py 2) Convert dynamic control flow into static control flow. This is possible almost all the time and requires minimal refactoring. Take a look at an example : https://pytorch.org/docs/stable/fx.html 3) Deterministic constructor (torch.zeros, torch.arange) should be used so that the value will be embedded in the trace as constant.

Limitation of torch FX can be found here: https://pytorch.org/docs/stable/fx.html#limitations-of-symbolic-tracing

Sri20021 commented 1 year ago

hi @quic-hitameht / @quic-akhobare

Is it possible to share any tutorial on how to handle these issues for yolo model. I am planning to perform QAT for custom object detector using yolov7 architecture. Thanks