|
29 | 29 | _build_stab_maps, |
30 | 30 | _decode_batch, |
31 | 31 | _parse_quant_format, |
| 32 | + _sync_and_raise_on_export_failure, |
32 | 33 | map_grid_to_stabilizer_tensor, |
33 | 34 | sample_predictions, |
34 | 35 | ) |
@@ -750,6 +751,7 @@ def _setup_trt_for_ablation(model, cfg, dist, device, basis, D, half, stim_dets) |
750 | 751 | ) |
751 | 752 |
|
752 | 753 | elif onnx_workflow in (OnnxWorkflow.EXPORT_ONNX_ONLY, OnnxWorkflow.EXPORT_AND_USE_TRT): |
| 754 | + export_error = None |
753 | 755 | if dist.rank == 0: |
754 | 756 | try: |
755 | 757 | fp32_onnx_path = ( |
@@ -781,39 +783,47 @@ def _setup_trt_for_ablation(model, cfg, dist, device, basis, D, half, stim_dets) |
781 | 783 | print(f"[Ablation] Exported FP32 ONNX: {fp32_onnx_path}") |
782 | 784 |
|
783 | 785 | if quant_format: |
784 | | - calib_samples = int(os.environ.get("QUANT_CALIB_SAMPLES", "256")) |
785 | | - calib_dets = stim_dets[:calib_samples].astype(np.uint8) |
| 786 | + # Quantization failure semantics mirror the LER path: FP8 is |
| 787 | + # fail-fast, INT8 falls back to the FP32 ONNX (README notes). |
786 | 788 | try: |
787 | | - import modelopt.onnx.quantization as mq |
788 | | - quant_kwargs = {} |
789 | | - if quant_format == "fp8": |
790 | | - quant_kwargs["op_types_to_quantize"] = ["Conv"] |
791 | | - quant_kwargs["high_precision_dtype"] = "fp16" |
792 | | - mq.quantize( |
793 | | - onnx_path=fp32_onnx_path, |
794 | | - quantize_mode=quant_format, |
795 | | - calibration_data={"dets": calib_dets.astype("float32")}, |
796 | | - output_path=onnx_path, |
797 | | - **quant_kwargs, |
798 | | - ) |
799 | | - except ImportError: |
| 789 | + calib_samples = int(os.environ.get("QUANT_CALIB_SAMPLES", "256")) |
| 790 | + calib_dets = stim_dets[:calib_samples].astype(np.uint8) |
| 791 | + try: |
| 792 | + import modelopt.onnx.quantization as mq |
| 793 | + quant_kwargs = {} |
| 794 | + if quant_format == "fp8": |
| 795 | + quant_kwargs["op_types_to_quantize"] = ["Conv"] |
| 796 | + quant_kwargs["high_precision_dtype"] = "fp16" |
| 797 | + mq.quantize( |
| 798 | + onnx_path=fp32_onnx_path, |
| 799 | + quantize_mode=quant_format, |
| 800 | + calibration_data={"dets": calib_dets.astype("float32")}, |
| 801 | + output_path=onnx_path, |
| 802 | + **quant_kwargs, |
| 803 | + ) |
| 804 | + except ImportError: |
| 805 | + if quant_format == "fp8": |
| 806 | + raise RuntimeError( |
| 807 | + "[Ablation] FP8 quantization requires nvidia-modelopt." |
| 808 | + ) |
| 809 | + from evaluation.logical_error_rate import _ort_quantize_int8 |
| 810 | + _ort_quantize_int8(fp32_onnx_path, onnx_path, calib_dets) |
| 811 | + print(f"[Ablation] Exported quantized ONNX: {onnx_path}") |
| 812 | + except Exception as e: |
800 | 813 | if quant_format == "fp8": |
801 | 814 | raise RuntimeError( |
802 | | - "[Ablation] FP8 quantization requires nvidia-modelopt." |
803 | | - ) |
804 | | - from evaluation.logical_error_rate import _ort_quantize_int8 |
805 | | - _ort_quantize_int8(fp32_onnx_path, onnx_path, calib_dets) |
806 | | - print(f"[Ablation] Exported quantized ONNX: {onnx_path}") |
| 815 | + f"[Ablation] FP8 ONNX quantization failed (fail-fast): {e}" |
| 816 | + ) from e |
| 817 | + print(f"[Ablation] ONNX quantization failed: {e}; using FP32 ONNX.") |
| 818 | + onnx_path = fp32_onnx_path |
807 | 819 | except Exception as e: |
808 | | - print(f"[Ablation] ONNX export failed: {e}; using PyTorch.") |
809 | | - onnx_workflow = OnnxWorkflow.TORCH_ONLY |
810 | | - |
811 | | - if dist.world_size > 1: |
812 | | - # Broadcast rank 0's onnx_workflow (may have been set to TORCH_ONLY on |
813 | | - # export failure) so non-zero ranks skip the TRT build when rank 0 failed. |
814 | | - wf_list = [onnx_workflow] |
815 | | - torch.distributed.broadcast_object_list(wf_list, src=0) |
816 | | - onnx_workflow = wf_list[0] |
| 820 | + # Stash instead of raising here: non-zero ranks must first learn |
| 821 | + # about the failure through the collective below, or they would |
| 822 | + # block in it after rank 0 dies. |
| 823 | + export_error = e |
| 824 | + # Doubles as the post-export sync so non-zero ranks don't race ahead to |
| 825 | + # the TRT build. |
| 826 | + _sync_and_raise_on_export_failure(export_error, onnx_workflow, dist, "[Ablation]") |
817 | 827 | engine_path = onnx_path.replace(".onnx", ".engine") |
818 | 828 |
|
819 | 829 | if onnx_workflow == OnnxWorkflow.EXPORT_AND_USE_TRT and device.type == "cuda": |
|
0 commit comments