blob: f24dfbe4550e07a071a92a98b86f2d019ec06244 [file] [log] [blame]
Eric Kunzee5e26762020-10-13 16:11:07 -07001
Tai Lya4d748b2023-03-28 22:06:56 +00002// Copyright (c) 2020-2023, 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#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
Tai Lya4d748b2023-03-28 22:06:56 +000032template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -070033class 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
Tai Lya4d748b2023-03-28 22:06:56 +000042 using CondEigenType = typename GetEigenType<TOSA_REF_TYPE_BOOL>::type;
Eric Kunzee5e26762020-10-13 16:11:07 -070043 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
Tai Lya4d748b2023-03-28 22:06:56 +000058template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -070059class 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();
Jerry Ge135c9552023-05-23 20:59:32 +000066 int broadcast(std::vector<int>& calculated_shape);
Eric Kunzee5e26762020-10-13 16:11:07 -070067
68 using InEigenType = typename OpSelectBase<Rank, Dtype>::InEigenType;
69};
70
71// partial specialization for rank 0
Tai Lya4d748b2023-03-28 22:06:56 +000072template <TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -070073class 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