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_DENSE_UPDATE_FUNCTOR_H_ 17 #define TENSORFLOW_CORE_KERNELS_DENSE_UPDATE_FUNCTOR_H_ 18 19 #define EIGEN_USE_THREADS 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 25 namespace tensorflow { 26 27 typedef Eigen::ThreadPoolDevice CPUDevice; 28 typedef Eigen::GpuDevice GPUDevice; 29 30 31 enum DenseUpdateType { ADD, SUB, ASSIGN }; 32 33 namespace functor { 34 35 template <typename Device, typename T, DenseUpdateType OP> 36 struct DenseUpdate { 37 void operator()(const Device& d, typename TTypes<T>::Flat params, 38 typename TTypes<T>::ConstFlat update); 39 }; 40 41 template <typename T> 42 struct DenseUpdate<CPUDevice, T, ADD> { 43 void operator()(const CPUDevice& d, typename TTypes<T>::Flat params, 44 typename TTypes<T>::ConstFlat update) { 45 params.device(d) += update; 46 } 47 }; 48 49 template <typename T> 50 struct DenseUpdate<CPUDevice, T, SUB> { 51 void operator()(const CPUDevice& d, typename TTypes<T>::Flat params, 52 typename TTypes<T>::ConstFlat update) { 53 params.device(d) -= update; 54 } 55 }; 56 57 template <typename T> 58 struct DenseUpdate<CPUDevice, T, ASSIGN> { 59 void operator()(const CPUDevice& d, typename TTypes<T>::Flat params, 60 typename TTypes<T>::ConstFlat update) { 61 params.device(d) = update; 62 } 63 }; 64 65 66 } // end namespace functor 67 68 template <typename Device> 69 Status VariantCopyFn(OpKernelContext* context, const Tensor& from, Tensor* to); 70 71 template <> 72 Status VariantCopyFn<CPUDevice>(OpKernelContext* context, const Tensor& from, 73 Tensor* to); 74 template <> 75 Status VariantCopyFn<GPUDevice>(OpKernelContext* context, const Tensor& from, 76 Tensor* to); 77 78 } // end namespace tensorflow 79 80 #endif // TENSORFLOW_CORE_KERNELS_DENSE_UPDATE_FUNCTOR_H_ 81