From e8b23817a4acc7c2cabc916e732b0e8e7fc8e5bd Mon Sep 17 00:00:00 2001
From: Ranjit Ranjan <ranjit.sastra@gmail.com>
Date: Mon, 17 Apr 2023 01:26:00 -0500
Subject: [PATCH 2/7] merge changes from tvkaibranch4 to rel-1.14.1

---
 .../test/contrib_ops/function_test_util.h     | 26 ++++++++-
 .../test/framework/sparse_kernels_test.cc     |  6 ++
 .../test/framework/tensorutils_test.cc        | 56 +++++++++++++++++++
 .../test/framework/test_tensor_loader.cc      | 17 +++++-
 onnxruntime/test/onnx/main.cc                 |  4 +-
 onnxruntime/test/onnx/tensorprotoutils.cc     | 27 ++++++++-
 .../optimizer/graph_transform_test_builder.h  |  5 ++
 .../test/optimizer/initializer_test.cc        |  4 +-
 .../test/optimizer/nchwc_optimizer_test.cc    |  4 ++
 .../test/providers/provider_test_utils.cc     |  3 +
 .../test/python/onnxruntime_test_python.py    |  2 +-
 .../symbols.exp                               |  2 +
 tools/ci_build/gen_def.py                     | 12 ++--
 13 files changed, 154 insertions(+), 14 deletions(-)
 create mode 100644 onnxruntime/test/testdata/custom_execution_provider_library/symbols.exp

diff --git a/onnxruntime/test/contrib_ops/function_test_util.h b/onnxruntime/test/contrib_ops/function_test_util.h
index 9b104eefb..f2b8eed25 100644
--- a/onnxruntime/test/contrib_ops/function_test_util.h
+++ b/onnxruntime/test/contrib_ops/function_test_util.h
@@ -102,11 +102,33 @@ struct FunctionTestCase {
 
   template <typename T, bool GenData = true>
   void AddInput(std::string input_name, std::vector<int64_t> shape) {
+    std::vector<int64_t> shapeforrandom;
+    shapeforrandom = shape;
+    char *bytes1 = (char *)shape.data();
+    if (1) {
+#ifdef DEBUG_AIX
+         std::cout<<"Doing byte swapping in AddInput test/contrib_ops/function_test_util.h "<<std::endl;
+#endif
+         const size_t element_size = sizeof(T);
+         const size_t num_elements = shape.size()/element_size;
+         for (size_t i = 0; i < num_elements; ++i) {
+             char* start_byte = bytes1 + i * element_size;
+             char* end_byte = start_byte + element_size - 1;
+             /* keep swapping */
+             for (size_t count = 0; count < element_size / 2; ++count) {
+                  char temp = *start_byte;
+                  *start_byte = *end_byte;
+                  *end_byte = temp;
+                  ++start_byte;
+                  --end_byte;
+             }
+         }
+    }
     auto arg_type = TensorType(utils::ToTensorProtoElementType<T>(), shape);
     input_args.emplace_back(input_name, &arg_type);
 
     if (GenData) {
-      std::vector<T> data = random<T>(shape);
+      std::vector<T> data = random<T>(shapeforrandom);
       OrtValue ort_value;
       CreateMLValue<T>(provider->GetAllocator(0, OrtMemTypeDefault), shape, data, &ort_value);
       input_values.push_back(std::make_pair(input_name, ort_value));
@@ -149,4 +171,4 @@ struct FunctionTestCase {
 };
 
 }  // namespace test
-}  // namespace onnxruntime
\ No newline at end of file
+}  // namespace onnxruntime
diff --git a/onnxruntime/test/framework/sparse_kernels_test.cc b/onnxruntime/test/framework/sparse_kernels_test.cc
index 3b3884325..b38267db4 100644
--- a/onnxruntime/test/framework/sparse_kernels_test.cc
+++ b/onnxruntime/test/framework/sparse_kernels_test.cc
@@ -705,6 +705,9 @@ struct InsertIndices {
       // Conversion on the fly to the target data type
       std::vector<T> indices(indices_data.cbegin(), indices_data.cend());
       indices_tp.mutable_raw_data()->assign(reinterpret_cast<const char*>(indices.data()), indices.size() * sizeof(T));
+      if constexpr (endian::native != endian::little) {
+       utils::ConvertRawDataInTensorProto((ONNX_NAMESPACE::TensorProto*)&indices_tp);
+      }
     }
   }
 };
