ONE - On-device Neural Engine
Loading...
Searching...
No Matches
BiasAdd.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
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
17#include "NodeExecution.h"
18
19#include "NodeDataImpl.h"
20#include "NodeDomain.h"
21#include "Validation.h"
22
27
31
32#include <cassert>
33#include <stdexcept>
34
35namespace
36{
38
39std::unique_ptr<NodeData> calc(const NodeData *input_data, const NodeData *bias_data,
40 uint32_t axis);
41
42} // namespace
43
44namespace
45{
46
47using namespace locomotiv;
48
49void execute_node(loco::BiasAdd<loco::Domain::Tensor> *bias_add)
50{
51 validate(bias_add, "BiasAdd is nullptr");
52
53 auto input_data = locomotiv::annot_data(bias_add->value());
54 auto bias_data = locomotiv::annot_data(bias_add->bias());
55
56 validate(input_data && bias_data, "Input not ready");
59 "Wrong input domain");
60
61 std::unique_ptr<NodeData> bias_add_data = calc(input_data, bias_data, bias_add->axis());
62
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()));
66}
67
68void execute_node(loco::BiasAdd<loco::Domain::Feature> *bias_add)
69{
70 validate(bias_add, "BiasAdd is nullptr");
71
72 auto input_data = locomotiv::annot_data(bias_add->value());
73 auto bias_data = locomotiv::annot_data(bias_add->bias());
74
75 validate(input_data && bias_data, "Input not ready");
78 "Wrong input domain");
79
80 std::unique_ptr<NodeData> bias_add_data = calc(input_data, bias_data, 3);
81
82 assert(bias_add_data != nullptr);
83 annot_data(bias_add, std::move(bias_add_data));
85}
86
87} // namespace
88
89namespace
90{
94
95std::unique_ptr<NodeData> calc(const NodeData *input_data, const NodeData *bias_data, uint32_t axis)
96{
97 validate(input_data->shape()->dim(axis) == bias_data->shape()->dim(0), "Bias size mismatch");
98
99 std::unique_ptr<NodeData> bias_add_data = nullptr;
100
101 switch (input_data->dtype())
102 {
103 case loco::DataType::FLOAT32:
104 {
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());
108
109 auto *shape = input_data->shape();
110
111 for (IndexEnumerator e{*shape}; e.valid(); e.advance())
112 {
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);
116 }
117
118 bias_add_data = make_data(bias_add_buf);
119 break;
120 }
121 default:
122 throw std::runtime_error("NYI for this DataType");
123 }
124 return bias_add_data;
125}
126
127} // namespace
128
129namespace locomotiv
130{
131
132void NodeExecution::execute(loco::BiasAdd<loco::Domain::Tensor> *bias_add)
133{
134 execute_node(bias_add);
135}
136
137void NodeExecution::execute(loco::BiasAdd<loco::Domain::Feature> *bias_add)
138{
139 execute_node(bias_add);
140}
141
142} // namespace locomotiv
Produce a value of domain D from an input value (of domain D) and a bias.
Definition Nodes.h:770
uint32_t & at(uint32_t axis)
Definition Index.cpp:49
uint32_t & dim(uint32_t axis)
Definition Shape.cpp:42
bool validate(Code *code)
list input_data
Definition infer.py:29
void validate(bool true_cond, const std::string &&exception_msg)
Definition Validation.h:26
void annot_domain(loco::Node *node, const loco::Domain &domain)
Wrapper to annotate domain to node. Cannot annotate unknown domain.
std::unique_ptr< NodeData > make_data(const NodeData::Buffer< DT > &buffer)
Copy buffer to make NodeData.
loco::GraphInputIndex index(const TFPlaceholder *node)
Definition TFNode.cpp:54
Buffer< T > make_buffer(const Shape &shape)
Definition Buffer.h:47
Read-only no-template wrapper for 'Buffer'. Serves interface for input and output of 'Session'.
Definition NodeData.h:36
virtual const Shape * shape() const =0
virtual const Buffer< float > * as_f32_bufptr() const =0