""" FINN模型转换脚本 功能: 1. 将ONNX模型转换为FINN可部署格式 2. 配置量化参数 3. 生成FPGA加速器 4. 生成PYNQ overlay 注意:此脚本需要在FINN Docker容器中运行 """ import os import sys import argparse import json from pathlib import Path def configure_stdio(): """Avoid Windows console failures on check/warning symbols.""" for stream in (sys.stdout, sys.stderr): if hasattr(stream, "reconfigure"): stream.reconfigure(encoding="utf-8", errors="replace") configure_stdio() DEFAULT_BOARD = "Pynq-Z2" DEFAULT_XILINX_VERSION = "2020.2" DEFAULT_PYNQ_VERSION = "2.7" DEFAULT_TARGET_FPS = 1 DEFAULT_FINN_IMAGE = "xilinx/finn:latest" SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) PROJECT_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, '..')) def _as_posix(path): return path.replace(os.sep, '/').replace('\\', '/') def _container_path(host_path): """Map a host project path to the FINN Docker /workspace mount.""" host_path = os.path.abspath(host_path) try: rel_path = os.path.relpath(host_path, PROJECT_ROOT) except ValueError: return _as_posix(host_path) if rel_path.startswith('..'): return _as_posix(host_path) return '/workspace/' + _as_posix(rel_path) def resolve_existing_project_path(raw_path): """Resolve inputs robustly whether called from project root or finn_deployment.""" if os.path.isabs(raw_path): return os.path.abspath(raw_path) candidates = [ os.path.abspath(os.path.join(os.getcwd(), raw_path)), os.path.abspath(os.path.join(SCRIPT_DIR, raw_path)), os.path.abspath(os.path.join(PROJECT_ROOT, raw_path)), ] for candidate in candidates: if os.path.exists(candidate): return candidate return candidates[1] def resolve_output_project_path(raw_path): """Resolve output path to the project-mounted directory used by FINN.""" if os.path.isabs(raw_path): return os.path.abspath(raw_path) norm = raw_path.replace('\\', '/') if norm.startswith('./'): norm_no_dot = norm[2:] else: norm_no_dot = norm if norm_no_dot.startswith('finn_deployment/'): return os.path.abspath(os.path.join(PROJECT_ROOT, norm_no_dot)) if norm.startswith('../'): return os.path.abspath(os.path.join(SCRIPT_DIR, raw_path)) return os.path.abspath(os.path.join(SCRIPT_DIR, raw_path)) def create_finn_config(args): """ 创建FINN配置文件 参数: args: 命令行参数 返回: config: 配置字典 """ model_host_path = resolve_existing_project_path(args.model) output_host_path = resolve_output_project_path(args.output) config = { # 模型配置 "model": { "onnx_path": _container_path(model_host_path), "host_onnx_path": model_host_path, "input_shape": [1, 3, args.input_size, args.input_size], "num_classes": args.num_classes }, # 目标平台配置 "platform": { "board": args.board, "part": "xc7z020clg400-1", # PYNQ Z2的FPGA型号 "clock_period_ns": 10.0, # 100MHz时钟 "pynq_version": DEFAULT_PYNQ_VERSION, "xilinx_version": args.xilinx_version, }, # 量化配置 "quantization": { "weight_width": args.weight_bits, "activation_width": args.activation_bits, "input_width": 8, # 输入图像使用8位 "accumulator_width": 32 # 累加器使用32位 }, # 性能配置 "performance": { "target_fps": args.target_fps, "folding_config": "auto", # 自动配置并行度 "mem_mode": "decoupled" # 解耦内存模式 }, # 优化选项 "optimization": { "streamline": True, # 流水线优化 "fold_constants": True, # 常量折叠 "remove_unused": True, # 移除未使用的节点 "merge_ops": True # 合并算子 }, # 输出配置 "output": { "output_dir": _container_path(output_host_path), "host_output_dir": output_host_path, "generate_driver": True, # 生成Python驱动 "generate_report": True, # 生成报告 "save_intermediate": args.save_intermediate # 保存中间结果 }, # 主机构建环境配置 "host": { "finn_image": args.finn_image, "xilinx_path": args.xilinx_path, "xilinx_version": args.xilinx_version, } } return config def generate_finn_script(config, output_path): """ 生成FINN转换脚本 参数: config: 配置字典 output_path: 输出路径 """ script_content = f'''#!/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 = {json.dumps(config, indent=4)} # 创建输出目录 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 ''' # 保存脚本 with open(output_path, 'w', encoding='utf-8', newline='\n') as f: f.write(script_content) # 设置可执行权限 os.chmod(output_path, 0o755) print(f"✓ FINN转换脚本已生成: {output_path}") def generate_docker_command(args): """ 生成Docker运行命令 参数: args: 命令行参数 返回: command: Docker命令字符串 """ # 构建可直接执行的Linux/VMware脚本。Windows版Vivado不能直接给Linux容器调用。 command = f'''#!/usr/bin/env bash set -euo pipefail # FINN Docker运行脚本 - PYNQ-Z2 / PYNQ v{DEFAULT_PYNQ_VERSION} / Xilinx {args.xilinx_version} # 在VMware/Linux中运行。Windows安装版Vivado不可直接给Linux容器调用。 SCRIPT_DIR="$(cd "$(dirname "${{BASH_SOURCE[0]}}")" && pwd)" PROJECT_ROOT="$(cd "${{SCRIPT_DIR}}/../.." && pwd)" FINN_IMAGE="${{FINN_DOCKER_IMAGE:-{args.finn_image}}}" FINN_XILINX_PATH="${{FINN_XILINX_PATH:-{args.xilinx_path}}}" FINN_XILINX_VERSION="${{FINN_XILINX_VERSION:-{args.xilinx_version}}}" if ! command -v docker >/dev/null 2>&1; then echo "ERROR: docker not found. Install Docker in VMware/Linux first." >&2 exit 2 fi if [ ! -d "${{FINN_XILINX_PATH}}/Vivado/${{FINN_XILINX_VERSION}}" ]; then echo "ERROR: Vivado ${{FINN_XILINX_VERSION}} not found under ${{FINN_XILINX_PATH}}." >&2 echo "Set FINN_XILINX_PATH to the Linux Xilinx install root, for example:" >&2 echo " export FINN_XILINX_PATH=/opt/Xilinx" >&2 echo " export FINN_XILINX_VERSION={args.xilinx_version}" >&2 echo "A Windows path such as F:\\\\ZYNQ_ is not executable inside a Linux FINN container." >&2 exit 2 fi if [ ! -f "${{FINN_XILINX_PATH}}/Vivado/${{FINN_XILINX_VERSION}}/settings64.sh" ]; then echo "ERROR: settings64.sh not found for Vivado ${{FINN_XILINX_VERSION}}." >&2 exit 2 fi echo "Project root: ${{PROJECT_ROOT}}" echo "FINN image: ${{FINN_IMAGE}}" echo "Xilinx tools: ${{FINN_XILINX_PATH}}/${{FINN_XILINX_VERSION}}" echo "Target: PYNQ-Z2, PYNQ v{DEFAULT_PYNQ_VERSION}, board {args.board}" docker run -it --rm \\ -v "${{PROJECT_ROOT}}:/workspace" \\ -v "${{FINN_XILINX_PATH}}:${{FINN_XILINX_PATH}}" \\ -e FINN_XILINX_PATH="${{FINN_XILINX_PATH}}" \\ -e FINN_XILINX_VERSION="${{FINN_XILINX_VERSION}}" \\ -e XILINXD_LICENSE_FILE="${{XILINXD_LICENSE_FILE:-}}" \\ -e LM_LICENSE_FILE="${{LM_LICENSE_FILE:-}}" \\ -w /workspace/finn_deployment \\ "${{FINN_IMAGE}}" \\ bash -lc 'set -euo pipefail source "$FINN_XILINX_PATH/Vivado/$FINN_XILINX_VERSION/settings64.sh" if [ -f "$FINN_XILINX_PATH/Vitis_HLS/$FINN_XILINX_VERSION/settings64.sh" ]; then source "$FINN_XILINX_PATH/Vitis_HLS/$FINN_XILINX_VERSION/settings64.sh" fi vivado -version if command -v vitis_hls >/dev/null 2>&1; then vitis_hls -version || true; fi python3 finn_build.py ' echo "FINN build finished. Check: ${{PROJECT_ROOT}}/finn_deployment/output" ls -lh "${{PROJECT_ROOT}}/finn_deployment/output" || true ''' return command def main(): """主函数""" parser = argparse.ArgumentParser(description='FINN模型转换工具') # 模型参数 parser.add_argument('--model', type=str, default='../data/models/finn_qcnn_finn.onnx', help='ONNX模型路径') parser.add_argument('--num_classes', type=int, default=10, help='分类类别数') # 平台参数 parser.add_argument('--input_size', type=int, default=128, help='FINN QCNN input image size') parser.add_argument('--board', type=str, default=DEFAULT_BOARD, help='目标板卡') # 量化参数 parser.add_argument('--weight_bits', type=int, default=8, help='权重量化位宽') parser.add_argument('--activation_bits', type=int, default=8, help='激活量化位宽') # 性能参数 parser.add_argument('--target_fps', type=int, default=DEFAULT_TARGET_FPS, help='目标帧率') # 输出参数 parser.add_argument('--output', type=str, default='./output', help='输出目录') parser.add_argument('--save_intermediate', action='store_true', help='保存中间结果') # 主机工具链参数 parser.add_argument('--xilinx_path', type=str, default=os.environ.get('FINN_XILINX_PATH', '/opt/Xilinx'), help='Linux中的Xilinx工具安装根目录') parser.add_argument('--xilinx_version', type=str, default=os.environ.get('FINN_XILINX_VERSION', DEFAULT_XILINX_VERSION), help='Xilinx工具版本') parser.add_argument('--finn_image', type=str, default=os.environ.get('FINN_DOCKER_IMAGE', DEFAULT_FINN_IMAGE), help='FINN Docker镜像') args = parser.parse_args() output_host_path = resolve_output_project_path(args.output) print("=" * 60) print("FINN模型转换配置生成器") print("=" * 60) # 创建输出目录 os.makedirs(output_host_path, exist_ok=True) # 1. 创建配置 print("\n1. 生成FINN配置...") config = create_finn_config(args) # 保存配置文件 config_path = os.path.join(output_host_path, 'finn_config.json') with open(config_path, 'w', encoding='utf-8') as f: json.dump(config, indent=4, fp=f) print(f" ✓ 配置文件: {config_path}") # 2. 生成转换脚本 print("\n2. 生成FINN转换脚本...") script_path = os.path.join(os.path.dirname(__file__), 'finn_build.py') generate_finn_script(config, script_path) # 3. 生成Docker命令 print("\n3. 生成Docker运行命令...") docker_cmd = generate_docker_command(args) docker_cmd_path = os.path.join(output_host_path, 'run_docker.sh') with open(docker_cmd_path, 'w', encoding='utf-8', newline='\n') as f: f.write(docker_cmd) os.chmod(docker_cmd_path, 0o755) print(f" ✓ Docker命令: {docker_cmd_path}") # 4. 打印使用说明 print("\n" + "=" * 60) print("配置生成完成!") print("=" * 60) print("\n配置摘要:") print(f" 模型: {args.model}") print(f" 容器模型路径: {config['model']['onnx_path']}") print(f" 板卡: {args.board}") print(f" PYNQ: v{DEFAULT_PYNQ_VERSION}") print(f" Xilinx工具链: {args.xilinx_version}") print(f" 量化: W{args.weight_bits}A{args.activation_bits}") print(f" 目标FPS: {args.target_fps}") print(f" 输出: {output_host_path}") print(f" 容器输出路径: {config['output']['output_dir']}") print("\n下一步操作:") print(" 1. 确保ONNX模型已生成") print(" 2. 安装Docker并拉取FINN镜像") print(f" 3. 在VMware/Linux中设置: export FINN_XILINX_PATH=/opt/Xilinx") print(f" 4. 设置: export FINN_XILINX_VERSION={args.xilinx_version}") print(f" 5. 运行: bash {docker_cmd_path}") print(" 6. 或进入FINN容器后手动运行: python3 finn_build.py") print("\n预计时间:") print(" - 模型转换: 30-60分钟") print(" - Vivado综合: 1-2小时") print(" - 总计: 2-3小时") print("\n" + "=" * 60) if __name__ == '__main__': main()