+
    Nåûj<ù  ã                  ó,  a € 0 t $ R t^ RIHt ^ RIt^ RIt^ RIt^ RIt^ RI	t	^ RI
Ht ^ RIHt ^ RIHt ^ RIHtHtHtHt ^ RIHt ]'       d	   ^ RI
HtHt ^ RIt^ R	IHtHtHt  ^ R
IHt H!t" ]'       g   Rt Rt"]! ]$4      t%]&]]'],          3,          t(R])R&   ]*]+],]+,          3,          t-R])R&   Rs.R])R&   Rs/R])R&   Rs0R])R&   . t1R])R&   Rs2R])R&   R R lt3R R lt4R R lt5R s6R])R!&   ],! 4       s7R"])R#&   R$ R% lt8R& R' lt9R( R) lt:R* R+ lt;R, R- lt<R. R/ lt=R0 R1 lt>R2 R3 lt?R4 R5 lt@R6tAR7tBR8 R9 ltCR: R; ltDR< R= ltER> R? ltFR@ RA ltGRB RC ltHRD RE ltIRF RG ltJRHRRIR/RJ RK lltK ! RL RM4      tL/ tMRN])RO&   ]Pœ                  ! 4       tORP RQ ltPRR RS ltQRT RU ltRRV RW ltSRsTRX])RY&   RZ R[ ltURsVRX])R\&   RsWRX])R]&   R^ R_ ltXR` Ra ltYRb Rc ltZ. t[Rd])Re&   Rft\Rg Rh lt]Ri Rj lt^Rk Rl lt_]`! 4       taRm])Rn&    ! Ro Rp]4      tbRq Rr ltcRs Rt ltd ! Ru Rv]PÊ                  4      tf]`! 4       tgRm])Rw&   Rx Ry lthRz R{ lti]R| R} l4       tj]R~R/R€ R� ll4       tkR‚ Rƒ ltlR„ R… ltmR† R‡ ltnRˆ R‰ ltoRŠ R‹ ltp ! RŒ R�]]+RŽ3,          4      tq]q! 4       trR� R� lts ! R‘ R’]]+]t3,          4      tu]u! 4       tvR“ R” ltwR• R– ltxR— R˜ lty]! R™]zRš7      R› Rœ l4       t{R� Rž lt|^<s}RŸ])R &   / t~R¡])R¢&   R£ R¤ ltR¥ R¦ lt€]R§ R¨ l4       t�R#   ]# d	    Rt Rt" EL¦i ; i)©aó  Annotate CUDA graph kernel nodes during capture.

During CUDA graph capture, ``mark_kernels`` records the current capture
frontier and the direct dependents already attached to that frontier.
On scope exit it walks only the newly added dependent edges to find the
nodes created within the scope. Each kernel, memcpy, or memset node found
is annotated by its ``toolsId`` so it can later be matched to profiler
trace events.

``mark_kernels`` now snapshots capture state from whatever stream is
current on scope entry, so that stream must already be participating in
the capture. ``mark_stream`` handles this by starting ``mark_kernels``
before switching to the target stream.

The annotations are baked into a Chrome profiler trace by
``prof.export_chrome_trace(path, cuda_graph_annotations=get_kernel_annotations())``.

Requires ``cuda.bindings`` package and a CUDA driver that supports
``cudaGraphNodeGetToolsId`` (CUDA >= 13.1 or appropriate cuda-compat).
When unavailable, ``mark_kernels`` silently becomes a no-op.

Annotation recording is enabled per capture via ``torch.cuda.graph``'s
``enable_annotations`` argument, which also resolves and remaps the recorded
annotations to the exec graph automatically on context exit.

Usage during capture::

    from torch.cuda._graph_annotations import mark_kernels, get_kernel_annotations

    with torch.cuda.graph(graph, enable_annotations=True):
        with mark_kernels("phase_A"):
            y = workload_a(x)
        with mark_kernels("phase_B"):
            z = workload_b(y)

    annotations = get_kernel_annotations()

When you need to drive this outside the context manager's automatic path,
``resolve_and_remap(graph)`` resolves pending annotations and applies the graph's
selected keying mode, as instantiation does.
)ÚannotationsN)ÚMapping)Úcontextmanager)Ú	getLogger)ÚAnyÚ
NamedTupleÚTYPE_CHECKINGÚ	TypeAlias)Ú
deprecated)ÚIterableÚIterator)Ú_check_cuda_bindingsÚ_check_cuda_bindings_driverÚ_HAS_CUDA_BINDINGS)ÚdriverÚruntimer	   Ú_CaptureStateÚ_ExistingDirectDependentszbool | NoneÚ_tools_id_availableFÚboolÚ_annotations_enabledÚ	edge_walkÚstrÚ_annotation_backendz2list[tuple[dict[str, Any], dict[str, Any] | None]]Ú_active_scopesú
int | NoneÚ_capture_root_graph_idc               ó   € V ^8„  d   QhRR/# ©é   Úreturnr   © )Úformats   "ÚYC:\Ai_Coding\apps\studio\ComfyUI\.venv\Lib\site-packages\torch\cuda\_graph_annotations.pyÚ__annotate__r$   ‹   s   € ÷ "ñ "˜zñ "ó    c                 ó   € \         # )zkThe active capture's top-level graph id, or ``None`` outside a capture that recorded
one. Not a public API.)r   r!   r%   r#   Úcapture_root_graph_idr'   ‹   s
   € ô "Ð!r%   c               ó    € V ^8„  d   QhRRRR/# )r   Úenabledr   r    ÚNoner!   )r"   s   "r#   r$   r$   ‘   s   € ÷ 
 ñ 
  dð 
 ¨tñ 
 r%   c                ór   € V s V '       g-   Rs\        P                  4        \        P                  4        R# R# )z}Set whether annotation recording is active. Used by ``torch.cuda.graph``
to scope annotations to a capture; not a public API.N)r   r   r   ÚclearÚ_body_graph_ids)r)   s   &r#   Ú_set_annotations_enabledr.   ‘   s0   € ð #ÐßØ!%Ðä×ÑÔä×ÑÖñ r%   c               ó    € V ^8„  d   QhRRRR/# )r   Úbackendr   r    r*   r!   )r"   s   "r#   r$   r$   ž   s   € ÷ "ñ " Sð "¨Tñ "r%   c                ó
   € V s R# )a…  Set how ``mark_kernels`` scopes discover their nodes for this capture.

Separate from :func:`_set_annotations_enabled` because the two are decided at different
points: recording is enabled before ``capture_begin`` (``maybe_stamp_capture_root`` is
gated on it), while the backend is only final once the CUPTI callback has actually been
armed, which needs the capture live. Not a public API.N)r   )r0   s   &r#   Ú_set_annotation_backendr2   ž   s
   € ð "Òr%   ÚexecÚ_annotation_key_byúset[int]r-   c               ó    € V ^8„  d   QhRRRR/# )r   Úkey_byr   r    r*   r!   )r"   s   "r#   r$   r$   µ   s   € ÷  ñ   3ð  ¨4ñ  r%   c                ó
   € V s R# )z{Publish this capture's key space. Set by ``torch.cuda.graph`` alongside
:func:`_set_annotations_enabled`. Not a public API.N©r4   )r7   s   &r#   Ú_set_annotation_key_byr:   µ   s
   € ð  Òr%   c               ó   € V ^8„  d   QhRR/# ©r   r    r   r!   )r"   s   "r#   r$   r$   ¼   s   € ÷ *ñ *�dñ *r%   c                 ó   € \         R8H  # )z›Whether the active capture keeps its annotations on the capture graph, i.e. whether
a body node's own id is a key a consumer can resolve. Not a public API.Úsourcer9   r!   r%   r#   Úsource_keyedr?   ¼   s   € ô  Ñ)Ð)r%   c               ó    € V ^8„  d   QhRRRR/# )r   Úgraph_idÚintr    r*   r!   )r"   s   "r#   r$   r$   Â   s   € ÷ "ñ " ð "¨ñ "r%   c                ó0   € \         P                  V 4       R# )zNRecord that this capture annotated into a nested body graph. Not a public API.N)r-   Úadd)rA   s   &r#   Únote_body_graph_idrE   Â   s   € ä×Ñ˜Ö!r%   c               ó   € V ^8„  d   QhRR/# )r   r    r5   r!   )r"   s   "r#   r$   r$   Ç   s   € ÷ ñ ˜Xñ r%   c                 ó(   € \         \        4       up s V # )zNThe body graph ids annotated since the last call, and reset. Not a public API.)r-   Úset)Úidss    r#   Útake_body_graph_idsrJ   Ç   s   € ô +¬C«EÐ€CˆØ€Jr%   c               ó   € V ^8„  d   QhRR/# ©r   r    údict[str, Any] | Noner!   )r"   s   "r#   r$   r$   Î   s   € ÷ ñ Ð1ñ r%   c                 ó�   € \         '       d:   \         R,          w  rVe   V\        4       J d   V # \         P                  4        KE  R# )zúThe annotation to attribute a node created right now to, or ``None``.

Entries whose region has been restored out from under them are dropped first: see
:data:`_active_scopes`, a backward bracket whose node raised leaves one behind. Not a
public API.Néÿÿÿÿ)r   Ú_current_regionÚpop)Ú
annotationÚregions     r#   Úcurrent_annotationrT   Î   s:   € ÷ ‹.Ü+¨BÕ/Ñˆ
ØŠ>˜V¤Ó'8Ó8ØÐÜ×ÑÖÙr%   c               ó$   € V ^8„  d   QhRRRRRR/# )r   Útools_idrB   rR   údict[str, Any]r    r*   r!   )r"   s   "r#   r$   r$   Ü   s!   € ÷ ,ñ , Sð ,°nð ,Èñ ,r%   c                ó   € \        W4       R# )zôAttribute one graph node, keyed by its capture-side ``toolsId``.

The CUPTI backend's entry point into the same store the edge walk fills, so both
backends are remapped to exec-graph ids by ``remap_to_exec_graph`` identically. Not a
public API.N)Ú_merge_annotation)rV   rR   s   &&r#   Úrecord_node_annotationrZ   Ü   s   € ô �hÖ+r%   c               ó    € V ^8„  d   QhRRRR/# )r   Úgraphr   r    rB   r!   )r"   s   "r#   r$   r$   å   s   € ÷ ñ �Sð ˜Sñ r%   c                ó@   € \        \        P                  ! V 4      4      # )zäThe driver's unique id for a ``cudaGraph_t``.

Identifies a graph across its whole lifetime, unlike the handle itself: that is a
pointer, so a graph created after another is destroyed can reuse the value and
compare equal to it.)r   Ú_cuda_runtimeÚcudaGraphGetId)r\   s   &r#   Ú	_graph_idr`   å   s   € ô  Ü×$Ò$ UÓ+óð r%   c               ó    € V ^8„  d   QhRRRR/# ©r   Útorch_cuda_graphútorch.cuda.CUDAGraphr    r*   r!   )r"   s   "r#   r$   r$   ð   s   € ÷ .ñ .Ð/Cð .Èñ .r%   c                ó>  € Rs \        '       d   \        4       '       d   R# \        P                  P                  4       P                  p\        V4      pVf   R# \        V^ ,          4      s \         V n	        V P                  P                  \         4       RV n        R# )a‚  Record the top-level capture graph of the capture just begun, and stamp it on the
graph for the later remap.

