From 28ee5caaddfa884a8a4fdecd414c64cd6be2a826 Mon Sep 17 00:00:00 2001 From: Ranjit Ranjan Date: Wed, 19 Jun 2024 02:48:48 -0500 Subject: [PATCH] using SwapByteOrderCopy for SetIndices --- onnxruntime/core/framework/tensorprotoutils.cc | 12 ++++++++---- onnxruntime/core/graph/graph.cc | 14 ++++++-------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/onnxruntime/core/framework/tensorprotoutils.cc b/onnxruntime/core/framework/tensorprotoutils.cc index 2dc8364e9fab4..e451b062539b7 100644 --- a/onnxruntime/core/framework/tensorprotoutils.cc +++ b/onnxruntime/core/framework/tensorprotoutils.cc @@ -1648,7 +1648,14 @@ static void SetIndices(gsl::span gathered_indices, } else { auto* dst = ind_dest + dest_index; T v = static_cast(src_index); - memcpy(dst, &v, sizeof(T)); + if constexpr (endian::native != endian::little) { + auto src = gsl::make_span(static_cast(reinterpret_cast(&v)), sizeof(T)); + auto dest = gsl::make_span(static_cast(reinterpret_cast(dst)) , sizeof(T)); + onnxruntime::utils::SwapByteOrderCopy(sizeof(T),src ,dest); + } + else { + memcpy(dst, &v, sizeof(T)); + } } ++dest_index; } @@ -1698,9 +1705,6 @@ static void SparsifyGeneric(const void* dense_raw_data, size_t n_dense_elements, } else { SetIndices(gathered_span, raw_indices, indices); } - if constexpr (endian::native != endian::little) { - utils::ConvertRawDataInTensorProto((ONNX_NAMESPACE::TensorProto*)&indices); - } } else { indices.set_data_type(ONNX_NAMESPACE::TensorProto_DataType_INT8); utils::SetRawDataInTensorProto(indices,std::string()); diff --git a/onnxruntime/core/graph/graph.cc b/onnxruntime/core/graph/graph.cc index 70a318029e1c7..7090384587c06 100644 --- a/onnxruntime/core/graph/graph.cc +++ b/onnxruntime/core/graph/graph.cc @@ -1194,15 +1194,13 @@ Graph::Graph(const Model& owning_model, const gsl::not_null tensor{graph_proto_->add_initializer()}; auto status = utils::ConstantNodeProtoToTensorProto(node, model_path, *tensor); if constexpr (endian::native != endian::little) { - const AttributeProto& attrib = node.attribute(0); - if (attrib.type() == AttributeProto_AttributeType_SPARSE_TENSOR) - { - const TensorProto& sparse_values = node.attribute(0).sparse_tensor().values(); - if ((!(sparse_values.has_raw_data())) && tensor->has_raw_data()) - { + const AttributeProto& attrib = node.attribute(0); + if (attrib.type() == AttributeProto_AttributeType_SPARSE_TENSOR) { + const TensorProto& sparse_values = node.attribute(0).sparse_tensor().values(); + if ((!(sparse_values.has_raw_data())) && tensor->has_raw_data()) { onnxruntime::utils::ConvertRawDataInTensorProto(tensor); - } - } + } + } } ORT_ENFORCE(status.IsOK(), status.ToString()); // Ensure initializers are also graph inputs.