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

fix the XXX_GRAD_CASE bug by HexToString #32004

Merged
merged 9 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 24 additions & 5 deletions paddle/fluid/operators/expand_as_op.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,25 @@ limitations under the License. */
#include "paddle/fluid/framework/operator.h"

#define MAX_RANK_SUPPORTED 6

// 1. BOOST_PP_REPEAT macro represents a fast horizontal repetition construct.
// Usage: BOOST_PP_REPEAT(count, macro, data).
// This macro expands to the sequence:
// macro(z, 0, data) macro(z, 1, data) ... macro(z, count - 1, data).
// 2. As for our case, count = MAX_RANK_SUPPORTED(which is 6).
// So the range of n is 0-5(which is count-1).
// We want to generate case 1-6 instead of case 0-5.
// So we need to change n to n + 1.
#define EXPAND_AS_TEMPLATE(z, n, data) \
case n + 1: { \
ExpandAs<n + 1>(context); \
break; \
}
#define REP_EXPAND_AS_TEMPLATE(n) BOOST_PP_REPEAT(n, EXPAND_AS_TEMPLATE, ~)
#define COND(n) BOOST_PP_GREATER_EQUAL(n, BOOST_PP_MOD(n, MAX_RANK_SUPPORTED))
#define EXPAND_AS_GRAD_CASE(n) \
case n: { \
ExpandAsBackward<n>(context, reshape_dims_vec, reduce_dims_vec); \
break; \
#define EXPAND_AS_GRAD_CASE(n) \
case n + 1: { \
HexToString marked this conversation as resolved.
Show resolved Hide resolved
ExpandAsBackward<n + 1>(context, reshape_dims_vec, reduce_dims_vec); \
break; \
}
#define EXPAND_AS_GRAD_TEMPLATE(z, n, data) \
BOOST_PP_IF(COND(n), EXPAND_AS_GRAD_CASE(n), )
Expand Down Expand Up @@ -143,6 +150,18 @@ class ExpandAsGradKernel : public framework::OpKernel<T> {
framework::TensorCopy(*in0, context.GetPlace(), context.device_context(),
out0);
} else {
PADDLE_ENFORCE_GE(dims, 1,
platform::errors::InvalidArgument(
"The rank of the input 'Out@GRAD' for "
"expand_as_grad op must be greater than or "
"equal to 1, but the value received is %d.",
dims));
PADDLE_ENFORCE_LE(dims, MAX_RANK_SUPPORTED,
platform::errors::InvalidArgument(
"The rank of the input 'Out@GRAD' for "
"expand_as_grad op must be less than or equal "
"to %d, but the value received is %d.",
MAX_RANK_SUPPORTED, dims));
switch (dims) {
REP_EXPAND_AS_GRAD_TEMPLATE(MAX_RANK_SUPPORTED)
default:
Expand Down
26 changes: 20 additions & 6 deletions paddle/fluid/operators/expand_as_v2_op.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,25 @@ limitations under the License. */
#include "paddle/fluid/framework/operator.h"

#define MAX_RANK_SUPPORTED 6

// 1. BOOST_PP_REPEAT macro represents a fast horizontal repetition construct.
// Usage: BOOST_PP_REPEAT(count, macro, data).
// This macro expands to the sequence:
// macro(z, 0, data) macro(z, 1, data) ... macro(z, count - 1, data).
// 2. As for our case, count = MAX_RANK_SUPPORTED(which is 6).
// So the range of n is 0-5(which is count-1).
// We want to generate case 1-6 instead of case 0-5.
// So we need to change n to n + 1.
#define EXPAND_AS_TEMPLATE(z, n, data) \
case n + 1: { \
ExpandAs<n + 1>(context); \
break; \
}
#define REP_EXPAND_AS_TEMPLATE(n) BOOST_PP_REPEAT(n, EXPAND_AS_TEMPLATE, ~)
#define COND(n) BOOST_PP_GREATER_EQUAL(n, BOOST_PP_MOD(n, MAX_RANK_SUPPORTED))
#define EXPAND_AS_GRAD_CASE(n) \
case n: { \
ExpandAsBackward<n>(context, reshape_dims_vec, reduce_dims_vec); \
break; \
#define EXPAND_AS_GRAD_CASE(n) \
case n + 1: { \
ExpandAsBackward<n + 1>(context, reshape_dims_vec, reduce_dims_vec); \
break; \
}
#define EXPAND_AS_GRAD_TEMPLATE(z, n, data) \
BOOST_PP_IF(COND(n), EXPAND_AS_GRAD_CASE(n), )
Expand Down Expand Up @@ -176,7 +183,14 @@ class ExpandAsV2GradKernel : public framework::OpKernel<T> {
"expand_as_v2_grad op must be less than or equal "
"to %d, but the value received is %d.",
MAX_RANK_SUPPORTED, dims));
switch (dims) { REP_EXPAND_AS_GRAD_TEMPLATE(MAX_RANK_SUPPORTED) }
switch (dims) {
REP_EXPAND_AS_GRAD_TEMPLATE(MAX_RANK_SUPPORTED)
default:
PADDLE_THROW(platform::errors::InvalidArgument(
"Only support tensor with rank being between 1 and 6. But "
"received tensor's rank = %d.",
dims));
}
}
}

Expand Down
26 changes: 20 additions & 6 deletions paddle/fluid/operators/expand_op.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,25 @@ limitations under the License. */
#include "paddle/fluid/framework/operator.h"

#define MAX_RANK_SUPPORTED 6

// 1. BOOST_PP_REPEAT macro represents a fast horizontal repetition construct.
// Usage: BOOST_PP_REPEAT(count, macro, data).
// This macro expands to the sequence:
// macro(z, 0, data) macro(z, 1, data) ... macro(z, count - 1, data).
// 2. As for our case, count = MAX_RANK_SUPPORTED(which is 6).
// So the range of n is 0-5(which is count-1).
// We want to generate case 1-6 instead of case 0-5.
// So we need to change n to n + 1.
#define EXPAND_TEMPLATE(z, n, data) \
case n + 1: { \
Expand<n + 1>(context); \
break; \
}
#define REP_EXPAND_TEMPLATE(n) BOOST_PP_REPEAT(n, EXPAND_TEMPLATE, ~)
#define COND(n) BOOST_PP_GREATER_EQUAL(n, BOOST_PP_MOD(n, MAX_RANK_SUPPORTED))
#define EXPAND_GRAD_CASE(n) \
case n: { \
ExpandBackward<n>(context, reshape_dims_vec, reduce_dims_vec); \
break; \
#define EXPAND_GRAD_CASE(n) \
case n + 1: { \
ExpandBackward<n + 1>(context, reshape_dims_vec, reduce_dims_vec); \
break; \
}
#define EXPAND_GRAD_TEMPLATE(z, n, data) \
BOOST_PP_IF(COND(n), EXPAND_GRAD_CASE(n), )
Expand Down Expand Up @@ -216,7 +223,14 @@ class ExpandGradKernel : public framework::OpKernel<T> {
"for Op(expand_grad) must be less than or equal "
"to %d, but the value received is %d.",
MAX_RANK_SUPPORTED, dims));
switch (dims) { REP_EXPAND_GRAD_TEMPLATE(MAX_RANK_SUPPORTED) }
switch (dims) {
REP_EXPAND_GRAD_TEMPLATE(MAX_RANK_SUPPORTED)
default:
PADDLE_THROW(platform::errors::InvalidArgument(
"Only support tensor with rank being between 1 and 6. But "
"received tensor's rank = %d.",
dims));
}
}
}