Called by ``torch.cuda.graph`` right after ``capture_begin``, while the current stream is
still capturing into the top-level graph. It has to go through the stream because the
``cudaGraph_t`` does not exist until ``capture_end`` -- ``raw_cuda_graph()`` raises before
then -- but the template graph keeps that one id for its whole life, so this single read
serves every consumer: the conditional-body checks in ``mark_kernels``, the CUPTI
backend's body-node filter, and ``remap_to_exec_graph`` via the stamp below. Not a public
API.N)r   r   Ú_is_tools_id_unavailableÚtorchÚcudaÚcurrent_streamÚcuda_streamÚ_get_capture_stater`   Ú_capture_graph_idÚ_recorded_exec_idsrD   Ú_remapped_exec_id)rc   Ústream_handleÚstates   &  r#   Úmaybe_stamp_capture_rootrq   ð   s|   € ð "ÐßÓÔ#;×#=Ò#=ÙÜ—J‘J×-Ñ-Ó/×;Ñ;€MÜ˜}Ó-€EØ‚}ÙÜ& u¨Q¥xÓ0ÐÜ)?ÐÔ&Ø×'Ñ'×+Ñ+Ô,BÔCà)-ÐÖ&r%   c               ó   € V ^8„  d   QhRR/# r<   r!   )r"   s   "r#   r$   r$   
  s   € ÷ )ñ )˜ñ )r%   c                 óp  € \        \        R4      '       g8   \        P                  P	                  R4      p \
        P                  RV 4       R# \        P                  ! ^ 4      vrV\        P                  P                  \        P                  P                  39  d   \
        P                  RV4       R# R# )aD  Probe whether cudaGraphNodeGetToolsId is supported by the driver.

Calls with a null node and accepts only the errors that prove the API is
really there: cudaErrorInvalidValue, i.e. the driver got as far as rejecting
the argument. Every other status means we cannot use it -- an old NVIDIA
driver answers cudaErrorCallRequiresNewerDriver, while an environment with no
CUDA driver behind the bindings at all (ROCm, where cuda-bindings can still
import) answers cudaErrorInsufficientDriver. Allowlisting rather than
denylisting keeps a new failure mode from reading as "supported".
ÚcudaGraphNodeGetToolsIdzcuda-bindingszãcudaGraphNodeGetToolsId API not found in cuda-bindings. Current version: %s, required: >= 13.1.0. CUDA graph kernel annotations will be disabled. To enable annotations, upgrade cuda-bindings: pip install --upgrade cuda-bindingsFz•cudaGraphNodeGetToolsId is unusable (%s); it needs a CUDA driver >= 13.1 or an equivalent cuda-compat. CUDA graph kernel annotations will be disabledT)Úhasattrr^   Ú	importlibÚmetadataÚversionÚloggerÚwarningrt   ÚcudaError_tÚcudaSuccessÚcudaErrorInvalidValueÚinfo)Úcuda_bindings_versionÚerrÚ_s      r#   Ú_probe_tools_idr‚   
  s¦   € ô ”=Ð";×<Ò<ä )× 2Ñ 2× :Ñ :¸?Ó KÐä�‰ð2ð
 "ô	
ñ Ü×3Ò3Ø	ó€G€Cð 	ä×%Ñ%×1Ñ1Ü×%Ñ%×;Ñ;ð
ô	
ô 	�‰ðð ô		
ñ Ùr%   i`ý ið2  c               ó   € V ^8„  d   QhRR/# r   r!   )r"   s   "r#   r$   r$   =  s   € ÷ ñ ˜
ñ r%   c                 óš   €  ^ RI Hp   T ! 4       P                  4       #   \         d     R# i ; i  \        \
        \        3 d     R# i ; i)a¥  CUPTI's version, or ``None`` when it cannot be asked: no cupti-python, or no
libcupti to load (which includes every platform whose CUPTI is not ``libcupti.so``).

Asks Cuspy's process-wide wrapper rather than dlopening libcupti here: ``torch``
front-loads the CUPTI wheel at import (``_preload_cuda_deps``), so this is the copy
already mapped into the process, and its soname stays defined in one place. Not a
public API.©Ú
pylibcuptiN)Ú"torch.profiler._cuspy.cupti_pythonr†   ÚImportErrorÚget_versionÚAttributeErrorÚOSErrorÚRuntimeErrorr…   s    r#   Ú_cupti_versionr�   =  sP   € ðÝAðÙ‹|×'Ñ'Ó)Ð)øô ô Úðûô œG¤\Ð2ô ò ðús   ‚ ‰0 ž-¬-°A
Á	A
c               ó   € V ^8„  d   QhRR/# r<   r!   )r"   s   "r#   r$   r$   Q  s   € ÷ Dñ D 4ñ Dr%   c                 óÚ   € \         '       g   R# \        4       p V e   V \        8  d   R# \        pVP                  P
                  pVP                  4       w  r4W28H  ;'       d
    V\        8¬  # )aÿ  Whether a node's source (capture-time) graph node is reported on replayed work,
which is what lets annotations stay keyed to the capture graph instead of being rekeyed
to each exec graph. CUPTI surfaces it as ``sourceGraphNodeId``: the field arrived in the
13.4 ABI and only a 13.4 user-mode driver fills it in, so both are checked. A CUPTI too
old to have the field is the quiet failure worth catching -- a 13.4 driver with a 13.3
CUPTI reports nothing and the annotations resolve to nothing. Not a public API.F)r   r�   Ú_MIN_SOURCE_NODE_CUPTI_VERSIONr^   r{   r|   ÚcudaDriverGetVersionÚ_MIN_SOURCE_NODE_DRIVER_VERSION)Úcupti_versionÚrtÚokr€   rx   s        r#   Úsource_node_ids_availabler–   Q  s`   € ÷ ÓÙÜ"Ó$€MØÒ  ]Ô5SÔ%SÙÜ	€BØ	�‰×	#Ñ	#€BØ×*Ñ*Ó,�L€CØ‰9×CÐC˜Ô$CÑCÐCr%   c               ó   € V ^8„  d   QhRR/# r<   r!   )r"   s   "r#   r$   r$   c  s   € ÷ #ñ # $ñ #r%   c                 ón   € \         '       g   R# \        e   \        '       * # \        4       s\        '       * # )z5Return True if cudaGraphNodeGetToolsId is not usable.T)r   r   r‚   r!   r%   r#   rf   rf   c  s.   € ÷ ÓÙÜÒ&Ü&Ô&Ð&Ü)Ó+ÐÜ"Ô"Ð"r%   c               ó   € V ^8„  d   QhRR/# r<   r!   )r"   s   "r#   r$   r$   n  s   € ÷ Hñ H�dñ Hr%   c                 ól   € \         P                  P                  4       ;'       d    \        4       '       * # )aŸ  is_available() -> bool

Return whether CUDA graph annotation recording is supported.

Requires a CUDA device, the ``cuda-bindings`` package, and a driver
that supports ``cudaGraphNodeGetToolsId`` (CUDA >= 13.1 or an
equivalent cuda-compat package). When this returns ``False``,
:func:`mark_kernels` is a silent no-op and no annotations are
recorded.

The first call may probe the CUDA driver; the result is cached.
)rg   rh   Úis_availablerf   r!   r%   r#   r›   r›   n  s&   € ô �:‰:×"Ñ"Ó$×GÐGÔ-EÓ-GÔ)GÐGr%   c               ó    € V ^8„  d   QhRRRR/# )r   Ústreamr   r    z_CaptureState | Noner!   )r"   s   "r#   r$   r$   ~  s   € ÷ *ñ *˜sð *Ð';ñ *r%   c                óª   € \        \        P                  ! V 4      4      w  rr4rVV\        P                  P                  8w  d   R# V\        VRV 4      3# )zBReturn ``(graph, frontier)`` for an active capture, else ``None``.N)r   r^   ÚcudaStreamGetCaptureInfoÚcudaStreamCaptureStatusÚcudaStreamCaptureStatusActiveÚlist)r�   ÚstatusÚ_idr\   Ú_depsÚ
_edge_dataÚ	_num_depss   &      r#   rk   rk   ~  sZ   € ä7KÜ×.Ò.Øó	
ó8Ñ4€F�˜zð 	Ü×0Ñ0×NÑNô	Oñ Ø”$�u˜Z˜iÐ(Ó)Ð)Ð)r%   c               ó    € V ^8„  d   QhRRRR/# )r   r\   r   r    ú	list[Any]r!   )r"   s   "r#   r$   r$   �  s   € ÷ #ñ #˜3ð # 9ñ #r%   c                ó¶   € \        \        P                  ! V 4      4      w  rV^ 8X  d   . # \        \        P                  ! WR7      4      w  r2\        VRV 4      # )z,Return the current root nodes for the graph.)ÚpNumRootNodesN)r   r^   ÚcudaGraphGetRootNodesr¢   )r\   r�   Ú	num_rootsÚrootss   &   r#   Ú_get_root_nodesr¯   �  s_   € ä'Ü×+Ò+Øó	
ó�L€Að
 �A„~Øˆ	Ü+Ü×+Ò+Øô	
óÑ€Eô
 ��j�yÐ!Ó"Ð"r%   c               ó    € V ^8„  d   QhRRRR/# )r   Únoder   r    r©   r!   )r"   s   "r#   r$   r$   ž  s   € ÷ -ñ -˜sð - yñ -r%   c                óº   € \        \        P                  ! V 4      4      w   rV^ 8X  d   . # \        \        P                  ! WR7      4      w  r4p\        VRV 4      # )z-Return the direct dependents of a graph node.)ÚpNumDependentNodesN)r   r^   ÚcudaGraphNodeGetDependentNodesr¢   )r±   r�   Únum_dependentsÚ
