1 /* Copyright 2021 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 #if GOOGLE_CUDA || TENSORFLOW_USE_ROCM
17
18 #include "tensorflow/core/framework/register_types.h"
19 #include "tensorflow/core/kernels/segment_reduction_ops_gpu.cu.h"
20
21 namespace tensorflow {
22
DisableSegmentReductionOpDeterminismExceptions()23 bool DisableSegmentReductionOpDeterminismExceptions() {
24 static bool cached_disable = [] {
25 bool disable = false;
26 TF_CHECK_OK(tensorflow::ReadBoolFromEnvVar(
27 "TF_DISABLE_SEGMENT_REDUCTION_OP_DETERMINISM_EXCEPTIONS",
28 /*default_val=*/false, &disable));
29 return disable;
30 }();
31 return cached_disable;
32 }
33
34 namespace functor {
35
36 #define DEFINE_SORTED_GPU_SPECS_INDEX(T, Index) \
37 template struct SegmentReductionFunctor<T, Index, functor::Zero<T>, \
38 functor::NonAtomicSumOpGpu<T>, \
39 functor::AtomicSumOpGpu<T>>; \
40 template struct SegmentReductionFunctor<T, Index, functor::One<T>, \
41 functor::NonAtomicProdOpGpu<T>, \
42 functor::AtomicProdOpGpu<T>>; \
43 template struct SegmentReductionFunctor<T, Index, functor::Highest<T>, \
44 functor::NonAtomicMinOpGpu<T>, \
45 functor::AtomicMinOpGpu<T>>; \
46 template struct SegmentReductionFunctor<T, Index, functor::Lowest<T>, \
47 functor::NonAtomicMaxOpGpu<T>, \
48 functor::AtomicMaxOpGpu<T>>;
49
50 #define DEFINE_SORTED_GPU_SPECS(T) DEFINE_SORTED_GPU_SPECS_INDEX(T, int32);
51
52 TF_CALL_GPU_NUMBER_TYPES(DEFINE_SORTED_GPU_SPECS);
53
54 #define DEFINE_REAL_UNSORTED_GPU_SPECS_INDEX(T, Index) \
55 template struct UnsortedSegmentFunctor< \
56 GPUDevice, T, Index, functor::Lowest<T>, functor::AtomicMaxOpGpu<T>>; \
57 template struct UnsortedSegmentFunctor< \
58 GPUDevice, T, Index, functor::Highest<T>, functor::AtomicMinOpGpu<T>>; \
59 template struct UnsortedSegmentFunctor<GPUDevice, T, Index, functor::One<T>, \
60 functor::AtomicProdOpGpu<T>>;
61
62 // Sum is the only op that supports all input types currently.
63 #define DEFINE_SUM_UNSORTED_GPU_SPECS_INDEX(T, Index) \
64 template struct UnsortedSegmentFunctor< \
65 GPUDevice, T, Index, functor::Zero<T>, functor::AtomicSumOpGpu<T>>;
66
67 #define DEFINE_REAL_GPU_SPECS(T) DEFINE_REAL_UNSORTED_GPU_SPECS_INDEX(T, int32);
68
69 #define DEFINE_SUM_GPU_SPECS(T) DEFINE_SUM_UNSORTED_GPU_SPECS_INDEX(T, int32);
70
71 TF_CALL_GPU_NUMBER_TYPES(DEFINE_REAL_GPU_SPECS);
72 TF_CALL_GPU_NUMBER_TYPES(DEFINE_SUM_GPU_SPECS);
73
74 #undef DEFINE_SORTED_GPU_SPECS_INDEX
75 #undef DEFINE_SORTED_GPU_SPECS
76 #undef DEFINE_REAL_UNSORTED_GPU_SPECS_INDEX
77 #undef DEFINE_SUM_UNSORTED_GPU_SPECS_INDEX
78 #undef DEFINE_REAL_GPU_SPECS
79 #undef DEFINE_SUM_GPU_SPECS
80
81 // TODO(benbarsdell): These kernels are disabled on Windows as a workaround for
82 // a CI build error: "formal parameter with requested alignment of 128 won't be
83 // aligned". The root cause is suspected to be an aligned type (AlignedVector)
84 // being passed to a function by value, possibly inside the CUB library
85 // somewhere, but I have not yet been able to reproduce it in isolation outside
86 // of the GitHub CI.
87 #if !defined(PLATFORM_WINDOWS)
88
89 #define DEFINE_SPARSE_SEGMENT_REDUCTION_FUNCTOR(T) \
90 template struct SparseSegmentReductionFunctor<T, int32, int32>; \
91 template struct SparseSegmentReductionFunctor<T, int32, int64>;
92 TF_CALL_GPU_NUMBER_TYPES(DEFINE_SPARSE_SEGMENT_REDUCTION_FUNCTOR);
93 #undef DEFINE_SPARSE_SEGMENT_REDUCTION_FUNCTOR
94
95 #define DEFINE_SPARSE_SEGMENT_GRAD_FUNCTOR(T) \
96 template struct SparseSegmentGradFunctor<GPUDevice, T, int32, int32>; \
97 template struct SparseSegmentGradFunctor<GPUDevice, T, int32, int64>;
98 TF_CALL_GPU_NUMBER_TYPES(DEFINE_SPARSE_SEGMENT_GRAD_FUNCTOR);
99 #undef DEFINE_SPARSE_SEGMENT_GRAD_FUNCTOR
100
101 #endif // !defined(PLATFORM_WINDOWS)
102
103 } // namespace functor
104 } // namespace tensorflow
105
106 #endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
107