Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions onnxruntime/core/framework/session_state_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ static common::Status ExtDataTensorProtoToTensor(const Env& env,
ORT_RETURN_IF_ERROR(utils::GetExtDataFromTensorProto(env, proto_path.c_str(), tensor_proto,
Comment thread
yuslepukhin marked this conversation as resolved.
ext_data_buf, ext_data_len, ext_data_deleter,
buffered_tensor, &prepacked_for_graph));
if constexpr (endian::native != endian::little) {
if (!proto_path.empty() && (proto_path.compare(onnxruntime::utils::kTensorProtoMemoryAddressTag) != 0)) {
utils::ConvertRawDataInTensorProto(const_cast<ONNX_NAMESPACE::TensorProto*>(&tensor_proto), ext_data_buf, ext_data_len);
}
}

// NB: creating a do-nothing allocator per tensor is wasteful; can perhaps be
// avoided if the Tensor class implements the do-nothing behavior when given a
Expand Down
16 changes: 15 additions & 1 deletion onnxruntime/core/framework/tensorprotoutils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,15 @@ void SetRawDataInTensorProto(ONNX_NAMESPACE::TensorProto& tensor_proto, std::str
tensor_proto.set_raw_data(std::move(param));
}

void ConvertRawDataInTensorProto(TensorProto* tensor) {
void ConvertRawDataInTensorProto(TensorProto* tensor,
void* ext_data_buf,
size_t ext_data_len) {
size_t element_size = 1;
char* bytes = NULL;
size_t num_elements = 0;
if (ext_data_buf && !ext_data_len) {
return;
}
switch (tensor->data_type()) {
case TensorProto_DataType_FLOAT:
bytes = reinterpret_cast<char*>(tensor->mutable_float_data()->mutable_data());
Expand Down Expand Up @@ -337,6 +342,15 @@ void ConvertRawDataInTensorProto(TensorProto* tensor) {
num_elements = (tensor->raw_data().size()) / element_size;
bytes = const_cast<char*>(tensor->mutable_raw_data()->c_str());
}

if (element_size == 1) {
return;
}
if (ext_data_buf) {
ORT_ENFORCE(ext_data_len % element_size == 0);
num_elements = ext_data_len / element_size;
bytes = reinterpret_cast<char*>(ext_data_buf);
}
for (size_t i = 0; i < num_elements; ++i) {
Comment thread
yuslepukhin marked this conversation as resolved.
char* start_byte = bytes + i * element_size;
char* end_byte = start_byte + element_size - 1;
Expand Down
10 changes: 8 additions & 2 deletions onnxruntime/core/framework/tensorprotoutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,18 @@ Status GetExternalDataInfo(const ONNX_NAMESPACE::TensorProto& tensor_proto,
ExternalDataInfo::PrepackedInfos* prepacked_infos = nullptr);
/**
* This function is used to convert the endianess of Tensor data.
* If ext_data_buf is provided, then this buffer content's endianess
* will be changed.
* Mostly, will be used in big endian system to support the model file
* generated on little endian system.
* @param initializer given initializer tensor
* @param tensor_proto given initializer tensor
* @param ext_data_buf optional externl data buffer
* @param ext_data_len optional externl data buffer lengeh
* @returns None
*/
void ConvertRawDataInTensorProto(ONNX_NAMESPACE::TensorProto* initializer);
void ConvertRawDataInTensorProto(ONNX_NAMESPACE::TensorProto* tensor_proto,
Comment thread
yuslepukhin marked this conversation as resolved.
void* ext_data_buf = NULL,
size_t ext_data_len = 0);

/**
* Wrapper function for set_raw_data.
Expand Down