S100双目深度模型量化失败

尝试使用OE3.2量化 Nvidia发布的FoundationStereo模型
步骤如下:
生成配置文件:

hb_config_generator --full-yaml --model FSV2320.onnx --march nash-e

生成内容如下:

calibration_parameters:
  cal_data_dir: ./calibration_data_dir;./calibration_data_dir
  quant_config: {}
compiler_parameters:
  advice: 0
  balance_factor: 0
  cache_mode: disable
  cache_path: ''
  compile_mode: latency
  core_num: 1
  debug: true
  jobs: 16
  max_time_per_fc: 0
  optimize_level: O2
input_parameters:
  input_layout_train: NCHW;NCHW
  input_name: left_image;right_image
  input_shape: 1x3x320x736;1x3x320x736
  input_type_rt: nv12;nv12
  input_type_train: bgr;bgr
  mean_value: ''
  scale_value: ''
  separate_batch: false
  std_value: ''
model_parameters:
  debug_mode: ''
  march: nash-e
  onnx_model: FSV2320.onnx
  output_model_file_prefix: FSV2320
  output_nodes: ''
  remove_node_name: ''
  remove_node_type: ''
  working_dir: ./model_output

鉴于只是做通过性验证不追求精度删除了校准数据目录使用随机数据

量化命令

hb_compile --config full_compile_config.yaml

获得命令行输出如下:

2026-03-03 10:28:59,236 INFO The log will be stored in /Data/Work/S100FS00/hb_compile.log
2026-03-03 10:28:59,237 INFO Start hb_compile...
2026-03-03 10:28:59,238 INFO hbdk version: 4.2.11
2026-03-03 10:28:59,238 INFO hmct version: 2.4.1
2026-03-03 10:28:59,239 INFO hb_compile version: 3.3.22
2026-03-03 10:28:59,240 INFO Start verifying yaml
2026-03-03 10:29:01,041 INFO Using the onnx model file: /Data/Work/S100FS00/FSV2320.onnx
2026-03-03 10:29:01,043 INFO Model has 2 inputs according to model file
2026-03-03 10:29:01,046 WARNING Please note that the parameter norm_type has been deprecated and will be determined by the specification of the mean/scale/std parameters
2026-03-03 10:29:01,047 INFO The calibration dataset has not been specified, the calibration method is modified to skip
2026-03-03 10:29:01,048 WARNING The input node left_image's input_source is not be specified, it will be specified as None by default
2026-03-03 10:29:01,048 WARNING The input node right_image's input_source is not be specified, it will be specified as None by default
2026-03-03 10:29:01,050 INFO The parameter compile_mode is specified as latency, the balance_factor will be specified as 100.
2026-03-03 10:29:01,050 INFO End verifying yaml
2026-03-03 10:29:01,054 INFO Start to Horizon NN Model Convert.
2026-03-03 10:29:08,567 INFO Loading horizon_nn debug methods: set().
2026-03-03 10:29:08,572 INFO The specified model compilation architecture: nash-e.
2026-03-03 10:29:08,574 INFO The specified model compilation optimization parameters: [].
2026-03-03 10:29:08,575 INFO Start to prepare the onnx model.
2026-03-03 10:29:11,874 INFO Input ONNX Model Information:
IR Version:               8
Opset Version:            ['ai.onnx v17', 'horizon v1']
Model Producer:           pytorch v2.6.0
Model Domain:             None
Model Version:            None
Graph Input:
    left_image          : shape=[1, 3, 320, 736], dtype=FLOAT32
    ...t_image          : shape=[1, 3, 320, 736], dtype=FLOAT32
Graph Output:
    disparity           : shape=[1, 1, 320, 736], dtype=FLOAT32
2026-03-03 10:29:11,885 INFO The input model's opset version is 17, try to convert to opset version of 19.
2026-03-03 10:29:28,311 INFO End to prepare the onnx model.
2026-03-03 10:29:31,251 INFO Saving model to: /Data/Work/S100FS00/model_output/FSV2320_original_float_model.onnx.
2026-03-03 10:29:31,252 INFO Start to optimize the onnx model.
2026-03-03 10:34:40,802 INFO End to optimize the onnx model.
2026-03-03 10:34:55,048 INFO Saving model to: /Data/Work/S100FS00/model_output/FSV2320_optimized_float_model.onnx.
2026-03-03 10:34:55,051 INFO The activation calibration configuration:
    calibration_type:     fixed
