-
Notifications
You must be signed in to change notification settings - Fork 61
Issue/282: fix bad shape while --enable-graph && OOM Exit mechanism #284
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
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 |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import os | ||
| import logging | ||
| from typing import Iterator | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def _iter_exception_chain( | ||
| e: BaseException, *, max_depth: int = 6 | ||
| ) -> Iterator[BaseException]: | ||
| """Iterate through exception chain with depth limit.""" | ||
| cur: BaseException | None = e | ||
| depth = 0 | ||
| seen: set[int] = set() | ||
| while cur is not None and depth < max_depth: | ||
| cur_id = id(cur) | ||
| if cur_id in seen: | ||
| break | ||
| seen.add(cur_id) | ||
| yield cur | ||
| depth += 1 | ||
| cur = cur.__cause__ or cur.__context__ | ||
|
|
||
|
|
||
| def is_oom_exception(e: BaseException) -> bool: | ||
| """ | ||
| Conservative OOM detector for MetaX allocator failures and CUDA/PyTorch OOMs. | ||
| Checks exception type (when available) and message substrings across chained exceptions. | ||
| """ | ||
| # PyTorch OOM exception type (only if torch is present in this environment) | ||
| try: | ||
| import torch # type: ignore | ||
|
|
||
| oom_type = getattr(torch, "OutOfMemoryError", None) | ||
| if oom_type is not None: | ||
| for ex in _iter_exception_chain(e): | ||
| if isinstance(ex, oom_type): | ||
| return True | ||
| except Exception: | ||
| pass | ||
|
|
||
| # Common patterns observed for allocator failures. | ||
| # Keep this allowlist small to avoid hard-exiting on unrelated errors. | ||
| patterns = ( | ||
| # MetaX / infinirt allocator | ||
| "hcmalloc", | ||
| "infinirtmalloc", | ||
| "out of memory", | ||
| # CUDA / driver / runtime alloc failures | ||
| "cuda out of memory", | ||
| "cumemalloc", | ||
| "cublas_status_alloc_failed", | ||
| "cudnn_status_alloc_failed", | ||
| ) | ||
|
|
||
| for ex in _iter_exception_chain(e): | ||
| msg = str(ex) | ||
| if not msg: | ||
| continue | ||
| msg_l = msg.lower() | ||
| if any(p in msg_l for p in patterns): | ||
| return True | ||
| return False | ||
|
|
||
|
|
||
| def handle_oom_and_exit(e: BaseException, exit_code: int = 137) -> None: | ||
| """Handle OOM exception by logging and exiting.""" | ||
| if is_oom_exception(e): | ||
| logger.error( | ||
| "OOM-like exception: exiting worker with code %d: %r", | ||
| exit_code, | ||
| e, | ||
| exc_info=False, | ||
| ) | ||
| os._exit(exit_code) |
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
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.
set_minus_one(graph_block_tables)does a full host allocation + H2D copy ofb * compiled_block_per_reqint32s on everyget_compiled()call. This can become a significant per-step overhead (and scales with the compiled dim1, nownblocks). Prefer a device-side fill (if available), or only fill the padding region[block_per_req:compiled_block_per_req]instead of the entire tensor, or reuse a cached host buffer to avoid repeated allocations.