Starting Hailo allocation and compilation flow stuck after quantization

I’m trying to convert a model from onnx to hef. During the conversion, it gets stuck at the Starting Hailo allocation and compilation flow. It’s been stuck for over an hour and won’t run any further. Could you help me figure out what’s wrong? Thank you very much. The code and output are as follows:
code:

import hailo_sdk_client
from hailo_sdk_client import ClientRunner
import numpy as np
import os
import logging
import traceback

# 设置日志级别为 DEBUG 以获取更多信息
logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler('hailo_quantization.log'),
        logging.StreamHandler()
    ]
)

# --- 1. Hailo 硬件架构 ---
chosen_hw_arch = "hailo8"

# --- 2. 模型路径和文件名 ---
onnx_model_name = "head_singleframe"
onnx_path = "/home/ubuntu/ycy/head_singleframe.onnx"
output_dir = "/home/ubuntu/ycy/onnx_models/hailo_outputs"
hailo_model_har_path = os.path.join(output_dir, f"{onnx_model_name}_hailo_model.har")
hailo_quantized_har_path = os.path.join(output_dir, f"{onnx_model_name}_hailo_quantized_model.har")
hailo_model_hef_path = os.path.join(output_dir, f"{onnx_model_name}.hef")

# --- 3. 确保输出目录存在 ---
os.makedirs(output_dir, exist_ok=True)

# --- 4. 初始化 Hailo ClientRunner ---
try:
    runner = ClientRunner(hw_arch=chosen_hw_arch)
    print(f"✅ Initialized ClientRunner with hardware architecture: {chosen_hw_arch}")
except Exception as e:
    print(f"❌ Failed to initialize ClientRunner: {e}")
    exit(1)

# --- 5. 验证 ONNX 模型文件存在 ---
if not os.path.exists(onnx_path):
    print(f"❌ ONNX model file not found: {onnx_path}")
    exit(1)
print(f"✅ ONNX model file found: {onnx_path}")

# --- 6. 指定 ONNX 模型的多个输入节点(去掉 time_interval)---
net_input_shapes = {
    "inst_dfg0": (1, 900, 512),
    "anchor": (1, 900, 11),
    "anchor_embed": (1, 900, 256),
    # 移除 time_interval
}

# --- 7. 转换 ONNX 模型为 HAR ---
try:
    hn, npz = runner.translate_onnx_model(
        model=onnx_path,
        net_name=onnx_model_name,
        net_input_shapes=net_input_shapes,
        start_node_names=list(net_input_shapes.keys())
    )
    print(f"✅ Successfully translated ONNX model to HAR")
except Exception as e:
    print(f"❌ Failed to translate ONNX model to HAR: {e}")
    traceback.print_exc()
    exit(1)

# --- 8. 保存 HAR 模型 ---
try:
    runner.save_har(hailo_model_har_path)
    print(f"✅ HAR model saved to: {hailo_model_har_path}")
except Exception as e:
    print(f"❌ Failed to save HAR model: {e}")
    exit(1)

# --- 9. 校准数据集准备(移除 time_interval) ---
def print_activation_ranges(model_layers):
    """
    打印每个激活层的数值范围
    """
    for layer in model_layers:
        print(f"{layer}: min={np.min(layer)}, max={np.max(layer)}")