2026-03-03 10:34:55,052 INFO Start to calibrate the model.
2026-03-03 10:34:55,848 INFO No calibration data provided, using random data.
2026-03-03 10:40:18,030 INFO Run calibration model with fixed thresholds method.
2026-03-03 10:43:07,417 INFO End to calibrate the model.
2026-03-03 10:43:21,592 INFO Saving model to: /Data/Work/S100FS00/model_output/FSV2320_calibrated_model.onnx.
2026-03-03 10:43:21,594 INFO Start to quantize the model.
2026-03-03 10:46:05,174 INFO End to quantize the model.
2026-03-03 10:46:18,368 INFO Saving model to: /Data/Work/S100FS00/model_output/FSV2320_ptq_model.onnx.
2026-03-03 10:46:20,479 INFO End to Horizon NN Model Convert.
2026-03-03 10:46:20,488 INFO Start to export model.
Convert Node onnx::Resize ver 19 failed!
Details of the node...
input: "/cost_agg/conv_patch/conv_patch.0/Conv_output_0_calibrated"
input: ""
input: "/cost_agg/Constant_output_0"
output: "/cost_agg/Resize_output_0"
name: "/cost_agg/Resize"
op_type: "Resize"
attribute {
  name: "coordinate_transformation_mode"
  s: "half_pixel"
  type: STRING
}
attribute {
  name: "cubic_coeff_a"
  f: -0.75
  type: FLOAT
}
attribute {
  name: "mode"
  s: "linear"
  type: STRING
}
attribute {
  name: "nearest_mode"
  s: "floor"
  type: STRING
}
doc_string: "/usr/local/lib/python3.12/dist-packages/torch/nn/functional.py(4693): interpolate\n/tao-pt/nvidia_tao_pytorch/cv/depth_net/model/stereo_depth/foundation_stereo/submodule.py(413): forward\n/usr/local/lib/python3.12/dist-packages/torch/nn/modules/module.py(1730): _slow_forward\n/usr/local/lib/python3.12/dist-packages/torch/nn/modules/module.py(1751): _call_impl\n/usr/local/lib/python3.12/dist-packages/torch/nn/modules/module.py(1740): _wrapped_call_impl\n/tao-pt/nvidia_tao_pytorch/cv/depth_net/model/stereo_depth/foundation_stereo/foundation_stereo.py(209): forward\n/usr/local/lib/python3.12/dist-packages/torch/nn/modules/module.py(1730): _slow_forward\n/usr/local/lib/python3.12/dist-packages/torch/nn/modules/module.py(1751): _call_impl\n/usr/local/lib/python3.12/dist-packages/torch/nn/modules/module.py(1740): _wrapped_call_impl\n/usr/local/lib/python3.12/dist-packages/torch/jit/_trace.py(129): wrapper\n/usr/local/lib/python3.12/dist-packages/torch/jit/_trace.py(138): forward\n/usr/local/lib/python3.12/dist-packages/torch/nn/modules/module.py(1751): _call_impl\n/usr/local/lib/python3.12/dist-packages/torch/nn/modules/module.py(1740): _wrapped_call_impl\n/usr/local/lib/python3.12/dist-packages/torch/jit/_trace.py(1498): _get_trace_graph\n/usr/local/lib/python3.12/dist-packages/torch/onnx/utils.py(844): _trace_and_get_graph_from_model\n/usr/local/lib/python3.12/dist-packages/torch/onnx/utils.py(937): _create_jit_graph\n/usr/local/lib/python3.12/dist-packages/torch/onnx/utils.py(1053): _model_to_graph\n/usr/local/lib/python3.12/dist-packages/torch/onnx/utils.py(1428): _export\n/usr/local/lib/python3.12/dist-packages/torch/onnx/utils.py(495): export\n/usr/local/lib/python3.12/dist-packages/torch/onnx/__init__.py(383): export\n/tao-pt/nvidia_tao_pytorch/cv/depth_net/scripts/export.py(196): stereo_onnx_export\n/tao-pt/nvidia_tao_pytorch/cv/depth_net/scripts/export.py(326): run_export\n/tao-pt/nvidia_tao_pytorch/cv/depth_net/scripts/export.py(62): main\n/tao-pt/nvidia_tao_pytorch/core/decorators/workflow.py(51): _func\n/usr/local/lib/python3.12/dist-packages/hydra/core/utils.py(186): run_job\n/usr/local/lib/python3.12/dist-packages/hydra/_internal/hydra.py(119): run\n/usr/local/lib/python3.12/dist-packages/hydra/_internal/utils.py(458): <lambda>\n/usr/local/lib/python3.12/dist-packages/hydra/_internal/utils.py(220): run_and_report\n/usr/local/lib/python3.12/dist-packages/hydra/_internal/utils.py(457): _run_app\n/usr/local/lib/python3.12/dist-packages/hydra/_internal/utils.py(394): _run_hydra\n/tao-pt/nvidia_tao_pytorch/core/hydra/hydra_runner.py(110): wrapper\n/tao-pt/nvidia_tao_pytorch/cv/depth_net/scripts/export.py(337): <module>\n"
domain: ""

