test_custom_dataset.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. @patch('llama_recipes.finetuning.train')
  6. @patch('llama_recipes.finetuning.LlamaForCausalLM.from_pretrained')
  7. @patch('llama_recipes.finetuning.optim.AdamW')
  8. @patch('llama_recipes.finetuning.StepLR')
  9. def test_custom_dataset(step_lr, optimizer, get_model, train, mocker):
  10. from llama_recipes.finetuning import main
  11. kwargs = {
  12. "dataset": "custom_dataset",
  13. "model_name": "decapoda-research/llama-7b-hf", # We use the tokenizer as a surrogate for llama2 tokenizer here
  14. "custom_dataset.file": "examples/custom_dataset.py",
  15. "custom_dataset.train_split": "validation",
  16. "batch_size_training": 2,
  17. "use_peft": False,
  18. }
  19. main(**kwargs)
  20. assert train.call_count == 1
  21. args, kwargs = train.call_args
  22. train_dataloader = args[1]
  23. eval_dataloader = args[2]
  24. tokenizer = args[3]
  25. assert len(train_dataloader) == 226
  26. assert len(eval_dataloader) == 2*226
  27. it = iter(train_dataloader)
  28. STRING = tokenizer.decode(next(it)["input_ids"][0], skip_special_tokens=True)
  29. EXPECTED_STRING = "[INST] Напиши функцию на языке swift, которая сортирует массив целых чисел, а затем выводит его на экран [/INST] Вот функция, "
  30. assert STRING.startswith(EXPECTED_STRING)
  31. next(it)
  32. next(it)
  33. next(it)
  34. STRING = tokenizer.decode(next(it)["input_ids"][0], skip_special_tokens=True)
  35. EXPECTED_SUBSTRING_1 = "Therefore you are correct. [INST] How can L’Hopital’s Rule be"
  36. EXPECTED_SUBSTRING_2 = "a circular path around the turn. [INST] How on earth is that related to L’Hopital’s Rule?"
  37. assert EXPECTED_SUBSTRING_1 in STRING
  38. assert EXPECTED_SUBSTRING_2 in STRING
  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.optim.AdamW')
  43. @patch('llama_recipes.finetuning.StepLR')
  44. def test_unknown_dataset_error(step_lr, optimizer, tokenizer, get_model, train, mocker):
  45. from llama_recipes.finetuning import main
  46. tokenizer.return_value = mocker.MagicMock(side_effect=lambda x: {"input_ids":[len(x)*[0,]], "attention_mask": [len(x)*[0,]]})
  47. kwargs = {
  48. "dataset": "custom_dataset",
  49. "custom_dataset.file": "examples/custom_dataset.py:get_unknown_dataset",
  50. "batch_size_training": 1,
  51. "use_peft": False,
  52. }
  53. with pytest.raises(AttributeError):
  54. main(**kwargs)