Skip to content

Commit a4cda6b

Browse files
authored
【Hackathon 9th No.3】Add bias check for fused_layer_norm (#74851)
* Add bias check for fused_layer_norm * refine code
1 parent 7947517 commit a4cda6b

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

paddle/phi/infermeta/multiary.cc

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2647,6 +2647,33 @@ void FusedLayerNormInferMeta(const MetaTensor& x,
26472647
x_dims_vec[i],
26482648
residual_dims_vec[i]));
26492649
}
2650+
if (bias) {
2651+
std::vector<int64_t> bias_dims_vec = common::vectorize(bias.dims());
2652+
PADDLE_ENFORCE_EQ(
2653+
x_dims_size - begin_norm_axis,
2654+
bias_dims_vec.size(),
2655+
common::errors::InvalidArgument(
2656+
"The normalized size of Input(X) must be equal to the size "
2657+
"of Bias, but received normalized size of Input(X) is [%d], "
2658+
"received size of Bias is [%d]",
2659+
x_dims_size - begin_norm_axis,
2660+
bias_dims_vec.size()));
2661+
for (size_t i = begin_norm_axis; i < x_dims_size; ++i) {
2662+
if (x_dims_vec[i] == -1 || bias_dims_vec[i - begin_norm_axis] == -1 ||
2663+
x_dims_vec[i] == 0)
2664+
continue;
2665+
2666+
PADDLE_ENFORCE_EQ(x_dims_vec[i],
2667+
bias_dims_vec[i - begin_norm_axis],
2668+
common::errors::InvalidArgument(
2669+
"The normalized dimension of Input(X) and Bias "
2670+
"must match at axis %d, but received Input(X) "
2671+
"dimension is [%d], Bias dimension is [%d]",
2672+
i,
2673+
x_dims_vec[i],
2674+
bias_dims_vec[i - begin_norm_axis]));
2675+
}
2676+
}
26502677
}
26512678

26522679
int64_t rows = 1;
@@ -2666,6 +2693,18 @@ void FusedLayerNormInferMeta(const MetaTensor& x,
26662693
normalized_dims,
26672694
norm_weight.dims()[0]));
26682695
}
2696+
if (norm_bias) {
2697+
PADDLE_ENFORCE_EQ(
2698+
normalized_dims,
2699+
norm_bias.dims()[0],
2700+
common::errors::InvalidArgument(
2701+
"The normalized size of Input(X) must equal to be "
2702+
"the size of Bias, but received "
2703+
"normalized size of Input(X) is [%d], received size "
2704+
"of Bias is [%d]",
2705+
normalized_dims,
2706+
norm_bias.dims()[0]));
2707+
}
26692708
}
26702709

26712710
auto out_dims = common::make_ddim(x_dims_vec);

test/legacy_test/test_fused_layernorm_op.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,5 +1229,41 @@ def setUp(self):
12291229
self.quant_min_bound = -127
12301230

12311231

1232+
@unittest.skipIf(
1233+
not core.is_compiled_with_cuda() and not paddle.is_compiled_with_rocm(),
1234+
"core is not compiled with CUDA or ROCM",
1235+
)
1236+
class TestFusedLayerNorm_ZeroSize_Error(unittest.TestCase):
1237+
def test_bias_error(self):
1238+
with paddle.base.dygraph.guard():
1239+
x = paddle.randn([16, 256], dtype="float32")
1240+
bias = paddle.randn([0], dtype="float32")
1241+
residual = paddle.rand([16, 256], "float32")
1242+
self.assertRaises(
1243+
ValueError,
1244+
paddle.incubate.nn.functional.fused_layer_norm,
1245+
x=x,
1246+
norm_weight=paddle.randn([256], dtype="float32"),
1247+
norm_bias=paddle.randn([256], dtype="float32"),
1248+
epsilon=1e-06,
1249+
begin_norm_axis=1,
1250+
bias=bias,
1251+
residual=residual,
1252+
)
1253+
1254+
bias = paddle.randn([256], dtype="float32")
1255+
self.assertRaises(
1256+
ValueError,
1257+
paddle.incubate.nn.functional.fused_layer_norm,
1258+
x=x,
1259+
norm_weight=paddle.randn([256], dtype="float32"),
1260+
norm_bias=paddle.randn([0], dtype="float32"),
1261+
epsilon=1e-06,
1262+
begin_norm_axis=1,
1263+
bias=bias,
1264+
residual=residual,
1265+
)
1266+
1267+
12321268
if __name__ == "__main__":
12331269
unittest.main()

0 commit comments

Comments
 (0)