blob: 75a2194b073607ef514bf7358fab1af915ab7119 [file] [log] [blame]
Eric Kunzee5e26762020-10-13 16:11:07 -07001
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
21using namespace tosa;
22
23namespace 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
32template <int Rank, DType Dtype>
33class OpSelectBase : public GraphNode
34{
35public:
Eric Kunzeb5fabec2022-06-07 05:20:44 +000036 OpSelectBase(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_);
Eric Kunzee5e26762020-10-13 16:11:07 -070037 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
47protected:
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
58template <int Rank, DType Dtype>
59class OpSelect : public OpSelectBase<Rank, Dtype>
60{
61public:
Eric Kunzeb5fabec2022-06-07 05:20:44 +000062 OpSelect(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_)
63 : OpSelectBase<Rank, Dtype>(sgt_, attribute_, id_)
Eric Kunzee5e26762020-10-13 16:11:07 -070064 {}
65 virtual int eval();
66 int broadcast();
67
68 using InEigenType = typename OpSelectBase<Rank, Dtype>::InEigenType;
69};
70
71// partial specialization for rank 0
72template <DType Dtype>
73class OpSelect<0, Dtype> : public OpSelectBase<0, Dtype>
74{
75public:
Eric Kunzeb5fabec2022-06-07 05:20:44 +000076 OpSelect(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_)
77 : OpSelectBase<0, Dtype>(sgt_, attribute_, id_)
Eric Kunzee5e26762020-10-13 16:11:07 -070078 {}
79 virtual int eval();
80};
81}; // namespace TosaReference
82
83#endif