Expand Down
26 changes: 20 additions & 6 deletions paddle/fluid/operators/expand_v2_op.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,25 @@ limitations under the License. */
#include "paddle/fluid/framework/operator.h"

#define MAX_RANK_SUPPORTED 6

// 1. BOOST_PP_REPEAT macro represents a fast horizontal repetition construct.
// Usage: BOOST_PP_REPEAT(count, macro, data).
// This macro expands to the sequence:
// macro(z, 0, data) macro(z, 1, data) ... macro(z, count - 1, data).
// 2. As for our case, count = MAX_RANK_SUPPORTED(which is 6).
// So the range of n is 0-5(which is count-1).
// We want to generate case 1-6 instead of case 0-5.
// So we need to change n to n + 1.
#define EXPAND_TEMPLATE(z, n, data) \
case n + 1: { \
Expand<n + 1>(context); \
break; \
}
#define REP_EXPAND_TEMPLATE(n) BOOST_PP_REPEAT(n, EXPAND_TEMPLATE, ~)
#define COND(n) BOOST_PP_GREATER_EQUAL(n, BOOST_PP_MOD(n, MAX_RANK_SUPPORTED))
#define EXPAND_GRAD_CASE(n) \
case n: { \
ExpandBackward<n>(context, reshape_dims_vec, reduce_dims_vec); \
break; \
#define EXPAND_GRAD_CASE(n) \
case n + 1: { \
ExpandBackward<n + 1>(context, reshape_dims_vec, reduce_dims_vec); \
break; \
}
#define EXPAND_GRAD_TEMPLATE(z, n, data) \
BOOST_PP_IF(COND(n), EXPAND_GRAD_CASE(n), )
Expand Down Expand Up @@ -260,7 +267,14 @@ class ExpandV2GradKernel : public framework::OpKernel<T> {
"expand_v2_grad op must be less than or equal "
"to %d, but the value received is %d.",
MAX_RANK_SUPPORTED, dims));
switch (dims) { REP_EXPAND_GRAD_TEMPLATE(MAX_RANK_SUPPORTED) }
switch (dims) {
REP_EXPAND_GRAD_TEMPLATE(MAX_RANK_SUPPORTED)
default:
PADDLE_THROW(platform::errors::InvalidArgument(
"Only support tensor with rank being between 1 and 6. But "
"received tensor's rank = %d.",
dims));
}
}
}

