Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1 | |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 2 | // Copyright (c) 2020-2022, 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 | #include "ewise_ternary.h" |
| 17 | |
| 18 | using namespace TosaReference; |
| 19 | using namespace Eigen; |
| 20 | using namespace tosa; |
| 21 | |
| 22 | template <int Rank, DType Dtype> |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 23 | OpSelectBase<Rank, Dtype>::OpSelectBase(SubgraphTraverser* sgt_, |
| 24 | TosaAttributeBase* attribute_, |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 25 | uint64_t id_) |
| 26 | : GraphNode(sgt_, Op_SELECT, id_) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 27 | { |
| 28 | setRequiredOperands(3, 1); |
| 29 | setRequiredRank(0, 6); |
| 30 | } |
| 31 | |
| 32 | template <int Rank, DType Dtype> |
| 33 | OpSelectBase<Rank, Dtype>::~OpSelectBase() |
| 34 | {} |
| 35 | |
| 36 | template <int Rank, DType Dtype> |
| 37 | int OpSelectBase<Rank, Dtype>::checkTensorAttributes() |
| 38 | { |
| 39 | if (validateRequiredOperands()) |
| 40 | return 1; |
| 41 | |
| 42 | if (validateRequiredRank(inputs[0]) || validateRequiredRank(inputs[1]) || validateRequiredRank(inputs[2]) || |
| 43 | validateRequiredRank(outputs[0])) |
| 44 | { |
| 45 | return 1; |
| 46 | } |
| 47 | |
| 48 | // output and input must be the same types |
Kevin Cheng | 1c3c847 | 2021-11-08 11:19:10 -0800 | [diff] [blame] | 49 | if (inputs[0]->matchRankShape(*outputs[0], true /* broadcastOk */) || |
| 50 | inputs[1]->matchRankTypeShape(*outputs[0], true /* broadcastOk */) || |
| 51 | inputs[2]->matchRankTypeShape(*outputs[0], true /* broadcastOk */)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 52 | { |
Kevin Cheng | 1c3c847 | 2021-11-08 11:19:10 -0800 | [diff] [blame] | 53 | printNodeValidationError("Failure to match input and output rank/type/shape"); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 54 | return 1; |
| 55 | } |
| 56 | |
| 57 | cond = dynamic_cast<TosaReference::TensorTemplate<TCond>*>(inputs[0]); |
| 58 | then_val = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[1]); |
| 59 | else_val = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[2]); |
| 60 | out = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(outputs[0]); |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | template <int Rank, DType Dtype> |
| 66 | int OpSelectBase<Rank, Dtype>::eval() |
| 67 | { |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 68 | FATAL_ERROR("shouldn't be called"); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | template <int Rank, DType Dtype> |
| 72 | int OpSelect<Rank, Dtype>::broadcast() |
| 73 | { |
Kevin Cheng | 1c3c847 | 2021-11-08 11:19:10 -0800 | [diff] [blame] | 74 | const std::vector<int>& cond_shape = this->cond->getShape(); |
| 75 | const std::vector<int>& then_shape = this->then_val->getShape(); |
| 76 | const std::vector<int>& else_shape = this->else_val->getShape(); |
| 77 | const std::vector<int>& output_shape = this->out->getShape(); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 78 | |
| 79 | for (int i = 0; i < Rank; i++) |
| 80 | { |
Kevin Cheng | 1c3c847 | 2021-11-08 11:19:10 -0800 | [diff] [blame] | 81 | this->bcast_cond[i] = (cond_shape[i] != output_shape[i] && cond_shape[i] == 1) ? output_shape[i] : 1; |
| 82 | this->bcast_then[i] = (then_shape[i] != output_shape[i] && then_shape[i] == 1) ? output_shape[i] : 1; |
| 83 | this->bcast_else[i] = (else_shape[i] != output_shape[i] && else_shape[i] == 1) ? output_shape[i] : 1; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | template <int Rank, DType Dtype> |
| 90 | int OpSelect<Rank, Dtype>::eval() |
| 91 | { |
| 92 | this->broadcast(); |
| 93 | this->out->getTensor() = this->cond->getTensor() |
| 94 | .broadcast(this->bcast_cond) |
| 95 | .select(this->then_val->getTensor().broadcast(this->bcast_then), |
| 96 | this->else_val->getTensor().broadcast(this->bcast_else)); |
| 97 | |
| 98 | return GraphNode::eval(); |
| 99 | } |
| 100 | |
| 101 | template <DType Dtype> |
| 102 | int OpSelect<0, Dtype>::eval() |
| 103 | { |
| 104 | this->out->getTensor() = this->cond->getTensor().select(this->then_val->getTensor(), this->else_val->getTensor()); |
| 105 | |
| 106 | return GraphNode::eval(); |
| 107 | } |
| 108 | |
| 109 | // template explicit instantiation |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 110 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSelect, FP16); |
Jeremy Johnson | bc2a3db | 2022-09-27 13:50:00 +0100 | [diff] [blame^] | 111 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSelect, FP32); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 112 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSelect, INT8); |
| 113 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSelect, INT16); |
| 114 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSelect, INT32); |
| 115 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSelect, BOOL); |