The ONNX exported by my QAT quantization training does not have a fake operator, the code is as follows?

import torch
from thop import profile
import torchvision.models as models
import copy
from torch.quantization.quantize_fx import prepare_qat_fx,convert_fx,fuse_fx
import torch.nn as nn

resnet18_model = models.resnet34(pretrained=True)
tensorrt_qconfig = torch.ao.quantization.QConfig(
activation= torch.ao.quantization.FakeQuantize.with_args(observer= torch.ao.quantization.MinMaxObserver,
dtype=torch.qint8, qscheme=torch.per_tensor_symmetric, reduce_range=False),
weight= torch.ao.quantization.FakeQuantize.with_args(observer= torch.ao.quantization.PerChannelMinMaxObserver,
dtype=torch.qint8, qscheme=torch.per_channel_symmetric, reduce_range=False),
)
def qat_version_model(model):

# print(tensorrt_qconfig )

qconfig_dict = {
    # Global Config
    "":  tensorrt_qconfig ,

    # # Disable by layer-name
    # "module_name": [(m, None) for m in disable_layers],

    # Or disable by layer-type
}
example_inputs = (torch.randn(1, 3, 32, 32),)#通过一个例子获取graph 的实例
model_to_quantize = copy.deepcopy(model)

model_fp32_prepared = prepare_qat_fx(
    model_to_quantize, qconfig_dict,    example_inputs )
return model_fp32_prepared

qat_model=qat_version_model(resnet18_model)
model=convert_fx(qat_model)

input1=torch.ones(1, 3, 32, 32)
torch.onnx.export(model, (input1), “quantized_model.onnx”,
input_names=[‘input1’], output_names=[‘result’],
opset_version=13, export_params=True)

There are no QDQ operators in the exported onnx file,how to solve?

you don’t need to call convert_fx if you want to get a QAT model for training, but I don’t think you can export the QAT model to onnx though

Why can’t ONNX export QAT models? Except for the problem there?thank

I improved the code based on the official example, the code is as follows:

import torch
from thop import profile
import torchvision.models as models
import copy

from torch.ao.quantization.quantize_fx import (
convert_to_reference_fx,
prepare_fx,
prepare_qat_fx,
)
import torch.nn as nn
from torch.quantization.quantize_fx import prepare_qat_fx,convert_fx, prepare_fx

from torch.ao.quantization.backend_config import (
get_tensorrt_backend_config_dict,
ObservationType,
)

resnet18_model = models.resnet34(pretrained=False)
from torch.testing._internal.common_quantization import (
NodeSpec as ns,
QuantizationTestCase,
)

prepare = prepare_qat_fx
trt_qconfig = torch.ao.quantization.QConfig(
activation=torch.ao.quantization.observer.HistogramObserver.with_args(
qscheme=torch.per_tensor_symmetric, dtype=torch.qint8
),
weight=torch.ao.quantization.default_weight_observer,
)
example_inputs = (torch.randn(1, 3, 32, 32),)#
trt_backend_config_dict = get_tensorrt_backend_config_dict()
prepared = prepare(
resnet18_model ,
{“”: trt_qconfig},
example_inputs,
backend_config=trt_backend_config_dict,
)

quantized = convert_to_reference_fx(
prepared,
backend_config=trt_backend_config_dict,
)

qq_model=convert_fx(quantized)

a_test=QuantizationTestCase()

input1=torch.ones(1, 3, 32, 32)
a_test.checkGraphModuleNodes(quantized)

print(quantized)

torch.onnx.export(quantized , (input1), “quantized_model.onnx”,
input_names=[‘input1’], output_names=[‘result’],
opset_version=11, operator_export_type=torch.onnx.OperatorExportTypes.ONNX_ATEN_FALLBACK)

There was a problem exporting ONNX files:AttributeError: ‘torch.qscheme’ object has no attribute ‘detach’

ONNX path is not officially supported by us, so you might need to open an issue and tag onnx people, see: Quantization — PyTorch main documentation