ONE - On-device Neural Engine
Loading...
Searching...
No Matches
bias_op.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2015 The TensorFlow Authors. All Rights Reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef __NNFW_CKER_EIGEN_BIAS_OP_H__
19#define __NNFW_CKER_EIGEN_BIAS_OP_H__
20
21// From tensorflow/core/kernels/bias_op.cc
22#define EIGEN_USE_THREADS
23
24#include "unsupported/Eigen/CXX11/Tensor"
26
27// From tensorflow/core/kernels/bias_op.h
28namespace nnfw
29{
30namespace cker
31{
32namespace bias_op
33{
34
35namespace functor
36{
37
38namespace internal
39{
40
41template <typename Device> struct MaybeWith32BitIndexingImpl
42{
43 template <typename Func, typename... Args> void operator()(Func func, Args &&...args) const
44 {
45 func(std::forward<Args>(args)...);
46 }
47};
48
49} // namespace internal
50
51template <typename Device, typename Func, typename... Args>
52void MaybeWith32BitIndexing(Func func, Args &&...args)
53{
54 return internal::MaybeWith32BitIndexingImpl<Device>()(func, std::forward<Args>(args)...);
55}
56
57// Functor used by BiasOp to do the computations.
58// NOTE Apply activation to Bias
59template <typename Device, typename T> struct Bias
60{
61 // Add "bias" to "input", repeating "bias".
62 void operator()(const Device &d, typename TTypes<T>::ConstFlat input,
63 typename TTypes<T>::ConstVec bias, typename TTypes<T>::Flat output,
64 T activation_min, T activation_max)
65 {
66 const Eigen::Index rest_size = input.size() / bias.dimension(0);
67 Eigen::DSizes<Eigen::Index, 1> bcast(rest_size);
68 MaybeWith32BitIndexing<Device>(
69 [&](auto input32, auto bias32, typename TTypes<T>::Flat output32, const auto &bcast32,
70 T activation_min, T activation_max) {
71 output32.device(d) =
72 (input32 + bias32.broadcast(bcast32))
73 .template cwiseMax<Eigen::PropagateNaN>(static_cast<T>(activation_min))
74 .template cwiseMin<Eigen::PropagateNaN>(static_cast<T>(activation_max));
75 },
76 input, bias, output, bcast, activation_min, activation_max);
77 }
78};
79
80} // namespace functor
81} // namespace bias_op
82} // namespace cker
83} // namespace nnfw
84
85// From tensorflow/core/kernels/bias_op.cc
86namespace nnfw
87{
88namespace cker
89{
90namespace bias_op
91{
92
93// Enable CPUDevice only for depthwise_conv_op
94using Device = Eigen::ThreadPoolDevice;
95
96template <typename T>
97void biasHelper(const Shape &bias_shape, const T *bias_data, const Shape &input_shape,
98 T *input_data, T activation_min, T activation_max)
99{
100 [[maybe_unused]] int channel_dim = input_shape.DimensionsCount() - 1;
101
102 assert(input_shape.Dims(channel_dim) == bias_shape.Dims(0));
103 assert(input_data);
104 assert(bias_data);
105
106 Tensor bias{bias_shape, const_cast<T *>(bias_data)};
107 Tensor input{input_shape, input_data};
108
110 const Eigen::ThreadPoolDevice &d = *eigen_support::GetThreadPoolDevice();
111 functor(d, static_cast<const Tensor &>(input).flat<T>(),
112 static_cast<const Tensor &>(bias).flat<T>(), input.flat<T>(), activation_min,
113 activation_max);
114}
115
116} // namespace bias_op
117} // namespace cker
118} // namespace nnfw
119#endif // __NNFW_CKER_EIGEN_BIAS_OP_H__
int32_t DimensionsCount() const
Definition Shape.h:91
int32_t Dims(int i) const
Definition Shape.h:92
void MaybeWith32BitIndexing(Func func, Args &&...args)
Definition bias_op.h:52
void biasHelper(const Shape &bias_shape, const T *bias_data, const Shape &input_shape, T *input_data, T activation_min, T activation_max)
Definition bias_op.h:97
Eigen::ThreadPoolDevice Device
Definition bias_op.h:94
const Eigen::ThreadPoolDevice * GetThreadPoolDevice()
Definition topk_v2.h:30
Eigen::TensorMap< Eigen::Tensor< const T, 1, Eigen::RowMajor, IndexType >, Eigen::Aligned > ConstFlat
Definition Tensor.h:63
Eigen::TensorMap< Eigen::Tensor< T, 1, Eigen::RowMajor, IndexType >, Eigen::Aligned > Flat
Definition Tensor.h:61
Eigen::TensorMap< Eigen::Tensor< const T, 1, Eigen::RowMajor, IndexType >, Eigen::Aligned > ConstVec
Definition Tensor.h:66
void operator()(const Device &d, typename TTypes< T >::ConstFlat input, typename TTypes< T >::ConstVec bias, typename TTypes< T >::Flat output, T activation_min, T activation_max)
Definition bias_op.h:62