From f696235356dffa486a84af1bbe7878fb4d1471d4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/On-Device=20Lab=28SR=29/Staff?= =?utf8?q?=20Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Thu, 4 Jul 2019 13:43:33 +0900 Subject: [PATCH] [angkor] Add TensorIndex & TensorShape alias (#4085) This commit introduces an alias of tensor::Index and tensor::Shape under nncc::core::ADT namespace (to shorten client-side code). Signed-off-by: Jonghyun Park --- contrib/angkor/include/angkor/TensorIndex.h | 29 +++++++++ contrib/angkor/include/angkor/TensorShape.h | 29 +++++++++ contrib/angkor/src/TensorIndex.test.cpp | 87 +++++++++++++++++++++++++ contrib/angkor/src/TensorShape.test.cpp | 99 +++++++++++++++++++++++++++++ 4 files changed, 244 insertions(+) create mode 100644 contrib/angkor/include/angkor/TensorIndex.h create mode 100644 contrib/angkor/include/angkor/TensorShape.h create mode 100644 contrib/angkor/src/TensorIndex.test.cpp create mode 100644 contrib/angkor/src/TensorShape.test.cpp diff --git a/contrib/angkor/include/angkor/TensorIndex.h b/contrib/angkor/include/angkor/TensorIndex.h new file mode 100644 index 0000000..2fc1050 --- /dev/null +++ b/contrib/angkor/include/angkor/TensorIndex.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2019 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 __ANGKOR_TENSOR_INDEX_H__ +#define __ANGKOR_TENSOR_INDEX_H__ + +#include "nncc/core/ADT/tensor/Index.h" + +namespace angkor +{ + +using TensorIndex = ::nncc::core::ADT::tensor::Index; + +} // namespace angkor + +#endif // __ANGKOR_TENSOR_INDEX_H__ diff --git a/contrib/angkor/include/angkor/TensorShape.h b/contrib/angkor/include/angkor/TensorShape.h new file mode 100644 index 0000000..ab62bd8 --- /dev/null +++ b/contrib/angkor/include/angkor/TensorShape.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2019 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 __ANGKOR_TENSOR_SHAPE_H__ +#define __ANGKOR_TENSOR_SHAPE_H__ + +#include "nncc/core/ADT/tensor/Shape.h" + +namespace angkor +{ + +using TensorShape = ::nncc::core::ADT::tensor::Shape; + +} // namespace angkor + +#endif // __ANGKOR_TENSOR_SHAPE_H__ diff --git a/contrib/angkor/src/TensorIndex.test.cpp b/contrib/angkor/src/TensorIndex.test.cpp new file mode 100644 index 0000000..68cf391 --- /dev/null +++ b/contrib/angkor/src/TensorIndex.test.cpp @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2019 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 "angkor/TensorIndex.h" + +#include + +TEST(TensorIndexTest, ctor) +{ + angkor::TensorIndex index; + + ASSERT_EQ(index.rank(), 0); +} + +TEST(TensorIndexTest, ctor_initializer_list) +{ + const angkor::TensorIndex index{1, 3, 5, 7}; + + ASSERT_EQ(index.rank(), 4); + + ASSERT_EQ(index.at(0), 1); + ASSERT_EQ(index.at(1), 3); + ASSERT_EQ(index.at(2), 5); + ASSERT_EQ(index.at(3), 7); +} + +TEST(TensorIndexTest, resize) +{ + angkor::TensorIndex index; + + index.resize(4); + + ASSERT_EQ(index.rank(), 4); +} + +TEST(TensorIndexTest, at) +{ + angkor::TensorIndex index; + + index.resize(4); + + uint32_t indices[4] = {3, 5, 2, 7}; + + for (uint32_t axis = 0; axis < 4; ++axis) + { + index.at(axis) = indices[axis]; + ASSERT_EQ(index.at(axis), indices[axis]); + } +} + +TEST(TensorIndexTest, copy) +{ + const angkor::TensorIndex original{3, 5, 2, 7}; + const angkor::TensorIndex copied{original}; + + ASSERT_EQ(original.rank(), copied.rank()); + + for (uint32_t axis = 0; axis < 4; ++axis) + { + ASSERT_EQ(original.at(axis), copied.at(axis)); + } +} + +TEST(TensorIndexTest, fill) +{ + angkor::TensorIndex index{1, 6}; + + index.fill(3); + + ASSERT_EQ(index.rank(), 2); + + ASSERT_EQ(index.at(0), 3); + ASSERT_EQ(index.at(1), 3); +} diff --git a/contrib/angkor/src/TensorShape.test.cpp b/contrib/angkor/src/TensorShape.test.cpp new file mode 100644 index 0000000..5e6766a --- /dev/null +++ b/contrib/angkor/src/TensorShape.test.cpp @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2019 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 "angkor/TensorShape.h" + +#include + +TEST(TensorShapeTest, ctor) +{ + angkor::TensorShape shape; + + ASSERT_EQ(shape.rank(), 0); +} + +TEST(TensorShapeTest, ctor_initializer_list) +{ + angkor::TensorShape shape{1, 3, 5, 7}; + + ASSERT_EQ(shape.rank(), 4); + + ASSERT_EQ(shape.dim(0), 1); + ASSERT_EQ(shape.dim(1), 3); + ASSERT_EQ(shape.dim(2), 5); + ASSERT_EQ(shape.dim(3), 7); +} + +TEST(TensorShapeTest, resize) +{ + angkor::TensorShape shape; + + shape.resize(4); + + ASSERT_EQ(shape.rank(), 4); +} + +TEST(TensorShapeTest, dim) +{ + angkor::TensorShape shape; + + shape.resize(4); + + uint32_t dims[4] = {3, 5, 2, 7}; + + for (uint32_t axis = 0; axis < 4; ++axis) + { + shape.dim(axis) = dims[axis]; + ASSERT_EQ(shape.dim(axis), dims[axis]); + } +} + +TEST(TensorShapeTest, copy) +{ + const angkor::TensorShape original{3, 5, 2, 7}; + const angkor::TensorShape copied{original}; + + ASSERT_EQ(original.rank(), copied.rank()); + + for (uint32_t axis = 0; axis < 4; ++axis) + { + ASSERT_EQ(original.dim(axis), copied.dim(axis)); + } +} + +TEST(TensorShapeTest, eq_negative_on_unmatched_rank) +{ + const angkor::TensorShape left{1, 1, 1}; + const angkor::TensorShape right{1, 1, 1, 1}; + + ASSERT_FALSE(left == right); +} + +TEST(TensorShapeTest, eq_negative_on_unmatched_dim) +{ + const angkor::TensorShape left{2, 3}; + const angkor::TensorShape right{2, 4}; + + ASSERT_FALSE(left == right); +} + +TEST(TensorShapeTest, eq_positive) +{ + const angkor::TensorShape left{2, 3}; + const angkor::TensorShape right{2, 3}; + + ASSERT_TRUE(left == right); +} -- 2.7.4