FX mode static_quantization for YOLOv7

I use the FX_mode post-training-static quantization on YOLOv7,
but I always see the error about the control-flow (which is happend at yolov7/models/yolo.py , that function is fuseforward(), and I don’t know how to solve it)

you can see the code below:

My pytorch version is 2.1.0, and I use the code in pytorch 2.1.0 document. to do quantization
Please help me to solve it, Thank you!

that’s the main issue with fx quantization, you’re model needs to be symbolically traceable. You’ll need to get rid of the control flow or try some other quantization method.

This error is documented in the quantization docs here: Quantization — PyTorch 2.2 documentation with links to additional resources for making models symbolically traceable.

I have corrected this error by modified the model’s code.
But I have encountered the “precision loss” after I using the Post-traing FX-mode static quantization during my inference job.
May I ask you “how to find the problem” or “where to modified” that I can continue my quantization job?

here is a guide for numerical debugging: Quantization Accuracy Debugging — PyTorch 2.2 documentation, but we may have updated tools later

Does it work when I am using pytorch 2.1.0?

yeah it should work on 2.1

Thx a lot!!
I am not sure about the fourth advice that is " consider QAT when trying PTQ", that means I need to use QAT(on my training step ) and then implement PTQ? or after I implement PTQ, then I use QAT just like finetuning?

Hi, I am trying using my “static_quantize_weights.pt” to do QAT(or you can call finetuning);
But it come across the problem that quantized-weight doesn’t have the same key compare with the original model.

the ERROR message is like below:

Traceback (most recent call last):
File “Static_quantize5.py”, line 200, in
train(hyp, opt = opt,device = torch.device(‘cpu’), tb_writer = tb_writer)
File “/home/gene/YuRen/yolov7/train.py”, line 102, in train
state_dict = ckpt[‘model’].float().state_dict() # to FP32
KeyError: ‘model’

How could I do next?