ONE - On-device Neural Engine
Loading...
Searching...
No Matches
NodeMixins.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef __LOCO_IR_NODE_MIXINS_H__
18#define __LOCO_IR_NODE_MIXINS_H__
19
20#include "loco/IR/Node.h"
21#include "loco/IR/DataType.h"
22#include "loco/IR/Dimension.h"
23
24#include <vector>
25#include <initializer_list>
26
27namespace loco
28{
29
30enum class NodeTrait
31{
33 // Nodes with TensorShape trait will provide the following methods:
34 // - rank()
35 // - rank(value)
36 // - dim()
37 // - dim(value)
38 // - shape({...})
40};
41
42template <NodeTrait T> class NodeMixin;
43
44template <> class NodeMixin<NodeTrait::DataType>
45{
46public:
47 NodeMixin() = default;
48
49public:
50 const DataType &dtype(void) const { return _dtype; }
51 void dtype(const DataType &dtype) { _dtype = dtype; }
52
53private:
55 DataType _dtype{DataType::Unknown};
56};
57
58template <> class NodeMixin<NodeTrait::TensorShape>
59{
60public:
61 NodeMixin() = default;
62
63public:
64 uint32_t rank(void) const { return _dims.size(); }
65 void rank(uint32_t value) { _dims.resize(value); }
66
67 const Dimension &dim(uint32_t axis) const { return _dims.at(axis); }
68 Dimension &dim(uint32_t axis) { return _dims.at(axis); }
69
70 void shape(std::initializer_list<uint32_t> dims)
71 {
72 rank(dims.size());
73
74 uint32_t axis = 0;
75 for (auto d : dims)
76 {
77 dim(axis++) = d;
78 }
79 }
80
81private:
83 std::vector<Dimension> _dims;
84};
85
86template <uint32_t N> struct FixedArity
87{
88 template <typename Base> class Mixin : public virtual Base
89 {
90 public:
92 {
93 for (uint32_t n = 0; n < N; ++n)
94 {
95 _args[n] = std::unique_ptr<Use>{new Use{this}};
96 }
97 }
98
99 virtual ~Mixin() = default;
100
101 public:
102 uint32_t arity(void) const final { return N; }
103
104 Node *arg(uint32_t n) const final { return _args.at(n)->node(); }
105
106 void drop(void) final
107 {
108 for (uint32_t n = 0; n < N; ++n)
109 {
110 _args.at(n)->node(nullptr);
111 }
112 }
113
114 protected:
115 // This API allows inherited classes to access "_args" field.
116 Use *at(uint32_t n) const { return _args.at(n).get(); }
117
118 private:
119 std::array<std::unique_ptr<Use>, N> _args{};
120 };
121};
122
123template <NodeTrait Trait> struct With
124{
125 template <typename Base> struct Mixin : public virtual Base, public NodeMixin<Trait>
126 {
127 // DO NOTHING
128 };
129};
130
131} // namespace loco
132
133#endif // __LOCO_IR_NODE_MIXINS_H__
The value of one dimension in a tensor shape.
Definition Dimension.h:30
virtual ~Mixin()=default
Use * at(uint32_t n) const
Definition NodeMixins.h:116
uint32_t arity(void) const final
Definition NodeMixins.h:102
void drop(void) final
Definition NodeMixins.h:106
Node * arg(uint32_t n) const final
Definition NodeMixins.h:104
Logical unit of computation.
Definition Node.h:54
const DataType & dtype(void) const
Definition NodeMixins.h:50
void dtype(const DataType &dtype)
Definition NodeMixins.h:51
const Dimension & dim(uint32_t axis) const
Definition NodeMixins.h:67
void shape(std::initializer_list< uint32_t > dims)
Definition NodeMixins.h:70
Dimension & dim(uint32_t axis)
Definition NodeMixins.h:68
The edge between a node definition and its user.
Definition Use.h:37
NodeTrait
Definition NodeMixins.h:31
DataType
"scalar" value type
Definition DataType.h:27