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_GPU_H_ 17 #define TENSORFLOW_CORE_KERNELS_BIAS_OP_GPU_H_ 18 19 #define EIGEN_USE_GPU 20 21 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor" 22 #include "tensorflow/core/framework/op_kernel.h" 23 #include "tensorflow/core/framework/tensor_types.h" 24 #include "tensorflow/core/kernels/gpu_utils.h" 25 #include "tensorflow/core/util/tensor_format.h" 26 27 namespace tensorflow { 28 29 typedef Eigen::GpuDevice GPUDevice; 30 31 template <typename T> 32 struct BiasGPU { 33 static void compute(const GPUDevice& d, const T* input, const T* bias, 34 T* output, int32 batch, int32 height, int32 width, 35 int32 depth, int32 channel, TensorFormat data_format); 36 }; 37 38 template <typename T> 39 struct BiasGradGPU { 40 static void compute(const GPUDevice& device, const T* output_backprop, 41 T* bias_backprop, int32 batch, int32 height, int32 width, 42 int32 depth, int32 channel, TensorFormat data_format); 43 44 static void DoRowReduction(OpKernelContext* context, T* output, 45 const T* input, int rows, int cols); 46 47 static void DoColReduction(OpKernelContext* context, T* output, 48 const T* input, int rows, int cols); 49 }; 50 51 enum class BiasAddGradGPUMode { 52 kInvalid = 0, 53 kNative = 1, 54 kReduction = 2, 55 }; 56 57 // Describe the BiasGradGPU result from a perf experiment. 58 // 59 // Arguments: 60 // algorithm: returns the method to use for bias add grad. 61 // elapsed_time; returns the measured elapsed time in microseconds. 62 class BiasGradGPUProfileResult { 63 public: is_valid()64 bool is_valid() const { 65 return (algorithm_ != BiasAddGradGPUMode::kInvalid && 66 elapsed_time_ != std::numeric_limits<float>::max()); 67 } algorithm()68 BiasAddGradGPUMode algorithm() const { return algorithm_; } set_algorithm(BiasAddGradGPUMode val)69 void set_algorithm(BiasAddGradGPUMode val) { algorithm_ = val; } elapsed_time()70 uint64 elapsed_time() const { return elapsed_time_; } set_elapsed_time(uint64 val)71 void set_elapsed_time(uint64 val) { elapsed_time_ = val; } 72 73 private: 74 BiasAddGradGPUMode algorithm_ = BiasAddGradGPUMode::kInvalid; 75 uint64 elapsed_time_ = std::numeric_limits<uint64>::max(); 76 }; 77 78 } // namespace tensorflow 79 80 #endif // TENSORFLOW_CORE_KERNELS_BIAS_OP_GPU_H_ 81