-
Notifications
You must be signed in to change notification settings - Fork 74k
/
cwise_op_gpu_select.cu.cc
151 lines (131 loc) · 5.78 KB
/
cwise_op_gpu_select.cu.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#if GOOGLE_CUDA || TENSORFLOW_USE_ROCM
#define EIGEN_USE_GPU
#include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
#include "tensorflow/core/kernels/cwise_ops_gpu_common.cu.h"
namespace tensorflow {
namespace functor {
// Used by bfloat16 even when MLIR_GENERATED_GPU_KERNELS_ENABLED are enabled
// #if !defined(MLIR_GENERATED_GPU_KERNELS_ENABLED)
template <typename T, int NDIMS>
struct BCastSelectFunctor<GPUDevice, T, NDIMS> {
void operator()(const GPUDevice& d,
typename TTypes<T, NDIMS>::Tensor output_tensor,
typename TTypes<bool, NDIMS>::ConstTensor cond_tensor,
typename TTypes<T, NDIMS>::ConstTensor then_tensor,
typename TTypes<T, NDIMS>::ConstTensor else_tensor,
typename Eigen::array<Eigen::DenseIndex, NDIMS> cond_bcast,
typename Eigen::array<Eigen::DenseIndex, NDIMS> then_bcast,
typename Eigen::array<Eigen::DenseIndex, NDIMS> else_bcast) {
output_tensor.device(d) = cond_tensor.broadcast(cond_bcast)
.select(then_tensor.broadcast(then_bcast),
else_tensor.broadcast(else_bcast));
}
};
// #endif
template <typename T>
struct SelectFunctor<GPUDevice, T> {
void operator()(const GPUDevice& d, typename TTypes<T>::Flat out,
typename TTypes<bool>::ConstFlat cond_flat,
typename TTypes<T>::ConstFlat then_flat,
typename TTypes<T>::ConstFlat else_flat) {
MaybeWith32BitIndexing<GPUDevice>(
[&](auto out32, auto cond_flat32, auto then_flat32, auto else_flat32) {
out32.device(d) = cond_flat32.select(then_flat32, else_flat32);
},
out, cond_flat, then_flat, else_flat);
}
};
template <typename T>
struct SelectScalarFunctor<GPUDevice, T> {
void operator()(const GPUDevice& d, typename TTypes<T>::Flat out,
typename TTypes<bool>::ConstScalar cond,
typename TTypes<T>::ConstFlat then_flat,
typename TTypes<T>::ConstFlat else_flat) {
Eigen::IndexList<Eigen::type2index<1> > rank1;
const int size = then_flat.dimension(0);
Eigen::array<int, 1> broadcast_dims{size};
MaybeWith32BitIndexing<GPUDevice>(
[&](auto out32) {
out32.device(d) = cond.reshape(rank1)
.broadcast(broadcast_dims)
.select(then_flat, else_flat);
},
out);
}
};
template <typename T>
struct BatchSelectFunctor<GPUDevice, T> {
void operator()(const GPUDevice& d,
typename TTypes<T>::Matrix output_flat_outer_dims,
TTypes<bool>::ConstVec cond_vec,
typename TTypes<T>::ConstMatrix then_flat_outer_dims,
typename TTypes<T>::ConstMatrix else_flat_outer_dims) {
const int batch = cond_vec.size();
const int all_but_batch = then_flat_outer_dims.dimension(1);
Eigen::IndexList<Eigen::type2index<1>, int> broadcast_dims;
broadcast_dims.set(1, all_but_batch);
Eigen::IndexList<int, Eigen::type2index<1> > reshape_dims;
reshape_dims.set(0, batch);
// TODO(ebrevdo): Figure out why this leads to erroneous memory access.
//
// To32Bit(output_flat_outer_dims).device(d) =
// To32Bit(cond_vec)
// .reshape(reshape_dims)
// .broadcast(broadcast_dims)
// .select(To32Bit(then_flat_outer_dims),
// To32Bit(else_flat_outer_dims));
output_flat_outer_dims.device(d) =
cond_vec.reshape(reshape_dims)
.broadcast(broadcast_dims)
.select(then_flat_outer_dims, else_flat_outer_dims);
}
};
#define SELECT_FUNCTOR(T) \
template struct SelectFunctor<GPUDevice, T>; \
template struct SelectScalarFunctor<GPUDevice, T>; \
template struct BatchSelectFunctor<GPUDevice, T>;
#define SELECT_AND_BCAST_SELECT_FUNCTOR(T) \
template struct BCastSelectFunctor<GPUDevice, T, 1>; \
template struct BCastSelectFunctor<GPUDevice, T, 2>; \
template struct BCastSelectFunctor<GPUDevice, T, 3>; \
template struct BCastSelectFunctor<GPUDevice, T, 4>; \
template struct BCastSelectFunctor<GPUDevice, T, 5>; \
template struct BCastSelectFunctor<GPUDevice, T, 6>; \
template struct BCastSelectFunctor<GPUDevice, T, 7>; \
template struct BCastSelectFunctor<GPUDevice, T, 8>; \
SELECT_FUNCTOR(T)
#if defined(MLIR_GENERATED_GPU_KERNELS_ENABLED)
SELECT_FUNCTOR(bool);
SELECT_FUNCTOR(Eigen::half);
SELECT_FUNCTOR(float);
SELECT_FUNCTOR(double);
SELECT_FUNCTOR(int32);
SELECT_FUNCTOR(int64);
SELECT_FUNCTOR(complex64);
SELECT_FUNCTOR(complex128);
#else
SELECT_AND_BCAST_SELECT_FUNCTOR(bool);
SELECT_AND_BCAST_SELECT_FUNCTOR(Eigen::half);
SELECT_AND_BCAST_SELECT_FUNCTOR(float);
SELECT_AND_BCAST_SELECT_FUNCTOR(double);
SELECT_AND_BCAST_SELECT_FUNCTOR(int32);
SELECT_AND_BCAST_SELECT_FUNCTOR(int64);
SELECT_AND_BCAST_SELECT_FUNCTOR(complex64);
SELECT_AND_BCAST_SELECT_FUNCTOR(complex128);
#endif
SELECT_AND_BCAST_SELECT_FUNCTOR(bfloat16);
#undef SELECT_FUNCTOR
} // namespace functor
} // namespace tensorflow
#endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM