test_opt.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import re
  2. import torch
  3. import pytest
  4. from transformers import OPTConfig
  5. from transformers.models.opt.modeling_opt import OPTForCausalLM
  6. from flash_attn.models.gpt import GPTLMHeadModel
  7. from flash_attn.models.opt import remap_state_dict_opt, opt_config_to_gpt2_config
  8. from flash_attn.utils.pretrained import state_dict_from_pretrained
  9. @pytest.mark.parametrize('model_name', ["facebook/opt-125m", "facebook/opt-350m", "facebook/opt-1.3b"])
  10. # @pytest.mark.parametrize('model_name', ["facebook/opt-350m"])
  11. def test_opt_state_dict(model_name):
  12. config = opt_config_to_gpt2_config(OPTConfig.from_pretrained(model_name))
  13. pretrained_state_dict = remap_state_dict_opt(state_dict_from_pretrained(model_name), config)
  14. model = GPTLMHeadModel(config)
  15. state_dict = model.state_dict()
  16. assert state_dict.keys() == pretrained_state_dict.keys()
  17. for k in state_dict.keys():
  18. assert state_dict[k].shape == pretrained_state_dict[k].shape
  19. @pytest.mark.parametrize('model_name', ["facebook/opt-125m", "facebook/opt-350m", "facebook/opt-1.3b"])
  20. # @pytest.mark.parametrize('model_name', ["facebook/opt-350m"])
  21. def test_opt_optimized(model_name):
  22. """Check that our implementation of OPT (without all optimizations enabled) matches the
  23. HF implementation: the output of our forward pass in fp16 should be around the same as the HF
  24. forward pass in fp16, when compared to the HF forward pass in fp32.
  25. """
  26. dtype = torch.float16
  27. device = 'cuda'
  28. config = opt_config_to_gpt2_config(OPTConfig.from_pretrained(model_name))
  29. config.use_flash_attn = True
  30. config.fused_bias_fc = True
  31. config.fused_mlp = True
  32. config.fused_dropout_add_ln = True
  33. # Only prenorm supports residual_in_fp32
  34. config.residual_in_fp32 = getattr(config, 'prenorm', True)
  35. config.pad_vocab_size_multiple = 8
  36. model = GPTLMHeadModel.from_pretrained(model_name, config, device=device, dtype=dtype)
  37. model_ref = OPTForCausalLM.from_pretrained(model_name).to(device=device)
  38. model_hf = OPTForCausalLM.from_pretrained(model_name, torch_dtype=dtype).to(device=device)
  39. model.eval()
  40. model_ref.eval()
  41. model_hf.eval()
  42. torch.manual_seed(0)
  43. batch_size = 2
  44. max_seqlen = 256
  45. seqlens = torch.randint(max_seqlen // 2, max_seqlen + 1, (batch_size,), device='cuda')
  46. input_ids = torch.randint(0, config.vocab_size, (batch_size, max_seqlen), dtype=torch.long,
  47. device='cuda')
  48. if model_name != 'facebook/opt-350m': # The OPT-350m projects the embeddings to dimension 512
  49. out = model.transformer(input_ids)
  50. out_hf = model_hf.model(input_ids).last_hidden_state
  51. out_ref = model_ref.model(input_ids).last_hidden_state
  52. print(f'Output max diff: {(out - out_ref).abs().max().item()}')
  53. print(f'Output mean diff: {(out - out_ref).abs().mean().item()}')
  54. print(f'HF fp16 max diff: {(out_hf - out_ref).abs().max().item()}')
  55. print(f'HF fp16 mean diff: {(out_hf - out_ref).abs().mean().item()}')
  56. assert (out - out_ref).abs().max().item() < 3 * (out_hf - out_ref).abs().max().item()
  57. logits = model(input_ids).logits
  58. logits_hf = model_hf(input_ids).logits
  59. logits_ref = model_ref(input_ids).logits
  60. print(f'Logits max diff: {(logits - logits_ref).abs().max().item()}')
  61. print(f'Logits mean diff: {(logits - logits_ref).abs().mean().item()}')
  62. print(f'HF fp16 max diff: {(logits_hf - logits_ref).abs().max().item()}')
  63. print(f'HF fp16 mean diff: {(logits_hf - logits_ref).abs().mean().item()}')
  64. assert (logits - logits_ref).abs().max().item() < 3 * (logits_hf - logits_ref).abs().max().item()