1/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations 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
25namespace tensorflow {
26
27typedef Eigen::ThreadPoolDevice CPUDevice;
28typedef Eigen::GpuDevice GPUDevice;
29
30
31enum DenseUpdateType { ADD, SUB, ASSIGN };
32
33namespace functor {
34
35template <typename Device, typename T, DenseUpdateType OP>
36struct DenseUpdate {
37 void operator()(const Device& d, typename TTypes<T>::Flat params,
38 typename TTypes<T>::ConstFlat update);
39};
40
41template <typename T>
42struct 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
49template <typename T>
50struct 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
57template <typename T>
58struct 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
68template <typename Device>
69Status VariantCopyFn(OpKernelContext* context, const Tensor& from, Tensor* to);
70
71template <>
72Status VariantCopyFn<CPUDevice>(OpKernelContext* context, const Tensor& from,
73 Tensor* to);
74template <>
75Status 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