Skip to content

xnnpack

xnnpack

XNNPACK backend for PT2E quantization.

XNNPACKPT2EBackend

XNNPACKPT2EBackend(is_dynamic=False, is_qat=False, is_per_channel=True)

Bases: BasePT2EBackend

XNNPACK PT2E backend for ExecuTorch deployment.

Initialize XNNPACK PT2E quantization settings.

Parameters:

Name Type Description Default
is_dynamic bool

Use dynamic activation quantization.

False
is_qat bool

Use quantization-aware training observers.

False
is_per_channel bool

Use per-channel symmetric weight quantization.

True
Source code in src/versatil/quantization/pt2e/backends/xnnpack.py
def __init__(
    self,
    is_dynamic: bool = False,
    is_qat: bool = False,
    is_per_channel: bool = True,
) -> None:
    """Initialize XNNPACK PT2E quantization settings.

    Args:
        is_dynamic: Use dynamic activation quantization.
        is_qat: Use quantization-aware training observers.
        is_per_channel: Use per-channel symmetric weight quantization.
    """
    self._is_dynamic = is_dynamic
    self._is_qat = is_qat
    self._is_per_channel = is_per_channel

name property

name

Serialized PT2E backend name.

is_dynamic property

is_dynamic

Whether this backend uses dynamic activation quantization.

is_qat property

is_qat

Whether this backend uses QAT observer configuration.

is_per_channel property

is_per_channel

Whether this backend uses per-channel weight quantization.

supported_device_types property

supported_device_types

XNNPACK ExecuTorch artifacts run on CPU.

create_quantizer

create_quantizer(module_path)

Create an XNNPACK quantizer targeting a module path.

Parameters:

Name Type Description Default
module_path str

Dotted path to the target submodule. Empty string means global quantization.

required

Returns:

Type Description
Quantizer

Configured XNNPACK quantizer.

Source code in src/versatil/quantization/pt2e/backends/xnnpack.py
def create_quantizer(self, module_path: str) -> Quantizer:
    """Create an XNNPACK quantizer targeting a module path.

    Args:
        module_path: Dotted path to the target submodule.
            Empty string means global quantization.

    Returns:
        Configured XNNPACK quantizer.
    """
    xnnpack_quantizer = _load_xnnpack_quantizer_module()
    quantizer = xnnpack_quantizer.XNNPACKQuantizer()
    config = xnnpack_quantizer.get_symmetric_quantization_config(
        is_per_channel=self._is_per_channel,
        is_qat=self._is_qat,
        is_dynamic=self._is_dynamic,
    )
    for operator_target in _XNNPACK_OPERATOR_TARGETS:
        quantizer.set_operator_type(operator_target, config)
    quantizer.set_filter_function(_XNNPACKNodeFilter(module_path=module_path))
    return quantizer

environment_context

environment_context()

Yield without changing process environment.

Source code in src/versatil/quantization/pt2e/backends/xnnpack.py
@contextmanager
def environment_context(self) -> Generator[None]:
    """Yield without changing process environment."""
    yield

activate_environment

activate_environment()

No-op because XNNPACK deployment does not use torch.compile env.

Source code in src/versatil/quantization/pt2e/backends/xnnpack.py
def activate_environment(self) -> None:
    """No-op because XNNPACK deployment does not use torch.compile env."""