test_cross_entropy_parallel.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # Run test with:
  2. # torchrun --no_python --nproc_per_node=8 pytest -q -s tests/losses/test_cross_entropy_parallel.py
  3. import math
  4. import torch
  5. import torch.nn.functional as F
  6. import pytest
  7. from apex.transformer import parallel_state
  8. from apex.transformer import tensor_parallel
  9. from flash_attn.losses.cross_entropy import CrossEntropyLoss
  10. is_sm8x = torch.cuda.get_device_capability('cuda')[0] >= 8
  11. @pytest.mark.parametrize('dtype', [torch.float16, torch.float32] + ([torch.bfloat16] if is_sm8x else []))
  12. # @pytest.mark.parametrize('dtype', [torch.float16])
  13. @pytest.mark.parametrize('inplace_backward', [False, True])
  14. # @pytest.mark.parametrize('inplace_backward', [False])
  15. @pytest.mark.parametrize('smoothing', [0.0, 0.9])
  16. # @pytest.mark.parametrize('smoothing', [0.9])
  17. @pytest.mark.parametrize('vocab_size', [50264])
  18. @pytest.mark.parametrize('world_size', [1, 2, 4, 8])
  19. # @pytest.mark.parametrize('world_size', [2])
  20. def test_cross_entropy_loss_apex(vocab_size, world_size, smoothing, inplace_backward, dtype):
  21. assert vocab_size % world_size == 0
  22. rtol, atol = ((1e-5, 1e-6) if dtype == torch.float32
  23. else ((1e-3, 1e-4) if dtype == torch.float16 else (1e-2, 3e-3)))
  24. if not torch.distributed.is_initialized():
  25. torch.distributed.init_process_group(backend='nccl', init_method='env://')
  26. partition_vocab_size = vocab_size // world_size
  27. device = f'cuda:{torch.distributed.get_rank()}'
  28. assert world_size <= torch.distributed.get_world_size()
  29. parallel_state.initialize_model_parallel(tensor_model_parallel_size_=world_size)
  30. rank = parallel_state.get_tensor_model_parallel_rank()
  31. # set seed
  32. torch.random.manual_seed(0)
  33. batch_size = 8
  34. seqlen = 128
  35. x_pt = (torch.randn(batch_size * seqlen, vocab_size, device=device,
  36. dtype=dtype) * 10).requires_grad_()
  37. x = tensor_parallel.scatter_to_tensor_model_parallel_region(x_pt).detach().clone().requires_grad_()
  38. y = torch.randint(0, vocab_size, (batch_size * seqlen,), dtype=torch.long, device=device)
  39. y[torch.randperm(batch_size * seqlen)[:10]] = -100
  40. model_pt = torch.nn.CrossEntropyLoss(label_smoothing=smoothing, reduction='none')
  41. model = CrossEntropyLoss(label_smoothing=smoothing, reduction='none',
  42. inplace_backward=inplace_backward)
  43. out = model(x, y, process_group=parallel_state.get_tensor_model_parallel_group())
  44. out_pt = model_pt(x_pt.float(), y)
  45. assert torch.allclose(out, out_pt, rtol=1e-5, atol=1e-6)
  46. g = torch.randn_like(out)
  47. out_pt.backward(g)
  48. out.backward(g)
  49. assert torch.allclose(x.grad, x_pt.grad[:, (rank * partition_vocab_size):(rank + 1) * partition_vocab_size], rtol=rtol, atol=atol)
  50. parallel_state.destroy_model_parallel()