Repository: microsoft/onnxruntime
File: onnxruntime/python/tools/transformers/models/whisper/convert_to_onnx.py (Python)

Code before:
                        use_decoder_masked_mha=use_decoder_masked_mha,
                        output_qk=output_qk,
                    )
                    # Remove old ONNX model and old data file
                    if os.path.exists(onnx_path):
                        os.remove(onnx_path)
                    if os.path.exists(onnx_path + ".data"):
                        os.remove(onnx_path + ".data")
                    onnx_path = output_path

                    if isinstance(model, WhisperEncoder):
                        model.verify_onnx(
                            onnx_path,
                            PROVIDERS[provider],
                            use_fp16_inputs=use_fp16_inputs,
                        )
                    else:
                        model.verify_onnx(
                            onnx_path,
                            PROVIDERS[provider],
                            use_fp16_inputs=use_fp16_inputs,
                            use_int32_inputs=use_int32_inputs,
                        )

                if precision in (Precision.INT8, Precision.INT4):
                    onnx_model = onnx.load(onnx_path, load_external_data=False)
                    load_external_data_for_model(onnx_model, os.path.dirname(onnx_path))
                    matmul_nodes = [node.name for node in onnx_model.graph.node if node.op_type == "MatMul"]
                    quant_algo_config = make_quant_algo_config(precision, "k_quant", matmul_nodes)

                    quant = MatMulNBitsQuantizer(
                        model=onnx_model,
                        block_size=32,
                        is_symmetric=True,
                        accuracy_level=4,
                        quant_format=QuantFormat.QOperator,
                        op_types_to_quantize=("MatMul",),
                        algo_config=quant_algo_config,
                    )
                    quant.process()
                    if os.path.exists(output_path):
                        os.remove(output_path)
                    if os.path.exists(output_path + ".data"):
                        os.remove(output_path + ".data")
                    onnx.save_model(
                        quant.model.model,
                        output_path,
                        save_as_external_data=True,
                        all_tensors_to_one_file=True,
                        location=os.path.basename(output_path) + ".data",
                        size_threshold=1024,

Review comment (suggestion):
```suggestion
                    onnx_model = onnx.load(onnx_path, load_external_data=True)
```

Code after:
                        model.verify_onnx(
                            onnx_path,
                            PROVIDERS[provider],
                            use_fp16_inputs=use_fp16_inputs,
                        )
                    else:
                        model.verify_onnx(
                            onnx_path,
                            PROVIDERS[provider],
                            use_fp16_inputs=use_fp16_inputs,
                            use_int32_inputs=use_int32_inputs,
                        )

                if precision in (Precision.INT8, Precision.INT4):
                    onnx_model = onnx.load(onnx_path, load_external_data=True)
                    matmul_nodes = [node.name for node in onnx_model.graph.node if node.op_type == "MatMul"]
                    quant_algo_config = make_quant_algo_config(precision, "k_quant", matmul_nodes)

                    quant = MatMulNBitsQuantizer(
                        model=onnx_model,
                        block_size=32,
                        is_symmetric=quantize_symmetric,
                        accuracy_level=accuracy_level,
                        quant_format=QuantFormat.QOperator,
                        op_types_to_quantize=("MatMul",),
                        algo_config=quant_algo_config,
                    )
                    quant.process()
                    if os.path.exists(output_path):
                        os.remove(output_path)
                    if os.path.exists(output_path + ".data"):
                        os.remove(output_path + ".data")
                    onnx.save_model(
                        quant.model.model,
                        output_path,
                        save_as_external_data=True,
                        all_tensors_to_one_file=True,
                        location=os.path.basename(output_path) + ".data",
                        size_threshold=0,
                        convert_attribute=False,
                    )
            else:
                logger.info(f"Skip optimizing: existing ONNX model {onnx_path}")
        else:
            output_path = onnx_path

        output_paths.append(output_path)

    return output_paths