dependentsr¦   s   &    r#   Ú_get_dependent_nodesr·   ž  se   € ä/Ü×4Ò4Øó	
óÑ€A€qð
 ˜ÔØˆ	Ü-AÜ×4Ò4Øô	
ó.Ñ*€J˜Nô
 �
˜?˜NÐ+Ó,Ð,r%   c               ó    € V ^8„  d   QhRRRR/# )r   r±   r   r    r!   )r"   s   "r#   r$   r$   ¯  s   € ÷ 	ñ 	˜ð 	 ñ 	r%   c                ó@   € \        \        P                  ! V 4      4      # )a  Return graph node type without tripping runtime bugs on newer node kinds.

The runtime ``cudaGraphNodeGetType`` can return ``cudaErrorUnknown`` (999)
for valid nodes whose driver type is ``CU_GRAPH_NODE_TYPE_BATCH_MEM_OP``.
Query via the driver API instead.
)r   Ú_cuda_driverÚcuGraphNodeGetType©r±   s   &r#   Ú_get_node_typer½   ¯  s   € ô 'Ü×'Ò'¨Ó-óð r%   Úexisting_direct_dependentsÚinclude_start_nodesc               ó(   € V ^8„  d   QhRRRRRRRR/# )	r   Ústart_nodesr©   r¾   z _ExistingDirectDependents | Noner¿   r   r    zdict[int, Any]r!   )r"   s   "r#   r$   r$   »  s3   € ÷ !ñ !Øð!ð !Að!ð ð	!ð
 ñ!r%   c               óÎ  € T;'       g    / pV  Uu0 uF  p\        V4      kK  	  pp/ p\        V 4      pV'       d   V  F  pW5\        V4      &   K  	  V'       d‡   VP                  4       pVP                  \        V4      \	        4       4      p\        V4       FB  p\        V4      p	W—9   g   W”9   d   K  VP                  V	4       W…V	&   VP                  V4       KD  	  KŽ  V# u upi )a  Walk dependent edges starting at ``start_nodes``.

``existing_direct_dependents`` maps each node in ``start_nodes`` to
the direct dependent node keys that were already present at scope
entry. Those edges are skipped so the traversal only follows nodes
added after scope entry.
)rB   r¢   rQ   ÚgetrH   r·   rD   Úappend)
rÁ   r¾   r¿   r±   ÚseenÚdescendantsÚstackÚold_dependentsÚ	dependentÚdependent_keys
   &$$       r#   Ú_collect_descendantsrË   »  sÍ   € ð "<×!AÐ!A¸rÐÙ"-Ó.¡+˜$ŒC�ŽI¡+€DÐ.Ø"$€KÜ�Ó€EçÛˆDØ%)œ˜D›	Ó"ñ  ÷ Ø�y‰y‹{ˆØ3×7Ñ7¼¸D»	Ä3Ã5ÓIˆÜ-¨dÖ3ˆIÜ 	›NˆMØÔ.°-Ô2GÙØ�H‰H�]Ô#Ø)2˜Ñ&Ø�L‰L˜Ö#ó 4ð Ðùò' /s   ‘C"c                  ó"   € ] tR tRtR R ltRtR# )Ú_GraphAnnotationsiß  c               ó   € V ^8„  d   QhRR/# ©r   r    r*   r!   )r"   s   "r#   r$   Ú_GraphAnnotations.__annotate__à  s   € ÷ ,ñ ,˜$ñ ,r%   c                	ó@   € / V n         \        4       V n        / V n        R # ©N)r   rH   Úsourceless_nodesÚ	py_stacks©Úselfs   &r#   Ú__init__Ú_GraphAnnotations.__init__à  s   € Ø68ˆÔÜ*-«%ˆÔà)+ˆŽr%   )r   rÔ   rÓ   N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__r×   Ú__static_attributes__r!   r%   r#   rÍ   rÍ   ß  s   † ÷,ñ ,r%   rÍ   zdict[int, _GraphAnnotations]Ú_GRAPH_ANNOTATIONSc               ó    € V ^8„  d   QhRRRR/# )r   rV   rB   r    r*   r!   )r"   s   "r#   r$   r$   î  s   € ÷ :ñ : 3ð :¨4ñ :r%   c                ó  € \        4       '       g   R# \        ;_uu_ 4        \        P                  V ^ ,	          \	        4       4      pVP
                  P                  V R,          4       RRR4       R#   + '       g   i     R# ; i)zìRecord that this node's type carries no source node id, so a source-keyed capture
still aliases it into exec space. Called by whichever backend discovered the node (it
has the node type in hand; the registry does not). Not a public API.Nì   ÿÿ )r?   Ú_REGISTRY_LOCKrÞ   Ú
setdefaultrÍ   rÓ   rD   ©rV   r\   s   & r#   Únote_sourceless_noderå   î  sR   € ô �>Š>Ùß	ŽÜ"×-Ñ-¨h¸"­nÔ>OÓ>QÓRˆØ×Ñ×"Ñ" 8¨jÕ#8Ô9÷ 
��Šús   ¡AA3Á3B	c               ó$   € V ^8„  d   QhRRRRRR/# )r   rV   rB   rR   r   r    r*   r!   )r"   s   "r#   r$   r$   ù  s!   € ÷ )ñ ) ð )°ð )¸ñ )r%   c                óÈ  € \        V\        4      '       d   TMRV/p\        ;_uu_ 4        \        P	                  V ^ ,	          \        4       4      pV R,          pVP                  P                  V4      pVf$   \        V4      VP                  V&    RRR4       R# VP                  4        F  w  rgVP	                  Wg4       K  	  RRR4       R#   + '       g   i     R# ; i)aì  Merge one scope's annotation into a node's entry; the first write wins per key.

First-wins is what makes nested scopes resolve inner-first: scopes are recorded in
completion order, so the innermost scope containing a node reaches it first and keeps
the keys it shares with its enclosing scopes, while their extra keys still come
through. Non-dict annotations are normalized to ``{"name": ...}``; ``mark_kernels``
already does that for strings, but the store is written by other callers too.
Únamerá   N)	Ú
isinstanceÚdictrâ   rÞ   rã   rÍ   r   rÃ   Úitems)rV   rR   Úincomingr\   Únode_idÚentryÚkeyÚvalues   &&      r#   rY   rY   ù  s­   € ô (¨
´D×9Ò9‰zÀÈ
Ð?S€Hß	ŽÜ"×-Ñ-¨h¸"­nÔ>OÓ>QÓRˆØ˜ZÕ'ˆØ×!Ñ!×%Ñ% gÓ.ˆØŠ=Ü)-¨h«ˆE×Ñ˜gÑ&Ø÷ 
‰ð #Ÿ.™.Ö*‰JˆCØ×Ñ˜SÖ(ñ +÷ 
��Šús   «A'CÂ*CÃC!	c               ó$   € V ^8„  d   QhRRRRRR/# )r   rV   rB   rÇ   r   r    r*   r!   )r"   s   "r#   r$   r$     s!   € ÷ 7ñ 7 3ð 7¨sð 7°tñ 7r%   c                óÐ   € \         ;_uu_ 4        \        P                  V ^ ,	          \        4       4      pWP                  V R,          &   RRR4       R#   + '       g   i     R# ; i©é    rá   N)râ   rÞ   rã   rÍ   rÔ   )rV   rÇ   r\   s   && r#   Úrecord_node_py_stackrõ     s>   € ß	ŽÜ"×-Ñ-¨h¸"­nÔ>OÓ>QÓRˆØ16�‰˜ :Õ-Ñ.÷ 
��Šús   �;AÁA%	c               ó    € V ^8„  d   QhRRRR/# )r   rV   rB   r    rM   r!   )r"   s   "r#   r$   r$     s   € ÷ Wñ W˜Sð WÐ%:ñ Wr%   c                óä   € \         ;_uu_ 4        \        P                  V ^ ,	          4      pVf   RM!VP                  P                  V R,          4      uuRRR4       #   + '       g   i     R# ; i)zàThe merged annotation recorded for one graph node, or ``None``. The in-process
accessor behind the profiler's graph annotation resolver, handing out the stored dict
rather than the list-wrapped public view. Not a public API.Nrá   )râ   rÞ   rÃ   r   rä   s   & r#   Úannotation_forrø     sH   € ÷ 
ŽÜ"×&Ñ& x°2¥~Ó6ˆØ’}‰t¨%×*;Ñ*;×*?Ñ*?ÀÈ:Õ@UÓ*V÷ 
��‹ús   �AAÁA/	zset[Any] | NoneÚ_ANNOTATABLE_TYPESc               ó   € V ^8„  d   QhRR/# ©r   r    zset[Any]r!   )r"   s   "r#   r$   r$   '  s   € ÷ ñ  ñ r%   c                 óÜ   € \         f`   \        P                  p V P                  V P                  V P
                  V P                  V P                  V P                  V P                  0s \         # rÒ   )
