test_grammar_datasets.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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_RESULTS = {
  6. "meta-llama/Llama-2-7b-hf":{
  7. "label": 1152,
  8. "pos": 31,
  9. },
  10. "meta-llama/Meta-Llama-3-8B":{
  11. "label": 40,
  12. "pos": 26,
  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_grammar_dataset(step_lr, optimizer, get_model, tokenizer, train, setup_tokenizer, llama_version):
  22. from llama_recipes.finetuning import main
  23. setup_tokenizer(tokenizer)
  24. BATCH_SIZE = 8
  25. kwargs = {
  26. "model_name": llama_version,
  27. "batch_size_training": BATCH_SIZE,
  28. "val_batch_size": 1,
  29. "use_peft": False,
  30. "dataset": "grammar_dataset",
  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. VAL_SAMPLES = 2988
  39. TRAIN_SAMPLES = 13016
  40. assert len(train_dataloader) == TRAIN_SAMPLES // BATCH_SIZE
  41. assert len(eval_dataloader) == VAL_SAMPLES
  42. batch = next(iter(train_dataloader))
  43. assert "labels" in batch.keys()
  44. assert "input_ids" in batch.keys()
  45. assert "attention_mask" in batch.keys()
  46. assert batch["labels"][0][EXPECTED_RESULTS[llama_version]["pos"]-1] == -100
  47. assert batch["labels"][0][EXPECTED_RESULTS[llama_version]["pos"]] == EXPECTED_RESULTS[llama_version]["label"]
  48. token = args[3]
  49. assert batch["input_ids"][0][0] == token.bos_token_id
  50. assert batch["labels"][0][-1] == token.eos_token_id
  51. assert batch["input_ids"][0][-1] == token.eos_token_id