conftest.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 utils import maybe_tokenizer
  5. ACCESS_ERROR_MSG = "Could not access tokenizer. 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.1-8B-Instruct", "fake_llama"]
  7. LLAMA_TOKENIZERS = {k: maybe_tokenizer(k) for k in LLAMA_VERSIONS}
  8. @pytest.fixture(params=LLAMA_VERSIONS)
  9. def llama_version(request):
  10. return request.param
  11. @pytest.fixture(params=["mllama", "llama"])
  12. def model_type(request):
  13. return request.param
  14. @pytest.fixture(scope="module")
  15. def llama_tokenizer(request):
  16. return LLAMA_TOKENIZERS
  17. @pytest.fixture
  18. def setup_tokenizer(llama_tokenizer, llama_version):
  19. def _helper(tokenizer_mock):
  20. #Align with Llama 2 tokenizer
  21. tokenizer_mock.from_pretrained.return_value = llama_tokenizer[llama_version]
  22. return _helper
  23. @pytest.fixture
  24. def setup_processor(llama_tokenizer, llama_version):
  25. def _helper(processor_mock):
  26. processor_mock.from_pretrained.return_value.tokenizer = llama_tokenizer[llama_version]
  27. return _helper
  28. def pytest_addoption(parser):
  29. parser.addoption(
  30. "--unskip-missing-tokenizer",
  31. action="store_true",
  32. default=False, help="disable skip missing tokenizer")
  33. def pytest_configure(config):
  34. config.addinivalue_line("markers", "skip_missing_tokenizer: skip if tokenizer is unavailable")
  35. def pytest_collection_modifyitems(config, items):
  36. #skip tests marked with skip_missing_tokenizer if tokenizer is unavailable unless --unskip-missing-tokenizer is passed
  37. if config.getoption("--unskip-missing-tokenizer"):
  38. return
  39. skip_missing_tokenizer = pytest.mark.skip(reason=ACCESS_ERROR_MSG)
  40. for item in items:
  41. # get the tokenizer for the test
  42. version = [v for v in LLAMA_VERSIONS for i in item.keywords if v in i]
  43. if len(version) == 0:
  44. # no tokenizer used in this test
  45. continue
  46. version = version.pop()
  47. assert version in LLAMA_TOKENIZERS
  48. if "skip_missing_tokenizer" in item.keywords and LLAMA_TOKENIZERS[version] is None:
  49. item.add_marker(skip_missing_tokenizer)