def generate_calibration_dataset(num_samples: int = 200):  # 增加样本数量
    calib_data_path = "/home/ubuntu/ycy/calib_data/calib_data1.npz"
    
    hailo_input_names_mapping = {
        'inst_dfg0': 'head_singleframe/input_layer1',
        'anchor': 'head_singleframe/input_layer2',
        'anchor_embed': 'head_singleframe/input_layer3',
    }
    
    if os.path.exists(calib_data_path):
        print(f"✅ Loading real calibration data from {calib_data_path}")
        data = np.load(calib_data_path, allow_pickle=True)
        calib_data = data["calib_data"][:num_samples]

        inst_dfg0_array = np.stack([np.nan_to_num(s["inst_dfg0"], nan=0.0, posinf=0.0, neginf=0.0) / 10.0 for s in calib_data]).astype(np.float32)
        anchor_array = np.stack([np.nan_to_num(s["anchor"], nan=0.0, posinf=0.0, neginf=0.0) / 100.0 for s in calib_data]).astype(np.float32)
        anchor_embed_array = np.stack([np.nan_to_num(s["anchor_embed"], nan=0.0, posinf=0.0, neginf=0.0) / 10.0 for s in calib_data]).astype(np.float32)
        
        print(f"✅ Loaded {num_samples} real calibration samples.")

        # 返回 NumPy 字典
        calib_dataset = {
            hailo_input_names_mapping['inst_dfg0']: inst_dfg0_array,
            hailo_input_names_mapping['anchor']: anchor_array,
            hailo_input_names_mapping['anchor_embed']: anchor_embed_array,
        }
        
        print(f"✅ Generated calibration dataset shapes:")
        for name, data in calib_dataset.items():
            print(f"  - {name}: {data.shape}")
        
        # 打印激活范围(调试阶段)
        print_activation_ranges(calib_dataset.values())

        return calib_dataset

    else:
        print(f"⚠️ Calibration data not found at {calib_data_path}, generating random data")
        
        random_data_dict = {
            hailo_input_names_mapping['inst_dfg0']: np.random.randn(num_samples, 1, 900, 512).astype(np.float32) / 10.0,
            hailo_input_names_mapping['anchor']: np.random.randn(num_samples, 1, 900, 11).astype(np.float32) / 100.0,
            hailo_input_names_mapping['anchor_embed']: np.random.randn(num_samples, 1, 900, 256).astype(np.float32) / 10.0,
        }

        print(f"✅ Generated random dataset shapes:")
        for name, data in random_data_dict.items():
            print(f"  - {name}: {data.shape}")
        
        # 打印激活范围(调试阶段)
        print_activation_ranges(random_data_dict.values())
        
        return random_data_dict

calib_dataset = generate_calibration_dataset(num_samples=200)  # 使用更多的校准样本

# --- 10. 量化 HAR 模型 ---
runner = ClientRunner(har=hailo_model_har_path)
alls_lines = [
    'model_optimization_flavor(optimization_level=1, compression_level=0)', # 提高优化级别
    'performance_param(compiler_optimization_level=max)'
]
print("--- Begin quantization ---")
runner.load_model_script('\n'.join(alls_lines))
try:
    runner.optimize_full_precision(calib_dataset)
    runner.optimize(calib_dataset)  # 量化时使用新的优化级别
    runner.save_har(hailo_quantized_har_path)
    print(f"✅ Quantized HAR model saved to: {hailo_quantized_har_path}")
except Exception as e:
    print(f"❌ Failed to quantize HAR model: {e}")
    traceback.print_exc()
    exit(1)

# --- 11. 导出 HEF 模型 ---
runner = ClientRunner(har=hailo_quantized_har_path)
try:
    compiled_hef = runner.compile()
    with open(hailo_model_hef_path, "wb") as f:
        f.write(compiled_hef)
        print(f"✅ HEF model exported to: {hailo_model_hef_path}")
except Exception as e:
    print(f"❌ Failed to compile HEF model: {e}")
    exit(1)

output:

[warning] Cannot use graphviz, so no visualizations will be created
✅ Initialized ClientRunner with hardware architecture: hailo8
✅ ONNX model file found: /home/ubuntu/ycy/head_singleframe.onnx
[info] Translation started on ONNX model head_singleframe
[info] Restored ONNX model head_singleframe (completion time: 00:00:00.02)
[info] Extracted ONNXRuntime meta-data for Hailo model (completion time: 00:00:00.11)
[info] Start nodes mapped from original model: 'inst_dfg0': 'head_singleframe/input_layer1', 'anchor': 'head_singleframe/input_layer2', 'anchor_embed': 'head_singleframe/input_layer3'.
[info] End nodes mapped from original model: '/layers.3/cls_layers/cls_layers.6/Add', '/layers.3/Concat', '/layers.3/quality_layers/quality_layers.6/Add'.
[info] Translation completed on ONNX model head_singleframe (completion time: 00:00:00.24)
✅ Successfully translated ONNX model to HAR
[info] Saved HAR to: /home/ubuntu/ycy/onnx_models/hailo_outputs/head_singleframe_hailo_model.har
✅ HAR model saved to: /home/ubuntu/ycy/onnx_models/hailo_outputs/head_singleframe_hailo_model.har
✅ Loading real calibration data from /home/ubuntu/ycy/calib_data/calib_data1.npz
✅ Loaded 200 real calibration samples.
✅ Generated calibration dataset shapes:
  - head_singleframe/input_layer1: (64, 1, 900, 512)
  - head_singleframe/input_layer2: (64, 1, 900, 11)
  - head_singleframe/input_layer3: (64, 1, 900, 256)
