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

use cumprod fix bug of prod_grad #64127

Merged
merged 42 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
0ad4663
add reverse and exclusive
YibinLiu666 Apr 27, 2024
7f55b52
support reverse and exclusive
YibinLiu666 May 2, 2024
9124b8b
fix inplace test
YibinLiu666 May 2, 2024
7f384a0
fix make on xpu
YibinLiu666 May 2, 2024
a47299d
remove print
YibinLiu666 May 2, 2024
474b150
update test time
YibinLiu666 May 3, 2024
7b0db15
fix prod_grad use cumprod
YibinLiu666 May 8, 2024
830a180
add 1-5D test
YibinLiu666 May 10, 2024
8f02aa8
speed up test
YibinLiu666 May 11, 2024
4c21d9a
mul out_grad
YibinLiu666 May 13, 2024
d5b1193
update test
YibinLiu666 May 13, 2024
5256ec2
Update CMakeLists.txt
YibinLiu666 May 14, 2024
2ceb862
update op_version
YibinLiu666 May 14, 2024
8934be5
Merge branch 'cumprod' of https://github.com/YibinLiu666/Paddle into …
YibinLiu666 May 14, 2024
db8288c
Merge branch 'cumprod' of https://github.com/YibinLiu666/Paddle into …
YibinLiu666 May 15, 2024
2cba65f
update detail.h
YibinLiu666 May 15, 2024
60d57aa
update composite
YibinLiu666 May 17, 2024
1638c6a
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
YibinLiu666 May 17, 2024
cd62a38
update detail.h
YibinLiu666 May 17, 2024
7fc46d2
stage
YibinLiu666 May 22, 2024
b62e36a
update test
YibinLiu666 May 24, 2024
c117324
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
YibinLiu666 May 24, 2024
bb0881a
only test on gpu
YibinLiu666 May 24, 2024
7f07af7
update details.h
YibinLiu666 May 26, 2024
5d41aa3
CI
YibinLiu666 May 26, 2024
f4d31a5
Update composite_backward_api.h
YibinLiu666 May 27, 2024
9ca9991
Update details.h
YibinLiu666 May 27, 2024
7b7acbe
update test
YibinLiu666 May 27, 2024
8efad95
update
YibinLiu666 May 27, 2024
236ba87
Update test_reduce_op.py
YibinLiu666 May 28, 2024
023de58
fix cumprod cpu bug
YibinLiu666 May 29, 2024
5c87a28
update test
YibinLiu666 May 29, 2024
30a3604
update test
YibinLiu666 May 29, 2024
52ee9c9
update
YibinLiu666 May 29, 2024
1a7827c
update
YibinLiu666 May 29, 2024
6678b74
ci
YibinLiu666 May 29, 2024
cf549b4
ci
YibinLiu666 May 29, 2024
2af40a2
remove comment
YibinLiu666 May 29, 2024
08a5aae
CI
YibinLiu666 May 30, 2024
fbc6487
Your commit message
YibinLiu666 May 30, 2024
e901443
CI
YibinLiu666 May 30, 2024
a51c237
update
YibinLiu666 May 30, 2024
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
1 change: 1 addition & 0 deletions paddle/fluid/prim/api/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
- pad
- sqrt
- cumsum
- cumprod
- put_along_axis
- sin
- cos
Expand Down
77 changes: 66 additions & 11 deletions paddle/fluid/prim/api/composite_backward/composite_backward_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -1127,11 +1127,13 @@ void prod_grad(const Tensor& x,
} else {
reduce_all = false;
}
auto x_grad_tmp = Tensor();
auto out_tmp = Tensor();
auto out_grad_tmp = Tensor();
auto x_reshape = Tensor();
std::vector<int64_t> unchange_axis, change_axis, transpose_shape,
cumprod_shape;
std::vector<int> transpose_dim, origin_position;
if (x_dim_size == 1) {
x_grad_tmp = out_grad.expand(IntArray(x_dim));
out_tmp = out.expand(IntArray(x_dim));
out_grad_tmp = out_grad.expand(IntArray(x_dim));
} else {
if (!keep_dim) {
auto axis_ = std::vector<int64_t>();
Expand All @@ -1149,16 +1151,69 @@ void prod_grad(const Tensor& x,
}
auto out_grad_shape = get_unsqueeze_dims(out_grad, axis_);
auto out_grad_ = reshape<T>(out_grad, out_grad_shape);
x_grad_tmp = out_grad_.expand(IntArray(x_dim));
auto out_ = reshape<T>(out, out_grad_shape);
out_tmp = out_.expand(IntArray(x_dim));
out_grad_tmp = out_grad_.expand(IntArray(x_dim));
} else {
x_grad_tmp = out_grad.expand(IntArray(x_dim));
out_tmp = out.expand(IntArray(x_dim));
out_grad_tmp = out_grad.expand(IntArray(x_dim));
}
}
auto x_grad_res = x_grad_tmp * out_tmp * (1 / x);
set_output<T>(x_grad_res, x_grad);
auto axis_ = std::vector<int64_t>();
if (reduce_all) {
int64_t numel = 1;
for (int64_t i = 0; i < x_dim_size; i++) {
axis_.push_back(i);
numel *= x_dim[i];
}
cumprod_shape.push_back(numel);
x_reshape = reshape<T>(x, cumprod_shape);
auto left_cumprod = cumprod<T>(x_reshape, -1, true, false);
auto right_cumprod = cumprod<T>(x_reshape, -1, true, true);
auto x_grad_tmp = left_cumprod * right_cumprod;
auto x_grad_tmp2 = reshape<T>(x_grad_tmp, x.shape());
auto x_grad_res = x_grad_tmp2 * out_grad_tmp;
set_output<T>(x_grad_res, x_grad);
} else {
int64_t unchange_size = x_dim_size - axis_size;
int64_t unchange_index = 0;
for (int64_t i = 0; i < axis_size; i++) {
if (axis[i] < 0) {
axis_.push_back(axis[i] + x_dim_size);
} else {
axis_.push_back(axis[i]);
}
}
for (int64_t i = 0; i < x_dim_size; i++) {
auto it = find(axis_.begin(), axis_.end(), i);
if (it != axis_.end()) {
int64_t index = it - axis_.begin();
origin_position.push_back(static_cast<int>(unchange_size + index));
} else {
unchange_axis.push_back(i);
origin_position.push_back(static_cast<int>(unchange_index));
unchange_index += 1;
}
}
int64_t numel = 1;
for (int64_t i = 0; i < unchange_size; i++) {
transpose_shape.push_back(x_dim[unchange_axis[i]]);
cumprod_shape.push_back(x_dim[unchange_axis[i]]);
transpose_dim.push_back(static_cast<int>(unchange_axis[i]));
}
for (int64_t i = 0; i < axis_size; i++) {
transpose_shape.push_back(x_dim[axis_[i]]);
transpose_dim.push_back(static_cast<int>(axis_[i]));
numel *= x_dim[axis_[i]];
}
cumprod_shape.push_back(numel);
auto x_transpose = transpose<T>(x, transpose_dim);
x_reshape = reshape<T>(x_transpose, cumprod_shape);
auto left_cumprod = cumprod<T>(x_reshape, -1, true, false);
auto right_cumprod = cumprod<T>(x_reshape, -1, true, true);
auto x_grad_tmp = left_cumprod * right_cumprod;
auto x_grad_reshape = reshape<T>(x_grad_tmp, transpose_shape);
auto x_grad_tmp2 = transpose<T>(x_grad_reshape, origin_position);
auto x_grad_res = x_grad_tmp2 * out_grad_tmp;
set_output<T>(x_grad_res, x_grad);
}
}
}

