@ -22,47 +22,73 @@ class FusedFCElementwiseLayerNormOp : public framework::OperatorWithKernel {
using framework : : OperatorWithKernel : : OperatorWithKernel ;
void InferShape ( framework : : InferShapeContext * ctx ) const override {
PADDLE_ENFORCE_EQ (
ctx - > HasInput ( " X " ) , true ,
" Input(X) of fused_fc_elementwise_layernorm should not be null. " ) ;
PADDLE_ENFORCE_EQ (
ctx - > HasInput ( " W " ) , true ,
" Input(W) of fused_fc_elementwise_layernorm should not be null. " ) ;
PADDLE_ENFORCE_EQ (
ctx - > HasInput ( " Y " ) , true ,
" Input(Y) of fused_fc_elementwise_layernorm should not be null. " ) ;
PADDLE_ENFORCE_EQ (
ctx - > HasOutput ( " Out " ) , true ,
" Output(Out) of fused_fc_elementwise_layernorm should not be null. " ) ;
OP_INOUT_CHECK ( ctx - > HasInput ( " X " ) , " Input " , " X " ,
" FusedFcElementwiseLayernorm " ) ;
OP_INOUT_CHECK ( ctx - > HasInput ( " W " ) , " Input " , " W " ,
" FusedFcElementwiseLayernorm " ) ;
OP_INOUT_CHECK ( ctx - > HasInput ( " Y " ) , " Input " , " Y " ,
" FusedFcElementwiseLayernorm " ) ;
OP_INOUT_CHECK ( ctx - > HasOutput ( " Out " ) , " Output " , " Out " ,
" FusedFcElementwiseLayernorm " ) ;
auto w_dims = ctx - > GetInputDim ( " W " ) ;
PADDLE_ENFORCE_EQ ( w_dims . size ( ) , 2 ,
" Fully Connected input should be 2-D tensor. " ) ;
PADDLE_ENFORCE_EQ (
w_dims . size ( ) , 2 ,
platform : : errors : : InvalidArgument (
" The input Weight of fc is expected to be a 2-D tensor. "
" But received the number of Weight's dimensions is %d, " ,
" Weight's shape is %s. " , w_dims . size ( ) , w_dims ) ) ;
if ( ctx - > HasInput ( " Bias0 " ) ) {
auto bias0_dims = ctx - > GetInputDim ( " Bias0 " ) ;
PADDLE_ENFORCE_LE ( bias0_dims . size ( ) , 2 ,
platform : : errors : : InvalidArgument (
" The input Bias of fc is expected to be an 1-D or "
" 2-D tensor. But received the number of Bias's "
" dimensions is %d, Bias's shape is %s. " ,
bias0_dims . size ( ) , bias0_dims ) ) ;
PADDLE_ENFORCE_EQ (
bias0_dims [ bias0_dims . size ( ) - 1 ] , w_dims [ 1 ] ,
platform : : errors : : InvalidArgument (
" The last dimension of input Bias is expected be equal "
" to the actual width of input Weight. But received the last "
" dimension of Bias is %d, Bias's shape is %s; "
" the actual width of Weight is %d, Weight's shape is %s. " ,
bias0_dims [ bias0_dims . size ( ) - 1 ] , bias0_dims , w_dims [ 1 ] ,
w_dims ) ) ;
if ( bias0_dims . size ( ) = = 2 ) {
PADDLE_ENFORCE_EQ ( bias0_dims [ 0 ] , 1 ,
" The shape of Bias must be [1, dim]. " ) ;
PADDLE_ENFORCE_EQ ( bias0_dims [ 1 ] , w_dims [ 1 ] ,
" The shape of Bias must be [1, dim]. " ) ;
} else if ( bias0_dims . size ( ) = = 1 ) {
PADDLE_ENFORCE_EQ ( bias0_dims [ 0 ] , w_dims [ 1 ] ,
" The shape of Bias must be [1, dim]. " ) ;
PADDLE_ENFORCE_EQ (
bias0_dims [ 0 ] , 1 ,
platform : : errors : : InvalidArgument (
" The first dimension of input Bias is expected to be 1, "
" but received %d, Bias's shape is %s. " ,
bias0_dims [ 0 ] , bias0_dims ) ) ;
}
}
auto x_dims = ctx - > GetInputDim ( " X " ) ;
int x_num_col_dims = ctx - > Attrs ( ) . Get < int > ( " x_num_col_dims " ) ;
PADDLE_ENFORCE_GT (
x_dims . size ( ) , x_num_col_dims ,
" The input tensor Input's rank of FCOp should be larger than "
" in_num_col_dims. " ) ;
PADDLE_ENFORCE_LT (
x_num_col_dims , x_dims . size ( ) ,
platform : : errors : : InvalidArgument (
" The attribute x_num_col_dims used to flatten input X to "
" a 2-D tensor, is expected to be less than the number of "
" input X's dimensions. But recieved x_num_col_dims is %d, "
" the number of input X's dimensions is %d, input X's shape is %s. " ,
x_num_col_dims , x_dims . size ( ) , x_dims ) ) ;
auto x_mat_dims = framework : : flatten_to_2d ( x_dims , x_num_col_dims ) ;
PADDLE_ENFORCE_EQ (
x_mat_dims [ 1 ] , w_dims [ 0 ] ,
" Fully Connected input and weigth size do not match. %s, %s " ) ;
platform : : errors : : InvalidArgument (
" The input's second dimension and weight's first dimension is "
" expected to be the same. But recieved input's second dimension is "
" %d, input's shape is %s; weight's first dimension is %d, weight's "
" shape is %s. " ,
x_mat_dims [ 1 ] , x_mat_dims , w_dims [ 0 ] , w_dims ) ) ;
std : : vector < int64_t > fc_out_dims ;
for ( int i = 0 ; i < x_num_col_dims ; + + i ) {
@ -71,29 +97,67 @@ class FusedFCElementwiseLayerNormOp : public framework::OperatorWithKernel {
fc_out_dims . push_back ( w_dims [ 1 ] ) ;
auto y_dims = ctx - > GetInputDim ( " Y " ) ;
PADDLE_ENFORCE_EQ ( framework : : make_ddim ( fc_out_dims ) , y_dims ) ;
PADDLE_ENFORCE_EQ ( framework : : make_ddim ( fc_out_dims ) , y_dims ,
platform : : errors : : InvalidArgument (
" The output's shape of fc is expected to be equal to "
" that of input Y. But recieved output's shape of fc "
" is %s, input Y's shape is %s. " ,
framework : : make_ddim ( fc_out_dims ) , y_dims ) ) ;
auto begin_norm_axis = ctx - > Attrs ( ) . Get < int > ( " begin_norm_axis " ) ;
PADDLE_ENFORCE_LT (
begin_norm_axis , y_dims . size ( ) ,
" 'begin_norm_axis' must be less than the rank of Input(Y). " ) ;
platform : : errors : : InvalidArgument (
" The attribute begin_norm_axis used to flatten input Y to a 2-D "
" tensor, is expected to be less than the number of input Y's "
" dimensions. But recieved begin_norm_axis is %d, the number of "
" input Y's dimensions is %d, input Y's shape is %s. " ,
begin_norm_axis , y_dims . size ( ) , y_dims ) ) ;
auto y_mat_dim = framework : : flatten_to_2d ( y_dims , begin_norm_axis ) ;
int64_t dim_0 = y_mat_dim [ 0 ] ;
int64_t dim_1 = y_mat_dim [ 1 ] ;
if ( ctx - > HasInput ( " Scale " ) ) {
PADDLE_ENFORCE_EQ ( ctx - > GetInputDim ( " Scale " ) . size ( ) , 1 ) ;
auto scale_dims = ctx - > GetInputDim ( " Scale " ) ;
PADDLE_ENFORCE_EQ ( scale_dims . size ( ) , 1 ,
platform : : errors : : InvalidArgument (
" The input Scale is expected to be an 1-D tensor. "
" But recieved the number of input Scale's "
" dimensions is %d, input Scale's shape is %s. " ,
scale_dims . size ( ) , scale_dims ) ) ;
if ( ctx - > IsRuntime ( ) ) {
PADDLE_ENFORCE_EQ ( ctx - > GetInputDim ( " Scale " ) [ 0 ] , dim_1 ,
" scale should with right " ) ;
PADDLE_ENFORCE_EQ (
scale_dims [ 0 ] , dim_1 ,
platform : : errors : : InvalidArgument (
" The first dimension of input Scale is expected to be equal to "
" the second dimension of input Y after flattened. "
" But recieved the first dimension of input Scale is %d, input "
" Scale's shape is %s; the second dimension of flattened input "
" Y is %d, input Y's shape is %s, flattened axis is %d. " ,
scale_dims [ 0 ] , scale_dims , dim_1 , y_dims , begin_norm_axis ) ) ;
}
}
if ( ctx - > HasInput ( " Bias1 " ) ) {
PADDLE_ENFORCE_EQ ( ctx - > GetInputDim ( " Bias1 " ) . size ( ) , 1 ) ;
auto bias1_dims = ctx - > GetInputDim ( " Bias1 " ) ;
PADDLE_ENFORCE_EQ (
bias1_dims . size ( ) , 1 ,
platform : : errors : : InvalidArgument (
" The input Bias1 is expected to be an 1-D tensor. "
" But recieved the number of input Bias1's dimension is %d, "
" input Bias1's shape is %s. " ,
bias1_dims . size ( ) , bias1_dims ) ) ;
if ( ctx - > IsRuntime ( ) ) {
PADDLE_ENFORCE_EQ ( ctx - > GetInputDim ( " Bias1 " ) [ 0 ] , dim_1 ,
" bias should with right " ) ;
PADDLE_ENFORCE_EQ (
bias1_dims [ 0 ] , dim_1 ,
platform : : errors : : InvalidArgument (
" The first dimension of input Bias1 is expected to be equal to "
" the second dimension of input Y after flattened. "
" But recieved the first dimension of input Bias1 is %d, input "
" Bias1's shape is %s; the second dimension of flatten input "
" Y is %d, input Y's shape is %s, flattened axis is %d. " ,
bias1_dims [ 0 ] , bias1_dims , dim_1 , y_dims , begin_norm_axis ) ) ;
}
}