# base_benchmark

Python API: `modelconverter.platforms.base_benchmark`

Platform-agnostic scaffolding for on-device benchmarking.

Every platform measures throughput and latency with its own runtime, but the surrounding work is the same: resolve the model to
either a path or URL of a model file or a HubAI slug, run it once per configuration, and report the collected numbers. That shared
part lives in
[Benchmark](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_benchmark.md#base_benchmark-Classes-Benchmark),
which the per-platform benchmarks subclass.

## Classes

| Name | Description |
| --- | --- |
| [`Benchmark`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_benchmark.md#base_benchmark-Classes-Benchmark) | Base class for benchmarking a converted model on a device. |

### class `Benchmark`

```python
class Benchmark(ABC)
```

Base class for benchmarking a converted model on a device.

Subclasses implement the platform-specific measurement in
[benchmark](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_benchmark.md#base_benchmark-Classes-Benchmark-benchmark)
and describe what to measure it with in
[default_configuration](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_benchmark.md#base_benchmark-Classes-Benchmark-default_configuration)
and
[all_configurations](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_benchmark.md#base_benchmark-Classes-Benchmark-all_configurations).

Attributes

| Name | Type | Description |
| --- | --- | --- |
| `model_name` |  |  |
| `model_path` | `PathType` |  |

Properties

| Name | Type | Description |
| --- | --- | --- |
| [`all_configurations`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_benchmark.md#base_benchmark-Classes-Benchmark-all_configurations) | `list[Configuration]` | The configurations a full benchmark run sweeps. |
| [`default_configuration`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_benchmark.md#base_benchmark-Classes-Benchmark-default_configuration) | `Configuration` | The configuration a plain benchmark run uses. |

Methods

| Name | Description |
| --- | --- |
| [`__init__`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_benchmark.md#base_benchmark-Classes-Benchmark-__init__) | Resolve the model to benchmark. |
| [`benchmark`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_benchmark.md#base_benchmark-Classes-Benchmark-benchmark) | Run a single benchmark with the given configuration. |
| [`print_results`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_benchmark.md#base_benchmark-Classes-Benchmark-print_results) | Print the benchmark results as a table. |
| [`run`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_benchmark.md#base_benchmark-Classes-Benchmark-run) | Benchmark the model and report the results. |
| [`save_results`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_benchmark.md#base_benchmark-Classes-Benchmark-save_results) | Save the benchmark results to a CSV file. |

#### property `all_configurations`

```python
all_configurations: list[Configuration]
```

The configurations a full benchmark run sweeps.

#### property `default_configuration`

```python
default_configuration: Configuration
```

The configuration a plain benchmark run uses.

#### method `__init__`

```python
def __init__(model_path: str)
```

Resolve the model to benchmark.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `model_path` | `str` | Either a local path or a URL of a model file ending in one of the supported extensions, or a HubAI model slug of the form `[team/]model[:variant[:version-or-hash]]`. A URL is downloaded first. | required |

Raises

| Type | Description |
| --- | --- |
| `ValueError` | If `model_path` is neither a supported model file nor a slug of a model available on HubAI. |

#### method `benchmark`

```python
def benchmark(configuration: Configuration) -> Result
```

Run a single benchmark with the given configuration.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `configuration` | `Configuration` | The options to benchmark with. | required |

Returns

| Type | Description |
| --- | --- |
| `Result` | The measured metrics. Must contain a `"fps"` entry and may contain a `"latency"` entry. |

#### method `print_results`

```python
def print_results(results: list[tuple[Configuration, Result]])
```

Print the benchmark results as a table.

The table is transposed relative to the results: one column per run, one row per configuration option and measured metric.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `results` | `list[tuple[Configuration, Result]]` | The configuration/result pairs to print. Must not be empty. | required |

#### method `run`

```python
def run(*, full: bool = True, save: bool = False, **kwargs: ConfigValue)
```

Benchmark the model and report the results.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `full` | `bool` | If `True`, benchmark every configuration in [all_configurations](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_benchmark.md#base_benchmark-Classes-Benchmark-all_configurations). If `False`, benchmark only [default_configuration](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_benchmark.md#base_benchmark-Classes-Benchmark-default_configuration). | `True` |
| `save` | `bool` | If `True`, the results are written to a CSV file in addition to being printed. | `False` |
| `**kwargs` | `ConfigValue` | Overrides for individual configuration options. An override of an option with a non-`None` default is cast to the type of that default, unless the default is a boolean. In a full run, an override is only applied to configurations that do not set the option themselves. |  |

#### method `save_results`

```python
def save_results(results: list[tuple[Configuration, Result]])
```

Save the benchmark results to a CSV file.

The file is named `<model_name>_benchmark_results.csv` and is written to the current working directory. A nested `power` column is
first split into `power_system` and `power_processor`.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `results` | `list[tuple[Configuration, Result]]` | The configuration/result pairs to save. Must not be empty. | required |

## Functions

| Name | Description |
| --- | --- |
| [`get_input_fps`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_benchmark.md#base_benchmark-Functions-get_input_fps) | Read and validate the input rate for a DAI benchmark. |
| [`get_option`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_benchmark.md#base_benchmark-Functions-get_option) | Read one benchmark option and check its type. |
| [`get_optional_option`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_benchmark.md#base_benchmark-Functions-get_optional_option) | Read one benchmark option that may also be unset. |

### function `get_input_fps`

```python
def get_input_fps(configuration: Configuration) -> float
```

Read and validate the input rate for a DAI benchmark.

`-1` lets the benchmark run without a rate limit. Otherwise the value must be a positive, finite number.

Returns

| Type | Description |
| --- | --- |
| `float` | The input rate, or `-1` for no rate limit. |

Raises

| Type | Description |
| --- | --- |
| `TypeError` | If `input_fps` is missing, a boolean or not a number. |
| `ValueError` | If `input_fps` is neither `-1` nor a positive, finite number. |

### function `get_option`

```python
def get_option(
    configuration: Configuration,
    key: str,
    option_type: type[OptionT],
) -> OptionT
```

Read one benchmark option and check its type.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `configuration` | `Configuration` | The options of a single benchmark run. | required |
| `key` | `str` | The name of the option. | required |
| `option_type` | `type[OptionT]` | The necessary type of the option. | required |

Returns

| Type | Description |
| --- | --- |
| `OptionT` | The value of the option. |

Raises

| Type | Description |
| --- | --- |
| `TypeError` | If the option is missing or has a different type. |

### function `get_optional_option`

```python
def get_optional_option(
    configuration: Configuration,
    key: str,
    option_type: type[OptionT],
) -> OptionT | None
```

Read one benchmark option that may also be unset.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `configuration` | `Configuration` | The options of a single benchmark run. | required |
| `key` | `str` | The name of the option. | required |
| `option_type` | `type[OptionT]` | The necessary type of the option. | required |

Returns

| Type | Description |
| --- | --- |
| `OptionT | None` | The value of the option, or `None` if it is not set. |

Raises

| Type | Description |
| --- | --- |
| `TypeError` | If the option has a different type. |

## Module Attributes

| Name | Type | Description |
| --- | --- | --- |
| [`Configuration`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_benchmark.md#base_benchmark-Module-Attributes-Configuration) | `TypeAlias` |  |
| [`ConfigValue`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_benchmark.md#base_benchmark-Module-Attributes-ConfigValue) | `TypeAlias` |  |
| [`OptionT`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_benchmark.md#base_benchmark-Module-Attributes-OptionT) |  |  |
| [`Result`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_benchmark.md#base_benchmark-Module-Attributes-Result) | `TypeAlias` |  |

### type alias `Configuration`

```python
Configuration: TypeAlias = dict[str, ConfigValue]
```

### type alias `ConfigValue`

```python
ConfigValue: TypeAlias = str | int | float | bool | None
```

### type variable `OptionT`

```python
OptionT = TypeVar('OptionT', bound=str | int | float | bool)
```

### type alias `Result`

```python
Result: TypeAlias = dict[str, float | str | None]
```
