blob: 8b480eeec6305f20bf285b11dd966bbb036e9bad [file] [log] [blame]
Eric Kunzee5e26762020-10-13 16:11:07 -07001
Won Jeon2c34b462024-02-06 18:37:00 +00002// Copyright (c) 2020-2024, 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 "data_nodes.h"
17
18using namespace TosaReference;
19using namespace Eigen;
20using namespace tosa;
21
Kevin Chengacb550f2021-06-29 15:32:19 -070022OpConst::OpConst(SubgraphTraverser* sgt_, uint64_t id_)
23 : GraphNode(sgt_, Op_CONST, id_)
Eric Kunzee5e26762020-10-13 16:11:07 -070024{
25 setRequiredOperands(0, 1);
26}
27
28OpConst::~OpConst()
29{}
30
31int OpConst::checkTensorAttributes()
32{
33 if (validateRequiredOperands())
34 return 1;
35
36 return 0;
37}
38
39int OpConst::eval()
40{
Jeremy Johnson452ba702023-10-23 16:56:46 +010041 for (auto ct : getOutputs())
42 {
43 if (!ct->getIsValid())
44 {
45 std::string err = "Constant tensor " + ct->getName() + " not correctly initialized";
46 printNodeValidationError(err.c_str());
47 return 1;
48 }
49 }
50
Eric Kunzee5e26762020-10-13 16:11:07 -070051 // Evaluation is trivial for constants
52 return GraphNode::eval();
53}
54
Tai Lya4d748b2023-03-28 22:06:56 +000055template <int Rank, TOSA_REF_TYPE Dtype>
Jerry Ge9c9c8da2023-07-19 23:08:16 +000056OpIdentity<Rank, Dtype>::OpIdentity(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_)
Kevin Chengacb550f2021-06-29 15:32:19 -070057 : GraphNode(sgt_, Op_IDENTITY, id_)
Eric Kunzee5e26762020-10-13 16:11:07 -070058{
59 setRequiredOperands(1, 1);
60 setRequiredRank(0, 6);
61}
62
Tai Lya4d748b2023-03-28 22:06:56 +000063template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -070064OpIdentity<Rank, Dtype>::~OpIdentity()
65{}
66
Tai Lya4d748b2023-03-28 22:06:56 +000067template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -070068int OpIdentity<Rank, Dtype>::checkTensorAttributes()
69{
70
71 if (inputs.size() != outputs.size())
72 {
73 printNodeValidationError("Input and output tensor list lengths are not equal");
74 return 1;
75 }
76
77 in = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]);
78 out = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]);
79
80 if (in->matchRankTypeShape(*out))
81 {
82 printNodeValidationError("Input and output tensor rank, type, or shape do not match");
83 return 1;
84 }
85
86 return 0;
87}
88
Tai Lya4d748b2023-03-28 22:06:56 +000089template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -070090int OpIdentity<Rank, Dtype>::eval()
91{
92 out->getTensor() = in->getTensor();
93
94 return GraphNode::eval();
95}
96
Eric Kunzee5e26762020-10-13 16:11:07 -070097// template explicit instantiation
Kevin Cheng14d7f7a2021-05-12 10:44:49 -070098// note OpConst is not templated
Eric Kunzee5e26762020-10-13 16:11:07 -070099
James Ward8b390432022-08-12 20:48:56 +0100100DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpIdentity, FP16);
James Ward24dbc422022-10-19 12:20:31 +0100101DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpIdentity, BF16);
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +0100102DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpIdentity, FP32);
evacha011adff832024-03-06 17:33:44 +0000103DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpIdentity, INT4);
Eric Kunzee5e26762020-10-13 16:11:07 -0700104DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpIdentity, INT8);
105DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpIdentity, INT16);
106DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpIdentity, INT32);
evacha011adff832024-03-06 17:33:44 +0000107DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpIdentity, INT48);
Eric Kunzee5e26762020-10-13 16:11:07 -0700108DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpIdentity, BOOL);
Tai Lya4d748b2023-03-28 22:06:56 +0000109DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpIdentity, FP64);
Won Jeon2c34b462024-02-06 18:37:00 +0000110DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpIdentity, FP8E4M3);
111DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpIdentity, FP8E5M2);