Skip to content

Problems encountered when performing xnnpack quantization #6925

@LuckyHeart

Description

@LuckyHeart

🐛 Describe the bug

Hi, I'm trying to perform XNNPACK quantization for PyTorch model. I have successfully delegate the model to xnnpACK and try to perform model quantization as follows:

model = export_for_training(model, sample_inputs).module() # 2-stage export for quantization path

from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e
from torch.ao.quantization.quantizer.xnnpack_quantizer import (
    get_symmetric_quantization_config,
    XNNPACKQuantizer,
)

def quantize(model, example_inputs):
    """This is the official recommended flow for quantization in pytorch 2.0 export"""
    print(f"Original model: {model}")
    quantizer = XNNPACKQuantizer()
    # if we set is_per_channel to True, we also need to add out_variant of quantize_per_channel/dequantize_per_channel
    operator_config = get_symmetric_quantization_config(is_per_channel=False)
    quantizer.set_global(operator_config)
    m = prepare_pt2e(model, quantizer)
    # calibration
    m(*example_inputs)
    m = convert_pt2e(m)
    print(f"Quantized model: {m}")
    # make sure we can export to flat buffer
    return m

quantized_mobilenetv2 = quantize(model, sample_inputs)

edge = to_edge_transform_and_lower(
    export(quantized_mobilenetv2, sample_inputs),
    compile_config=EdgeCompileConfig(_check_ir_validity=False),
    partitioner=[XnnpackPartitioner()]
)

exec_prog = edge.to_executorch()

with open("qs8_xnnpack_sm_small.pte", "wb") as file:
    exec_prog.write_to_file(file)

However, I encountered error as follows:

    
# To see more debug info, please use `graph_module.print_readable()`
/opt/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/export/exported_program.py:1068: UserWarning: This API is deprecated and soon will be removed. Please look at the docstring to see how to preserve an operator.
  warnings.warn(
Traceback (most recent call last):
  File "/Users/rx/Desktop/MMSTest/SCNet-main/scnet/xnnpack-quan-2.py", line 71, in <module>
    exec_prog = edge.to_executorch()
  File "/opt/anaconda3/envs/executorch/lib/python3.10/site-packages/executorch/exir/program/_program.py", line 1322, in to_executorch
    new_gm_res = p(new_gm)
  File "/opt/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/fx/passes/infra/pass_base.py", line 41, in __call__
    res = self.call(graph_module)
  File "/opt/anaconda3/envs/executorch/lib/python3.10/site-packages/executorch/exir/passes/__init__.py", line 426, in call
    raise RuntimeError(f"Missing out variants: {missing_out_vars}")
RuntimeError: Missing out variants: {'quantized_decomposed::dequantize_per_tensor', 'quantized_decomposed::quantize_per_tensor'}

Based on the previous issue like #1263, I guess it's because of the installation problem of buck2.

I tried to execute

  SO_LIB=$($BUCK2 build //kernels/quantized:aot_lib --show-output | grep "buck-out" | cut -d" " -f2)

it reported the error as follows:

Starting new buck2 daemon...
Connected to new buck2 daemon.
Action failed: root//kernels/quantized:quantized_ops_need_aot_registration (genrule)
Local command returned non-zero exit code 1
Reproduce locally: `env -- 'ASAN_OPTIONS=detect_leaks=0,detect_odr_violation=0' 'BUCK_SCRATCH_PATH=buck-out/v2/tmp/root/ ...<omitted>... v2/gen/root/200212f73efcd57d/kernels/quantized/__quantized_ops_need_aot_registration__/sh/genrule.sh (run `buck2 log what-failed` to get the full command)`
stdout:
stderr:
Traceback (most recent call last):
  File "<string>", line 52, in <module>
  File "<string>", line 49, in __run
  File "/Users/rx/Desktop/MMSTest/executorch/buck-out/v2/gen/root/200212f73efcd57d/codegen/tools/__gen_oplist__/gen_oplist#link-tree/__par__/bootstrap.py", line 69, in run_as_main
    runpy._run_module_as_main(main_module, alter_argv=False)
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/Users/rx/Desktop/MMSTest/executorch/buck-out/v2/gen/root/200212f73efcd57d/codegen/tools/__gen_oplist__/gen_oplist#link-tree/executorch/codegen/tools/gen_oplist.py", line 15, in <module>
    from torchgen.executorch.parse import strip_et_fields
ModuleNotFoundError: No module named 'torchgen'
Build ID: a8477286-0c73-4349-ada3-858276f51064
Network: Up: 0B  Down: 670KiB
Jobs completed: 299. Time elapsed: 12.4s.
Cache hits: 0%. Commands: 50 (cached: 0, remote: 0, local: 50)
BUILD FAILED
Failed to build 'root//kernels/quantized:quantized_ops_need_aot_registration (prelude//platforms:default#200212f73efcd57d)'

Hope to get your help. THX!!

Versions

none

Metadata

Metadata

Assignees

No one assigned

    Labels

    module: exirIssues related to Export IR and the code under exir/module: xnnpackIssues related to xnnpack delegation and the code under backends/xnnpack/

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions