-
Notifications
You must be signed in to change notification settings - Fork 106
[release/0.3] Fix memory leak caused by c_pointers #1435
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
risemeup1111
wants to merge
1
commit into
PaddlePaddle:release/0.3
Choose a base branch
from
risemeup1111:cherry-pick/1325/release/0.3
base: release/0.3
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,65 @@ | |
|
|
||
| import paddle | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Patch CUTLASS DSL scalar types to cache __c_pointers__ results. | ||
| # Prevents pymalloc arena fragmentation from short-lived ctypes objects | ||
| # created on every kernel launch. Must run before any CUTLASS kernel is called. | ||
| # --------------------------------------------------------------------------- | ||
| def _patch_cutlass_cptr_cache(maxsize=4096): | ||
| try: | ||
| from cutlass.base_dsl import typing as _typing | ||
| except Exception: | ||
| return 0 | ||
| _logger = logging.getLogger(__name__) | ||
| _warned = set() | ||
| count = 0 | ||
|
|
||
| def _make_cached(orig_fn, cache, cls_name): | ||
| def _cached(self): | ||
| v = self.value | ||
| r = cache.get(v) | ||
| if r is None: | ||
| r = orig_fn(self) | ||
| if len(cache) < maxsize: | ||
| cache[v] = r | ||
| elif cls_name not in _warned: | ||
| _warned.add(cls_name) | ||
| _logger.warning( | ||
| f"[cptr_cache] {cls_name}.__c_pointers__ cache exceeded " | ||
| f"{maxsize} entries. Dynamic scalar values may cause " | ||
| f"pymalloc arena fragmentation." | ||
| ) | ||
| return r | ||
|
|
||
| return _cached | ||
|
|
||
| _Numeric = getattr(_typing, "Numeric", None) | ||
| for name in dir(_typing): | ||
| cls = getattr(_typing, name) | ||
| if ( | ||
| isinstance(cls, type) | ||
| and "__c_pointers__" in cls.__dict__ | ||
| and _Numeric | ||
| and issubclass(cls, _Numeric) | ||
| and cls is not _Numeric | ||
| and not name.startswith("_") | ||
| ): | ||
| cache = {} | ||
| cls.__c_pointers__ = _make_cached(cls.__c_pointers__, cache, name) | ||
| count += 1 | ||
| return count | ||
|
|
||
|
|
||
| _patched_count = _patch_cutlass_cptr_cache() | ||
| if _patched_count: | ||
| logging.getLogger(__name__).warning( | ||
| f"[cptr_cache] Patched {_patched_count} CUTLASS DSL types" | ||
|
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. 🟡 建议 这里的成功日志和 PR 描述要求的验证信号不一致。 PR 描述让使用者搜索 建议统一为描述中的固定格式,例如 |
||
| ) | ||
| del _patch_cutlass_cptr_cache, _patched_count | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| from .utils import ( | ||
| HardwareIncompatibleBlocker, | ||
| ModuleContext, | ||
|
|
||
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.
🟡 建议 这里会把
cutlass.base_dsl.typing导入过程中的任意异常静默吞掉。当前补丁是否生效完全依赖这个导入;如果安装了
nvidia-cutlass-dsl但其依赖或初始化异常,函数会直接返回 0,后面也不会打印失败原因,训练进程仍然带着未缓存的__c_pointers__继续跑。按 checklist §C,except Exception吞异常且无日志属于错误静默。建议只把
ImportError/ModuleNotFoundError作为 CUTLASS 不存在的正常路径;对其它异常至少记录 warning 后返回,或让异常暴露出来。