From 9a36755397ba993d6cc6597c39d9cf2bbcee3d50 Mon Sep 17 00:00:00 2001 From: Eric Kunze Date: Tue, 11 Jul 2023 13:27:36 -0700 Subject: Update tensor allocation logging Also move a large amount of redundant code to a macro Signed-off-by: Eric Kunze Change-Id: Ib1dcb2ce15e56076e444a4739c4d1292389c4a78 --- reference_model/src/subgraph_traverser.cc | 2 +- reference_model/src/tensor.cc | 435 +++++------------------------- reference_model/src/tensor.h | 1 + 3 files changed, 63 insertions(+), 375 deletions(-) diff --git a/reference_model/src/subgraph_traverser.cc b/reference_model/src/subgraph_traverser.cc index c02581f..5f6fd01 100644 --- a/reference_model/src/subgraph_traverser.cc +++ b/reference_model/src/subgraph_traverser.cc @@ -420,7 +420,7 @@ int SubgraphTraverser::allocateTensor() if (!ts->GetData().empty()) { - DEBUG_INFO(GT, "Allocating tensor %s", tensor->getName().c_str()); + DEBUG_INFO(GT, "Setting data for tensor %s", tensor->getName().c_str()); auto serialization_dtype = ts->GetDtype(); switch (serialization_dtype) { diff --git a/reference_model/src/tensor.cc b/reference_model/src/tensor.cc index 1cfa6c5..c5f5e02 100644 --- a/reference_model/src/tensor.cc +++ b/reference_model/src/tensor.cc @@ -2605,382 +2605,69 @@ int TosaReference::Tensor6::getTensorValueBool(const size_t bufLen, bool* return 0; } -template <> -int TosaReference::Tensor0::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor0(); - - if (tensor) - return 0; - else - return 1; -} -template <> -int TosaReference::Tensor1::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor1(shape[0]); - if (tensor) - return 0; - else - return 1; -} -template <> -int TosaReference::Tensor2::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor2(shape[0], shape[1]); - if (tensor) - return 0; - else - return 1; -} - -template <> -int TosaReference::Tensor3::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor3(shape[0], shape[1], shape[2]); - if (tensor) - return 0; - else - return 1; -} - -template <> -int TosaReference::Tensor4::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor4(shape[0], shape[1], shape[2], shape[3]); - if (tensor) - return 0; - else - return 1; -} - -template <> -int TosaReference::Tensor5::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor5(shape[0], shape[1], shape[2], shape[3], shape[4]); - if (tensor) - return 0; - else - return 1; -} - -template <> -int TosaReference::Tensor6::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor6(shape[0], shape[1], shape[2], shape[3], shape[4], shape[5]); - if (tensor) - return 0; - else - return 1; -} - -template <> -int TosaReference::Tensor0::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor0(); - - if (tensor) - return 0; - else - return 1; -} -template <> -int TosaReference::Tensor1::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor1(shape[0]); - if (tensor) - return 0; - else - return 1; -} -template <> -int TosaReference::Tensor2::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor2(shape[0], shape[1]); - if (tensor) - return 0; - else - return 1; -} - -template <> -int TosaReference::Tensor3::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor3(shape[0], shape[1], shape[2]); - if (tensor) - return 0; - else - return 1; -} - -template <> -int TosaReference::Tensor4::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor4(shape[0], shape[1], shape[2], shape[3]); - if (tensor) - return 0; - else - return 1; -} - -template <> -int TosaReference::Tensor5::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor5(shape[0], shape[1], shape[2], shape[3], shape[4]); - if (tensor) - return 0; - else - return 1; -} - -template <> -int TosaReference::Tensor6::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor6(shape[0], shape[1], shape[2], shape[3], shape[4], shape[5]); - if (tensor) - return 0; - else - return 1; -} - -template <> -int TosaReference::Tensor0::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor0(); - if (tensor) - return 0; - else - return 1; -} -template <> -int TosaReference::Tensor1::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor1(shape[0]); - if (tensor) - return 0; - else - return 1; -} -template <> -int TosaReference::Tensor2::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor2(shape[0], shape[1]); - if (tensor) - return 0; - else - return 1; -} - -template <> -int TosaReference::Tensor3::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor3(shape[0], shape[1], shape[2]); - if (tensor) - return 0; - else - return 1; -} - -template <> -int TosaReference::Tensor4::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor4(shape[0], shape[1], shape[2], shape[3]); - if (tensor) - return 0; - else - return 1; -} - -template <> -int TosaReference::Tensor5::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor5(shape[0], shape[1], shape[2], shape[3], shape[4]); - if (tensor) - return 0; - else - return 1; -} - -template <> -int TosaReference::Tensor6::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor6(shape[0], shape[1], shape[2], shape[3], shape[4], shape[5]); - if (tensor) - return 0; - else - return 1; -} - -template <> -int TosaReference::Tensor0::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor0(); - if (tensor) - return 0; - else - return 1; -} -template <> -int TosaReference::Tensor1::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor1(shape[0]); - if (tensor) - return 0; - else - return 1; -} -template <> -int TosaReference::Tensor2::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor2(shape[0], shape[1]); - if (tensor) - return 0; - else - return 1; -} - -template <> -int TosaReference::Tensor3::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor3(shape[0], shape[1], shape[2]); - if (tensor) - return 0; - else - return 1; -} - -template <> -int TosaReference::Tensor4::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor4(shape[0], shape[1], shape[2], shape[3]); - if (tensor) - return 0; - else - return 1; -} - -template <> -int TosaReference::Tensor5::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor5(shape[0], shape[1], shape[2], shape[3], shape[4]); - if (tensor) - return 0; - else - return 1; -} - -template <> -int TosaReference::Tensor6::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor6(shape[0], shape[1], shape[2], shape[3], shape[4], shape[5]); - if (tensor) - return 0; - else - return 1; -} - -template <> -int TosaReference::Tensor0::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor0(); - if (tensor) - return 0; - else - return 1; -} -template <> -int TosaReference::Tensor1::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor1(shape[0]); - if (tensor) - return 0; - else - return 1; -} -template <> -int TosaReference::Tensor2::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor2(shape[0], shape[1]); - if (tensor) - return 0; - else - return 1; -} - -template <> -int TosaReference::Tensor3::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor3(shape[0], shape[1], shape[2]); - if (tensor) - return 0; - else - return 1; -} - -template <> -int TosaReference::Tensor4::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor4(shape[0], shape[1], shape[2], shape[3]); - if (tensor) - return 0; - else - return 1; -} +#define TOSAREF_ZERORANK_TENSOR_ALLOCATE(dtype) \ + template <> \ + int TosaReference::Tensor0::allocate() \ + { \ + ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); \ + tensor = new ETensor0(); \ + \ + if (tensor) \ + return 0; \ + else \ + return 1; \ + } -template <> -int TosaReference::Tensor5::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor5(shape[0], shape[1], shape[2], shape[3], shape[4]); - if (tensor) - return 0; - else - return 1; -} +#define TOSAREF_TENSOR_ALLOCATE(rank, dtype) \ + template <> \ + int TosaReference::Tensor##rank::allocate() \ + { \ + ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); \ + std::array arrshape; \ + std::copy_n(shape.begin(), rank, arrshape.begin()); \ + tensor = new ETensor##rank(arrshape); \ + \ + if (tensor) \ + return 0; \ + else \ + return 1; \ + } -template <> -int TosaReference::Tensor6::allocate() -{ - ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); - tensor = new ETensor6(shape[0], shape[1], shape[2], shape[3], shape[4], shape[5]); - if (tensor) - return 0; - else - return 1; -} +TOSAREF_ZERORANK_TENSOR_ALLOCATE(double) +TOSAREF_TENSOR_ALLOCATE(1, double) +TOSAREF_TENSOR_ALLOCATE(2, double) +TOSAREF_TENSOR_ALLOCATE(3, double) +TOSAREF_TENSOR_ALLOCATE(4, double) +TOSAREF_TENSOR_ALLOCATE(5, double) +TOSAREF_TENSOR_ALLOCATE(6, double) +TOSAREF_ZERORANK_TENSOR_ALLOCATE(float) +TOSAREF_TENSOR_ALLOCATE(1, float) +TOSAREF_TENSOR_ALLOCATE(2, float) +TOSAREF_TENSOR_ALLOCATE(3, float) +TOSAREF_TENSOR_ALLOCATE(4, float) +TOSAREF_TENSOR_ALLOCATE(5, float) +TOSAREF_TENSOR_ALLOCATE(6, float) +TOSAREF_ZERORANK_TENSOR_ALLOCATE(int32_t) +TOSAREF_TENSOR_ALLOCATE(1, int32_t) +TOSAREF_TENSOR_ALLOCATE(2, int32_t) +TOSAREF_TENSOR_ALLOCATE(3, int32_t) +TOSAREF_TENSOR_ALLOCATE(4, int32_t) +TOSAREF_TENSOR_ALLOCATE(5, int32_t) +TOSAREF_TENSOR_ALLOCATE(6, int32_t) +TOSAREF_ZERORANK_TENSOR_ALLOCATE(int64_t) +TOSAREF_TENSOR_ALLOCATE(1, int64_t) +TOSAREF_TENSOR_ALLOCATE(2, int64_t) +TOSAREF_TENSOR_ALLOCATE(3, int64_t) +TOSAREF_TENSOR_ALLOCATE(4, int64_t) +TOSAREF_TENSOR_ALLOCATE(5, int64_t) +TOSAREF_TENSOR_ALLOCATE(6, int64_t) +TOSAREF_ZERORANK_TENSOR_ALLOCATE(bool) +TOSAREF_TENSOR_ALLOCATE(1, bool) +TOSAREF_TENSOR_ALLOCATE(2, bool) +TOSAREF_TENSOR_ALLOCATE(3, bool) +TOSAREF_TENSOR_ALLOCATE(4, bool) +TOSAREF_TENSOR_ALLOCATE(5, bool) +TOSAREF_TENSOR_ALLOCATE(6, bool) template <> int TosaReference::Tensor0::dumpTensor(FILE* out) const diff --git a/reference_model/src/tensor.h b/reference_model/src/tensor.h index 21cf148..d49e004 100644 --- a/reference_model/src/tensor.h +++ b/reference_model/src/tensor.h @@ -324,6 +324,7 @@ public: { if (tensor) { + DEBUG_INFO(GT, "Deallocating tensor %s", tensorName.c_str()); delete tensor; } tensor = nullptr; -- cgit v1.2.1