[[[[-0.04446972 -0.0908632  -0.02634671 ...  0.          0.
     0.        ]
   [-0.06144894 -0.06481514 -0.05915117 ...  0.          0.
     0.        ]
   [-0.01487256 -0.09840052 -0.07631298 ...  0.          0.
     0.        ]
   ...
   [ 0.01513086 -0.06846617 -0.02031311 ...  0.          0.
     0.        ]
   [-0.04044682 -0.08370568 -0.05769402 ...  0.          0.
     0.        ]
   [-0.05305144 -0.10610386 -0.13674602 ...  0.          0.
     0.        ]]]


 [[[-0.03839913 -0.07559796 -0.01114828 ...  0.          0.
     0.        ]
   [-0.05145439 -0.04717213 -0.04785878 ...  0.          0.
     0.        ]
   [-0.00167706 -0.11890294 -0.05868775 ...  0.          0.
     0.        ]
   ...
   [-0.00725096 -0.08283284 -0.03572255 ...  0.          0.
     0.        ]
   [-0.04908505 -0.08390286 -0.07959612 ...  0.          0.
     0.        ]
   [-0.0270978  -0.10135622 -0.09520007 ...  0.          0.
     0.        ]]]


 [[[-0.04894761 -0.0720778  -0.01892958 ...  0.          0.
     0.        ]
   [-0.04676833 -0.04805689 -0.05130321 ...  0.          0.
     0.        ]
   [-0.02206917 -0.10593612 -0.06944686 ...  0.          0.
     0.        ]
   ...
   [-0.03490587 -0.11125467 -0.05976767 ...  0.          0.
     0.        ]
   [-0.04590744 -0.0666104  -0.02617436 ...  0.          0.
     0.        ]
   [-0.04663993 -0.11697765 -0.09900304 ...  0.          0.
     0.        ]]]


 ...


 [[[-0.16497484 -0.09341989 -0.06281898 ...  0.          0.
     0.        ]
   [-0.07132959 -0.03953239 -0.03568714 ...  0.          0.
     0.        ]
   [ 0.02262312 -0.04893972  0.03153962 ...  0.          0.
     0.        ]
   ...
   [ 0.0350185  -0.05148827  0.03337138 ...  0.          0.
     0.        ]
   [-0.01068129 -0.05627486  0.01030517 ...  0.          0.
     0.        ]
   [-0.00173737 -0.07398015 -0.00836129 ...  0.          0.
     0.        ]]]


 [[[-0.04871424 -0.04840341 -0.00139104 ...  0.          0.
     0.        ]
   [-0.05837286 -0.08906256 -0.04894479 ...  0.          0.
     0.        ]
   [-0.00046857 -0.04842551  0.02678067 ...  0.          0.
     0.        ]
   ...
   [-0.00675825 -0.09088189 -0.03461251 ...  0.          0.
     0.        ]
   [-0.01516346 -0.05922837  0.00740717 ...  0.          0.
     0.        ]
   [-0.02498161 -0.10280172 -0.05579036 ...  0.          0.
     0.        ]]]


 [[[-0.06585409 -0.08128958 -0.01704485 ...  0.          0.
     0.        ]
   [-0.07126457 -0.08206468 -0.06296729 ...  0.          0.
     0.        ]
   [-0.01294607 -0.05991405 -0.0084698  ...  0.          0.
     0.        ]
   ...
   [-0.0702889  -0.16441876 -0.09527504 ...  0.          0.
     0.        ]
   [-0.01622746 -0.05545236 -0.00106105 ...  0.          0.
     0.        ]
   [ 0.01746718 -0.11306826 -0.03023566 ...  0.          0.
     0.        ]]]]: min=-2.260219097137451, max=2.538593053817749
