test_samsum_datasets.py 2.0 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 functools import partial
  5. from unittest.mock import patch
  6. EXPECTED_RESULTS = {
  7. "meta-llama/Llama-2-7b-hf":{
  8. "label": 8432,
  9. "pos": 242,
  10. },
  11. "meta-llama/Meta-Llama-3-8B":{
  12. "label": 2250,
  13. "pos": 211,
  14. },
  15. }
  16. @pytest.mark.skip_missing_tokenizer
  17. @patch('llama_recipes.finetuning.train')
  18. @patch('llama_recipes.finetuning.AutoTokenizer')
  19. @patch('llama_recipes.finetuning.LlamaForCausalLM.from_pretrained')
  20. @patch('llama_recipes.finetuning.optim.AdamW')
  21. @patch('llama_recipes.finetuning.StepLR')
  22. def test_samsum_dataset(step_lr, optimizer, get_model, tokenizer, train, mocker, setup_tokenizer, llama_version):
  23. from llama_recipes.finetuning import main
  24. setup_tokenizer(tokenizer)
  25. BATCH_SIZE = 8
  26. kwargs = {
  27. "model_name": llama_version,
  28. "batch_size_training": BATCH_SIZE,
  29. "val_batch_size": 1,
  30. "use_peft": False,
  31. "dataset": "samsum_dataset",
  32. "batching_strategy": "padding",
  33. }
  34. main(**kwargs)
  35. assert train.call_count == 1
  36. args, kwargs = train.call_args
  37. train_dataloader = args[1]
  38. eval_dataloader = args[2]
  39. token = args[3]
  40. VAL_SAMPLES = 818
  41. TRAIN_SAMPLES = 14732
  42. assert len(train_dataloader) == TRAIN_SAMPLES // BATCH_SIZE
  43. assert len(eval_dataloader) == VAL_SAMPLES
  44. batch = next(iter(train_dataloader))
  45. assert "labels" in batch.keys()
  46. assert "input_ids" in batch.keys()
  47. assert "attention_mask" in batch.keys()
  48. assert batch["labels"][0][EXPECTED_RESULTS[llama_version]["pos"]-1] == -100
  49. assert batch["labels"][0][EXPECTED_RESULTS[llama_version]["pos"]] == EXPECTED_RESULTS[llama_version]["label"]
  50. assert batch["input_ids"][0][0] == token.bos_token_id
  51. assert batch["labels"][0][-1] == token.eos_token_id
  52. assert batch["input_ids"][0][-1] == token.eos_token_id