rù   rº   ÚCUgraphNodeTypeÚCU_GRAPH_NODE_TYPE_KERNELÚCU_GRAPH_NODE_TYPE_MEMCPYÚCU_GRAPH_NODE_TYPE_MEMSETÚCU_GRAPH_NODE_TYPE_BATCH_MEM_OPÚCU_GRAPH_NODE_TYPE_EVENT_RECORDÚCU_GRAPH_NODE_TYPE_WAIT_EVENTÚCU_GRAPH_NODE_TYPE_HOST©Ú
node_typess    r#   Ú_get_annotatable_typesr  '  sb   € äÒ!Ü!×1Ñ1ˆ
à×0Ñ0Ø×0Ñ0Ø×0Ñ0Ø×6Ñ6Ø×6Ñ6Ø×4Ñ4Ø×.Ñ.ð
Ðô Ðr%   Ú_NESTED_GRAPH_TYPESÚ_SOURCELESS_NODE_TYPESc               ó   € V ^8„  d   QhRR/# rû   r!   )r"   s   "r#   r$   r$   J  s   € ÷ "ñ " Hñ "r%   c                 ón   € \         f)   \        P                  p V P                  V P                  0s \         # rÒ   )r	  rº   rý   r  rÿ   r  s    r#   Ú_get_sourceless_node_typesr  J  s5   € äÒ%Ü!×1Ñ1ˆ
à×.Ñ.Ø×0Ñ0ð"
Ðô "Ð!r%   c               ó    € V ^8„  d   QhRRRR/# )r   Ú	node_typer   r    r   r!   )r"   s   "r#   r$   r$   U  s   € ÷ 9ñ 9 sð 9¨tñ 9r%   c                ó   € V \        4       9  # )a  Whether CUPTI reports a source (capture-graph) node id for this node type, i.e.
whether an annotation kept on the capture graph can name it. The driver enums are int
subclasses, so this takes the runtime enum the walk has or the plain int CUPTI reports
just as well. Not a public API.)r  )r  s   &r#   Únode_type_has_source_idr  U  s   € ð
 Ô6Ó8Ñ8Ð8r%   c               ó   € V ^8„  d   QhRR/# rû   r!   )r"   s   "r#   r$   r$   ]  s   € ÷ ñ  ñ r%   c                 ón   € \         f)   \        P                  p V P                  V P                  0s \         # rÒ   )r  rº   rý   ÚCU_GRAPH_NODE_TYPE_GRAPHÚCU_GRAPH_NODE_TYPE_CONDITIONALr  s    r#   Ú_get_nested_graph_typesr  ]  s5   € äÒ"Ü!×1Ñ1ˆ
à×/Ñ/Ø×5Ñ5ð
Ðô Ðr%   zlist[tuple[Any, list[int]]]Ú_pending_scopesÚcuda_graph_annotation_regionc               ó   € V ^8„  d   QhRR/# rL   r!   )r"   s   "r#   r$   r$   �  s   € ÷ ñ Ð.ñ r%   c                 óž   € \         P                  P                  \        4      '       d$   \         P                  P	                  \        4      # R # rÒ   )rg   Ú_CÚ_is_key_in_tlsÚ_REGION_TLS_KEYÚ_get_obj_in_tlsr!   r%   r#   rP   rP   �  s/   € Ü‡x�x×Ñœ×/Ò/Ü�x‰x×'Ñ'¬Ó8Ð8Ùr%   c               ó    € V ^8„  d   QhRRRR/# )r   Ú	collapsedrW   r    rM   r!   )r"   s   "r#   r$   r$   –  s   € ÷ ñ ˜^ð Ð0Eñ r%   c                ób   € \        4       p\        P                  P                  \        V 4       V# )zEInstall ``collapsed`` as the current region; return the previous one.)rP   rg   r  Ú_stash_obj_in_tlsr  )r  Úprevs   & r#   Ú_enter_regionr#  –  s#   € äÓ€DÜ	‡H�H×Ñœ°	Ô:Ø€Kr%   c               ó    € V ^8„  d   QhRRRR/# )r   r"  rM   r    r*   r!   )r"   s   "r#   r$   r$   �  s   € ÷ :ñ :Ð,ð :°ñ :r%   c                ó    € V f&   \         P                  P                  \        4       R # \         P                  P	                  \        V 4       R # rÒ   )rg   r  Ú_remove_obj_from_tlsr  r!  )r"  s   &r#   Ú_exit_regionr'  �  s-   € Ø‚|Ü�‰×%Ñ%¤oÖ6ä�‰×"Ñ"¤?°DÖ9r%   r   Ú_HOOKED_KEYc                  óD   € ] tR tRt$ RtR]R&   R]R&   R]R&   R	]R
&   RtR# )Ú_KernelScopei«  zGCapture-frontier snapshot taken at scope entry, consumed at scope exit.r   r\   r©   Úfrontierzset[int] | NoneÚentry_root_keysr   Úentry_direct_dependentsr!   N)rÙ   rÚ   rÛ   rÜ   Ú__doc__Ú__annotations__rÝ   r!   r%   r#   r*  r*  «  s   ‡ ÙQàƒJØÓØ$Ó$Ø6×6r%   r*  c               ó   € V ^8„  d   QhRR/# )r   r    z_KernelScope | Noner!   )r"   s   "r#   r$   r$   ´  s   € ÷ Sñ SÐ0ñ Sr%   c                 óŒ  € \         P                  P                  4       P                  p \	        V 4      pVf   R# Vw  r#RpV UUu/ uF1  p\        V4      \        V4       Uu0 uF  p\        V4      kK  	  upbK3  	  pppV'       g$   \        V4       Uu0 uF  p\        V4      kK  	  pp\        W#WG4      # u upi u uppi u upi )zJSnapshot the current stream's capture frontier; ``None`` if not capturing.N)	rg   rh   ri   rj   rk   rB   r·   r¯   r*  )r�   Úcapture_stater\   r+  r,  r±   Údepr-  s           r#   Ú_begin_kernel_scoper4  ´  s¼   € ä�Z‰Z×&Ñ&Ó(×4Ñ4€FÜ& vÓ.€MØÒÙØ#�O€Eà'+€OáPXôÙPXÈŒˆD‹	Ô(<¸TÔ(BÓCÑ(B ”C˜–HÑ(BÑCÒCÑPXð ñ ÷ Ü1@ÀÔ1GÓHÑ1G¨œ3˜tž9Ñ1GˆÐHÜ˜¨ÓRÐRùò	 Dùóùò Is   ÁB;Á!B6Á5B;ÂCÂ6B;c               ó    € V ^8„  d   QhRRRR/# )r   Úscoper*  r    z	list[int]r!   )r"   s   "r#   r$   r$   Å  s   € ÷ +ñ +˜\ð +¨iñ +r%   c                óþ  € V P                   '       d#   \        V P                   V P                  R7      pM_\        V P                  4       Uu. uF3  p\        V4      V P                  ;'       g    \        4       9  g   K1  VNK5  	  pp\        VRR7      p\        4       p\        4       p\        4       p. pVP                  4        Fƒ  p\        V4      pW…9   d   VP                  VP                  4       W„9  d   K7  \        \        P                   ! V4      4      p	\#        V4      '       g   \%        V	4       VP'                  V	4       K…  	  V'       d&   \(        P*                  ! R\-        V4       R2^R7       V# u upi )zEWalk nodes captured since ``scope`` was begun; return their toolsIds.)r¾   T)r¿   z"mark_kernels: this scope contains zy nodes; the work inside them is in a separate cudaGraph_t that this walk does not descend into, so it is left unannotated©Ú
stacklevel)r+  rË   r-  r¯   r\   rB   r,  rH   r  r  Úvaluesr½   rD   rè   r   r^   rt   r  rå   rÄ   ÚwarningsÚwarnÚsorted)
r6  Úscope_nodesr±   Ú	new_rootsÚannotatableÚnestedÚnested_seenÚ	tools_idsr  rV   s
   &         r#   Ú_end_kernel_scoperD  Å  sP  € à‡~‡~€~Ü*Ø�N‰NØ',×'DÑ'Dô
‰ô (¨¯©Ô4ó
á4�Ü�4‹y ×!6Ñ!6×!?Ð!?¼#»%Ñ@÷ ˆDÙ4ð 	ð 
ô
 +¨9È$ÔOˆä(Ó*€KÜ$Ó&€FÜ›E€KØ€IØ×"Ñ"Ö$ˆÜ" 4Ó(ˆ	ØÔØ�O‰O˜IŸN™NÔ+ØÔ'ÙÜ'Ü×1Ò1Øóó
ˆô
 ' y×1Ò1Ü  Ô*Ø×Ñ˜Ö"ñ %÷ ô 	�ŠØ0´¸Ó1DÐ0Eð F.ð .ð õ		
ð ÐùòG
s   Á!E:Á.E:Á?E:c                  ó&   € ] tR tRtRtR R ltRtR# )Ú_BracketStateió  a  Per-thread stack of open backward brackets for one hook pair.

Thread-local so concurrent executions of a retained node (separate
graph tasks on different engine threads) cannot interleave each
other's scope snapshots; within one thread pre/post pair up LIFO.

If the node throws, its posthook never runs (the engine's
call_function has no try/finally around fn) and the prehook's entry
is left on the stack. That is harmless. Say a retained node runs
three times on one thread and the second run throws:

    run 1: prehook push [e1]; posthook pops e1     -> []
    run 2: prehook push [e2]; node throws, no pop  -> [e2]
    run 3: prehook push [e2, e3]; posthook pops e3 -> [e2]

Pops always take the top, so the leaked e2 sits below all later
entries and every subsequent run still pops its own. The only loss
is e2 itself: that run's kernels go unrecorded, which is moot since
its backward failed. (The region/creation-hook TLS the prehook set
is not leaked -- the engine task's ThreadLocalStateGuard restores
those on unwind.)
c               ó   € V ^8„  d   QhRR/# rÏ   r!   )r"   s   "r#   r$   Ú_BracketState.__annotate__  s   € ÷ #ñ #˜$ñ #r%   c                	ó   € . V n         R # rÒ   ©rÇ   rÕ   s   &r#   r×   Ú_BracketState.__init__  s	   € à "ˆŽ
r%   rJ  N)rÙ   rÚ   rÛ   rÜ   r.  r×   rÝ   r!   r%   r#   rF  rF  ó  s   † ñ÷.#ñ #r%   rF  Ú_SKIPPEDc               ó    € V ^8„  d   QhRRRR/# ©r   r±   r   r    r*   r!   )r"   s   "r#   r$   r$     s   € ÷ )ñ )˜cð ) dñ )r%   c                óü   € \        V \        P                  P                  P                  4      '       d   R# \
        V P                  9   d   R# \        4       pVf   R# RV P                  \
        &   \        W4       R# )aU  Node creation hook: freeze the current region into ``node``'s bracket.

Reads the region TLS slot and, if a region is open, installs the node's
single backward hook pair closing over that snapshot. Every backward-
annotating ``mark_kernels`` scope pushes one of these (and so does an
executing hooked node, to cover nodes created during its backward), so
a node created under nested scopes sees several identical firings; the
``node.metadata`` marker makes only the first attach.

