Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions tests/unit/utilities/test_hf_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

from functools import wraps
from types import SimpleNamespace
from typing import Any, List

Expand Down Expand Up @@ -149,6 +150,32 @@ def test_idempotent(self) -> None:
assert before is after


class TestSelectCompatibleKwargs:
def test_keeps_keyword_only_parameters(self) -> None:
def target(repo_id: str, *, revision: str, local_files_only: bool) -> None:
pass

kwargs = {"repo_id": "any/repo", "revision": "abc", "local_files_only": True, "bogus": 1}

assert hf_utils.select_compatible_kwargs(kwargs, target) == {
"repo_id": "any/repo",
"revision": "abc",
"local_files_only": True,
}

def test_follows_wrapped_function_parameters(self) -> None:
def target(revision: str) -> str:
return revision

@wraps(target)
def wrapped(*args: Any, **kwargs: Any) -> str:
return target(*args, **kwargs)

assert hf_utils.select_compatible_kwargs({"revision": "abc", "bogus": 1}, wrapped) == {
"revision": "abc"
}


class TestDownloadFileFromHf:
"""End-to-end coverage: download_file_from_hf must actually use the retry helper.

Expand Down Expand Up @@ -180,6 +207,28 @@ def fake_hub_download(**kwargs: Any) -> str:
assert state["calls"] == 2
assert len(_no_sleep) == 1

def test_forwards_keyword_only_download_parameters(
self, monkeypatch: pytest.MonkeyPatch, tmp_path: Any
) -> None:
fake_file = tmp_path / "data.json"
fake_file.write_text('{"ok": true}')
calls = []

def fake_hub_download(
*, revision: str | None = None, local_files_only: bool = False, **kwargs: Any
) -> str:
calls.append((revision, local_files_only))
return str(fake_file)

monkeypatch.setattr(hf_utils, "hf_hub_download", fake_hub_download)

result = hf_utils.download_file_from_hf(
"any/repo", "data.json", revision="abc", local_files_only=True
)

assert result == {"ok": True}
assert calls == [("abc", True)]


class TestRemotePostInitCompat:
"""4.x-era remote configs define argless __post_init__, which >=5.x calls
Expand Down
9 changes: 8 additions & 1 deletion transformer_lens/utilities/hf_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,14 @@ def get_rotary_pct_from_config(config: Any) -> float:

def select_compatible_kwargs(kwargs_dict: Dict[str, Any], callable: Callable) -> Dict[str, Any]:
"""Return a dict with the elements kwargs_dict that are parameters of callable"""
return {k: v for k, v in kwargs_dict.items() if k in inspect.getfullargspec(callable).args}
parameters = inspect.signature(callable).parameters
return {
k: v
for k, v in kwargs_dict.items()
if k in parameters
and parameters[k].kind
in (inspect.Parameter.POSITIONAL_OR_KEYWORD, inspect.Parameter.KEYWORD_ONLY)
}


def download_file_from_hf(
Expand Down
Loading