|
| 1 | +# MIT License |
| 2 | + |
| 3 | +# Copyright (c) 2024 The HuggingFace Team |
| 4 | + |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | + |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | + |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | + |
| 23 | +# tests/utils/pretend_missing.py |
| 24 | +import functools |
| 25 | +import importlib |
| 26 | + |
| 27 | +import pytest |
| 28 | + |
| 29 | +import lighteval.utils.imports as imports |
| 30 | + |
| 31 | + |
| 32 | +def pretend_missing(*names): |
| 33 | + """ |
| 34 | + Decorator: pretend that certain packages are missing |
| 35 | + by patching mypkg.utils.is_package_available. |
| 36 | + """ |
| 37 | + |
| 38 | + def decorator(test_func): |
| 39 | + @functools.wraps(test_func) |
| 40 | + def wrapper(*args, **kwargs): |
| 41 | + from unittest.mock import patch |
| 42 | + |
| 43 | + def fake(name): |
| 44 | + return False if name in names else (importlib.util.find_spec(name) is not None) |
| 45 | + |
| 46 | + with patch.object(imports, "is_package_available", side_effect=fake): |
| 47 | + # If your module caches results at import time, reload here |
| 48 | + import lighteval |
| 49 | + |
| 50 | + importlib.reload(lighteval) |
| 51 | + |
| 52 | + return test_func(*args, **kwargs) |
| 53 | + |
| 54 | + return wrapper |
| 55 | + |
| 56 | + return decorator |
| 57 | + |
| 58 | + |
| 59 | +@pretend_missing("langdetect") |
| 60 | +def test_langdetect_required_for_ifeval(): |
| 61 | + from lighteval.main_accelerate import accelerate |
| 62 | + |
| 63 | + with pytest.raises( |
| 64 | + ImportError, |
| 65 | + match="Through the use of ifeval_prompt, you requested the use of `langdetect` for this evaluation, but it is not available in your current environment. Please install it using pip.", |
| 66 | + ): |
| 67 | + accelerate(model_args="model_name=gpt2,batch_size=1", tasks="extended|ifeval|0", max_samples=0) |
| 68 | + |
| 69 | + |
| 70 | +@pretend_missing("spacy", "stanza") |
| 71 | +def test_multilingual_required_for_xnli(): |
| 72 | + from lighteval.main_accelerate import accelerate |
| 73 | + |
| 74 | + with pytest.raises( |
| 75 | + ImportError, |
| 76 | + match="Through the use of get_multilingual_normalizer, you are trying to run an evaluation requiring multilingual capabilities. Please install the required extra: `pip install lighteval[multilingual]`", |
| 77 | + ): |
| 78 | + accelerate(model_args="model_name=gpt2,batch_size=1", tasks="lighteval|xnli_zho_mcf|0", max_samples=0) |
| 79 | + |
| 80 | + |
| 81 | +@pretend_missing("vllm") |
| 82 | +def test_vllm_required_for_vllm_usage(): |
| 83 | + from lighteval.main_vllm import vllm |
| 84 | + |
| 85 | + with pytest.raises( |
| 86 | + ImportError, |
| 87 | + match="You requested the use of `vllm` for this evaluation, but it is not available in your current environment. Please install it using pip.'", |
| 88 | + ): |
| 89 | + vllm(model_args="model_name=gpt2,batch_size=1", tasks="lighteval|xnli_zho_mcf|0", max_samples=0) |
0 commit comments