# config

Python API: `modelconverter.utils.config`

Configuration models describing a conversion.

A conversion is described by a YAML file (see `configs/defaults.yaml`) that is parsed into the pydantic models defined here. The
top-level
[Config](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/config.md)
holds one
[SingleStageConfig](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/config.md)
per stage, each describing the input model, its inputs and outputs, the calibration data and the platform-specific options
([HailoConfig](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/config.md),
[RVC2Config](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/config.md),
[RVC3Config](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/config.md)
and
[RVC4Config](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/config.md)).

Values that the user did not provide are filled in from the metadata of the input model itself, so that the exporter running
inside the per-backend Docker image receives a fully resolved description of the model.

## Classes

### BlobBaseConfig

Options shared by the OpenVINO-based platforms.

Base class for the RVC2 and RVC3 configurations, which both convert the model through OpenVINO into a blob.

#### Attributes

##### compile_tool_args

Additional arguments passed to the OpenVINO `compile_tool`. They take precedence over the default ones.

##### compress_to_fp16

Whether to compress FP32 weights and biases of the original model to FP16. All intermediate data is kept in the original
precision.

##### mo_args

Additional arguments passed to the OpenVINO model optimizer. They take precedence over the default ones.

### Config

Top-level configuration of a conversion.

A single-stage configuration is accepted as well: the stage fields can be given at the top level and are wrapped into a single
stage during validation, so the shorthand

```yaml
input_model: models/yolov6n.onnx
encoding:
  from: RGB
  to: BGR
calibration:
  path: models/coco128
```

is equivalent to the same fields nested under a single entry of `stages`. See `configs/defaults.yaml` for every field with its
default.

#### Methods

##### get_stage_config

```python
def get_stage_config(stage: str | None) -> SingleStageConfig:
```

Return the configuration of the given stage.

Parameters

 * `stage` (`str | None`): Name of the stage. Can be `None` only if the configuration has a single stage.

Returns

 * `SingleStageConfig`: The configuration of the requested stage.

Raises

 * `ValueError`: If no stage name is given and the configuration has more than one stage.
 * `KeyError`: If no stage of the given name exists.

#### Attributes

##### name

Name of the model. Defaults to the stage names joined by dashes, or to the stem of the input model for a single unnamed stage.

##### rich_logging

Whether to use rich formatting for the log messages.

##### stages

Configurations of the individual stages, keyed by stage name.

### EncodingConfig

Color encoding conversion requested for an input.

The `from` field, named `from_` in Python, is the encoding the original model expects. The `to` field is the encoding the
converted model is fed with at runtime. If the two differ, a channel reversal is prepended to the input, so that the converted
model takes `to` data while the original graph still receives `from` data.

#### Attributes

##### from_

### Encodings

Set of quantization encodings overriding the computed ones.

#### Attributes

##### activation_encodings

Encodings of the activation tensors, keyed by tensor name.

##### param_encodings

Encodings of the parameter tensors, keyed by tensor name.

### HailoConfig

Options of the Hailo conversion.

#### Attributes

##### alls

Additional lines of the Hailo model script (`alls`) passed to the model optimizer.

##### batch_size

Batch size used for the calibration.

##### compression_level

Compression level, between `0` and `5`.

##### disable_compilation

Whether to stop after the quantization, without compiling the model.

##### force_onnx_names

Whether to force the ONNX names on the produced `.har` and `.hef` models.

##### hw_arch

Hailo hardware architecture to compile for.

##### optimization_level

Optimization level, between `0` and `4`, or `-100` to disable all optimizations.

### ImageCalibrationConfig

Calibration data read from a directory of files.

#### Attributes

##### max_images

Number of files to use from the calibration data. A negative value means all of them.

##### path

Path to the calibration data. Can be a local directory, a remote URL, or an LDF dataset identifier in the `<dataset_name>:<split>`
form; it is downloaded during validation and stored as a local path.

##### resize_method

How to resize the images to the input shape.

### InputConfig

Description of a single input of the model.

Extends
[OutputConfig](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/config.md)
with the pre-processing and calibration settings of the input. Each conversion applies `mean_values` and `scale_values` in its own
way: the RVC4 exporter bakes them into the ONNX graph, RVC2 and RVC3 hand them to the OpenVINO model optimizer, and Hailo puts
them in the model script.

#### Methods

##### requires_onnx_input_modification

```python
def requires_onnx_input_modification(*, reverse_only: bool = False) -> bool:
```

Check whether the ONNX graph must be modified for this input.

Parameters

 * `reverse_only` (`bool`): If `True`, only the channel reversal is taken into account and the mean and scale values are ignored.

Returns

 * `bool`: `True` if the encodings differ or -- unless `reverse_only` is set -- if any non-neutral mean or scale values are set.

#### Attributes

##### calibration

Source of the calibration data for this input.

##### encoding

Color encoding conversion for this input.

##### encoding_mismatch

Return whether the `from` and `to` encodings differ.

##### frozen_value

List of constant values the input is frozen to. Used only by the OpenVINO-based conversions (RVC2 and RVC3), which pass it on to
the model optimizer.

##### is_color_input

Return whether the input is fed with color image data.

