-
Notifications
You must be signed in to change notification settings - Fork 81
feat: add test and docs for get_loaded_kernels. #490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cff2603
feat: add test and docs for get_loaded_kernels.
sayakpaul c6746d8
ruff
sayakpaul aea089d
up
sayakpaul 2c472b8
fix
sayakpaul 12b2038
feat: add test and docs for get_loaded_kernels.
sayakpaul 8ca704c
resolve conflicts.
sayakpaul 58d717a
namedtuple -> dataclasses.
sayakpaul 013ea40
reminder.
sayakpaul c186c27
upgrade to kernels-community/relu.
sayakpaul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| from dataclasses import fields | ||
|
|
||
| import pytest | ||
|
|
||
| from kernels import get_kernel, get_loaded_kernels, get_local_kernel, install_kernel | ||
| from kernels.utils import LoadedKernel, RepoInfos, _loaded_kernels | ||
|
|
||
| _REPO_ID = "kernels-community/relu" | ||
| _PACKAGE_NAME = "relu" | ||
| _VERSION = 1 | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def fresh_registry(): | ||
| """Snapshot the process-wide registry, run the test with a clean one, restore on teardown.""" | ||
| saved = _loaded_kernels.copy() | ||
| _loaded_kernels.clear() | ||
| yield | ||
| _loaded_kernels.clear() | ||
| _loaded_kernels.update(saved) | ||
|
|
||
|
|
||
| def test_dataclass_shape(): | ||
| assert tuple(f.name for f in fields(LoadedKernel)) == ( | ||
| "kernel_id", | ||
| "module", | ||
| "module_name", | ||
| "repo_infos", | ||
| ) | ||
| assert tuple(f.name for f in fields(RepoInfos)) == ("repo_id", "revision", "backend") | ||
|
|
||
|
|
||
| def test_get_loaded_kernels_returns_copy(fresh_registry): | ||
| kernel = get_kernel(_REPO_ID, version=_VERSION, backend="cpu") | ||
|
|
||
| snapshot = get_loaded_kernels() | ||
| assert len(snapshot) == 1 | ||
|
|
||
| snapshot.clear() | ||
| snapshot.append("garbage") # type: ignore[arg-type] | ||
|
|
||
| again = get_loaded_kernels() | ||
| assert len(again) == 1 | ||
| assert again[0].module is kernel | ||
|
|
||
|
|
||
| def test_get_kernel_registers_loaded_kernel(fresh_registry): | ||
| kernel = get_kernel(_REPO_ID, version=_VERSION, backend="cpu") | ||
|
|
||
| loaded = get_loaded_kernels() | ||
| assert len(loaded) == 1 | ||
|
|
||
| entry = loaded[0] | ||
| assert entry.module is kernel | ||
| assert entry.module_name == _PACKAGE_NAME | ||
| assert entry.repo_infos is not None | ||
| assert entry.repo_infos.repo_id == _REPO_ID | ||
| assert isinstance(entry.repo_infos.revision, str) and entry.repo_infos.revision | ||
| assert entry.repo_infos.backend == "cpu" | ||
|
|
||
|
|
||
| def test_repeated_get_kernel_is_cached(fresh_registry): | ||
| first = get_kernel(_REPO_ID, version=_VERSION, backend="cpu") | ||
| second = get_kernel(_REPO_ID, version=_VERSION, backend="cpu") | ||
|
|
||
| assert first is second | ||
| assert len(get_loaded_kernels()) == 1 | ||
|
|
||
|
|
||
| def test_get_local_kernel_registers_with_null_repo_infos(fresh_registry): | ||
| # Populate the HF cache via get_kernel, grab the variant path it registered, | ||
| # then clear the registry and exercise get_local_kernel against that path. | ||
| get_kernel(_REPO_ID, version=_VERSION, backend="cpu") | ||
| (variant_path,) = list(_loaded_kernels.keys()) | ||
|
|
||
| _loaded_kernels.clear() | ||
|
|
||
| kernel = get_local_kernel(variant_path, _PACKAGE_NAME, backend="cpu") | ||
|
|
||
| loaded = get_loaded_kernels() | ||
| assert len(loaded) == 1 | ||
|
|
||
| entry = loaded[0] | ||
| assert entry.module is kernel | ||
| assert entry.module_name == _PACKAGE_NAME | ||
| assert entry.repo_infos is None | ||
|
|
||
|
|
||
| def test_install_kernel_plus_import_does_not_set_repo_infos(fresh_registry): | ||
| # install_kernel alone does not import; it returns a path. Any loader | ||
| # that does not go through get_kernel must leave repo_infos as None. | ||
| package_name, variant_path = install_kernel(_REPO_ID, revision="main", backend="cpu") | ||
| assert package_name == _PACKAGE_NAME | ||
| assert get_loaded_kernels() == [] | ||
|
|
||
| get_local_kernel(variant_path, package_name, backend="cpu") | ||
| (entry,) = get_loaded_kernels() | ||
| assert entry.repo_infos is None |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.