MCPcopy Create free account
hub / github.com/pytorch/tutorials / main

Function main

advanced_source/cuda_graph_annotations_tutorial.py:541–595  ·  view source on GitHub ↗

End-to-end CUDA graph annotation and profiling demo.

()

Source from the content-addressed store, hash-verified

539# annotations, profile it, and post-process the trace.
540
541def main():
542 """End-to-end CUDA graph annotation and profiling demo."""
543 if not torch.cuda.is_available():
544 raise SystemExit("CUDA required for this tutorial")
545
546 # Check if annotation support is available
547 # PyTorch will log a warning if cuda-bindings version is too old
548 supported = not _is_tools_id_unavailable()
549 print(f"Annotation support available: {supported}")
550 if not supported:
551 print("NOTE: Annotation API not available.")
552 print("This could be due to:")
553 print(" - Driver/CUDA-compat < 13.1")
554 print(" - Outdated cuda-bindings (check PyTorch warnings above)")
555 print("Annotations will not be recorded, but the demo will still run.")
556 print("Kernels will be reassigned to the default lane, not semantic lanes.\n")
557
558 output_dir = Path("traces")
559
560 # Build the model
561 print("\n1. Building transformer block model...")
562 model_fn = build_transformer_block()
563
564 # Capture graph with annotations
565 print("\n2. Capturing CUDA graph with annotations...")
566 graph, output = capture_graph_with_annotations(model_fn)
567
568 # Save annotations
569 print("\n3. Saving annotation metadata...")
570 annotations_path = save_annotations(output_dir)
571
572 # Profile the graph
573 print("\n4. Profiling graph replays...")
574 raw_trace_path = profile_graph(graph, output_dir)
575
576 # Post-process the trace
577 print("\n5. Post-processing: merging annotations into trace...")
578 annotated_path, raw_trace, annotated_trace = post_process_trace(
579 raw_trace_path, annotations_path, output_dir
580 )
581
582 # Compare before and after
583 print("\n6. Comparing traces...")
584 compare_traces(raw_trace, annotated_trace)
585
586 # Summary
587 print("\n" + "="*60)
588 print("SUMMARY")
589 print("="*60)
590 print(f"Raw trace: {raw_trace_path}")
591 print(f"Annotated trace: {annotated_path}")
592 print(f"Annotations: {annotations_path}")
593 print("\nOpen the annotated trace in https://ui.perfetto.dev/ to visualize")
594 print("the semantic kernel lanes.")
595 print("="*60)
596
597# Example output:
598# if __name__ == "__main__":

Callers

nothing calls this directly

Calls 6

build_transformer_blockFunction · 0.85
save_annotationsFunction · 0.85
profile_graphFunction · 0.85
post_process_traceFunction · 0.85
compare_tracesFunction · 0.85

Tested by

no test coverage detected