Skip to content

Commit 59c32d6

Browse files
committed
add DataBound struct for reduce_max and reduce_min
1 parent 6174b50 commit 59c32d6

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

paddle/fluid/operators/reduce_ops/reduce_functor_op.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,45 @@ See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

1515
#pragma once
16+
#include <cuda.h>
17+
#include <float.h>
18+
#include <limits.h>
19+
#include <math.h>
20+
#include <cstdint>
1621

1722
namespace paddle {
1823
namespace operators {
1924

25+
template <typename T>
26+
struct DataBound {
27+
static inline T max() { return static_cast<T>(FLT_MAX); }
28+
static inline T min() { return static_cast<T>(-FLT_MAX); }
29+
};
30+
31+
template <>
32+
struct DataBound<float> {
33+
static inline float max() { return FLT_MAX; }
34+
static inline float min() { return -FLT_MAX; }
35+
};
36+
37+
template <>
38+
struct DataBound<double> {
39+
static inline double max() { return DBL_MAX; }
40+
static inline double min() { return -DBL_MAX; }
41+
};
42+
43+
template <>
44+
struct DataBound<int32_t> {
45+
static inline int32_t max() { return INT32_MAX; }
46+
static inline int32_t min() { return INT32_MIN; }
47+
};
48+
49+
template <>
50+
struct DataBound<int64_t> {
51+
static inline int64_t max() { return INT64_MAX; }
52+
static inline int64_t min() { return INT64_MIN; }
53+
};
54+
2055
template <typename T>
2156
struct CustomMin {
2257
__device__ T operator()(const T &a, const T &b) const {

paddle/fluid/operators/reduce_ops/reduce_max_op.cu

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class ReduceMaxKernel : public framework::OpKernel<T> {
4444

4545
auto stream = context.cuda_device_context().stream();
4646
TensorReduceFunc<T, T, CustomMax<T>, detail::IdentityFunctor<T>>(
47-
*input, output, reduce_dims, static_cast<T>(-FLT_MAX), CustomMax<T>(),
47+
*input, output, reduce_dims, DataBound<T>::min(), CustomMax<T>(),
4848
detail::IdentityFunctor<T>(), stream);
4949
}
5050
};

paddle/fluid/operators/reduce_ops/reduce_min_op.cu

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class ReduceMinKernel : public framework::OpKernel<T> {
4444

4545
auto stream = context.cuda_device_context().stream();
4646
TensorReduceFunc<T, T, CustomMin<T>, detail::IdentityFunctor<T>>(
47-
*input, output, reduce_dims, static_cast<T>(FLT_MAX), CustomMin<T>(),
47+
*input, output, reduce_dims, DataBound<T>::max(), CustomMin<T>(),
4848
detail::IdentityFunctor<T>(), stream);
4949
}
5050
};

0 commit comments

Comments
 (0)