coreai_opt.quantization.spec.QuantizationComponentFactory

class coreai_opt.quantization.spec.QuantizationComponentFactory[source]

Bases: CompressionComponentFactoryBase

Factory class for creating quantization components from QuantizationSpec.

This factory eliminates circular dependencies between QuantizationSpec and component classes (FakeQuantizeImplBase, QParamsCalculatorBase, RangeCalculatorBase) by centralizing the creation logic.

__init__()

Methods

construct(spec, target)

Create a fake quantizer instance from a QuantizationSpec.

construct_partial(spec, target)

Create a fake quantizer partial object for deferred construction.

create_fake_quantizer(spec, quantization_target)

Create a FakeQuantizeImplBase instance from a QuantizationSpec.

create_fake_quantizer_partial(spec, ...)

Create a fake quantizer partial object for deferred construction by the graph-mode prepare API (torchao PT2E).

create_qparams_calculator(spec, ...)

Create a QParamsCalculatorBase instance from a QuantizationSpec.

create_range_calculator(spec)

Create a RangeCalculatorBase instance from a QuantizationSpec.

classmethod construct(spec, target)[source]

Create a fake quantizer instance from a QuantizationSpec.

This method implements the base class interface and delegates to create_fake_quantizer.

Parameters:
Returns:

FakeQuantizeImplBase instance configured from the spec, or None if spec is None

Return type:

FakeQuantizeImplBase | None

classmethod construct_partial(spec, target)[source]

Create a fake quantizer partial object for deferred construction.

This method implements the base class interface and delegates to create_fake_quantizer_partial.

Parameters:
Returns:

A partial object for deferred construction, or None if spec is None

Return type:

PartialConstructor

classmethod create_fake_quantizer(spec, quantization_target)[source]

Create a FakeQuantizeImplBase instance from a QuantizationSpec.

This method automatically detects any extra arguments in the spec beyond the base QuantizationSpec fields and passes them to the fake quantizer constructor.

Parameters:
Returns:

FakeQuantizeImplBase instance configured from the spec

Return type:

FakeQuantizeImplBase

Example

>>> spec = QuantizationSpec(...)
>>> fake_quantize = QuantizationComponentFactory.create_fake_quantizer(
...     spec, quantization_target=CompressionTargetTensor.WEIGHT
... )
>>> extended_spec = ExtraArgQuantizationSpec(eps=0.1, ...)
>>> fake_quantize = QuantizationComponentFactory.create_fake_quantizer(
...     extended_spec,
...     quantization_target=CompressionTargetTensor.ACTIVATION
... )
classmethod create_fake_quantizer_partial(spec, quantization_target)[source]

Create a fake quantizer partial object for deferred construction by the graph-mode prepare API (torchao PT2E).

Parameters:
Returns:

A partial object that can be used by the graph-mode

prepare API to construct fake quantizer instances. Each call to the partial will create a new instance with its own qparams_calculator.

Return type:

PartialConstructor

classmethod create_qparams_calculator(spec, quantization_target)[source]

Create a QParamsCalculatorBase instance from a QuantizationSpec.

Parameters:
Returns:

QParamsCalculatorBase instance configured from the spec

Return type:

QParamsCalculatorBase

classmethod create_range_calculator(spec)[source]

Create a RangeCalculatorBase instance from a QuantizationSpec.

Parameters:

spec (QuantizationSpec) – QuantizationSpec instance containing configuration

Returns:

RangeCalculatorBase instance configured from the spec

Return type:

RangeCalculatorBase