AccumulateGrad is always excluded: a leaf's node is created once and
cached (possibly during warmup), so which scope would own it is an
accident of first use, and its work (the ``.grad`` accumulation)
belongs to the leaf rather than to any forward region. If accumulation
ever needs annotation it should get a dedicated leaf-accumulation
region, not first-use assignment.
NT)	ré   rg   r  Ú
_functionsÚAccumulateGradr(  rw   rP   Ú_attach_backward_hooks)r±   Úfrozens   & r#   Ú_freeze_region_hookrT    sY   € ô" �$œŸ™×+Ñ+×:Ñ:×;Ò;ÙÜ�d—m‘mÔ#ÙÜÓ€FØ‚~ÙØ!%€D‡M�M”+ÑÜ˜4Ö(r%   c               ó$   € V ^8„  d   QhRRRRRR/# )r   r±   r   rS  rW   r    r*   r!   )r"   s   "r#   r$   r$   2  s&   € ÷ S!ñ S! ð S!¨nð S!Àñ S!r%   c                ó˜   aaa€ \        4       oR R loR VVV3R llpR V3R llpV P                  V4       V P                  V4       R# )aO  Register one pre/post hook pair bracketing ``node``'s backward execution.

``frozen`` is the region that was current when the node was created:
the annotations of every scope then open, already collapsed inner-wins.
The pair brackets the node's execution the same way ``mark_kernels``
brackets forward work, and splits on the backend the same way: under the
edge walk it snapshots the capture frontier before and collects the new
nodes after, under CUPTI it publishes itself as the ambient annotation
for the nodes created while it runs. It runs on the autograd engine
thread executing the node, which during a whole-graph capture
participates in the capture; when backward runs outside a capture the
prehook sees no active capture and the pair is a no-op.

Executing the node is semantically re-entering its frozen region, so
the prehook merges ``frozen`` over the live region (a ``mark_kernels``
enclosing the whole ``backward()`` call, if any) and installs the
result while the node runs. That gives nodes created during its
backward (``create_graph=True``, checkpoint recomputation) the forward
region's ownership -- a creation hook is pushed alongside so they get
hooked even when this backward runs eagerly and no scope's hook is
live -- and it puts a ``mark_kernels`` opened inside the execution
dynamically inner, so it outranks ``frozen``, which outranks the
enclosing scope. The posthook records the merged region once; scope
completion order then yields exactly that precedence at resolve.

The TLS pops are exception-safe without a finally: each engine task runs
under an ``at::ThreadLocalStateGuard`` that restores both the
creation-hook TLS and the region slot even when the node throws. The
ambient-annotation entry the CUPTI path publishes is module-global and so
is not restored that way; it is dropped on next read instead (see
:data:`_active_scopes`).
c               ó    € V ^8„  d   QhRRRR/# )r   Úchildr   r    r*   r!   )r"   s   "r#   r$   Ú,_attach_backward_hooks.<locals>.__annotate__U  s   € ÷ #ñ #˜Sð # Tñ #r%   c                ó   € \        V 4       R # rÒ   ©rT  )rX  s   &r#   Úcreation_hookÚ-_attach_backward_hooks.<locals>.creation_hookU  s
   € Ü˜EÖ"r%   c               ó    € V ^8„  d   QhRRRR/# )r   Ú_grad_outputsr   r    r*   r!   )r"   s   "r#   r$   rY  X  s   € ÷ Dñ D˜sð D tñ Dr%   c                ó  <€ \        4       '       d"   S	P                  P                  \        4       R # / \	        4       ;'       g    / CSCp/ VCRR/Cp\        V4      p\        P                  P                  P                  S4       \        ;'       d
    \        R8H  p\        \        4      pV'       d   \        P                  W!34       \        '       d   V'       g   \        4       MR pS	P                  P                  WcW$V34       R # )NÚautograd_phaseÚbackwardÚcupti)rf   rÇ   rÄ   rL  rP   r#  rg   r  Ú	_autogradÚ_push_node_creation_hookr   r   Úlenr   r4  )
r_  ÚmergedÚtaggedr"  Ú	publishedÚdepthr6  r\  rS  rp   s
   &      €€€r#   ÚprehookÚ'_attach_backward_hooks.<locals>.prehookX  sÕ   ø€ Ü#×%Ò%Ø�K‰K×ÑœxÔ(Ùð 9”_Ó&×,Ð,¨"Ð8°Ð8ˆØ9�FÐ9Ð,¨jÑ9ˆÜ˜VÓ$ˆÜ�‰×Ñ×3Ñ3°MÔBô )×KÐKÔ-@ÀGÑ-Kˆ	Ü”NÓ#ˆßÜ×!Ñ! 6Ð"2Ô3ç%9Ó%9Ç)ÔÔ!ÐQUð 	ð 	�‰×Ñ˜E¨¸EÐBÖCr%   c               ó$   € V ^8„  d   QhRRRRRR/# )r   Ú_grad_inputsr   r_  r    r*   r!   )r"   s   "r#   r$   rY  s  s!   € ÷ 8ñ 8˜sð 8°3ð 8¸4ñ 8r%   c                ór  <€ S	P                   '       g   R # S	P                   P                  4       pV\        J d   R # Vw  r4rVpV'       d
   \        VR 1 \        P
                  P                  P                  4        \        V4       Vf   R # \        V4      pV'       d   \        P                  WX34       R # R # rÒ   )rÇ   rQ   rL  r   rg   r  rd  Ú_pop_node_creation_hookr'  rD  r  rÄ   )
rn  r_  rî   r6  r"  rh  ri  rj  rC  rp   s
   &&       €r#   ÚposthookÚ(_attach_backward_hooks.<locals>.posthooks  sŒ   ø€ Ø�{�{ˆ{ÙØ—‘—‘Ó!ˆØ”HÓÙØ05Ñ-ˆ�V¨ßÜ˜u™vÐ&Ü�‰×Ñ×2Ñ2Ô4Ü�TÔØŠ=ÙÜ% eÓ,ˆ	ßÜ×"Ñ" FÐ#6Ö7ñ r%   N)rF  Úregister_prehookÚregister_hook)r±   rS  rk  rq  r\  rp   s   &f  @@r#   rR  rR  2  sF   ú€ ôB ‹O€Eõ#÷Dò D÷68ð 8ð" 	×Ñ˜'Ô"Ø×Ñ�xÖ r%   c               ó    € V ^8„  d   QhRRRR/# )r   rR   rW   rb  r   r!   )r"   s   "r#   r$   r$   ‰  s   € ÷ ñ  >ð ¸Tñ r%   c              #  óP  "  € R R lp\        / \        4       ;'       g    / CV C4      p V'       d@   \        P                  P                  P                  V4      ;_uu_ 4        Rx € RRR4       MRx € \        V4       R#   + '       g   i     L; i  \        T4       i ; i5i)a•  Publish ``annotation`` as the current region for the body, hooking the autograd
nodes created in it when ``backward``.

Backend-independent, and shared by both of ``mark_kernels``' paths. The region is
entered even when ``backward`` is False, since it must reflect the full dynamic scope
for nodes hooked by a nested ``backward=True`` scope (or by an executing hooked node)
to freeze this annotation too.
c               ó    € V ^8„  d   QhRRRR/# rN  r!   )r"   s   "r#   r$   Ú(_annotation_region.<locals>.__annotate__“  s   € ÷ "ñ "˜Cð " Dñ "r%   c                ó   € \        V 4       R # rÒ   r[  r¼   s   &r#   r\  Ú)_annotation_region.<locals>.creation_hook“  s
   € Ü˜DÖ!r%   N)r#  rP   rg   Úautogradr\   Únode_creation_hookr'  )rR   rb  r\  r"  s   &&  r#   Ú_annotation_regionr}  ˆ  s|   é € õ"ô ÐDœ_Ó.×4Ð4°"ÐD¸ÐDÓE€DðßÜ—‘×%Ñ%×8Ñ8¸×GÕGÛ÷ HÐGó ä�TÖ÷ H×Gûô
 	�TÕüs9   ‚(B&«B ³1B Á$BÁ)B Á6B&ÂB	ÂB ÂB#Â#B&rb  Tc               ó    € V ^8„  d   QhRRRR/# )r   rR   ústr | dict[str, Any]rb  r   r!   )r"   s   "r#   r$   r$   ¢  s   € ÷ 8ñ 8Ð1ð 8Àñ 8r%   c             #  ó  "  € \         '       d   \        4       '       d   Rx € R# \        V \        4      '       d   RV /p \        R8X  dk   \        4       p\        \        4      p\        P                  V'       d   / VCV CMT R34        \        W4      ;_uu_ 4        Rx € RRR4       \        VR1 R# \        4       pVf   Rx € R# \        e=   \        VP                  4      \        8w  d   \        P                  ! R^R7       Rx € R# \        W4      ;_uu_ 4        Rx € RRR4       \!        V4      pV'       d   \"        P                  W34       R# R#   + '       g   i     LÀ; i  \        TR1 i ; i  + '       g   i     L^; i5i)aÿ  mark_kernels(annotation, *, backward=True)

Context manager that annotates GPU work captured within its scope.

Must be used inside an active :class:`torch.cuda.graph` capture with
``enable_annotations=True``. Every kernel, memcpy, and memset node the
capture adds within the scope is tagged with :attr:`annotation`. Outside
a capture, with annotations disabled, or when :func:`is_available` is
``False``, the context manager is a no-op.

When scopes overlap on the same node (e.g. nested scopes), their
annotation dicts are merged key-by-key with the inner scope winning
common keys.

By default backward work is annotated too: autograd nodes created by
forward operations inside the scope get hooks (via
:class:`torch.autograd.graph.node_creation_hook`) that bracket their
backward execution, so when the backward pass is itself captured --
in the same capture as the forward or in a later one -- its kernels
are tagged with the same annotation, plus an ``"autograd_phase":
"backward"`` key marking them as backward work (``"autograd_phase"``
is therefore reserved: backward annotation overwrites it). When
backward runs outside a capture the hooks are a no-op. Ownership
extends to higher-order gradients: nodes created while a hooked node
executes (``create_graph=True``, checkpoint recomputation) inherit its
annotations, so a later grad-of-grad capture is attributed too.
``AccumulateGrad`` nodes are never annotated: a leaf's node is created
once and cached, so scope ownership would be an accident of first use.
Pass ``backward=False`` to annotate only the forward work, e.g. when a
wrapper implements its own backward attribution. The keyword's
presence also serves as the feature probe for that native backward
support: ``"backward" in inspect.signature(mark_kernels).parameters``.

Implementation: on entry, records the current stream's capture frontier
and its existing direct dependents; on scope exit, walks only the
dependent nodes added since entry (falling back to newly created graph
roots when the scope is the first captured work).

