diff options
Diffstat (limited to 'libs/misc/include/misc/tensor')
-rw-r--r-- | libs/misc/include/misc/tensor/Comparator.h | 95 | ||||
-rw-r--r-- | libs/misc/include/misc/tensor/Diff.h | 70 | ||||
-rw-r--r-- | libs/misc/include/misc/tensor/Index.h | 105 | ||||
-rw-r--r-- | libs/misc/include/misc/tensor/IndexEnumerator.h | 131 | ||||
-rw-r--r-- | libs/misc/include/misc/tensor/IndexFormatter.h | 75 | ||||
-rw-r--r-- | libs/misc/include/misc/tensor/IndexIterator.h | 107 | ||||
-rw-r--r-- | libs/misc/include/misc/tensor/NonIncreasingStride.h | 83 | ||||
-rw-r--r-- | libs/misc/include/misc/tensor/Object.h | 100 | ||||
-rw-r--r-- | libs/misc/include/misc/tensor/Reader.h | 58 | ||||
-rw-r--r-- | libs/misc/include/misc/tensor/Shape.h | 152 | ||||
-rw-r--r-- | libs/misc/include/misc/tensor/Zipper.h | 104 |
11 files changed, 0 insertions, 1080 deletions
diff --git a/libs/misc/include/misc/tensor/Comparator.h b/libs/misc/include/misc/tensor/Comparator.h deleted file mode 100644 index 80f53043c..000000000 --- a/libs/misc/include/misc/tensor/Comparator.h +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright (c) 2018 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. - */ - -/** - * @file Comparator.h - * @ingroup COM_AI_RUNTIME - * @brief This file contains nnfw::misc::tensor::Comparator class - */ - -#ifndef __NNFW_MISC_TENSOR_COMPARATOR_H__ -#define __NNFW_MISC_TENSOR_COMPARATOR_H__ - -#include "misc/tensor/Index.h" -#include "misc/tensor/Shape.h" -#include "misc/tensor/Reader.h" -#include "misc/tensor/Diff.h" - -#include <functional> - -#include <vector> - -namespace nnfw -{ -namespace misc -{ -namespace tensor -{ - -/** - * @brief Class to compare two tensors (expected and obtained to compare) - */ -class Comparator -{ -public: - /** - * @brief Construct a new @c Comparator object - * @param[in] fn Function that compares two float values - */ - Comparator(const std::function<bool(float lhs, float rhs)> &fn) : _compare_fn{fn} - { - // DO NOTHING - } - -public: - /** - * @brief Struct to observe comparison results - */ - struct Observer - { - /** - * @brief Get notification of comparison result at every index of two tensors - * @param[in] index Index of tensors compared - * @param[in] expected Expected value of element at @c index - * @param[in] obtained Obtained value of element at @c index - * @return N/A - */ - virtual void notify(const Index &index, float expected, float obtained) = 0; - }; - -public: - /** - * @brief Compare two tensors - * @param[in] shape Shape of two tensors - * @param[in] expected @c Reader<float> object that accesses expected tensor - * @param[in] obtained @c Reader<float> object that accesses obtained tensor - * @param[in] observer @c Observer notified of expected value and obtained value at every index - * @return @c std::vector<Diff<float>> containing information of failed comparison - */ - // NOTE Observer should live longer than comparator - std::vector<Diff<float>> compare(const Shape &shape, const Reader<float> &expected, - const Reader<float> &obtained, - Observer *observer = nullptr) const; - -private: - std::function<bool(float lhs, float rhs)> _compare_fn; -}; - -} // namespace tensor -} // namespace misc -} // namespace nnfw - -#endif // __NNFW_MISC_TENSOR_COMPARATOR_H__ diff --git a/libs/misc/include/misc/tensor/Diff.h b/libs/misc/include/misc/tensor/Diff.h deleted file mode 100644 index c41a97987..000000000 --- a/libs/misc/include/misc/tensor/Diff.h +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright (c) 2018 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. - */ - -/** - * @file Diff.h - * @ingroup COM_AI_RUNTIME - * @brief This file contains nnfw::misc::tensor::Diff struct - */ - -#ifndef __NNFW_MISC_TENSOR_DIFF_H__ -#define __NNFW_MISC_TENSOR_DIFF_H__ - -#include "misc/tensor/Index.h" - -namespace nnfw -{ -namespace misc -{ -namespace tensor -{ - -/** - * @brief Struct to have information after comparing two elements of two tensors - */ -template <typename T> struct Diff -{ - Index index; /**< Index of elements in two tensors, which turn out to be different */ - - T expected; /**< Expected value of element of first tensor */ - T obtained; /**< Obtained value of element of second tensor */ - - /** - * @brief Construct a new @c Diff object - * @param[in] i Initial value of index - */ - Diff(const Index &i) : index(i) - { - // DO NOTHING - } - - /** - * @brief Construct a new @c Diff object - * @param[in] i Index value - * @param[in] e Expected value of element of first tensor - * @param[in] o Obtained value of element of second tensor - */ - Diff(const Index &i, const T &e, const T &o) : index(i), expected{e}, obtained{o} - { - // DO NOTHING - } -}; - -} // namespace tensor -} // namespace misc -} // namespace nnfw - -#endif // __NNFW_MISC_TENSOR_DIFF_H__ diff --git a/libs/misc/include/misc/tensor/Index.h b/libs/misc/include/misc/tensor/Index.h deleted file mode 100644 index a08d7099e..000000000 --- a/libs/misc/include/misc/tensor/Index.h +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright (c) 2018 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. - */ - -/** - * @file Index.h - * @ingroup COM_AI_RUNTIME - * @brief This file contains nnfw::misc::tensor::Index struct - */ -#ifndef __NNFW_MISC_TENSOR_INDEX_H__ -#define __NNFW_MISC_TENSOR_INDEX_H__ - -#include <cstdint> -#include <cstddef> - -#include <vector> -#include <initializer_list> - -namespace nnfw -{ -namespace misc -{ -namespace tensor -{ - -/** - * @brief Struct to represent index of each dimension of a tensor - */ -struct Index -{ -public: - /** - * @brief Construct a new @c Index object - * @param[in] rank Rank of a tensor - */ - Index(size_t rank) { _offsets.resize(rank); } - -public: - /** - * @brief Construct a new @c Index object - * @param[in] offsets Rank of a tensor of @c std::initializer_list<int32_t> type - */ - Index(std::initializer_list<int32_t> offsets) : _offsets{offsets} - { - // DO NOTHING - } - -public: - /** - * @brief Get the rank - * @return Rank that this @c Index object can handle - */ - size_t rank(void) const { return _offsets.size(); } - -public: - /** - * @brief Get the index n'th dimension - * @param[in] n Dimension - * @return index of n'th dimension - */ - int32_t at(size_t n) const { return _offsets.at(n); } - - /** - * @brief Get the reference of the index n'th dimension - * @param[in] n Dimension - * @return reference of index of n'th dimension - */ - int32_t &at(size_t n) { return _offsets.at(n); } - -private: - std::vector<int32_t> _offsets; -}; - -/** - * @brief Copy an @c Index with reversed order - * @param[in] origin @c Index object to copy - * @return an @c Index object with reversed order - * @note This is used to convert NNAPI tensor index to ARM tensor index or vice versa - */ -inline static Index copy_reverse(const Index &origin) -{ - size_t rank = origin.rank(); - Index target(rank); - for (int i = 0; i < rank; i++) - target.at(i) = origin.at(rank - 1 - i); - return target; -} - -} // namespace tensor -} // namespace misc -} // namespace nnfw - -#endif // __NNFW_MISC_TENSOR_INDEX_H__ diff --git a/libs/misc/include/misc/tensor/IndexEnumerator.h b/libs/misc/include/misc/tensor/IndexEnumerator.h deleted file mode 100644 index 4912ea289..000000000 --- a/libs/misc/include/misc/tensor/IndexEnumerator.h +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Copyright (c) 2018 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. - */ - -/** - * @file IndexEnumerator.h - * @ingroup COM_AI_RUNTIME - * @brief This file contains nnfw::misc::tensor::IndexEnumerator class - */ - -#ifndef __NNFW_MISC_TENSOR_INDEX_ENUMERATOR_H__ -#define __NNFW_MISC_TENSOR_INDEX_ENUMERATOR_H__ - -#include "misc/tensor/Shape.h" -#include "misc/tensor/Index.h" - -namespace nnfw -{ -namespace misc -{ -namespace tensor -{ -/** - * @brief Class to enumerate index of a tensor - * - */ -class IndexEnumerator -{ -public: - /** - * @brief Construct a new @c IndexEnumerator object - * @param[in] shape Shape of tensor of which index will be enumerate - */ - explicit IndexEnumerator(const Shape &shape) : _shape(shape), _index(shape.rank()), _cursor(0) - { - const size_t rank = _shape.rank(); - - for (size_t axis = 0; axis < rank; ++axis) - { - _index.at(axis) = 0; - } - - for (_cursor = 0; _cursor < rank; ++_cursor) - { - if (_index.at(_cursor) < _shape.dim(_cursor)) - { - break; - } - } - } - -public: - /** - * @brief Prevent constructing @c IndexEnumerator object by using R-value reference - */ - IndexEnumerator(IndexEnumerator &&) = delete; - /** - * @brief Prevent copy constructor - */ - IndexEnumerator(const IndexEnumerator &) = delete; - -public: - /** - * @brief Check if more enumeration is available - * @return @c true if more @c advance() is available, otherwise @c false - */ - bool valid(void) const { return _cursor < _shape.rank(); } - -public: - /** - * @brief Get the current index to enumerate - * @return Current index - */ - const Index &curr(void) const { return _index; } - -public: - /** - * @brief Advance index by +1 - */ - void advance(void) - { - const size_t rank = _shape.rank(); - - // Find axis to be updated - while ((_cursor < rank) && !(_index.at(_cursor) + 1 < _shape.dim(_cursor))) - { - ++_cursor; - } - - if (_cursor == rank) - { - return; - } - - // Update index - _index.at(_cursor) += 1; - - for (size_t axis = 0; axis < _cursor; ++axis) - { - _index.at(axis) = 0; - } - - // Update cursor - _cursor = 0; - } - -public: - const Shape _shape; //!< Shape to enumerate - -private: - size_t _cursor; - Index _index; -}; - -} // namespace tensor -} // namespace misc -} // namespace nnfw - -#endif // __NNFW_MISC_TENSOR_INDEX_ENUMERATOR_H__ diff --git a/libs/misc/include/misc/tensor/IndexFormatter.h b/libs/misc/include/misc/tensor/IndexFormatter.h deleted file mode 100644 index 7ae34eec1..000000000 --- a/libs/misc/include/misc/tensor/IndexFormatter.h +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright (c) 2018 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. - */ - -/** - * @file IndexFormatter.h - * @ingroup COM_AI_RUNTIME - * @brief This file contains nnfw::misc::tensor::IndexFormatter class - */ - -#ifndef __NNFW_MISC_TENSOR_INDEX_FORMATTER_H__ -#define __NNFW_MISC_TENSOR_INDEX_FORMATTER_H__ - -#include "misc/tensor/Index.h" - -#include <ostream> - -namespace nnfw -{ -namespace misc -{ -namespace tensor -{ - -/** - * @brief Class to send @c Index object to output stream - */ -class IndexFormatter -{ -public: - /** - * @brief Construct a new @c IndexFormatter object - * @param[in] index index to be sent to output stream - */ - IndexFormatter(const nnfw::misc::tensor::Index &index) : _index(index) - { - // DO NOTHING - } - -public: - /** - * @brief Get an @c Index object - * @return @c Index object previously passed to the constructor - */ - const nnfw::misc::tensor::Index &index(void) const { return _index; } - -private: - const nnfw::misc::tensor::Index &_index; -}; - -/** - * @brief Send @c IndexFormatter object to output stream - * @param[in] os Output stream - * @param[in] fmt @c IndexFormatter object that is sent to output stream - * @return Output stream - */ -std::ostream &operator<<(std::ostream &os, const IndexFormatter &fmt); - -} // namespace tensor -} // namespace misc -} // namespace nnfw - -#endif // __NNFW_MISC_TENSOR_INDEX_FORMATTER_H__ diff --git a/libs/misc/include/misc/tensor/IndexIterator.h b/libs/misc/include/misc/tensor/IndexIterator.h deleted file mode 100644 index f6428e19e..000000000 --- a/libs/misc/include/misc/tensor/IndexIterator.h +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Copyright (c) 2018 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. - */ - -/** - * @file IndexIterator.h - * @ingroup COM_AI_RUNTIME - * @brief This file contains nnfw::misc::tensor::IndexIterator class and - * helper function and operator - */ -#ifndef __NNFW_MISC_TENSOR_INDEX_ITERATOR_H__ -#define __NNFW_MISC_TENSOR_INDEX_ITERATOR_H__ - -#include "misc/tensor/Shape.h" -#include "misc/tensor/Index.h" -#include "misc/tensor/IndexEnumerator.h" - -namespace nnfw -{ -namespace misc -{ -namespace tensor -{ - -/** - * @brief Class to iterate indexes available for given shape - */ -class IndexIterator -{ -public: - /** - * @brief Construct a new @c IndexIterator object - * @param[in] shape Shape of tensor of which index will be iterated - */ - IndexIterator(const Shape &shape) : _shape(shape) - { - // DO NOTHING - } - -public: - /** - * @brief Construct a new IndexIterator object using reference - * @param[in] IndexIterator @c IndexIterator object to move - */ - IndexIterator(IndexIterator &&) = default; - - /** - * @brief Prevent copy constructor - */ - IndexIterator(const IndexIterator &) = delete; - -public: - /** - * @brief Iterate all available indexes and run a function for each index - * @param[in] fn Function that requires an index as a parameter. - * @return @c IndexIterator object - */ - template <typename Callable> IndexIterator &iter(Callable fn) - { - for (IndexEnumerator e{_shape}; e.valid(); e.advance()) - { - fn(e.curr()); - } - - return (*this); - } - -private: - const Shape &_shape; -}; - -/** - * @brief Get an @c IndexItator object - * @param[in] shape Shape of tensor of which index will be iterated - * @return @c IndexIterator object - */ -inline IndexIterator iterate(const Shape &shape) { return IndexIterator{shape}; } - -/** - * @brief Iterate all indexes and apply a function - * @param[in] it @c IndexIterator object that is constructed with a tensor shape - * @param[in] cb A function that will receive a specific index. - * Inside the function, the index is used to manipulate tensor element. - * @return @c IndexIterator object - */ -template <typename Callable> IndexIterator &operator<<(IndexIterator &&it, Callable cb) -{ - return it.iter(cb); -} - -} // namespace tensor -} // namespace misc -} // namespace nnfw - -#endif // __NNFW_MISC_TENSOR_INDEX_ITERATOR_H__ diff --git a/libs/misc/include/misc/tensor/NonIncreasingStride.h b/libs/misc/include/misc/tensor/NonIncreasingStride.h deleted file mode 100644 index e7ad0857b..000000000 --- a/libs/misc/include/misc/tensor/NonIncreasingStride.h +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright (c) 2018 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. - */ - -/** - * @file NonIncreasingStride.h - * @ingroup COM_AI_RUNTIME - * @brief This file contains nnfw::misc::tensor::NonIncreasingStride class - */ -#ifndef __NNFW_MISC_TENSOR_NON_INCREASING_STRIDE_H__ -#define __NNFW_MISC_TENSOR_NON_INCREASING_STRIDE_H__ - -#include "misc/tensor/Shape.h" -#include "misc/tensor/Index.h" - -#include <vector> - -namespace nnfw -{ -namespace misc -{ -namespace tensor -{ - -/** - * @brief Class to represent strides where stride[N-1] >= stride[N] holds for all N < rank - */ -class NonIncreasingStride -{ -public: - /** - * @brief Initialize the stride data using @c Shape - * @param[in] shape to build stride info - * @return N/A - */ - void init(const Shape &shape) - { - _stride.resize(shape.rank()); - _stride.at(shape.rank() - 1) = 1; - - for (uint32_t axis = shape.rank() - 1; axis > 0; --axis) - { - _stride.at(axis - 1) = _stride.at(axis) * shape.dim(axis); - } - } - -public: - /** - * @brief Get an stride value for specific axis - * @param[in] axis Axis of stride - * @return The value of stride - */ - uint32_t at(uint32_t axis) const { return _stride.at(axis); } - -public: - /** - * @brief Get the 1-D offset of specified index for n-D tensor - * @param index @c Index object - * @return 1-D offset of index - */ - uint32_t offset(const Index &index) const; - -private: - std::vector<uint32_t> _stride; -}; - -} // namespace tensor -} // namespace misc -} // namespace nnfw - -#endif // __NNFW_MISC_TENSOR_NON_INCREASING_STRIDE_H__ diff --git a/libs/misc/include/misc/tensor/Object.h b/libs/misc/include/misc/tensor/Object.h deleted file mode 100644 index 83fbc0bd1..000000000 --- a/libs/misc/include/misc/tensor/Object.h +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Copyright (c) 2018 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. - */ - -/** - * @file Object.h - * @ingroup COM_AI_RUNTIME - * @brief This file contains nnfw::misc::tensor::Object class - */ - -#ifndef __NNFW_MISC_TENSOR_OBJECT_H__ -#define __NNFW_MISC_TENSOR_OBJECT_H__ - -#include "misc/tensor/Shape.h" -#include "misc/tensor/Index.h" -#include "misc/tensor/IndexIterator.h" -#include "misc/tensor/NonIncreasingStride.h" -#include "misc/tensor/Reader.h" - -#include <vector> - -namespace nnfw -{ -namespace misc -{ -namespace tensor -{ - -/** - * @brief Class to build a tensor using specific generator - * @tparam T Type of tensor element - */ - -template <typename T> class Object final : public Reader<T> -{ -public: - /** - * @brief Function to generate tensor element - */ - using Generator = std::function<T(const Shape &shape, const Index &index)>; - -public: - /** - * @brief Construct a new @c Object object - * @param[in] shape Tensor shape - * @param[in] fn Function to generate tensor elements - */ - Object(const Shape &shape, const Generator &fn) : _shape{shape} - { - // Set 'stride' - _stride.init(shape); - - // Pre-allocate buffer - _values.resize(_shape.dim(0) * _stride.at(0)); - - // Set 'value' - iterate(_shape) << - [this, &fn](const Index &index) { _values.at(_stride.offset(index)) = fn(_shape, index); }; - } - -public: - /** - * @brief Get reference of shape - * @return Reference of shape - */ - const Shape &shape(void) const { return _shape; } - -public: - /** - * @brief Get and element of tensor - * @param[in] index Index of a tensor element - * @return Value of tensor element - */ - T at(const Index &index) const override { return _values.at(_stride.offset(index)); } - -private: - Shape _shape; - NonIncreasingStride _stride; - -private: - std::vector<T> _values; -}; - -} // namespace tensor -} // namespace misc -} // namespace nnfw - -#endif // __NNFW_MISC_FEATURE_OBJECT_H__ diff --git a/libs/misc/include/misc/tensor/Reader.h b/libs/misc/include/misc/tensor/Reader.h deleted file mode 100644 index 9175a913e..000000000 --- a/libs/misc/include/misc/tensor/Reader.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (c) 2018 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. - */ - -/** - * @file Reader.h - * @ingroup COM_AI_RUNTIME - * @brief This file contains nnfw::misc::tensor::Reader struct - */ - -#ifndef __NNFW_MISC_TENSOR_READER_H__ -#define __NNFW_MISC_TENSOR_READER_H__ - -#include "misc/tensor/Index.h" - -namespace nnfw -{ -namespace misc -{ -namespace tensor -{ - -/** - * @brief Struct to read element of tensor - * @tparam T Type of elements in tensor - */ -template <typename T> struct Reader -{ - /** - * @brief Destroy the Reader object - */ - virtual ~Reader() = default; - - /** - * @brief Get an element of tensor - * @param[in] index Index specifying indexes of tensor element - * @return The value of specificed element - */ - virtual T at(const Index &index) const = 0; -}; - -} // namespace tensor -} // namespace misc -} // namespace nnfw - -#endif // __NNFW_MISC_TENSOR_READER_H__ diff --git a/libs/misc/include/misc/tensor/Shape.h b/libs/misc/include/misc/tensor/Shape.h deleted file mode 100644 index 6e6c23502..000000000 --- a/libs/misc/include/misc/tensor/Shape.h +++ /dev/null @@ -1,152 +0,0 @@ -/* - * Copyright (c) 2018 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. - */ - -/** - * @file Shape.h - * @ingroup COM_AI_RUNTIME - * @brief This file contains nnfw::misc::tensor::Shape class - */ - -#ifndef __NNFW_MISC_TENSOR_SHAPE_H__ -#define __NNFW_MISC_TENSOR_SHAPE_H__ - -#include <cstdint> -#include <cstddef> -#include <deque> -#include <initializer_list> -#include <ostream> -#include <string> - -namespace nnfw -{ -namespace misc -{ -namespace tensor -{ - -/** - * @brief Class to represent shape of a tensor - */ -class Shape -{ -public: - /** - * @brief Construct a new Shape object - * @param[in] rank Rank of a tensor - */ - Shape(size_t rank) { _dimensions.resize(rank); } - -public: - /** - * @brief Construct a new Shape object - * @param[in] dimensions @c initializer_list<int32_t> of dimensions of tensor - */ - Shape(const std::initializer_list<int32_t> &dimensions) : _dimensions{dimensions} - { - // DO NOTHING - } - - /** - * @brief Construct a new Shape object - * @param[in] origin @c Shape object to copy - */ - Shape(const Shape &origin) = default; - -public: - /** - * @brief Add dimension to the beginning - * @param[in] d dimension to add to the beginning - * @return N/A - */ - void prepend(int32_t d) { _dimensions.emplace_front(d); } - - /** - * @brief Add dimension to the back - * @param[in] d dimension to add to the back - * @return N/A - */ - void append(int32_t d) { _dimensions.emplace_back(d); } - -public: - /** - * @brief Get the rank of this shape - * @return rank - */ - size_t rank(void) const { return _dimensions.size(); } - -public: - /** - * @brief Get specific dimension - * @param[in] n Index of dimension - * @return n'th dimension - */ - int32_t dim(size_t n) const { return _dimensions.at(n); } - - /** - * @brief Get the reference of specific dimension - * @param[in] n Index of dimension - * @return Reference of n'th dimension - */ - int32_t &dim(size_t n) { return _dimensions.at(n); } - -public: - /** - * @brief Get the number of elements specified by this shape - * @return The number of elements - */ - size_t element_nums() const - { - size_t nums = 1; - for (auto d : _dimensions) - { - nums *= d; - } - return nums; - } - -private: - std::deque<int32_t> _dimensions; - -public: - /** - * @brief Get a @c Shape object after parsing string - * @param[in] s String of dimension list. Accepted format is numbers separated by comma. - * @return @c Shape object - */ - static Shape from(const std::string &s); -}; - -/** - * @brief Check equality of two @c Shape - * @param[in] Shape First shape to compare - * @param[in] Shape Second shape to compare - * @return @c true if both shapes are equal, otherwise @c false - */ -bool operator==(const Shape &, const Shape &); - -/** - * @brief Send @c Shape to @c std::ostream - * @param[in] os @c std::ostream to process this @c Shape - * @param[in] shape @c Shape to send to @c ostream - * @return Reference of @c std::ostream - */ -std::ostream &operator<<(std::ostream &os, const Shape &shape); - -} // namespace tensor -} // namespace misc -} // namespace nnfw - -#endif // __NNFW_MISC_TENSOR_SHAPE_H__ diff --git a/libs/misc/include/misc/tensor/Zipper.h b/libs/misc/include/misc/tensor/Zipper.h deleted file mode 100644 index 8f0ec4ab6..000000000 --- a/libs/misc/include/misc/tensor/Zipper.h +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright (c) 2018 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. - */ - -/** - * @file Zipper.h - * @ingroup COM_AI_RUNTIME - * @brief This file contains nnfw::misc::tensor::Zipper class - */ - -#ifndef __NNFW_MISC_TENSOR_ZIPPER_H__ -#define __NNFW_MISC_TENSOR_ZIPPER_H__ - -#include "misc/tensor/Index.h" -#include "misc/tensor/IndexIterator.h" -#include "misc/tensor/Reader.h" - -namespace nnfw -{ -namespace misc -{ -namespace tensor -{ - -/** - * @brief Class to apply a function with three params: @c Index, elements of a tensor - * at passed index read by @c Reader objects - */ -template <typename T> class Zipper -{ -public: - /** - * @brief Construct a new @c Zipper object - * @param[in] shape Shape of @c lhs and @c rhs - * @param[in] lhs @c Reader object of a tensor - * @param[in] rhs @c Reader object of a tensor - */ - Zipper(const Shape &shape, const Reader<T> &lhs, const Reader<T> &rhs) - : _shape{shape}, _lhs{lhs}, _rhs{rhs} - { - // DO NOTHING - } - -public: - /** - * @brief Apply @c cb to all elements of tensors. Elements of two tensors - * at passed @c index are read by @c lhs and @c rhs - * @param[in] cb Function to apply - * @return N/A - */ - template <typename Callable> void zip(Callable cb) const - { - iterate(_shape) << - [this, &cb](const Index &index) { cb(index, _lhs.at(index), _rhs.at(index)); }; - } - -private: - const Shape &_shape; - const Reader<T> &_lhs; - const Reader<T> &_rhs; -}; - -/** - * @brief Apply @c cb by using @c lhs and @c rhs passed to the constructor of @c zipper - * @param[in] zipper @c Zipper object - * @param[in] cb Function to zpply using @c zip function - * @return @c zipper object after applying @c cb to @c zipper - */ -template <typename T, typename Callable> -const Zipper<T> &operator<<(const Zipper<T> &zipper, Callable cb) -{ - zipper.zip(cb); - return zipper; -} - -/** - * @brief Get @c Zipper object constructed using passed params - * @param shape Shape of @c lhs and @c rhs - * @param lhs @c Reader object of a tensor - * @param rhs @c Reader object of a tensor - * @return @c Zipper object - */ -template <typename T> Zipper<T> zip(const Shape &shape, const Reader<T> &lhs, const Reader<T> &rhs) -{ - return Zipper<T>{shape, lhs, rhs}; -} - -} // namespace tensor -} // namespace misc -} // namespace nnfw - -#endif // __NNFW_MISC_TENSOR_ZIPPER_H__ |