147 lines
4.2 KiB
Python
147 lines
4.2 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
自动生成的FINN转换脚本
|
|
"""
|
|
import os
|
|
import json
|
|
import inspect
|
|
from finn.util.basic import make_build_dir
|
|
from finn.builder.build_dataflow import build_dataflow_cfg
|
|
from finn.builder.build_dataflow_config import (
|
|
DataflowBuildConfig,
|
|
ShellFlowType,
|
|
DataflowOutputType,
|
|
)
|
|
|
|
# 加载配置
|
|
config = {
|
|
"model": {
|
|
"onnx_path": "/workspace/data/models/finn_qcnn_finn.onnx",
|
|
"host_onnx_path": "F:\\Job\\\u5149\u74e3\u8bc6\u522b\\0504\\data\\models\\finn_qcnn_finn.onnx",
|
|
"input_shape": [
|
|
1,
|
|
3,
|
|
128,
|
|
128
|
|
],
|
|
"num_classes": 10
|
|
},
|
|
"platform": {
|
|
"board": "Pynq-Z2",
|
|
"part": "xc7z020clg400-1",
|
|
"clock_period_ns": 10.0,
|
|
"pynq_version": "2.7",
|
|
"xilinx_version": "2020.2"
|
|
},
|
|
"quantization": {
|
|
"weight_width": 8,
|
|
"activation_width": 8,
|
|
"input_width": 8,
|
|
"accumulator_width": 32
|
|
},
|
|
"performance": {
|
|
"target_fps": 1,
|
|
"folding_config": "auto",
|
|
"mem_mode": "decoupled"
|
|
},
|
|
"optimization": {
|
|
"streamline": true,
|
|
"fold_constants": true,
|
|
"remove_unused": true,
|
|
"merge_ops": true
|
|
},
|
|
"output": {
|
|
"output_dir": "/workspace/finn_deployment/output",
|
|
"host_output_dir": "F:\\Job\\\u5149\u74e3\u8bc6\u522b\\0504\\finn_deployment\\output",
|
|
"generate_driver": true,
|
|
"generate_report": true,
|
|
"save_intermediate": false
|
|
},
|
|
"host": {
|
|
"finn_image": "xilinx/finn:latest",
|
|
"xilinx_path": "/opt/Xilinx",
|
|
"xilinx_version": "2020.2"
|
|
}
|
|
}
|
|
|
|
# 创建输出目录
|
|
output_dir = config["output"]["output_dir"]
|
|
os.makedirs(output_dir, exist_ok=True)
|
|
|
|
def _supported_kwargs(cls, kwargs):
|
|
"""Keep generated script compatible with minor FINN API differences."""
|
|
params = inspect.signature(cls).parameters
|
|
return {key: value for key, value in kwargs.items() if key in params}
|
|
|
|
|
|
def _optional_output(name):
|
|
if hasattr(DataflowOutputType, name):
|
|
return getattr(DataflowOutputType, name)
|
|
print(f"WARNING: FINN DataflowOutputType.{name} is unavailable in this FINN version")
|
|
return None
|
|
|
|
|
|
generate_outputs = [
|
|
_optional_output("PYNQ_DRIVER"),
|
|
_optional_output("BITFILE"),
|
|
_optional_output("DEPLOYMENT_PACKAGE"),
|
|
]
|
|
generate_outputs = [item for item in generate_outputs if item is not None]
|
|
|
|
build_kwargs = dict(
|
|
output_dir=output_dir,
|
|
synth_clk_period_ns=config["platform"]["clock_period_ns"],
|
|
board=config["platform"]["board"],
|
|
shell_flow_type=ShellFlowType.VIVADO_ZYNQ,
|
|
generate_outputs=generate_outputs,
|
|
target_fps=config["performance"]["target_fps"],
|
|
streamline=config["optimization"]["streamline"],
|
|
folding_config_file=None, # 使用自动配置
|
|
verify_steps=[
|
|
"streamline",
|
|
"convert_to_hls",
|
|
"create_dataflow_partition",
|
|
"create_stitched_ip",
|
|
],
|
|
save_intermediate_models=config["output"]["save_intermediate"],
|
|
)
|
|
|
|
# 配置FINN构建
|
|
build_cfg = DataflowBuildConfig(**_supported_kwargs(DataflowBuildConfig, build_kwargs))
|
|
|
|
# 运行FINN构建
|
|
print("=" * 60)
|
|
print("开始FINN模型转换")
|
|
print("=" * 60)
|
|
print(f"输入模型: {config['model']['onnx_path']}")
|
|
print(f"目标板卡: {config['platform']['board']}")
|
|
print(f"PYNQ版本: v{config['platform']['pynq_version']}")
|
|
print(f"Xilinx工具链: {config['platform']['xilinx_version']}")
|
|
print(f"输出目录: {output_dir}")
|
|
print("=" * 60)
|
|
|
|
try:
|
|
build_dataflow_cfg(config["model"]["onnx_path"], build_cfg)
|
|
print("\n" + "=" * 60)
|
|
print("✓ FINN转换成功!")
|
|
print("=" * 60)
|
|
print(f"\n输出文件位于: {output_dir}")
|
|
print("\n生成的文件:")
|
|
print(" - finn_design.bit: FPGA bitstream")
|
|
print(" - finn_design.hwh: 硬件描述文件")
|
|
print(" - driver.py: Python驱动接口")
|
|
print(" - runtime_weights/: 运行时权重")
|
|
print("=" * 60)
|
|
|
|
except Exception as e:
|
|
print("\n" + "=" * 60)
|
|
print("✗ FINN转换失败")
|
|
print("=" * 60)
|
|
print(f"错误信息: {e}")
|
|
print("\n请检查:")
|
|
print(" 1. ONNX模型是否有效")
|
|
print(" 2. 算子是否被FINN支持")
|
|
print(" 3. 资源配置是否合理")
|
|
print("=" * 60)
|
|
raise
|