|
|
|
import importlib |
|
import os |
|
import sys |
|
from datetime import datetime |
|
from typing import List, Tuple |
|
|
|
import pytest |
|
import torch |
|
|
|
|
|
from lm_eval.caching.cache import PATH |
|
|
|
|
|
MODULE_DIR = os.path.dirname(os.path.realpath(__file__)) |
|
|
|
|
|
|
|
sys.path.append(f"{MODULE_DIR}/../scripts") |
|
model_loader = importlib.import_module("requests_caching") |
|
run_model_for_task_caching = model_loader.run_model_for_task_caching |
|
|
|
|
|
DEFAULT_TASKS = ["lambada_openai", "hellaswag"] |
|
|
|
|
|
@pytest.fixture(autouse=True) |
|
def setup_and_teardown(): |
|
|
|
torch.use_deterministic_algorithms(False) |
|
clear_cache() |
|
|
|
yield |
|
|
|
|
|
|
|
def clear_cache(): |
|
if os.path.exists(PATH): |
|
cache_files = os.listdir(PATH) |
|
for file in cache_files: |
|
file_path = f"{PATH}/{file}" |
|
os.unlink(file_path) |
|
|
|
|
|
|
|
def get_cache_files(tasks: List[str] = None) -> Tuple[List[str], List[str]]: |
|
cache_files = os.listdir(PATH) |
|
|
|
file_task_names = [] |
|
|
|
for file in cache_files: |
|
file_without_prefix = file.split("-")[1] |
|
file_without_prefix_and_suffix = file_without_prefix.split(".")[0] |
|
file_task_names.append(file_without_prefix_and_suffix) |
|
|
|
return cache_files, file_task_names |
|
|
|
|
|
def assert_created(tasks: List[str], file_task_names: List[str]): |
|
tasks.sort() |
|
file_task_names.sort() |
|
|
|
assert tasks == file_task_names |
|
|
|
|
|
@pytest.mark.parametrize("tasks", [DEFAULT_TASKS]) |
|
def test_requests_caching_true(tasks: List[str]): |
|
run_model_for_task_caching(tasks=tasks, cache_requests="true") |
|
|
|
cache_files, file_task_names = get_cache_files() |
|
|
|
assert_created(tasks=tasks, file_task_names=file_task_names) |
|
|
|
|
|
@pytest.mark.parametrize("tasks", [DEFAULT_TASKS]) |
|
def test_requests_caching_refresh(tasks: List[str]): |
|
run_model_for_task_caching(tasks=tasks, cache_requests="true") |
|
|
|
timestamp_before_test = datetime.now().timestamp() |
|
|
|
run_model_for_task_caching(tasks=tasks, cache_requests="refresh") |
|
|
|
cache_files, file_task_names = get_cache_files() |
|
|
|
for file in cache_files: |
|
modification_time = os.path.getmtime(f"{PATH}/{file}") |
|
assert modification_time > timestamp_before_test |
|
|
|
tasks.sort() |
|
file_task_names.sort() |
|
|
|
assert tasks == file_task_names |
|
|
|
|
|
@pytest.mark.parametrize("tasks", [DEFAULT_TASKS]) |
|
def test_requests_caching_delete(tasks: List[str]): |
|
|
|
test_requests_caching_true(tasks=tasks) |
|
|
|
run_model_for_task_caching(tasks=tasks, cache_requests="delete") |
|
|
|
cache_files, file_task_names = get_cache_files() |
|
|
|
assert len(cache_files) == 0 |
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
def run_tests(): |
|
tests = [ |
|
test_requests_caching_true, |
|
test_requests_caching_refresh, |
|
test_requests_caching_delete, |
|
] |
|
|
|
for test_func in tests: |
|
clear_cache() |
|
test_func(tasks=DEFAULT_TASKS) |
|
|
|
print("Tests pass") |
|
|
|
run_tests() |
|
|