Expand Down
77 changes: 66 additions & 11 deletions paddle/fluid/primitive/rule/vjp/details.h
Original file line number Diff line number Diff line change
Expand Up @@ -1716,11 +1716,13 @@ void prod_grad(const Tensor& x,
} else {
reduce_all = false;
}
auto x_grad_tmp = Tensor();
auto out_tmp = Tensor();
auto out_grad_tmp = Tensor();
auto x_reshape = Tensor();
std::vector<int64_t> unchange_axis, change_axis, transpose_shape,
cumprod_shape;
std::vector<int> transpose_dim, origin_position;
if (x_dim_size == 1) {
x_grad_tmp = out_grad.expand(IntArray(x_dim));
out_tmp = out.expand(IntArray(x_dim));
out_grad_tmp = out_grad.expand(IntArray(x_dim));
} else {
if (!keep_dim) {
auto axis_ = std::vector<int64_t>();
Expand All @@ -1738,16 +1740,69 @@ void prod_grad(const Tensor& x,
}
auto out_grad_shape = get_unsqueeze_dims(out_grad, axis_);
auto out_grad_ = reshape<T>(out_grad, out_grad_shape);
x_grad_tmp = out_grad_.expand(IntArray(x_dim));
auto out_ = reshape<T>(out, out_grad_shape);
out_tmp = out_.expand(IntArray(x_dim));
out_grad_tmp = out_grad_.expand(IntArray(x_dim));
} else {
x_grad_tmp = out_grad.expand(IntArray(x_dim));
out_tmp = out.expand(IntArray(x_dim));
out_grad_tmp = out_grad.expand(IntArray(x_dim));
}
}
auto x_grad_res = x_grad_tmp * out_tmp * (1 / x);
set_output<T>(x_grad_res, x_grad);
auto axis_ = std::vector<int64_t>();
if (reduce_all) {
int64_t numel = 1;
for (int64_t i = 0; i < x_dim_size; i++) {
axis_.push_back(i);
numel *= x_dim[i];
}
cumprod_shape.push_back(numel);
x_reshape = reshape<T>(x, cumprod_shape);
auto left_cumprod = cumprod<T>(x_reshape, -1, true, false);
auto right_cumprod = cumprod<T>(x_reshape, -1, true, true);
auto x_grad_tmp = left_cumprod * right_cumprod;
auto x_grad_tmp2 = reshape<T>(x_grad_tmp, x.shape());
auto x_grad_res = x_grad_tmp2 * out_grad_tmp;
set_output<T>(x_grad_res, x_grad);
} else {
int64_t unchange_size = x_dim_size - axis_size;
int64_t unchange_index = 0;
for (int64_t i = 0; i < axis_size; i++) {
if (axis[i] < 0) {
axis_.push_back(axis[i] + x_dim_size);
} else {
axis_.push_back(axis[i]);
}
}
for (int64_t i = 0; i < x_dim_size; i++) {
auto it = find(axis_.begin(), axis_.end(), i);
if (it != axis_.end()) {
int64_t index = it - axis_.begin();
origin_position.push_back(static_cast<int>(unchange_size + index));
} else {
unchange_axis.push_back(i);
origin_position.push_back(static_cast<int>(unchange_index));
unchange_index += 1;
}
}
int64_t numel = 1;
for (int64_t i = 0; i < unchange_size; i++) {
transpose_shape.push_back(x_dim[unchange_axis[i]]);
cumprod_shape.push_back(x_dim[unchange_axis[i]]);
transpose_dim.push_back(static_cast<int>(unchange_axis[i]));
}
for (int64_t i = 0; i < axis_size; i++) {
transpose_shape.push_back(x_dim[axis_[i]]);
transpose_dim.push_back(static_cast<int>(axis_[i]));
numel *= x_dim[axis_[i]];
}
cumprod_shape.push_back(numel);
auto x_transpose = transpose<T>(x, transpose_dim);
x_reshape = reshape<T>(x_transpose, cumprod_shape);
auto left_cumprod = cumprod<T>(x_reshape, -1, true, false);
auto right_cumprod = cumprod<T>(x_reshape, -1, true, true);
auto x_grad_tmp = left_cumprod * right_cumprod;
auto x_grad_reshape = reshape<T>(x_grad_tmp, transpose_shape);
auto x_grad_tmp2 = transpose<T>(x_grad_reshape, origin_position);
auto x_grad_res = x_grad_tmp2 * out_grad_tmp;
set_output<T>(x_grad_res, x_grad);
}
}
}

Expand Down
11 changes: 10 additions & 1 deletion paddle/phi/kernels/cpu/cumprod_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,16 @@ void CumprodKernel(const Context& dev_ctx,
DenseTensor* out) {
const DenseTensor* x = &input;
auto* x_data = x->data<T>();
auto* out_data = dev_ctx.template Alloc<T>(out);
auto* out_ptr = dev_ctx.template Alloc<T>(out);
DDim shape = x->dims();
DenseTensor out_tmp;
T* out_data = nullptr;
if (x_data == out_ptr) {
out_tmp.Resize(shape);
out_data = dev_ctx.template Alloc<T>(&out_tmp);
} else {
out_data = out_ptr;
}

size_t outer_dim = 1;
size_t mid_dim = 1;
Expand Down Expand Up @@ -88,6 +96,7 @@ void CumprodKernel(const Context& dev_ctx,
}
}
}
memcpy(out_ptr, out_data, out->numel() * sizeof(T));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议加上判断:if (x_data == out_ptr)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}

} // namespace phi
Expand Down