Skip to content

Commit b051c4a

Browse files
authored
[0-size Tensor No.84] Add 0-size Tensor support for paddle.incubate.nn.functional.fused_layer_norm [fluid_ops] (#74046)
* Fix * Fix
1 parent 9f30b0b commit b051c4a

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

paddle/phi/kernels/fusion/gpu/fused_layernorm_kernel.cu

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,13 @@ void FusedLayerNormKernel(const Context& dev_ctx,
10441044
}
10451045

10461046
using U = phi::funcs::LayerNormParamType<T>;
1047+
if (x.numel() == 0) {
1048+
dev_ctx.template Alloc<T>(out);
1049+
if (residual_out) dev_ctx.template Alloc<T>(residual_out);
1050+
if (mean) dev_ctx.template Alloc<U>(mean);
1051+
if (variance) dev_ctx.template Alloc<U>(variance);
1052+
return;
1053+
}
10471054
const T* x_data = x.data<T>();
10481055
const U* norm_weight_data =
10491056
norm_weight ? norm_weight.get().data<U>() : nullptr;

test/legacy_test/test_fused_layernorm_op.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,5 +1204,30 @@ def test_residual_bias_add_layernorm(self):
12041204
)
12051205

12061206

1207+
@unittest.skipIf(
1208+
not core.is_compiled_with_cuda() and not paddle.is_compiled_with_rocm(),
1209+
"core is not compiled with CUDA or ROCM",
1210+
)
1211+
class TestlayernormOp_ZeroSize(TestlayernormOp):
1212+
def setUp(self):
1213+
np.random.seed(20)
1214+
# 0-size
1215+
batch = 0
1216+
cols = 256
1217+
1218+
self.x_np = np.random.uniform(-0.05, 0.05, [batch, cols])
1219+
self.residual_np = np.random.uniform(-0.05, 0.05, [batch, cols])
1220+
self.bias_np = np.random.uniform(-0.05, 0.05, [cols])
1221+
self.norm_weight_np = np.random.uniform(-0.05, 0.05, [cols])
1222+
self.norm_bias_np = np.random.uniform(-0.05, 0.05, [cols])
1223+
self.epsilon = 1e-5
1224+
self.residual_alpha = np.random.uniform(low=0.1, high=1.1, size=[1])
1225+
1226+
self.quant_scale = 0.15
1227+
self.quant_round_type = 1
1228+
self.quant_max_bound = 127
1229+
self.quant_min_bound = -127
1230+
1231+
12071232
if __name__ == "__main__":
12081233
unittest.main()

0 commit comments

Comments
 (0)