test_gptj.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import re
  2. import torch
  3. import pytest
  4. from transformers import GPTJConfig, AutoTokenizer
  5. from transformers.models.gptj.modeling_gptj import GPTJForCausalLM
  6. from flash_attn.models.gpt import GPTLMHeadModel
  7. from flash_attn.models.gptj import remap_state_dict_hf_gptj, gptj_config_to_gpt2_config
  8. from flash_attn.utils.pretrained import state_dict_from_pretrained
  9. @pytest.mark.parametrize('model_name', ["EleutherAI/gpt-j-6B"])
  10. def test_gptj_state_dict(model_name):
  11. config = gptj_config_to_gpt2_config(GPTJConfig.from_pretrained(model_name))
  12. pretrained_state_dict = remap_state_dict_hf_gptj(state_dict_from_pretrained(model_name), config)
  13. model = GPTLMHeadModel(config, device='meta') # Without device='meta' init is very slow
  14. state_dict = model.state_dict()
  15. rotary_inv_freq_keys = {f'transformer.layers.{l}.mixer.rotary_emb.inv_freq'
  16. for l in range(config.n_layer)}
  17. assert state_dict.keys() == pretrained_state_dict.keys() | rotary_inv_freq_keys
  18. for k in state_dict.keys() - rotary_inv_freq_keys:
  19. assert state_dict[k].shape == pretrained_state_dict[k].shape
  20. @pytest.mark.parametrize('model_name', ["EleutherAI/gpt-j-6B"])
  21. def test_gptj_optimized(model_name):
  22. """Check that our implementation of GPT-J (with 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 = gptj_config_to_gpt2_config(GPTJConfig.from_pretrained(model_name))
  29. config.use_flash_attn = False # FlashAttention doesn't support hdim 256 yet
  30. config.fused_bias_fc = True
  31. config.fused_mlp = True
  32. config.fused_dropout_add_ln = True
  33. config.residual_in_fp32 = True
  34. model = GPTLMHeadModel.from_pretrained(model_name, config, device=device, dtype=dtype)
  35. model.eval()
  36. torch.manual_seed(0)
  37. batch_size = 2
  38. max_seqlen = 256
  39. seqlens = torch.randint(max_seqlen // 2, max_seqlen + 1, (batch_size,), device=device)
  40. input_ids = torch.randint(0, config.vocab_size, (batch_size, max_seqlen), dtype=torch.long,
  41. device=device)
  42. with torch.no_grad():
  43. out = model.transformer(input_ids)
  44. logits = model(input_ids).logits
  45. del model
  46. # Without device_map, the model is loaded on the CPU, which is very slow
  47. model_ref = GPTJForCausalLM.from_pretrained(model_name, device_map={"": device})
  48. model_ref.eval()
  49. with torch.no_grad():
  50. out_ref = model_ref.transformer(input_ids).last_hidden_state
  51. logits_ref = model_ref(input_ids).logits
  52. del model_ref
  53. model_hf = GPTJForCausalLM.from_pretrained(model_name, torch_dtype=dtype,
  54. device_map={"": device})
  55. model_hf.eval()
  56. out_hf = model_hf.transformer(input_ids).last_hidden_state
  57. logits_hf = model_hf(input_ids).logits
  58. del model_hf
  59. print(f'Output max diff: {(out - out_ref).abs().max().item()}')
  60. print(f'Output mean diff: {(out - out_ref).abs().mean().item()}')
  61. print(f'HF fp16 max diff: {(out_hf - out_ref).abs().max().item()}')
  62. print(f'HF fp16 mean diff: {(out_hf - out_ref).abs().mean().item()}')
  63. assert (out - out_ref).abs().max().item() < 3 * (out_hf - out_ref).abs().max().item()
  64. print(f'Logits max diff: {(logits - logits_ref).abs().max().item()}')
  65. print(f'Logits mean diff: {(logits - logits_ref).abs().mean().item()}')
  66. print(f'HF fp16 max diff: {(logits_hf - logits_ref).abs().max().item()}')
  67. print(f'HF fp16 mean diff: {(logits_hf - logits_ref).abs().mean().item()}')
  68. assert (logits - logits_ref).abs().max().item() < 3 * (logits_hf - logits_ref).abs().max().item()