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 #ifdef TENSORFLOW_USE_SYCL 31 typedef Eigen::SyclDevice SYCLDevice; 32 #endif // TENSORFLOW_USE_SYCL 33 34 enum DenseUpdateType { ADD, SUB, ASSIGN }; 35 36 namespace functor { 37 38 template <typename Device, typename T, DenseUpdateType OP> 39 struct DenseUpdate { 40 void operator()(const Device& d, typename TTypes<T>::Flat params, 41 typename TTypes<T>::ConstFlat update); 42 }; 43 44 template <typename T> 45 struct DenseUpdate<CPUDevice, T, ADD> { 46 void operator()(const CPUDevice& d, typename TTypes<T>::Flat params, 47 typename TTypes<T>::ConstFlat update) { 48 params.device(d) += update; 49 } 50 }; 51 52 template <typename T> 53 struct DenseUpdate<CPUDevice, T, SUB> { 54 void operator()(const CPUDevice& d, typename TTypes<T>::Flat params, 55 typename TTypes<T>::ConstFlat update) { 56 params.device(d) -= update; 57 } 58 }; 59 60 template <typename T> 61 struct DenseUpdate<CPUDevice, T, ASSIGN> { 62 void operator()(const CPUDevice& d, typename TTypes<T>::Flat params, 63 typename TTypes<T>::ConstFlat update) { 64 params.device(d) = update; 65 } 66 }; 67 68 #ifdef TENSORFLOW_USE_SYCL 69 template <typename T> 70 struct DenseUpdate<SYCLDevice, T, ADD> { 71 void operator()(const SYCLDevice& d, typename TTypes<T>::Flat params, 72 typename TTypes<T>::ConstFlat update) { 73 params.device(d) += update; 74 } 75 }; 76 77 template <typename T> 78 struct DenseUpdate<SYCLDevice, T, SUB> { 79 void operator()(const SYCLDevice& d, typename TTypes<T>::Flat params, 80 typename TTypes<T>::ConstFlat update) { 81 params.device(d) -= update; 82 } 83 }; 84 85 template <typename T> 86 struct DenseUpdate<SYCLDevice, T, ASSIGN> { 87 void operator()(const SYCLDevice& d, typename TTypes<T>::Flat params, 88 typename TTypes<T>::ConstFlat update) { 89 params.device(d) = update; 90 } 91 }; 92 #endif // TENSORFLOW_USE_SYCL 93 94 } // end namespace functor 95 96 template <typename Device> 97 Status VariantCopyFn(OpKernelContext* context, const Tensor& from, Tensor* to); 98 99 template <> 100 Status VariantCopyFn<CPUDevice>(OpKernelContext* context, const Tensor& from, 101 Tensor* to); 102 template <> 103 Status VariantCopyFn<GPUDevice>(OpKernelContext* context, const Tensor& from, 104 Tensor* to); 105 106 } // end namespace tensorflow 107 108 #endif // TENSORFLOW_CORE_KERNELS_DENSE_UPDATE_FUNCTOR_H_ 109