Repository navigation
kernels: do not reload kernel with the same ID #892
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
Open
danieldk
wants to merge
2
commits into
main
Choose a base branch
from
kernel-id-import-improvements
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,14 @@ | ||
| import json | ||
| import sys | ||
| import types | ||
|
|
||
| import pytest | ||
|
|
||
| from kernels.importer import _import_from_path, _loaded_kernels | ||
|
|
||
| _EXEC_LOG_MODULE = "_kernels_test_exec_log" | ||
| _COUNTING_ID = "counting_1_cuda" | ||
|
|
||
|
|
||
| def _write_variant(tmp_path): | ||
| variant_dir = tmp_path / "build" / "torch28-cxx11-cu128-x86_64-linux" | ||
|
|
@@ -35,3 +39,61 @@ def test_failed_import_cleans_up_sys_modules(tmp_path): | |
| finally: | ||
| _loaded_kernels.pop(variant_dir, None) | ||
| sys.modules.pop("broken_1_cuda", None) | ||
|
|
||
|
|
||
| def _write_counting_variant(base_path, kernel_id): | ||
| """Write a kernel variant that records each execution of its module.""" | ||
| variant_dir = base_path / "build" / "torch28-cxx11-cu128-x86_64-linux" | ||
| variant_dir.mkdir(parents=True) | ||
| metadata = { | ||
| "id": kernel_id, | ||
| "name": "counting", | ||
| "version": 1, | ||
| "license": "Apache-2.0", | ||
| "python-depends": ["torch"], | ||
| "backend": {"type": "cuda"}, | ||
| } | ||
| (variant_dir / "metadata.json").write_text(json.dumps(metadata)) | ||
| (variant_dir / "__init__.py").write_text(f"import {_EXEC_LOG_MODULE}\n{_EXEC_LOG_MODULE}.calls.append(__file__)\n") | ||
| return variant_dir | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def exec_log(monkeypatch): | ||
| log = types.ModuleType(_EXEC_LOG_MODULE) | ||
| log.calls = [] # type: ignore[attr-defined] | ||
| monkeypatch.setitem(sys.modules, _EXEC_LOG_MODULE, log) | ||
| return log.calls # type: ignore[attr-defined] | ||
|
|
||
|
|
||
| def test_same_id_different_path_is_not_reloaded(tmp_path, exec_log): | ||
| first_dir = _write_counting_variant(tmp_path / "first", _COUNTING_ID) | ||
| second_dir = _write_counting_variant(tmp_path / "second", _COUNTING_ID) | ||
| try: | ||
| first = _import_from_path(first_dir, deps={}) | ||
| second = _import_from_path(second_dir, deps={}) | ||
|
|
||
| assert first is second | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wish we could also do |
||
| assert len(exec_log) == 1 | ||
| assert _loaded_kernels[first_dir].module is first | ||
| assert _loaded_kernels[second_dir].module is first | ||
| finally: | ||
| _loaded_kernels.pop(first_dir, None) | ||
| _loaded_kernels.pop(second_dir, None) | ||
| sys.modules.pop(_COUNTING_ID, None) | ||
|
|
||
|
|
||
| def test_already_imported_kernel_is_reregistered(tmp_path, exec_log): | ||
| variant_dir = _write_counting_variant(tmp_path, _COUNTING_ID) | ||
| try: | ||
| first = _import_from_path(variant_dir, deps={}) | ||
| _loaded_kernels.pop(variant_dir) | ||
|
|
||
| second = _import_from_path(variant_dir, deps={}) | ||
|
|
||
| assert first is second | ||
| assert len(exec_log) == 1 | ||
| assert _loaded_kernels[variant_dir].module is first | ||
| finally: | ||
| _loaded_kernels.pop(variant_dir, None) | ||
| sys.modules.pop(_COUNTING_ID, 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the debug message emitting behaviour should also be tested?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why? I'm not sure it's that important. I mean the debug message is already something that is purely informative. It's not something we really have to rely on that it is there, there is a lot of caching in a lot of places that we don't even log?
Testing logging seems mostly important for error/critical and maaaybe warning.