Repository: microsoft/onnxruntime
File: onnxruntime/test/ep_graph/test_ep_graph.cc (C++)

Code before:
  auto output_type_shape = ort_output.GetTensorTypeAndShapeInfo();
  size_t num_output_elems = output_type_shape.GetElementCount();

  ASSERT_EQ(output_type_shape.GetElementType(), ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT);
  ASSERT_EQ(num_output_elems, 32 * 26 * 26);

  // Return output data.
  const float* output_values = ort_output.GetTensorData<float>();
  output_data.assign(output_values, output_values + num_output_elems);
}

// Test serializing an OrtGraph with external initializers to GraphProto.
// Checks that the outputs of the serialized and original models are identical.
TEST(EpGraphTest, SerializeToProto_InputModelHasExternalIni) {
  const ORTCHAR_T* original_model_path = ORT_TSTR("testdata/conv_qdq_external_ini.onnx");
  const ORTCHAR_T* serialized_model_path = ORT_TSTR("conv_qdq_ext_ini_serialized.onnx");
  std::filesystem::remove(serialized_model_path);

  {
    auto test_graph = TestGraph::Load(original_model_path);
    ASSERT_NE(test_graph, nullptr) << "Failed to load test model";

    // Serialize OrtGraph to GraphProto. Save initializers to external file.
    std::string ext_ini_file_path = "conv_qdq_ext_ini_serialized.bin";
    std::filesystem::remove(ext_ini_file_path);
    std::ofstream ext_ini_ofs(ext_ini_file_path, std::ios::binary);
    auto handle_initializer_data = [&ext_ini_ofs, &ext_ini_file_path](const OrtValueInfo* value_info,
                                                                      const void* data, size_t bytes,
                                                                      bool& is_external, std::string& location,
                                                                      int64_t& offset) -> Ort::Status {
      // OrtValueInfo* could be used to query initializer's name, type, shape,
      // node consumers, etc.
      (void)value_info;

      if (bytes <= 127) {
        is_external = false;  // Keep small initializers stored inside the TensorProto.
        return Ort::Status{nullptr};
      }

      // For BE system, Before writing to file, we need to do data coversion.
      if constexpr (endian::native != endian::little) {
        OrtEpUtils::ConvertExternalData(value_info, const_cast<void*>(data), bytes);
      }

      offset = ext_ini_ofs.tellp();
      location = ext_ini_file_path;
      ext_ini_ofs.write(static_cast<const char*>(data), bytes);
      ext_ini_ofs.flush();
      is_external = true;  // True if is external initializer.

      return Ort::Status{nullptr};

Review comment (bug):
Please, create a copy, do not  modify the original const data, the consequences of that is not known.
it also serves as a bad example and a source of bugs.
In production, the process may continue with data swapped which would be wrong for AIX anyway. #Resolved

Code after:
    ASSERT_NE(test_graph, nullptr) << "Failed to load test model";

    // Serialize OrtGraph to GraphProto. Save initializers to external file.
    std::string ext_ini_file_path = "conv_qdq_ext_ini_serialized.bin";
    std::filesystem::remove(ext_ini_file_path);
    std::ofstream ext_ini_ofs(ext_ini_file_path, std::ios::binary);
    auto handle_initializer_data = [&ext_ini_ofs, &ext_ini_file_path](const OrtValueInfo* value_info,
                                                                      const void* data, size_t bytes,
                                                                      bool& is_external, std::string& location,
                                                                      int64_t& offset) -> Ort::Status {
      if (bytes <= 127) {
        is_external = false;  // Keep small initializers stored inside the TensorProto.
        return Ort::Status{nullptr};
      }

      // For BE system, Before writing to file, we need to do data coversion.
      if constexpr (endian::native != endian::little) {
        auto data_buf = std::make_unique<char[]>(bytes);
        std::memcpy(data_buf.get(), data, bytes);
        OrtEpUtils::ConvertExternalData(value_info, data_buf.get(), bytes);
        offset = ext_ini_ofs.tellp();
        ext_ini_ofs.write(static_cast<const char*>(data_buf.get()), bytes);
      } else {
        offset = ext_ini_ofs.tellp();
        ext_ini_ofs.write(static_cast<const char*>(data), bytes);
      }

      location = ext_ini_file_path;
      ext_ini_ofs.flush();
      is_external = true;  // True if is external initializer.

      return Ort::Status{nullptr};
    };

    ONNX_NAMESPACE::ModelProto model_proto;
    ASSERT_CXX_ORTSTATUS_OK(OrtEpUtils::OrtGraphToProto(test_graph->GetOrtGraph(), model_proto,
                                                        handle_initializer_data));

    std::ofstream ofs(serialized_model_path, std::ios::binary);
    model_proto.SerializeToOstream(&ofs);
    ofs.flush();

    ASSERT_TRUE(std::filesystem::exists(serialized_model_path));
    ASSERT_TRUE(std::filesystem::exists(ext_ini_file_path));
  }

  // Compare output of the original and serialized models. Should be identical.
  std::vector<float> output_original;
  std::vector<float> output_serialized;

  RunConvQDQExtIni(original_model_path, output_original);