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

Function annotate_collective

advanced_source/cuda_graph_annotations_tutorial.py:664–696  ·  view source on GitHub ↗

Annotate a collective with the metadata eager NCCL traces expose. Returns a ``mark_kernels`` context manager. Any kernels launched inside (i.e. the collective) are tagged with the collective type, message sizes, dtype, and the process group's name/description/ranks, and placed on a

(collective_name, input_tensor, output_tensor, group=None)

Source from the content-addressed store, hash-verified

662# annotated trace consistent with non-graphed traces.
663
664def annotate_collective(collective_name, input_tensor, output_tensor, group=None):
665 """Annotate a collective with the metadata eager NCCL traces expose.
666
667 Returns a ``mark_kernels`` context manager. Any kernels launched inside
668 (i.e. the collective) are tagged with the collective type, message sizes,
669 dtype, and the process group's name/description/ranks, and placed on a
670 dedicated lane keyed by the process group so comms are visually separated
671 from compute.
672
673 The field names match the keys the profiler records for eager collectives
674 (``In msg nelems``, ``Group size``, ``Process Group Name``, ...), so an
675 annotated graphed collective reads exactly like a non-graphed one.
676 """
677 pg = group if group is not None else (dist.group.WORLD if dist.is_initialized() else None)
678 ranks = dist.get_process_group_ranks(pg) if pg is not None else [0]
679 group_name = getattr(pg, "group_name", "default")
680 group_desc = getattr(pg, "group_desc", "default")
681
682 # NCCL always uses its own internal stream, so key the lane on the process
683 # group (name + description) and give it a stable id (>= 60).
684 pg_key = f"{group_name}_{group_desc}"
685 annotation = {
686 "name": collective_name,
687 "In msg nelems": input_tensor.numel(),
688 "Out msg nelems": output_tensor.numel(),
689 "Group size": len(ranks),
690 "dtype": str(input_tensor.dtype).replace("torch.", ""),
691 "Process Group Name": group_name,
692 "Process Group Description": group_desc,
693 "Process Group Ranks": ranks,
694 "stream": get_stream_for_pg(pg_key),
695 }
696 return mark_kernels(annotation)
697
698###############################################################################
699# A Block That Mixes Compute and Communication

Callers 1

forwardFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected