test_custom_dataset.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. from transformers import LlamaTokenizer
  6. EXPECTED_RESULTS={
  7. "meta-llama/Llama-2-7b-hf":{
  8. "example_1": "[INST] Who made Berlin [/INST] dunno",
  9. "example_2": "[INST] Quiero preparar una pizza de pepperoni, puedes darme los pasos para hacerla? [/INST] Claro!",
  10. },
  11. "meta-llama/Meta-Llama-3-8B":{
  12. "example_1": "<|begin_of_text|><|start_header_id|>user<|end_header_id|>\n\nWho made Berlin<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\ndunno<|eot_id|><|end_of_text|>",
  13. "example_2": "<|begin_of_text|><|start_header_id|>user<|end_header_id|>\n\nHow to start learning guitar and become a master at it?",
  14. },
  15. }
  16. def check_padded_entry(batch, tokenizer):
  17. seq_len = sum(batch["attention_mask"][0])
  18. assert seq_len < len(batch["attention_mask"][0])
  19. if tokenizer.vocab_size >= 128000:
  20. END_OF_TEXT_ID = 128009
  21. else:
  22. END_OF_TEXT_ID = tokenizer.eos_token_id
  23. assert batch["labels"][0][0] == -100
  24. assert batch["labels"][0][seq_len-1] == END_OF_TEXT_ID
  25. assert batch["labels"][0][-1] == -100
  26. assert batch["input_ids"][0][0] == tokenizer.bos_token_id
  27. assert batch["input_ids"][0][-1] == tokenizer.eos_token_id
  28. @pytest.mark.skip_missing_tokenizer
  29. @patch('llama_recipes.finetuning.train')
  30. @patch('llama_recipes.finetuning.AutoTokenizer')
  31. @patch('llama_recipes.finetuning.LlamaForCausalLM.from_pretrained')
  32. @patch('llama_recipes.finetuning.optim.AdamW')
  33. @patch('llama_recipes.finetuning.StepLR')
  34. def test_custom_dataset(step_lr, optimizer, get_model, tokenizer, train, mocker, setup_tokenizer, llama_version):
  35. from llama_recipes.finetuning import main
  36. setup_tokenizer(tokenizer)
  37. skip_special_tokens = llama_version == "meta-llama/Llama-2-7b-hf"
  38. kwargs = {
  39. "dataset": "custom_dataset",
  40. "model_name": llama_version,
  41. "custom_dataset.file": "recipes/finetuning/datasets/custom_dataset.py",
  42. "custom_dataset.train_split": "validation",
  43. "batch_size_training": 2,
  44. "val_batch_size": 4,
  45. "use_peft": False,
  46. "batching_strategy": "padding"
  47. }
  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. tokenizer = args[3]
  54. assert len(train_dataloader) == 1120
  55. assert len(eval_dataloader) == 1120 //2
  56. it = iter(eval_dataloader)
  57. batch = next(it)
  58. STRING = tokenizer.decode(batch["input_ids"][0], skip_special_tokens=skip_special_tokens)
  59. assert STRING.startswith(EXPECTED_RESULTS[llama_version]["example_1"])
  60. assert batch["input_ids"].size(0) == 4
  61. assert set(("labels", "input_ids", "attention_mask")) == set(batch.keys())
  62. check_padded_entry(batch, tokenizer)
  63. it = iter(train_dataloader)
  64. next(it)
  65. batch = next(it)
  66. STRING = tokenizer.decode(batch["input_ids"][0], skip_special_tokens=skip_special_tokens)
  67. assert STRING.startswith(EXPECTED_RESULTS[llama_version]["example_2"])
  68. assert batch["input_ids"].size(0) == 2
  69. assert set(("labels", "input_ids", "attention_mask")) == set(batch.keys())
  70. check_padded_entry(batch, tokenizer)
  71. @patch('llama_recipes.finetuning.train')
  72. @patch('llama_recipes.finetuning.LlamaForCausalLM.from_pretrained')
  73. @patch('llama_recipes.finetuning.AutoTokenizer.from_pretrained')
  74. @patch('llama_recipes.finetuning.optim.AdamW')
  75. @patch('llama_recipes.finetuning.StepLR')
  76. def test_unknown_dataset_error(step_lr, optimizer, tokenizer, get_model, train, mocker):
  77. from llama_recipes.finetuning import main
  78. tokenizer.return_value = mocker.MagicMock(side_effect=lambda x: {"input_ids":[len(x)*[0,]], "attention_mask": [len(x)*[0,]]})
  79. kwargs = {
  80. "dataset": "custom_dataset",
  81. "custom_dataset.file": "recipes/finetuning/datasets/custom_dataset.py:get_unknown_dataset",
  82. "batch_size_training": 1,
  83. "use_peft": False,
  84. }
  85. with pytest.raises(AttributeError):
  86. main(**kwargs)