2026-03-03 10:46:27,916 ERROR *** ERROR-OCCUR-DURING hbdk.export ***, error message:

hb_compile.log 内容如下:

2026-03-03 10:28:59,236 file: tool_utils.py func: tool_utils line No: 80 The log will be stored in /Data/Work/S100FS00/hb_compile.log
2026-03-03 10:28:59,237 file: hb_compile.py func: hb_compile line No: 280 Start hb_compile...
2026-03-03 10:28:59,238 file: hb_compile.py func: hb_compile line No: 281 hbdk version: 4.2.11
2026-03-03 10:28:59,238 file: hb_compile.py func: hb_compile line No: 282 hmct version: 2.4.1
2026-03-03 10:28:59,239 file: hb_compile.py func: hb_compile line No: 283 hb_compile version: 3.3.22
2026-03-03 10:28:59,240 file: hb_compile.py func: hb_compile line No: 33 Start verifying yaml
2026-03-03 10:28:59,247 file: params_parser.py func: params_parser line No: 1142 Using abs path /Data/Work/S100FS00/FSV2320.onnx
2026-03-03 10:29:01,041 file: params_parser.py func: params_parser line No: 177 Using the onnx model file: /Data/Work/S100FS00/FSV2320.onnx
2026-03-03 10:29:01,042 file: onnx_parser.py func: onnx_parser line No: 39 Model input names: ['left_image', 'right_image']
2026-03-03 10:29:01,043 file: params_parser.py func: params_parser line No: 188 Model has 2 inputs according to model file
2026-03-03 10:29:01,045 file: params_parser.py func: params_parser line No: 1142 Using abs path /Data/Work/S100FS00/model_output
2026-03-03 10:29:01,045 file: params_parser.py func: params_parser line No: 370 node_dict: {}
2026-03-03 10:29:01,046 file: params_parser.py func: params_parser line No: 647 Please note that the parameter norm_type has been deprecated and will be determined by the specification of the mean/scale/std parameters
2026-03-03 10:29:01,047 file: params_parser.py func: params_parser line No: 784 The calibration dataset has not been specified, the calibration method is modified to skip
2026-03-03 10:29:01,048 file: params_parser.py func: params_parser line No: 916 The input node left_image's input_source is not be specified, it will be specified as None by default
2026-03-03 10:29:01,048 file: params_parser.py func: params_parser line No: 916 The input node right_image's input_source is not be specified, it will be specified as None by default
2026-03-03 10:29:01,050 file: params_parser.py func: params_parser line No: 981 The parameter compile_mode is specified as latency, the balance_factor will be specified as 100.
2026-03-03 10:29:01,050 file: hb_compile.py func: hb_compile line No: 36 End verifying yaml
2026-03-03 10:29:01,051 file: wrap_utils.py func: wrap_utils line No: 40 Start to execute hmct.custom.op_registration.op_register.
2026-03-03 10:29:01,051 file: wrap_utils.py func: wrap_utils line No: 43 hmct.custom.op_registration.op_register input args ()
2026-03-03 10:29:01,052 file: wrap_utils.py func: wrap_utils line No: 46 hmct.custom.op_registration.op_register input kwargs {}
2026-03-03 10:29:01,052 file: wrap_utils.py func: wrap_utils line No: 50 End to execute hmct.custom.op_registration.op_register.
2026-03-03 10:29:01,053 file: wrap_utils.py func: wrap_utils line No: 40 Start to execute hmct.api.build_model.
2026-03-03 10:29:01,053 file: wrap_utils.py func: wrap_utils line No: 43 hmct.api.build_model input args ()
2026-03-03 10:29:01,053 file: wrap_utils.py func: wrap_utils line No: 46 hmct.api.build_model input kwargs {'params': {'cali_dict': {'calibration_type': ''}, 'input_dict': {'left_image': {'input_shape': [1, 3, 320, 736]}, 'right_image': {'input_shape': [1, 3, 320, 736]}}, 'node_dict': {}, 'debug_methods': [], 'output_nodes': [], 'optimization_methods': None, 'quant_config': {}, 'check_mode': True}}
2026-03-03 10:29:01,054 file: model_builder.py func: model_builder line No: 36 Start to Horizon NN Model Convert.
2026-03-03 10:29:08,567 file: model_debugger.py func: model_debugger line No: 67 Loading horizon_nn debug methods: set().
2026-03-03 10:29:08,572 file: model_builder.py func: model_builder line No: 230 The specified model compilation architecture: nash-e.
2026-03-03 10:29:08,574 file: model_builder.py func: model_builder line No: 244 The specified model compilation optimization parameters: [].
2026-03-03 10:29:08,575 file: model_builder.py func: model_builder line No: 36 Start to prepare the onnx model.
2026-03-03 10:29:11,874 file: display_model.py func: display_model line No: 69 Input ONNX Model Information:
IR Version:               8
Opset Version:            ['ai.onnx v17', 'horizon v1']
Model Producer:           pytorch v2.6.0
Model Domain:             None
Model Version:            None
Graph Input:
    left_image          : shape=[1, 3, 320, 736], dtype=FLOAT32
    ...t_image          : shape=[1, 3, 320, 736], dtype=FLOAT32
