aboutsummaryrefslogtreecommitdiff
path: root/reference_model/src/ops/data_nodes.cc
blob: 2ee4935be2d2ea3ec391ed2983e4ab338f282234 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172

// 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.

#include "data_nodes.h"

using namespace TosaReference;
using namespace Eigen;
using namespace tosa;

OpConst::OpConst(uint64_t id_)
    : GraphNode(Op_CONST, id_)
{
    setRequiredOperands(0, 1);
}

OpConst::~OpConst()
{}

int OpConst::checkTensorAttributes()
{
    if (validateRequiredOperands())
        return 1;

    return 0;
}

int OpConst::eval()
{
    // Evaluation is trivial for constants
    return GraphNode::eval();
}

OpPlaceholder::OpPlaceholder(uint64_t id_)
    : GraphNode(Op_PLACEHOLDER, id_)
{
    setRequiredOperands(0, 1);
}

OpPlaceholder::~OpPlaceholder()
{}

int OpPlaceholder::checkTensorAttributes()
{
    if (validateRequiredOperands())
        return 1;

    return 0;
}

int OpPlaceholder::eval()
{
    // Evaluation is trivial for placeholders
    return GraphNode::eval();
}

template <int Rank, DType Dtype>
OpIdentity<Rank, Dtype>::OpIdentity(TosaAttributeBase* attribute_, TosaQuantInfoBase* qinfo_, uint64_t id_)
    : GraphNode(Op_IDENTITY, id_)
{
    setRequiredOperands(1, 1);
    setRequiredRank(0, 6);
}

template <int Rank, DType Dtype>
OpIdentity<Rank, Dtype>::~OpIdentity()
{}

template <int Rank, DType Dtype>
int OpIdentity<Rank, Dtype>::checkTensorAttributes()
{

    if (inputs.size() != outputs.size())
    {
        printNodeValidationError("Input and output tensor list lengths are not equal");
        return 1;
    }

    in  = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]);
    out = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]);

    if (in->matchRankTypeShape(*out))
    {
        printNodeValidationError("Input and output tensor rank, type, or shape do not match");
        return 1;
    }

    return 0;
}

template <int Rank, DType Dtype>
int OpIdentity<Rank, Dtype>::eval()
{
    out->getTensor() = in->getTensor();

    return GraphNode::eval();
}

template <int Rank, DType Dtype>
OpIdentityN<Rank, Dtype>::OpIdentityN(TosaAttributeBase* attribute_, TosaQuantInfoBase* qinfo_, uint64_t id_)
    : GraphNode(Op_IDENTITYN, id_)
{
    setRequiredRank(0, 6);
}

template <int Rank, DType Dtype>
OpIdentityN<Rank, Dtype>::~OpIdentityN()
{}

template <int Rank, DType Dtype>
int OpIdentityN<Rank, Dtype>::checkTensorAttributes()
{

    if (inputs.size() != outputs.size())
    {
        printNodeValidationError("Input and output tensor list lengths are not equal");
        return 1;
    }

    for (size_t i = 0; i < inputs.size(); i++)
    {
        ins.push_back(dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[i]));
        outs.push_back(dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[i]));

        if (ins[i]->matchRankTypeShape(*outs[i]))
        {
            printNodeValidationError("Input and output tensor rank, type, or shape do not match");
            return 1;
        }
    }

    return 0;
}

template <int Rank, DType Dtype>
int OpIdentityN<Rank, Dtype>::eval()
{
    for (size_t i = 0; i < ins.size(); i++)
    {
        outs[i]->getTensor() = ins[i]->getTensor();
    }

    return GraphNode::eval();
}

// template explicit instantiation
// note OpConst and OpPlaceholder are not templated

DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpIdentity, FLOAT);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpIdentity, AINT8);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpIdentity, INT8);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpIdentity, INT16);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpIdentity, INT32);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpIdentity, BOOL);

DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpIdentityN, FLOAT);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpIdentityN, AINT8);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpIdentityN, INT8);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpIdentityN, INT16);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpIdentityN, INT32);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpIdentityN, BOOL);