1 | /* Copyright 2015 The TensorFlow Authors. All Rights Reserved. |
2 | |
3 | Licensed under the Apache License, Version 2.0 (the "License"); |
4 | you may not use this file except in compliance with the License. |
5 | You may obtain a copy of the License at |
6 | |
7 | http://www.apache.org/licenses/LICENSE-2.0 |
8 | |
9 | Unless required by applicable law or agreed to in writing, software |
10 | distributed under the License is distributed on an "AS IS" BASIS, |
11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | See the License for the specific language governing permissions and |
13 | limitations under the License. |
14 | ==============================================================================*/ |
15 | |
16 | #ifndef TENSORFLOW_CORE_KERNELS_BIAS_OP_H_ |
17 | #define TENSORFLOW_CORE_KERNELS_BIAS_OP_H_ |
18 | // Functor definition for BiasOp, must be compilable by nvcc. |
19 | |
20 | #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor" |
21 | #include "tensorflow/core/framework/tensor_types.h" |
22 | |
23 | namespace tensorflow { |
24 | namespace functor { |
25 | |
26 | // Functor used by BiasOp to do the computations. |
27 | template <typename Device, typename T> |
28 | struct Bias { |
29 | // Add "bias" to "input", repeating "bias". |
30 | void operator()(const Device& d, typename TTypes<T>::ConstFlat input, |
31 | typename TTypes<T>::ConstVec bias, |
32 | typename TTypes<T>::Flat output) { |
33 | const Eigen::Index rest_size = input.size() / bias.dimension(0); |
34 | Eigen::DSizes<Eigen::Index, 1> bcast(rest_size); |
35 | MaybeWith32BitIndexing<Device>( |
36 | [&](auto input32, auto bias32, auto output32, const auto& bcast32) { |
37 | output32.device(d) = input32 + bias32.broadcast(bcast32); |
38 | }, |
39 | input, bias, output, bcast); |
40 | } |
41 | |
42 | // NCHW layout, repeating on the first dimension, broadcasting on the last |
43 | // dimension. |
44 | void operator()(const Device& d, typename TTypes<T>::ConstMatrix input, |
45 | typename TTypes<T>::ConstMatrix bias1, // shape [C, 1]. |
46 | typename TTypes<T>::Matrix output) { |
47 | const Eigen::Index rest_size = input.dimension(0) / bias1.dimension(0); |
48 | Eigen::DSizes<Eigen::Index, 2> bcast(rest_size, input.dimension(1)); |
49 | MaybeWith32BitIndexing<Device>( |
50 | [&](auto input32, auto bias32, auto output32, const auto& bcast32) { |
51 | output32.device(d) = input32 + bias32.broadcast(bcast32); |
52 | }, |
53 | input, bias1, output, bcast); |
54 | } |
55 | }; |
56 | |
57 | } // namespace functor |
58 | } // namespace tensorflow |
59 | |
60 | #endif // TENSORFLOW_CORE_KERNELS_BIAS_OP_H_ |
61 | |