Graph Output:
    disparity           : shape=[1, 1, 320, 736], dtype=FLOAT32
2026-03-03 10:29:11,885 file: preprocess_model.py func: preprocess_model line No: 380 The input model's opset version is 17, try to convert to opset version of 19.
2026-03-03 10:29:28,311 file: model_builder.py func: model_builder line No: 39 End to prepare the onnx model.
2026-03-03 10:29:31,251 file: model_builder.py func: model_builder line No: 287 Saving model to: /Data/Work/S100FS00/model_output/FSV2320_original_float_model.onnx.
2026-03-03 10:29:31,252 file: model_builder.py func: model_builder line No: 36 Start to optimize the onnx model.
2026-03-03 10:33:55,439 file: constant_folding.py func: constant_folding line No: 71 Summary info for constant folding:
2026-03-03 10:33:55,440 file: constant_folding.py func: constant_folding line No: 72   After constant folding, the number of nodes has changed from 34003 to 9511.
2026-03-03 10:33:55,441 file: constant_folding.py func: constant_folding line No: 76   After constant_folding, the number of parameters has changed from 122435652 to 331860857.
2026-03-03 10:33:55,442 file: constant_folding.py func: constant_folding line No: 81 Detailed info for constant folding:
2026-03-03 10:33:55,443 file: constant_folding.py func: constant_folding line No: 88
2026-03-03 10:34:03,444 file: constant_folding.py func: constant_folding line No: 71 Summary info for constant folding:
2026-03-03 10:34:03,445 file: constant_folding.py func: constant_folding line No: 72   After constant folding, the number of nodes has changed from 9413 to 9413.
2026-03-03 10:34:03,446 file: constant_folding.py func: constant_folding line No: 76   After constant_folding, the number of parameters has changed from 331892593 to 331892593.
2026-03-03 10:34:03,447 file: constant_folding.py func: constant_folding line No: 81 Detailed info for constant folding:
2026-03-03 10:34:03,447 file: constant_folding.py func: constant_folding line No: 88
2026-03-03 10:34:09,331 file: constant_folding.py func: constant_folding line No: 71 Summary info for constant folding:
2026-03-03 10:34:09,332 file: constant_folding.py func: constant_folding line No: 72   After constant folding, the number of nodes has changed from 8888 to 8888.
2026-03-03 10:34:09,333 file: constant_folding.py func: constant_folding line No: 76   After constant_folding, the number of parameters has changed from 331893210 to 331893210.
2026-03-03 10:34:09,333 file: constant_folding.py func: constant_folding line No: 81 Detailed info for constant folding:
2026-03-03 10:34:09,333 file: constant_folding.py func: constant_folding line No: 88
2026-03-03 10:34:15,159 file: constant_folding.py func: constant_folding line No: 71 Summary info for constant folding:
2026-03-03 10:34:15,161 file: constant_folding.py func: constant_folding line No: 72   After constant folding, the number of nodes has changed from 8532 to 8532.
2026-03-03 10:34:15,162 file: constant_folding.py func: constant_folding line No: 76   After constant_folding, the number of parameters has changed from 331884282 to 331884282.
2026-03-03 10:34:15,162 file: constant_folding.py func: constant_folding line No: 81 Detailed info for constant folding:
2026-03-03 10:34:15,163 file: constant_folding.py func: constant_folding line No: 88
2026-03-03 10:34:22,744 file: constant_folding.py func: constant_folding line No: 71 Summary info for constant folding:
2026-03-03 10:34:22,746 file: constant_folding.py func: constant_folding line No: 72   After constant folding, the number of nodes has changed from 8481 to 8481.
2026-03-03 10:34:22,747 file: constant_folding.py func: constant_folding line No: 76   After constant_folding, the number of parameters has changed from 331874498 to 331874498.
2026-03-03 10:34:22,748 file: constant_folding.py func: constant_folding line No: 81 Detailed info for constant folding:
2026-03-03 10:34:22,748 file: constant_folding.py func: constant_folding line No: 88
2026-03-03 10:34:40,802 file: model_builder.py func: model_builder line No: 39 End to optimize the onnx model.
2026-03-03 10:34:55,048 file: model_builder.py func: model_builder line No: 287 Saving model to: /Data/Work/S100FS00/model_output/FSV2320_optimized_float_model.onnx.
2026-03-03 10:34:55,051 file: quant_config.py func: quant_config line No: 295 The activation calibration configuration:
    calibration_type:     fixed
