test_cross_entropy.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Copyright (c) 2024, Tri Dao.
  2. import pytest
  3. import torch
  4. import torch.nn.functional as F
  5. from flash_attn.losses.cross_entropy import CrossEntropyLoss
  6. is_sm8x = torch.cuda.get_device_capability("cuda")[0] >= 8
  7. @pytest.mark.parametrize(
  8. "dtype", [torch.float16, torch.float32] + ([torch.bfloat16] if is_sm8x else [])
  9. )
  10. # @pytest.mark.parametrize("dtype", [torch.float16])
  11. @pytest.mark.parametrize("precompute_lse", [False, True])
  12. # @pytest.mark.parametrize("precompute_lse", [False])
  13. @pytest.mark.parametrize("inplace_backward", [False, True])
  14. # @pytest.mark.parametrize("inplace_backward", [False])
  15. @pytest.mark.parametrize("lse_square_scale", [0.0, 1e-2])
  16. @pytest.mark.parametrize("return_z_loss", [False, True])
  17. # @pytest.mark.parametrize("lse_square_scale", [1e-2])
  18. @pytest.mark.parametrize("logit_scale", [1.0, 0.7])
  19. # @pytest.mark.parametrize("logit_scale", [1.0])
  20. @pytest.mark.parametrize("smoothing", [0.0, 0.9])
  21. # @pytest.mark.parametrize("smoothing", [0.0])
  22. @pytest.mark.parametrize("vocab_size", [50257, 128256]) # test vocab larger than 64k for split
  23. # @pytest.mark.parametrize("vocab_size", [12])
  24. def test_cross_entropy_loss(
  25. vocab_size,
  26. smoothing,
  27. logit_scale,
  28. lse_square_scale,
  29. return_z_loss,
  30. inplace_backward,
  31. precompute_lse,
  32. dtype,
  33. ):
  34. if precompute_lse and (logit_scale != 1.0 or smoothing != 0.0):
  35. pytest.skip("precompute_lse only works with logit_scale=1.0 and smoothing=0.0")
  36. device = "cuda"
  37. rtol, atol = (1e-5, 1e-6) if dtype == torch.float32 else (1e-3, 1e-4)
  38. # set seed
  39. torch.random.manual_seed(0)
  40. batch_size = 1 if dtype == torch.float32 else 4 # Otherwise OOM
  41. seqlen = 4096 if lse_square_scale == 0.0 and logit_scale == 1.0 else 1024 # Otherwise OOM
  42. x_pt = torch.randn(
  43. batch_size * seqlen, vocab_size, device=device, dtype=dtype, requires_grad=True
  44. )
  45. x = x_pt.detach().clone().requires_grad_()
  46. y = torch.randint(0, vocab_size, (batch_size * seqlen,), dtype=torch.long, device=device)
  47. if batch_size * seqlen > 10:
  48. y[torch.randperm(batch_size * seqlen)[:10]] = -100
  49. model_pt = torch.nn.CrossEntropyLoss(label_smoothing=smoothing)
  50. model = CrossEntropyLoss(
  51. label_smoothing=smoothing,
  52. logit_scale=logit_scale,
  53. lse_square_scale=lse_square_scale,
  54. return_z_loss=return_z_loss,
  55. inplace_backward=inplace_backward,
  56. )
  57. if precompute_lse:
  58. with torch.no_grad():
  59. lse = torch.logsumexp(x.float(), dim=-1)
  60. else:
  61. lse = None
  62. if return_z_loss:
  63. out, out_z_loss = model(x, y, precomputed_lse=lse)
  64. else:
  65. out = model(x, y, precomputed_lse=lse)
  66. x_pt_scaled = (x_pt.float() * logit_scale) if logit_scale != 1.0 else x_pt.float()
  67. out_pt = model_pt(x_pt_scaled, y)
  68. if lse_square_scale > 0.0:
  69. lse_pt = torch.logsumexp(x_pt_scaled, dim=-1)
  70. z_loss_pt = lse_square_scale * (lse_pt[y != -100] ** 2).mean()
  71. if return_z_loss:
  72. assert torch.allclose(out_z_loss, z_loss_pt, rtol=rtol, atol=atol)
  73. out_pt += z_loss_pt
  74. assert torch.allclose(out, out_pt, rtol=1e-5, atol=1e-6)
  75. g = torch.randn_like(out)
  76. out_pt.backward(g)
  77. out.backward(g)
  78. assert torch.allclose(x.grad, x_pt.grad, rtol=rtol, atol=atol)