Args:
    annotation (str or dict): Metadata to attach to each captured node.
        A string ``s`` is recorded as ``{"name": s}``. Dict values must
        be picklable. The key ``"name"`` names the region in trace
        tooling; ``"stream"`` is reserved for stream-lane assignment.
    backward (bool): Whether to also annotate the backward kernels of
        autograd nodes created inside the scope. Default: ``True``.

.. note::
    The nodes to annotate must be reachable from the capture frontier of
    the stream that is current on scope entry. Work on a different
    already-capturing stream must be synchronized with the current
    stream first.

.. note::
    Child-graph and conditional nodes have bodies in a separate
    ``cudaGraph_t`` that this walk does not descend into, so their work is
    left unannotated and a warning is issued. A scope *inside* a conditional
    body (``torch.cond`` / ``torch.while_loop``) records nothing for the same
    reason: with the default ``annotation_config["key_by"]`` a body node's id
    is not rekeyed to the exec graph, so the annotation would match nothing in
    a trace.

.. warning::
    This API is in prototype and may change in future releases.

Example::

    >>> # xdoctest: +SKIP("requires cuda-bindings and driver >= 13.1")
    >>> g = torch.cuda.CUDAGraph()
    >>> x = torch.randn(8, device="cuda")
    >>> with torch.cuda.graph(g, enable_annotations=True):
    ...     with torch.cuda.graph_annotations.mark_kernels("phase_A"):
    ...         y = x + 1
Nrè   rc  zãmark_kernels: this scope is inside a CUDA graph conditional-node body (torch.cond / torch.while_loop), which is captured into a separate cudaGraph_t whose node ids are not remapped to the exec graph; nothing is annotated for itr8  )r   rf   ré   r   r   rT   rf  r   rÄ   r}  r4  r   r`   r\   r;  r<  rD  r  )rR   rb  Ú	enclosingrj  r6  rC  s   &$    r#   Úmark_kernelsr‚  ¡  s@  é € ÷V  ÓÔ#;×#=Ò#=ÛÙä�*œc×"Ò"Ø˜jÐ)ˆ
ä˜gÔ%ô 'Ó(ˆ	Ü”NÓ#ˆÜ×Ñß,5Ð(�	Ð(˜ZÑ(¸:ÀtÐLô	
ð	'Ü# J×9Õ9Û÷ :ô ˜u™vÐ&ÙäÓ!€EØ‚}ÛÙô 	Ò*Ü�e—k‘kÓ"Ô&<Ô<ô 	�Šðð õ	
ó 	Ùä	˜J×	1Õ	1Û÷ 
2ô " %Ó(€IßÜ×Ñ 
Ð6Ö7ñ ÷C :×9ûô ˜u™vÑ&ú÷4 
2×	1üsO   ‚BFÂ	E, ÂEÂ!E, Â)A6FÄE:Ä$5FÅE)	Å$E, Å,E7Å7FÅ:F
	ÆFc               ó   € V ^8„  d   QhRR/# rÏ   r!   )r"   s   "r#   r$   r$   $  s   € ÷  ñ   Tñ  r%   c                 ó  € \         '       g   R#  \          F  w  rV F  p\        W 4       K  	  K  	  \         P                  4        R#   \         d    \        P	                  R4        L8i ; i  \         P                  4        i ; i)z7Resolve pending scope toolsIds into kernel annotations.Nz"resolve_pending_annotations failed)r  rY   Ú	Exceptionry   Ú	exceptionr,   )rR   rC  rV   s      r#   Úresolve_pending_annotationsr‡  $  sm   € ç‹?Ùð	 ÷ &5Ñ!ˆJÛ%�Ü! (Ö7ó &ñ &5ô 	×ÑÖøô ô ?Ü×ÑÐ=Ö>ð?ûô 	×ÑÕús#   �#A	 Á	 A,Á)A/ Á+A,Á,A/ Á/Bc               ó    € V ^8„  d   QhRRRR/# rb   r!   )r"   s   "r#   r$   r$   5  s   € ÷ Gñ GÐ2Fð GÈ4ñ Gr%   c                ó€   € \         P                  4        V P                  '       g   \        V P                  4       R# R# )zJDiscard entries if capture_end failed before creating an executable graph.N)r  r,   Ú_has_graph_execÚremove_kernel_annotationsrm   ©rc   s   &r#   Údiscard_capture_annotationsr�  5  s.   € ä×ÑÔØ×+×+Ð+Ü!Ð"2×"EÑ"EÖFñ ,r%   c               ó    € V ^8„  d   QhRRRR/# rb   r!   )r"   s   "r#   r$   r$   <  s   € ÷ -;ñ -;Ð*>ð -;À4ñ -;r%   c                ót  € V P                   pVf   R# \        \        P                  ! V P	                  4       4      4      pV P
                  f   TMV P
                  pW28X  d   R# \        ;_uu_ 4        \        P                  VR4      pVe
   V\        V&   W n        RRR4       R#   + '       g   i     R# ; i)a  Remap annotation keys from capture graph ID to exec graph ID.

During capture, toolsId encodes the capture graph's ID in the upper
32 bits. After instantiation, the profiler uses the exec graph's ID.
This function rewrites the keys so annotations match the trace.

The graph's capture id is read from the ``_capture_graph_id`` stamped on it
by ``maybe_stamp_capture_root`` at capture_begin, so only the annotations
belonging to this graph are rekeyed. This is order-independent and correct
when several graphs are captured in sequence: call once per graph. Graphs
captured with annotations disabled have no capture id and are skipped.

The exec graph id is only defined once the graph is instantiated. With
``keep_graph=True`` instantiation is deferred past the ``torch.cuda.graph()``
context, so the remap is driven from the graph's ``instantiate()``/
``replay()`` instead of context exit. Each ``instantiate()`` (even on an
unmodified template) produces a fresh exec graph id, so this rekeys from
wherever the annotations are currently keyed -- the capture id before the
first remap, the previous exec id after a re-instantiate -- to the current
exec id. ``_remapped_exec_id`` tracks that current key; when it already
matches the live exec id (e.g. replay after instantiate) this is a no-op.
N)	rl   r   r^   ÚcudaGraphExecGetIdÚraw_cuda_graph_execrn   râ   rÞ   rQ   )rc   Úcapture_graph_idÚexec_graph_idÚcurrent_key_idr\   s   &    r#   Úremap_to_exec_graphr•  <  s£   € ð. (×9Ñ9ÐØÒÙä(Ü×(Ò(Ø×0Ñ0Ó2ó	
ó€Mð ×-Ñ-Ò5ñ 	à×/Ñ/ð ð
 Ô&Ùç	ŽÜ"×&Ñ& ~°tÓ<ˆØÒØ05Ô˜}Ñ-Ø-:Ô*÷	 
��Šús   Á2*B&Â&B7	c               ó    € V ^8„  d   QhRRRR/# )r   rc   rd   r    r   r!   )r"   s   "r#   r$   r$   l  s   € ÷ *ñ *Ø*ð*àñ*r%   c                óò  € V P                   pVf   R# \        ;_uu_ 4        \        P                  V4      pVe   VP                  '       g    RRR4       R# \        \        P                  ! V P                  4       4      4      pV P                  pWC8X  d   VuuRRR4       # \        4       pVP                  P                  4        Fe  pWbP                  9   d!   VP                  V,          VP                  V&   WbP                  9   g   KE  VP                  V,          VP                  V&   Kg  	  Ve   \        P                  VR4       V\        V&   W0n	        VuuRRR4       #   + '       g   i     R# ; i)a9  Give this graph's source-id-less nodes an exec-keyed copy of their annotation.

The counterpart of :func:`remap_to_exec_graph` for a capture that chose
``annotation_config={"key_by": "source"}``: its annotations stay on the capture graph,
but host and (peer-to-peer) memcpy nodes are reported by CUPTI without a source node
id, so a consumer can only name them by their exec node id. Those entries -- and only
those -- are copied to the exec key, leaving the capture key in place: the two keys
share one annotation dict, so a later merge into either is seen through both.

Each instantiate() mints a fresh exec id, so the previous instantiate's aliases are
dropped first rather than left behind. Returns the exec graph id the aliases are under
(for the caller's destroy bookkeeping), or None when there was nothing to alias.
N)rl   râ   rÞ   rÃ   rÓ   r   r^   r�  r‘  rn   rÍ   Úcopyr   rÔ   rQ   )rc   r’  r\   r“  ÚpreviousÚaliasesrí   s   &      r#   Úalias_sourceless_to_exec_graphr›  l  s1  € ð  (×9Ñ9ÐØÒÙß	ŽÜ"×&Ñ&Ð'7Ó8ˆØŠ= × 6× 6Ð 6Ø÷ 
‰ô
 -Ü×,Ò,Ø ×4Ñ4Ó6óó
ˆð
 $×5Ñ5ˆØÔ$Ø ÷ 
Šô $Ó%ˆØ×-Ñ-×2Ñ2Ö4ˆGØ×+Ñ+Ô+Ø/4×/@Ñ/@ÀÕ/I�×#Ñ# GÑ,ØŸ/™/Ö)Ø-2¯_©_¸WÕ-E�×!Ñ! 'Ó*ñ	 5ð
 ÒÜ×"Ñ" 8¨TÔ2Ø,3Ô˜=Ñ)Ø-:Ô*Ø÷/ 
��‹ús&   ¡+E%ÁE%ÁA E%Â#A$E%ÄAE%Å%E6	c               ó    € V ^8„  d   QhRRRR/# rb   r!   )r"   s   "r#   r$   r$   ™  s   € ÷ 0ñ 0Ð(<ð 0Àñ 0r%   c                ó:   € \        4        V P                  4        R# )z�Resolve pending scopes and apply the graph's selected annotation keying.

Source keying retains capture IDs and adds exec aliases where needed.
N)r‡  Ú_maybe_remap_annotationsrŒ  s   &r#   Úresolve_and_remaprŸ  ™  s   € ô
  Ô!Ø×-Ñ-Ö/r%   c                  óV   € ] tR tRtRtR R ltR R ltR R ltR	 R
 ltR R lt	Rt
R# )Ú_AnnotationsViewi¢  a{  Read-only view of the annotation store, presenting each node's annotation as a
one-element list.

The store holds exactly one merged dict per node, but the public mapping has always
had list values and pickles of it are read back by out-of-tree consumers, so the
shape is kept. Wrapping on read rather than storing lists is what makes "at most one
annotation per node" explicit.
c               ó    € V ^8„  d   QhRRRR/# )r   rV   rB   r    r©   r!   )r"   s   "r#   r$   Ú_AnnotationsView.__annotate__¬  s   € ÷ ñ  Cð ¨Iñ r%   c                	ó¶   € \         ;_uu_ 4        \        V^ ,	          ,          P                  VR,          ,          .uuRRR4       #   + '       g   i     R# ; iró   )râ   rÞ   r   ©rÖ   rV   s   &&r#   Ú__getitem__Ú_AnnotationsView.__getitem__¬  s4   € ßŽ^ä" 8¨r¥>Õ2×>Ñ>¸xÈ*Õ?TÕUð÷ �^�^‹^ús   �-AÁA	c               ó   € V ^8„  d   QhRR/# ©r   r    zIterator[int]r!   )r"   s   "r#   r$   r£  ²  ó   € ÷ !ñ !˜-ñ !r%   c                	ó4   € \        V P                  4       4      # rÒ   ©Úiterr˜  rÕ   s   &r#   Ú__iter__Ú_AnnotationsView.__iter__²  ó   € Ü�D—I‘I“KÓ Ð r%   c               ó   € V ^8„  d   QhRR/# ©r   r    rB   r!   )r"   s   "r#   r$   r£  µ  ó   € ÷ ñ ˜ñ r%   c                	óÀ   € \         ;_uu_ 4        \        R  \        P                  4       P	                  4        4       4      uuRRR4       #   + '       g   i     R# ; i)c              3  óL   "  € T F  p\        VP                  4      x € K  	  R # 5irÒ   )rf  r   ©Ú.0r\   s   & r#   Ú	<genexpr>Ú+_AnnotationsView.__len__.<locals>.<genexpr>·  s"   é € ð Ù4V¨5”�E×%Ñ%×&Ð&Ó4Vùó   ‚"$N©râ   ÚsumrÞ   r˜  r:  rÕ   s   &r#   Ú__len__Ú_AnnotationsView.__len__µ  s:   € ßŽ^Üñ Ü4F×4KÑ4KÓ4M×4TÑ4TÔ4Vóó ÷ �^�^‹^úó   �2AÁA	c               ó   € V ^8„  d   QhRR/# )r   r    r   r!   )r"   s   "r#   r$   r£  »  s   € ÷ !ñ !˜#ñ !r%   c                	ó4   € \        V P                  4       4      # rÒ   )Úreprr˜  rÕ   s   &r#   Ú__repr__Ú_AnnotationsView.__repr__»  r°  r%   c               ó   € V ^8„  d   QhRR/# )r   r    zdict[int, list[Any]]r!   )r"   s   "r#   r$   r£  ¾  s   € ÷ ñ Ð*ñ r%   c                	óf  € \         ;_uu_ 4        \        P                  4       pVP                  4        UUUUu/ uFH  w  r#VP                  P                  4       P                  4        F  w  rEV^ ,          V,          V.bK  	  KJ  	  uppppuuRRR4       # u uppppi   + '       g   i     R# ; i©rô   N)râ   rÞ   r˜  rë   r   )rÖ   ÚgraphsrA   r\   rí   rR   s   &     r#   r˜  Ú_AnnotationsView.copy¾  sˆ   € ßŽ^ä'×,Ñ,Ó.ˆFð (.§|¡|¤~öá'5‘O�HØ+0×+<Ñ+<×+AÑ+AÓ+C×+IÑ+IÖ+KÑ'�Gð ˜R• 7Õ*¨Z¨LÒ8á+Kñ +Ù'5ô÷ Š^ùõ÷ �^ˆ^ús   �*B¹ABÂBÂBÂB0	r!   N)rÙ   rÚ   rÛ   rÜ   r.  r¦  r®  r½  rÃ  r˜  rÝ   r!   r%   r#   r¡  r¡  ¢  s%   † ñõõ!õõ!÷ñ r%   r¡  r©   c               ó   € V ^8„  d   QhRR/# )r   r    zMapping[int, list[Any]]r!   )r"   s   "r#   r$   r$   Ì  s   € ÷ %ñ %Ð 7ñ %r%   c                 ó   € \         # )ai  get_kernel_annotations() -> Mapping[int, list]

Return the live registry of recorded kernel annotations.

Keys are opaque integers matching the ``graph node id`` field that
CUPTI-based profilers attach to kernel events; values are one-element
lists holding the annotation dict recorded for that node -- annotations
from overlapping scopes are merged into that single dict. The registry
accumulates across captures and is global to the process.

The returned mapping is a **live view**: it is updated in place when a
graph is instantiated (annotation keys are rekeyed to the executable
graph's ids), so a reference obtained early stays current. Keys are
valid for joining against a profiler trace once the corresponding
graphs have been instantiated. The mapping is read-only; snapshot it
with ``dict(...)`` if isolation is needed.

.. note::
    Keep graphs alive until all profiles using their annotations have been
    exported. For asynchronous Cuspy exports, also call
    :meth:`~torch.profiler.profile.wait_for_exports` before resetting or
    destroying the graphs. Stopping the profiler or synchronizing CUDA alone
    does not guarantee that buffered profiling records have been processed.
    Graph cleanup removes entries from this live view; it can leave pending
    profiles without annotations.

.. warning::
    This API is in prototype and may change in future releases.

Example::

    >>> # xdoctest: +SKIP("requires cuda-bindings and driver >= 13.1")
    >>> annotations = torch.cuda.graph_annotations.get_kernel_annotations()
    >>> with open("annotations.pkl", "wb") as f:
    ...     pickle.dump(dict(annotations), f)
)Ú_annotations_viewr!   r%   r#   Úget_kernel_annotationsrÍ  Ì  s   € ôJ Ðr%   c                  óF   € ] tR tRtR R ltR R ltR R ltR R	 ltR
tR# )Ú_PyStacksViewiô  c               ó    € V ^8„  d   QhRRRR/# )r   rV   rB   r    r   r!   )r"   s   "r#   r$   Ú_PyStacksView.__annotate__õ  s   € ÷ Wñ W Cð W¨Cñ Wr%   c                	ó´   € \         ;_uu_ 4        \        V^ ,	          ,          P                  VR,          ,          uuRRR4       #   + '       g   i     R# ; iró   )râ   rÞ   rÔ   r¥  s   &&r#   r¦  Ú_PyStacksView.__getitem__õ  s/   € ßŽ^Ü% h°"¥nÕ5×?Ñ?ÀÈ:Õ@UÕV÷ �^�^‹^ús   �,AÁA	c               ó   € V ^8„  d   QhRR/# r©  r!   )r"   s   "r#   r$   rÑ  ù  rª  r%   c                	ó4   € \        V P                  4       4      # rÒ   r¬  rÕ   s   &r#   r®  Ú_PyStacksView.__iter__ù  r°  r%   c               ó   € V ^8„  d   QhRR/# r²  r!   )r"   s   "r#   r$   rÑ  ü  r³  r%   c                	óÀ   € \         ;_uu_ 4        \        R  \        P                  4       P	                  4        4       4      uuRRR4       #   + '       g   i     R# ; i)c              3  óL   "  € T F  p\        VP                  4      x € K  	  R # 5irÒ   )rf  rÔ   r¶  s   & r#   r¸  Ú(_PyStacksView.__len__.<locals>.<genexpr>þ  s    é € ð Ù2T¨”�E—O‘O×$Ð$Ó2Tùrº  Nr»  rÕ   s   &r#   r½  Ú_PyStacksView.__len__ü  s:   € ßŽ^Üñ Ü2D×2IÑ2IÓ2K×2RÑ2RÔ2Tóó ÷ �^�^‹^úr¿  c               ó   € V ^8„  d   QhRR/# )r   r    zdict[int, str]r!   )r"   s   "r#   r$   rÑ    s   € ÷ ñ �nñ r%   c                	ód  € \         ;_uu_ 4        \        P                  4       pVP                  4        UUUUu/ uFG  w  r#VP                  P                  4       P                  4        F  w  rEV^ ,          V,          VbK  	  KI  	  uppppuuRRR4       # u uppppi   + '       g   i     R# ; irÇ  )râ   rÞ   r˜  rë   rÔ   )rÖ   rÈ  rA   r\   rí   rÇ   s   &     r#   r˜  Ú_PyStacksView.copy  sƒ   € ßŽ^Ü'×,Ñ,Ó.ˆFð (.§|¡|¤~öá'5‘O�HØ&+§o¡o×&:Ñ&:Ó&<×&BÑ&BÖ&D‘N�Gð ˜R• 7Õ*¨EÒ1á&Dñ +Ù'5ô÷ Š^ùõ÷ �^ˆ^ús   �*B¹ABÂBÂBÂB/	r!   N)	rÙ   rÚ   rÛ   rÜ   r¦  r®  r½  r˜  rÝ   r!   r%   r#   rÏ  rÏ  ô  s   † õWõ!õ÷ñ r%   rÏ  c               ó   € V ^8„  d   QhRR/# )r   r    zMapping[int, str]r!   )r"   s   "r#   r$   r$     s   € ÷ ñ Ð/ñ r%   c                 ó   € \         # )a`  Return Python launch stacks recorded during CUDA graph capture.