[[[[-0.03017934  0.05499397 -0.00863752 ...  0.          0.
     0.        ]
   [ 0.03877269 -0.27766436 -0.02044639 ...  0.          0.
     0.        ]
   [-0.23310377  0.2847417   0.00118126 ...  0.          0.
     0.        ]
   ...
   [-0.09503412  0.47198254  0.010451   ...  0.          0.
     0.        ]
   [ 0.17598535 -0.3761403  -0.01850983 ...  0.          0.
     0.        ]
   [-0.11910927  0.29049754  0.00206278 ...  0.          0.
     0.        ]]]


 [[[-0.03017934  0.05499397 -0.00863752 ...  0.          0.
     0.        ]
   [ 0.03877269 -0.27766436 -0.02044639 ...  0.          0.
     0.        ]
   [-0.23310377  0.2847417   0.00118126 ...  0.          0.
     0.        ]
   ...
   [-0.09503412  0.47198254  0.010451   ...  0.          0.
     0.        ]
   [ 0.17598535 -0.3761403  -0.01850983 ...  0.          0.
     0.        ]
   [-0.11910927  0.29049754  0.00206278 ...  0.          0.
     0.        ]]]


 [[[-0.03017934  0.05499397 -0.00863752 ...  0.          0.
     0.        ]
   [ 0.03877269 -0.27766436 -0.02044639 ...  0.          0.
     0.        ]
   [-0.23310377  0.2847417   0.00118126 ...  0.          0.
     0.        ]
   ...
   [-0.09503412  0.47198254  0.010451   ...  0.          0.
     0.        ]
   [ 0.17598535 -0.3761403  -0.01850983 ...  0.          0.
     0.        ]
   [-0.11910927  0.29049754  0.00206278 ...  0.          0.
     0.        ]]]


 ...


 [[[-0.03017934  0.05499397 -0.00863752 ...  0.          0.
     0.        ]
   [ 0.03877269 -0.27766436 -0.02044639 ...  0.          0.
     0.        ]
   [-0.23310377  0.2847417   0.00118126 ...  0.          0.
     0.        ]
   ...
   [-0.09503412  0.47198254  0.010451   ...  0.          0.
     0.        ]
   [ 0.17598535 -0.3761403  -0.01850983 ...  0.          0.
     0.        ]
   [-0.11910927  0.29049754  0.00206278 ...  0.          0.
     0.        ]]]


 [[[-0.03017934  0.05499397 -0.00863752 ...  0.          0.
     0.        ]
   [ 0.03877269 -0.27766436 -0.02044639 ...  0.          0.
     0.        ]
   [-0.23310377  0.2847417   0.00118126 ...  0.          0.
     0.        ]
   ...
   [-0.09503412  0.47198254  0.010451   ...  0.          0.
     0.        ]
   [ 0.17598535 -0.3761403  -0.01850983 ...  0.          0.
     0.        ]
   [-0.11910927  0.29049754  0.00206278 ...  0.          0.
     0.        ]]]


 [[[-0.03017934  0.05499397 -0.00863752 ...  0.          0.
     0.        ]
   [ 0.03877269 -0.27766436 -0.02044639 ...  0.          0.
     0.        ]
   [-0.23310377  0.2847417   0.00118126 ...  0.          0.
     0.        ]
   ...
   [-0.09503412  0.47198254  0.010451   ...  0.          0.
     0.        ]
   [ 0.17598535 -0.3761403  -0.01850983 ...  0.          0.
     0.        ]
   [-0.11910927  0.29049754  0.00206278 ...  0.          0.
     0.        ]]]]: min=-0.5318491458892822, max=0.535064697265625
