Repository: microsoft/onnxruntime
File: include/onnxruntime/core/providers/utils/ort_graph_to_proto.h (C/C++)

Code before:
            break;
          }
          case ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT64: {
            tensor_proto.set_data_type(onnx::TensorProto_DataType_UINT64);
            break;
          }
          default: {
            std::string err_msg = "Unexpected ONNXTensorElementDataType with value " + std::to_string(static_cast<int>(element_type));
            return Ort::Status(err_msg.c_str(), ORT_FAIL);
          }
        }

        auto shape = type_shape_info.GetShape();

        for (auto& dim : shape) {
          tensor_proto.add_dims(dim);
        }

        const void* data = tensor.GetTensorRawData();
        const size_t data_bytes = tensor.GetTensorSizeInBytes();

        // Copy the Ortvalue to TensorProto as raw data
        if constexpr (isBigEndian()) {
          size_t element_size = 0;
          GetTensorElementSize(element_type, element_size);
          SwapByteOrderInplace(const_cast<void*>(data), data_bytes, element_size);
        }
        tensor_proto.set_raw_data(data, data_bytes);

        *(attr_proto.mutable_t()) = std::move(tensor_proto);
        break;
      }
      default: {
        std::string err_msg = "Unexpected OrtOpAttrType with value " + std::to_string(static_cast<int>(attr_type));
        return Ort::Status(err_msg.c_str(), ORT_FAIL);
      }
    }
  } catch (const Ort::Exception& ex) {
    return Ort::Status{ex};
  } catch (const std::exception& ex) {
    return Ort::Status{ex.what(), ORT_FAIL};
  }

  return Ort::Status{nullptr};
}

Ort::Status ConvertExternalData(const OrtValueInfo* value_info, void* data, size_t bytes) {
  if constexpr (!isBigEndian()) {
    return Ort::Status{nullptr};
  }
  std::vector<int64_t> initializer_dims;

Review comment (refactor):
>SwapByteOrderInplace(const_cast<void*>(data),  [](http://example.com/codeflow?start=10&length=46)

There is a small utility that can also be copies here.
It would helps us to copy/convert which is what we need and avoid const_cast. It is in framework/endianess.cc

Code after:
            tensor_proto.set_data_type(onnx::TensorProto_DataType_DOUBLE);
            break;
          }
          case ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT32: {
            tensor_proto.set_data_type(onnx::TensorProto_DataType_UINT32);
            break;
          }
          case ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT64: {
            tensor_proto.set_data_type(onnx::TensorProto_DataType_UINT64);
            break;
          }
          default: {
            std::string err_msg = "Unexpected ONNXTensorElementDataType with value " + std::to_string(static_cast<int>(element_type));
            return Ort::Status(err_msg.c_str(), ORT_FAIL);
          }
        }

        auto shape = type_shape_info.GetShape();

        for (auto& dim : shape) {
          tensor_proto.add_dims(dim);
        }

        const void* data = tensor.GetTensorRawData();
        const size_t data_bytes = tensor.GetTensorSizeInBytes();

        // Copy the Ortvalue to TensorProto as raw data
        if constexpr (endian::native == endian::big) {
          size_t element_size = 0;
          GetTensorElementSize(element_type, element_size);
          // create local copy of data and do endianess conversion
          auto raw_data_buf = std::make_unique<unsigned char[]>(data_bytes);
          std::memcpy(raw_data_buf.get(), data, data_bytes);
          SwapByteOrderInplace(raw_data_buf.get(), data_bytes, element_size);
          tensor_proto.set_raw_data(raw_data_buf.get(), data_bytes);
        } else {
          tensor_proto.set_raw_data(data, data_bytes);
        }

        *(attr_proto.mutable_t()) = std::move(tensor_proto);
        break;
      }
      default: {
        std::string err_msg = "Unexpected OrtOpAttrType with value " + std::to_string(static_cast<int>(attr_type));
        return Ort::Status(err_msg.c_str(), ORT_FAIL);
      }
    }
  } catch (const Ort::Exception& ex) {
    return Ort::Status{ex};
  } catch (const std::exception& ex) {
    return Ort::Status{ex.what(), ORT_FAIL};