Skip to content

Commit 89e4127

Browse files
committed
remove all useless template
1 parent a791464 commit 89e4127

File tree

6 files changed

+21
-23
lines changed

6 files changed

+21
-23
lines changed

paddle/fluid/operators/kron_op.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@ struct KronGradElemFunctor<platform::complex<T>> {
237237
const int ndims_;
238238
};
239239

240-
template <typename T>
241240
struct IdentityFunctor {
242241
HOSTDEVICE explicit inline IdentityFunctor() {}
243242

@@ -315,13 +314,13 @@ struct KronGradOpFunctor {
315314
#if defined(__NVCC__) || defined(__HIPCC__)
316315
auto stream = dev_ctx.stream(); // it is a cuda device_context
317316
if (dx) {
318-
TensorReduce<T, T, cub::Sum, IdentityFunctor<T>>(
319-
dout_x, dx, {1}, static_cast<T>(0), cub::Sum(), IdentityFunctor<T>(),
317+
TensorReduce<T, T, cub::Sum, IdentityFunctor>(
318+
dout_x, dx, {1}, static_cast<T>(0), cub::Sum(), IdentityFunctor(),
320319
stream);
321320
}
322321
if (dy) {
323-
TensorReduce<T, T, cub::Sum, IdentityFunctor<T>>(
324-
dout_y, dy, {1}, static_cast<T>(0), cub::Sum(), IdentityFunctor<T>(),
322+
TensorReduce<T, T, cub::Sum, IdentityFunctor>(
323+
dout_y, dy, {1}, static_cast<T>(0), cub::Sum(), IdentityFunctor(),
325324
stream);
326325
}
327326
#else

paddle/fluid/operators/matmul_v2_op.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ namespace operators {
3434

3535
using framework::Tensor;
3636

37-
template <typename T>
3837
struct IdentityFunctor {
3938
HOSTDEVICE explicit inline IdentityFunctor() {}
4039

@@ -50,9 +49,9 @@ void ReduceSumForMatmulGrad(const Tensor* input, Tensor* output,
5049
const paddle::framework::ExecutionContext& ctx) {
5150
#if defined(__NVCC__) || defined(__HIPCC__)
5251
auto stream = ctx.cuda_device_context().stream();
53-
TensorReduce<T, T, cub::Sum, IdentityFunctor<T>>(
54-
*input, output, reduce_dims, static_cast<T>(0), cub::Sum(),
55-
IdentityFunctor<T>(), stream);
52+
TensorReduce<T, T, cub::Sum, IdentityFunctor>(*input, output, reduce_dims,
53+
static_cast<T>(0), cub::Sum(),
54+
IdentityFunctor(), stream);
5655
#else
5756
ReduceKernelFunctor<DeviceContext, T, ops::SumFunctor>(
5857
input, output, reduce_dims, true, false, ctx)

paddle/fluid/operators/reduce_ops/cub_reduce.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,7 @@ void TensorReduce(const framework::Tensor& x, framework::Tensor* y,
436436
#undef CUB_BLOCK_DIM_CASE
437437
}
438438

439-
template <typename Tx, typename ReduceOp,
440-
template <typename, typename> class TransformOp>
439+
template <typename Tx, typename ReduceOp, template <typename> class TransformOp>
441440
struct TensorReduceFunctor {
442441
const framework::Tensor& x;
443442
framework::Tensor* y;
@@ -459,9 +458,9 @@ struct TensorReduceFunctor {
459458

460459
void apply() const {
461460
const Ty& init_cast = static_cast<Ty>(init);
462-
TensorReduce<Tx, Ty, ReduceOp, TransformOp<Tx, Ty>>(
463-
x, y, origin_reduce_dims, init_cast, reducer, TransformOp<Tx, Ty>(),
464-
stream);
461+
TensorReduce<Tx, Ty, ReduceOp, TransformOp<Ty>>(x, y, origin_reduce_dims,
462+
init_cast, reducer,
463+
TransformOp<Ty>(), stream);
465464
}
466465
};
467466

paddle/fluid/operators/reduce_ops/reduce_sum_op.cu

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
namespace paddle {
1919
namespace operators {
2020

21-
template <typename Tx, typename Ty = Tx>
21+
template <typename Tout>
2222
struct IdentityFunctor {
2323
HOSTDEVICE explicit inline IdentityFunctor() {}
2424

2525
template <typename U>
26-
HOSTDEVICE inline Ty operator()(const U& x) const {
27-
return static_cast<Ty>(x);
26+
HOSTDEVICE inline Tout operator()(const U& x) const {
27+
return static_cast<Tout>(x);
2828
}
2929
};
3030

@@ -63,9 +63,9 @@ class ReduceSumKernel : public framework::OpKernel<T> {
6363
*input, output, reduce_dims, static_cast<double>(0.0), cub::Sum(),
6464
stream));
6565
} else {
66-
TensorReduce<T, T, cub::Sum, IdentityFunctor<T, T>>(
66+
TensorReduce<T, T, cub::Sum, IdentityFunctor<T>>(
6767
*input, output, reduce_dims, static_cast<T>(0), cub::Sum(),
68-
IdentityFunctor<T, T>(), stream);
68+
IdentityFunctor<T>(), stream);
6969
}
7070
}
7171
};

paddle/fluid/operators/trace_op.cu

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
namespace paddle {
2121
namespace operators {
2222

23-
template <typename T>
2423
struct IdentityFunctor {
2524
HOSTDEVICE explicit inline IdentityFunctor() {}
2625

@@ -48,9 +47,9 @@ class TraceCUDAKernel : public framework::OpKernel<T> {
4847
auto stream = context.cuda_device_context().stream();
4948
std::vector<int> reduce_dims;
5049
reduce_dims.push_back(out->dims().size());
51-
TensorReduce<T, T, cub::Sum, IdentityFunctor<T>>(
50+
TensorReduce<T, T, cub::Sum, IdentityFunctor>(
5251
diag, out, reduce_dims, static_cast<T>(0), cub::Sum(),
53-
IdentityFunctor<T>(), stream);
52+
IdentityFunctor(), stream);
5453
}
5554
}
5655
};

python/paddle/fluid/tests/unittests/test_reduce_op.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def setUp(self):
5050
self.gradient = self.calc_gradient()
5151

5252
def test_check_output(self):
53-
self.check_output(atol=1e-2)
53+
self.check_output()
5454

5555
def calc_gradient(self):
5656
x = self.inputs["X"]
@@ -65,6 +65,8 @@ class TestSumOp_fp16_withInt(OpTest):
6565
def setUp(self):
6666
self.op_type = "reduce_sum"
6767
self.inputs = {
68+
# ref to https://en.wikipedia.org/wiki/Half-precision_floating-point_format
69+
# Precision limitations on integer values between 0 and 2048 can be exactly represented
6870
'X': np.random.randint(0, 30, (10, 10)).astype("float16")
6971
}
7072
self.attrs = {'dim': [0, 1]}

0 commit comments

Comments
 (0)