test_batching.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # Copyright (c) Meta Platforms, Inc. and affiliates.
  2. # This software may be used and distributed according to the terms of the Llama 2 Community License Agreement.
  3. import pytest
  4. from unittest.mock import patch
  5. EXPECTED_SAMPLE_NUMBER ={
  6. "meta-llama/Llama-2-7b-hf": {
  7. "train": 96,
  8. "eval": 42,
  9. },
  10. "meta-llama/Meta-Llama-3-8B": {
  11. "train": 79,
  12. "eval": 34,
  13. }
  14. }
  15. @pytest.mark.skip_missing_tokenizer
  16. @patch('llama_recipes.finetuning.train')
  17. @patch('llama_recipes.finetuning.AutoTokenizer')
  18. @patch('llama_recipes.finetuning.LlamaForCausalLM.from_pretrained')
  19. @patch('llama_recipes.finetuning.optim.AdamW')
  20. @patch('llama_recipes.finetuning.StepLR')
  21. def test_packing(step_lr, optimizer, get_model, tokenizer, train, setup_tokenizer, llama_version):
  22. from llama_recipes.finetuning import main
  23. setup_tokenizer(tokenizer)
  24. kwargs = {
  25. "model_name": llama_version,
  26. "batch_size_training": 8,
  27. "val_batch_size": 1,
  28. "use_peft": False,
  29. "dataset": "samsum_dataset",
  30. "batching_strategy": "packing",
  31. }
  32. main(**kwargs)
  33. assert train.call_count == 1
  34. args, kwargs = train.call_args
  35. train_dataloader = args[1]
  36. eval_dataloader = args[2]
  37. assert len(train_dataloader) == EXPECTED_SAMPLE_NUMBER[llama_version]["train"]
  38. assert len(eval_dataloader) == EXPECTED_SAMPLE_NUMBER[llama_version]["eval"]
  39. batch = next(iter(train_dataloader))
  40. assert "labels" in batch.keys()
  41. assert "input_ids" in batch.keys()
  42. assert "attention_mask" in batch.keys()
  43. assert batch["labels"][0].size(0) == 4096
  44. assert batch["input_ids"][0].size(0) == 4096
  45. assert batch["attention_mask"][0].size(0) == 4096
  46. @pytest.mark.skip_missing_tokenizer
  47. @patch('llama_recipes.finetuning.train')
  48. @patch('llama_recipes.finetuning.AutoTokenizer')
  49. @patch('llama_recipes.finetuning.LlamaForCausalLM.from_pretrained')
  50. @patch('llama_recipes.finetuning.optim.AdamW')
  51. @patch('llama_recipes.finetuning.StepLR')
  52. @patch('llama_recipes.finetuning.setup')
  53. @patch('llama_recipes.finetuning.FSDP')
  54. @patch('llama_recipes.finetuning.torch.distributed.is_initialized')
  55. @patch('llama_recipes.utils.config_utils.dist')
  56. def test_distributed_packing(dist, is_initialized, fsdp, setup, step_lr, optimizer, get_model, tokenizer, train, setup_tokenizer, llama_version):
  57. import os
  58. from llama_recipes.finetuning import main
  59. setup_tokenizer(tokenizer)
  60. rank = 1
  61. os.environ['LOCAL_RANK'] = f'{rank}'
  62. os.environ['RANK'] = f'{rank}'
  63. os.environ['WORLD_SIZE'] = '2'
  64. os.environ['MASTER_ADDR'] = 'localhost'
  65. os.environ['MASTER_PORT'] = '12345'
  66. kwargs = {
  67. "model_name": llama_version,
  68. "batch_size_training": 8,
  69. "val_batch_size": 1,
  70. "use_peft": False,
  71. "dataset": "samsum_dataset",
  72. "batching_strategy": "packing",
  73. "enable_fsdp": True
  74. }
  75. is_initialized.return_value = True
  76. dist.get_rank.return_value = rank
  77. dist.get_world_size.return_value = 2
  78. main(**kwargs)
  79. assert train.call_count == 1
  80. args, kwargs = train.call_args
  81. train_dataloader = args[1]
  82. eval_dataloader = args[2]
  83. assert len(train_dataloader) == EXPECTED_SAMPLE_NUMBER[llama_version]["train"] //2
  84. assert len(eval_dataloader) == EXPECTED_SAMPLE_NUMBER[llama_version]["eval"] //2