Enable recording with ``torch.cuda.graph(..., enable_annotations=True,
annotation_config={"record_py_stacks": True})``. Values contain user launch
frames as newline-separated ``filename:line:function`` strings, innermost
first. Framework and generated Inductor frames are omitted by default;
``annotation_config["py_stack_filter_paths"]`` can replace the excluded
directories, or disable filtering with an empty list.

With the default ``key_by="exec"``, match a Cuspy Chrome trace event using
``(args["graph id"] << 32) | args["graph node id"]``. For consumers reading
CUPTI's ``sourceGraphNodeId``, set ``annotation_config["key_by"]="source"``
and use that ID directly (requires CUPTI and driver >= 13.4).
Conditional/child-body stacks require ``"source"`` keying; they are omitted
with ``"exec"``.

The mapping is a live, read-only view. Save it with
:func:`dump_kernel_py_stacks` after instantiation and before resetting or
destroying the graph, which removes its entries.

.. warning::
    This API is in prototype and may change in future releases.
)Ú_PY_STACKS_VIEWr!   r%   r#   Úget_kernel_py_stacksrâ    s
   € ô0 Ðr%   c               ó    € V ^8„  d   QhRRRR/# )r   Úpathr   r    r*   r!   )r"   s   "r#   r$   r$   *  s   € ÷ ñ  ð ¨ñ r%   c                óÖ   € \         P                  4       p\        P                  ! V RRR7      ;_uu_ 4       p\        P
                  ! W4       RRR4       R#   + '       g   i     R# ; i)af  Save recorded CUDA graph launch stacks as gzip-compressed JSON.

