conftest.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 transformers import AutoTokenizer
  5. ACCESS_ERROR_MSG = "Could not access tokenizer at 'meta-llama/Llama-2-7b-hf'. Did you log into huggingface hub and provided the correct token?"
  6. LLAMA_VERSIONS = ["meta-llama/Llama-2-7b-hf", "meta-llama/Meta-Llama-3-8B"]
  7. @pytest.fixture(params=LLAMA_VERSIONS)
  8. def llama_version(request):
  9. return request.param
  10. @pytest.fixture(scope="module")
  11. def llama_tokenizer(request):
  12. return {k: AutoTokenizer.from_pretrained(k) for k in LLAMA_VERSIONS}
  13. @pytest.fixture
  14. def setup_tokenizer(llama_tokenizer, llama_version):
  15. def _helper(tokenizer_mock):
  16. #Align with Llama 2 tokenizer
  17. tokenizer_mock.from_pretrained.return_value = llama_tokenizer[llama_version]
  18. return _helper
  19. def pytest_addoption(parser):
  20. parser.addoption(
  21. "--unskip-missing-tokenizer",
  22. action="store_true",
  23. default=False, help="disable skip missing tokenizer")
  24. def pytest_configure(config):
  25. config.addinivalue_line("markers", "skip_missing_tokenizer: skip if tokenizer is unavailable")
  26. def pytest_collection_modifyitems(config, items):
  27. if config.getoption("--unskip-missing-tokenizer"):
  28. return
  29. try:
  30. AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-hf")
  31. tokenizer_available = True
  32. except OSError:
  33. tokenizer_available = False
  34. skip_missing_tokenizer = pytest.mark.skip(reason=ACCESS_ERROR_MSG)
  35. for item in items:
  36. if "skip_missing_tokenizer" in item.keywords and not tokenizer_available:
  37. item.add_marker(skip_missing_tokenizer)