Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'q_config' is needed when export an INT8 model #1736

Open
ZhangShuoAlreadyExists opened this issue Apr 18, 2024 · 3 comments
Open

'q_config' is needed when export an INT8 model #1736

ZhangShuoAlreadyExists opened this issue Apr 18, 2024 · 3 comments
Assignees
Labels
aitce AI TCE to handle it firstly

Comments

@ZhangShuoAlreadyExists
Copy link

Hi,

I want to convert and quantize Pytorch model to ONNX model. I refer to this example https://github.com/intel/neural-compressor/blob/master/examples/pytorch/image_recognition/torchvision_models/export/fx/main.py
When calling export function, there is error "'q_config' is needed when export an INT8 model"
I don't see anything about q_config in example code. May I know how to solve this issue?

Here is my code:
if name == "main":
model = timm.create_model('resnet50.a1_in1k', pretrained=True)
model = model.eval()

val_dataset = SampleDataset('golden_image')
val_loader = torch.utils.data.DataLoader(
    val_dataset,
    batch_size=1, shuffle=False,
    num_workers=1, pin_memory=True)

conf = PostTrainingQuantConfig(approach='static')
q_model = quantization.fit(model,
                           conf,
                           calib_dataloader=val_loader) # Don't need tuning.
int8_onnx_config = Torch2ONNXConfig(
    dtype="int8",
    opset_version=14,
    quant_format="QDQ",
    example_inputs=torch.randn(1, 3, 224, 224),
    input_names=['input'],
    output_names=['output'],
    dynamic_axes={"input": {0: "batch_size"},
                    "output": {0: "batch_size"}},
)

inc_model = Model(q_model)
inc_model.export("resnet50_int8.onnx", int8_onnx_config)

Error message:
neural_compressor\experimental\export\torch2onnx.py", line 389, in torch_to_int8_onnx
assert q_config is not None, "'q_config' is needed when export an INT8 model."
AssertionError: 'q_config' is needed when export an INT8 model.

torch.version '2.2.2+cpu'
neural_compressor.version '2.6'

@NeoZhangJianyu NeoZhangJianyu added the aitce AI TCE to handle it firstly label Apr 25, 2024
@NeoZhangJianyu NeoZhangJianyu self-assigned this Apr 25, 2024
@NeoZhangJianyu
Copy link
Collaborator

@ZhangShuoAlreadyExists We will check it and feedback as soon!

@NeoZhangJianyu
Copy link
Collaborator

@ZhangShuoAlreadyExists
I create an example as your code. It's passed.
Could you refer to it?

Install packages:

pip install neural_compressor
pip install torch torchvision onnxruntime onnx

Code:


import argparse
import os
import random
import shutil
import time
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.distributed as dist
import torch.optim
import torch.multiprocessing as mp
import torch.utils.data
import torch.utils.data.distributed
import torchvision
import torchvision.transforms as transforms
import torchvision.datasets as datasets
import torchvision.models as models
import torch

from neural_compressor import PostTrainingQuantConfig
from neural_compressor import quantization
from neural_compressor.config import Torch2ONNXConfig

from neural_compressor.model import Model



model = torchvision.models.resnet50(pretrained=True)
model = model.eval()

Transform = transforms.Compose([
    transforms.Resize((224, 224)),
    transforms.ToTensor(),
    transforms.Normalize(mean=(0.5, 0.5, 0.5),
                        std=(0.5, 0.5, 0.5))])

test_data = datasets.CIFAR10(
    root="data",
    train=False,
    download=True,
    transform=Transform,
)

val_loader = torch.utils.data.DataLoader(
    test_data,
    batch_size=1, shuffle=False,
    num_workers=1, pin_memory=True)

conf = PostTrainingQuantConfig(approach='static')
q_model = quantization.fit(model,
                           conf,
                           calib_dataloader=val_loader) # Don't need tuning.
int8_onnx_config = Torch2ONNXConfig(
    dtype="int8",
    opset_version=14,
    quant_format="QDQ",
    example_inputs=torch.randn(1, 3, 224, 224),
    input_names=['input'],
    output_names=['output'],
    dynamic_axes={"input": {0: "batch_size"},
                    "output": {0: "batch_size"}},
)

# inc_model = Model(q_model)
q_model.export("resnet50_int8.onnx", int8_onnx_config)

@NeoZhangJianyu
Copy link
Collaborator

@ZhangShuoAlreadyExists
Could you confirm if our answer support you?

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
aitce AI TCE to handle it firstly
Projects
None yet
Development

No branches or pull requests

2 participants