|
| 1 | +import paddle |
| 2 | +import paddle.incubate.nn.functional as F |
| 3 | +import FusedQuantOps as FQO |
| 4 | +import numpy as np |
| 5 | + |
| 6 | + |
| 7 | +def test_fused_spaq(height, width): |
| 8 | + print(f'test_fused_spaq', (height, width)) |
| 9 | + |
| 10 | + x = paddle.randn([height, width], dtype='bfloat16').clip_(min=-50, max=50) |
| 11 | + prob = paddle.randn([height, 1]).astype("float32") |
| 12 | + |
| 13 | + out, scale = FQO.fused_spaq(x, prob, using_pow2_scaling=False) |
| 14 | + paddle.base.core.eager._for_test_check_cuda_error() |
| 15 | + |
| 16 | + out_golden = F.swiglu(x) * prob |
| 17 | + out_dequant = ( |
| 18 | + out.astype('float32') * |
| 19 | + scale.repeat_interleave(128, axis=1)[:, :out.shape[-1]] |
| 20 | + ) |
| 21 | + |
| 22 | + np.testing.assert_allclose(out_dequant, out_golden, atol=1, rtol=0.01) |
| 23 | + |
| 24 | + |
| 25 | +def test_fused_act_quant_dequant(height, width): |
| 26 | + print(f"test_fused_act_quant_dequant width:{width}, height:{height}") |
| 27 | + |
| 28 | + x = paddle.randn([height, width], dtype="bfloat16").clip_(min=-50, max=50) |
| 29 | + |
| 30 | + x_fp8, scale = FQO.fused_act_quant( |
| 31 | + x, |
| 32 | + transpose_output=False, |
| 33 | + padding_last_dim_to_8x=False, |
| 34 | + using_pow2_scaling=False |
| 35 | + ) |
| 36 | + paddle.base.core.eager._for_test_check_cuda_error() |
| 37 | + |
| 38 | + x_dequant = FQO.fused_act_dequant(x_fp8, scale) |
| 39 | + paddle.base.core.eager._for_test_check_cuda_error() |
| 40 | + |
| 41 | + x = x.astype('float32') |
| 42 | + x_dequant = x_dequant.astype('float32') |
| 43 | + np.testing.assert_allclose(x, x_dequant, atol=1, rtol=0.01) |
| 44 | + |
| 45 | + |
| 46 | +def test_fused_swiglu_probs_bwd(topk, seq_len, moe_intermediate_size): |
| 47 | + print(f'test_fused_swiglu_probs_bwd topk:{topk} seq_len:{seq_len} moe_intermediate_size:{moe_intermediate_size}') |
| 48 | + |
| 49 | + o1 = paddle.rand([topk, seq_len, moe_intermediate_size * 2], dtype="bfloat16") |
| 50 | + unzipped_probs = paddle.rand([ topk, seq_len, 1], dtype="float32") |
| 51 | + do2_s = paddle.rand([topk, seq_len , moe_intermediate_size], dtype="bfloat16") |
| 52 | + |
| 53 | + do1, pg, o2_s = FQO.fused_swiglu_probs_bwd(o1, do2_s, unzipped_probs) |
| 54 | + paddle.base.core.eager._for_test_check_cuda_error() |
| 55 | + |
| 56 | + def fn_gold(): |
| 57 | + o2 = F.swiglu(o1) |
| 58 | + o2_s = (o2 * unzipped_probs) |
| 59 | + do2 = (do2_s.cast(paddle.float32) * unzipped_probs) |
| 60 | + do2 = do2.cast(paddle.bfloat16) |
| 61 | + do1, _ = paddle._C_ops.swiglu_grad(o1, None, do2) |
| 62 | + probs_grad = (do2_s.cast(paddle.float32) * (o2.cast(paddle.float32))).sum(axis=-1) |
| 63 | + return do1, probs_grad, o2_s |
| 64 | + |
| 65 | + do1_gold, pg_gold, o2_s_gold = fn_gold() |
| 66 | + |
| 67 | + np.testing.assert_allclose(do1.astype('float32'), do1_gold.astype('float32'), atol=1e-2, rtol=1e-2) |
| 68 | + np.testing.assert_allclose(pg, pg_gold.flatten(), atol=1e-2, rtol=1e-3) |
| 69 | + np.testing.assert_allclose(o2_s.astype('float32'), o2_s_gold, atol=1e-2, rtol=1e-2) |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == '__main__': |
| 73 | + for height in [8192, 16384, 32768, 128000, 510336]: |
| 74 | + for width in [4096, 7168]: |
| 75 | + test_fused_spaq(width, height) |
| 76 | + |
| 77 | + for height in [4096, 16384, 32768, 128000, 510336]: |
| 78 | + for width in [4096, 7168]: |
| 79 | + test_fused_act_quant_dequant(height, width) |
| 80 | + |
| 81 | + for topk in [8]: |
| 82 | + for seq_len in [4096, 7168]: |
| 83 | + for moe_intermediate_size in [2048, 20480, 40960]: |
| 84 | + test_fused_swiglu_probs_bwd(topk, seq_len, moe_intermediate_size) |
0 commit comments