-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Warn if peak mem exceeds
allowed_mem
(#516)
* Warn if peak mem exceeds `allowed_mem` * Remove usage of Python 3.10 API (Counter.total) * Don't run mem warn test on Windows
- Loading branch information
Showing
3 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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,35 @@ | ||
import warnings | ||
from collections import Counter | ||
|
||
from cubed.runtime.pipeline import visit_nodes | ||
from cubed.runtime.types import Callback | ||
|
||
|
||
class MemoryWarningCallback(Callback): | ||
def on_compute_start(self, event): | ||
# store ops keyed by name | ||
self.ops = {} | ||
for name, node in visit_nodes(event.dag, event.resume): | ||
primitive_op = node["primitive_op"] | ||
self.ops[name] = primitive_op | ||
|
||
# count number of times each op exceeds allowed mem | ||
self.counter = Counter() | ||
|
||
def on_task_end(self, event): | ||
allowed_mem = self.ops[event.name].allowed_mem | ||
if ( | ||
event.peak_measured_mem_end is not None | ||
and event.peak_measured_mem_end > allowed_mem | ||
): | ||
self.counter.update({event.name: 1}) | ||
|
||
def on_compute_end(self, event): | ||
if sum(self.counter.values()) > 0: | ||
exceeded = [ | ||
f"{k} ({v}/{self.ops[k].num_tasks})" for k, v in self.counter.items() | ||
] | ||
warnings.warn( | ||
f"Peak memory usage exceeded allowed_mem when running tasks: {', '.join(exceeded)}", | ||
UserWarning, | ||
) |
This file contains 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 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