##### is_raw_input

Return whether the input is fed with non-image data.

##### mean_values

Per-channel values subtracted from the input, with the same options as `scale_values`.

##### scale_values

Per-channel values the input is divided by, after `mean_values` has been subtracted. Can be given as a single number, a list, or
the name of a preset such as `"imagenet"`.

### LinkCalibrationConfig

Calibration data produced by another stage of the conversion.

Used for multi-stage models, where the calibration data of one stage is obtained by running inference with a previously converted
stage.

#### Attributes

##### output

Name of the output of `stage` to use as the calibration data.

##### script

Python script post-processing the outputs of `stage`. Either the source code itself or a path to a `.py` file, whose contents are
read during validation. Either `output` or `script` must be provided.

##### stage

Name of the stage to take the calibration data from.

### ONNXOptimizationsConfig

Switches of the individual ONNX graph optimizations.

The optimizations are applied to the ONNX model before it is handed over to the platform-specific conversion. All of them are
enabled by default.

#### Methods

##### all_disabled

```python
def all_disabled(self) -> bool:
```

Check whether every optimization is switched off.

#### Attributes

##### fuse_add_mul_to_bn

Whether to fuse `Add`/`Sub` and `Mul` nodes following a `Conv` node into a `BatchNormalization` node.

##### fuse_comb_add_mul_to_conv

Whether to fuse combinations of `Add` and `Mul` nodes preceding a `Conv` node into the `Conv` node.

##### fuse_single_add_mul_to_conv

Whether to fuse a single `Add` or `Mul` node preceding a `Conv` node into the `Conv` node.

##### fuse_split_concat_to_conv

Whether to fuse `Split` and `Concat` nodes preceding a `Conv` node into the `Conv` node.

##### substitute_div_with_mul

Whether to replace a `Div` node whose second operand is a float constant with a `Mul` node and a reciprocal copy of that constant.

##### substitute_sub_with_add

Whether to replace a `Sub` node whose second operand is a constant with an `Add` node and a negated copy of that constant.

### OutputConfig

Description of a single output of the model.

#### Methods

##### validate_layout

```python
def validate_layout(self) -> Self:
```

Check that the layout and the shape have the same length.

Returns

 * `Self`: The validated model.

Raises

 * `ValueError`: If the length of the layout does not match the length of the shape.

#### Attributes

##### data_type

Data type of the output.

##### layout

Lettercode representation of the layout, e.g. `"NCHW"`. Requires `shape` to be provided; a default layout is guessed from the
shape if only `shape` is given.

##### name

Name of the output tensor or node.

##### shape

Shape of the output. Inferred from the model if not provided.

### PlatformConfig

Base class for the platform-specific sections of a stage.

#### Attributes

##### disable_calibration

Whether to skip calibration, and with it the quantization of the model.

### QuantizationOverridesItem

Quantization encoding of a single tensor.

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

#### Methods

##### serialize_is_symmetric

```python
def serialize_is_symmetric(value: bool | None) -> str | None:
```

Serialize `is_symmetric` as a string in JSON output.

Parameters

 * `value` (`bool | None`): Value of the `is_symmetric` field.

Returns

 * `str | None`: The string representation of the value, or `None` if the value is `None`.

#### Attributes

##### bitwidth

Number of bits used to represent the tensor.

##### dtype

Whether the tensor is quantized as an integer or a float.

##### is_symmetric

Whether the quantization is symmetric.

##### max

Largest representable value.

##### min

Smallest representable value.

##### offset

Zero-point offset of the quantization grid.

##### scale

Step size of the quantization grid.

### RandomCalibrationConfig

Calibration data generated from a normal distribution.

Used when no calibration data is provided. The generated values are drawn from a normal distribution and clipped to the allowed
range.

#### Attributes

##### data_type

Data type the generated samples are cast to.

##### max_images

Number of samples to generate.

##### max_value

Upper bound the generated values are clipped to.

##### mean

Mean of the normal distribution.

##### min_value

Lower bound the generated values are clipped to.

##### std

Standard deviation of the normal distribution.

### RVC2Config

Options of the RVC2 conversion.

#### Attributes

##### n_workers

Number of workers used for the parallel superblob compilation. Overrides the automatically determined number.

##### number_of_shaves

Number of SHAVE cores the blob is compiled for. Forced to `8` when `superblob` is enabled.

##### superblob

Whether to produce a `.superblob` file instead of a regular `.blob`.

### RVC3Config

Options of the RVC3 conversion.

#### Attributes

##### pot_target_device

Target device of the OpenVINO post-training optimization tool.

### RVC4Config

Options of the RVC4 conversion.

#### Methods

##### validate_encodings

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

Parse the quantization encodings from the config.

Parameters

 * `value` (`ParamValue | Encodings`): Either `None`, a JSON string, a path to a JSON file, or an already parsed mapping.

Returns

 * `Encodings | None`: The parsed encodings, or `None` if no value was given.

##### validate_quantization_overrides

```python
def validate_quantization_overrides(self) -> Self:
```

Normalize raw quantization override arguments into
[Encodings](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/config.md).

