diff options
Diffstat (limited to 'compiler/angkor/include/nncc/core/ADT/tensor')
10 files changed, 562 insertions, 0 deletions
diff --git a/compiler/angkor/include/nncc/core/ADT/tensor/Accessor.h b/compiler/angkor/include/nncc/core/ADT/tensor/Accessor.h new file mode 100644 index 000000000..6a60b4b34 --- /dev/null +++ b/compiler/angkor/include/nncc/core/ADT/tensor/Accessor.h @@ -0,0 +1,43 @@ +/* + * 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. + */ + +#ifndef __NNCC_CORE_ADT_TENSOR_ACCESSOR_H__ +#define __NNCC_CORE_ADT_TENSOR_ACCESSOR_H__ + +#include "nncc/core/ADT/tensor/Index.h" + +namespace nncc +{ +namespace core +{ +namespace ADT +{ +namespace tensor +{ + +template <typename T> struct Accessor +{ + virtual ~Accessor() = default; + + virtual T &at(const Index &) = 0; +}; + +} // namespace tensor +} // namespace ADT +} // namespace core +} // namespace nncc + +#endif // __NNCC_CORE_ADT_TENSOR_ACCESSOR_H__ diff --git a/compiler/angkor/include/nncc/core/ADT/tensor/Buffer.h b/compiler/angkor/include/nncc/core/ADT/tensor/Buffer.h new file mode 100644 index 000000000..f62f3040f --- /dev/null +++ b/compiler/angkor/include/nncc/core/ADT/tensor/Buffer.h @@ -0,0 +1,57 @@ +/* + * 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. + */ + +#ifndef __NNCC_CORE_ADT_TENSOR_BUFFER_H__ +#define __NNCC_CORE_ADT_TENSOR_BUFFER_H__ + +#include "nncc/core/ADT/tensor/View.h" + +namespace nncc +{ +namespace core +{ +namespace ADT +{ +namespace tensor +{ + +template <typename T> class Buffer final : public View<T> +{ +public: + explicit Buffer(const Shape &shape, const Layout &layout) : View<T>{shape, layout} + { + _buffer.resize(num_elements(shape)); + } + +public: + T *base(void) override { return _buffer.data(); } + const T *base(void) const override { return _buffer.data(); } + +private: + std::vector<T> _buffer; +}; + +template <typename T, typename LayoutImpl> Buffer<T> make_buffer(const Shape &shape) +{ + return Buffer<T>{shape, LayoutImpl{}}; +} + +} // namespace tensor +} // namespace ADT +} // namespace core +} // namespace nncc + +#endif // __NNCC_CORE_ADT_TENSOR_BUFFER_H__ diff --git a/compiler/angkor/include/nncc/core/ADT/tensor/Index.h b/compiler/angkor/include/nncc/core/ADT/tensor/Index.h new file mode 100644 index 000000000..19beafafc --- /dev/null +++ b/compiler/angkor/include/nncc/core/ADT/tensor/Index.h @@ -0,0 +1,65 @@ +/* + * 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. + */ + +#ifndef __NNCC_CORE_ADT_TENSOR_INDEX_H__ +#define __NNCC_CORE_ADT_TENSOR_INDEX_H__ + +#include <initializer_list> +#include <vector> +#include <cstdint> + +namespace nncc +{ +namespace core +{ +namespace ADT +{ +namespace tensor +{ + +class Index +{ +public: + Index() = default; + Index(std::initializer_list<uint32_t> &&l); + +public: + uint32_t rank(void) const; + +public: + Index &resize(uint32_t size); + +public: + Index &fill(uint32_t index); + +public: + uint32_t &at(uint32_t axis); + uint32_t at(uint32_t axis) const; + +private: + std::vector<uint32_t> _indices; +}; + +// It throws an exception when rank of inputs does not match. +Index operator+(const Index &lhs, const Index &rhs); +bool operator==(const Index &lhs, const Index &rhs); + +} // namespace tensor +} // namespace ADT +} // namespace core +} // namespace nncc + +#endif // __NNCC_CORE_ADT_TENSOR_INDEX_H__ diff --git a/compiler/angkor/include/nncc/core/ADT/tensor/IndexEnumerator.h b/compiler/angkor/include/nncc/core/ADT/tensor/IndexEnumerator.h new file mode 100644 index 000000000..ef85b2c10 --- /dev/null +++ b/compiler/angkor/include/nncc/core/ADT/tensor/IndexEnumerator.h @@ -0,0 +1,63 @@ +/* + * 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. + */ + +#ifndef __NNCC_CORE_ADT_TENSOR_INDEX_ENUMERATOR_H__ +#define __NNCC_CORE_ADT_TENSOR_INDEX_ENUMERATOR_H__ + +#include "nncc/core/ADT/tensor/Index.h" +#include "nncc/core/ADT/tensor/Shape.h" + +namespace nncc +{ +namespace core +{ +namespace ADT +{ +namespace tensor +{ + +class IndexEnumerator +{ +public: + explicit IndexEnumerator(const Shape &shape); + +public: + IndexEnumerator(IndexEnumerator &&) = delete; + IndexEnumerator(const IndexEnumerator &) = delete; + +public: + bool valid(void) const { return _cursor < _shape.rank(); } + +public: + const Index ¤t(void) const { return _index; } + +public: + void advance(void); + +private: + const Shape _shape; + Index _index; + +private: + uint32_t _cursor; +}; + +} // namespace tensor +} // namespace ADT +} // namespace core +} // namespace nncc + +#endif // __NNCC_CORE_ADT_TENSOR_INDEX_ENUMERATOR_H__ diff --git a/compiler/angkor/include/nncc/core/ADT/tensor/Layout.h b/compiler/angkor/include/nncc/core/ADT/tensor/Layout.h new file mode 100644 index 000000000..0e410ff01 --- /dev/null +++ b/compiler/angkor/include/nncc/core/ADT/tensor/Layout.h @@ -0,0 +1,52 @@ +/* + * 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. + */ + +#ifndef __NNCC_CORE_ADT_TENSOR_LAYOUT_H__ +#define __NNCC_CORE_ADT_TENSOR_LAYOUT_H__ + +#include "nncc/core/ADT/tensor/Shape.h" +#include "nncc/core/ADT/tensor/Index.h" + +namespace nncc +{ +namespace core +{ +namespace ADT +{ +namespace tensor +{ + +class Layout +{ +public: + using Func = uint32_t (*)(const Shape &, const Index &); + +public: + explicit Layout(const Func &func); + +public: + uint32_t offset(const Shape &shape, const Index &index) const { return _func(shape, index); } + +private: + Func _func; +}; + +} // namespace tensor +} // namespace ADT +} // namespace core +} // namespace nncc + +#endif // __NNCC_CORE_ADT_TENSOR_LAYOUT_H__ diff --git a/compiler/angkor/include/nncc/core/ADT/tensor/LexicalLayout.h b/compiler/angkor/include/nncc/core/ADT/tensor/LexicalLayout.h new file mode 100644 index 000000000..b497ad844 --- /dev/null +++ b/compiler/angkor/include/nncc/core/ADT/tensor/LexicalLayout.h @@ -0,0 +1,41 @@ +/* + * 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. + */ + +#ifndef __NNCC_CORE_ADT_TENSOR_LEXICAL_LAYOUT_H__ +#define __NNCC_CORE_ADT_TENSOR_LEXICAL_LAYOUT_H__ + +#include "nncc/core/ADT/tensor/Layout.h" + +namespace nncc +{ +namespace core +{ +namespace ADT +{ +namespace tensor +{ + +struct LexicalLayout final : public Layout +{ + LexicalLayout(); +}; + +} // namespace tensor +} // namespace ADT +} // namespace core +} // namespace nncc + +#endif // __NNCC_CORE_ADT_TENSOR_LEXICAL_LAYOUT_H__ diff --git a/compiler/angkor/include/nncc/core/ADT/tensor/Overlay.h b/compiler/angkor/include/nncc/core/ADT/tensor/Overlay.h new file mode 100644 index 000000000..11ee5350c --- /dev/null +++ b/compiler/angkor/include/nncc/core/ADT/tensor/Overlay.h @@ -0,0 +1,58 @@ +/* + * 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. + */ + +#ifndef __NNCC_CORE_ADT_TENSOR_OVERLAY_H__ +#define __NNCC_CORE_ADT_TENSOR_OVERLAY_H__ + +#include "nncc/core/ADT/tensor/View.h" + +namespace nncc +{ +namespace core +{ +namespace ADT +{ +namespace tensor +{ + +template <typename T> class Overlay final : public View<T> +{ +public: + explicit Overlay(const Shape &shape, const Layout &layout, T *base) + : View<T>{shape, layout}, _base{base} + { + // DO NOTHING + } + +public: + T *base(void) override { return _base; } + const T *base(void) const override { return _base; } + +private: + T *const _base; +}; + +template <typename T, typename LayoutImpl> Overlay<T> make_overlay(const Shape &shape, T *base) +{ + return Overlay<T>{shape, LayoutImpl{}, base}; +} + +} // namespace tensor +} // namespace ADT +} // namespace core +} // namespace nncc + +#endif // __NNCC_CORE_ADT_TENSOR_OVERLAY_H__ diff --git a/compiler/angkor/include/nncc/core/ADT/tensor/Reader.h b/compiler/angkor/include/nncc/core/ADT/tensor/Reader.h new file mode 100644 index 000000000..49f1287d2 --- /dev/null +++ b/compiler/angkor/include/nncc/core/ADT/tensor/Reader.h @@ -0,0 +1,43 @@ +/* + * 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. + */ + +#ifndef __NNCC_CORE_ADT_TENSOR_READER_H__ +#define __NNCC_CORE_ADT_TENSOR_READER_H__ + +#include "nncc/core/ADT/tensor/Index.h" + +namespace nncc +{ +namespace core +{ +namespace ADT +{ +namespace tensor +{ + +template <typename T> struct Reader +{ + virtual ~Reader() = default; + + virtual T at(const Index &) const = 0; +}; + +} // namespace tensor +} // namespace ADT +} // namespace core +} // namespace nncc + +#endif // __NNCC_CORE_ADT_TENSOR_READER_H__ diff --git a/compiler/angkor/include/nncc/core/ADT/tensor/Shape.h b/compiler/angkor/include/nncc/core/ADT/tensor/Shape.h new file mode 100644 index 000000000..3eaab0e54 --- /dev/null +++ b/compiler/angkor/include/nncc/core/ADT/tensor/Shape.h @@ -0,0 +1,70 @@ +/* + * 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. + */ + +#ifndef __NNCC_CORE_ADT_TENSOR_SHAPE_H__ +#define __NNCC_CORE_ADT_TENSOR_SHAPE_H__ + +#include <initializer_list> +#include <vector> +#include <cstdint> + +namespace nncc +{ +namespace core +{ +namespace ADT +{ +namespace tensor +{ + +class Shape +{ +public: + Shape() = default; + Shape(std::initializer_list<uint32_t> &&l); + +public: + uint32_t rank(void) const; + +public: + Shape &resize(uint32_t size); + +public: + uint32_t &dim(uint32_t axis); + uint32_t dim(uint32_t axis) const; + +public: + Shape &squeeze(void); + +private: + std::vector<uint32_t> _dims; +}; + +/** + * NOTE num_elements returns 1 for rank-0 tensors + */ +uint64_t num_elements(const Shape &); + +Shape squeeze(const Shape &); + +bool operator==(const Shape &, const Shape &); + +} // namespace tensor +} // namespace ADT +} // namespace core +} // namespace nncc + +#endif // __NNCC_CORE_ADT_TENSOR_SHAPE_H__ diff --git a/compiler/angkor/include/nncc/core/ADT/tensor/View.h b/compiler/angkor/include/nncc/core/ADT/tensor/View.h new file mode 100644 index 000000000..4c9a91539 --- /dev/null +++ b/compiler/angkor/include/nncc/core/ADT/tensor/View.h @@ -0,0 +1,70 @@ +/* + * 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. + */ + +#ifndef __NNCC_CORE_ADT_TENSOR_VIEW_H__ +#define __NNCC_CORE_ADT_TENSOR_VIEW_H__ + +#include "nncc/core/ADT/tensor/Shape.h" +#include "nncc/core/ADT/tensor/Index.h" +#include "nncc/core/ADT/tensor/Reader.h" +#include "nncc/core/ADT/tensor/Accessor.h" +#include "nncc/core/ADT/tensor/Layout.h" + +namespace nncc +{ +namespace core +{ +namespace ADT +{ +namespace tensor +{ + +template <typename T> class View : public Reader<T>, public Accessor<T> +{ +public: + explicit View(const Shape &shape, const Layout &layout) + : _shape{shape}, _layout{std::move(layout)} + { + // DO NOTHING + } + +public: + virtual ~View() = default; + +public: + virtual T *base(void) = 0; + virtual const T *base(void) const = 0; + +public: + T at(const Index &index) const override { return *(base() + _layout.offset(_shape, index)); } + +public: + T &at(const Index &index) override { return *(base() + _layout.offset(_shape, index)); } + +public: + const Shape &shape(void) const { return _shape; } + +private: + const Shape _shape; + const Layout _layout; +}; + +} // namespace tensor +} // namespace ADT +} // namespace core +} // namespace nncc + +#endif // __NNCC_CORE_ADT_TENSOR_VIEW_H__ |