本指南解释如何通过添加新适配器和指标来扩展 InfiniMetrics。
通过继承 BaseAdapter 创建新适配器类:
from infinimetrics.adapter import BaseAdapter
class MyCustomAdapter(BaseAdapter):
def __init__(self, config):
super().__init__(config)
# 初始化适配器
self.device = config.get('device', 'nvidia')
def setup(self):
# 准备测试环境
print(f"正在设置 {self.__class__.__name__}")
# 加载模型、分配内存等
def process(self, test_input):
# 执行测试并返回指标
results = {
"my.metric": {
"value": 42.0,
"unit": "operations/s"
}
}
return results
def teardown(self):
# 清理资源
print(f"正在清理 {self.__class__.__name__}")
# 释放内存、关闭连接等在 dispatcher.py 中将适配器添加到适配器注册表:
# 在 dispatcher.py 中
self.adapter_registry = {
("operator", "myframework"): MyCustomAdapter,
# ... 现有适配器 ...
}创建 JSON 配置文件:
{
"run_id": "my_test",
"testcase": "operator.myframework.MyTest",
"config": {
"device": "nvidia",
"iterations": 100
},
"metrics": [
{"name": "my.metric"}
]
}python main.py my_test_config.json在 infinimetrics/common/metrics.py 中:
class CustomMetric(Metric):
def __init__(self, name: str, value: float, unit: str = ""):
super().__init__(name, value, unit)
def to_dict(self):
return {
"name": self.name,
"value": self.value,
"unit": self.unit,
"timestamp": self.timestamp
}在适配器的 process 方法中:
def process(self, test_input):
metric = CustomMetric("custom.metric", 123.45, "ms")
return {"custom.metric": metric.to_dict()}| 方法 | 描述 | 必需 |
|---|---|---|
__init__(config) |
使用配置初始化适配器 | 是 |
setup() |
准备测试环境 | 是 |
process(test_input) |
执行测试并返回指标 | 是 |
teardown() |
清理资源 | 是 |
{
"run_id": "unique_identifier",
"testcase": "category.framework.test_name",
"config": {...},
"metrics": [...]
}{
"name": "metric.name",
"value": 42.0,
"unit": "unit_name"
}infinimetrics/
├── hardware/ # 硬件测试适配器
├── operators/ # 算子测试适配器
├── inference/ # 推理测试适配器
├── communication/ # 通信测试适配器
└── common/ # 共享工具
- 适配器文件:
{framework}_adapter.py - 适配器类:
{Framework}Adapter(例如InfiniCoreAdapter) - 测试用例:
<category>.<framework>.<TestName>
- 错误处理: 始终在 try-except 块中包装关键操作
- 日志记录: 使用 Python 的 logging 模块进行调试输出
- 资源管理: 确保
teardown()正确释放所有资源 - 配置: 为所有配置参数提供合理的默认值
- 文档: 为所有公共方法添加文档字符串
- 创建测试配置文件
- 使用
--verbose标志运行以获取详细输出 - 检查输出目录中的 metrics.json
- 验证日志中的任何错误
python main.py test_config.json --verbose贡献适配器或指标时:
- 遵循现有代码风格
- 为新功能添加文档
- 包含示例配置
- 更新相关文档文件
如有问题或讨论,请在 GitHub 上开启 issue。