From cfb9213762d218eff6ecd3f1cb39dc1baf4cbdc1 Mon Sep 17 00:00:00 2001 From: Dayoung Lee <dayoung.lee@samsung.com> Date: Mon, 20 Jan 2025 16:00:52 +0900 Subject: [PATCH 1/8] [luci/pass] Introduce ArrayIndex helper Let's introduce Array4DIndex to help calculating 4D array index. ONE-DCO-Signed-off-by: Dayoung Lee <dayoung.lee@samsung.com> --- compiler/luci/pass/src/helpers/ArrayIndex.cpp | 48 +++++++++++++++++ compiler/luci/pass/src/helpers/ArrayIndex.h | 52 +++++++++++++++++++ .../luci/pass/src/helpers/ArrayIndex.test.cpp | 40 ++++++++++++++ 3 files changed, 140 insertions(+) create mode 100644 compiler/luci/pass/src/helpers/ArrayIndex.cpp create mode 100644 compiler/luci/pass/src/helpers/ArrayIndex.h create mode 100644 compiler/luci/pass/src/helpers/ArrayIndex.test.cpp diff --git a/compiler/luci/pass/src/helpers/ArrayIndex.cpp b/compiler/luci/pass/src/helpers/ArrayIndex.cpp new file mode 100644 index 00000000000..28faf4a31ce --- /dev/null +++ b/compiler/luci/pass/src/helpers/ArrayIndex.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ArrayIndex.h" + +#include <cassert> + +namespace luci +{ + +Array4DIndex::Array4DIndex(uint32_t D0, uint32_t D1, uint32_t D2, uint32_t D3) + : _dim{D0, D1, D2, D3} +{ + assert(D0 > 0 && D1 > 0 && D2 > 0 && D3 > 0); + + _strides[3] = 1; + _strides[2] = D3; + _strides[1] = D3 * D2; + _strides[0] = D3 * D2 * D1; + + for (int i = 0; i < 4; ++i) + { + assert(_strides[i] > 0); + } +} +uint32_t Array4DIndex::operator()(uint32_t i0, uint32_t i1, uint32_t i2, uint32_t i3) const +{ + assert(i0 < _dim[0] && i1 < _dim[1] && i2 < _dim[2] && i3 < _dim[3]); + return i0 * _strides[0] + i1 * _strides[1] + i2 * _strides[2] + i3 * _strides[3]; +} + +uint32_t Array4DIndex::size(void) const { return _strides[3]; } +uint32_t Array4DIndex::stride(uint32_t axis) const { return _strides[axis]; } + +} // namespace luci diff --git a/compiler/luci/pass/src/helpers/ArrayIndex.h b/compiler/luci/pass/src/helpers/ArrayIndex.h new file mode 100644 index 00000000000..3658ffae23b --- /dev/null +++ b/compiler/luci/pass/src/helpers/ArrayIndex.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __LUCI_ARRAY_INDEX_H__ +#define __LUCI_ARRAY_INDEX_H__ + +#include <cstdint> + +namespace luci +{ + +/** + * @brief Index class for 4D tensor (NHWC) to calculate linear index from multi-dimensional indices. + */ +class Array4DIndex +{ +public: + Array4DIndex(uint32_t N, uint32_t H, uint32_t W, uint32_t D); + /** + * @brief Calculate linear index from multi-dimensional indices. + */ + uint32_t operator()(uint32_t n, uint32_t h, uint32_t w, uint32_t d) const; + /** + * @brief Get total number of elements in the tensor. + */ + uint32_t size(void) const; + /** + * @brief Get stride of the given axis. + */ + uint32_t stride(uint32_t axis) const; + +protected: + uint32_t _dim[4]; + uint32_t _strides[4]; +}; + +} // namespace luci + +#endif // __LUCI_ARRAY_INDEX_H__ diff --git a/compiler/luci/pass/src/helpers/ArrayIndex.test.cpp b/compiler/luci/pass/src/helpers/ArrayIndex.test.cpp new file mode 100644 index 00000000000..3e0bb39fdd0 --- /dev/null +++ b/compiler/luci/pass/src/helpers/ArrayIndex.test.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ArrayIndex.h" + +#include <gtest/gtest.h> + +TEST(LuciPassHelpersArrayIndex, array_index_4d) +{ + luci::Array4DIndex idx(5, 4, 3, 2); + + EXPECT_EQ(idx.rank(), 4); + + EXPECT_EQ(idx(0, 0, 0, 0), 0); + + EXPECT_EQ(idx(1, 0, 0, 0), idx.stride(0)); + EXPECT_EQ(idx(0, 1, 0, 0), idx.stride(1)); + EXPECT_EQ(idx(0, 0, 1, 0), idx.stride(2)); + EXPECT_EQ(idx(0, 0, 0, 1), idx.stride(3)); + + EXPECT_EQ(idx(4, 3, 2, 1), 4 * 4 * 3 * 2 + 3 * 3 * 2 + 2 * 2 + 1); +} + +TEST(LuciPassHelpersArrayIndex, array_index_4d_NEG) +{ + ASSERT_THROW(luci::Array4DIndex idx(5, 4, 3, -1)); +} From 5e7e09ced86cc1861e279d21d029a68fb52df55b Mon Sep 17 00:00:00 2001 From: Dayoung Lee <dayoung.lee@samsung.com> Date: Mon, 20 Jan 2025 17:03:33 +0900 Subject: [PATCH 2/8] Apply feedback --- compiler/luci/pass/src/helpers/ArrayIndex.cpp | 2 ++ compiler/luci/pass/src/helpers/ArrayIndex.h | 27 +++++++++---------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/compiler/luci/pass/src/helpers/ArrayIndex.cpp b/compiler/luci/pass/src/helpers/ArrayIndex.cpp index 28faf4a31ce..dcdc1fda091 100644 --- a/compiler/luci/pass/src/helpers/ArrayIndex.cpp +++ b/compiler/luci/pass/src/helpers/ArrayIndex.cpp @@ -36,6 +36,7 @@ Array4DIndex::Array4DIndex(uint32_t D0, uint32_t D1, uint32_t D2, uint32_t D3) assert(_strides[i] > 0); } } + uint32_t Array4DIndex::operator()(uint32_t i0, uint32_t i1, uint32_t i2, uint32_t i3) const { assert(i0 < _dim[0] && i1 < _dim[1] && i2 < _dim[2] && i3 < _dim[3]); @@ -43,6 +44,7 @@ uint32_t Array4DIndex::operator()(uint32_t i0, uint32_t i1, uint32_t i2, uint32_ } uint32_t Array4DIndex::size(void) const { return _strides[3]; } + uint32_t Array4DIndex::stride(uint32_t axis) const { return _strides[axis]; } } // namespace luci diff --git a/compiler/luci/pass/src/helpers/ArrayIndex.h b/compiler/luci/pass/src/helpers/ArrayIndex.h index 3658ffae23b..ff3f18e51cf 100644 --- a/compiler/luci/pass/src/helpers/ArrayIndex.h +++ b/compiler/luci/pass/src/helpers/ArrayIndex.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef __LUCI_ARRAY_INDEX_H__ -#define __LUCI_ARRAY_INDEX_H__ +#ifndef __LUCI_PASS_HELPERS_ARRAY_INDEX_H__ +#define __LUCI_PASS_HELPERS_ARRAY_INDEX_H__ #include <cstdint> @@ -23,23 +23,20 @@ namespace luci { /** - * @brief Index class for 4D tensor (NHWC) to calculate linear index from multi-dimensional indices. + * @brief Index class for 4D tensor to calculate linear index from multi-dimensional indices. */ class Array4DIndex { public: - Array4DIndex(uint32_t N, uint32_t H, uint32_t W, uint32_t D); - /** - * @brief Calculate linear index from multi-dimensional indices. - */ - uint32_t operator()(uint32_t n, uint32_t h, uint32_t w, uint32_t d) const; - /** - * @brief Get total number of elements in the tensor. - */ + Array4DIndex(uint32_t d0, uint32_t d1, uint32_t d2, uint32_t d3); + + /// @brief Calculate linear index from multi-dimensional indices. + uint32_t operator()(uint32_t i0, uint32_t i1, uint32_t i2, uint32_t i3) const; + + /// @brief Get total number of elements in the tensor. uint32_t size(void) const; - /** - * @brief Get stride of the given axis. - */ + + /// @brief Get stride of the given axis. uint32_t stride(uint32_t axis) const; protected: @@ -49,4 +46,4 @@ class Array4DIndex } // namespace luci -#endif // __LUCI_ARRAY_INDEX_H__ +#endif // __LUCI_PASS_HELPERS_ARRAY_INDEX_H__ From c26d344087c205a27cd388d2684ec6ddb8524035 Mon Sep 17 00:00:00 2001 From: Dayoung Lee <dayoung.lee@samsung.com> Date: Mon, 20 Jan 2025 18:06:47 +0900 Subject: [PATCH 3/8] Fix ArrayIndex test --- compiler/luci/pass/src/helpers/ArrayIndex.cpp | 12 +++++++++--- compiler/luci/pass/src/helpers/ArrayIndex.test.cpp | 4 +--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/compiler/luci/pass/src/helpers/ArrayIndex.cpp b/compiler/luci/pass/src/helpers/ArrayIndex.cpp index dcdc1fda091..030d29f3be5 100644 --- a/compiler/luci/pass/src/helpers/ArrayIndex.cpp +++ b/compiler/luci/pass/src/helpers/ArrayIndex.cpp @@ -17,15 +17,19 @@ #include "ArrayIndex.h" #include <cassert> +#include <stdexcept> namespace luci { +#define UNLESS_INVALID_ARGUMENT(COND) \ + if (not(COND)) \ + throw std::invalid_argument(""); Array4DIndex::Array4DIndex(uint32_t D0, uint32_t D1, uint32_t D2, uint32_t D3) : _dim{D0, D1, D2, D3} { - assert(D0 > 0 && D1 > 0 && D2 > 0 && D3 > 0); - + UNLESS_INVALID_ARGUMENT(D0 > 0 && D1 > 0 && D2 > 0 && D3 > 0); + _strides[3] = 1; _strides[2] = D3; _strides[1] = D3 * D2; @@ -39,7 +43,9 @@ Array4DIndex::Array4DIndex(uint32_t D0, uint32_t D1, uint32_t D2, uint32_t D3) uint32_t Array4DIndex::operator()(uint32_t i0, uint32_t i1, uint32_t i2, uint32_t i3) const { - assert(i0 < _dim[0] && i1 < _dim[1] && i2 < _dim[2] && i3 < _dim[3]); + UNLESS_INVALID_ARGUMENT(i0 < _dim[0] && i1 < _dim[1] && i2 < _dim[2] && i3 < _dim[3]); + UNLESS_INVALID_ARGUMENT(0 <= i0 && 0 <= i1 && 0 <= i2 && 0 <= i3); + return i0 * _strides[0] + i1 * _strides[1] + i2 * _strides[2] + i3 * _strides[3]; } diff --git a/compiler/luci/pass/src/helpers/ArrayIndex.test.cpp b/compiler/luci/pass/src/helpers/ArrayIndex.test.cpp index 3e0bb39fdd0..4e2322a0582 100644 --- a/compiler/luci/pass/src/helpers/ArrayIndex.test.cpp +++ b/compiler/luci/pass/src/helpers/ArrayIndex.test.cpp @@ -22,8 +22,6 @@ TEST(LuciPassHelpersArrayIndex, array_index_4d) { luci::Array4DIndex idx(5, 4, 3, 2); - EXPECT_EQ(idx.rank(), 4); - EXPECT_EQ(idx(0, 0, 0, 0), 0); EXPECT_EQ(idx(1, 0, 0, 0), idx.stride(0)); @@ -36,5 +34,5 @@ TEST(LuciPassHelpersArrayIndex, array_index_4d) TEST(LuciPassHelpersArrayIndex, array_index_4d_NEG) { - ASSERT_THROW(luci::Array4DIndex idx(5, 4, 3, -1)); + EXPECT_ANY_THROW(luci::Array4DIndex idx(-1000, 4, 3, -1)); } From 36eda15f126581852ca5e0646863a19a6a439653 Mon Sep 17 00:00:00 2001 From: Dayoung Lee <dayoung.lee@samsung.com> Date: Mon, 20 Jan 2025 18:57:27 +0900 Subject: [PATCH 4/8] fix --- compiler/luci/pass/src/helpers/ArrayIndex.cpp | 3 --- compiler/luci/pass/src/helpers/ArrayIndex.test.cpp | 4 +++- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/compiler/luci/pass/src/helpers/ArrayIndex.cpp b/compiler/luci/pass/src/helpers/ArrayIndex.cpp index 030d29f3be5..97821af3190 100644 --- a/compiler/luci/pass/src/helpers/ArrayIndex.cpp +++ b/compiler/luci/pass/src/helpers/ArrayIndex.cpp @@ -28,8 +28,6 @@ namespace luci Array4DIndex::Array4DIndex(uint32_t D0, uint32_t D1, uint32_t D2, uint32_t D3) : _dim{D0, D1, D2, D3} { - UNLESS_INVALID_ARGUMENT(D0 > 0 && D1 > 0 && D2 > 0 && D3 > 0); - _strides[3] = 1; _strides[2] = D3; _strides[1] = D3 * D2; @@ -44,7 +42,6 @@ Array4DIndex::Array4DIndex(uint32_t D0, uint32_t D1, uint32_t D2, uint32_t D3) uint32_t Array4DIndex::operator()(uint32_t i0, uint32_t i1, uint32_t i2, uint32_t i3) const { UNLESS_INVALID_ARGUMENT(i0 < _dim[0] && i1 < _dim[1] && i2 < _dim[2] && i3 < _dim[3]); - UNLESS_INVALID_ARGUMENT(0 <= i0 && 0 <= i1 && 0 <= i2 && 0 <= i3); return i0 * _strides[0] + i1 * _strides[1] + i2 * _strides[2] + i3 * _strides[3]; } diff --git a/compiler/luci/pass/src/helpers/ArrayIndex.test.cpp b/compiler/luci/pass/src/helpers/ArrayIndex.test.cpp index 4e2322a0582..84d2a962db7 100644 --- a/compiler/luci/pass/src/helpers/ArrayIndex.test.cpp +++ b/compiler/luci/pass/src/helpers/ArrayIndex.test.cpp @@ -34,5 +34,7 @@ TEST(LuciPassHelpersArrayIndex, array_index_4d) TEST(LuciPassHelpersArrayIndex, array_index_4d_NEG) { - EXPECT_ANY_THROW(luci::Array4DIndex idx(-1000, 4, 3, -1)); + luci::Array4DIndex idx(4, 4, 3, 2); + + EXPECT_ANY_THROW(idx(5, 0, 0, 0)); } From 4ec592eb4cb5bf148f3a7ec526cf9d5110231982 Mon Sep 17 00:00:00 2001 From: Dayoung Lee <dayoung.lee@samsung.com> Date: Mon, 20 Jan 2025 19:39:22 +0900 Subject: [PATCH 5/8] Fix index size --- compiler/luci/pass/src/helpers/ArrayIndex.cpp | 2 +- compiler/luci/pass/src/helpers/ArrayIndex.test.cpp | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/compiler/luci/pass/src/helpers/ArrayIndex.cpp b/compiler/luci/pass/src/helpers/ArrayIndex.cpp index 97821af3190..5d1cb35c0af 100644 --- a/compiler/luci/pass/src/helpers/ArrayIndex.cpp +++ b/compiler/luci/pass/src/helpers/ArrayIndex.cpp @@ -46,7 +46,7 @@ uint32_t Array4DIndex::operator()(uint32_t i0, uint32_t i1, uint32_t i2, uint32_ return i0 * _strides[0] + i1 * _strides[1] + i2 * _strides[2] + i3 * _strides[3]; } -uint32_t Array4DIndex::size(void) const { return _strides[3]; } +uint32_t Array4DIndex::size(void) const { return _dim[0] * _dim[1] * _dim[2] * _dim[3]; } uint32_t Array4DIndex::stride(uint32_t axis) const { return _strides[axis]; } diff --git a/compiler/luci/pass/src/helpers/ArrayIndex.test.cpp b/compiler/luci/pass/src/helpers/ArrayIndex.test.cpp index 84d2a962db7..b7a83c176a0 100644 --- a/compiler/luci/pass/src/helpers/ArrayIndex.test.cpp +++ b/compiler/luci/pass/src/helpers/ArrayIndex.test.cpp @@ -24,11 +24,15 @@ TEST(LuciPassHelpersArrayIndex, array_index_4d) EXPECT_EQ(idx(0, 0, 0, 0), 0); + // stride EXPECT_EQ(idx(1, 0, 0, 0), idx.stride(0)); EXPECT_EQ(idx(0, 1, 0, 0), idx.stride(1)); EXPECT_EQ(idx(0, 0, 1, 0), idx.stride(2)); EXPECT_EQ(idx(0, 0, 0, 1), idx.stride(3)); + // size + EXPECT_EQ(idx.size(), 5 * 4 * 3 * 2); + EXPECT_EQ(idx(4, 3, 2, 1), 4 * 4 * 3 * 2 + 3 * 3 * 2 + 2 * 2 + 1); } From ba9b962c2b8f1aba8df131b1186abbac11e25128 Mon Sep 17 00:00:00 2001 From: Dayoung Lee <dayoung.lee@samsung.com> Date: Mon, 20 Jan 2025 21:25:56 +0900 Subject: [PATCH 6/8] Formatting --- compiler/luci/pass/src/helpers/ArrayIndex.cpp | 1 + compiler/luci/pass/src/helpers/ArrayIndex.h | 4 +--- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/compiler/luci/pass/src/helpers/ArrayIndex.cpp b/compiler/luci/pass/src/helpers/ArrayIndex.cpp index 5d1cb35c0af..6f57e91ec07 100644 --- a/compiler/luci/pass/src/helpers/ArrayIndex.cpp +++ b/compiler/luci/pass/src/helpers/ArrayIndex.cpp @@ -21,6 +21,7 @@ namespace luci { + #define UNLESS_INVALID_ARGUMENT(COND) \ if (not(COND)) \ throw std::invalid_argument(""); diff --git a/compiler/luci/pass/src/helpers/ArrayIndex.h b/compiler/luci/pass/src/helpers/ArrayIndex.h index ff3f18e51cf..878effb19c2 100644 --- a/compiler/luci/pass/src/helpers/ArrayIndex.h +++ b/compiler/luci/pass/src/helpers/ArrayIndex.h @@ -22,9 +22,7 @@ namespace luci { -/** - * @brief Index class for 4D tensor to calculate linear index from multi-dimensional indices. - */ +/// @brief Index class for 4D tensor to calculate linear index from multi-dimensional indices. class Array4DIndex { public: From 17c9dcfb372338dc90c2747399c6ea22a4c875c8 Mon Sep 17 00:00:00 2001 From: Dayoung Lee <dayoung.lee@samsung.com> Date: Tue, 21 Jan 2025 15:57:12 +0900 Subject: [PATCH 7/8] Apply suggestions from code review Co-authored-by: Hyukjin Jeong <hj1.jeong@samsung.com> Co-authored-by: SaeHie Park <saehie.park@gmail.com> --- compiler/luci/pass/src/helpers/ArrayIndex.h | 4 ++-- compiler/luci/pass/src/helpers/ArrayIndex.test.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/luci/pass/src/helpers/ArrayIndex.h b/compiler/luci/pass/src/helpers/ArrayIndex.h index 878effb19c2..df81cf29e49 100644 --- a/compiler/luci/pass/src/helpers/ArrayIndex.h +++ b/compiler/luci/pass/src/helpers/ArrayIndex.h @@ -23,7 +23,7 @@ namespace luci { /// @brief Index class for 4D tensor to calculate linear index from multi-dimensional indices. -class Array4DIndex +class Array4DIndex final { public: Array4DIndex(uint32_t d0, uint32_t d1, uint32_t d2, uint32_t d3); @@ -37,7 +37,7 @@ class Array4DIndex /// @brief Get stride of the given axis. uint32_t stride(uint32_t axis) const; -protected: +private: uint32_t _dim[4]; uint32_t _strides[4]; }; diff --git a/compiler/luci/pass/src/helpers/ArrayIndex.test.cpp b/compiler/luci/pass/src/helpers/ArrayIndex.test.cpp index b7a83c176a0..69f3cf0a4fd 100644 --- a/compiler/luci/pass/src/helpers/ArrayIndex.test.cpp +++ b/compiler/luci/pass/src/helpers/ArrayIndex.test.cpp @@ -36,7 +36,7 @@ TEST(LuciPassHelpersArrayIndex, array_index_4d) EXPECT_EQ(idx(4, 3, 2, 1), 4 * 4 * 3 * 2 + 3 * 3 * 2 + 2 * 2 + 1); } -TEST(LuciPassHelpersArrayIndex, array_index_4d_NEG) +TEST(LuciPassHelpersArrayIndex, array_invalid_index_4d_NEG) { luci::Array4DIndex idx(4, 4, 3, 2); From eb0d26b5161e35bb8bcb2696b13c1cb49ba6d2a1 Mon Sep 17 00:00:00 2001 From: Dayoung Lee <dayoung.lee@samsung.com> Date: Tue, 21 Jan 2025 16:01:06 +0900 Subject: [PATCH 8/8] Apply feedback --- compiler/luci/pass/src/helpers/ArrayIndex.cpp | 19 ++++++++++++++----- .../luci/pass/src/helpers/ArrayIndex.test.cpp | 5 +++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/compiler/luci/pass/src/helpers/ArrayIndex.cpp b/compiler/luci/pass/src/helpers/ArrayIndex.cpp index 6f57e91ec07..f87162d60f8 100644 --- a/compiler/luci/pass/src/helpers/ArrayIndex.cpp +++ b/compiler/luci/pass/src/helpers/ArrayIndex.cpp @@ -22,8 +22,8 @@ namespace luci { -#define UNLESS_INVALID_ARGUMENT(COND) \ - if (not(COND)) \ +#define THROW_UNLESS(COND) \ + if (not(COND)) \ throw std::invalid_argument(""); Array4DIndex::Array4DIndex(uint32_t D0, uint32_t D1, uint32_t D2, uint32_t D3) @@ -36,18 +36,27 @@ Array4DIndex::Array4DIndex(uint32_t D0, uint32_t D1, uint32_t D2, uint32_t D3) for (int i = 0; i < 4; ++i) { - assert(_strides[i] > 0); + THROW_UNLESS(_strides[i] > 0); } } uint32_t Array4DIndex::operator()(uint32_t i0, uint32_t i1, uint32_t i2, uint32_t i3) const { - UNLESS_INVALID_ARGUMENT(i0 < _dim[0] && i1 < _dim[1] && i2 < _dim[2] && i3 < _dim[3]); + THROW_UNLESS(i0 < _dim[0] && i1 < _dim[1] && i2 < _dim[2] && i3 < _dim[3]); return i0 * _strides[0] + i1 * _strides[1] + i2 * _strides[2] + i3 * _strides[3]; } -uint32_t Array4DIndex::size(void) const { return _dim[0] * _dim[1] * _dim[2] * _dim[3]; } +uint32_t Array4DIndex::size(void) const +{ + + for (int i = 0; i < 4; ++i) + { + THROW_UNLESS(_dim[i] > 0); + } + + return _dim[0] * _dim[1] * _dim[2] * _dim[3]; +} uint32_t Array4DIndex::stride(uint32_t axis) const { return _strides[axis]; } diff --git a/compiler/luci/pass/src/helpers/ArrayIndex.test.cpp b/compiler/luci/pass/src/helpers/ArrayIndex.test.cpp index 69f3cf0a4fd..8bad4f3c9e0 100644 --- a/compiler/luci/pass/src/helpers/ArrayIndex.test.cpp +++ b/compiler/luci/pass/src/helpers/ArrayIndex.test.cpp @@ -42,3 +42,8 @@ TEST(LuciPassHelpersArrayIndex, array_invalid_index_4d_NEG) EXPECT_ANY_THROW(idx(5, 0, 0, 0)); } + +TEST(LuciPassHelpersArrayIndex, array_invalid_dim_4d_NEG) +{ + EXPECT_ANY_THROW(luci::Array4DIndex idx(4, 0, 3, 2)); +}