-
Notifications
You must be signed in to change notification settings - Fork 30
Leak testing #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
Open
ccummingsNV
wants to merge
11
commits into
main
Choose a base branch
from
dev/ccummings/leak-testing
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
Leak testing #490
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6e826e6
Fixes for slangpy leaking python object refs
ccummingsNV 1aca68a
Resolve type reflections by full name
ccummingsNV daf76d9
Add more object labels
ccummingsNV 35e11c0
Bind heap tostring
ccummingsNV 836635c
Correctly use full name in type util
ccummingsNV 444137f
Initial work on mem leak system
ccummingsNV 3f7b1e5
Merge remote-tracking branch 'origin/main' into dev/ccummings/fix-sla…
ccummingsNV 6321330
PR fixes
ccummingsNV 5f63c16
Merge branch 'dev/ccummings/fix-slangpy-leaks' into dev/ccummings/lea…
ccummingsNV 22af5d0
Mark lots of things as leaking, clean up tracking system
ccummingsNV 4476b9f
Merge branch 'main' into dev/ccummings/leak-testing
skallweitNV 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,5 +1,6 @@ | ||||||||||||||||
| # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||||||||||||||
|
|
||||||||||||||||
| import gc | ||||||||||||||||
| import hashlib | ||||||||||||||||
| import sys | ||||||||||||||||
| from pathlib import Path | ||||||||||||||||
|
|
@@ -26,7 +27,7 @@ | |||||||||||||||
| LogLevel, | ||||||||||||||||
| NativeHandle, | ||||||||||||||||
| ) | ||||||||||||||||
| from slangpy.types.buffer import NDBuffer | ||||||||||||||||
| from slangpy.types.buffer import NDBuffer, get_lookup_module | ||||||||||||||||
| from slangpy.core.function import Function | ||||||||||||||||
|
|
||||||||||||||||
| # Global variables for device isolation. If SELECTED_DEVICE_TYPES is None, no restriction. | ||||||||||||||||
|
|
@@ -44,6 +45,9 @@ | |||||||||||||||
| else: | ||||||||||||||||
| raise RuntimeError("Unsupported platform") | ||||||||||||||||
|
|
||||||||||||||||
| # If live object tracking is supported, enable leak tracking | ||||||||||||||||
| LEAK_TRACKING_ENABLED = hasattr(spy.Object, "report_live_objects") | ||||||||||||||||
|
Contributor
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. Should we move all the leak checking specifics into a new |
||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| # Called from pytest plugin if 'device-types' argument is provided | ||||||||||||||||
| def set_device_types(device_types_str: Optional[str]) -> None: | ||||||||||||||||
|
|
@@ -82,6 +86,59 @@ def set_device_types(device_types_str: Optional[str]) -> None: | |||||||||||||||
| USED_TORCH_DEVICES: bool = False | ||||||||||||||||
| METAL_PARAMETER_BLOCK_SUPPORT: Optional[bool] = None | ||||||||||||||||
|
|
||||||||||||||||
| TRACKED_LIVE_OBJECTS: Optional[list[Any]] = None | ||||||||||||||||
|
|
||||||||||||||||
| # Types to ignore when checking for leaked objects | ||||||||||||||||
| # - The reflection types are created and cached per device when buffers are loaded, so are hard | ||||||||||||||||
| # to identify as actual leaks. | ||||||||||||||||
| # - CoopVec is created on demand within the device when the coopvec api is used, and so will appear | ||||||||||||||||
|
Contributor
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.
Suggested change
|
||||||||||||||||
| # as a leak for cached devices. | ||||||||||||||||
| IGNORE_LIVE_OBJECT_TYPES = ["NativeSlangType", "TypeLayoutReflection", "TypeReflection", "CoopVec"] | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| def save_live_objects(): | ||||||||||||||||
| if LEAK_TRACKING_ENABLED: | ||||||||||||||||
| global TRACKED_LIVE_OBJECTS | ||||||||||||||||
| TRACKED_LIVE_OBJECTS = spy.Object.report_live_objects(True) | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| def compare_and_save_live_objects(allowed_leaks: Optional[dict[str, int]] = None): | ||||||||||||||||
| if LEAK_TRACKING_ENABLED: | ||||||||||||||||
| while gc.collect() > 0: | ||||||||||||||||
| pass | ||||||||||||||||
|
|
||||||||||||||||
| # Make a copy of allowed_leaks so we don't modify the original dict | ||||||||||||||||
| allowed_leaks = allowed_leaks.copy() if allowed_leaks else {} | ||||||||||||||||
|
|
||||||||||||||||
| # Get current live objects and compare to previous captured list | ||||||||||||||||
| global TRACKED_LIVE_OBJECTS | ||||||||||||||||
| new = spy.Object.report_live_objects(True) | ||||||||||||||||
| if TRACKED_LIVE_OBJECTS: | ||||||||||||||||
| errors = [] | ||||||||||||||||
|
|
||||||||||||||||
| # Build a lookup by address for fast comparison | ||||||||||||||||
| current_by_address = {x["object"]: x for x in TRACKED_LIVE_OBJECTS} | ||||||||||||||||
|
|
||||||||||||||||
| # Find any new objects, and build list of errors | ||||||||||||||||
| for obj in new: | ||||||||||||||||
| if obj["object"] not in current_by_address: | ||||||||||||||||
| cn = obj["class_name"] | ||||||||||||||||
| if not cn in IGNORE_LIVE_OBJECT_TYPES: | ||||||||||||||||
| if cn in allowed_leaks: | ||||||||||||||||
| if allowed_leaks[cn] > 0: | ||||||||||||||||
| allowed_leaks[cn] -= 1 | ||||||||||||||||
| continue | ||||||||||||||||
| errors.append(obj) | ||||||||||||||||
|
|
||||||||||||||||
| # If any errors, raise runtime error with all of them in | ||||||||||||||||
| if len(errors) > 0: | ||||||||||||||||
| msg = "\n".join([f" {e}" for e in errors]) | ||||||||||||||||
| raise RuntimeError(f"Leaked objects detected:\n{msg}") | ||||||||||||||||
|
|
||||||||||||||||
| # Store updated live objects list | ||||||||||||||||
| TRACKED_LIVE_OBJECTS = new | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| # Always dump stuff when testing | ||||||||||||||||
| spy.set_dump_generated_shaders(True) | ||||||||||||||||
| # spy.set_dump_slang_intermediates(True) | ||||||||||||||||
|
|
@@ -104,6 +161,9 @@ def close_all_devices(): | |||||||||||||||
|
|
||||||||||||||||
| torch.cuda.synchronize() | ||||||||||||||||
|
|
||||||||||||||||
| # Clear device cache | ||||||||||||||||
| DEVICE_CACHE.clear() | ||||||||||||||||
|
|
||||||||||||||||
| # Close all devices that were created during the tests. | ||||||||||||||||
| for device in Device.get_created_devices(): | ||||||||||||||||
| print(f"Closing device on shutdown {device.desc.label}") | ||||||||||||||||
|
|
@@ -231,7 +291,15 @@ def get_device( | |||||||||||||||
| ) | ||||||||||||||||
|
|
||||||||||||||||
| if use_cache: | ||||||||||||||||
| # Cache device | ||||||||||||||||
| DEVICE_CACHE[cache_key] = device | ||||||||||||||||
|
|
||||||||||||||||
| # When leak tracking, init the slangpy loopup cache up front and save live | ||||||||||||||||
| # objects so that we don't report cached device resources as leaks. | ||||||||||||||||
| if LEAK_TRACKING_ENABLED: | ||||||||||||||||
| get_lookup_module(device) | ||||||||||||||||
| save_live_objects() | ||||||||||||||||
|
|
||||||||||||||||
| return device | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
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
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
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
Oops, something went wrong.
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.
Can we move this definition to the slangpy pytest plugin somehow?