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#include "tensorflow/core/kernels/fill_functor.h"
17
18#define EIGEN_USE_THREADS
19
20#include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
21#include "tensorflow/core/framework/register_types.h"
22#include "tensorflow/core/framework/tensor_types.h"
23#include "tensorflow/core/framework/types.h"
24#include "tensorflow/core/framework/variant_encode_decode.h"
25
26namespace tensorflow {
27namespace functor {
28
29template <typename T>
30void SetZeroFunctor<Eigen::ThreadPoolDevice, T>::operator()(
31 const Eigen::ThreadPoolDevice& d, typename TTypes<T>::Flat out) {
32 out.device(d) = out.constant(T(0));
33}
34
35void SetZeroFunctor<Eigen::ThreadPoolDevice, tstring>::operator()(
36 const Eigen::ThreadPoolDevice& d, typename TTypes<tstring>::Flat out) {
37 out.device(d) = out.constant(tstring());
38}
39
40// Explicit instantiations.
41#define DEFINE_SETZERO_CPU(T) \
42 template struct SetZeroFunctor<Eigen::ThreadPoolDevice, T>;
43DEFINE_SETZERO_CPU(bool);
44DEFINE_SETZERO_CPU(Eigen::half);
45DEFINE_SETZERO_CPU(bfloat16);
46DEFINE_SETZERO_CPU(float);
47DEFINE_SETZERO_CPU(double);
48DEFINE_SETZERO_CPU(uint32);
49DEFINE_SETZERO_CPU(uint64);
50DEFINE_SETZERO_CPU(uint8);
51DEFINE_SETZERO_CPU(int8);
52DEFINE_SETZERO_CPU(uint16);
53DEFINE_SETZERO_CPU(int16);
54DEFINE_SETZERO_CPU(int32);
55DEFINE_SETZERO_CPU(int64_t);
56DEFINE_SETZERO_CPU(quint8);
57DEFINE_SETZERO_CPU(qint8);
58DEFINE_SETZERO_CPU(quint16);
59DEFINE_SETZERO_CPU(qint16);
60DEFINE_SETZERO_CPU(qint32);
61DEFINE_SETZERO_CPU(complex64);
62DEFINE_SETZERO_CPU(complex128);
63DEFINE_SETZERO_CPU(Variant);
64#undef DEFINE_SETZERO_CPU
65
66
67template <typename T>
68void SetOneFunctor<Eigen::ThreadPoolDevice, T>::operator()(
69 const Eigen::ThreadPoolDevice& d, typename TTypes<T>::Flat out) {
70 out.device(d) = out.constant(T(1));
71}
72
73// Explicit instantiations.
74#define DEFINE_SETONE_CPU(T) \
75 template struct SetOneFunctor<Eigen::ThreadPoolDevice, T>;
76DEFINE_SETONE_CPU(bool);
77DEFINE_SETONE_CPU(Eigen::half);
78DEFINE_SETONE_CPU(bfloat16);
79DEFINE_SETONE_CPU(float);
80DEFINE_SETONE_CPU(double);
81DEFINE_SETONE_CPU(uint32);
82DEFINE_SETONE_CPU(uint64);
83DEFINE_SETONE_CPU(uint8);
84DEFINE_SETONE_CPU(int8);
85DEFINE_SETONE_CPU(uint16);
86DEFINE_SETONE_CPU(int16);
87DEFINE_SETONE_CPU(int32);
88DEFINE_SETONE_CPU(int64_t);
89DEFINE_SETONE_CPU(complex64);
90DEFINE_SETONE_CPU(complex128);
91#undef DEFINE_SETONE_CPU
92
93template <typename T>
94void SetNanFunctor<Eigen::ThreadPoolDevice, T>::operator()(
95 const Eigen::ThreadPoolDevice& d, typename TTypes<T>::Flat out) {
96 out.device(d) = out.constant(Eigen::NumTraits<T>::quiet_NaN());
97}
98
99// Explicit instantiations.
100#define DEFINE_SETNAN_CPU(T) \
101 template struct SetNanFunctor<Eigen::ThreadPoolDevice, T>;
102TF_CALL_NUMBER_TYPES(DEFINE_SETNAN_CPU);
103TF_CALL_bool(DEFINE_SETNAN_CPU);
104#undef DEFINE_SETNAN_CPU
105
106template <typename T>
107struct FillFunctor<Eigen::ThreadPoolDevice, T> {
108 void operator()(const Eigen::ThreadPoolDevice& d,
109 typename TTypes<T>::Flat out,
110 typename TTypes<T>::ConstScalar in) {
111 out.device(d) = out.constant(in());
112 }
113};
114
115// Explicit instantiations.
116#define DEFINE_FILL_CPU(T) \
117 template struct FillFunctor<Eigen::ThreadPoolDevice, T>;
118
119TF_CALL_ALL_TYPES(DEFINE_FILL_CPU);
120DEFINE_FILL_CPU(quint8);
121DEFINE_FILL_CPU(quint16);
122DEFINE_FILL_CPU(qint8);
123DEFINE_FILL_CPU(qint16);
124DEFINE_FILL_CPU(qint32);
125#undef DEFINE_FILL_CPU
126
127
128} // namespace functor
129} // namespace tensorflow
130