[[[[-0.06995326  0.12841378  0.2928448  ... -0.05868956 -0.05868956
    -0.01869817]
   [-0.06027998 -0.06027998 -0.06027998 ... -0.05868956 -0.05868956
    -0.01869817]
   [-0.06885998  0.05718956  0.19684848 ... -0.05868956 -0.05868956
    -0.01869817]
   ...
   [-0.06926065  0.167193    0.14540103 ... -0.05868956 -0.05868956
    -0.01869817]
   [-0.06346054 -0.06346054 -0.06346054 ... -0.05868956 -0.05868956
    -0.01869811]
   [-0.06712365  0.10927399  0.19297567 ... -0.05868956 -0.05868956
    -0.01869811]]]


 [[[ 0.00172684  0.10103413 -0.070883   ...  0.17231414 -0.06800588
    -0.06800588]
   [-0.07841718 -0.07841718 -0.06165853 ...  0.17231414 -0.06800588
    -0.06800588]
   [ 0.02601107  0.12189623 -0.06775819 ...  0.17231414 -0.06800588
    -0.06800588]
   ...
   [ 0.03347871  0.16058384 -0.07339907 ...  0.17231414 -0.06800588
    -0.06800588]
   [-0.07238501 -0.07238501 -0.07238501 ...  0.17231406 -0.06800589
    -0.06800589]
   [ 0.01242606  0.15509477 -0.07107773 ...  0.17231406 -0.06800589
    -0.06800589]]]


 [[[-0.06747498 -0.06747498 -0.02739371 ...  0.05495894  0.2309963
    -0.06866357]
   [ 0.12320472  0.17842323  0.32408717 ...  0.05495894  0.2309963
    -0.06866357]
   [-0.0710955  -0.0710955   0.04731641 ...  0.05495894  0.2309963
    -0.06866357]
   ...
   [-0.07076986 -0.07076986 -0.07076986 ...  0.05495894  0.2309963
    -0.06866357]
   [ 0.16563907  0.14925079  0.21641894 ...  0.05495887  0.23099628
    -0.06866357]
   [-0.0678173  -0.0678173  -0.0678173  ...  0.05495887  0.23099628
    -0.06866357]]]


 ...


 [[[-0.07687792  0.03284594 -0.00335262 ... -0.07136811  0.16489092
     0.01562924]
   [-0.02525332 -0.07437874 -0.07437874 ... -0.07136811  0.16489092
     0.01562924]
   [-0.0758829  -0.0758829   0.11654099 ... -0.07136811  0.16489092
     0.01562924]
   ...
   [-0.07419545  0.10029572 -0.07287232 ... -0.07136811  0.16489092
     0.01562924]
   [-0.02327197 -0.07782315 -0.07782315 ... -0.07136808  0.16489089
     0.0156292 ]
   [-0.07472431  0.01719039 -0.07472431 ... -0.07136808  0.16489089
     0.0156292 ]]]


 [[[ 0.1625965   0.15313278  0.20819981 ... -0.07893704 -0.0272102
    -0.07893704]
   [-0.08238295 -0.06097027 -0.06268218 ... -0.07893704 -0.0272102
    -0.07893704]
   [ 0.10799159  0.13293153  0.34304982 ... -0.07893704 -0.0272102
    -0.07893704]
   ...
   [ 0.06336843  0.17816952  0.20409667 ... -0.07893704 -0.0272102
    -0.07893704]
   [-0.0746233   0.01380232 -0.05264052 ... -0.07893705 -0.02721019
    -0.07893705]
   [ 0.10722997  0.13785031  0.24316168 ... -0.07893705 -0.02721019
    -0.07893705]]]


 [[[ 0.14262058 -0.03780425 -0.06569587 ... -0.04339524 -0.05874483
    -0.05874483]
   [ 0.11445364 -0.06354143 -0.06354143 ... -0.04339524 -0.05874483
    -0.05874483]
   [ 0.21624692 -0.06484863 -0.06484863 ... -0.04339524 -0.05874483
    -0.05874483]
   ...
   [ 0.24468294 -0.01902553 -0.06077867 ... -0.04339524 -0.05874483
    -0.05874483]
   [-0.03910775 -0.07021856 -0.07021856 ... -0.04339523 -0.05874483
    -0.05874483]
   [ 0.23260374 -0.05261029 -0.06358418 ... -0.04339523 -0.05874483
    -0.05874483]]]]: min=-0.10928480327129364, max=0.6397421360015869