Expand Down
17 changes: 12 additions & 5 deletions paddle/fluid/operators/meshgrid_op.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@
#include "paddle/fluid/platform/errors.h"

#define MAX_RANK_SUPPORTED 6

// 1. BOOST_PP_REPEAT macro represents a fast horizontal repetition construct.
// Usage: BOOST_PP_REPEAT(count, macro, data).
// This macro expands to the sequence:
// macro(z, 0, data) macro(z, 1, data) ... macro(z, count - 1, data).
// 2. As for our case, count = MAX_RANK_SUPPORTED(which is 6).
// So the range of n is 0-5(which is count-1).
// We want to generate case 1-6 instead of case 0-5.
// So we need to change n to n + 1.
#define MESHGRID_TEMPLATE(z, n, data) \
case n + 1: { \
MeshgridForward<n + 1>(context); \
Expand All @@ -37,10 +44,10 @@
#define REP_MESHGRID_TEMPLATE(n) BOOST_PP_REPEAT(n, MESHGRID_TEMPLATE, ~)
#define COND(n) BOOST_PP_GREATER_EQUAL(n, BOOST_PP_MOD(n, MAX_RANK_SUPPORTED))

#define MESHGRID_GRAD_CASE(n) \
case n: { \
MeshgridBackward<n>(context); \
break; \
#define MESHGRID_GRAD_CASE(n) \
case n + 1: { \
MeshgridBackward<n + 1>(context); \
break; \
}
#define MESHGRID_GRAD_TEMPLATE(z, n, data) \
BOOST_PP_IF(COND(n), MESHGRID_GRAD_CASE(n), )
Expand Down
26 changes: 20 additions & 6 deletions paddle/fluid/operators/tile_op.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,25 @@ limitations under the License. */
#include "paddle/fluid/framework/operator.h"

#define MAX_RANK_SUPPORTED 6

// 1. BOOST_PP_REPEAT macro represents a fast horizontal repetition construct.
// Usage: BOOST_PP_REPEAT(count, macro, data).
// This macro expands to the sequence:
// macro(z, 0, data) macro(z, 1, data) ... macro(z, count - 1, data).
// 2. As for our case, count = MAX_RANK_SUPPORTED(which is 6).
// So the range of n is 0-5(which is count-1).
// We want to generate case 1-6 instead of case 0-5.
// So we need to change n to n + 1.
#define TILE_TEMPLATE(z, n, data) \
case n + 1: { \
Tile<n + 1>(context); \
break; \
}
#define REP_TILE_TEMPLATE(n) BOOST_PP_REPEAT(n, TILE_TEMPLATE, ~)
#define COND(n) BOOST_PP_GREATER_EQUAL(n, BOOST_PP_MOD(n, MAX_RANK_SUPPORTED))
#define TILE_GRAD_CASE(n) \
case n: { \
TileBackward<n>(context, reshape_dims_vec, reduce_dims_vec); \
break; \
#define TILE_GRAD_CASE(n) \
case n + 1: { \
TileBackward<n + 1>(context, reshape_dims_vec, reduce_dims_vec); \
break; \
}
#define TILE_GRAD_TEMPLATE(z, n, data) BOOST_PP_IF(COND(n), TILE_GRAD_CASE(n), )
#define REP_TILE_GRAD_TEMPLATE(n) BOOST_PP_REPEAT(n, TILE_GRAD_TEMPLATE, ~)
Expand Down Expand Up @@ -240,7 +247,14 @@ class TileGradKernel : public framework::OpKernel<T> {
"must be less than or equal "
"to %d, but the value received is %d.",
MAX_RANK_SUPPORTED, dims));
switch (dims) { REP_TILE_GRAD_TEMPLATE(MAX_RANK_SUPPORTED) }
switch (dims) {
REP_TILE_GRAD_TEMPLATE(MAX_RANK_SUPPORTED)
default:
PADDLE_THROW(platform::errors::InvalidArgument(
"Only support tensor with rank being between 1 and 6. But "
"received tensor's rank = %d.",
dims));
}
}
}

Expand Down