Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1 | |
Luke Hutton | a4e48ca | 2023-02-22 11:53:48 +0000 | [diff] [blame] | 2 | // Copyright (c) 2020-2023, ARM Limited. |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 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 | #ifndef OPS_DATA_LAYOUT_H |
| 17 | #define OPS_DATA_LAYOUT_H |
| 18 | |
| 19 | #include "graph_node.h" |
| 20 | |
| 21 | using namespace tosa; |
| 22 | |
| 23 | namespace TosaReference |
| 24 | { |
| 25 | |
| 26 | template <int Rank, DType Dtype> |
| 27 | class OpConcat : public GraphNode |
| 28 | { |
| 29 | public: |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 30 | OpConcat(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 31 | virtual ~OpConcat(); |
| 32 | |
| 33 | virtual int checkTensorAttributes(); |
| 34 | virtual int eval(); |
| 35 | |
| 36 | using InEigenType = typename GetEigenType<Dtype>::type; |
| 37 | using OutEigenType = typename GetEigenType<Dtype>::type; |
| 38 | using TIn = Eigen::Tensor<InEigenType, Rank>; |
| 39 | using TOut = Eigen::Tensor<OutEigenType, Rank>; |
| 40 | |
| 41 | protected: |
| 42 | Eigen::array<int, Rank> reverser; |
Kevin Cheng | ad15dfa | 2021-03-04 15:15:03 -0800 | [diff] [blame] | 43 | std::vector<TosaReference::TensorTemplate<TIn>*> ins; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 44 | TosaAxisAttribute* attribute; |
| 45 | TosaReference::TensorTemplate<TOut>* out; |
| 46 | }; |
| 47 | |
| 48 | template <int Rank, DType Dtype> |
| 49 | class OpPad : public GraphNode |
| 50 | { |
| 51 | public: |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 52 | OpPad(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 53 | virtual ~OpPad(); |
| 54 | virtual int checkTensorAttributes(); |
| 55 | virtual int eval(); |
| 56 | |
| 57 | using InEigenType = typename GetEigenType<Dtype>::type; |
| 58 | using OutEigenType = typename GetEigenType<Dtype>::type; |
| 59 | using TIn = Eigen::Tensor<InEigenType, Rank>; |
| 60 | using TOut = Eigen::Tensor<OutEigenType, Rank>; |
| 61 | |
| 62 | protected: |
| 63 | Eigen::array<std::pair<ptrdiff_t, ptrdiff_t>, Rank> paddings_array; |
| 64 | TosaReference::TensorTemplate<TIn>* in; |
| 65 | TosaReference::TensorTemplate<TOut>* out; |
Kevin Cheng | fe392ce | 2021-10-18 21:51:55 +0000 | [diff] [blame] | 66 | TosaPadAttribute* attribute; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | template <int InRank, int OutRank, DType Dtype> |
| 70 | class OpReshape : public GraphNode |
| 71 | { |
| 72 | public: |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 73 | OpReshape(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 74 | virtual ~OpReshape(); |
| 75 | |
| 76 | virtual int checkTensorAttributes(); |
| 77 | virtual int eval(); |
| 78 | |
| 79 | using InEigenType = typename GetEigenType<Dtype>::type; |
| 80 | using OutEigenType = typename GetEigenType<Dtype>::type; |
| 81 | using TIn = Eigen::Tensor<InEigenType, InRank>; |
| 82 | using TOut = Eigen::Tensor<OutEigenType, OutRank>; |
| 83 | |
| 84 | protected: |
| 85 | Eigen::array<Eigen::Index, OutRank> array_shape; |
| 86 | Eigen::array<Eigen::Index, InRank> in_reverser; |
| 87 | Eigen::array<Eigen::Index, OutRank> out_reverser; |
| 88 | TosaReference::TensorTemplate<TIn>* in; |
| 89 | TosaReshapeAttribute* attribute; |
| 90 | TosaReference::TensorTemplate<TOut>* out; |
| 91 | }; |
| 92 | |
| 93 | template <int Rank, DType Dtype> |
| 94 | class OpReverse : public GraphNode |
| 95 | { |
| 96 | public: |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 97 | OpReverse(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 98 | virtual ~OpReverse(); |
| 99 | |
| 100 | virtual int checkTensorAttributes(); |
| 101 | virtual int eval(); |
| 102 | |
| 103 | using InEigenType = typename GetEigenType<Dtype>::type; |
| 104 | using OutEigenType = typename GetEigenType<Dtype>::type; |
| 105 | using TIn = Eigen::Tensor<InEigenType, Rank>; |
| 106 | using TOut = Eigen::Tensor<OutEigenType, Rank>; |
| 107 | |
| 108 | protected: |
| 109 | TosaAxisAttribute* attribute; |
| 110 | TosaReference::TensorTemplate<TIn>* in; |
| 111 | TosaReference::TensorTemplate<TOut>* out; |
| 112 | Eigen::array<bool, Rank> reverse_array; |
| 113 | }; |
| 114 | |
| 115 | template <int Rank, DType Dtype> |
| 116 | class OpSlice : public GraphNode |
| 117 | { |
| 118 | public: |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 119 | OpSlice(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 120 | virtual ~OpSlice(); |
| 121 | |
| 122 | virtual int checkTensorAttributes(); |
| 123 | virtual int eval(); |
| 124 | |
| 125 | using InEigenType = typename GetEigenType<Dtype>::type; |
| 126 | using OutEigenType = typename GetEigenType<Dtype>::type; |
| 127 | using TIn = Eigen::Tensor<InEigenType, Rank>; |
| 128 | using TOut = Eigen::Tensor<OutEigenType, Rank>; |
| 129 | |
| 130 | protected: |
| 131 | TosaSliceAttribute* attribute; |
| 132 | Eigen::array<Eigen::Index, Rank> begin_array; |
| 133 | Eigen::array<Eigen::Index, Rank> size_array; |
| 134 | TosaReference::TensorTemplate<TIn>* in; |
| 135 | TosaReference::TensorTemplate<TOut>* out; |
| 136 | }; |
| 137 | |
| 138 | template <int Rank, DType Dtype> |
| 139 | class OpTileBase : public GraphNode |
| 140 | { |
| 141 | public: |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 142 | OpTileBase(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 143 | virtual ~OpTileBase(); |
| 144 | |
| 145 | virtual int checkTensorAttributes(); |
| 146 | |
| 147 | using InEigenType = typename GetEigenType<Dtype>::type; |
| 148 | using OutEigenType = typename GetEigenType<Dtype>::type; |
| 149 | using TIn = Eigen::Tensor<InEigenType, Rank>; |
| 150 | using TOut = Eigen::Tensor<OutEigenType, Rank>; |
| 151 | |
| 152 | protected: |
| 153 | TosaTileAttribute* attribute; |
| 154 | TosaReference::TensorTemplate<TIn>* in; |
| 155 | TosaReference::TensorTemplate<TOut>* out; |
| 156 | }; |
| 157 | |
| 158 | // primary template for op tile |
| 159 | template <int Rank, DType Dtype> |
| 160 | class OpTile : public OpTileBase<Rank, Dtype> |
| 161 | { |
| 162 | public: |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 163 | OpTile(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) |
| 164 | : OpTileBase<Rank, Dtype>(sgt_, attribute_, id_) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 165 | {} |
| 166 | |
| 167 | protected: |
| 168 | virtual int eval(); |
| 169 | }; |
| 170 | |
| 171 | // partial specialization for specific rank |
| 172 | #define DEF_OP_TILE_RANK(N) \ |
| 173 | template <DType Dtype> \ |
| 174 | class OpTile<N, Dtype> : public OpTileBase<N, Dtype> \ |
| 175 | { \ |
| 176 | public: \ |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 177 | OpTile(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) \ |
| 178 | : OpTileBase<N, Dtype>(sgt_, attribute_, id_) \ |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 179 | {} \ |
| 180 | \ |
| 181 | protected: \ |
| 182 | virtual int eval(); \ |
| 183 | }; |
| 184 | |
| 185 | DEF_OP_TILE_RANK(1) |
| 186 | DEF_OP_TILE_RANK(2) |
| 187 | DEF_OP_TILE_RANK(3) |
| 188 | DEF_OP_TILE_RANK(4) |
Luke Hutton | a4e48ca | 2023-02-22 11:53:48 +0000 | [diff] [blame] | 189 | DEF_OP_TILE_RANK(5) |
| 190 | DEF_OP_TILE_RANK(6) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 191 | |
| 192 | #undef DEF_OP_TILE_RANK |
| 193 | |
| 194 | template <int Rank, DType Dtype> |
| 195 | class OpTranspose : public GraphNode |
| 196 | { |
| 197 | public: |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 198 | OpTranspose(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 199 | virtual ~OpTranspose(); |
| 200 | |
| 201 | virtual int checkTensorAttributes(); |
| 202 | virtual int eval(); |
| 203 | |
| 204 | using InEigenType = typename GetEigenType<Dtype>::type; |
| 205 | using OutEigenType = typename GetEigenType<Dtype>::type; |
| 206 | using TIn = Eigen::Tensor<InEigenType, Rank>; |
| 207 | using TOut = Eigen::Tensor<OutEigenType, Rank>; |
| 208 | |
| 209 | protected: |
| 210 | Eigen::array<int, Rank> perm_array; |
Kevin Cheng | fe392ce | 2021-10-18 21:51:55 +0000 | [diff] [blame] | 211 | TosaTransposeAttribute* attribute; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 212 | TosaReference::TensorTemplate<TIn>* in; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 213 | TosaReference::TensorTemplate<TOut>* out; |
| 214 | }; |
| 215 | }; // namespace TosaReference |
| 216 | |
| 217 | #endif |