--- Begin quantization ---
[info] ParsedPerformanceParam command, setting optimization_level(max=2)
[info] Loading model script commands to head_singleframe from string
[info] ParsedPerformanceParam command, setting optimization_level(max=2)
[info] Starting Model Optimization
[info] Model received quantization params from the hn
[info] Starting Mixed Precision
[info] Mixed Precision is done (completion time is 00:00:00.07)
[info] Starting Layer Norm Decomposition
[info] Using dataset with 64 entries for calibration
Calibration: 100%|███████████████████████████████████████████████████████████████████████████████████████████| 64/64 [00:41<00:00,  1.56entries/s]
[info] Layer Norm Decomposition is done (completion time is 00:00:44.50)
[info] Starting Stats Collector
[info] Using dataset with 64 entries for calibration
Calibration: 100%|███████████████████████████████████████████████████████████████████████████████████████████| 64/64 [00:29<00:00,  2.14entries/s]
[info] Stats Collector is done (completion time is 00:00:30.94)
[info] Starting Fix zp_comp Encoding
[info] Fix zp_comp Encoding is done (completion time is 00:00:00.00)
[info] matmul_equalization skipped
[info] activation fitting started for head_singleframe/conv_var_inv_layer_normalization1/act_op
[info] No shifts available for layer head_singleframe/conv3/conv_op, using max shift instead. delta=0.7209
[info] No shifts available for layer head_singleframe/conv3/conv_op, using max shift instead. delta=0.3605
[info] No shifts available for layer head_singleframe/conv3/conv_op, using max shift instead. delta=0.3605
[info] activation fitting started for head_singleframe/conv_var_inv_layer_normalization2/act_op
[info] activation fitting started for head_singleframe/conv_var_inv_layer_normalization3/act_op
[info] activation fitting started for head_singleframe/conv_var_inv_layer_normalization4/act_op
[info] activation fitting started for head_singleframe/conv_var_inv_layer_normalization5/act_op
[info] activation fitting started for head_singleframe/conv_var_inv_layer_normalization6/act_op
[info] activation fitting started for head_singleframe/conv_var_inv_layer_normalization7/act_op
[info] activation fitting started for head_singleframe/conv_var_inv_layer_normalization8/act_op
[info] Finetune encoding skipped
[info] Starting Bias Correction
[info] The algorithm Bias Correction will use up to 0.83 GB of storage space
[info] Using dataset with 64 entries for Bias Correction
Bias Correction: 100%|███████████████████████████████████████████| 16/16 [03:27<00:00, 12.98s/blocks, Layers=['head_singleframe/conv14_output_0']]
[info] Bias Correction is done (completion time is 00:03:28.18)
[info] Adaround skipped
[info] Fine Tune skipped
[info] Starting Layer Noise Analysis
Full Quant Analysis: 100%|██████████████████████████████████████████████████████████████████████████████████| 2/2 [03:08<00:00, 94.24s/iterations]
[info] Layer Noise Analysis is done (completion time is 00:03:10.97)
[info] Output layers signal-to-noise ratio (SNR): measures the quantization noise (higher is better)
[info]  head_singleframe/output_layer3 SNR:     20.22 dB
[info]  head_singleframe/output_layer1 SNR:     -0.06076 dB
[info]  head_singleframe/output_layer2 SNR:     1.433 dB
[info] Model Optimization is done
[info] Saved HAR to: /home/ubuntu/ycy/onnx_models/hailo_outputs/head_singleframe_hailo_quantized_model.har
✅ Quantized HAR model saved to: /home/ubuntu/ycy/onnx_models/hailo_outputs/head_singleframe_hailo_quantized_model.har
[info] Loading network parameters
[info] Starting Hailo allocation and compilation flow

Hey @chenyao,

I took a look at the logs and noticed a few things that might help resolve the issue you’re running into:

First, try explicitly passing the hw_arch parameter and make sure to call configure() before compiling:

runner = ClientRunner(har=hailo_quantized_har_path, hw_arch="hailo8")
runner.configure()
compiled_hef = runner.compile()

Second, you might want to reduce the compiler’s search space to speed things up:

runner.load_model_script("performance_param(compiler_optimization_level=balanced)")

Third, enable DFC debug logging so we can see exactly where the process is getting hung up:

os.environ["HAILO_DFC_LOG_LEVEL"] = "DEBUG"

Give these a try and let me know how it goes ! if this continues please give me the logs of the error to better help you out !

Thank you for your reply. The latest problem encountered during operation is in this link. Please take a look at it for me. Thank you very much.

Hello. What is your compiler version? I’m using version 3.28.0. I don’t have compiler_optimization_level=balanced and runner.configure(). Do I need to upgrade to version 3.32.0?

Yes, 3.32 is the latest version and all those flags I just gave you are specifically for 3.32! I can check what’s available for 3.28, but keep in mind that the HEF version is a bit dated!