Repository: microsoft/onnxruntime
File: onnxruntime/core/providers/migraphx/migraphx_execution_provider.cc (C++)

Code before:
          input_shapes.push_back(tensor_shape->dim(j).dim_value());
        }
      }
      model_cache_file = model_cache_path_ / (mxr_filename_prefix + make_hash(input_shapes) + ".mxr");
    }

    // map parameter input name to index
    std::unordered_map<std::string, std::size_t> input_name_index;
    const auto& input_defs = fused_node.InputDefs();
    input_name_index.reserve(input_defs.size());
    for (std::size_t i = 0; i < input_defs.size(); ++i) {
      input_name_index[input_defs[i]->Name()] = i;
    }

    auto model = graph_body_viewer.CreateModel(*GetLogger());
    auto model_proto = model->ToProto();
    graph_body_viewer.ToProto(*model_proto->mutable_graph(), true, true);
    model_proto->set_ir_version(ONNX_NAMESPACE::Version::IR_VERSION);
    std::string onnx_string_buffer;
    model_proto->SerializeToString(onnx_string_buffer);

    if (dump_model_ops_) {
      std::string onnx_name = fused_node.Name() + ".onnx";
      std::ofstream ofs(onnx_name, std::ios::binary);
      if (!ofs.is_open()) {
        ORT_THROW("Failed to open file to dump ONNX model: " + model_name);
      }
      ofs.write(onnx_string_buffer.data(), onnx_string_buffer.size());
      ofs.close();
      LOGS_DEFAULT(INFO) << "ONNX model dumped to " << model_name;
    }

    std::vector<std::string> input_names, output_names;
    no_input_shape = no_input_shape || get_input_output_names(graph_body_viewer, input_names, output_names);

    // by parsing the model_proto, create a program corresponding to
    // the input fused_node
    migraphx::program prog;

    if (!no_input_shape) {
      if (!load_precompiled_model(prog, model_cache_file)) {
        LOGS_DEFAULT(VERBOSE) << "No input shapes detected quantizing model";
#ifndef ENABLE_TRAINING_CORE
#ifdef HAVE_MIGRAPHX_API_ONNX_OPTIONS_SET_EXTERNAL_DATA_PATH
        options.set_external_data_path(model_path_.parent_path().string());
#endif
#endif
        prog = migraphx::parse_onnx_buffer(onnx_string_buffer, options);
        migraphx::program_parameters quant_params;

        calibrate_and_quantize(prog, t_, quant_params, fp16_enable_, bf16_enable_, int8_enable_,

Review comment (suggestion):
This is a good fix. For even more robust error handling, you might consider checking the stream's state after closing the file (e.g., `if (!ofs)`). This would catch potential errors during the final flush to disk, which could result in a corrupt or incomplete file. However, I see that not checking the return of `close()` is a common pattern in this codebase, so this is more of a suggestion for hardening the code rather than a required change for this PR.

Code after:
          input_shapes.push_back(tensor_shape->dim(j).dim_value());
        }
      }
      model_cache_file = model_cache_path_ / (mxr_filename_prefix + make_hash(input_shapes) + ".mxr");
    }

    // map parameter input name to index
    std::unordered_map<std::string, std::size_t> input_name_index;
    const auto& input_defs = fused_node.InputDefs();
    input_name_index.reserve(input_defs.size());
    for (std::size_t i = 0; i < input_defs.size(); ++i) {
      input_name_index[input_defs[i]->Name()] = i;
    }

    auto model = graph_body_viewer.CreateModel(*GetLogger());
    auto model_proto = model->ToProto();
    graph_body_viewer.ToProto(*model_proto->mutable_graph(), true, true);
    model_proto->set_ir_version(ONNX_NAMESPACE::Version::IR_VERSION);
    std::string onnx_string_buffer;
    model_proto->SerializeToString(onnx_string_buffer);

    if (dump_model_ops_) {
      std::string onnx_name = fused_node.Name() + ".onnx";
      std::ofstream ofs(onnx_name, std::ios::binary);
      if (!ofs.is_open()) {
        ORT_THROW("Failed to open file to dump ONNX model: " + onnx_name);
      }
      ofs.write(onnx_string_buffer.data(), onnx_string_buffer.size());
      ofs.close();
      LOGS_DEFAULT(INFO) << "ONNX model dumped to " << onnx_name;
    }

    std::vector<std::string> input_names, output_names;
    no_input_shape = no_input_shape || get_input_output_names(graph_body_viewer, input_names, output_names);

    // by parsing the model_proto, create a program corresponding to
    // the input fused_node
    migraphx::program prog;

    if (!no_input_shape) {
      if (!load_precompiled_model(prog, model_cache_file)) {
        LOGS_DEFAULT(VERBOSE) << "No input shapes detected quantizing model";
#ifndef ENABLE_TRAINING_CORE
#ifdef HAVE_MIGRAPHX_API_ONNX_OPTIONS_SET_EXTERNAL_DATA_PATH
        options.set_external_data_path(model_path_.parent_path().string());
#endif
#endif
        prog = migraphx::parse_onnx_buffer(onnx_string_buffer, options);
        migraphx::program_parameters quant_params;

        calibrate_and_quantize(prog, t_, quant_params, fp16_enable_, bf16_enable_, int8_enable_,