Home | History | Annotate | Download | only in kernels
      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_KERNELS_DENSE_UPDATE_FUNCTOR_H_
     17 #define TENSORFLOW_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/tensor_types.h"
     23 
     24 namespace tensorflow {
     25 
     26 typedef Eigen::ThreadPoolDevice CPUDevice;
     27 #ifdef TENSORFLOW_USE_SYCL
     28 typedef Eigen::SyclDevice SYCLDevice;
     29 #endif  // TENSORFLOW_USE_SYCL
     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 #ifdef TENSORFLOW_USE_SYCL
     66 template <typename T>
     67 struct DenseUpdate<SYCLDevice, T, ADD> {
     68   void operator()(const SYCLDevice& d, typename TTypes<T>::Flat params,
     69                   typename TTypes<T>::ConstFlat update) {
     70     params.device(d) += update;
     71   }
     72 };
     73 
     74 template <typename T>
     75 struct DenseUpdate<SYCLDevice, T, SUB> {
     76   void operator()(const SYCLDevice& d, typename TTypes<T>::Flat params,
     77                   typename TTypes<T>::ConstFlat update) {
     78     params.device(d) -= update;
     79   }
     80 };
     81 
     82 template <typename T>
     83 struct DenseUpdate<SYCLDevice, T, ASSIGN> {
     84   void operator()(const SYCLDevice& d, typename TTypes<T>::Flat params,
     85                   typename TTypes<T>::ConstFlat update) {
     86     params.device(d) = update;
     87   }
     88 };
     89 #endif  // TENSORFLOW_USE_SYCL
     90 
     91 }  // end namespace functor
     92 }  // end namespace tensorflow
     93 
     94 #endif  // TENSORFLOW_KERNELS_DENSE_UPDATE_FUNCTOR_H_
     95