提交 c8d87719 编写于 作者: G guosheng

Revise the reduce_op unit test accordingly

上级 3994e91a
...@@ -30,12 +30,14 @@ class ReduceOp : public framework::OperatorWithKernel { ...@@ -30,12 +30,14 @@ class ReduceOp : public framework::OperatorWithKernel {
auto x_dims = ctx.Input<Tensor>("X")->dims(); auto x_dims = ctx.Input<Tensor>("X")->dims();
auto x_rank = x_dims.size(); auto x_rank = x_dims.size();
PADDLE_ENFORCE_LE(x_rank, 6, "Tensors with rank at most 6 are supported"); PADDLE_ENFORCE_LE(x_rank, 6, "Tensors with rank at most 6 are supported");
int dim = static_cast<int>(ctx.Attr<int>("dim")); int dim = ctx.Attr<int>("dim");
if (dim < 0) dim = x_rank + dim; if (dim < 0) dim = x_rank + dim;
PADDLE_ENFORCE_LT( PADDLE_ENFORCE_LT(
dim, x_rank, dim, x_rank,
"The dim should be in the range [-rank(input), rank(input)]"); "The dim should be in the range [-rank(input), rank(input))");
bool keep_dim = true; // TODO; PADDLE_ENFORCE_GE(ctx.Attr<int>("keep_dim"), 0, "keep_dim must be 0 or 1");
PADDLE_ENFORCE_LE(ctx.Attr<int>("keep_dim"), 1, "keep_dim must be 0 or 1");
bool keep_dim = ctx.Attr<int>("keep_dim") == 1;
auto dims_vector = vectorize(x_dims); auto dims_vector = vectorize(x_dims);
if (keep_dim || x_rank == 1) { if (keep_dim || x_rank == 1) {
dims_vector[dim] = 1; dims_vector[dim] = 1;
...@@ -59,11 +61,11 @@ class ReduceGradOp : public framework::OperatorWithKernel { ...@@ -59,11 +61,11 @@ class ReduceGradOp : public framework::OperatorWithKernel {
auto x_dims = ctx.Input<Tensor>("X")->dims(); auto x_dims = ctx.Input<Tensor>("X")->dims();
auto x_rank = x_dims.size(); auto x_rank = x_dims.size();
PADDLE_ENFORCE_LE(x_rank, 6, "Tensors with rank at most 6 are supported"); PADDLE_ENFORCE_LE(x_rank, 6, "Tensors with rank at most 6 are supported");
int dim = static_cast<int>(ctx.Attr<int>("dim")); int dim = ctx.Attr<int>("dim");
if (dim < 0) dim = x_rank + dim; if (dim < 0) dim = x_rank + dim;
PADDLE_ENFORCE_LT( PADDLE_ENFORCE_LT(
dim, x_rank, dim, x_rank,
"The dim should be in the range [-rank(input), rank(input)]"); "The dim should be in the range [-rank(input), rank(input))");
auto *x_grad = ctx.Output<Tensor>(framework::GradVarName("X")); auto *x_grad = ctx.Output<Tensor>(framework::GradVarName("X"));
if (x_grad) x_grad->Resize(x_dims); if (x_grad) x_grad->Resize(x_dims);
} }
...@@ -84,12 +86,13 @@ The result tensor has 1 fewer dimension than the input unless `keep_dim` is true ...@@ -84,12 +86,13 @@ The result tensor has 1 fewer dimension than the input unless `keep_dim` is true
)DOC"); )DOC");
AddAttr<int>("dim", AddAttr<int>("dim",
"(int, default 0) The dimension to reduce. " "(int, default 0) The dimension to reduce. "
"Must be in the range [-rank(input), rank(input)]") "Must be in the range [-rank(input), rank(input))")
.SetDefault(0);
AddAttr<int>(
"keep_dim",
"(int, default 0) "
"Must be 0 or 1. If 1, retain the reduced dimension with length 1.")
.SetDefault(0); .SetDefault(0);
AddAttr<bool>("keep_dim",
"(bool, default fasle) "
"If true, retain the reduced dimension with length 1.")
.SetDefault(false);
} }
}; };
...@@ -108,12 +111,13 @@ The result tensor has 1 fewer dimension than the input unless `keep_dim` is true ...@@ -108,12 +111,13 @@ The result tensor has 1 fewer dimension than the input unless `keep_dim` is true
)DOC"); )DOC");
AddAttr<int>("dim", AddAttr<int>("dim",
"(int, default 0) The dimension to reduce. " "(int, default 0) The dimension to reduce. "
"Must be in the range [-rank(input), rank(input)]") "Must be in the range [-rank(input), rank(input))")
.SetDefault(0);
AddAttr<int>(
"keep_dim",
"(int, default 0) "
"Must be 0 or 1. If 1, retain the reduced dimension with length 1.")
.SetDefault(0); .SetDefault(0);
AddAttr<bool>("keep_dim",
"(bool, default fasle) "
"If true, retain the reduced dimension with length 1.")
.SetDefault(false);
} }
}; };
...@@ -132,12 +136,13 @@ The result tensor has 1 fewer dimension than the input unless `keep_dim` is true ...@@ -132,12 +136,13 @@ The result tensor has 1 fewer dimension than the input unless `keep_dim` is true
)DOC"); )DOC");
AddAttr<int>("dim", AddAttr<int>("dim",
"(int, default 0) The dimension to reduce. " "(int, default 0) The dimension to reduce. "
"Must be in the range [-rank(input), rank(input)]") "Must be in the range [-rank(input), rank(input))")
.SetDefault(0);
AddAttr<int>(
"keep_dim",
"(int, default 0) "
"Must be 0 or 1. If 1, retain the reduced dimension with length 1.")
.SetDefault(0); .SetDefault(0);
AddAttr<bool>("keep_dim",
"(bool, default fasle) "
"If true, retain the reduced dimension with length 1.")
.SetDefault(false);
} }
}; };
...@@ -156,12 +161,13 @@ The result tensor has 1 fewer dimension than the input unless `keep_dim` is true ...@@ -156,12 +161,13 @@ The result tensor has 1 fewer dimension than the input unless `keep_dim` is true
)DOC"); )DOC");
AddAttr<int>("dim", AddAttr<int>("dim",
"(int, default 0) The dimension to reduce. " "(int, default 0) The dimension to reduce. "
"Must be in the range [-rank(input), rank(input)]") "Must be in the range [-rank(input), rank(input))")
.SetDefault(0);
AddAttr<int>(
"keep_dim",
"(int, default 0) "
"Must be 0 or 1. If 1, retain the reduced dimension with length 1.")
.SetDefault(0); .SetDefault(0);
AddAttr<bool>("keep_dim",
"(bool, default fasle) "
"If true, retain the reduced dimension with length 1.")
.SetDefault(false);
} }
}; };
......
...@@ -21,8 +21,8 @@ REGISTER_OP_GPU_KERNEL( ...@@ -21,8 +21,8 @@ REGISTER_OP_GPU_KERNEL(
reduce_sum, reduce_sum,
ops::ReduceKernel<paddle::platform::GPUPlace, float, ops::SumFunctor>); ops::ReduceKernel<paddle::platform::GPUPlace, float, ops::SumFunctor>);
REGISTER_OP_GPU_KERNEL(reduce_sum_grad, REGISTER_OP_GPU_KERNEL(reduce_sum_grad,
ops::ReduceGradEigenKernel<paddle::platform::GPUPlace, ops::ReduceGradKernel<paddle::platform::GPUPlace, float,
float, ops::SumGradFunctor>); ops::SumGradFunctor>);
REGISTER_OP_GPU_KERNEL( REGISTER_OP_GPU_KERNEL(
reduce_mean, reduce_mean,
......
...@@ -127,7 +127,7 @@ class ReduceKernel : public framework::OpKernel { ...@@ -127,7 +127,7 @@ class ReduceKernel : public framework::OpKernel {
if (dim < 0) dim = x_rank + dim; if (dim < 0) dim = x_rank + dim;
auto reduce_dim = Eigen::array<int, 1>({{dim}}); auto reduce_dim = Eigen::array<int, 1>({{dim}});
// construct the squeezed output tensor // construct the squeezed output tensor
bool keep_dim = true; // static_cast<bool>(context.Attr<bool>("keep_dim")); bool keep_dim = context.Attr<int>("keep_dim") == 1;
DDim dims = output->dims(); DDim dims = output->dims();
auto dims_vector = vectorize(dims); auto dims_vector = vectorize(dims);
if (keep_dim && x_rank > 1) { if (keep_dim && x_rank > 1) {
......
import unittest import unittest
import numpy as np import numpy as np
from gradient_checker import GradientChecker, create_op from op_test import OpTest
from op_test_util import OpTestMeta
from paddle.v2.framework.op import Operator
class TestSumOp(unittest.TestCase): class TestSumOp(OpTest):
__metaclass__ = OpTestMeta
def setUp(self): def setUp(self):
self.type = "reduce_sum" self.op_type = "reduce_sum"
self.inputs = {'X': np.random.random((5, 6, 10)).astype("float32")} self.inputs = {'X': np.random.random((5, 6, 10)).astype("float32")}
self.attrs = {'dim': -2} self.outputs = {'Out': self.inputs['X'].sum(axis=0)}
out = self.inputs['X'].sum(axis=self.attrs['dim'])
self.outputs = {'Out': out}
def test_check_output(self):
self.check_output()
class TestSumGradOp(GradientChecker): def test_check_grad(self):
def test_normal(self): self.check_grad(['X'], 'Out')
op = Operator("reduce_sum", X="X", Out="Out", dim=-2)
# use small size to decrease the error of numerical calculation
inputs = {'X': np.random.random((5, 6, 10)).astype("float32")}
self.check_grad(op, inputs, set(["X"]), "Out")
def test_1d_tensor(self):
op = Operator("reduce_sum", X="X", Out="Out", dim=0)
# use small size to decrease the error of numerical calculation
inputs = {'X': np.random.random(10).astype("float32")}
self.check_grad(op, inputs, set(["X"]), "Out")
class TestMeanOp(OpTest):
def setUp(self):
self.op_type = "reduce_mean"
self.inputs = {'X': np.random.random((5, 6, 2, 10)).astype("float32")}
self.attrs = {'dim': 1}
self.outputs = {'Out': self.inputs['X'].mean(axis=self.attrs['dim'])}
class TestKeepdimSumOp(unittest.TestCase): def test_check_output(self):
__metaclass__ = OpTestMeta self.check_output()
def setUp(self): def test_check_grad(self):
self.type = "reduce_sum" self.check_grad(['X'], 'Out')
self.inputs = {'X': np.random.random((5, 6, 10)).astype("float32")}
self.attrs = {'dim': -2}
out = self.inputs['X'].sum(axis=self.attrs['dim'], keepdims=True)
self.outputs = {'Out': out}
class TestMeanOp(unittest.TestCase): class TestMaxOp(OpTest):
__metaclass__ = OpTestMeta """Remove Max with subgradient from gradient check to confirm the success of CI."""
def setUp(self): def setUp(self):
self.type = "reduce_mean" self.op_type = "reduce_max"
self.inputs = {'X': np.random.random((5, 6, 10)).astype("float32")} self.inputs = {'X': np.random.random((5, 6, 10)).astype("float32")}
self.attrs = {'dim': -1} self.attrs = {'dim': -1}
out = self.inputs['X'].mean(axis=self.attrs['dim']) self.outputs = {'Out': self.inputs['X'].max(axis=self.attrs['dim'])}
self.outputs = {'Out': out}
def test_check_output(self):
self.check_output()
class TestMeanGradOp(GradientChecker): class TestMinOp(OpTest):
def test_normal(self): """Remove Min with subgradient from gradient check to confirm the success of CI."""
op = Operator("reduce_mean", X="X", Out="Out", dim=-2)
# use small size to decrease the error of numerical calculation
inputs = {'X': np.random.random((5, 6, 10)).astype("float32")}
self.check_grad(op, inputs, set(["X"]), "Out")
def test_1d_tensor(self): def setUp(self):
op = Operator("reduce_mean", X="X", Out="Out", dim=0) self.op_type = "reduce_min"
# use small size to decrease the error of numerical calculation self.inputs = {'X': np.random.random((5, 6, 10)).astype("float32")}
inputs = {'X': np.random.random(10).astype("float32")} self.attrs = {'dim': 2}
self.check_grad(op, inputs, set(["X"]), "Out") self.outputs = {'Out': self.inputs['X'].min(axis=self.attrs['dim'])}
def test_check_output(self):
self.check_output()
class TestMaxOp(unittest.TestCase):
__metaclass__ = OpTestMeta
class TestKeepDimReduce(OpTest):
def setUp(self): def setUp(self):
self.type = "reduce_max" self.op_type = "reduce_sum"
self.inputs = {'X': np.random.random((5, 6, 10)).astype("float32")} self.inputs = {'X': np.random.random((5, 6, 10)).astype("float32")}
self.attrs = {'dim': -1} self.attrs = {'dim': -2, 'keep_dim': 1}
out = self.inputs['X'].max(axis=self.attrs['dim']) self.outputs = {
self.outputs = {'Out': out} 'Out': self.inputs['X'].sum(axis=self.attrs['dim'], keepdims=True)
}
def test_check_output(self):
self.check_output()
def test_check_grad(self):
self.check_grad(['X'], 'Out')
class TestMinOp(unittest.TestCase):
__metaclass__ = OpTestMeta
class Test1DReduce(OpTest):
def setUp(self): def setUp(self):
self.type = "reduce_max" self.op_type = "reduce_sum"
self.inputs = {'X': np.random.random((5, 6, 10)).astype("float32")} self.inputs = {'X': np.random.random(20).astype("float32")}
self.attrs = {'dim': -2} self.outputs = {'Out': self.inputs['X'].sum(axis=0)}
out = self.inputs['X'].min(axis=self.attrs['dim'])
self.outputs = {'Out': out} def test_check_output(self):
self.check_output()
def test_check_grad(self):
self.check_grad(['X'], 'Out')
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册