2026-03-03 10:34:55,052 file: model_builder.py func: model_builder line No: 36 Start to calibrate the model.
2026-03-03 10:34:55,845 file: dataset.py func: dataset line No: 128 input name: left_image, number_of_samples: 1
2026-03-03 10:34:55,847 file: dataset.py func: dataset line No: 128 input name: right_image, number_of_samples: 1
2026-03-03 10:34:55,848 file: calibrate.py func: calibrate line No: 38 No calibration data provided, using random data.
2026-03-03 10:40:18,030 file: fixed_calibrater.py func: fixed_calibrater line No: 33 Run calibration model with fixed thresholds method.
2026-03-03 10:43:07,417 file: model_builder.py func: model_builder line No: 39 End to calibrate the model.
2026-03-03 10:43:21,592 file: model_builder.py func: model_builder line No: 287 Saving model to: /Data/Work/S100FS00/model_output/FSV2320_calibrated_model.onnx.
2026-03-03 10:43:21,594 file: model_builder.py func: model_builder line No: 36 Start to quantize the model.
2026-03-03 10:46:05,174 file: model_builder.py func: model_builder line No: 39 End to quantize the model.
2026-03-03 10:46:18,368 file: model_builder.py func: model_builder line No: 287 Saving model to: /Data/Work/S100FS00/model_output/FSV2320_ptq_model.onnx.
2026-03-03 10:46:20,479 file: model_builder.py func: model_builder line No: 39 End to Horizon NN Model Convert.
2026-03-03 10:46:20,487 file: wrap_utils.py func: wrap_utils line No: 50 End to execute hmct.api.build_model.
2026-03-03 10:46:20,488 file: hbm_builder.py func: hbm_builder line No: 275 Start to export model.
2026-03-03 10:46:20,489 file: wrap_utils.py func: wrap_utils line No: 40 Start to execute hbdk.export.
2026-03-03 10:46:20,489 file: wrap_utils.py func: wrap_utils line No: 43 hbdk.export input args ()
2026-03-03 10:46:20,490 file: wrap_utils.py func: wrap_utils line No: 46 hbdk.export input kwargs {'output_prefix_name': 'FSV2320'}
2026-03-03 10:46:27,912 file: tool_utils.py func: tool_utils line No: 138 Exception in command: main
2026-03-03 10:46:27,915 file: tool_utils.py func: tool_utils line No: 139 Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/horizon_tc_ui/utils/wrap_utils.py", line 49, in wrapper
    result = func(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/horizon_tc_ui/hbir_handle.py", line 57, in export_hbir
    return export(proto=ptq_model, name=output_prefix_name)
  File "/usr/local/lib/python3.10/dist-packages/hbdk4/compiler/onnx/__init__.py", line 42, in export
    graph.emit_mlir_func_op(OpConvertorRegistry(), True)
  File "/usr/local/lib/python3.10/dist-packages/hbdk4/compiler/frontend/adaptor.py", line 358, in emit_mlir_func_op
    node_adaptor.emit_mlir_op(registry)
  File "/usr/local/lib/python3.10/dist-packages/hbdk4/compiler/frontend/adaptor.py", line 210, in emit_mlir_op
    raise e
  File "/usr/local/lib/python3.10/dist-packages/hbdk4/compiler/frontend/adaptor.py", line 195, in emit_mlir_op
    results = cvt.emit_mlir_op(self, *args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/hbdk4/compiler/onnx/opset18.py", line 200, in emit_mlir_op
    assert len(input_shape) == 4
AssertionError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/horizon_tc_ui/utils/tool_utils.py", line 136, in __decorator
    func(*args, **kargs)
  File "/usr/local/lib/python3.10/dist-packages/horizon_tc_ui/hb_compile.py", line 300, in main
    onnx_config_mode(config, skip=skip)
  File "/usr/local/lib/python3.10/dist-packages/horizon_tc_ui/hb_compile.py", line 214, in onnx_config_mode
    hbm_build(conf, ptq_model, skip=skip)
  File "/usr/local/lib/python3.10/dist-packages/horizon_tc_ui/hb_compile.py", line 125, in hbm_build
    hbm_builder.build()
  File "/usr/local/lib/python3.10/dist-packages/horizon_tc_ui/compile/hbm_builder.py", line 332, in build
    self.export_model()
  File "/usr/local/lib/python3.10/dist-packages/horizon_tc_ui/compile/hbm_builder.py", line 276, in export_model
    self.float_hbir = HBIRHandle.export_hbir(
  File "/usr/local/lib/python3.10/dist-packages/horizon_tc_ui/utils/wrap_utils.py", line 54, in wrapper
    raise ValueError(f"*** ERROR-OCCUR-DURING {module_info} ***," +
ValueError: *** ERROR-OCCUR-DURING hbdk.export ***, error message:

2026-03-03 10:46:27,916 file: tool_utils.py func: tool_utils line No: 140 *** ERROR-OCCUR-DURING hbdk.export ***, error message:

OE3.2是在工具链Docker镜像ai_toolchain_ubuntu_22_s100_cpu_v3.2.0中运行的
FSV2320.onnx对应了 Nvidia模型发布页面的deployable_foundationstereo_small_320x736_v2.0.onnx

File "/usr/local/lib/python3.10/dist-packages/hbdk4/compiler/onnx/opset18.py", line 200, in emit_mlir_op
    assert len(input_shape) == 4
AssertionError

BPU内部Resize节点不支持除4维之外的维度,估测此模型为5D,建议使用CPU运行此node

请问在哪里配置?我在OE文档里没找到 ~~

calibration_parameters 里面配置 quant_config: 和 run_on_cpu:

额 目前的OE文档似乎没有对run_on_cpu的配置说明啊~~
我只需要配置这一个么?还是quant_config这边也需要做配置?

我在配置文件中添加了run_on_cpu

calibration_parameters:
  quant_config: {}
  run_on_cpu: '/cost_agg/Resize'

对应编译日志中出现

2026-03-03 12:10:57,305 file: info_dict.py func: info_dict line No: 146 The quantization type configuration of the model:
===========================
Name              qtype    
---------------------------
/cost_agg/Resize  float32

但是最终还是报错了

2026-03-03 12:21:23,015 file: wrap_utils.py func: wrap_utils line No: 46 hbdk.export input kwargs {'output_prefix_name': 'FSV2320'}
2026-03-03 12:21:25,739 file: tool_utils.py func: tool_utils line No: 138 Exception in command: main
2026-03-03 12:21:25,745 file: tool_utils.py func: tool_utils line No: 139 Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/horizon_tc_ui/utils/wrap_utils.py", line 49, in wrapper
    result = func(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/horizon_tc_ui/hbir_handle.py", line 57, in export_hbir
    return export(proto=ptq_model, name=output_prefix_name)
  File "/usr/local/lib/python3.10/dist-packages/hbdk4/compiler/onnx/__init__.py", line 42, in export
    graph.emit_mlir_func_op(OpConvertorRegistry(), True)
  File "/usr/local/lib/python3.10/dist-packages/hbdk4/compiler/frontend/adaptor.py", line 358, in emit_mlir_func_op
    node_adaptor.emit_mlir_op(registry)
  File "/usr/local/lib/python3.10/dist-packages/hbdk4/compiler/frontend/adaptor.py", line 210, in emit_mlir_op
    raise e
  File "/usr/local/lib/python3.10/dist-packages/hbdk4/compiler/frontend/adaptor.py", line 195, in emit_mlir_op
    results = cvt.emit_mlir_op(self, *args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/hbdk4/compiler/onnx/opset18.py", line 200, in emit_mlir_op
    assert len(input_shape) == 4
AssertionError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/horizon_tc_ui/utils/tool_utils.py", line 136, in __decorator
    func(*args, **kargs)
  File "/usr/local/lib/python3.10/dist-packages/horizon_tc_ui/hb_compile.py", line 300, in main
    onnx_config_mode(config, skip=skip)
  File "/usr/local/lib/python3.10/dist-packages/horizon_tc_ui/hb_compile.py", line 214, in onnx_config_mode
    hbm_build(conf, ptq_model, skip=skip)
  File "/usr/local/lib/python3.10/dist-packages/horizon_tc_ui/hb_compile.py", line 125, in hbm_build
    hbm_builder.build()
  File "/usr/local/lib/python3.10/dist-packages/horizon_tc_ui/compile/hbm_builder.py", line 332, in build
    self.export_model()
  File "/usr/local/lib/python3.10/dist-packages/horizon_tc_ui/compile/hbm_builder.py", line 276, in export_model
    self.float_hbir = HBIRHandle.export_hbir(
  File "/usr/local/lib/python3.10/dist-packages/horizon_tc_ui/utils/wrap_utils.py", line 54, in wrapper
    raise ValueError(f"*** ERROR-OCCUR-DURING {module_info} ***," +
ValueError: *** ERROR-OCCUR-DURING hbdk.export ***, error message: 

2026-03-03 12:21:25,746 file: tool_utils.py func: tool_utils line No: 140 *** ERROR-OCCUR-DURING hbdk.export ***, error message:

那说明工具链并不支持5维的resize算子,需要在模型算法层面做对应修改,降低维度

从当前对话和错误日志来看,问题的核心是 BPU 不支持 5 维的 Resize 算子,而模型中存在一个维度为 5 的 Resize 节点(/cost_agg/Resize),导致在导出阶段触发了 assert len(input_shape) == 4 的断言失败。

Marcelo 已经指出:工具链不支持 5 维 Resize,需要在模型算法层面做对应修改,降低维度。


:white_check_mark: 解决方案总结

:red_exclamation_mark:根本原因

  • 模型中存在一个 Resize 节点,其输入张量维度为 5(如 [1, 1, 1, 320, 736] 或类似),但 BPU 的 Resize 算子只支持 4 维输入。
  • 即使你通过 run_on_cpu: '/cost_agg/Resize' 尝试让该节点在 CPU 上运行,导出阶段仍然会尝试在 BPU 上编译它,导致断言失败 —— 因为导出流程是统一的,不会因为 run_on_cpu 而跳过节点验证。

:hammer_and_wrench: 推荐解决方案

:white_check_mark: 方案一:修改模型结构,避免 5 维 Resize(推荐)

这是最根本、最可靠的解决方式:

  1. 定位到模型中使用 Resize 的地方(通常是 /cost_agg/Resize)。
  2. 检查该 Resize 节点的输入张量维度是否为 5
    • 如果是,说明模型在 PyTorch 中可能做了 unsqueezereshape 操作,导致维度变多。
  3. 在模型前向传播中,手动将输入维度降维为 4
    # 示例:假设输入是 [B, C, H, W, D],改为 [B, C, H, W]
    x = x.squeeze(-1)  # 移除最后一维
    # 或者
    x = x.permute(0, 1, 4, 2, 3).contiguous()  # 重新排列维度,再 squeeze
    
  4. 重新导出 ONNX 模型,确保所有 Resize 输入都是 4 维。
  5. 再次使用 OE3.2 量化编译,应该不会再报错。

:light_bulb: 提示:你可以用 onnxruntimenetron 可视化模型结构,找到 /cost_agg/Resize 节点并查看其输入形状。


:white_check_mark: 方案二:如果无法修改模型,可尝试绕过 Resize 节点(不推荐)

如果你不能修改模型源码,可以尝试:

  1. 在 ONNX 模型中删除或替换 Resize 节点(需谨慎,可能破坏模型功能)。
  2. 使用 ONNX 转换工具(如 onnx-simplifier)简化模型结构
    pip install onnx-simplifier
    onnxsim input.onnx output.onnx
    
  3. 再用 hb_compile 编译

:warning: 注意:这可能会导致精度下降或推理失败,仅作为临时 workaround。


:white_check_mark: 方案三:确认 run_on_cpu 配置是否生效

虽然 Marcelo 提供了配置示例,但根据 RoyLi 的后续测试,即使配置了:

calibration_parameters:
  run_on_cpu: '/cost_agg/Resize'

导出阶段仍然报错,说明:

  • run_on_cpu 只影响 量化过程中的算子执行位置不影响导出时的节点校验逻辑
  • 导出阶段仍会尝试将整个模型(包括 CPU 运行的节点)转换为 BPU 可执行格式,因此仍会遇到维度不匹配的问题。

:magnifying_glass_tilted_left: 补充建议

1. 查看模型原始结构

你可以用以下命令查看 ONNX 模型结构:

pip install onnxruntime netron
onnxsim FSV2320.onnx FSV2320_sim.onnx
netron FSV2320_sim.onnx

在 Netron 中搜索 /cost_agg/Resize,查看其输入张量的 shape。

2. 检查模型导出代码

如果你是从 PyTorch 导出 ONNX 的,检查是否有如下操作:

# 错误示例:添加了不必要的维度
x = x.unsqueeze(1)  # 导致维度变成 [B, 1, C, H, W]
# 正确做法:保持 [B, C, H, W]

或者:

# 使用 torch.nn.functional.interpolate 时,参数设置不当
output = F.interpolate(x, size=(H, W), mode='bilinear', align_corners=False)
# 确保输入是 4D,输出也是 4D

:pushpin: 结论

最有效的解决方案是:在模型算法层面修改,确保 Resize 节点的输入维度为 4 维。
run_on_cpu 配置无法解决导出阶段的维度断言错误,因为它只影响执行位置,不影响编译时的节点校验。


:puzzle_piece: 如果你希望我帮你进一步分析模型结构

你可以提供:

  1. FSV2320.onnx 的结构截图(Netron 截图)
  2. 模型导出时的 PyTorch 代码片段(特别是 Resize 相关部分)
  3. 是否有自定义模块或子网络导致维度变化?

我可以帮你具体定位并修改代码。


:white_check_mark: 最终建议操作步骤:

  1. 用 Netron 打开 FSV2320.onnx,找到 /cost_agg/Resize 节点。
  2. 查看其输入张量的 shape。
  3. 如果是 5 维 → 修改 PyTorch 模型代码,确保输入是 4 维。
  4. 重新导出 ONNX。
  5. 再次运行 hb_compile

这样就能彻底解决这个问题!


如有需要,我可以为你生成一个修改后的 PyTorch 模型代码模板 :backhand_index_pointing_down:

是否需要?