Skip to content

Commit 4bfbdda

Browse files
committed
Label CUDA Graph backward annotations
## Human Note ## Agent note Preserve the prototype annotation API's `autograd_phase` metadata when generating GPU annotation boxes. Backward kernels inherited from a forward region now render as labels such as `attention backward`, while unowned backward plumbing can retain the explicit `backward` label. ## Test Plan ```bash ~/.venvs/nightly/bin/python -m pytest test/test_profiler.py test/test_perfetto.py -q uvx ruff check transformer_nuggets/utils/perfetto.py test/test_perfetto.py uvx ruff format --check transformer_nuggets/utils/perfetto.py test/test_perfetto.py prek ```
1 parent 742466e commit 4bfbdda

2 files changed

Lines changed: 37 additions & 6 deletions

File tree

test/test_perfetto.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,29 @@ def test_cuda_graph_annotations_become_contiguous_gpu_boxes():
7373
assert len(trace["traceEvents"]) == 3
7474

7575

76+
def test_cuda_graph_annotation_boxes_label_backward_phase():
77+
graph_id = 2
78+
trace = {
79+
"traceEvents": [
80+
{
81+
"ph": "X",
82+
"cat": "kernel",
83+
"name": "kernel",
84+
"pid": 0,
85+
"tid": 7,
86+
"ts": 10,
87+
"dur": 3,
88+
"args": {"graph id": graph_id, "graph node id": 1},
89+
}
90+
]
91+
}
92+
annotations = {(graph_id << 32) | 1: [{"name": "attention", "autograd_phase": "backward"}]}
93+
94+
processed = add_cuda_graph_annotation_boxes(trace, annotations)
95+
96+
assert processed["traceEvents"][-1]["name"] == "attention backward"
97+
98+
7699
def test_cuda_graph_annotation_boxes_accept_monitor_embedded_metadata():
77100
trace = {
78101
"traceEvents": [

transformer_nuggets/utils/perfetto.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,21 @@ def write_trace(path: str | Path, trace: dict[str, Any], *, indent: int | None =
6666
json.dump(trace, f, indent=indent)
6767

6868

69-
def _annotation_name(entries: Sequence[Any] | None) -> str | None:
70-
name = None
69+
def _annotation_label(entries: Sequence[Any] | None) -> str | None:
70+
metadata: dict[str, Any] = {}
7171
for annotation in entries or ():
72-
if isinstance(annotation, dict) and "name" in annotation:
73-
name = str(annotation["name"])
72+
if isinstance(annotation, dict):
73+
metadata.update(annotation)
7474
elif isinstance(annotation, str):
75-
name = annotation
75+
metadata["name"] = annotation
76+
77+
name = metadata.get("name")
78+
if name is None:
79+
return None
80+
name = str(name)
81+
autograd_phase = metadata.get("autograd_phase")
82+
if autograd_phase == "backward" and name != "backward":
83+
return f"{name} backward"
7684
return name
7785

7886

@@ -133,7 +141,7 @@ def add_cuda_graph_annotation_boxes(
133141
embedded = None
134142
if isinstance(embedded, list):
135143
entries = embedded
136-
name = _annotation_name(entries)
144+
name = _annotation_label(entries)
137145
if name is not None:
138146
annotated_work[(event.get("pid"), event.get("tid"))].append((event, name))
139147

0 commit comments

Comments
 (0)