The file maps decimal node-id strings to the stack strings described by
:func:`get_kernel_py_stacks`.
Call after instantiation and before resetting or destroying the graphs.

Args:
    path (str): Output file path.

.. warning::
    This API is in prototype and may change in future releases.
Úwtzutf-8)ÚencodingN)rá  r˜  ÚgzipÚopenÚjsonÚdump)rä  ÚsnapshotÚfs   &  r#   Údump_kernel_py_stacksrî  *  sB   € ô ×#Ñ#Ó%€HÜ	�Š�4˜¨×	0Õ	0°AÜ�	Š	�(Ô÷ 
1×	0×	0Ò	0ús   ¶AÁA(	c               ó   € V ^8„  d   QhRR/# rÏ   r!   )r"   s   "r#   r$   r$   =  s   € ÷ ñ  4ñ r%   c                 óÒ   € \         ;_uu_ 4        \        P                  4        RRR4       \        P                  4        \        P                  4        R#   + '       g   i     L:; i)ai  Empty the annotation registry.

Backward brackets already attached to live autograd nodes are NOT revoked -- they
cannot be detached, and they go on recording into the emptied registry. The
implementation behind the deprecated :func:`clear_kernel_annotations`, and what tests
use to isolate themselves without tripping its deprecation warning. Not a public
API.N)râ   rÞ   r,   r-   r  r!   r%   r#   Ú_reset_kernel_annotationsrñ  =  s:   € ÷ 
ŽÜ× Ñ Ô"÷ 
ä×ÑÔÜ×ÑÖ÷ 
�ús   �AÁA&	a  `torch.cuda.graph_annotations.clear_kernel_annotations` is deprecated. The registry bounds itself: annotations are rekeyed to the exec graph on instantiate and dropped when that graph is destroyed, so a global wipe is not needed and discards annotations for graphs that are still live.)Úcategoryc               ó   € V ^8„  d   QhRR/# rÏ   r!   )r"   s   "r#   r$   r$   R  s   € ÷  ñ   $ñ  r%   c                 ó   € \        4        R# )a  clear_kernel_annotations() -> None

Clear all recorded kernel annotations.

.. deprecated:: 2.15
    The registry is self-bounding, so nothing needs to call this. Annotations are
    rekeyed to the exec graph id on instantiation and dropped when that graph is
    destroyed; in a long-running workload -- where graphs are captured once and
    replayed for the whole run -- a global wipe instead discards annotations for
    graphs that are still live and still being joined against.

Forgets everything recorded so far. It does not stop anything from recording:
the backward hooks :func:`mark_kernels` attached to live autograd nodes cannot be
detached and go on writing into the emptied registry, and a forward scope open
across the clear registers on exit as usual. In particular this breaks backward
projection across a forward/backward capture pair -- the forward graph's entries
are gone and its scope no longer names the backward graph's kernels.

.. warning::
    This API is in prototype and may change in future releases.
N)rñ  r!   r%   r#   Úclear_kernel_annotationsrõ  K  s
   € ô: Ör%   c               ó    € V ^8„  d   QhRRRR/# )r   Ú	graph_idszIterable[int]r    r*   r!   )r"   s   "r#   r$   r$   k  s   € ÷ 3ñ 3¨ð 3¸4ñ 3r%   c                óš   € \         ;_uu_ 4        V  F  p\        P                  VR4       K  	  RRR4       R#   + '       g   i     R# ; i)zGDrop the destroyed graph's capture, body, and executable graph entries.N)râ   rÞ   rQ   )r÷  rA   s   & r#   r‹  r‹  k  s-   € ç	ŽÛ!ˆHÜ×"Ñ" 8¨TÖ2ñ "÷ 
��Šús	   � 9¹A
	rB   Ú_stream_id_counterzdict[int, int]Ú_stream_id_mapc               ó    € V ^8„  d   QhRRRR/# )r   r�   útorch.cuda.Streamr    rB   r!   )r"   s   "r#   r$   r$   y  s   € ÷ ñ Ð,ð °ñ r%   c                ó~   € V P                   pV\        9  d   \        \        V&   \        ^,          s\        V,          # )z;Return a small, stable stream ID for the given CUDA stream.)rj   rú  rù  )r�   rï   s   & r#   Ú_get_stream_idrþ  y  s7   € ð ×
Ñ
€CØ
”.Ô Ü0Œ�sÑÜ˜aÕÐÜ˜#ÕÐr%   c               ó    € V ^8„  d   QhRRRR/# )r   Úpg_keyr   r    rB   r!   )r"   s   "r#   r$   r$   ƒ  s   € ÷ "ñ "˜cð " cñ "r%   c                óf   € V \         9  d   \        \         V &   \        ^,          s\         V ,          # )z:Return a unique stream ID for the given process group key.)rú  rù  )r   s   &r#   Úget_stream_for_pgr  ƒ  s,   € ð ”^Ô#Ü!3Œ�vÑÜ˜aÕÐÜ˜&Õ!Ð!r%   c               ó    € V ^8„  d   QhRRRR/# )r   r�   rü  rR   r  r!   )r"   s   "r#   r$   r$   �  s   € ÷ ñ Ð)ð Ð7Kñ r%   c           	   #  ó  "  € \         '       g7   \        P                  P                  V 4      ;_uu_ 4        Rx € RRR4       R# V P                  \        P                  P                  4       P                  8X  d#   \        V4      ;_uu_ 4        Rx € RRR4       R# \        V\        4      '       d   RV/p\        V\        4      '       d   / VCR\        V 4      /Cp\        V4      ;_uu_ 4        \        P                  P                  V 4      ;_uu_ 4        Rx € RRR4       RRR4       R#   + '       g   i     R# ; i  + '       g   i     R# ; i  + '       g   i     LB; i  + '       g   i     R# ; i5i)uZ  Switch to stream, inject its ID into annotation, and mark kernels.

If *stream* is already the current stream, no stream switch or stream ID
injection happens â€” the kernels stay on whatever stream is active (which
keeps the trace faithful when e.g. FSDP uses the current stream for
copy-in instead of a separate one). When switching to a different stream,
this snapshots the current capturing branch before the target stream
runs marked work. If the target stream is already capturing, the marked
work must still be synchronized with the current stream so it is
reachable from that snapped frontier.
Nrè   r�   )r   rg   rh   r�   rj   ri   r‚  ré   r   rê   rþ  )r�   rR   s   &&r#   Úmark_streamr  Œ  sý   é € ÷  ÓÜ�Z‰Z×Ñ˜v×&Õ&Û÷ 'áØ×ÑœUŸZ™Z×6Ñ6Ó8×DÑDÔDÜ˜*×%Õ%Û÷ &Ñ%ô �j¤#×&Ò&Ø  *Ð-ˆJÜ�j¤$×'Ò'ð
 J˜JÐI¨´.ÀÓ2HÑIˆJÜ˜*×%Õ%Ü—‘×"Ñ" 6×*Õ*Û÷ +÷ &Ñ%÷ 'Ö&áúç%×%Ð%ú÷ +×*ú÷ &×%Ð%üse   ‚4F ¶D1»AF ÂEÂAF Ã2(E,ÄE	ÄE,Ä'
F Ä1E	Ä<	F ÅE	Å	F ÅE)Å$E,Å,E=	Å7	F )‚Ú__conditional_annotations__r.  Ú
__future__r   rè  Úimportlib.metadatarv   rê  Ú	threadingr;  Úcollections.abcr   Ú
contextlibr   Úloggingr   Útypingr   r   r   r	   Útyping_extensionsr
   r   r   rg   Útorch.cuda._utilsr   r   r   Úcuda.bindingsr   rº   r   r^   rˆ   rÙ   ry   Útupler¢   r   r/  rê   rB   rH   r   r   r   r   r   r   r'   r.   r2   r4   r-   r:   r?   rE   rJ   rT   rZ   r`   rq   r‚   r�   r’   r�   r–   rf   r›   rk   r¯   r·   r½   rË   rÍ   rÞ   ÚRLockrâ   rå   rY   rõ   rø   rù   r  r  r	  r  r  r  r  r  rP   r#  r'  Úobjectr(  r*  r4  rD  ÚlocalrF  rL  rT  rR  r}  r‚  r‡  r�  r•  r›  rŸ  r¡  rÌ  rÍ  r   rÏ  rá  râ  rî  rñ  ÚFutureWarningrõ  r‹  rù  rú  rþ  r  r  )r  s   @r#   Ú<module>r     s”  øðô(õT #ã Û Û Û Û Ý #Ý %Ý ß <Ó <Ý (÷ ß2ã ÷ñ ð÷÷ ð €LØ€Mñ 
�8Ó	€ð !  d¨3¥i Õ0€ˆyÓ 0Ø'+¨C°°Sµ¨MÕ':Ð ˜9Ó :ð
 $(Ð �[Ó 'ð #Ð �dÓ "ð 'Ð �SÓ &ð  FH€ÐBÓ Gð &*Ð ˜
Ó )õ"õ
 õ"ð !Ð �CÓ  ñ  ›E€�Ó !õ õ*õ"õ
õõ,õõ.õ4)ð^ "(Ð Ø"'Ð õõ(Dõ$#õHõ *õ#õ"-õ"	ð!ð DHð!ð !&÷	!÷H,ñ ,ð 46Ð Ð0Ó 5Ø—’Ó"€õ:õ)õ*7õWð  '+Ð �OÓ *õð0 (,Ð �_Ó +ð +/Ð ˜Ó .õ"õ9õðH 02€Ð,Ó 1ð 1€õõõ:ñ “8€ˆSÓ ô7�:ô 7õSõ"+ô\#�I—O‘Oô #ñ@ “€ˆ#Ó õ)õ8S!ðl ôó ðð0 ð8Àtö 8ó ð8õD õ"Gõ-;õ`*õZ0ô$�w˜s KÐ/Õ0ô $ñN %Ó&Ð õ%ôP�G˜C ˜HÕ%ô ñ0  “/€õõ6õ&ñ ð&ð ôô óð õ23ð Ð �CÓ Ø!#€�Ó #õõ"ð ôó òøðI* ô Ø€LØƒMðús   Á$L ÌLÌL