blob: d85da1a42583ec600d32588a431fb258cf846407 [file] [log] [blame]
Eric Kunzee5e26762020-10-13 16:11:07 -07001
James Ward8b390432022-08-12 20:48:56 +01002// Copyright (c) 2020-2022, ARM Limited.
Eric Kunzee5e26762020-10-13 16:11:07 -07003//
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
18using namespace TosaReference;
19using namespace Eigen;
20using namespace tosa;
21
22template <int Rank, DType Dtype>
Kevin Chengacb550f2021-06-29 15:32:19 -070023OpSelectBase<Rank, Dtype>::OpSelectBase(SubgraphTraverser* sgt_,
24 TosaAttributeBase* attribute_,
Kevin Chengacb550f2021-06-29 15:32:19 -070025 uint64_t id_)
26 : GraphNode(sgt_, Op_SELECT, id_)
Eric Kunzee5e26762020-10-13 16:11:07 -070027{
28 setRequiredOperands(3, 1);
29 setRequiredRank(0, 6);
30}
31
32template <int Rank, DType Dtype>
33OpSelectBase<Rank, Dtype>::~OpSelectBase()
34{}
35
36template <int Rank, DType Dtype>
37int 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 Cheng1c3c8472021-11-08 11:19:10 -080049 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 Kunzee5e26762020-10-13 16:11:07 -070052 {
Kevin Cheng1c3c8472021-11-08 11:19:10 -080053 printNodeValidationError("Failure to match input and output rank/type/shape");
Eric Kunzee5e26762020-10-13 16:11:07 -070054 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
65template <int Rank, DType Dtype>
66int OpSelectBase<Rank, Dtype>::eval()
67{
Kevin Chengacb550f2021-06-29 15:32:19 -070068 FATAL_ERROR("shouldn't be called");
Eric Kunzee5e26762020-10-13 16:11:07 -070069}
70
71template <int Rank, DType Dtype>
72int OpSelect<Rank, Dtype>::broadcast()
73{
Kevin Cheng1c3c8472021-11-08 11:19:10 -080074 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 Kunzee5e26762020-10-13 16:11:07 -070078
79 for (int i = 0; i < Rank; i++)
80 {
Kevin Cheng1c3c8472021-11-08 11:19:10 -080081 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 Kunzee5e26762020-10-13 16:11:07 -070084 }
85
86 return 0;
87}
88
89template <int Rank, DType Dtype>
90int 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
101template <DType Dtype>
102int 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 Ward8b390432022-08-12 20:48:56 +0100110DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSelect, FP16);
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +0100111DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSelect, FP32);
Eric Kunzee5e26762020-10-13 16:11:07 -0700112DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSelect, INT8);
113DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSelect, INT16);
114DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSelect, INT32);
115DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSelect, BOOL);