Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +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 | #include "ewise_ternary.h" |
| 17 | |
| 18 | using namespace TosaReference; |
| 19 | using namespace Eigen; |
| 20 | using namespace tosa; |
| 21 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 22 | template <int Rank, TOSA_REF_TYPE Dtype> |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 23 | OpSelectBase<Rank, Dtype>::OpSelectBase(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 24 | : GraphNode(sgt_, Op_SELECT, id_) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 25 | { |
| 26 | setRequiredOperands(3, 1); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 29 | template <int Rank, TOSA_REF_TYPE Dtype> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 30 | OpSelectBase<Rank, Dtype>::~OpSelectBase() |
| 31 | {} |
| 32 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 33 | template <int Rank, TOSA_REF_TYPE Dtype> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 34 | int OpSelectBase<Rank, Dtype>::checkTensorAttributes() |
| 35 | { |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 36 | // Check Tosa Level |
| 37 | auto tosa_level = g_func_config.tosa_level; |
| 38 | LEVEL_CHECK(Rank <= tosa_level.MAX_RANK, "Rank should be smaller than or equal to MAX_RANK"); |
| 39 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 40 | if (validateRequiredOperands()) |
| 41 | return 1; |
| 42 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 43 | // output and input must be the same types |
Kevin Cheng | 1c3c847 | 2021-11-08 11:19:10 -0800 | [diff] [blame] | 44 | if (inputs[0]->matchRankShape(*outputs[0], true /* broadcastOk */) || |
| 45 | inputs[1]->matchRankTypeShape(*outputs[0], true /* broadcastOk */) || |
| 46 | inputs[2]->matchRankTypeShape(*outputs[0], true /* broadcastOk */)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 47 | { |
Kevin Cheng | 1c3c847 | 2021-11-08 11:19:10 -0800 | [diff] [blame] | 48 | printNodeValidationError("Failure to match input and output rank/type/shape"); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 49 | return 1; |
| 50 | } |
| 51 | |
| 52 | cond = dynamic_cast<TosaReference::TensorTemplate<TCond>*>(inputs[0]); |
| 53 | then_val = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[1]); |
| 54 | else_val = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[2]); |
| 55 | out = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(outputs[0]); |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 60 | template <int Rank, TOSA_REF_TYPE Dtype> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 61 | int OpSelectBase<Rank, Dtype>::eval() |
| 62 | { |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 63 | FATAL_ERROR("shouldn't be called"); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 66 | template <int Rank, TOSA_REF_TYPE Dtype> |
Jerry Ge | 135c955 | 2023-05-23 20:59:32 +0000 | [diff] [blame] | 67 | int OpSelect<Rank, Dtype>::broadcast(std::vector<int>& calculated_shape) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 68 | { |
Kevin Cheng | 1c3c847 | 2021-11-08 11:19:10 -0800 | [diff] [blame] | 69 | const std::vector<int>& cond_shape = this->cond->getShape(); |
| 70 | const std::vector<int>& then_shape = this->then_val->getShape(); |
| 71 | const std::vector<int>& else_shape = this->else_val->getShape(); |
| 72 | const std::vector<int>& output_shape = this->out->getShape(); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 73 | |
Jerry Ge | 135c955 | 2023-05-23 20:59:32 +0000 | [diff] [blame] | 74 | // calculates the multipliers for Eigen |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 75 | for (int i = 0; i < Rank; i++) |
| 76 | { |
Kevin Cheng | 1c3c847 | 2021-11-08 11:19:10 -0800 | [diff] [blame] | 77 | this->bcast_cond[i] = (cond_shape[i] != output_shape[i] && cond_shape[i] == 1) ? output_shape[i] : 1; |
| 78 | this->bcast_then[i] = (then_shape[i] != output_shape[i] && then_shape[i] == 1) ? output_shape[i] : 1; |
| 79 | 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] | 80 | } |
| 81 | |
Jerry Ge | 135c955 | 2023-05-23 20:59:32 +0000 | [diff] [blame] | 82 | // calculates the broadcasted output shape |
| 83 | calculated_shape = cond_shape; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 84 | for (size_t i = 0; i < calculated_shape.size(); i++) |
| 85 | { |
| 86 | if (calculated_shape[i] == 1) |
| 87 | { |
Jerry Ge | 135c955 | 2023-05-23 20:59:32 +0000 | [diff] [blame] | 88 | calculated_shape[i] = then_shape[i]; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 89 | } |
| 90 | else |
| 91 | { |
| 92 | ERROR_IF(then_shape[i] != 1 && then_shape[i] != calculated_shape[i], |
| 93 | "Broadcast_shape failure, input shapes are not compatible"); |
Jerry Ge | 135c955 | 2023-05-23 20:59:32 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 96 | if (calculated_shape[i] == 1) |
| 97 | { |
Jerry Ge | 135c955 | 2023-05-23 20:59:32 +0000 | [diff] [blame] | 98 | calculated_shape[i] = else_shape[i]; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 99 | } |
| 100 | else |
| 101 | { |
| 102 | ERROR_IF(else_shape[i] != 1 && else_shape[i] != calculated_shape[i], |
| 103 | "Broadcast_shape failure, input shapes are not compatible"); |
Jerry Ge | 135c955 | 2023-05-23 20:59:32 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 110 | template <int Rank, TOSA_REF_TYPE Dtype> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 111 | int OpSelect<Rank, Dtype>::eval() |
| 112 | { |
Jerry Ge | 135c955 | 2023-05-23 20:59:32 +0000 | [diff] [blame] | 113 | std::vector<int> calculated_shape; |
| 114 | this->broadcast(calculated_shape); |
| 115 | |
| 116 | auto result_shape = this->out->getShape(); |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 117 | ERROR_IF(calculated_shape != result_shape, |
| 118 | "Broadcast_shape failure, calculated_shape and result_shape don't match"); |
Jerry Ge | 135c955 | 2023-05-23 20:59:32 +0000 | [diff] [blame] | 119 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 120 | this->out->getTensor() = this->cond->getTensor() |
| 121 | .broadcast(this->bcast_cond) |
| 122 | .select(this->then_val->getTensor().broadcast(this->bcast_then), |
| 123 | this->else_val->getTensor().broadcast(this->bcast_else)); |
| 124 | |
| 125 | return GraphNode::eval(); |
| 126 | } |
| 127 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 128 | template <TOSA_REF_TYPE Dtype> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 129 | int OpSelect<0, Dtype>::eval() |
| 130 | { |
| 131 | this->out->getTensor() = this->cond->getTensor().select(this->then_val->getTensor(), this->else_val->getTensor()); |
| 132 | |
| 133 | return GraphNode::eval(); |
| 134 | } |
| 135 | |
| 136 | // template explicit instantiation |
Jared Smolens | 98c281f | 2022-12-20 15:09:25 -0800 | [diff] [blame] | 137 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSelectBase, FP16); |
| 138 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSelectBase, BF16); |
| 139 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSelectBase, FP32); |
| 140 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSelectBase, INT8); |
| 141 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSelectBase, INT16); |
| 142 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSelectBase, INT32); |
| 143 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSelectBase, BOOL); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 144 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSelectBase, FP64); |
Jared Smolens | 98c281f | 2022-12-20 15:09:25 -0800 | [diff] [blame] | 145 | |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 146 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSelect, FP16); |
James Ward | 24dbc42 | 2022-10-19 12:20:31 +0100 | [diff] [blame] | 147 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSelect, BF16); |
Jeremy Johnson | bc2a3db | 2022-09-27 13:50:00 +0100 | [diff] [blame] | 148 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSelect, FP32); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 149 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSelect, INT8); |
| 150 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSelect, INT16); |
| 151 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSelect, INT32); |
| 152 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSelect, BOOL); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 153 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSelect, FP64); |