Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OpAttr]Adapt tensor axis for reduce_min/max/mean/sum/prod #45078

Merged
merged 25 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions paddle/fluid/operators/reduce_ops/reduce_max_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ class ReduceMaxOpMaker : public ops::ReduceOpMaker {
virtual std::string GetOpType() const { return "Reduce reduce_max"; }
};

DECLARE_INFER_SHAPE_FUNCTOR(reduce_max,
ReduceMaxInferShapeFunctor,
PD_INFER_META(phi::ReduceInferMetaBase));
DECLARE_INFER_SHAPE_FUNCTOR(
reduce_max,
ReduceMaxInferShapeFunctor,
PD_INFER_META(phi::ReduceIntArrayAxisInferMetaBase));

REGISTER_OPERATOR(
reduce_max,
Expand Down
7 changes: 4 additions & 3 deletions paddle/fluid/operators/reduce_ops/reduce_mean_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ class __reduce_meanMaker__ : public ops::ReduceOpMaker {
virtual std::string GetOpType() const { return "Reduce reduce_mean"; }
};

DECLARE_INFER_SHAPE_FUNCTOR(reduce_mean,
ReduceMeanInferShapeFunctor,
PD_INFER_META(phi::ReduceInferMetaBase));
DECLARE_INFER_SHAPE_FUNCTOR(
reduce_mean,
ReduceMeanInferShapeFunctor,
PD_INFER_META(phi::ReduceIntArrayAxisInferMetaBase));

REGISTER_OPERATOR(reduce_mean,
ops::ReduceOp,
Expand Down
7 changes: 4 additions & 3 deletions paddle/fluid/operators/reduce_ops/reduce_min_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ class ReduceMinOpMaker : public ops::ReduceOpMaker {
virtual std::string GetOpType() const { return "Reduce reduce_min"; }
};

DECLARE_INFER_SHAPE_FUNCTOR(reduce_min,
ReduceMinInferShapeFunctor,
PD_INFER_META(phi::ReduceInferMetaBase));
DECLARE_INFER_SHAPE_FUNCTOR(
reduce_min,
ReduceMinInferShapeFunctor,
PD_INFER_META(phi::ReduceIntArrayAxisInferMetaBase));

REGISTER_OPERATOR(
reduce_min,
Expand Down
3 changes: 2 additions & 1 deletion paddle/fluid/operators/reduce_ops/reduce_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,8 @@ class ReduceOpMaker : public framework::OpProtoAndCheckerMaker {
"Must be in the range [-rank(input), rank(input)). "
"If `dim[i] < 0`, the dims[i] to reduce is `rank + dims[i]`. "
"Note that reducing on the first dim will make the LoD info lost.")
.SetDefault({0});
.SetDefault({0})
.SupportTensor();
AddAttr<bool>("keep_dim",
"(bool, default false) "
"If true, retain the reduced dimension with length 1.")
Expand Down
7 changes: 4 additions & 3 deletions paddle/fluid/operators/reduce_ops/reduce_prod_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ class ReduceProdOpMaker : public ops::ReduceOpMaker {
virtual std::string GetOpType() const { return "Reduce reduce_prod"; }
};

DECLARE_INFER_SHAPE_FUNCTOR(reduce_prod,
ReduceProdInferShapeFunctor,
PD_INFER_META(phi::ReduceInferMetaBase));
DECLARE_INFER_SHAPE_FUNCTOR(
reduce_prod,
ReduceProdInferShapeFunctor,
PD_INFER_META(phi::ReduceIntArrayAxisInferMetaBase));

REGISTER_OPERATOR(
reduce_prod,
Expand Down
18 changes: 9 additions & 9 deletions paddle/phi/api/yaml/legacy_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1689,10 +1689,10 @@
func : matrix_rank_tol

- api : max
args : (Tensor x, int64_t[] dims={}, bool keep_dim=false)
args : (Tensor x, IntArray dims={}, bool keep_dim=false)
output : Tensor(out)
infer_meta :
func : ReduceInferMeta
func : ReduceIntArrayAxisInferMeta
kernel :
func : max
backward : max_grad
Expand Down Expand Up @@ -1734,10 +1734,10 @@
backward : maxout_grad

- api : mean
args : (Tensor x, int64_t[] dims={}, bool keep_dim=false)
args : (Tensor x, IntArray dims={}, bool keep_dim=false)
output : Tensor(out)
infer_meta :
func : ReduceInferMeta
func : ReduceIntArrayAxisInferMeta
kernel :
func : mean
backward : mean_grad
Expand All @@ -1761,10 +1761,10 @@
backward : meshgrid_grad

- api : min
args : (Tensor x, int64_t[] dims={}, bool keep_dim=false)
args : (Tensor x, IntArray dims={}, bool keep_dim=false)
output : Tensor(out)
infer_meta :
func : ReduceInferMeta
func : ReduceIntArrayAxisInferMeta
kernel :
func : min
backward : min_grad
Expand Down Expand Up @@ -2102,10 +2102,10 @@

# reduce_prod
- api : reduce_prod
args : (Tensor x, int64_t[] dims, bool keep_dim, bool reduce_all)
args : (Tensor x, IntArray dims, bool keep_dim, bool reduce_all)
output : Tensor
infer_meta :
func : ReduceInferMetaBase
func : ReduceIntArrayAxisInferMetaBase
kernel :
func : prod_raw
backward : reduce_prod_grad
Expand Down Expand Up @@ -2562,7 +2562,7 @@
backward : subtract_grad

- api : sum
args : (Tensor x, int64_t[] dims={}, DataType out_dtype=DataType::UNDEFINED, bool keep_dim=false)
args : (Tensor x, IntArray dims={}, DataType out_dtype=DataType::UNDEFINED, bool keep_dim=false)
output : Tensor(out)
infer_meta :
func : SumInferMeta
Expand Down
32 changes: 16 additions & 16 deletions paddle/phi/api/yaml/legacy_backward.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1440,8 +1440,8 @@
func : matrix_power_grad

- backward_api : max_grad
forward: max (Tensor x, int64_t[] dims={}, bool keep_dim=false) -> Tensor(out)
args : (Tensor x, Tensor out, Tensor out_grad, int64_t[] dims={}, bool keep_dim=false, bool reduce_all=false)
forward: max (Tensor x, IntArray dims={}, bool keep_dim=false) -> Tensor(out)
args : (Tensor x, Tensor out, Tensor out_grad, IntArray dims={}, bool keep_dim=false, bool reduce_all=false)
output : Tensor(x_grad)
infer_meta :
func : UnchangedInferMeta
Expand Down Expand Up @@ -1498,14 +1498,14 @@
func : mean_all_grad

- backward_api : mean_double_grad
forward: mean_grad (Tensor x, Tensor grad_out, int64_t[] dims={}, bool keep_dim=false, bool reduce_all = false) -> Tensor(grad_x)
args : (Tensor grad_x_grad, int64_t[] dims={}, bool keep_dim=false, bool reduce_all=false)
forward: mean_grad (Tensor x, Tensor grad_out, IntArray dims={}, bool keep_dim=false, bool reduce_all = false) -> Tensor(grad_x)
args : (Tensor grad_x_grad, IntArray dims={}, bool keep_dim=false, bool reduce_all=false)
output : Tensor(grad_out_grad)
invoke : mean(grad_x_grad, dims, keep_dim)

- backward_api : mean_grad
forward: mean (Tensor x, int64_t[] dims={}, bool keep_dim=false) -> Tensor(out)
args : (Tensor x, Tensor out_grad, int64_t[] dims={}, bool keep_dim=false, bool reduce_all=false)
forward: mean (Tensor x, IntArray dims={}, bool keep_dim=false) -> Tensor(out)
args : (Tensor x, Tensor out_grad, IntArray dims={}, bool keep_dim=false, bool reduce_all=false)
output : Tensor(x_grad)
infer_meta :
func : UnchangedInferMeta
Expand All @@ -1525,8 +1525,8 @@
func : meshgrid_grad

- backward_api : min_grad
forward: min (Tensor x, int64_t[] dims={}, bool keep_dim=false) -> Tensor(out)
args : (Tensor x, Tensor out, Tensor out_grad, int64_t[] dims={}, bool keep_dim=false, bool reduce_all=false)
forward: min (Tensor x, IntArray dims={}, bool keep_dim=false) -> Tensor(out)
args : (Tensor x, Tensor out, Tensor out_grad, IntArray dims={}, bool keep_dim=false, bool reduce_all=false)
output : Tensor(x_grad)
infer_meta :
func : UnchangedInferMeta
Expand Down Expand Up @@ -1849,8 +1849,8 @@
inplace : (out_grad -> x_grad)

- backward_api : reduce_prod_grad
forward : reduce_prod (Tensor x, int64_t[] dims, bool keep_dim, bool reduce_all) -> Tensor(out)
args : (Tensor x, Tensor out, Tensor out_grad, int64_t[] dims, bool keep_dim, bool reduce_all)
forward : reduce_prod (Tensor x, IntArray dims, bool keep_dim, bool reduce_all) -> Tensor(out)
args : (Tensor x, Tensor out, Tensor out_grad, IntArray dims, bool keep_dim, bool reduce_all)
output : Tensor(x_grad)
infer_meta :
func : UnchangedInferMeta
Expand Down Expand Up @@ -2402,15 +2402,15 @@
inplace : (out_grad -> x_grad)

- backward_api : sum_double_grad
forward : sum_grad (Tensor x, Tensor grad_out, int64_t[] dims, bool keep_dim, bool reduce_all=false) -> Tensor(grad_x)
args : (Tensor grad_x_grad, int64_t[] dims={}, bool keep_dim=false)
forward : sum_grad (Tensor x, Tensor grad_out, IntArray dims, bool keep_dim, bool reduce_all=false) -> Tensor(grad_x)
args : (Tensor grad_x_grad, IntArray dims={}, bool keep_dim=false)
output : Tensor(grad_out_grad)
invoke : sum(grad_x_grad, dims, grad_x_grad.dtype(), keep_dim)
backward : sum_triple_grad

- backward_api : sum_grad
forward : sum (Tensor x, int64_t[] dims={}, DataType out_dtype=DataType::UNDEFINED, bool keep_dim=false) -> Tensor(out)
args : (Tensor x, Tensor out_grad, int64_t[] dims, bool keep_dim, bool reduce_all=false)
forward : sum (Tensor x, IntArray dims={}, DataType out_dtype=DataType::UNDEFINED, bool keep_dim=false) -> Tensor(out)
args : (Tensor x, Tensor out_grad, IntArray dims, bool keep_dim, bool reduce_all=false)
output : Tensor(x_grad)
infer_meta :
func : UnchangedInferMeta
Expand All @@ -2421,8 +2421,8 @@
backward : sum_double_grad

- backward_api : sum_triple_grad
forward : sum_double_grad (Tensor grad_grad_x, int64_t[] dims={}, bool keep_dim=false) -> Tensor(grad_grad_out)
args : (Tensor grad_grad_x, Tensor grad_grad_out_grad, int64_t[] dims={}, bool keep_dim=false, bool reduce_all=false)
forward : sum_double_grad (Tensor grad_grad_x, IntArray dims={}, bool keep_dim=false) -> Tensor(grad_grad_out)
args : (Tensor grad_grad_x, Tensor grad_grad_out_grad, IntArray dims={}, bool keep_dim=false, bool reduce_all=false)
output : Tensor(grad_grad_x_grad)
invoke : sum_grad(grad_grad_x, grad_grad_out_grad, dims, keep_dim, reduce_all, grad_grad_x_grad)

Expand Down
87 changes: 81 additions & 6 deletions paddle/phi/infermeta/unary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2663,6 +2663,54 @@ void ReduceInferMetaBase(const MetaTensor& x,
out->set_layout(x.layout());
}

void ReduceIntArrayAxisInferMetaBase(const MetaTensor& x,
const IntArray& axis,
bool keep_dim,
bool reduce_all,
MetaTensor* out,
MetaConfig config) {
std::vector<int64_t> vec_axis = axis.GetData();
if (config.is_runtime || !axis.FromTensor()) {
ReduceInferMetaBase(x, vec_axis, keep_dim, reduce_all, out);
} else {
std::vector<int64_t> vec_dim;
if (reduce_all) {
if (keep_dim) {
vec_dim = std::vector<int64_t>(x.dims().size(), 1);
} else {
vec_dim = {1};
}
} else {
if (keep_dim) {
vec_dim = std::vector<int64_t>(x.dims().size(), -1);
} else {
auto x_rank = static_cast<size_t>(x.dims().size());
if (vec_axis.size() >= x_rank) {
vec_dim = {-1};
} else {
vec_dim = std::vector<int64_t>(x.dims().size() - vec_axis.size(), -1);
}
}
}
DDim out_dim = phi::make_ddim(vec_dim);
out->set_dims(out_dim);
out->set_dtype(x.dtype());
out->set_layout(x.layout());
}
}

void ReduceIntArrayAxisInferMeta(const MetaTensor& x,
const IntArray& axis,
bool keep_dim,
MetaTensor* out,
MetaConfig config) {
bool reduce_all = false;
if (axis.size() == 0) {
reduce_all = true;
}
ReduceIntArrayAxisInferMetaBase(x, axis, keep_dim, reduce_all, out, config);
}

void RepeatInterleaveInferMeta(const MetaTensor& x,
int repeats,
int dim,
Expand Down Expand Up @@ -3312,24 +3360,51 @@ void StridedSliceInferMeta(const MetaTensor& x,
api.yaml
*/
void SumInferMeta(const MetaTensor& x,
const std::vector<int64_t>& axis,
const IntArray& axis,
DataType dtype,
bool keep_dim,
MetaTensor* out) {
MetaTensor* out,
MetaConfig config) {
bool reduce_all = false;
if (axis.size() == 0) {
reduce_all = true;
}
SumRawInferMeta(x, axis, keep_dim, reduce_all, dtype, out);
SumRawInferMeta(x, axis, keep_dim, reduce_all, dtype, out, config);
}

void SumRawInferMeta(const MetaTensor& x,
const std::vector<int64_t>& axis,
const IntArray& axis,
bool keep_dim,
bool reduce_all,
DataType dtype,
MetaTensor* out) {
DDim out_dim = ReduceInferDim(x, axis, keep_dim, reduce_all);
MetaTensor* out,
MetaConfig config) {
DDim out_dim;
std::vector<int64_t> vec_axis = axis.GetData();
Aurelius84 marked this conversation as resolved.
Show resolved Hide resolved
if (config.is_runtime || !axis.FromTensor()) {
out_dim = ReduceInferDim(x, vec_axis, keep_dim, reduce_all);
} else {
std::vector<int64_t> vec_dim;
if (reduce_all) {
if (keep_dim) {
vec_dim = std::vector<int64_t>(x.dims().size(), 1);
} else {
vec_dim = {1};
}
} else {
if (keep_dim) {
vec_dim = std::vector<int64_t>(x.dims().size(), -1);
} else {
auto x_rank = static_cast<size_t>(x.dims().size());
if (vec_axis.size() >= x_rank) {
vec_dim = {-1};
} else {
vec_dim = std::vector<int64_t>(x.dims().size() - vec_axis.size(), -1);
}
}
}
out_dim = phi::make_ddim(vec_dim);
}

DataType out_dtype;
if (dtype != DataType::UNDEFINED) {
Expand Down
23 changes: 19 additions & 4 deletions paddle/phi/infermeta/unary.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,19 @@ void ReduceInferMetaBase(const MetaTensor& x,
bool reduce_all,
MetaTensor* out);

void ReduceIntArrayAxisInferMetaBase(const MetaTensor& x,
const IntArray& axis,
bool keep_dim,
bool reduce_all,
MetaTensor* out,
MetaConfig config = MetaConfig());

void ReduceIntArrayAxisInferMeta(const MetaTensor& x,
const IntArray& axis,
bool keep_dim,
MetaTensor* out,
MetaConfig config = MetaConfig());

void RepeatInterleaveInferMeta(const MetaTensor& x,
int repeats,
int dim,
Expand Down Expand Up @@ -465,17 +478,19 @@ void StridedSliceInferMeta(const MetaTensor& x,
MetaConfig config = MetaConfig());

void SumInferMeta(const MetaTensor& x,
const std::vector<int64_t>& axis,
const IntArray& axis,
DataType dtype,
bool keep_dim,
MetaTensor* out);
MetaTensor* out,
MetaConfig config = MetaConfig());

void SumRawInferMeta(const MetaTensor& x,
const std::vector<int64_t>& axis,
const IntArray& axis,
bool keep_dim,
bool reduce_all,
DataType dtype,
MetaTensor* out);
MetaTensor* out,
MetaConfig config = MetaConfig());

void SvdInferMeta(const MetaTensor& x,
bool full_matrices,
Expand Down
Loading