Both `--quantization_overrides PATH` and `--quantization_overrides=PATH` forms are removed from `snpe_onnx_to_dlc_args` and the
referenced JSON file is parsed into the `encodings` field.

Returns

 * `Self`: The validated model.

Raises

 * `ValueError`: If a raw override path is missing, more than one raw override is provided, or a raw override is combined with
   `encodings`.

#### Attributes

##### disable_calibration

##### encodings

Quantization encodings overriding the computed ones.

##### htp_socs

Platforms to pre-compute the DLC graph for.

##### keep_raw_images

Whether to keep the raw calibration images in the intermediate outputs. They can get very large.

##### optimization_level

Optimization level of the DLC graph preparation. Higher levels take longer but yield a faster graph.

##### quantization_mode

Pre-defined quantization mode. All modes except `CUSTOM` override the user-provided SNPE arguments.

##### snpe_dlc_graph_prepare_args

Additional arguments passed to SNPE `snpe-dlc-graph-prepare`.

##### snpe_dlc_quant_args

Additional arguments passed to SNPE `snpe-dlc-quant`.

##### snpe_onnx_to_dlc_args

Additional arguments passed to SNPE `snpe-onnx-to-dlc`. They take precedence over the default ones.

##### strict_quantization_overrides

Whether to validate configured quantization override names against their activation/parameter groups in the effective ONNX model.

##### use_per_channel_quantization

Whether to use per-axis-element quantization for the weights and biases of the supported layer types.

##### use_per_row_quantization

Whether to use row-wise quantization of the `MatMul` and `FullyConnected` operations.

### SingleStageConfig

Description of a single stage of the conversion.

Holds everything needed to convert one model: the model itself, its inputs and outputs, and the options of every supported
platform. Most of the fields are filled in from the metadata of the input model when they are not given explicitly.

#### Methods

##### get_platform_config

```python
def get_platform_config(platform: Platform) -> PlatformConfig:
```

Return the platform configuration for the given platform.

##### validate_disable_onnx_optimizations

```python
def validate_disable_onnx_optimizations(data: Params) -> Params:
```

Translate the deprecated `disable_onnx_optimizations`.

> **Deprecated**
> Deprecated since version 0.5.6:Use `onnx_optimizations: false` instead.

Parameters

 * `data` (`Params`): Raw stage configuration.

Returns

 * `Params`: The configuration with all ONNX optimizations disabled if the deprecated option was enabled.

Raises

 * `ValueError`: If `disable_onnx_optimizations` is enabled and `onnx_optimizations` is specified as well.

##### validate_onnx_optimizations

```python
def validate_onnx_optimizations(data: Params) -> Params:
```

Expand the shorthand values of `onnx_optimizations`.

Parameters

 * `data` (`Params`): Raw stage configuration.

Returns

 * `Params`: The configuration with `onnx_optimizations` replaced by an
   [ONNXOptimizationsConfig](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/config.md)
   with all optimizations either enabled or disabled, if a shorthand value was used.

##### validate_onnx_simplification

```python
def validate_onnx_simplification(data: Params) -> Params:
```

Translate the deprecated `disable_onnx_simplification`.

> **Deprecated**
> Deprecated since version 0.5.6:Use `onnx_simplification: false` instead.

Parameters

 * `data` (`Params`): Raw stage configuration.

Returns

 * `Params`: The configuration with `onnx_simplification` set to `False` if the deprecated option was enabled.

Raises

 * `ValueError`: If `disable_onnx_simplification` is enabled and `onnx_simplification` is specified as well.

#### Attributes

##### hailo

Options of the Hailo conversion.

##### input_bin

Path to the `.bin` file of an OpenVINO IR model. Derived from `input_model` for IR models.

##### input_file_type

Type of the input model, derived from the suffix of `input_model`.

##### input_model

Path to the model to convert. Downloaded to the local models directory during validation.

##### inputs

Descriptions of the model inputs.

##### intermediate_outputs_remote_url

Remote URL to upload the intermediate outputs to.

##### keep_intermediate_outputs

Whether to keep the files created during the conversion.

##### onnx_optimizations

Switches of the ONNX graph optimizations.

##### onnx_simplification

ONNX simplification method to use, or `False` to disable the simplification.

##### output_remote_url

Remote URL to upload the converted model to.

##### outputs

Descriptions of the model outputs.

##### put_file_plugin

Name of the registered plugin used for the uploads.

##### rvc2

Options of the RVC2 conversion.

##### rvc3

Options of the RVC3 conversion.

##### rvc4

Options of the RVC4 conversion.

## Functions

### generate_renamed_onnx

```python
def generate_renamed_onnx(onnx_path: PathType, rename_dict: dict[str, str], output_path: PathType):
```

Save a copy of an ONNX model with renamed node connections.

Every node input and output whose name appears in `rename_dict` is replaced by the new name. The model is saved with external data
if the original model used it.

Parameters

 * `onnx_path` (`PathType`): Path to the ONNX model to rename.
 * `rename_dict` (`dict[str, str]`): Mapping from the old tensor names to the new ones.
 * `output_path` (`PathType`): Path to save the renamed model to.

## Attributes

### NAMED_VALUES
