test_fused_dense.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import math
  2. import torch
  3. import torch.nn.functional as F
  4. import pytest
  5. from einops import rearrange
  6. from flash_attn.ops.fused_dense import FusedDense, FusedDenseGeluDense
  7. @pytest.mark.parametrize('dtype', [torch.float16, torch.bfloat16])
  8. @pytest.mark.parametrize('return_residual', [False, True])
  9. @pytest.mark.parametrize('has_bias', [True, False])
  10. @pytest.mark.parametrize('out_features', [1024, 4096])
  11. @pytest.mark.parametrize('in_features', [1024, 4096])
  12. def test_fused_linear_bias(in_features, out_features, has_bias, return_residual, dtype):
  13. device = 'cuda'
  14. rtol, atol = (3e-3, 1e-2) if dtype == torch.bfloat16 else (3e-3, 1e-3)
  15. # set seed
  16. torch.random.manual_seed(0)
  17. batch_size = 8
  18. seqlen = 512
  19. x_pt = torch.randn(batch_size, seqlen, in_features, device=device, dtype=dtype,
  20. requires_grad=True)
  21. x = x_pt.detach().clone().requires_grad_()
  22. model_pt = torch.nn.Linear(in_features, out_features, bias=has_bias, device=device, dtype=dtype)
  23. model = FusedDense(in_features, out_features, bias=has_bias, return_residual=return_residual,
  24. device=device, dtype=dtype)
  25. with torch.no_grad():
  26. model.weight.copy_(model_pt.weight)
  27. if has_bias:
  28. model.bias.copy_(model_pt.bias)
  29. out_pt = model_pt(x_pt)
  30. if not return_residual:
  31. out = model(x)
  32. else:
  33. out, x_copy = model(x)
  34. x_copy = (x_copy[..., :out_features] if out_features < in_features
  35. else F.pad(x_copy, (0, out_features - in_features)))
  36. x_pt_copy = (x_pt[..., :out_features] if out_features < in_features
  37. else F.pad(x_pt, (0, out_features - in_features)))
  38. # Just add some random function of the residual
  39. out_pt = out_pt + F.gelu(x_pt_copy)
  40. out = out + F.gelu(x_copy)
  41. # with torch.no_grad():
  42. # out_fl = F.linear(x_pt.float(), model.weight.float(), model.bias.float()).half()
  43. assert torch.allclose(out, out_pt, rtol=rtol, atol=atol)
  44. # If we don't divide by batch_size, the gradient gets a bit too large.
  45. g = torch.randn_like(out) / 32
  46. out_pt.backward(g)
  47. out.backward(g)
  48. assert torch.allclose(x.grad, x_pt.grad, rtol=rtol, atol=atol)
  49. # The error for d_weight and d_bias is quite a bit higher
  50. assert torch.allclose(model.weight.grad, model_pt.weight.grad, rtol=rtol, atol=atol * 10)
  51. if has_bias:
  52. assert torch.allclose(model.bias.grad, model_pt.bias.grad, rtol=rtol, atol=atol * 5)
  53. @pytest.mark.parametrize('dtype', [torch.float16, torch.bfloat16])
  54. @pytest.mark.parametrize('heuristic', [0, -1])
  55. @pytest.mark.parametrize('checkpoint_lvl', [0, 1, 2])
  56. @pytest.mark.parametrize('return_residual', [False, True])
  57. @pytest.mark.parametrize('has_bias2', [True, False])
  58. @pytest.mark.parametrize('has_bias1', [True, False])
  59. @pytest.mark.parametrize('out_features', [1024, 4096])
  60. @pytest.mark.parametrize('in_features', [1024, 4096])
  61. def test_fused_dense_gelu_dense(in_features, out_features, has_bias1, has_bias2, return_residual,
  62. checkpoint_lvl, heuristic, dtype):
  63. device = 'cuda'
  64. rtol, atol = (3e-3, 3e-2) if dtype == torch.bfloat16 else (3e-3, 1e-3)
  65. # set seed
  66. torch.random.manual_seed(0)
  67. batch_size = 8
  68. seqlen = 512
  69. x_pt = torch.randn(batch_size, seqlen, in_features, device=device, dtype=dtype,
  70. requires_grad=True)
  71. x = x_pt.detach().clone().requires_grad_()
  72. model_pt_fc1 = torch.nn.Linear(in_features, out_features, bias=has_bias1, device=device,
  73. dtype=dtype)
  74. model_pt_fc2 = torch.nn.Linear(out_features, in_features, bias=has_bias2, device=device,
  75. dtype=dtype)
  76. model = FusedDenseGeluDense(in_features, out_features, in_features, bias1=has_bias1,
  77. bias2=has_bias2, return_residual=return_residual,
  78. checkpoint_lvl=checkpoint_lvl, heuristic=heuristic,
  79. device=device, dtype=dtype)
  80. with torch.no_grad():
  81. model.fc1.weight.copy_(model_pt_fc1.weight)
  82. if has_bias1:
  83. model.fc1.bias.copy_(model_pt_fc1.bias)
  84. model.fc2.weight.copy_(model_pt_fc2.weight)
  85. if has_bias2:
  86. model.fc2.bias.copy_(model_pt_fc2.bias)
  87. out_pt = model_pt_fc2(F.gelu(model_pt_fc1(x_pt), approximate='tanh'))
  88. if not return_residual:
  89. out = model(x)
  90. else:
  91. out, x_copy = model(x)
  92. # Just add some random function of the residual
  93. out_pt = out_pt + F.gelu(x_pt)
  94. out = out + F.gelu(x_copy)
  95. assert torch.allclose(out, out_pt, rtol=rtol, atol=atol)
  96. # If we don't divide by batch_size, the gradient gets a bit too large.
  97. g = torch.randn_like(out) / 32
  98. out_pt.backward(g)
  99. out.backward(g)
  100. assert torch.allclose(x.grad, x_pt.grad, rtol=rtol, atol=atol)
  101. # The error for d_weight and d_bias is quite a bit higher
  102. assert torch.allclose(model.fc1.weight.grad, model_pt_fc1.weight.grad, rtol=rtol, atol=atol * 10)
  103. if has_bias1:
  104. assert torch.allclose(model.fc1.bias.grad, model_pt_fc1.bias.grad, rtol=rtol, atol=atol * 5)
  105. assert torch.allclose(model.fc2.weight.grad, model_pt_fc2.weight.grad, rtol=rtol, atol=atol * 10)
  106. if has_bias2:
  107. assert torch.allclose(model.fc2.bias.grad, model_pt_fc2.bias.grad, rtol=rtol, atol=atol * 5)