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, int Dims> 28 struct Bias { 29 // Add "bias" to "input", broadcasting it on all dimensions but the last one. operatorBias30 void operator()(const Device& d, typename TTypes<T, Dims>::ConstTensor input, 31 typename TTypes<T>::ConstVec bias, 32 typename TTypes<T, Dims>::Tensor output) { 33 if (input.size() >= INT_MAX) { 34 const int64_t bias_size = bias.dimension(0); 35 const int64_t rest_size = input.size() / bias_size; 36 Eigen::DSizes<int64_t, 1> one_d(input.size()); 37 Eigen::DSizes<int64_t, 1> bcast(rest_size); 38 output.reshape(one_d).device(d) = 39 input.reshape(one_d) + bias.broadcast(bcast).reshape(one_d); 40 } else { 41 const int bias_size = bias.dimension(0); 42 const int rest_size = input.size() / bias_size; 43 Eigen::DSizes<int, 1> one_d(input.size()); 44 Eigen::DSizes<int, 1> bcast(rest_size); 45 To32Bit(output).reshape(one_d).device(d) = 46 To32Bit(input).reshape(one_d) + 47 To32Bit(bias).broadcast(bcast).reshape(one_d); 48 } 49 } 50 }; 51 52 } // namespace functor 53 } // namespace tensorflow 54 55 #endif // TENSORFLOW_CORE_KERNELS_BIAS_OP_H_ 56