2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "NodeExecution.h"
19 #include "NodeDataImpl.h"
20 #include "NodeDomain.h"
21 #include "Validation.h"
23 #include <nncc/core/ADT/tensor/Shape.h>
24 #include <nncc/core/ADT/tensor/Buffer.h>
25 #include <nncc/core/ADT/tensor/IndexEnumerator.h>
26 #include <nncc/core/ADT/tensor/LexicalLayout.h>
28 using nncc::core::ADT::tensor::IndexEnumerator;
29 using nncc::core::ADT::tensor::LexicalLayout;
30 using nncc::core::ADT::tensor::make_buffer;
37 using locomotiv::NodeData;
39 std::unique_ptr<NodeData> calc(const NodeData *input_data, const NodeData *bias_data,
47 using namespace locomotiv;
49 void execute_node(loco::BiasAdd<loco::Domain::Tensor> *bias_add)
51 validate(bias_add, "BiasAdd is nullptr");
53 auto input_data = locomotiv::annot_data(bias_add->value());
54 auto bias_data = locomotiv::annot_data(bias_add->bias());
56 validate(input_data && bias_data, "Input not ready");
57 validate(locomotiv::annot_domain(bias_add->value()) == loco::Domain::Tensor &&
58 locomotiv::annot_domain(bias_add->bias()) == loco::Domain::Bias,
59 "Wrong input domain");
61 std::unique_ptr<NodeData> bias_add_data = calc(input_data, bias_data, bias_add->axis());
63 assert(bias_add_data != nullptr);
64 annot_data(bias_add, std::move(bias_add_data));
65 annot_domain(bias_add, annot_domain(bias_add->value()));
68 void execute_node(loco::BiasAdd<loco::Domain::Feature> *bias_add)
70 validate(bias_add, "BiasAdd is nullptr");
72 auto input_data = locomotiv::annot_data(bias_add->value());
73 auto bias_data = locomotiv::annot_data(bias_add->bias());
75 validate(input_data && bias_data, "Input not ready");
76 validate(locomotiv::annot_domain(bias_add->value()) == loco::Domain::Feature &&
77 locomotiv::annot_domain(bias_add->bias()) == loco::Domain::Bias,
78 "Wrong input domain");
80 std::unique_ptr<NodeData> bias_add_data = calc(input_data, bias_data, 3);
82 assert(bias_add_data != nullptr);
83 annot_data(bias_add, std::move(bias_add_data));
84 annot_domain(bias_add, loco::Domain::Feature);
91 using locomotiv::NodeData;
92 using locomotiv::validate;
93 using locomotiv::make_data;
95 std::unique_ptr<NodeData> calc(const NodeData *input_data, const NodeData *bias_data, uint32_t axis)
97 validate(input_data->shape()->dim(axis) == bias_data->shape()->dim(0), "Bias size mismatch");
99 std::unique_ptr<NodeData> bias_add_data = nullptr;
101 switch (input_data->dtype())
103 case loco::DataType::FLOAT32:
105 auto input_bufptr = input_data->as_f32_bufptr();
106 auto bias_bufptr = bias_data->as_f32_bufptr();
107 auto bias_add_buf = make_buffer<float, LexicalLayout>(*input_data->shape());
109 auto *shape = input_data->shape();
111 for (IndexEnumerator e{*shape}; e.valid(); e.advance())
113 const auto &index = e.current();
114 nncc::core::ADT::tensor::Index bias_index({index.at(axis)});
115 bias_add_buf.at(index) = input_bufptr->at(index) + bias_bufptr->at(bias_index);
118 bias_add_data = make_data(bias_add_buf);
122 throw std::runtime_error("NYI for this DataType");
124 return bias_add_data;
132 void NodeExecution::execute(loco::BiasAdd<loco::Domain::Tensor> *bias_add)
134 execute_node(bias_add);
137 void NodeExecution::execute(loco::BiasAdd<loco::Domain::Feature> *bias_add)
139 execute_node(bias_add);
142 } // namespace locomotiv