From f74f97624d4c4ea5fbed7ca497872f46c92431cd Mon Sep 17 00:00:00 2001 From: Thomas Young <35565423+HexToString@users.noreply.github.com> Date: Thu, 8 Apr 2021 13:19:45 +0800 Subject: [PATCH] fix the XXX_GRAD_CASE bug by HexToString (#32004) --- paddle/fluid/operators/expand_as_op.h | 29 ++++++++++++++++++++---- paddle/fluid/operators/expand_as_v2_op.h | 26 ++++++++++++++++----- paddle/fluid/operators/expand_op.h | 26 ++++++++++++++++----- paddle/fluid/operators/expand_v2_op.h | 26 ++++++++++++++++----- paddle/fluid/operators/meshgrid_op.h | 17 ++++++++++---- paddle/fluid/operators/tile_op.h | 26 ++++++++++++++++----- 6 files changed, 116 insertions(+), 34 deletions(-) mode change 100644 => 100755 paddle/fluid/operators/expand_as_op.h mode change 100644 => 100755 paddle/fluid/operators/expand_as_v2_op.h mode change 100644 => 100755 paddle/fluid/operators/expand_op.h mode change 100644 => 100755 paddle/fluid/operators/expand_v2_op.h mode change 100644 => 100755 paddle/fluid/operators/meshgrid_op.h mode change 100644 => 100755 paddle/fluid/operators/tile_op.h diff --git a/paddle/fluid/operators/expand_as_op.h b/paddle/fluid/operators/expand_as_op.h old mode 100644 new mode 100755 index 4cefadb24ec..406455af741 --- a/paddle/fluid/operators/expand_as_op.h +++ b/paddle/fluid/operators/expand_as_op.h @@ -25,7 +25,14 @@ limitations under the License. */ #include "paddle/fluid/operators/eigen/eigen_function.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(context); \ @@ -33,10 +40,10 @@ limitations under the License. */ } #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(context, reshape_dims_vec, reduce_dims_vec); \ - break; \ +#define EXPAND_AS_GRAD_CASE(n) \ + case n + 1: { \ + ExpandAsBackward(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), ) @@ -145,6 +152,18 @@ class ExpandAsGradKernel : public framework::OpKernel { 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: diff --git a/paddle/fluid/operators/expand_as_v2_op.h b/paddle/fluid/operators/expand_as_v2_op.h old mode 100644 new mode 100755 index 441dd353804..6df4c592378 --- a/paddle/fluid/operators/expand_as_v2_op.h +++ b/paddle/fluid/operators/expand_as_v2_op.h @@ -26,7 +26,14 @@ limitations under the License. */ #include "paddle/fluid/operators/eigen/eigen_function.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(context); \ @@ -34,10 +41,10 @@ limitations under the License. */ } #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(context, reshape_dims_vec, reduce_dims_vec); \ - break; \ +#define EXPAND_AS_GRAD_CASE(n) \ + case n + 1: { \ + ExpandAsBackward(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), ) @@ -178,7 +185,14 @@ class ExpandAsV2GradKernel : public framework::OpKernel { "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)); + } } } diff --git a/paddle/fluid/operators/expand_op.h b/paddle/fluid/operators/expand_op.h old mode 100644 new mode 100755 index abd525497d6..e1a1ce0a817 --- a/paddle/fluid/operators/expand_op.h +++ b/paddle/fluid/operators/expand_op.h @@ -28,7 +28,14 @@ limitations under the License. */ #include "paddle/fluid/operators/eigen/eigen_function.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(context); \ @@ -36,10 +43,10 @@ limitations under the License. */ } #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(context, reshape_dims_vec, reduce_dims_vec); \ - break; \ +#define EXPAND_GRAD_CASE(n) \ + case n + 1: { \ + ExpandBackward(context, reshape_dims_vec, reduce_dims_vec); \ + break; \ } #define EXPAND_GRAD_TEMPLATE(z, n, data) \ BOOST_PP_IF(COND(n), EXPAND_GRAD_CASE(n), ) @@ -219,7 +226,14 @@ class ExpandGradKernel : public framework::OpKernel { "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)); + } } } diff --git a/paddle/fluid/operators/expand_v2_op.h b/paddle/fluid/operators/expand_v2_op.h old mode 100644 new mode 100755 index af5fdf22cd9..8a87a067c51 --- a/paddle/fluid/operators/expand_v2_op.h +++ b/paddle/fluid/operators/expand_v2_op.h @@ -29,7 +29,14 @@ limitations under the License. */ #include "paddle/fluid/operators/eigen/eigen_function.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(context); \ @@ -37,10 +44,10 @@ limitations under the License. */ } #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(context, reshape_dims_vec, reduce_dims_vec); \ - break; \ +#define EXPAND_GRAD_CASE(n) \ + case n + 1: { \ + ExpandBackward(context, reshape_dims_vec, reduce_dims_vec); \ + break; \ } #define EXPAND_GRAD_TEMPLATE(z, n, data) \ BOOST_PP_IF(COND(n), EXPAND_GRAD_CASE(n), ) @@ -263,7 +270,14 @@ class ExpandV2GradKernel : public framework::OpKernel { "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)); + } } } diff --git a/paddle/fluid/operators/meshgrid_op.h b/paddle/fluid/operators/meshgrid_op.h old mode 100644 new mode 100755 index 345e007de4a..2aad894e11d --- a/paddle/fluid/operators/meshgrid_op.h +++ b/paddle/fluid/operators/meshgrid_op.h @@ -29,7 +29,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(context); \ @@ -38,10 +45,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(context); \ - break; \ +#define MESHGRID_GRAD_CASE(n) \ + case n + 1: { \ + MeshgridBackward(context); \ + break; \ } #define MESHGRID_GRAD_TEMPLATE(z, n, data) \ BOOST_PP_IF(COND(n), MESHGRID_GRAD_CASE(n), ) diff --git a/paddle/fluid/operators/tile_op.h b/paddle/fluid/operators/tile_op.h old mode 100644 new mode 100755 index 4bbde8d08e0..1fb0fa6ce51 --- a/paddle/fluid/operators/tile_op.h +++ b/paddle/fluid/operators/tile_op.h @@ -29,7 +29,14 @@ limitations under the License. */ #include "paddle/fluid/operators/eigen/eigen_function.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(context); \ @@ -37,10 +44,10 @@ limitations under the License. */ } #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(context, reshape_dims_vec, reduce_dims_vec); \ - break; \ +#define TILE_GRAD_CASE(n) \ + case n + 1: { \ + TileBackward(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, ~) @@ -243,7 +250,14 @@ class TileGradKernel : public framework::OpKernel { "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)); + } } } -- GitLab