diff --git a/paddle/fluid/operators/reduce_ops/reduce_max_op.cc b/paddle/fluid/operators/reduce_ops/reduce_max_op.cc index bfc89403d8dd7d73ffe83ea05251230798a737ab..21e16a5cd14c65c40123ab62a381419e7b34d915 100644 --- a/paddle/fluid/operators/reduce_ops/reduce_max_op.cc +++ b/paddle/fluid/operators/reduce_ops/reduce_max_op.cc @@ -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, diff --git a/paddle/fluid/operators/reduce_ops/reduce_mean_op.cc b/paddle/fluid/operators/reduce_ops/reduce_mean_op.cc index 9afa493e4ad9e15f24c732dd4a0e2eb4ac5cceb7..a5827b1e0a9d1a3e90e312824080a3f6c99bdad8 100644 --- a/paddle/fluid/operators/reduce_ops/reduce_mean_op.cc +++ b/paddle/fluid/operators/reduce_ops/reduce_mean_op.cc @@ -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, diff --git a/paddle/fluid/operators/reduce_ops/reduce_min_op.cc b/paddle/fluid/operators/reduce_ops/reduce_min_op.cc index 2dced4fecee12fcee674160d81d9c66127058d7e..e9fafce1332d2cc52be70b4642b9d6a8e6cf5af9 100644 --- a/paddle/fluid/operators/reduce_ops/reduce_min_op.cc +++ b/paddle/fluid/operators/reduce_ops/reduce_min_op.cc @@ -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, diff --git a/paddle/fluid/operators/reduce_ops/reduce_op.h b/paddle/fluid/operators/reduce_ops/reduce_op.h index 9e53a6b56de5cab3e62a315523079a407b23d189..df7804dc7a68df3f570c20cb361a4b0aa938adcf 100644 --- a/paddle/fluid/operators/reduce_ops/reduce_op.h +++ b/paddle/fluid/operators/reduce_ops/reduce_op.h @@ -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("keep_dim", "(bool, default false) " "If true, retain the reduced dimension with length 1.") diff --git a/paddle/fluid/operators/reduce_ops/reduce_prod_op.cc b/paddle/fluid/operators/reduce_ops/reduce_prod_op.cc index 578954663c7f5c5a2a48a4c662c583e1ed73c076..0b78e80746412c2419e4a8c9e0d325157368d8f3 100644 --- a/paddle/fluid/operators/reduce_ops/reduce_prod_op.cc +++ b/paddle/fluid/operators/reduce_ops/reduce_prod_op.cc @@ -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, diff --git a/paddle/phi/api/yaml/legacy_api.yaml b/paddle/phi/api/yaml/legacy_api.yaml index c74e0b0f73a027679f9bcd74f11d88b8127c72b7..4dff5c4ac8c8fc2aa4c371109a66f02b35977930 100755 --- a/paddle/phi/api/yaml/legacy_api.yaml +++ b/paddle/phi/api/yaml/legacy_api.yaml @@ -1668,10 +1668,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 @@ -1713,10 +1713,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 @@ -1762,10 +1762,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 @@ -2091,10 +2091,10 @@ backward : reciprocal_grad - 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 @@ -2555,7 +2555,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 diff --git a/paddle/phi/api/yaml/legacy_backward.yaml b/paddle/phi/api/yaml/legacy_backward.yaml index 0a3326574ee67b55ade403def6d381a80c288dd0..a19b54be724252955506ae9d87b61e7e1eefc08a 100755 --- a/paddle/phi/api/yaml/legacy_backward.yaml +++ b/paddle/phi/api/yaml/legacy_backward.yaml @@ -1451,8 +1451,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 @@ -1509,14 +1509,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) + 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) 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 @@ -1536,8 +1536,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 @@ -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 @@ -2386,14 +2386,14 @@ 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_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 diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index 82ebc89c666b5e4742727167ac7ab1134980fbc2..56e524b1bbb9114c0e346f91f025b707fd69e979 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -2656,6 +2656,33 @@ DDim ReduceInferDim(const MetaTensor& x, return out_dim; } +DDim ReduceInferDimForIntArrayAxis(const MetaTensor& x, + const IntArray& axis, + bool keep_dim, + bool reduce_all) { + std::vector vec_axis = axis.GetData(); + std::vector vec_dim; + if (reduce_all) { + if (keep_dim) { + vec_dim = std::vector(x.dims().size(), 1); + } else { + vec_dim = {1}; + } + } else { + if (keep_dim) { + vec_dim = std::vector(x.dims().size(), -1); + } else { + auto x_rank = static_cast(x.dims().size()); + if (vec_axis.size() >= x_rank) { + vec_dim = {-1}; + } else { + vec_dim = std::vector(x.dims().size() - vec_axis.size(), -1); + } + } + } + return phi::make_ddim(vec_dim); +} + void ReduceInferMeta(const MetaTensor& x, const std::vector& axis, bool keep_dim, @@ -2678,6 +2705,34 @@ 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) { + if (config.is_runtime || !axis.FromTensor()) { + ReduceInferMetaBase(x, axis.GetData(), keep_dim, reduce_all, out); + } else { + DDim out_dim = ReduceInferDimForIntArrayAxis(x, axis, keep_dim, reduce_all); + 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, @@ -3354,24 +3409,31 @@ void StridedSliceInferMeta(const MetaTensor& x, api.yaml */ void SumInferMeta(const MetaTensor& x, - const std::vector& 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& 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; + if (config.is_runtime || !axis.FromTensor()) { + out_dim = ReduceInferDim(x, axis.GetData(), keep_dim, reduce_all); + } else { + out_dim = ReduceInferDimForIntArrayAxis(x, axis, keep_dim, reduce_all); + } DataType out_dtype; if (dtype != DataType::UNDEFINED) { diff --git a/paddle/phi/infermeta/unary.h b/paddle/phi/infermeta/unary.h index 1d1995db43f64c6beaf758dc3384344251470cdd..9a67066cab2c54e80c0a5857bc4cb1ca57902bab 100644 --- a/paddle/phi/infermeta/unary.h +++ b/paddle/phi/infermeta/unary.h @@ -372,6 +372,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, @@ -477,17 +490,19 @@ void StridedSliceInferMeta(const MetaTensor& x, MetaConfig config = MetaConfig()); void SumInferMeta(const MetaTensor& x, - const std::vector& axis, + const IntArray& axis, DataType dtype, bool keep_dim, - MetaTensor* out); + MetaTensor* out, + MetaConfig config = MetaConfig()); void SumRawInferMeta(const MetaTensor& x, - const std::vector& 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, diff --git a/paddle/phi/kernels/cpu/graph_send_ue_recv_grad_kernel.cc b/paddle/phi/kernels/cpu/graph_send_ue_recv_grad_kernel.cc index c7b1e3e51853ba8c0cc69bca6ad270d97e3693e7..912426a778d0cf4a3d4cbbe99a10b9a34d053f8d 100644 --- a/paddle/phi/kernels/cpu/graph_send_ue_recv_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/graph_send_ue_recv_grad_kernel.cc @@ -73,7 +73,7 @@ void CalculateXGrad(const Context& ctx, DenseTensor x_grad_out = phi::Sum( ctx, x_grad_v2, - reduce_idx, + phi::IntArray(reduce_idx), paddle::experimental::CppTypeToDataType::Type(), true); memcpy(x_grad, x_grad_out.data(), x_grad_out.numel() * sizeof(T)); @@ -131,7 +131,7 @@ void CalculateXGrad(const Context& ctx, DenseTensor x_grad_out = phi::Sum( ctx, x_grad_v2, - reduce_idx, + phi::IntArray(reduce_idx), paddle::experimental::CppTypeToDataType::Type(), true); memcpy(x_grad, x_grad_out.data(), x_grad_out.numel() * sizeof(T)); @@ -166,7 +166,7 @@ void CalculateXGrad(const Context& ctx, DenseTensor x_grad_out = phi::Sum( ctx, x_grad_v2, - reduce_idx, + phi::IntArray(reduce_idx), paddle::experimental::CppTypeToDataType::Type(), true); memcpy(x_grad, x_grad_out.data(), x_grad_out.numel() * sizeof(T)); @@ -220,7 +220,7 @@ void CalculateXGrad(const Context& ctx, DenseTensor x_grad_out = phi::Sum( ctx, x_grad_v2, - reduce_idx, + phi::IntArray(reduce_idx), paddle::experimental::CppTypeToDataType::Type(), true); memcpy(x_grad, x_grad_out.data(), x_grad_out.numel() * sizeof(T)); diff --git a/paddle/phi/kernels/cpu/graph_send_uv_grad_kernel.cc b/paddle/phi/kernels/cpu/graph_send_uv_grad_kernel.cc index 4e28acdad3db42ed402f5b90b87421d3fe8d5aa1..23e5172c3afa722a21018f806675f899e3cc6ba5 100644 --- a/paddle/phi/kernels/cpu/graph_send_uv_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/graph_send_uv_grad_kernel.cc @@ -86,7 +86,7 @@ void CalculateGrad(const Context& ctx, DenseTensor x_grad_out = phi::Sum( ctx, x_grad_v2, - reduce_idx, + phi::IntArray(reduce_idx), paddle::experimental::CppTypeToDataType::Type(), true); memcpy(x_grad, x_grad_out.data(), x_grad_out.numel() * sizeof(T)); @@ -148,7 +148,7 @@ void CalculateGrad(const Context& ctx, DenseTensor x_grad_out = phi::Sum( ctx, x_grad_v2, - reduce_idx, + phi::IntArray(reduce_idx), paddle::experimental::CppTypeToDataType::Type(), true); memcpy(x_grad, x_grad_out.data(), x_grad_out.numel() * sizeof(T)); diff --git a/paddle/phi/kernels/cpu/identity_loss_kernel.cc b/paddle/phi/kernels/cpu/identity_loss_kernel.cc index 941174eb5b0bdb1edfb1d11af47c63c1c6e51cf7..5edf6d9e160243cbe20ea464b26ff8d6bc579bd8 100644 --- a/paddle/phi/kernels/cpu/identity_loss_kernel.cc +++ b/paddle/phi/kernels/cpu/identity_loss_kernel.cc @@ -31,7 +31,7 @@ void IdentityLossKernel(const Context& dev_ctx, case 0: // sum phi::SumRawKernel( - dev_ctx, x, std::vector{0}, false, true, out->dtype(), out); + dev_ctx, x, phi::IntArray({0}), false, true, out->dtype(), out); break; case 1: // mean diff --git a/paddle/phi/kernels/cpu/matrix_rank_tol_kernel.cc b/paddle/phi/kernels/cpu/matrix_rank_tol_kernel.cc index 4f941099c9d05faeb6b3a10670c08e3f08d79618..491e9c5d210cb9ccbe2c8ae6283cdfdb1a76bd77 100644 --- a/paddle/phi/kernels/cpu/matrix_rank_tol_kernel.cc +++ b/paddle/phi/kernels/cpu/matrix_rank_tol_kernel.cc @@ -118,7 +118,7 @@ void MatrixRankTolKernel(const Context& dev_ctx, dev_ctx.template Alloc(&max_eigenvalue_tensor); phi::MaxKernel(dev_ctx, eigenvalue_tensor, - std::vector{-1}, + phi::IntArray({-1}), false, &max_eigenvalue_tensor); diff --git a/paddle/phi/kernels/cpu/reduce_max_kernel.cc b/paddle/phi/kernels/cpu/reduce_max_kernel.cc index f9ea0aa0faf06918253f9037282b924199e3a314..b15a555a2cf4d21d4d98679463407dd28a2807a3 100644 --- a/paddle/phi/kernels/cpu/reduce_max_kernel.cc +++ b/paddle/phi/kernels/cpu/reduce_max_kernel.cc @@ -24,13 +24,13 @@ namespace phi { template void MaxRawKernel(const Context& dev_ctx, const DenseTensor& x, - const std::vector& dims, + const IntArray& dims, bool keep_dim, bool reduce_all, DenseTensor* out) { auto out_dtype = x.dtype(); phi::Reduce( - dev_ctx, x, reduce_all, dims, keep_dim, out_dtype, out); + dev_ctx, x, reduce_all, dims.GetData(), keep_dim, out_dtype, out); } } // namespace phi diff --git a/paddle/phi/kernels/cpu/reduce_mean_grad_kernel.cc b/paddle/phi/kernels/cpu/reduce_mean_grad_kernel.cc index 77176d5d7469e207e350b75d0882e65859539ff6..3ab8a40a85e55679d9ab681ce3e29248a0b63e1e 100644 --- a/paddle/phi/kernels/cpu/reduce_mean_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/reduce_mean_grad_kernel.cc @@ -24,12 +24,18 @@ template void ReduceMeanGradKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& out_grad, - const std::vector& dims, + const IntArray& dims, bool keep_dim, bool reduce_all, DenseTensor* x_grad) { - ReduceGradKernel( - dev_ctx, x, paddle::none, out_grad, dims, keep_dim, reduce_all, x_grad); + ReduceGradKernel(dev_ctx, + x, + paddle::none, + out_grad, + dims.GetData(), + keep_dim, + reduce_all, + x_grad); } } // namespace phi diff --git a/paddle/phi/kernels/cpu/reduce_mean_kernel.cc b/paddle/phi/kernels/cpu/reduce_mean_kernel.cc index 8fa687632f6534b10151b6919d75831a98080502..7164ec8b2bf9914e151f036942938214d2cc2b8d 100644 --- a/paddle/phi/kernels/cpu/reduce_mean_kernel.cc +++ b/paddle/phi/kernels/cpu/reduce_mean_kernel.cc @@ -24,13 +24,13 @@ namespace phi { template void MeanRawKernel(const Context& dev_ctx, const DenseTensor& x, - const std::vector& dims, + const IntArray& dims, bool keep_dim, bool reduce_all, DenseTensor* out) { auto out_dtype = x.dtype(); phi::Reduce( - dev_ctx, x, reduce_all, dims, keep_dim, out_dtype, out); + dev_ctx, x, reduce_all, dims.GetData(), keep_dim, out_dtype, out); } } // namespace phi diff --git a/paddle/phi/kernels/cpu/reduce_min_kernel.cc b/paddle/phi/kernels/cpu/reduce_min_kernel.cc index 0a241c81dbe690493b00caf71c0526bb76206e5e..a11de5ea81ab65380a81f095745cda56e7f1d1f9 100644 --- a/paddle/phi/kernels/cpu/reduce_min_kernel.cc +++ b/paddle/phi/kernels/cpu/reduce_min_kernel.cc @@ -24,13 +24,13 @@ namespace phi { template void MinRawKernel(const Context& dev_ctx, const DenseTensor& x, - const std::vector& dims, + const IntArray& dims, bool keep_dim, bool reduce_all, DenseTensor* out) { auto out_dtype = x.dtype(); phi::Reduce( - dev_ctx, x, reduce_all, dims, keep_dim, out_dtype, out); + dev_ctx, x, reduce_all, dims.GetData(), keep_dim, out_dtype, out); } } // namespace phi diff --git a/paddle/phi/kernels/cpu/reduce_prod_kernel.cc b/paddle/phi/kernels/cpu/reduce_prod_kernel.cc index d31a6e562628967ad2e0f1becd5094bbc21dcda0..36766d27ed4345e48047d6e14190e9a379d6cbf8 100644 --- a/paddle/phi/kernels/cpu/reduce_prod_kernel.cc +++ b/paddle/phi/kernels/cpu/reduce_prod_kernel.cc @@ -24,13 +24,13 @@ namespace phi { template void ProdRawKernel(const Context& dev_ctx, const DenseTensor& x, - const std::vector& dims, + const IntArray& dims, bool keep_dim, bool reduce_all, DenseTensor* out) { auto out_dtype = x.dtype(); phi::Reduce( - dev_ctx, x, reduce_all, dims, keep_dim, out_dtype, out); + dev_ctx, x, reduce_all, dims.GetData(), keep_dim, out_dtype, out); } } // namespace phi diff --git a/paddle/phi/kernels/cpu/reduce_sum_grad_kernel.cc b/paddle/phi/kernels/cpu/reduce_sum_grad_kernel.cc index abc18b1c578a822c3ba1f8e27e39e7047e135773..87e3df717b2442f5c41aec6ccd0f05438bc9d02a 100644 --- a/paddle/phi/kernels/cpu/reduce_sum_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/reduce_sum_grad_kernel.cc @@ -73,7 +73,7 @@ template void ReduceSumGradKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& out_grad, - const std::vector& dims, + const IntArray& dims, bool keep_dim, bool reduce_all, DenseTensor* x_grad) { @@ -84,17 +84,25 @@ void ReduceSumGradKernel(const Context& dev_ctx, DenseTensor x_grad_tmp = phi::Empty(dev_ctx, std::move(x_grad_meta)); - ComputeFromInput(dev_ctx, x, out_grad, dims, &x_grad_tmp); + ComputeFromInput( + dev_ctx, x, out_grad, dims.GetData(), &x_grad_tmp); phi::CastKernel(dev_ctx, x_grad_tmp, x.dtype(), x_grad); } else { - ComputeFromInput(dev_ctx, x, out_grad, dims, x_grad); + ComputeFromInput( + dev_ctx, x, out_grad, dims.GetData(), x_grad); } } - ReduceGradKernel( - dev_ctx, x, paddle::none, out_grad, dims, keep_dim, reduce_all, x_grad); + ReduceGradKernel(dev_ctx, + x, + paddle::none, + out_grad, + dims.GetData(), + keep_dim, + reduce_all, + x_grad); } } // namespace phi diff --git a/paddle/phi/kernels/cpu/reduce_sum_kernel.cc b/paddle/phi/kernels/cpu/reduce_sum_kernel.cc index 0b4c4b9f4705a2c7439c4728f70348f08485deb6..95e5a1a214bb976308e2bc01bd8592b6853da7fb 100644 --- a/paddle/phi/kernels/cpu/reduce_sum_kernel.cc +++ b/paddle/phi/kernels/cpu/reduce_sum_kernel.cc @@ -24,7 +24,7 @@ namespace phi { template void SumRawKernel(const Context& dev_ctx, const DenseTensor& x, - const std::vector& dims, + const IntArray& dims, bool keep_dim, bool reduce_all, DataType out_dtype, @@ -33,7 +33,7 @@ void SumRawKernel(const Context& dev_ctx, out_dtype = out->dtype(); } phi::Reduce( - dev_ctx, x, reduce_all, dims, keep_dim, out_dtype, out); + dev_ctx, x, reduce_all, dims.GetData(), keep_dim, out_dtype, out); } } // namespace phi diff --git a/paddle/phi/kernels/gpu/graph_send_ue_recv_grad_kernel.cu b/paddle/phi/kernels/gpu/graph_send_ue_recv_grad_kernel.cu index a1d522cc3d4d1da9f787ae3926c77bd12dfd86d9..41667be1a95452712fb245bfe1dfd03e85cd4ee0 100644 --- a/paddle/phi/kernels/gpu/graph_send_ue_recv_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/graph_send_ue_recv_grad_kernel.cu @@ -158,7 +158,7 @@ void CalculateXGrad(const Context& ctx, DenseTensor x_grad_out = phi::Sum( ctx, x_grad_v2, - reduce_idx, + phi::IntArray(reduce_idx), paddle::experimental::CppTypeToDataType::Type(), true); #ifdef PADDLE_WITH_HIP @@ -235,7 +235,7 @@ void CalculateXGrad(const Context& ctx, DenseTensor x_grad_out = phi::Sum( ctx, x_grad_v2, - reduce_idx, + phi::IntArray(reduce_idx), paddle::experimental::CppTypeToDataType::Type(), true); #ifdef PADDLE_WITH_HIP @@ -281,7 +281,7 @@ void CalculateXGrad(const Context& ctx, DenseTensor x_grad_out = phi::Sum( ctx, x_grad_v2, - reduce_idx, + phi::IntArray(reduce_idx), paddle::experimental::CppTypeToDataType::Type(), true); #ifdef PADDLE_WITH_HIP @@ -349,7 +349,7 @@ void CalculateXGrad(const Context& ctx, DenseTensor x_grad_out = phi::Sum( ctx, x_grad_v2, - reduce_idx, + phi::IntArray(reduce_idx), paddle::experimental::CppTypeToDataType::Type(), true); // TODO(daisiming): Whether use x_grad instead. diff --git a/paddle/phi/kernels/gpu/graph_send_uv_grad_kernel.cu b/paddle/phi/kernels/gpu/graph_send_uv_grad_kernel.cu index d845e9cc4372aaf96db9c2fdeff1d28a0916139b..1671fa7e17cd09f64897f61adf2222a84dd76696 100644 --- a/paddle/phi/kernels/gpu/graph_send_uv_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/graph_send_uv_grad_kernel.cu @@ -108,7 +108,7 @@ void CalculateGrad(const Context& ctx, DenseTensor x_grad_out = phi::Sum( ctx, x_grad_v2, - reduce_idx, + phi::IntArray(reduce_idx), paddle::experimental::CppTypeToDataType::Type(), true); #ifdef PADDLE_WITH_HIP @@ -190,7 +190,7 @@ void CalculateGrad(const Context& ctx, DenseTensor x_grad_out = phi::Sum( ctx, x_grad_v2, - reduce_idx, + phi::IntArray(reduce_idx), paddle::experimental::CppTypeToDataType::Type(), true); #ifdef PADDLE_WITH_HIP diff --git a/paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu b/paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu index 5661d61c4e8c5b0ff942c180f610e5fee7543f87..050c6d2faf5356169908ca7f0e0395b6513225b9 100644 --- a/paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu +++ b/paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu @@ -378,7 +378,7 @@ void MatrixRankTolKernel(const Context& dev_ctx, phi::MaxKernel(dev_ctx, eigenvalue_tensor, - std::vector{-1}, + phi::IntArray({-1}), false, &max_eigenvalue_tensor); diff --git a/paddle/phi/kernels/gpu/reduce_mean_grad_kernel.cu b/paddle/phi/kernels/gpu/reduce_mean_grad_kernel.cu index 50564a339ddc05b48755a22ac2b9562eb94e41f7..7da2502a5eea7039322a6f849be1736faefe5517 100644 --- a/paddle/phi/kernels/gpu/reduce_mean_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/reduce_mean_grad_kernel.cu @@ -25,13 +25,13 @@ template void ReduceMeanGradKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& out_grad, - const std::vector& dims, + const IntArray& dims, bool keep_dim, bool reduce_all, DenseTensor* x_grad) { int dim_size = x.dims().size(); std::vector reduce_dims = - funcs::details::GetReduceDim(dims, dim_size, reduce_all); + funcs::details::GetReduceDim(dims.GetData(), dim_size, reduce_all); int reduce_num = 1; for (auto i : reduce_dims) { reduce_num *= (x.dims())[i]; @@ -41,7 +41,7 @@ void ReduceMeanGradKernel(const Context& dev_ctx, dev_ctx, x, out_grad, - dims, + dims.GetData(), keep_dim, reduce_all, x_grad, diff --git a/paddle/phi/kernels/gpu/reduce_sum_grad_kernel.cu b/paddle/phi/kernels/gpu/reduce_sum_grad_kernel.cu index c0955cd7424aefc7331acf1da730ab018f89dc27..2230b4b8525b3d4eb9bb42ae1fab7cfce21d262f 100644 --- a/paddle/phi/kernels/gpu/reduce_sum_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/reduce_sum_grad_kernel.cu @@ -25,7 +25,7 @@ template void ReduceSumGradKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& out_grad, - const std::vector& dims, + const IntArray& dims, bool keep_dim, bool reduce_all, DenseTensor* x_grad) { @@ -41,7 +41,7 @@ void ReduceSumGradKernel(const Context& dev_ctx, reduce_all = true; } std::vector reduce_dims = - funcs::details::GetReduceDim(dims, dim_size, reduce_all); + funcs::details::GetReduceDim(dims.GetData(), dim_size, reduce_all); auto update_dims = vectorize(d_x->dims()); int reduce_num = 1; diff --git a/paddle/phi/kernels/impl/einsum_grad_impl.h b/paddle/phi/kernels/impl/einsum_grad_impl.h index a04185a0c53ed8a730a71dc230b89b2cb4dfc7a8..992b7572c1be5b7d9d5d9af949be063c3d327b54 100644 --- a/paddle/phi/kernels/impl/einsum_grad_impl.h +++ b/paddle/phi/kernels/impl/einsum_grad_impl.h @@ -80,7 +80,7 @@ DenseTensor PerformTileAndReduction(const Context& dev_ctx, if (to_reduce.size() != 0) { ret = Sum(dev_ctx, after_tile, - to_reduce, + phi::IntArray(to_reduce), after_tile.dtype(), false); // not keep dim. } else { diff --git a/paddle/phi/kernels/impl/einsum_impl.h b/paddle/phi/kernels/impl/einsum_impl.h index b5bc826881af80efbba61c951578c8b9802ac664..80529c8b669aac9aa04f4883d4465a6fd720bcb1 100644 --- a/paddle/phi/kernels/impl/einsum_impl.h +++ b/paddle/phi/kernels/impl/einsum_impl.h @@ -383,7 +383,8 @@ DenseTensor PerformReduction(const Context& dev_ctx, VLOG(5) << "call PerformReduction: with axis: " << paddle::string::join_strings(indices, ","); if (indices.size() == 0) return tensor; - return Sum(dev_ctx, tensor, indices, tensor.dtype(), true); + return Sum( + dev_ctx, tensor, phi::IntArray(indices), tensor.dtype(), true); } inline bool is_no_need_transpose(const std::vector& axis) { diff --git a/paddle/phi/kernels/impl/lstsq_kernel_impl.h b/paddle/phi/kernels/impl/lstsq_kernel_impl.h index 73ba954614a2215006e91f54e5128fc50ab0d7a6..1644d0852a87dc2e8102ce96f4d6a5af64f3c912 100644 --- a/paddle/phi/kernels/impl/lstsq_kernel_impl.h +++ b/paddle/phi/kernels/impl/lstsq_kernel_impl.h @@ -69,8 +69,8 @@ inline void GetResidualsTensor(const DeviceContext& dev_ctx, dev_ctx.template Alloc(pow_tensor); phi::PowKernel(dev_ctx, sub_tensor, Scalar(2), pow_tensor); - auto sum_tensor = - phi::Sum(dev_ctx, *pow_tensor, {-2}, pow_tensor->dtype(), false); + auto sum_tensor = phi::Sum( + dev_ctx, *pow_tensor, phi::IntArray({-2}), pow_tensor->dtype(), false); phi::Copy( dev_ctx, sum_tensor, dev_ctx.GetPlace(), true, residuals); } else { diff --git a/paddle/phi/kernels/impl/reduce_max_grad_kernel_impl.h b/paddle/phi/kernels/impl/reduce_max_grad_kernel_impl.h index 83dd4a2b576bbd695c882650487af965fc64e577..33730a3717781ea4ac988a26cd29dc5614e45aaf 100644 --- a/paddle/phi/kernels/impl/reduce_max_grad_kernel_impl.h +++ b/paddle/phi/kernels/impl/reduce_max_grad_kernel_impl.h @@ -25,12 +25,12 @@ void ReduceMaxGradKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& out, const DenseTensor& out_grad, - const std::vector& dims, + const IntArray& dims, bool keep_dim, bool reduce_all, DenseTensor* x_grad) { ReduceGradKernel( - dev_ctx, x, out, out_grad, dims, keep_dim, reduce_all, x_grad); + dev_ctx, x, out, out_grad, dims.GetData(), keep_dim, reduce_all, x_grad); } } // namespace phi diff --git a/paddle/phi/kernels/impl/reduce_min_grad_kernel_impl.h b/paddle/phi/kernels/impl/reduce_min_grad_kernel_impl.h index 592b5309cd970af4d3b3d8141086de1a0598c350..93afa07ff01af343c984b618f0bfece72928622f 100644 --- a/paddle/phi/kernels/impl/reduce_min_grad_kernel_impl.h +++ b/paddle/phi/kernels/impl/reduce_min_grad_kernel_impl.h @@ -25,12 +25,12 @@ void ReduceMinGradKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& out, const DenseTensor& out_grad, - const std::vector& dims, + const IntArray& dims, bool keep_dim, bool reduce_all, DenseTensor* x_grad) { ReduceGradKernel( - dev_ctx, x, out, out_grad, dims, keep_dim, reduce_all, x_grad); + dev_ctx, x, out, out_grad, dims.GetData(), keep_dim, reduce_all, x_grad); } } // namespace phi diff --git a/paddle/phi/kernels/impl/reduce_prod_grad_kernel_impl.h b/paddle/phi/kernels/impl/reduce_prod_grad_kernel_impl.h index 69775281a259c586647d0cb26c9c5d2f079b4b9c..a6f92543cc9c6b030257cf146ef2cb0faf2c16f3 100644 --- a/paddle/phi/kernels/impl/reduce_prod_grad_kernel_impl.h +++ b/paddle/phi/kernels/impl/reduce_prod_grad_kernel_impl.h @@ -14,6 +14,7 @@ #pragma once +#include "paddle/phi/common/int_array.h" #include "paddle/phi/kernels/funcs/reduce_functor.h" #include "paddle/phi/kernels/impl/reduce_grad.h" #include "paddle/phi/kernels/reduce_prod_grad_kernel.h" @@ -25,12 +26,12 @@ void ReduceProdGradKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& out, const DenseTensor& out_grad, - const std::vector& dims, + const IntArray& dims, bool keep_dim, bool reduce_all, DenseTensor* x_grad) { ReduceGradKernel( - dev_ctx, x, out, out_grad, dims, keep_dim, reduce_all, x_grad); + dev_ctx, x, out, out_grad, dims.GetData(), keep_dim, reduce_all, x_grad); } } // namespace phi diff --git a/paddle/phi/kernels/kps/reduce_max_kernel.cu b/paddle/phi/kernels/kps/reduce_max_kernel.cu index 52644849ad8bf3cbc25a5cf4bd19d92099f0b865..fb47b64f6ecec6e49592068b889aa0a3f7c3b824 100644 --- a/paddle/phi/kernels/kps/reduce_max_kernel.cu +++ b/paddle/phi/kernels/kps/reduce_max_kernel.cu @@ -12,22 +12,22 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include "paddle/phi/kernels/reduce_max_kernel.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/gpu/reduce.h" -#include "paddle/phi/kernels/reduce_max_kernel.h" namespace phi { template void MaxRawKernel(const Context& dev_ctx, const DenseTensor& x, - const std::vector& dims, + const IntArray& dims, bool keep_dim, bool reduce_all, DenseTensor* out) { auto out_dtype = x.dtype(); phi::Reduce( - dev_ctx, x, reduce_all, dims, keep_dim, out_dtype, out); + dev_ctx, x, reduce_all, dims.GetData(), keep_dim, out_dtype, out); } } // namespace phi diff --git a/paddle/phi/kernels/kps/reduce_mean_kernel.cu b/paddle/phi/kernels/kps/reduce_mean_kernel.cu index c4ecd4380c306d2d209c082bc3abb7d898567079..7f7946e0300631a8981aae15ba3dd1386552bba4 100644 --- a/paddle/phi/kernels/kps/reduce_mean_kernel.cu +++ b/paddle/phi/kernels/kps/reduce_mean_kernel.cu @@ -12,22 +12,22 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include "paddle/phi/kernels/reduce_mean_kernel.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/gpu/reduce.h" -#include "paddle/phi/kernels/reduce_mean_kernel.h" namespace phi { template void MeanRawKernel(const Context& dev_ctx, const DenseTensor& x, - const std::vector& dims, + const IntArray& dims, bool keep_dim, bool reduce_all, DenseTensor* out) { auto out_dtype = x.dtype(); phi::Reduce( - dev_ctx, x, reduce_all, dims, keep_dim, out_dtype, out, true); + dev_ctx, x, reduce_all, dims.GetData(), keep_dim, out_dtype, out, true); } } // namespace phi diff --git a/paddle/phi/kernels/kps/reduce_min_kernel.cu b/paddle/phi/kernels/kps/reduce_min_kernel.cu index 6fea48b588abb0ee51ca95faf4dd0d01ac3e8fee..9c3e61d3c0bc531b37b7467dc46ed576700a59d2 100644 --- a/paddle/phi/kernels/kps/reduce_min_kernel.cu +++ b/paddle/phi/kernels/kps/reduce_min_kernel.cu @@ -12,22 +12,22 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include "paddle/phi/kernels/reduce_min_kernel.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/gpu/reduce.h" -#include "paddle/phi/kernels/reduce_min_kernel.h" namespace phi { template void MinRawKernel(const Context& dev_ctx, const DenseTensor& x, - const std::vector& dims, + const IntArray& dims, bool keep_dim, bool reduce_all, DenseTensor* out) { auto out_dtype = x.dtype(); phi::Reduce( - dev_ctx, x, reduce_all, dims, keep_dim, out_dtype, out); + dev_ctx, x, reduce_all, dims.GetData(), keep_dim, out_dtype, out); } } // namespace phi diff --git a/paddle/phi/kernels/kps/reduce_prod_kernel.cu b/paddle/phi/kernels/kps/reduce_prod_kernel.cu index 13d8e29b60b127047a68d00ff69f947f5753d2f5..f5b52937e36fea42c9b6c5a71e0a4b5cbfe0f6f5 100644 --- a/paddle/phi/kernels/kps/reduce_prod_kernel.cu +++ b/paddle/phi/kernels/kps/reduce_prod_kernel.cu @@ -12,22 +12,22 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include "paddle/phi/kernels/reduce_prod_kernel.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/gpu/reduce.h" -#include "paddle/phi/kernels/reduce_prod_kernel.h" namespace phi { template void ProdRawKernel(const Context& dev_ctx, const DenseTensor& x, - const std::vector& dims, + const IntArray& dims, bool keep_dim, bool reduce_all, DenseTensor* out) { auto out_dtype = x.dtype(); phi::Reduce( - dev_ctx, x, reduce_all, dims, keep_dim, out_dtype, out); + dev_ctx, x, reduce_all, dims.GetData(), keep_dim, out_dtype, out); } } // namespace phi diff --git a/paddle/phi/kernels/kps/reduce_sum_kernel.cu b/paddle/phi/kernels/kps/reduce_sum_kernel.cu index f219abd3348a63fd909588ed60b0fe239be4bded..f3d3246854f3364a24f0d6fea47d0b09f4e0a097 100644 --- a/paddle/phi/kernels/kps/reduce_sum_kernel.cu +++ b/paddle/phi/kernels/kps/reduce_sum_kernel.cu @@ -12,16 +12,16 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include "paddle/phi/kernels/reduce_sum_kernel.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/gpu/reduce.h" -#include "paddle/phi/kernels/reduce_sum_kernel.h" namespace phi { template void SumRawKernel(const Context& dev_ctx, const DenseTensor& x, - const std::vector& dims, + const IntArray& dims, bool keep_dim, bool reduce_all, DataType out_dtype, @@ -30,7 +30,7 @@ void SumRawKernel(const Context& dev_ctx, out_dtype = out->dtype(); } phi::Reduce( - dev_ctx, x, reduce_all, dims, keep_dim, out_dtype, out); + dev_ctx, x, reduce_all, dims.GetData(), keep_dim, out_dtype, out); } } // namespace phi diff --git a/paddle/phi/kernels/reduce_max_grad_kernel.h b/paddle/phi/kernels/reduce_max_grad_kernel.h index ef3d9f36d28dedf380d8c90a4d7a3d28ed30bc0e..d1522667935f61a2bbb18bcc58cadb82e3c39453 100644 --- a/paddle/phi/kernels/reduce_max_grad_kernel.h +++ b/paddle/phi/kernels/reduce_max_grad_kernel.h @@ -15,6 +15,7 @@ #pragma once #include "paddle/phi/common/data_type.h" +#include "paddle/phi/common/int_array.h" #include "paddle/phi/core/dense_tensor.h" namespace phi { @@ -23,7 +24,7 @@ void ReduceMaxGradKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& out, const DenseTensor& out_grad, - const std::vector& dims, + const IntArray& dims, bool keep_dim, bool reduce_all, DenseTensor* x_grad); diff --git a/paddle/phi/kernels/reduce_max_kernel.cc b/paddle/phi/kernels/reduce_max_kernel.cc index 7bdf9ba2bbcc6cfa0e85877b05a6f26d6ddf3f03..72dd515fc4321c24c8a230313700ea7ea886668e 100644 --- a/paddle/phi/kernels/reduce_max_kernel.cc +++ b/paddle/phi/kernels/reduce_max_kernel.cc @@ -22,7 +22,7 @@ namespace phi { template void MaxKernel(const Context& dev_ctx, const DenseTensor& x, - const std::vector& dims, + const IntArray& dims, bool keep_dim, DenseTensor* out) { bool reduce_all = false; diff --git a/paddle/phi/kernels/reduce_max_kernel.h b/paddle/phi/kernels/reduce_max_kernel.h index f224f494a7229ec00884b9d5d80b97592ac3be4e..2af22a2ddde3d00c2bc4c67f34b008b2259ee167 100644 --- a/paddle/phi/kernels/reduce_max_kernel.h +++ b/paddle/phi/kernels/reduce_max_kernel.h @@ -14,13 +14,14 @@ #pragma once +#include "paddle/phi/common/int_array.h" #include "paddle/phi/core/dense_tensor.h" namespace phi { template void MaxRawKernel(const Context& dev_ctx, const DenseTensor& x, - const std::vector& dims, + const IntArray& dims, bool keep_dim, bool reduce_all, DenseTensor* out); @@ -28,7 +29,7 @@ void MaxRawKernel(const Context& dev_ctx, template void MaxKernel(const Context& dev_ctx, const DenseTensor& x, - const std::vector& dims, + const IntArray& dims, bool keep_dim, DenseTensor* out); diff --git a/paddle/phi/kernels/reduce_mean_grad_kernel.h b/paddle/phi/kernels/reduce_mean_grad_kernel.h index ccda3160aa9e509f78e7903948075cdcc4039873..572e5a0f6fb0c65144650fe62f44961c10f3122f 100644 --- a/paddle/phi/kernels/reduce_mean_grad_kernel.h +++ b/paddle/phi/kernels/reduce_mean_grad_kernel.h @@ -15,6 +15,7 @@ #pragma once #include "paddle/phi/common/data_type.h" +#include "paddle/phi/common/int_array.h" #include "paddle/phi/core/dense_tensor.h" namespace phi { @@ -22,7 +23,7 @@ template void ReduceMeanGradKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& out_grad, - const std::vector& dims, + const IntArray& dims, bool keep_dim, bool reduce_all, DenseTensor* x_grad); diff --git a/paddle/phi/kernels/reduce_mean_kernel.cc b/paddle/phi/kernels/reduce_mean_kernel.cc index 599b7eca32110c0472b3e00a1801b9f0e9851b9c..4bb77ac9747925ae1e015923c4437c2a47bfd178 100644 --- a/paddle/phi/kernels/reduce_mean_kernel.cc +++ b/paddle/phi/kernels/reduce_mean_kernel.cc @@ -22,7 +22,7 @@ namespace phi { template void MeanKernel(const Context& dev_ctx, const DenseTensor& x, - const std::vector& dims, + const IntArray& dims, bool keep_dim, DenseTensor* out) { bool reduce_all = false; diff --git a/paddle/phi/kernels/reduce_mean_kernel.h b/paddle/phi/kernels/reduce_mean_kernel.h index 2ac4bd8a46e64fcdeb9329b5705cf497db73ffba..13c387a86e2286fd293b73c6933c2db0defbacbd 100644 --- a/paddle/phi/kernels/reduce_mean_kernel.h +++ b/paddle/phi/kernels/reduce_mean_kernel.h @@ -22,7 +22,7 @@ namespace phi { template void MeanRawKernel(const Context& dev_ctx, const DenseTensor& x, - const std::vector& dims, + const IntArray& dims, bool keep_dim, bool reduce_all, DenseTensor* out); @@ -30,14 +30,14 @@ void MeanRawKernel(const Context& dev_ctx, template void MeanKernel(const Context& dev_ctx, const DenseTensor& x, - const std::vector& dims, + const IntArray& dims, bool keep_dim, DenseTensor* out); template DenseTensor Mean(const Context& dev_ctx, const DenseTensor& x, - const std::vector& axis, + const IntArray& axis, bool keep_dim) { DenseTensor dense_out; MetaTensor meta_out(&dense_out); diff --git a/paddle/phi/kernels/reduce_min_grad_kernel.h b/paddle/phi/kernels/reduce_min_grad_kernel.h index 3c6ea3a3564cf5e5c5ee4ca6987c52dc28c2e8bf..c73776143908926deb2e3e21d3b58e8b2c316f2f 100644 --- a/paddle/phi/kernels/reduce_min_grad_kernel.h +++ b/paddle/phi/kernels/reduce_min_grad_kernel.h @@ -15,6 +15,7 @@ #pragma once #include "paddle/phi/common/data_type.h" +#include "paddle/phi/common/int_array.h" #include "paddle/phi/core/dense_tensor.h" namespace phi { @@ -23,7 +24,7 @@ void ReduceMinGradKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& out, const DenseTensor& out_grad, - const std::vector& dims, + const IntArray& dims, bool keep_dim, bool reduce_all, DenseTensor* x_grad); diff --git a/paddle/phi/kernels/reduce_min_kernel.cc b/paddle/phi/kernels/reduce_min_kernel.cc index 69725759e4e82cfa6fb5c3cd71cce064a5d54d9f..11f11b772ef6f4b064d92a83e76c07f4a997a43e 100644 --- a/paddle/phi/kernels/reduce_min_kernel.cc +++ b/paddle/phi/kernels/reduce_min_kernel.cc @@ -22,7 +22,7 @@ namespace phi { template void MinKernel(const Context& dev_ctx, const DenseTensor& x, - const std::vector& dims, + const IntArray& dims, bool keep_dim, DenseTensor* out) { bool reduce_all = false; diff --git a/paddle/phi/kernels/reduce_min_kernel.h b/paddle/phi/kernels/reduce_min_kernel.h index bbf3f2ab818260f31431e8d4b9c06278450f475b..e6f133cc9ca005593d7ca7f695007431678cba51 100644 --- a/paddle/phi/kernels/reduce_min_kernel.h +++ b/paddle/phi/kernels/reduce_min_kernel.h @@ -14,6 +14,7 @@ #pragma once +#include "paddle/phi/common/int_array.h" #include "paddle/phi/core/dense_tensor.h" namespace phi { @@ -21,7 +22,7 @@ namespace phi { template void MinRawKernel(const Context& dev_ctx, const DenseTensor& x, - const std::vector& dims, + const IntArray& dims, bool keep_dim, bool reduce_all, DenseTensor* out); @@ -29,7 +30,7 @@ void MinRawKernel(const Context& dev_ctx, template void MinKernel(const Context& dev_ctx, const DenseTensor& x, - const std::vector& dims, + const IntArray& dims, bool keep_dim, DenseTensor* out); diff --git a/paddle/phi/kernels/reduce_prod_grad_kernel.h b/paddle/phi/kernels/reduce_prod_grad_kernel.h index fbf9f19a1bb824eb3eb1af9f30e3c8a03df7bb3d..fb773f167f90b48c37a673701a905a99670df29a 100644 --- a/paddle/phi/kernels/reduce_prod_grad_kernel.h +++ b/paddle/phi/kernels/reduce_prod_grad_kernel.h @@ -15,6 +15,7 @@ #pragma once #include "paddle/phi/common/data_type.h" +#include "paddle/phi/common/int_array.h" #include "paddle/phi/core/dense_tensor.h" namespace phi { @@ -23,7 +24,7 @@ void ReduceProdGradKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& out, const DenseTensor& out_grad, - const std::vector& dims, + const IntArray& dims, bool keep_dim, bool reduce_all, DenseTensor* x_grad); diff --git a/paddle/phi/kernels/reduce_prod_kernel.cc b/paddle/phi/kernels/reduce_prod_kernel.cc index 3bb1c7552b11f2871b2109ad6e1981d64ba9ccee..37f1f7bb8172eddfc5d8f4c94d28e8fb61162f7a 100644 --- a/paddle/phi/kernels/reduce_prod_kernel.cc +++ b/paddle/phi/kernels/reduce_prod_kernel.cc @@ -22,7 +22,7 @@ namespace phi { template void ProdKernel(const Context& dev_ctx, const DenseTensor& x, - const std::vector& dims, + const IntArray& dims, bool keep_dim, DenseTensor* out) { bool reduce_all = false; diff --git a/paddle/phi/kernels/reduce_prod_kernel.h b/paddle/phi/kernels/reduce_prod_kernel.h index be46a554b57e11212cbaf68e06335d01d9b45b45..91de087ccbc735d7c66270c2296ba848b0eaf700 100644 --- a/paddle/phi/kernels/reduce_prod_kernel.h +++ b/paddle/phi/kernels/reduce_prod_kernel.h @@ -14,13 +14,14 @@ #pragma once +#include "paddle/phi/common/int_array.h" #include "paddle/phi/core/dense_tensor.h" namespace phi { template void ProdRawKernel(const Context& dev_ctx, const DenseTensor& x, - const std::vector& dims, + const IntArray& dims, bool keep_dim, bool reduce_all, DenseTensor* out); @@ -28,7 +29,7 @@ void ProdRawKernel(const Context& dev_ctx, template void ProdKernel(const Context& dev_ctx, const DenseTensor& x, - const std::vector& dims, + const IntArray& dims, bool keep_dim, DenseTensor* out); diff --git a/paddle/phi/kernels/reduce_sum_grad_kernel.h b/paddle/phi/kernels/reduce_sum_grad_kernel.h index b8b6618d43ec9f96fa92f4803da92fc8630b396f..8cea1f4b34594b4a4ae1979ba52fae8093ee6091 100644 --- a/paddle/phi/kernels/reduce_sum_grad_kernel.h +++ b/paddle/phi/kernels/reduce_sum_grad_kernel.h @@ -15,6 +15,7 @@ #pragma once #include "paddle/phi/common/data_type.h" +#include "paddle/phi/common/int_array.h" #include "paddle/phi/core/dense_tensor.h" namespace phi { @@ -22,7 +23,7 @@ template void ReduceSumGradKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& out_grad, - const std::vector& dims, + const IntArray& dims, bool keep_dim, bool reduce_all, DenseTensor* x_grad); diff --git a/paddle/phi/kernels/reduce_sum_kernel.cc b/paddle/phi/kernels/reduce_sum_kernel.cc index c9622768c45d9289e256085069f82a642940ab19..5fed4dbc44d99f1ded7072f8ab1947e2fb165198 100644 --- a/paddle/phi/kernels/reduce_sum_kernel.cc +++ b/paddle/phi/kernels/reduce_sum_kernel.cc @@ -22,7 +22,7 @@ namespace phi { template void SumKernel(const Context& dev_ctx, const DenseTensor& x, - const std::vector& dims, + const IntArray& dims, DataType out_dtype, bool keep_dim, DenseTensor* out) { diff --git a/paddle/phi/kernels/reduce_sum_kernel.h b/paddle/phi/kernels/reduce_sum_kernel.h index c969cea296db13ffffe318062ce633d128e7ebfe..3bcf025d96bc4c59d40e0a04b4546633583e6401 100644 --- a/paddle/phi/kernels/reduce_sum_kernel.h +++ b/paddle/phi/kernels/reduce_sum_kernel.h @@ -14,6 +14,7 @@ #pragma once +#include "paddle/phi/common/int_array.h" #include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/infermeta/unary.h" @@ -21,7 +22,7 @@ namespace phi { template void SumRawKernel(const Context& dev_ctx, const DenseTensor& x, - const std::vector& dims, + const IntArray& dims, bool keep_dim, bool reduce_all, DataType out_dtype, @@ -30,7 +31,7 @@ void SumRawKernel(const Context& dev_ctx, template void SumKernel(const Context& dev_ctx, const DenseTensor& x, - const std::vector& dims, + const IntArray& dims, DataType out_dtype, bool keep_dim, DenseTensor* out); @@ -38,7 +39,7 @@ void SumKernel(const Context& dev_ctx, template DenseTensor Sum(const Context& dev_ctx, const DenseTensor& x, - const std::vector& axis, + const IntArray& axis, DataType dtype, bool keep_dim) { DenseTensor dense_out; diff --git a/paddle/phi/tests/kernels/test_sum_dev_api.cc b/paddle/phi/tests/kernels/test_sum_dev_api.cc index 20e934eb69297b295a0913f6f32467d2f616b9e7..b870861eaad2d0634ec1f830029416c4cf8c8111 100644 --- a/paddle/phi/tests/kernels/test_sum_dev_api.cc +++ b/paddle/phi/tests/kernels/test_sum_dev_api.cc @@ -51,8 +51,8 @@ TEST(DEV_API, sum) { .get()); // 2. test API - auto out = - phi::Sum(dev_ctx, dense_x, axis, phi::DataType::FLOAT32, false); + auto out = phi::Sum( + dev_ctx, dense_x, phi::IntArray(axis), phi::DataType::FLOAT32, false); // 3. check result ASSERT_EQ(out.dims().size(), 1); diff --git a/python/paddle/fluid/tests/unittests/test_max_op.py b/python/paddle/fluid/tests/unittests/test_max_op.py index dc11d78699e73a7f8a9ff9180696fd96dec4b3d1..235eef5d9098326e12456862a6522ab54f9c97ed 100644 --- a/python/paddle/fluid/tests/unittests/test_max_op.py +++ b/python/paddle/fluid/tests/unittests/test_max_op.py @@ -14,12 +14,16 @@ from __future__ import print_function +import os import unittest +import tempfile import numpy as np from op_test import OpTest, skip_check_grad_ci, check_out_dtype import paddle from paddle.fluid.framework import _test_eager_guard import paddle.fluid.core as core +import paddle.inference as paddle_infer +from test_sum_op import TestReduceOPTensorAxisBase class ApiMaxTest(unittest.TestCase): @@ -70,15 +74,6 @@ class ApiMaxTest(unittest.TestCase): self.assertRaises(TypeError, test_input_type) - def test_axis_type(): - with paddle.static.program_guard(paddle.static.Program(), - paddle.static.Program()): - data = paddle.static.data("data", shape=[10, 10], dtype="int64") - axis = paddle.static.data("axis", shape=[10, 10], dtype="int64") - result_min = paddle.min(data, axis) - - self.assertRaises(TypeError, test_axis_type) - def test_imperative_api(self): paddle.disable_static() np_x = np.array([10, 10]).astype('float64') @@ -124,5 +119,29 @@ class TestOutDtype(unittest.TestCase): expect_dtypes=['float32', 'float64', 'int32', 'int64']) +class TestMaxWithTensorAxis1(TestReduceOPTensorAxisBase): + + def init_data(self): + self.pd_api = paddle.max + self.np_api = np.max + self.x = paddle.randn([10, 5, 9, 9], dtype='float64') + self.np_axis = np.array([1, 2], dtype='int64') + self.tensor_axis = paddle.to_tensor([1, 2], dtype='int64') + + +class TestMaxWithTensorAxis2(TestReduceOPTensorAxisBase): + + def init_data(self): + self.pd_api = paddle.max + self.np_api = np.max + self.x = paddle.randn([10, 10, 9, 9], dtype='float64') + self.np_axis = np.array([0, 1, 2], dtype='int64') + self.tensor_axis = [ + 0, + paddle.to_tensor([1], 'int64'), + paddle.to_tensor([2], 'int64') + ] + + if __name__ == '__main__': unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_mean_op.py b/python/paddle/fluid/tests/unittests/test_mean_op.py index 965d70a59503f75266acb48e0dd38ddc83784130..a4ed6dc3b575cfa4b877f1a44725406940a114b4 100644 --- a/python/paddle/fluid/tests/unittests/test_mean_op.py +++ b/python/paddle/fluid/tests/unittests/test_mean_op.py @@ -22,6 +22,7 @@ import paddle.fluid.core as core import paddle.fluid as fluid from paddle.fluid import Program, program_guard from paddle.fluid.framework import _test_eager_guard +from test_sum_op import TestReduceOPTensorAxisBase np.random.seed(10) @@ -408,6 +409,30 @@ class TestMeanAPI(unittest.TestCase): self.assertRaises(TypeError, paddle.mean, x) +class TestMeanWithTensorAxis1(TestReduceOPTensorAxisBase): + + def init_data(self): + self.pd_api = paddle.mean + self.np_api = np.mean + self.x = paddle.randn([10, 5, 9, 9], dtype='float64') + self.np_axis = np.array([1, 2], dtype='int64') + self.tensor_axis = paddle.to_tensor([1, 2], dtype='int64') + + +class TestMeanWithTensorAxis2(TestReduceOPTensorAxisBase): + + def init_data(self): + self.pd_api = paddle.mean + self.np_api = np.mean + self.x = paddle.randn([10, 10, 9, 9], dtype='float64') + self.np_axis = np.array([0, 1, 2], dtype='int64') + self.tensor_axis = [ + 0, + paddle.to_tensor([1], 'int64'), + paddle.to_tensor([2], 'int64') + ] + + if __name__ == "__main__": paddle.enable_static() unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_min_op.py b/python/paddle/fluid/tests/unittests/test_min_op.py index 6e5f9d132159300f0bc2f149b9992f36488b555d..2daa813997154a4d29e464a38c7dc44bc3a17eea 100644 --- a/python/paddle/fluid/tests/unittests/test_min_op.py +++ b/python/paddle/fluid/tests/unittests/test_min_op.py @@ -14,12 +14,16 @@ from __future__ import print_function +import os import unittest +import tempfile import numpy as np from op_test import OpTest, skip_check_grad_ci, check_out_dtype import paddle import paddle.fluid.core as core from paddle.fluid.framework import _test_eager_guard +import paddle.inference as paddle_infer +from test_sum_op import TestReduceOPTensorAxisBase class ApiMinTest(unittest.TestCase): @@ -70,15 +74,6 @@ class ApiMinTest(unittest.TestCase): self.assertRaises(TypeError, test_input_type) - def test_axis_type(): - with paddle.static.program_guard(paddle.static.Program(), - paddle.static.Program()): - data = paddle.static.data("data", shape=[10, 10], dtype="int64") - axis = paddle.static.data("axis", shape=[10, 10], dtype="int64") - result_min = paddle.min(data, axis) - - self.assertRaises(TypeError, test_axis_type) - def test_imperative_api(self): paddle.disable_static() np_x = np.array([10, 10]).astype('float64') @@ -103,5 +98,30 @@ class TestOutDtype(unittest.TestCase): expect_dtypes=['float32', 'float64', 'int32', 'int64']) +class TestMinWithTensorAxis1(TestReduceOPTensorAxisBase): + + def init_data(self): + self.pd_api = paddle.min + self.np_api = np.min + self.x = paddle.randn([10, 5, 9, 9], dtype='float64') + self.np_axis = np.array([1, 2], dtype='int64') + self.tensor_axis = paddle.to_tensor([1, 2], dtype='int64') + + +class TestMinWithTensorAxis2(TestReduceOPTensorAxisBase): + + def init_data(self): + self.pd_api = paddle.min + self.np_api = np.min + self.x = paddle.randn([10, 10, 9, 9], dtype='float64') + self.np_axis = np.array([0, 1, 2], dtype='int64') + self.tensor_axis = [ + 0, + paddle.to_tensor([1], 'int64'), + paddle.to_tensor([2], 'int64') + ] + self.keepdim = True + + if __name__ == '__main__': unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_prod_op.py b/python/paddle/fluid/tests/unittests/test_prod_op.py index 19611a933cc29f82c38df69fa2a11aa24368a3d4..dde612ea746c56fa627fe0e21e010723e6e295cc 100644 --- a/python/paddle/fluid/tests/unittests/test_prod_op.py +++ b/python/paddle/fluid/tests/unittests/test_prod_op.py @@ -17,6 +17,7 @@ from __future__ import print_function import paddle import unittest import numpy as np +from test_sum_op import TestReduceOPTensorAxisBase class TestProdOp(unittest.TestCase): @@ -168,5 +169,29 @@ class TestProdOpError(unittest.TestCase): self.assertRaises(TypeError, paddle.prod, x, 'bool') +class TestProdWithTensorAxis1(TestReduceOPTensorAxisBase): + + def init_data(self): + self.pd_api = paddle.prod + self.np_api = np.prod + self.x = paddle.randn([10, 5, 9, 9], dtype='float64') + self.np_axis = np.array([1, 2], dtype='int64') + self.tensor_axis = paddle.to_tensor([1, 2], dtype='int64') + + +class TestProdWithTensorAxis2(TestReduceOPTensorAxisBase): + + def init_data(self): + self.pd_api = paddle.prod + self.np_api = np.prod + self.x = paddle.randn([10, 10, 9, 9], dtype='float64') + self.np_axis = np.array([0, 1, 2], dtype='int64') + self.tensor_axis = [ + 0, + paddle.to_tensor([1], 'int64'), + paddle.to_tensor([2], 'int64') + ] + + if __name__ == "__main__": unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_sum_op.py b/python/paddle/fluid/tests/unittests/test_sum_op.py index e327c335b0dd0e65892f92e4b19ed94957436db7..b143af2ac50c340e438bb181ad3ea5ff5f4294c5 100644 --- a/python/paddle/fluid/tests/unittests/test_sum_op.py +++ b/python/paddle/fluid/tests/unittests/test_sum_op.py @@ -14,7 +14,9 @@ from __future__ import print_function +import os import unittest +import tempfile import numpy as np from op_test import OpTest import paddle @@ -27,6 +29,7 @@ from paddle.fluid.tests.unittests.op_test import (OpTest, convert_uint16_to_float) from paddle import _C_ops, _legacy_C_ops from paddle.fluid.framework import _test_eager_guard +import paddle.inference as paddle_infer class TestSumOp(OpTest): @@ -483,6 +486,100 @@ class TestSumOpError(unittest.TestCase): create_test_sum_fp16_class(TestSelectedRowsSumOp) create_test_sum_fp16_class(TestLoDTensorAndSelectedRowsOp) + +class TestReduceOPTensorAxisBase(unittest.TestCase): + + def setUp(self): + paddle.disable_static() + paddle.seed(2022) + self.temp_dir = tempfile.TemporaryDirectory() + self.save_path = os.path.join(self.temp_dir.name, 'reduce_tensor_axis') + self.place = paddle.CUDAPlace( + 0) if paddle.is_compiled_with_cuda() else paddle.CPUPlace() + self.keepdim = False + self.init_data() + + def tearDwon(self): + self.temp_dir.cleanup() + + def init_data(self): + self.pd_api = paddle.sum + self.np_api = np.sum + self.x = paddle.randn([10, 5, 9, 9], dtype='float64') + self.np_axis = np.array((1, 2), dtype='int64') + self.tensor_axis = paddle.to_tensor(self.np_axis, dtype='int64') + + def test_dygraph(self): + self.x.stop_gradient = False + pd_out = self.pd_api(self.x, self.tensor_axis) + np_out = self.np_api(self.x.numpy(), tuple(self.np_axis)) + np.testing.assert_allclose( + pd_out.numpy() if pd_out.size > 1 else pd_out.item(), np_out) + pd_out.backward() + self.assertEqual(self.x.gradient().shape, tuple(self.x.shape)) + + def test_static_and_infer(self): + paddle.enable_static() + main_prog = paddle.static.Program() + starup_prog = paddle.static.Program() + with paddle.static.program_guard(main_prog, starup_prog): + # run static + x = paddle.static.data(shape=self.x.shape, + name='x', + dtype='float32') + if isinstance(self.tensor_axis, paddle.Tensor): + axis = paddle.assign(self.np_axis) + else: + axis = [] + for i, item in enumerate(self.tensor_axis): + if isinstance(item, int): + axis.append(item) + else: + axis.append(paddle.full([1], self.np_axis[i], 'int64')) + + linear = paddle.nn.Linear(x.shape[-1], 5) + linear_out = linear(x) + out = self.pd_api(linear_out, axis, keepdim=self.keepdim) + exe = paddle.static.Executor(self.place) + exe.run(starup_prog) + static_out = exe.run(feed={'x': self.x.numpy().astype('float32')}, + fetch_list=[out]) + + # run infer + paddle.static.save_inference_model(self.save_path, [x], [out], exe) + config = paddle_infer.Config(self.save_path + '.pdmodel', + self.save_path + '.pdiparams') + if paddle.is_compiled_with_cuda(): + config.enable_use_gpu(100, 0) + else: + config.disable_gpu() + predictor = paddle_infer.create_predictor(config) + input_names = predictor.get_input_names() + input_handle = predictor.get_input_handle(input_names[0]) + fake_input = self.x.numpy().astype('float32') + input_handle.reshape(self.x.shape) + input_handle.copy_from_cpu(fake_input) + predictor.run() + output_names = predictor.get_output_names() + output_handle = predictor.get_output_handle(output_names[0]) + infer_out = output_handle.copy_to_cpu() + np.testing.assert_allclose(static_out[0], infer_out) + + +class TestSumWithTensorAxis1(TestReduceOPTensorAxisBase): + + def init_data(self): + self.pd_api = paddle.sum + self.np_api = np.sum + self.x = paddle.randn([10, 5, 9, 9], dtype='float64') + self.np_axis = np.array([0, 1, 2], dtype='int64') + self.tensor_axis = [ + 0, + paddle.to_tensor([1], 'int64'), + paddle.to_tensor([2], 'int64') + ] + + if __name__ == "__main__": enable_static() unittest.main() diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 0bad545025c1c2c0f3bda776eb670760d7f7837d..3acd9d5897aa16ff39a01a223a87ae5f212e3f0b 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -34,6 +34,7 @@ from ..fluid.framework import _in_legacy_dygraph from ..framework import _varbase_creator, convert_np_dtype_to_dtype_ from ..fluid.data_feeder import check_variable_and_dtype, check_type, check_dtype, convert_dtype from ..fluid.dygraph.inplace_utils import inplace_apis_in_dygraph_only +from ..fluid.layers import utils # TODO: define math functions # yapf: disable @@ -1144,11 +1145,22 @@ def sum(x, axis=None, dtype=None, keepdim=False, name=None): out8 = paddle.sum(x, axis=0) # [1, 1, 1, 1] out9 = paddle.sum(x, axis=1) # [4, 0] """ - if axis is not None and not isinstance(axis, (list, tuple)): - axis = [axis] + if isinstance(axis, Variable): + reduce_all_flag = True if axis.shape[0] == len(x.shape) else False + else: + if axis is not None and not isinstance(axis, (list, tuple)): + axis = [axis] - if not axis: - axis = [] + if not axis: + axis = [] + + if len(axis) == 0: + reduce_all_flag = True + else: + if len(axis) == len(x.shape): + reduce_all_flag = True + else: + reduce_all_flag = False dtype_flag = False if dtype is not None: @@ -1158,16 +1170,12 @@ def sum(x, axis=None, dtype=None, keepdim=False, name=None): if in_dygraph_mode(): return _C_ops.sum(x, axis, dtype, keepdim) - if len(axis) == 0: - reduce_all_flag = True - else: - if len(axis) == len(x.shape): - reduce_all_flag = True - else: - reduce_all_flag = False + if not isinstance(axis, Variable): + axis = axis if axis != None and axis != [] and axis != () else [0] + if utils._contain_var(axis): + axis = utils._convert_to_tensor_list(axis) if _in_legacy_dygraph(): - axis = axis if axis != None and axis != [] else [0] if dtype_flag: return _legacy_C_ops.reduce_sum(x, 'dim', axis, 'keep_dim', keepdim, 'reduce_all', reduce_all_flag, 'in_dtype', @@ -1177,7 +1185,7 @@ def sum(x, axis=None, dtype=None, keepdim=False, name=None): 'reduce_all', reduce_all_flag) attrs = { - 'dim': axis if axis != None and axis != [] and axis != () else [0], + 'dim': axis, 'keep_dim': keepdim, 'reduce_all': reduce_all_flag } @@ -1194,7 +1202,7 @@ def sum(x, axis=None, dtype=None, keepdim=False, name=None): u'bool', u'float16', u'float32', u'float64', u'int32', u'int64', u'complex64', u'complex128'], 'sum') - check_type(axis, 'axis', (int, list, tuple, type(None)), 'sum') + check_type(axis, 'axis', (int, list, tuple, type(None), Variable), 'sum') helper = LayerHelper('sum', **locals()) if dtype_flag: @@ -2058,6 +2066,11 @@ def _get_reduce_axis(axis): axis = [] return reduce_all, axis +def _get_reduce_axis_with_tensor(axis): + if isinstance(axis, Variable): + return False, axis + return _get_reduce_axis(axis) + def _get_reduce_all_value(axis): """ Internal function for max, min, amax and amin. @@ -2154,7 +2167,7 @@ def max(x, axis=None, keepdim=False, name=None): #[7., 8.], [[[0., 0.], [0., 0.]], [[0., 0.], [1., 1.]]] """ - reduce_all, axis = _get_reduce_axis(axis) + reduce_all, axis = _get_reduce_axis_with_tensor(axis) if in_dygraph_mode(): return _C_ops.max(x, axis, keepdim) if _in_legacy_dygraph(): @@ -2164,6 +2177,8 @@ def max(x, axis=None, keepdim=False, name=None): helper = LayerHelper('max', **locals()) check_variable_and_dtype( x, 'x', ['float32', 'float64', 'int32', 'int64'], 'max') + if not isinstance(axis, Variable) and utils._contain_var(axis): + axis = utils._convert_to_tensor_list(axis) out = helper.create_variable_for_type_inference( dtype=x.dtype) @@ -2255,7 +2270,7 @@ def min(x, axis=None, keepdim=False, name=None): #[1., 2.], [[[1., 1.], [0., 0.]], [[0., 0.], [0., 0.]]] """ - reduce_all, axis = _get_reduce_axis(axis) + reduce_all, axis = _get_reduce_axis_with_tensor(axis) if in_dygraph_mode(): return _C_ops.min(x, axis, keepdim) @@ -2266,6 +2281,8 @@ def min(x, axis=None, keepdim=False, name=None): helper = LayerHelper('min', **locals()) check_variable_and_dtype( x, 'x', ['float32', 'float64', 'int32', 'int64'], 'min') + if not isinstance(axis, Variable) and utils._contain_var(axis): + axis = utils._convert_to_tensor_list(axis) out = helper.create_variable_for_type_inference( dtype=x.dtype) @@ -3369,19 +3386,22 @@ def prod(x, axis=None, keepdim=False, dtype=None, name=None): x = cast(x, dtype) dim = axis - if dim is not None and not isinstance(dim, list): - if isinstance(dim, tuple): - dim = list(dim) - elif isinstance(dim, int): - dim = [dim] - else: - raise TypeError( - "The type of axis must be int, list or tuple, but received {}". - format(type(dim))) + if isinstance(dim, Variable): + reduce_all = True if axis.shape[0] == len(x.shape) else False + else: + if dim is not None and not isinstance(dim, list): + if isinstance(dim, tuple): + dim = list(dim) + elif isinstance(dim, int): + dim = [dim] + else: + raise TypeError( + "The type of axis must be int, list or tuple, but received {}". + format(type(dim))) - reduce_all = True if dim is None or len(dim) == 0 or len(dim) == len(x.shape) else False - if dim is None or len(dim) == 0: - dim = [0] + reduce_all = True if dim is None or len(dim) == 0 or len(dim) == len(x.shape) else False + if dim is None or len(dim) == 0: + dim = [0] if in_dygraph_mode(): return _C_ops.reduce_prod(x, dim, keepdim, reduce_all) @@ -3393,6 +3413,8 @@ def prod(x, axis=None, keepdim=False, dtype=None, name=None): check_variable_and_dtype( x, 'x/input', ['float32', 'float64', 'int32', 'int64'], 'reduce_prod') out = helper.create_variable_for_type_inference(dtype=helper.input_dtype()) + if not isinstance(dim, Variable) and utils._contain_var(dim): + dim = utils._convert_to_tensor_list(dim) helper.append_op( type='reduce_prod', inputs={'X': x}, diff --git a/python/paddle/tensor/stat.py b/python/paddle/tensor/stat.py index 043449cd6d81da2090621901cb91da79aa8e2170..b3e14784c3d1cee2eb8c44d0226baab80ee68cde 100644 --- a/python/paddle/tensor/stat.py +++ b/python/paddle/tensor/stat.py @@ -21,6 +21,7 @@ from ..framework import core from paddle.fluid.framework import _in_legacy_dygraph, in_dygraph_mode from .search import where from ..fluid.data_feeder import convert_dtype, check_variable_and_dtype, check_type, check_dtype +from ..fluid.layers import utils import paddle from paddle import _C_ops, _legacy_C_ops @@ -80,17 +81,20 @@ def mean(x, axis=None, keepdim=False, name=None): # [ 8.5 12.5 16.5] """ - if isinstance(axis, int): - axis = [axis] - reduce_all = True if axis is None \ - or len(axis)==0 \ - or len(axis) == len(x.shape) else False - if axis is None or len(axis) == 0: - axis = [0] + if isinstance(axis, Variable): + reduce_all = True if axis.shape[0] == len(x.shape) else False + else: + if isinstance(axis, int): + axis = [axis] + reduce_all = True if axis is None \ + or len(axis)==0 \ + or len(axis) == len(x.shape) else False + if axis is None or len(axis) == 0: + axis = [0] if in_dygraph_mode(): if reduce_all: - axis = range(len(x.shape)) + axis = list(range(len(x.shape))) return _C_ops.mean(x, axis, keepdim) if _in_legacy_dygraph(): return _legacy_C_ops.reduce_mean(x, 'dim', axis, 'keep_dim', keepdim, @@ -99,12 +103,17 @@ def mean(x, axis=None, keepdim=False, name=None): check_variable_and_dtype(x, 'x/input', ['uint16', 'float16', 'float32', 'float64'], 'mean/reduce_mean') - check_type(axis, 'axis/dim', (int, list, tuple), 'mean/reduce_mean') + check_type(axis, 'axis/dim', (int, list, tuple, Variable), + 'mean/reduce_mean') if isinstance(axis, (list, tuple)): for item in axis: - check_type(item, 'elements of axis/dim', (int), 'mean/reduce_mean') + check_type(item, 'elements of axis/dim', (int, Variable), + 'mean/reduce_mean') helper = LayerHelper('mean', **locals()) + + if not isinstance(axis, Variable) and utils._contain_var(axis): + axis = utils._convert_to_tensor_list(axis) attrs = {'dim': axis, 'keep_dim': keepdim, 'reduce_all': reduce_all} out = helper.create_variable_for_type_inference(x.dtype) helper.append_op(type='reduce_mean',