# base_benchmark

Python API: `modelconverter.platforms.base_benchmark`

用于设备上基准测试的平台无关脚手架。

每个平台都使用自己的运行时测量吞吐量和延迟，但周围的工作是相同的：将模型解析为模型文件的路径或 URL，或 HubAI slug，针对每种配置运行一次，并报告收集到的数字。共享部分位于
[Benchmark](/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_benchmark.md)
中，每个平台的基准测试都继承自它。

## 类

### Benchmark

用于在设备上对转换后的模型进行基准测试的基类。

子类在
[benchmark](/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_benchmark.md)
中实现平台特定的测量，并在
[default_configuration](/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_benchmark.md)
和
[all_configurations](/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_benchmark.md)
中描述要用什么进行测量。

#### 方法

##### init

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

解析要进行基准测试的模型。

参数

 * `model_path` (`str`): 模型文件的本地路径或 URL，以支持的扩展名之一结尾，或 HubAI 模型 slug，格式为 `[team_name/]model_name:variant[:model_instance]`。URL
   会首先下载。

引发

 * `ValueError`: 如果 `model_path` 既不是支持的模型文件，也不是 HubAI 上可用模型的 slug。

##### benchmark

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

使用给定配置运行单个基准测试。

参数

 * `configuration` (`Configuration`): 用于基准测试的选项。

返回

 * `Result`: 测量的指标。必须包含 `"fps"` 条目，并且可以包含 `"latency"` 条目。

##### print_results

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

以表格形式打印基准测试结果。

表格相对于结果进行了转置：每列一个运行，每行一个配置选项和测量指标。

参数

 * `results` (`list[tuple[Configuration, Result]]`): 要打印的配置/结果对。不能为空。

##### run

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

对模型进行基准测试并报告结果。

参数

 * `full` (`bool`): 如果为 `True`，则对
   [all_configurations](/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_benchmark.md)
   中的每个配置进行基准测试。如果为 `False`，则仅对
   [default_configuration](/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_benchmark.md)
   进行基准测试。
 * `save` (`bool`): 如果为 `True`，则结果除了打印之外还会写入 CSV 文件。
 * `**kwargs` (`ConfigValue`): 单个配置选项的覆盖。具有非 `None` 默认值的选项的覆盖将被转换为该默认值的类型，除非默认值为布尔值。在完整运行中，覆盖仅应用于未自行设置选项的配置。

##### save_results

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

将基准测试结果保存到 CSV 文件。

文件名为 `<model_name>_benchmark_results.csv`，并写入当前工作目录。嵌套的 `power` 列首先拆分为 `power_system` 和 `power_processor`。

参数

 * `results` (`list[tuple[Configuration, Result]]`): 要保存的配置/结果对。不能为空。

#### 属性

##### all_configurations

返回完整基准测试运行所遍历的配置。

##### default_configuration

返回普通基准测试运行所使用的配置。

##### model_name

##### model_path

## 函数

### get_input_fps

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

读取并验证 DAI 基准测试的输入速率。

`-1` 允许基准测试在没有速率限制的情况下运行。否则，该值必须是正的有限数字。

### get_option

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

读取一个基准测试选项并检查其类型。

参数

 * `configuration` (`Configuration`): 单个基准测试运行的选项。
 * `key` (`str`): 选项的名称。
 * `option_type` (`type[OptionT]`): 选项的必要类型。

返回

 * `OptionT`: 选项的值。

引发

 * `TypeError`: 如果选项缺失或类型不同。

### get_optional_option

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

读取一个可能未设置的基准测试选项。

参数

 * `configuration` (`Configuration`): 单个基准测试运行的选项。
 * `key` (`str`): 选项的名称。
 * `option_type` (`type[OptionT]`): 选项的必要类型。

返回

 * `OptionT | None`: 选项的值，如果未设置则为 `None`。

引发

 * `TypeError`: 如果选项类型不同。

## 属性

### Configuration

### ConfigValue

### OptionT

### Result
