test_finetuning.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 pytest import approx
  5. from unittest.mock import patch
  6. import torch
  7. from torch.optim import AdamW
  8. from torch.utils.data.dataloader import DataLoader
  9. from torch.utils.data.sampler import BatchSampler
  10. from llama_recipes.finetuning import main
  11. from llama_recipes.data.sampler import LengthBasedBatchSampler
  12. def get_fake_dataset():
  13. return [{
  14. "input_ids":[1],
  15. "attention_mask":[1],
  16. "labels":[1],
  17. }]
  18. @patch('llama_recipes.finetuning.train')
  19. @patch('llama_recipes.finetuning.LlamaForCausalLM.from_pretrained')
  20. @patch('llama_recipes.finetuning.LlamaTokenizer.from_pretrained')
  21. @patch('llama_recipes.finetuning.get_preprocessed_dataset')
  22. @patch('llama_recipes.finetuning.optim.AdamW')
  23. @patch('llama_recipes.finetuning.StepLR')
  24. def test_finetuning_no_validation(step_lr, optimizer, get_dataset, tokenizer, get_model, train):
  25. kwargs = {"run_validation": False}
  26. get_dataset.return_value = get_fake_dataset()
  27. main(**kwargs)
  28. assert train.call_count == 1
  29. args, kwargs = train.call_args
  30. train_dataloader = args[1]
  31. eval_dataloader = args[2]
  32. assert isinstance(train_dataloader, DataLoader)
  33. assert eval_dataloader is None
  34. if torch.cuda.is_available():
  35. assert get_model.return_value.to.call_count == 1
  36. assert get_model.return_value.to.call_args.args[0] == "cuda"
  37. else:
  38. assert get_model.return_value.to.call_count == 0
  39. @patch('llama_recipes.finetuning.train')
  40. @patch('llama_recipes.finetuning.LlamaForCausalLM.from_pretrained')
  41. @patch('llama_recipes.finetuning.LlamaTokenizer.from_pretrained')
  42. @patch('llama_recipes.finetuning.get_preprocessed_dataset')
  43. @patch('llama_recipes.finetuning.optim.AdamW')
  44. @patch('llama_recipes.finetuning.StepLR')
  45. def test_finetuning_with_validation(step_lr, optimizer, get_dataset, tokenizer, get_model, train):
  46. kwargs = {"run_validation": True}
  47. get_dataset.return_value = get_fake_dataset()
  48. main(**kwargs)
  49. assert train.call_count == 1
  50. args, kwargs = train.call_args
  51. train_dataloader = args[1]
  52. eval_dataloader = args[2]
  53. assert isinstance(train_dataloader, DataLoader)
  54. assert isinstance(eval_dataloader, DataLoader)
  55. if torch.cuda.is_available():
  56. assert get_model.return_value.to.call_count == 1
  57. assert get_model.return_value.to.call_args.args[0] == "cuda"
  58. else:
  59. assert get_model.return_value.to.call_count == 0
  60. @patch('llama_recipes.finetuning.train')
  61. @patch('llama_recipes.finetuning.LlamaForCausalLM.from_pretrained')
  62. @patch('llama_recipes.finetuning.LlamaTokenizer.from_pretrained')
  63. @patch('llama_recipes.finetuning.get_preprocessed_dataset')
  64. @patch('llama_recipes.finetuning.generate_peft_config')
  65. @patch('llama_recipes.finetuning.get_peft_model')
  66. @patch('llama_recipes.finetuning.optim.AdamW')
  67. @patch('llama_recipes.finetuning.StepLR')
  68. def test_finetuning_peft(step_lr, optimizer, get_peft_model, gen_peft_config, get_dataset, tokenizer, get_model, train):
  69. kwargs = {"use_peft": True}
  70. get_dataset.return_value = get_fake_dataset()
  71. main(**kwargs)
  72. if torch.cuda.is_available():
  73. assert get_model.return_value.to.call_count == 1
  74. assert get_model.return_value.to.call_args.args[0] == "cuda"
  75. else:
  76. assert get_model.return_value.to.call_count == 0
  77. assert get_peft_model.return_value.print_trainable_parameters.call_count == 1
  78. @patch('llama_recipes.finetuning.train')
  79. @patch('llama_recipes.finetuning.LlamaForCausalLM.from_pretrained')
  80. @patch('llama_recipes.finetuning.LlamaTokenizer.from_pretrained')
  81. @patch('llama_recipes.finetuning.get_preprocessed_dataset')
  82. @patch('llama_recipes.finetuning.get_peft_model')
  83. @patch('llama_recipes.finetuning.StepLR')
  84. def test_finetuning_weight_decay(step_lr, get_peft_model, get_dataset, tokenizer, get_model, train, mocker):
  85. kwargs = {"weight_decay": 0.01}
  86. get_dataset.return_value = get_fake_dataset()
  87. model = mocker.MagicMock(name="Model")
  88. model.parameters.return_value = [torch.ones(1,1)]
  89. get_model.return_value = model
  90. main(**kwargs)
  91. assert train.call_count == 1
  92. args, kwargs = train.call_args
  93. optimizer = args[4]
  94. print(optimizer.state_dict())
  95. assert isinstance(optimizer, AdamW)
  96. assert optimizer.state_dict()["param_groups"][0]["weight_decay"] == approx(0.01)
  97. @patch('llama_recipes.finetuning.train')
  98. @patch('llama_recipes.finetuning.LlamaForCausalLM.from_pretrained')
  99. @patch('llama_recipes.finetuning.LlamaTokenizer.from_pretrained')
  100. @patch('llama_recipes.finetuning.get_preprocessed_dataset')
  101. @patch('llama_recipes.finetuning.optim.AdamW')
  102. @patch('llama_recipes.finetuning.StepLR')
  103. def test_batching_strategy(step_lr, optimizer, get_dataset, tokenizer, get_model, train):
  104. kwargs = {"batching_strategy": "packing"}
  105. get_dataset.return_value = get_fake_dataset()
  106. main(**kwargs)
  107. assert train.call_count == 1
  108. args, kwargs = train.call_args
  109. train_dataloader, eval_dataloader = args[1:3]
  110. assert isinstance(train_dataloader.batch_sampler, BatchSampler)
  111. assert isinstance(eval_dataloader.batch_sampler, BatchSampler)
  112. kwargs["batching_strategy"] = "padding"
  113. train.reset_mock()
  114. main(**kwargs)
  115. assert train.call_count == 1
  116. args, kwargs = train.call_args
  117. train_dataloader, eval_dataloader = args[1:3]
  118. assert isinstance(train_dataloader.batch_sampler, LengthBasedBatchSampler)
  119. assert isinstance(eval_dataloader.batch_sampler, LengthBasedBatchSampler)
  120. kwargs["batching_strategy"] = "none"
  121. with pytest.raises(ValueError):
  122. main(**kwargs)