-
Notifications
You must be signed in to change notification settings - Fork 5
feat: add --memory flag to measure RAM and VRAM per phase #861
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
29 commits
Select commit
Hold shift + click to select a range
6fdc38a
Add --memory flag to winml perf for process/device memory measurement
DingmaomaoBJTU e50a39c
Simplify memory output to single line with peak process + device memory
DingmaomaoBJTU a8bef09
Remove unnecessary del statement (CodeQL)
DingmaomaoBJTU 1b7d016
Use post-inference working set instead of process peak for memory dis…
DingmaomaoBJTU 753bec6
Merge remote-tracking branch 'origin/main' into dingmaomaobjtu/perf-m…
DingmaomaoBJTU 125c455
Fix CI: add missing memory_profile mock in TestPerfFormatJson
DingmaomaoBJTU 56704ba
refactor: align memory tracker with reference 3-phase approach
github-actions[bot] ed8fac1
feat: warmup EP before memory baseline
github-actions[bot] e57e8dc
fix: correct memory measurement phase ordering
github-actions[bot] 9665a54
simplify: reduce memory_tracker to ~100 lines
github-actions[bot] b4e8bd3
simplify: remove MemoryTracker/MemoryProfile classes
github-actions[bot] b927dc2
simplify: reduce _resolve_adapter_luid branching
github-actions[bot] b1a9c08
fix: take baseline after model load, before compile
github-actions[bot] e5b8408
fix: pre-import libs + warmup EP before baseline
github-actions[bot] 4eb603f
fix: baseline right before compile() for accurate model memory
github-actions[bot] b5795cb
fix: address PR review comments
github-actions[bot] e4e176a
feat: query device memory after compile and inference
github-actions[bot] 155547f
Remove device memory measurement (unreliable for NPU)
github-actions[bot] 7e68fef
Remove Device Mem line from hardware monitor display
github-actions[bot] c9c9fec
Remove device memory from live display and hw_monitor
github-actions[bot] 66b0c99
Revert "Remove device memory from live display and hw_monitor"
github-actions[bot] fccd5e1
Remove dim styling from memory breakdown line
github-actions[bot] b4b3e9f
Rename Mem → RAM and Device Mem → VRAM for clarity
github-actions[bot] e677812
Add VRAM phase measurement alongside RAM
github-actions[bot] 4b5db1d
Compact memory display to single line per metric
github-actions[bot] 62a0064
Split VRAM into local/shared in memory display
github-actions[bot] fce3cc3
Merge main and resolve conflict in perf.py
github-actions[bot] de15995
Fix CodeQL: initialize memory variables before conditional blocks
github-actions[bot] 11e573c
Fix type error: use self._single in _resolve_adapter_luid
github-actions[bot] 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,54 @@ | ||
| # ------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. | ||
| # -------------------------------------------------------------------------- | ||
| """Process memory helper for perf benchmarking.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| import os | ||
| import sys | ||
|
|
||
| import psutil | ||
|
|
||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def get_rss_mb() -> float: | ||
| """Return current RSS in MB for this process.""" | ||
| return psutil.Process(os.getpid()).memory_info().rss / (1024 * 1024) | ||
|
|
||
|
|
||
| def get_vram_mb(adapter_luid: str | None) -> tuple[float, float]: | ||
| """Return current VRAM usage as (local_mb, shared_mb) via PDH. | ||
|
|
||
| Returns (0.0, 0.0) on non-Windows, if no adapter_luid, or on failure. | ||
| """ | ||
| if sys.platform != "win32" or not adapter_luid: | ||
| return 0.0, 0.0 | ||
|
|
||
| try: | ||
| from ._pdh import PdhQuery | ||
|
|
||
| pid = os.getpid() | ||
| q = PdhQuery() | ||
| q.open() | ||
| q.add_counter( | ||
| "local", | ||
| rf"\GPU Process Memory(pid_{pid}_luid_{adapter_luid}_phys_0)\Local Usage", | ||
| ) | ||
| q.add_counter( | ||
| "shared", | ||
| rf"\GPU Process Memory(pid_{pid}_luid_{adapter_luid}_phys_0)\Shared Usage", | ||
| ) | ||
| # Memory counters are absolute (not rate-based), single collect suffices. | ||
| values = q.collect() | ||
| q.close() | ||
| local = (values.get("local") or 0) / (1024 * 1024) | ||
| shared = (values.get("shared") or 0) / (1024 * 1024) | ||
| return local, shared | ||
| except Exception: | ||
| logger.debug("VRAM query failed", exc_info=True) | ||
| return 0.0, 0.0 |
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,25 @@ | ||
| # ------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. | ||
| # -------------------------------------------------------------------------- | ||
| """Tests for the memory_tracker module.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from winml.modelkit.session.monitor.memory_tracker import get_rss_mb | ||
|
|
||
|
|
||
| class TestGetRssMb: | ||
| """Test process RSS retrieval.""" | ||
|
|
||
| def test_returns_positive_float(self) -> None: | ||
| rss = get_rss_mb() | ||
| assert isinstance(rss, float) | ||
| assert rss > 0 | ||
|
|
||
| def test_increases_after_allocation(self) -> None: | ||
| before = get_rss_mb() | ||
| _data = [bytearray(1024 * 1024) for _ in range(10)] # ~10 MB | ||
| after = get_rss_mb() | ||
| assert after >= before | ||
| assert _data is not None |
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.
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.