Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1 | |
| 2 | // Copyright (c) 2020, ARM Limited. |
| 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_TERNARY_H |
| 17 | #define OPS_TERNARY_H |
| 18 | |
| 19 | #include "graph_node.h" |
| 20 | |
| 21 | using namespace tosa; |
| 22 | |
| 23 | namespace TosaReference |
| 24 | { |
| 25 | |
| 26 | // The Ternary Select op has the following operands: |
| 27 | // 1. Cond: rank N, type=bool |
| 28 | // 2. Then_val: Rank N, type=<V> |
| 29 | // 3. Else_val: Rank N, type=<V> |
| 30 | // 4. Result: Rank N, type=<V> |
| 31 | // Cond, Then_val, Else_val need to be mutually-broadcastable |
| 32 | template <int Rank, DType Dtype> |
| 33 | class OpSelectBase : public GraphNode |
| 34 | { |
| 35 | public: |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 36 | OpSelectBase(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 37 | virtual ~OpSelectBase(); |
| 38 | |
| 39 | virtual int checkTensorAttributes(); |
| 40 | virtual int eval(); |
| 41 | |
| 42 | using CondEigenType = typename GetEigenType<DType_BOOL>::type; |
| 43 | using InEigenType = typename GetEigenType<Dtype>::type; |
| 44 | using TCond = Eigen::Tensor<CondEigenType, Rank>; |
| 45 | using TIn = Eigen::Tensor<InEigenType, Rank>; |
| 46 | |
| 47 | protected: |
| 48 | TosaReference::TensorTemplate<TCond>* cond; |
| 49 | Eigen::array<int, Rank> bcast_cond; |
| 50 | Eigen::array<int, Rank> bcast_then; |
| 51 | Eigen::array<int, Rank> bcast_else; |
| 52 | TosaReference::TensorTemplate<TIn>* then_val; |
| 53 | TosaReference::TensorTemplate<TIn>* else_val; |
| 54 | TosaReference::TensorTemplate<TIn>* out; |
| 55 | }; |
| 56 | |
| 57 | // primary class |
| 58 | template <int Rank, DType Dtype> |
| 59 | class OpSelect : public OpSelectBase<Rank, Dtype> |
| 60 | { |
| 61 | public: |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 62 | OpSelect(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) |
| 63 | : OpSelectBase<Rank, Dtype>(sgt_, attribute_, id_) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 64 | {} |
| 65 | virtual int eval(); |
| 66 | int broadcast(); |
| 67 | |
| 68 | using InEigenType = typename OpSelectBase<Rank, Dtype>::InEigenType; |
| 69 | }; |
| 70 | |
| 71 | // partial specialization for rank 0 |
| 72 | template <DType Dtype> |
| 73 | class OpSelect<0, Dtype> : public OpSelectBase<0, Dtype> |
| 74 | { |
| 75 | public: |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 76 | OpSelect(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) |
| 77 | : OpSelectBase<0, Dtype>(sgt_, attribute_, id_) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 78 | {} |
| 79 | virtual int eval(); |
| 80 | }; |
| 81 | }; // namespace TosaReference |
| 82 | |
| 83 | #endif |