@@ -838,6 +841,9 @@ template <typename T>
 static void RawDataWriter(const std::vector<T>& values, TensorProto& tp, TensorProto_DataType datatype) {
   tp.set_data_type(datatype);
   tp.set_raw_data(values.data(), values.size() * sizeof(T));
+  if constexpr (endian::native != endian::little) {
+       utils::ConvertRawDataInTensorProto((ONNX_NAMESPACE::TensorProto*)&tp);
+  }
 }
 
 int64_t ActualSize(const TensorProto& actual) {
diff --git a/onnxruntime/test/framework/tensorutils_test.cc b/onnxruntime/test/framework/tensorutils_test.cc
index e632f0f44..bffcbb048 100644
--- a/onnxruntime/test/framework/tensorutils_test.cc
+++ b/onnxruntime/test/framework/tensorutils_test.cc
@@ -31,6 +31,9 @@ void TestUnpackFloatTensor(TensorProto_DataType type, const Path& model_path) {
     memcpy(rawdata + i * sizeof(T), &(f[i]), sizeof(T));
   }
   float_tensor_proto.set_raw_data(rawdata, len);
+  if constexpr (endian::native != endian::little) {
+       utils::ConvertRawDataInTensorProto((ONNX_NAMESPACE::TensorProto*)&float_tensor_proto);
+  }
   T float_data2[4];
   auto status = UnpackTensor(float_tensor_proto, model_path, float_data2, 4);
   EXPECT_TRUE(status.IsOK()) << status.ErrorMessage();
@@ -104,6 +107,23 @@ std::vector<BFloat16> CreateValues<BFloat16>() {
 
 template <typename T>
 void WriteDataToFile(FILE* fp, const std::vector<T>& test_data) {
+  char *bytes1 = (char *)test_data.data();
+  if constexpr (endian::native != endian::little) {
+     const size_t element_size = sizeof(T);
+     const size_t num_elements = test_data.size();
+     for (size_t i = 0; i < num_elements; ++i) {
+         char* start_byte = bytes1 + i * element_size;
+         char* end_byte = start_byte + element_size - 1;
+         /* keep swapping */
+         for (size_t count = 0; count < element_size / 2; ++count) {
+             char temp = *start_byte;
+             *start_byte = *end_byte;
+             *end_byte = temp;
+             ++start_byte;
+             --end_byte;
+         }
+     }
+  }
   size_t size_in_bytes = test_data.size() * sizeof(T);
   ASSERT_EQ(size_in_bytes, fwrite(test_data.data(), 1, size_in_bytes, fp));
 }
@@ -148,6 +168,24 @@ void UnpackAndValidate(const TensorProto& tensor_proto, const Path& model_path,
   auto st = utils::UnpackTensor(tensor_proto, model_path, val.data(), test_data.size());
   ASSERT_TRUE(st.IsOK()) << st.ErrorMessage();
 
+  char *bytes1 = (char *)val.data();
+  if constexpr (endian::native != endian::little) {
+     const size_t element_size = sizeof(T);
+     const size_t num_elements = test_data.size();
+     for (size_t i = 0; i < num_elements; ++i) {
+         char* start_byte = bytes1 + i * element_size;
+         char* end_byte = start_byte + element_size - 1;
+         /* keep swapping */
+         for (size_t count = 0; count < element_size / 2; ++count) {
+              char temp = *start_byte;
+              *start_byte = *end_byte;
+              *end_byte = temp;
+              ++start_byte;
+              --end_byte;
+         }
+     }
+  }
+
   // Validate data
   for (size_t i = 0; i < test_data.size(); i++) {
     ASSERT_TRUE(val[i] == test_data[i]);  // need to use ASSERT_TRUE with '==' to handle MFLoat16 and BFloat16
@@ -325,6 +363,24 @@ static void TestConstantNodeConversionWithExternalData(TensorProto_DataType type
   std::vector<T> val(test_data.size());
   auto st = utils::UnpackTensor(tp, model_path, val.data(), test_data.size());
   ASSERT_TRUE(st.IsOK()) << st.ErrorMessage();
+  char *bytes1 = (char *)val.data();
+  if constexpr (endian::native != endian::little) {
+     const size_t element_size = sizeof(T);
+     const size_t num_elements = test_data.size();
+     for (size_t i = 0; i < num_elements; ++i) {
+         char* start_byte = bytes1 + i * element_size;
+         char* end_byte = start_byte + element_size - 1;
+         /* keep swapping */
+         for (size_t count = 0; count < element_size / 2; ++count) {
+              char temp = *start_byte;
+              *start_byte = *end_byte;
+              *end_byte = temp;
+              ++start_byte;
+              --end_byte;
+         }
+     }
+  }
+
   for (size_t i = 0; i < test_data.size(); i++) {
     ASSERT_EQ(val[i], test_data[i]);
   }
diff --git a/onnxruntime/test/framework/test_tensor_loader.cc b/onnxruntime/test/framework/test_tensor_loader.cc
index f627409bb..29f6e7e89 100644
--- a/onnxruntime/test/framework/test_tensor_loader.cc
+++ b/onnxruntime/test/framework/test_tensor_loader.cc
@@ -72,6 +72,21 @@ static void run_external_data_test() {
   std::unique_ptr<ORTCHAR_T, decltype(&DeleteFileFromDisk)> file_deleter(const_cast<ORTCHAR_T*>(filename.c_str()),
                                                                          DeleteFileFromDisk);
   float test_data[] = {1.0f, 2.2f, 3.5f};
+  int element_size = sizeof(float); 
+  char *bytes = (char *)&test_data;
+  const size_t num_elements = sizeof(test_data);
+  for (size_t i = 0; i < num_elements; ++i) {
+         char* start_byte =  bytes + i * element_size;
+         char* end_byte = start_byte + element_size - 1;
+         /* keep swapping */
+         for (size_t count = 0; count < element_size / 2; ++count) {
+              char temp = *start_byte;
+              *start_byte = *end_byte;
+              *end_byte = temp;
+              ++start_byte;
+              --end_byte;
+         }
+  }
   ASSERT_EQ(sizeof(test_data), fwrite(test_data, 1, sizeof(test_data), fp));
   ASSERT_EQ(0, fclose(fp));
   // construct a tensor proto
@@ -97,7 +112,7 @@ static void run_external_data_test() {
     ASSERT_NE(len, (DWORD)0);
     cwd.append(ORT_TSTR("\\fake.onnx"));
 #else
-    char* p = getcwd(nullptr, 0);
+    char* p = getcwd(nullptr, 512);
     ASSERT_NE(p, nullptr);
     cwd = p;
     free(p);
diff --git a/onnxruntime/test/onnx/main.cc b/onnxruntime/test/onnx/main.cc
index 11d635519..1fc4c9bda 100644
--- a/onnxruntime/test/onnx/main.cc
+++ b/onnxruntime/test/onnx/main.cc
@@ -6,8 +6,10 @@
 #include <fstream>
 #include <string>
 #include <unordered_map>
-#ifdef _WIN32
+#if defined(_WIN32)
 #include "getopt.h"
+#elif defined(_AIX)
+#include <thread>
 #else
 #include <getopt.h>
 #include <thread>
diff --git a/onnxruntime/test/onnx/tensorprotoutils.cc b/onnxruntime/test/onnx/tensorprotoutils.cc
index afafd0c6b..22929857c 100644
--- a/onnxruntime/test/onnx/tensorprotoutils.cc
+++ b/onnxruntime/test/onnx/tensorprotoutils.cc
@@ -69,10 +69,31 @@ static void UnpackTensorWithRawData(const void* raw_data, size_t raw_data_length
                                  expected_size_in_bytes, ", got ", raw_data_length),
                       OrtErrorCode::ORT_FAIL);
   if constexpr (endian::native != endian::little) {
-    ORT_CXX_API_THROW("UnpackTensorWithRawData only handles little-endian native byte order for now.",
-                      OrtErrorCode::ORT_NOT_IMPLEMENTED);
+	/* Convert Endianness and copy */
+    char *nraw_data = (char*)malloc(raw_data_length);
+    memcpy(nraw_data, raw_data, raw_data_length);
+    char* bytes = nraw_data;
+    size_t element_size = sizeof(T);
+    size_t num_elements = raw_data_length / element_size;
+
+    for (size_t i = 0; i < num_elements; ++i) {
+        char* start_byte = bytes + i * element_size;
+        char* end_byte = start_byte + element_size - 1;
+        /* keep swapping */
+        for (size_t count = 0; count < element_size / 2; ++count) {
+            char temp = *start_byte;
+            *start_byte = *end_byte;
+            *end_byte = temp;
+            ++start_byte;
+            --end_byte;
+        }
+    }
+    memcpy(p_data, nraw_data, raw_data_length);
+    free(nraw_data);
   }
-  memcpy(p_data, raw_data, raw_data_length);
+  else{
+    memcpy(p_data, raw_data, raw_data_length);
+ }
 }
 
 // This macro doesn't work for Float16/bool/string tensors
diff --git a/onnxruntime/test/optimizer/graph_transform_test_builder.h b/onnxruntime/test/optimizer/graph_transform_test_builder.h
index 14c73b2b5..3678fa4d2 100644
--- a/onnxruntime/test/optimizer/graph_transform_test_builder.h
+++ b/onnxruntime/test/optimizer/graph_transform_test_builder.h
@@ -11,6 +11,7 @@
 #include "core/framework/framework_common.h"
 #include "core/optimizer/graph_transformer_level.h"
 #include "core/graph/onnx_protobuf.h"
+#include "core/framework/tensorprotoutils.h"
 #include "test/framework/test_utils.h"
 #include "test/common/tensor_op_test_utils.h"
 #include "test/framework/test_utils.h"
@@ -143,6 +144,10 @@ class ModelTestBuilder {
     tensor_proto.set_data_type(utils::ToTensorProtoElementType<T>());
     tensor_proto.set_raw_data(data.data(), data.size() * sizeof(T));
 
+	if (endian::native != endian::little){
+		 ::onnxruntime::utils::ConvertRawDataInTensorProto(&tensor_proto);
+	}
+
     for (auto& dim : shape) {
       tensor_proto.add_dims(dim);
     }
diff --git a/onnxruntime/test/optimizer/initializer_test.cc b/onnxruntime/test/optimizer/initializer_test.cc
index e73950bbd..b86d8f65e 100644
--- a/onnxruntime/test/optimizer/initializer_test.cc
+++ b/onnxruntime/test/optimizer/initializer_test.cc
@@ -163,7 +163,9 @@ void TestInitializerRawData() {
   tensor_proto.add_dims(3);
   tensor_proto.add_dims(4);
   tensor_proto.set_raw_data(data.data(), data.size() * sizeof(T));
-
+  if constexpr (endian::native != endian::little) {
+	utils::ConvertRawDataInTensorProto((ONNX_NAMESPACE::TensorProto *) &tensor_proto);
+  }
   const Initializer init(tensor_proto, Path());
 
   for (size_t idx = 0; idx < data.size(); idx++) {
diff --git a/onnxruntime/test/optimizer/nchwc_optimizer_test.cc b/onnxruntime/test/optimizer/nchwc_optimizer_test.cc
index 06beedbd5..3038bea52 100644
--- a/onnxruntime/test/optimizer/nchwc_optimizer_test.cc
+++ b/onnxruntime/test/optimizer/nchwc_optimizer_test.cc
@@ -6,6 +6,7 @@
 #include "core/mlas/inc/mlas.h"
 #include "core/session/environment.h"
 #include "core/session/inference_session.h"
+#include "core/framework/tensorprotoutils.h"
 #include "test/compare_ortvalue.h"
 #include "test/test_environment.h"
 #include "test/framework/test_utils.h"
@@ -64,6 +65,9 @@ struct NchwcTestHelper {
     tensor_proto.set_data_type(utils::ToTensorProtoElementType<T>());
     tensor_proto.set_raw_data(data.data(), data.size() * sizeof(T));
 
+	if constexpr (endian::native != endian::little) {
+		::onnxruntime::utils::ConvertRawDataInTensorProto((ONNX_NAMESPACE::TensorProto*)&tensor_proto);
+	}
     for (auto& dim : shape) {
       tensor_proto.add_dims(dim);
     }
diff --git a/onnxruntime/test/providers/provider_test_utils.cc b/onnxruntime/test/providers/provider_test_utils.cc
index fa8ef69f9..82823b9bc 100644
--- a/onnxruntime/test/providers/provider_test_utils.cc
+++ b/onnxruntime/test/providers/provider_test_utils.cc
@@ -776,6 +776,9 @@ void OpTester::AddInitializers(onnxruntime::Graph& graph) {
     } else {
       auto buffer_size = tensor.DataType()->Size() * shape.Size();
       tensor_proto.set_raw_data(tensor.DataRaw(), buffer_size);
+	  if constexpr (endian::native != endian::little) {
+		::onnxruntime::utils::ConvertRawDataInTensorProto((ONNX_NAMESPACE::TensorProto*)&tensor_proto);
+      } 
     }
     // 4. name
     tensor_proto.set_name(data.def_.Name());
diff --git a/onnxruntime/test/python/onnxruntime_test_python.py b/onnxruntime/test/python/onnxruntime_test_python.py
index 89fd90ad3..d6e54bde9 100644
--- a/onnxruntime/test/python/onnxruntime_test_python.py
+++ b/onnxruntime/test/python/onnxruntime_test_python.py
@@ -1213,7 +1213,7 @@ class TestInferenceSession(unittest.TestCase):
         )
 
     def testMemoryArenaShrinkage(self):
-        if platform.architecture()[0] == "32bit" or "ppc" in platform.machine() or "powerpc" in platform.machine():
+        if platform.architecture()[0] == "32bit" or "ppc" in platform.machine() or "powerpc" in platform.machine() or "powerpc" in platform.processor():
             # on x86 or ppc builds, the CPU allocator does not use an arena
             print("Skipping testMemoryArenaShrinkage in 32bit or powerpc platform.")
         else:
diff --git a/onnxruntime/test/testdata/custom_execution_provider_library/symbols.exp b/onnxruntime/test/testdata/custom_execution_provider_library/symbols.exp
new file mode 100644
index 000000000..c9a5232be
--- /dev/null
+++ b/onnxruntime/test/testdata/custom_execution_provider_library/symbols.exp
@@ -0,0 +1,2 @@
+GetProvider   
+ProviderHashFunc
diff --git a/tools/ci_build/gen_def.py b/tools/ci_build/gen_def.py
index b020fd2f9..5fc8a5fb3 100755
--- a/tools/ci_build/gen_def.py
+++ b/tools/ci_build/gen_def.py
@@ -41,8 +41,9 @@ with open(args.output, "w") as file:
     elif args.style == "xcode":
         pass  # xcode compile don't has any header.
     else:
-        file.write("VERS_%s {\n" % VERSION_STRING)
-        file.write(" global:\n")
+		pass
+        #file.write("VERS_%s {\n" % VERSION_STRING)
+        #file.write(" global:\n")
 
     for symbol in symbols:
         if args.style == "vc":
@@ -54,9 +55,10 @@ with open(args.output, "w") as file:
         symbol_index += 1
 
     if args.style == "gcc":
-        file.write(" local:\n")
-        file.write("    *;\n")
-        file.write("};   \n")
+		pass
+        #file.write(" local:\n")
+        #file.write("    *;\n")
+        #file.write("};   \n")
 
 with open(args.output_source, "w") as file:
     file.write("#include <onnxruntime_c_api.h>\n")
-- 
2.38.3

