Hi, I tried to use the torch.fx
based method to run static post-training quantization on a ResNet-18 model, then use torch.jit.trace
to produce a TorchScript model. However, I was unable to import the TorchScript model into TVM using the PyTorch frontend, here’s the error:
Traceback (most recent call last):
...
File ".../test_pytorch_quant.py", line 28, in <module>
mod, params = relay.frontend.from_pytorch(
File ".../tvm/python/tvm/relay/frontend/pytorch.py", line 3963, in from_pytorch
input_scales_for_bias = qnn_torch.add_input_quant_params_to_op_inputs(graph)
File ".../tvm/relay/frontend/qnn_torch.py", line 493, in add_input_quant_params_to_op_inputs
input_scales_for_bias[node.inputsAt(1).debugName()] = scale.node().f("value")
RuntimeError: required keyword attribute 'value' is undefined
This is my test code:
import torch
from torchvision.models.resnet import resnet18
from torch.quantization import quantize_fx
import tvm
from tvm import relay
if __name__ == "__main__":
model_fp = resnet18(pretrained=True).eval()
qconfig = torch.quantization.get_default_qconfig("qnnpack")
qconfig_dict = {"": qconfig}
model_fp = quantize_fx.fuse_fx(model_fp)
model_fp = quantize_fx.prepare_fx(model_fp, qconfig_dict)
# Calibrate.
x = torch.rand((1, 3, 224, 224))
_ = model_fp(x)
# Quantize.
model_quant = quantize_fx.convert_fx(model_fp)
# Get TorchScript model.
model_traced = torch.jit.trace(model_quant, x).eval()
# Import to TVM.
mod, params = relay.frontend.from_pytorch(
script_module=model_traced,
input_infos=[("x", x.shape)],
keep_quantized_weight=True
)
mod: tvm.IRModule = mod
print("[RELAY]")
print(mod.astext(show_meta_data=False))