test_custom_dataset.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. def check_padded_entry(batch):
  7. seq_len = sum(batch["attention_mask"][0])
  8. assert seq_len < len(batch["attention_mask"][0])
  9. assert batch["labels"][0][0] == -100
  10. assert batch["labels"][0][seq_len-1] == 2
  11. assert batch["labels"][0][-1] == -100
  12. assert batch["input_ids"][0][0] == 1
  13. assert batch["input_ids"][0][-1] == 2
  14. @pytest.mark.skip_missing_tokenizer
  15. @patch('llama_recipes.finetuning.train')
  16. @patch('llama_recipes.finetuning.LlamaTokenizer')
  17. @patch('llama_recipes.finetuning.LlamaForCausalLM.from_pretrained')
  18. @patch('llama_recipes.finetuning.optim.AdamW')
  19. @patch('llama_recipes.finetuning.StepLR')
  20. def test_custom_dataset(step_lr, optimizer, get_model, tokenizer, train, mocker, setup_tokenizer):
  21. from llama_recipes.finetuning import main
  22. setup_tokenizer(tokenizer)
  23. kwargs = {
  24. "dataset": "custom_dataset",
  25. "model_name": "meta-llama/Llama-2-7b-hf",
  26. "custom_dataset.file": "examples/custom_dataset.py",
  27. "custom_dataset.train_split": "validation",
  28. "batch_size_training": 2,
  29. "val_batch_size": 4,
  30. "use_peft": False,
  31. "batching_strategy": "padding"
  32. }
  33. main(**kwargs)
  34. assert train.call_count == 1
  35. args, kwargs = train.call_args
  36. train_dataloader = args[1]
  37. eval_dataloader = args[2]
  38. tokenizer = args[3]
  39. assert len(train_dataloader) == 1120
  40. assert len(eval_dataloader) == 1120 //2
  41. it = iter(eval_dataloader)
  42. batch = next(it)
  43. STRING = tokenizer.decode(batch["input_ids"][0], skip_special_tokens=True)
  44. EXPECTED_STRING = "[INST] Who made Berlin [/INST] dunno"
  45. assert STRING.startswith(EXPECTED_STRING)
  46. assert batch["input_ids"].size(0) == 4
  47. assert set(("labels", "input_ids", "attention_mask")) == set(batch.keys())
  48. check_padded_entry(batch)
  49. it = iter(train_dataloader)
  50. for _ in range(5):
  51. next(it)
  52. batch = next(it)
  53. STRING = tokenizer.decode(batch["input_ids"][0], skip_special_tokens=True)
  54. EXPECTED_STRING = "[INST] How do I initialize a Typescript project using npm and git? [/INST] # Initialize a new NPM project"
  55. assert STRING.startswith(EXPECTED_STRING)
  56. assert batch["input_ids"].size(0) == 2
  57. assert set(("labels", "input_ids", "attention_mask")) == set(batch.keys())
  58. check_padded_entry(batch)
  59. @patch('llama_recipes.finetuning.train')
  60. @patch('llama_recipes.finetuning.LlamaForCausalLM.from_pretrained')
  61. @patch('llama_recipes.finetuning.LlamaTokenizer.from_pretrained')
  62. @patch('llama_recipes.finetuning.optim.AdamW')
  63. @patch('llama_recipes.finetuning.StepLR')
  64. def test_unknown_dataset_error(step_lr, optimizer, tokenizer, get_model, train, mocker):
  65. from llama_recipes.finetuning import main
  66. tokenizer.return_value = mocker.MagicMock(side_effect=lambda x: {"input_ids":[len(x)*[0,]], "attention_mask": [len(x)*[0,]]})
  67. kwargs = {
  68. "dataset": "custom_dataset",
  69. "custom_dataset.file": "examples/custom_dataset.py:get_unknown_dataset",
  70. "batch_size_training": 1,
  71. "use_peft": False,
  72. }
  73. with pytest.raises(AttributeError):
  74. main(**kwargs)