Skip to content

API reference

Main API

auto_quantize(model, pretrained=True)

Turns a normal model into a quantized model, using an appropriate backend

Parameters:

Name Type Description Default
model Any

The model to quantize.

required
pretrained bool

Whether this model is pretrained

True
Source code in approx/core/api.py
Python
def auto_quantize(model: Any, pretrained: bool = True) -> Any:
    """Turns a normal model into a quantized model, using an appropriate backend

    Args:
        model: The model to quantize.
        pretrained: Whether this model is pretrained
    """
    if _vars._APPROX_BACKEND is None:
        raise ValueError(
            "No backend has been set. "
            "Please call `approx.auto_set_backend()`."
        )
    qmodel = _vars._APPROX_BACKEND.auto_quantize(model, pretrained)
    return qmodel

auto_set_backend()

Automatically sets an appropriate backend for approx to use.

Returns:

Type Description
None

None.

Source code in approx/core/api.py
Python
def auto_set_backend() -> None:
    """Automatically sets an appropriate backend for `approx` to use.

    Returns:
        None.
    """

    _vars._APPROX_BACKEND = auto_select_backend()

compare(model, quantized_model, test_loader, *, eval_loop)

Compares your normal model with your quantized model

Parameters:

Name Type Description Default
model Any

Your normal model

required
quantized_model Any

Your quantized model

required
test_loader Any

The "dataloader" to be used for testing.

required
eval_loop EvalLoop

Your evaluation loop that operates on your model and it's data, and returns a dictionary which maps each metric to its history

required

Returns:

Type Description
CompareResult

Useful statistical information

Source code in approx/core/api.py
Python
def compare(
    model: Any, quantized_model: Any, test_loader: Any, *, eval_loop: EvalLoop
) -> CompareResult:
    """
    Compares your normal model with your quantized model

    Args:
        model: Your normal model
        quantized_model: Your quantized model
        test_loader: The "dataloader" to be used for testing.
        eval_loop: Your evaluation loop that operates on your model and it's data,
                   and returns a dictionary which maps each metric to its history

    Returns:
        Useful statistical information
    """
    runner = _CompareRunner([model, quantized_model], test_loader, eval_loop)
    return runner.run()

Types

EvalLoop

A function which accepts your model and the data, and returns a dictionary mapping metrics to their histories.

For example

Python
def eval_loop(model, data):
    return {
        "loss": [1.0, 2.0],
        "accuracy": [3.0, 4.0],
    }

Bases: Protocol

Source code in approx/core/compare.py
Python
class EvalLoop(Protocol):
    def __call__(self, model: Any, test_dl: Any) -> Dict[str, List[float]]:
        ...