aboutsummaryrefslogtreecommitdiff
path: root/reference_model/src/ops/ewise_ternary.h
blob: b354247e7d3bb4a4ffc0a57a6b382b893dcfe0b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

// Copyright (c) 2020, ARM Limited.
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//
//         http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.

#ifndef OPS_TERNARY_H
#define OPS_TERNARY_H

#include "graph_node.h"

using namespace tosa;

namespace TosaReference
{

// The Ternary Select op has the following operands:
//   1.   Cond:   rank N, type=bool
//   2.   Then_val: Rank N, type=<V>
//   3.   Else_val: Rank N, type=<V>
//   4.   Result:   Rank N, type=<V>
// Cond, Then_val, Else_val need to be mutually-broadcastable
template <int Rank, DType Dtype>
class OpSelectBase : public GraphNode
{
public:
    OpSelectBase(TosaAttributeBase* attribute_, TosaQuantInfoBase* qinfo_, uint64_t id_);
    virtual ~OpSelectBase();

    virtual int checkTensorAttributes();
    virtual int eval();

    using CondEigenType = typename GetEigenType<DType_BOOL>::type;
    using InEigenType   = typename GetEigenType<Dtype>::type;
    using TCond         = Eigen::Tensor<CondEigenType, Rank>;
    using TIn           = Eigen::Tensor<InEigenType, Rank>;

protected:
    TosaReference::TensorTemplate<TCond>* cond;
    Eigen::array<int, Rank> bcast_cond;
    Eigen::array<int, Rank> bcast_then;
    Eigen::array<int, Rank> bcast_else;
    TosaReference::TensorTemplate<TIn>* then_val;
    TosaReference::TensorTemplate<TIn>* else_val;
    TosaReference::TensorTemplate<TIn>* out;
};

// primary class
template <int Rank, DType Dtype>
class OpSelect : public OpSelectBase<Rank, Dtype>
{
public:
    OpSelect(TosaAttributeBase* attribute_, TosaQuantInfoBase* qinfo_, uint64_t id_)
        : OpSelectBase<Rank, Dtype>(attribute_, qinfo_, id_)
    {}
    virtual int eval();
    int broadcast();

    using InEigenType = typename OpSelectBase<Rank, Dtype>::InEigenType;
};

// partial specialization for rank 0
template <DType Dtype>
class OpSelect<0, Dtype> : public OpSelectBase<0, Dtype>
{
public:
    OpSelect(TosaAttributeBase* attribute_, TosaQuantInfoBase* qinfo_, uint64_t id_)
        : OpSelectBase<0, Dtype>(attribute_, qinfo_, id_)
    {}
    virtual int eval();
};
};    // namespace TosaReference

#endif