# encodings

Python API: `modelconverter.utils.encodings`

Helpers for externally supplied quantization encodings.

Quantization encodings describe the scale, offset and bit width used to quantize activations and parameters of a model. They can
be handed to the RVC4 conversion to override the values derived from calibration. Raw `rvc4.encodings` data is passed through to
SNPE rather than normalized through ModelConverter's `Encodings` schema. The parser below is kept for already-typed compatibility
paths; strict validation uses only the activation/parameter names it needs from the raw structure.

## Classes

| Name | Description |
| --- | --- |
| [`Encodings`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/encodings.md#encodings-Classes-Encodings) | Set of quantization encodings overriding the computed ones. |
| [`QuantizationOverridesItem`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/encodings.md#encodings-Classes-QuantizationOverridesItem) | Quantization encoding of a single tensor. |

### class `Encodings`

```python
class Encodings(BaseModelExtraForbid)
```

Set of quantization encodings overriding the computed ones.

Attributes

| Name | Type | Description |
| --- | --- | --- |
| `activation_encodings` | `dict[str, list[QuantizationOverridesItem]]` | Encodings of the activation tensors, keyed by tensor name. |
| `param_encodings` | `dict[str, list[QuantizationOverridesItem]]` | Encodings of the parameter tensors, keyed by tensor name. |

### class `QuantizationOverridesItem`

```python
class QuantizationOverridesItem(BaseModelExtraForbid)
```

Quantization encoding of a single tensor.

Follows the AIMET encoding specification and is used to override the quantization parameters computed during the RVC4 conversion.

Attributes

| Name | Type | Description |
| --- | --- | --- |
| `bitwidth` | `Annotated[int, Literal[4, 8, 16, 32]] | None` | Number of bits used to represent the tensor. |
| `dtype` | `Literal['int', 'float'] | None` | Whether the tensor is quantized as an integer or a float. |
| `is_symmetric` | `bool | None` | Whether the quantization is symmetric. |
| `max` | `float | None` | Largest representable value. |
| `min` | `float | None` | Smallest representable value. |
| `offset` | `int | None` | Zero-point offset of the quantization grid. |
| `scale` | `float | None` | Step size of the quantization grid. |

## Functions

| Name | Description |
| --- | --- |
| [`collect_quantization_override_names`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/encodings.md#encodings-Functions-collect_quantization_override_names) | Collect activation and parameter names without normalizing entries. |
| [`parse_encodings`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/encodings.md#encodings-Functions-parse_encodings) | Parse quantization encodings into an `Encodings` model. |
| [`validate_quantization_override_names`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/encodings.md#encodings-Functions-validate_quantization_override_names) | Reject override names that are absent or in the wrong encoding group. |

### function `collect_quantization_override_names`

```python
def collect_quantization_override_names(
    encodings: Encodings | Mapping[str, Any],
) -> tuple[set[str], set[str]]
```

Collect activation and parameter names without normalizing entries.

### function `parse_encodings`

```python
def parse_encodings(value: ParamValue | Encodings) -> Encodings
```

Parse quantization encodings into an `Encodings` model.

Accepts an `Encodings` instance, which is returned unchanged, a JSON string, or a dictionary with `activation_encodings` and
`param_encodings` keys. Each of the two groups may either be a mapping from tensor name to entries, or a list of entries carrying
a `name` field. Entries are normalized on the way: the legacy `bw` and `is_sym` keys are renamed to `bitwidth` and `is_symmetric`,
the `dtype` is lower-cased, unknown keys are dropped, single-element lists are unwrapped and per-channel entries are expanded into
one entry per channel.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `value` | `ParamValue | Encodings` | Encodings to parse. | required |

Returns

| Type | Description |
| --- | --- |
| `Encodings` | Parsed encodings. |

Raises

| Type | Description |
| --- | --- |
| `TypeError` | If `value` is not a dictionary once deserialized, if a group is neither a list nor a dictionary, or if an individual entry is not a dictionary. |
| `ValueError` | If an entry in a list-shaped group has a missing or invalid tensor name, or if the per-channel fields of an entry have different lengths. |

### function `validate_quantization_override_names`

```python
def validate_quantization_override_names(
    encodings: Encodings | Mapping[str, Any],
    model_path: str | Path,
)
```

Reject override names that are absent or in the wrong encoding group.

## Module Attributes

| Name | Type | Description |
| --- | --- | --- |
| [`ALLOWED_ENCODING_KEYS`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/encodings.md#encodings-Module-Attributes-ALLOWED_ENCODING_KEYS) | `set[str]` |  |

### constant `ALLOWED_ENCODING_KEYS`

```python
ALLOWED_ENCODING_KEYS: set[str] = {
    'bitwidth',
    'is_symmetric',
    'dtype',
    'max',
    'min',
    'offset',
    'scale',
}
```
