+
    Pj                      a  0 t $ ^ RIHt ^ RIt^ RIt^ RIHt ^ RIHtHtH	t	H
t
 ^ RIHtHtHt ^ RIt^ RIHt ^ RIHt  ^ RIHtHt ]'       d   ^ R	IHt ^ R
IHt ^RIHt . R%Ot]! R4      t ]! R4      t!]"! ]PF                  R4      '       g^   ]! R4      ]PF                  PH                  R&   ]! R4      ]PF                  PH                  R&   ]! R4      ]PF                  PH                  R&   ^ RI%H&t&H't'H(t( R R lt)R R lt* ! R R]'4      t+ ! R R4      t,]
R]R]-3,          3,          t.R]/R&   ]R&R R  ll4       t0]R&R! R" ll4       t0R&R# R$ llt0R#   ] d	    RtRt ELi ; i)'    )annotationsN)Callable)overloadTYPE_CHECKING	TypeAliasUnion)	ParamSpecSelfTypeVar)Tensor)_check_cuda_bindings)driverruntime)_POOL_HANDLE_CUDAGraphInputLivenessTracker)_dummy_type	CUDAGraphgraph_R_P_CudaStreamBase
_CUDAGraph_graph_pool_handle_cuda_isCurrentStreamCapturing)r   r   r   c                   V ^8  d   QhRR/# )   returnbool )formats   "j/Users/ahmed/devFolder/Ultron/claude-voice/gateway/.venv/lib/python3.14/site-packages/torch/cuda/graphs.py__annotate__r#   9   s     , ,T ,    c                     \        4       # )zReturn True if CUDA graph capture is underway on the current CUDA stream, False otherwise.

If a CUDA context does not exist on the current device, returns False without initializing the context.
)r   r    r$   r"   is_current_stream_capturingr&   9   s    
 *++r$   c                   V ^8  d   QhRR/# r   r   r   r    )r!   s   "r"   r#   r#   B   s     9 9< 9r$   c                 P    \         P                  P                  \        4       4      # )zReturn an opaque token representing the id of a graph memory pool.

See :ref:`Graph memory management<graph-memory-management>`.

.. warning::
    This API is in beta and may change in future releases.
)torchcudar   r   r    r$   r"   graph_pool_handler,   B   s     ::""#5#788r$   c                    a  ] tR t^Nt$ RtR]R&   RR V 3R llltR R ltR R V 3R	 llltR
 V 3R llt	R V 3R llt
R V 3R lltR V 3R lltR V 3R lltR V 3R lltR V 3R lltR V 3R lltR V 3R lltR R ltRtV ;t# )!r   a  Wrapper around a CUDA graph.

Arguments:
    keep_graph (bool, optional): If ``keep_graph=False``, the
        cudaGraphExec_t will be instantiated on GPU at the end of
        ``capture_end`` and the underlying cudaGraph_t will be
        destroyed. Users who want to query or otherwise modify the
        underlying cudaGraph_t before instantiation can set
        ``keep_graph=True`` and access it via ``raw_cuda_graph`` after
        ``capture_end``. Note that the cudaGraphExec_t will not be
        instantiated at the end of ``capture_end`` in this
        case. Instead, it will be instantiated via an explicit called
        to ``instantiate`` or automatically on the first call to
        ``replay`` if ``instantiate`` was not already called. Calling
        ``instantiate`` manually before ``replay`` is recommended to
        prevent increased latency on the first call to ``replay``. It
        is allowed to modify the raw cudaGraph_t after first calling
        ``instantiate``, but the user must call ``instantiate`` again
        manually to make sure the instantiated graph has these
        changes. Pytorch has no means of tracking these changes.

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

z%_CUDAGraphInputLivenessTracker | None_trackerc                    V ^8  d   QhRRRR/# )r   
keep_graphr   r   r
   r    )r!   s   "r"   r#   CUDAGraph.__annotate__k   s       $ r$   c                	4   < \         SV `  W4      pR Vn        V# N)super__new__r.   )clsr0   instance	__class__s   && r"   r5   CUDAGraph.__new__k   s    7?33 r$   c                   V ^8  d   QhRR/# r   r   Noner    )r!   s   "r"   r#   r1   p   s       r$   c                	~     V P                   R uqn         Ve   VP                  4        R # R #   \         d     R # i ; ir3   )r.   stop	Exception)selftrackers   & r"   __del__CUDAGraph.__del__p   s=    	%)]]D"G]" # 		s   '- <<c               (    V ^8  d   QhRRRRRRRR/# )	r   pool_POOL_HANDLE | Nonecapture_error_modestrcheck_input_livenessr   r   r<   r    )r!   s   "r"   r#   r1   x   s2     &" &"!&"  &" #	&"
 
&"r$   c                   < V P                   e"   V P                   P                  4        RV n         \        SV `  WR7       V'       d/   ^ RIHp V! 4       V n         V P                   P                  4        R# R# )ae  Begin capturing CUDA work on the current stream.

Typically, you shouldn't call ``capture_begin`` yourself.
Use :class:`~torch.cuda.graph` or :func:`~torch.cuda.make_graphed_callables`,
which call ``capture_begin`` internally.

Arguments:
    pool (optional): Token (returned by :func:`~torch.cuda.graph_pool_handle` or
        :meth:`other_Graph_instance.pool()<torch.cuda.CUDAGraph.pool>`) that hints this graph may share memory
        with the indicated pool.  See :ref:`Graph memory management<graph-memory-management>`.
    capture_error_mode (str, optional): specifies the cudaStreamCaptureMode for the graph capture stream.
        Can be "global", "thread_local" or "relaxed". During cuda graph capture, some actions, such as cudaMalloc,
        may be unsafe. "global" will error on actions in other threads, "thread_local" will only error for
        actions in the current thread, and "relaxed" will not error on these actions. Do NOT change this setting
        unless you're familiar with `cudaStreamCaptureMode <https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__STREAM.html#group__CUDART__STREAM_1g9d0535d93a214cbf126835257b16ba85>`_
    check_input_liveness (bool, optional):
        If ``True``, tracks external tensor inputs during graph capture and
        raises an error if any are deallocated before replay. This helps debug "use after free" errors
        where input tensors are garbage collected between capture and replay. Default: ``False``.

        .. note::
            Custom CUDA kernels added outside PyTorch (e.g., via cuLaunchKernel or DLPack) are not
            tracked by this mechanism.
N)rE   rG   r   )r.   r>   r4   capture_begintorch.utils._cuda_debugr   start)r@   rE   rG   rI   r   r8   s   &&&& r"   rK   CUDAGraph.capture_beginx   s[    < ==$MM  DM4ON:<DMMM!	  r$   c                   V ^8  d   QhRR/# r;   r    )r!   s   "r"   r#   r1      s     ! !T !r$   c                x   < \         SV `  4        V P                  e   V P                  P                  4        R# R# )a  End CUDA graph capture on the current stream.

After ``capture_end``, ``replay`` may be called on this instance.

Typically, you shouldn't call ``capture_end`` yourself.
Use :class:`~torch.cuda.graph` or :func:`~torch.cuda.make_graphed_callables`,
which call ``capture_end`` internally.
N)r4   capture_endr.   r>   r@   r8   s   &r"   rQ   CUDAGraph.capture_end   s/     	==$MM  %r$   c                   V ^8  d   QhRR/# r;   r    )r!   s   "r"   r#   r1      s      T r$   c                $   < \         SV `  4        R# )zInstantiate the CUDA graph. Will be called by
``capture_end`` if ``keep_graph=False``, or by ``replay`` if
``keep_graph=True`` and ``instantiate`` has not already been
explicitly called. Does not destroy the cudaGraph_t returned
by ``raw_cuda_graph``.
N)r4   instantiaterR   s   &r"   rV   CUDAGraph.instantiate   s     	r$   c                   V ^8  d   QhRR/# r;   r    )r!   s   "r"   r#   r1      s       r$   c                   < V P                   e*   V P                   P                  V P                  4       4       \        SV `  4        R# )z,Replay the CUDA work captured by this graph.N)r.   check_aliverE   r4   replayrR   s   &r"   r[   CUDAGraph.replay   s/    ==$MM%%diik2r$   c                   V ^8  d   QhRR/# r;   r    )r!   s   "r"   r#   r1      s      t r$   c                   < V P                   e"   V P                   P                  4        RV n         \        SV `  4        R# )z1Delete the graph currently held by this instance.N)r.   r>   r4   resetrR   s   &r"   r_   CUDAGraph.reset   s-    ==$MM  DMr$   c                   V ^8  d   QhRR/# r(   r    )r!   s   "r"   r#   r1      s      l r$   c                    < \         SV `  4       # )zReturn an opaque token representing the id of this graph's memory pool.

This id can optionally be passed to another graph's ``capture_begin``,
which hints the other graph may share the same memory pool.
)r4   rE   rR   s   &r"   rE   CUDAGraph.pool   s     w|~r$   c                   V ^8  d   QhRR/# r;   r    )r!   s   "r"   r#   r1      s     + +4 +r$   c                    < \         SV `  4       # )z/Enable debugging mode for CUDAGraph.debug_dump.)r4   enable_debug_moderR   s   &r"   rf   CUDAGraph.enable_debug_mode   s    w(**r$   c                    V ^8  d   QhRRRR/# )r   
debug_pathrH   r   r<   r    )r!   s   "r"   r#   r1      s     . .S .T .r$   c                "   < \         SV `  V4      # )z
Arguments:
    debug_path (required): Path to dump the graph to.

Calls a debugging function to dump the graph if the debugging is
enabled via CUDAGraph.enable_debug_mode()
)r4   
debug_dump)r@   ri   r8   s   &&r"   rk   CUDAGraph.debug_dump   s     w!*--r$   c                   V ^8  d   QhRR/# r   r   intr    )r!   s   "r"   r#   r1      s     ( ( (r$   c                    < \         SV `  4       # )al  Returns the underlying cudaGraph_t. ``keep_graph`` must be True.

See the following for APIs for how to manipulate this object: `Graph Management <https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__GRAPH.html>`_ and `cuda-python Graph Management bindings <https://nvidia.github.io/cuda-python/cuda-bindings/latest/module/runtime.html#graph-management>`_
)r4   raw_cuda_graphrR   s   &r"   rq   CUDAGraph.raw_cuda_graph   s    
 w%''r$   c                   V ^8  d   QhRR/# rn   r    )r!   s   "r"   r#   r1      s     - -S -r$   c                    < \         SV `  4       # )a  Returns the underlying cudaGraphExec_t. ``instantiate`` must have been called if ``keep_graph`` is True, or ``capture_end`` must have been called if ``keep_graph`` is False. If you call ``instantiate()`` after ``raw_cuda_graph_exec()``, the previously returned cudaGraphExec_t will be destroyed. It is your responsibility not to use this object after destruction.

See the following for APIs for how to manipulate this object: `Graph Execution <https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__GRAPH__EXEC.html>`_ and `cuda-python Graph Execution bindings <https://nvidia.github.io/cuda-python/cuda-bindings/latest/module/runtime.html#graph-execution>`_
)r4   raw_cuda_graph_execrR   s   &r"   ru   CUDAGraph.raw_cuda_graph_exec   s    
 w*,,r$   c                   V ^8  d   QhRR/# )r   r   dictr    )r!   s   "r"   r#   r1      s     C
 C
 C
r$   c                	   ^ RI Hp \        e	   \        f   \	        R4      hV! 4       '       d   \	        R4      h\        P
                  P                  R\        P
                  P                  R\        P
                  P                  R\        P
                  P                  R\        P
                  P                  R	\        P
                  P                  R
\        P
                  P                  R\        P
                  P                  R\        P
                  P                  R\        P
                  P                  R/
pV P!                  4       p\#        \        P$                  ! V^ R7      4      w  rE\#        \        P$                  ! W5R7      4      w  re/ p. p\'        V4       EF  p	Wi,          p
W\)        V
4      &   \#        \        P*                  ! V
4      4      p\#        \        P,                  ! V
4      4      pV^ ,	          pVR,          pRpV\        P
                  P                  8X  d   \        P.                  ! \)        V
4      R7      p\        P0                  ! V4      w  ppV\        P2                  P4                  8X  d   \)        VP6                  4      '       d   \        P8                  ! \)        VP6                  4      R7      p\        P:                  ! V4      w  ppV\        P2                  P4                  8X  d)   \=        V\>        4      '       d   VPA                  4       MTpVPC                  RV	RVPE                  V\G        V4      4      RVRVRVRVR. R. /4       EK  	  \#        \        PH                  ! V^ R7      4      w    ppV^ 8  d   \#        \        PH                  ! VVR7      4      w  pppp\'        V4       F  p	VPE                  \)        VV	,          4      4      pVPE                  \)        VV	,          4      4      pVf   KK  Vf   KQ  VV,          R,          PC                  V4       VV,          R,          PC                  V4       K  	  \        PJ                  ! V PM                  4       R7      p\#        \        PN                  ! V4      4      pV F"  pV^ ,          VR,          ,          VR&   VVR&   K$  	  RVRV/# )a  Return a dictionary describing the graph's topology and node metadata.

``keep_graph`` must be True.  The graph must have been instantiated
(via :meth:`instantiate`) before calling this method.
Requires the ``cuda.bindings`` package.

Returns a dictionary with structure::

    {
        "exec_graph_id": int,
        "nodes": [
            {
                "index": int,
                "node_type": str,
                "tools_id": int,
                "graph_id": int,
                "node_id": int,
                "kernel_name": str or None,
                "dependencies": [int, ...],
                "dependents": [int, ...],
            },
            ...,
        ],
    }

Each node's ``graph_id`` is remapped to the exec graph id so that
``tools_id`` values match those reported by CUPTI-based profilers.
``dependencies`` and ``dependents`` are lists of node indices within
the ``nodes`` list.

This structure is useful for inspecting a profiler trace and
establishing whether a particular dependency observed in the profile
is a true dependency (encoded in the graph) or a fake dependency
caused by mapping of independent streams to the same hardware
channel.
)_is_tools_id_unavailableNz1get_graph_data requires the cuda.bindings packagezget_graph_data requires cudaGraphNodeGetToolsId which needs cuda.bindings >= 13.1 and CUDA driver >= 13.1 (or cuda-compat >= 13.1 in LD_LIBRARY_PATH)kernelmemcpymemsethostchild_graphempty
wait_eventevent_record	mem_allocmem_free)numNodesl    )
init_valueindex	node_typetools_idgraph_idnode_idkernel_namedependencies
dependents)numEdgesexec_graph_idnodes)(torch.cuda._graph_annotationsrz   _cuda_runtime_cuda_driverRuntimeErrorcudaGraphNodeTypecudaGraphNodeTypeKernelcudaGraphNodeTypeMemcpycudaGraphNodeTypeMemsetcudaGraphNodeTypeHostcudaGraphNodeTypeGraphcudaGraphNodeTypeEmptycudaGraphNodeTypeWaitEventcudaGraphNodeTypeEventRecordcudaGraphNodeTypeMemAlloccudaGraphNodeTypeMemFreerq   r   cudaGraphGetNodesrangero   cudaGraphNodeGetTypecudaGraphNodeGetToolsIdCUgraphNodecuGraphKernelNodeGetParamsCUresultCUDA_SUCCESSfunc
CUfunctioncuFuncGetName
isinstancebytesdecodeappendgetrH   cudaGraphGetEdgescudaGraphExec_tru   cudaGraphExecGetId)r@   rz   node_type_namesraw_numr   handle_to_idx
node_infosinodentyper   r   r   r   cu_nodeerrparamscu_funcname	num_edges
from_nodesto_nodes
_edge_datasrcdstexec_handler   infos   &                             r"   get_graph_dataCUDAGraph.get_graph_data   s   J 	K L$8RSS#%%>  ++CCX++CCX++CCX++AA6++BBM++BBG++FF++HH.++EE{++DDj
 !!#%m&E&EcTU&VW)++C>

 )+!#
sA8D'(#d)$()K)KD)QRE+M,Q,QRV,WXH2~H+GK77OOO&22c$iH*EEgNV,//<<<V[[AQAQ*55V[[AQRG , : :7 CICl33@@@7A$7N7NdkkmTXQ!4!4UCJ!Gw!;"B "	' @ 2++C!<
1a q=:N//iH;7J*i 9%#''JqM(:;#''HQK(89?ssOL188=sON3::3? & $33//1
 -,,[9
 D - 3tIFD,D 
 ]Z
 	
r$   )r.   )F)NglobalF)__name__
__module____qualname____firstlineno____doc____annotations__r5   rB   rK   rQ   rV   r[   r_   rE   rf   rk   rq   ru   r   __static_attributes____classcell__)r8   s   @r"   r   r   N   s    4 43 
&" &"P! !    + +. .( (- -C
 C
r$   c                  R    ] tR tRt$ RtRtR]R&   RR R lltR R	 ltR
 R lt	Rt
R# )r   im  a  Context-manager that captures CUDA work into a :class:`torch.cuda.CUDAGraph` object for later replay.

See :ref:`CUDA Graphs <cuda-graph-semantics>` for a general introduction,
detailed use, and constraints.

Arguments:
    cuda_graph (torch.cuda.CUDAGraph): Graph object used for capture.
    pool (optional): Opaque token (returned by a call to :func:`~torch.cuda.graph_pool_handle()` or
        :meth:`other_Graph_instance.pool()<torch.cuda.CUDAGraph.pool>`) hinting this graph's capture
        may share memory from the specified pool. See :ref:`Graph memory management<graph-memory-management>`.
    stream (torch.cuda.Stream, optional): If supplied, will be set as the current stream in the context.
        If not supplied, ``graph`` sets its own internal side stream as the current stream in the context.
    capture_error_mode (str, optional): specifies the cudaStreamCaptureMode for the graph capture stream.
        Can be "global", "thread_local" or "relaxed". During cuda graph capture, some actions, such as cudaMalloc,
        may be unsafe. "global" will error on actions in other threads, "thread_local" will only error for
        actions in the current thread, and "relaxed" will not error on actions. Do NOT change this setting
        unless you're familiar with `cudaStreamCaptureMode <https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__STREAM.html#group__CUDART__STREAM_1g9d0535d93a214cbf126835257b16ba85>`_
    enable_annotations (bool, optional): If ``True``, enables kernel annotation
        recording on entry and automatically calls
        :func:`~torch.cuda._graph_annotations.resolve_pending_annotations` before
        the capture ends.  Annotations are **not** cleared on exit so that multiple
        graphs in the same workload can accumulate annotations.
        Requires ``cuda.bindings`` package and cuda-compat >= 13.1 or CUDA driver >= 13.1.
    check_input_liveness (bool, optional): If ``True``, tracks external tensor inputs during graph capture and
        raises an error if any are deallocated before replay. This helps debug "use after free" errors
        where input tensors are garbage collected between capture and replay. Default: ``False``.

        .. note::
            Custom CUDA kernels added outside PyTorch (e.g., via cuLaunchKernel or DLPack) are not
            tracked by this mechanism.

.. note::
    For effective memory sharing, if you pass a ``pool`` used by a previous capture and the previous capture
    used an explicit ``stream`` argument, you should pass the same ``stream`` argument to this capture.

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

.. _cudaStreamCaptureMode:
    https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__STREAM.html#group__CUDART__STREAM_1g9d0535d93a214cbf126835257b16ba85
Ntorch.cuda.Stream | Nonedefault_capture_streamc               0    V ^8  d   QhRRRRRRRRR	R
RR
/# )r   
cuda_graphr   rE   rF   streamr   rG   rH   enable_annotationsr   rI   r    )r!   s   "r"   r#   graph.__annotate__  sF     9 99 "9 )	9
  9 !9 #9r$   c                	   VfF   V P                   P                  f.   \        P                  P	                  4       V P                   n        Vf   RMV3V n        Ve   TMV P                   P                  V n        V P                  f   \        R4      h\        P                  P                  V P                  4      V n	        Wn
        W@n        WPn        W`n        R # )Nzcapture_stream must not be Noner    )r8   r   r*   r+   StreamrE   capture_streamAssertionErrorr   
stream_ctxr   rG   _enable_annotationsrI   )r@   r   rE   r   rG   r   rI   s   &&&&&&&r"   __init__graph.__init__  s     >dnnCCK49JJ4E4E4GDNN1;?<RdW	(Fdnn.S.S 	 & !BCC**++D,?,?@$"4#5 $8!r$   c                   V ^8  d   QhRR/# r;   r    )r!   s   "r"   r#   r     s      
  
4  
r$   c                	"   \         P                  P                  4        \         P                  P                  P
                  '       d   \        P                  ! 4        \         P                  P                  4        \         P                  P                  4        V P                  '       d   ^ RIHp V! 4        V P                  P                  4        V P                   P"                  ! V P$                  RV P&                  RV P(                  /  R# )r   )r   rG   rI   N)r*   r+   synchronizecompilerconfigforce_cudagraph_gcgccollectempty_cache_C_host_emptyCacher   r   r   r   	__enter__r   rK   rE   rG   rI   )r@   _enable_anns   & r"   r   graph.__enter__  s    

 >>  333 JJL

 !!####WM 	!!#%%YY	
  $66		
 "&!:!:	
r$   c                    V ^8  d   QhRRRR/# )r   argsobjectr   r<   r    )r!   s   "r"   r#   r     s     1 1f 1 1r$   c                	   V P                   '       d   ^ RIHp V! 4        V P                  P	                  4        V P
                  P                  ! V!   V P                   '       d   ^ RIHp V! V P                  4       R# R# )r   )resolve_pending_annotations)remap_to_exec_graphN)r   r   r   r   rQ   r   __exit__r   )r@   r   r   r   s   &*  r"   r   graph.__exit__  sY    ###Q')##%  $'###I0 $r$   )r   rG   r   rI   r   rE   r   )NNr   FF)r   r   r   r   r   r   r   r   r   r   r   r    r$   r"   r   r   m  s+    (T 8<4;96 
D1 1r$   torch.nn.Module.r   _ModuleOrCallablec               0    V ^8  d   QhRRRRRRRRR	R
RR/# )r   	callablesr   sample_argstuple[Tensor, ...]num_warmup_itersro   allow_unused_inputr   rE   rF   r   r    )r!   s   "r"   r#   r#     sD       #  	
  r$   c                    R # r3   r    r   r   r  r  rE   s   &&&&&r"   make_graphed_callablesr    s     r$   c               0    V ^8  d   QhRRRRRRRRR	R
RR/# )r   r   ztuple[_ModuleOrCallable, ...]r   ztuple[tuple[Tensor, ...], ...]r  ro   r  r   rE   rF   r   r    )r!   s   "r"   r#   r#     sD     ( (,(/( ( 	(
 ( #(r$   c                    R # r3   r    r  s   &&&&&r"   r  r    s     %(r$   c               0    V ^8  d   QhRRRRRRRRR	R
RR/# )r   r   z1_ModuleOrCallable | tuple[_ModuleOrCallable, ...]r   z3tuple[Tensor, ...] | tuple[tuple[Tensor, ...], ...]r  ro   r  r   rE   rF   r   r    )r!   s   "r"   r#   r#     sL     J J@JDJ J 	J
 J 7Jr$   c                   \         P                  ! 4       '       d'   \         P                  ! 4       '       d   \        R4      hRp\	        V \
        4      '       g0   RpV 3p \        P                  ! \
        \        R3,          V4      3pM5\        P                  ! \
        \
        \        R3,          R3,          V4      p. p\        W4       EFq  w  r\	        V\         P                  P                  4      '       d   \        VP                  4      ^ 8X  d5   \        VP                  4      ^ 8X  d   \        VP                  4      ^ 8X  g   \!        R4      h\"        ;QJ d*    R VP%                  4        4       F  '       d   K   RM	  RM! R VP%                  4        4       4      '       g   \!        R4      h\         P&                  P(                  P*                  ! V	!  p
VP-                  \        V
4      4       \"        ;QJ d    R V
 4       F  '       d   K   RM	  RM! R V
 4       4      '       d   EKi  \!        R4      h	  V U	u. uF  p	\        V	4      NK  	  pp	V  Uu. uFH  p\	        V\         P                  P                  4      '       d   \        VP/                  4       4      MRNKJ  	  pp\1        \        V 4      4       Uu. uF  pW},          W,          ,           NK  	  pp\1        \        V 4      4       Uu. uF!  p\         P2                  P5                  4       NK#  	  pp\1        \        V 4      4       Uu. uF!  p\         P2                  P5                  4       NK#  	  ppVf   \7        4       MTp\         P2                  P9                  4        \         P2                  P;                  \         P2                  P=                  4       4      ;_uu_ 4        \        WV4       EF  w  pp	pRRRppp\1        V4       F  p\         P&                  P(                  P?                  V! V	!  4      p\
        ;QJ d    . R V 4       F  NK  	  5M! R V 4       4      p\        V4      ^ 8  g   Kn  \         P@                  PC                  T\
        ;QJ d    . R	 V 4       F  NK  	  5M! R	 V 4       4      \
        ;QJ d    . R
 V 4       F  NK  	  5M! R
 V 4       4      RVR7      pK  	  VVV3 F  p?K  	  EK  	  RRR4       \         P2                  P9                  4        . p. p\        WV4       F  w  pp	p\         P2                  PE                  VVR7      ;_uu_ 4        V! V	!  pRRR4       \         P&                  P(                  PG                  X4      w  ppVP-                  \        V4      4       VP-                  V4       K  	  . p. p \        \I        V4      \I        V4      \I        V4      4       EF  w  pp!p"\
        ;QJ d    . R V! 4       F  NK  	  5M! R V! 4       4      p#\
        ;QJ d    . R V! 4       F  NK  	  5M! R V! 4       4      pRp\        V4      ^ 8  d   \         P2                  PE                  V"VR7      ;_uu_ 4        \         P@                  PC                  T\
        ;QJ d    . R V 4       F  NK  	  5M! R V 4       4      \
        ;QJ d    . R V# 4       F  NK  	  5M! R V# 4       4      RVR7      pRRR4       . p$^ p%V FM  p&V&PJ                  '       d(   Ve$   V$P-                  VV%,          4       V%^,          p%K<  V$P-                  R4       KO  	  \        V$4      p$VP-                  V#4       V P-                  V$4       EK  	  VPM                  4        V PM                  4        R R lp'. p(\O        V 4       F  w  ppV'! VV,          VV,          W,          W,          VV,          W,          VV,          VV,          V V,          4	      p)\	        V\         P                  P                  4      '       d>   R R lp*V*! VVPP                  V)VPR                  4      Vn)        V(P-                  V4       K  V(P-                  V)4       K  	  V'       d
   V(^ ,          # \        V(4      # u up	i u upi u upi u upi u upi   + '       g   i     EL; i  + '       g   i     EL; i  + '       g   i     EL; i)a  Accept callables (functions or :class:`nn.Module<torch.nn.Module>`\ s) and returns graphed versions.

Each graphed callable's forward pass runs its source callable's
forward CUDA work as a CUDA graph inside a single autograd node.

The graphed callable's forward pass also appends
a backward node to the autograd graph. During backward, this node runs the
callable's backward work as a CUDA graph.

Therefore, each graphed callable should be a drop-in replacement for its source callable
in an autograd-enabled training loop.

See :ref:`Partial-network capture<partial-network-capture>` for detailed use and constraints.

If you pass a tuple of several callables, their captures will use the same memory pool.
See :ref:`Graph memory management<graph-memory-management>` for when this is appropriate.

Arguments:
    callables (torch.nn.Module or Python function, or tuple of these): Callable or callables to graph.
        See :ref:`Graph memory management<graph-memory-management>` for when passing a tuple of callables
        is appropriate.  If you pass a tuple of callables, their order in the tuple must be the same order
        they'll run in the live workload.
    sample_args (tuple of Tensors, or tuple of tuples of Tensors): Samples args for each callable.
        If a single callable was passed, ``sample_args`` must be a single tuple of argument Tensors.
        If a tuple of callables was passed, ``sample_args`` must be tuple of tuples of argument Tensors.
    num_warmup_iters (int): The number of warmup iterations. Currently, ``DataDistributedParallel`` needs
        11 iterations for warm up. Default: ``3``.
    allow_unused_input (bool): If False, specifying inputs that were not used when computing outputs
        (and therefore their grad is always zero) is an error. Defaults to False.
    pool (optional): Token (returned by :func:`~torch.cuda.graph_pool_handle` or
        :meth:`other_Graph_instance.pool()<torch.cuda.CUDAGraph.pool>`) that hints this graph may share memory
        with the indicated pool.  See :ref:`Graph memory management<graph-memory-management>`.

.. note::
    The ``requires_grad`` state of each Tensor in ``sample_args`` must match the state
    that's expected for the corresponding real input in the training loop.

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

.. warning::
    ``sample_args`` for each callable must contain only Tensors. Other types are not allowed.

.. warning::
    Returned callables do not support higher order differentiation (e.g., double backward).

.. warning::
    In any :class:`~torch.nn.Module` passed to :func:`~make_graphed_callables`, only parameters
    may be trainable. Buffers must have ``requires_grad=False``.

.. warning::
    After you pass a :class:`torch.nn.Module` through :func:`~make_graphed_callables`,
    you may not add or remove any of that Module's parameters or buffers.

.. warning::
    :class:`torch.nn.Module`\s passed to :func:`~torch.cuda.make_graphed_callables` must not have module hooks
    registered on them at the time they are passed. However, registering hooks on modules *after* passing them
    through :func:`~torch.cuda.make_graphed_callables` is allowed.

.. warning::
    When running a graphed callable, you must pass its arguments in the same order and format
    they appeared in that callable's ``sample_args``.

.. warning::
    The automatic mixed precision is supported in :func:`~torch.cuda.make_graphed_callables` only with disabled
    caching. The context manager `torch.cuda.amp.autocast()` must have `cache_enabled=False`.
z_make_graphed_callables does not support the autocast caching. Please set `cache_enabled=False`.FT.c              3  <   "   T F  qP                   R J x  K  	  R# 5i)FNrequires_grad.0bs   & r"   	<genexpr>)make_graphed_callables.<locals>.<genexpr>d  s     EA%/s   c              3  V   "   T F  p\        V\        P                  4      x  K!  	  R # 5ir3   )r   r*   r   )r  args   & r"   r  r  l  s     HKS:c5<<00Ks   ')Nc              3  L   "   T F  qP                   '       g   K  Vx  K  	  R # 5ir3   r  r  os   & r"   r  r    s     $K1??QQ   $
$c              3  L   "   T F  qP                   '       g   K  Vx  K  	  R # 5ir3   r  r  r   s   & r"   r  r    s      %';!AA';r  c              3  t   "   T F.  qP                   '       g   K  \        P                  ! V4      x  K0  	  R # 5ir3   r  r*   
empty_liker  s   & r"   r  r    s(      +9@AOO/E,,Q//s   88)outputsinputsgrad_outputsonly_inputsallow_unused)rE   c              3  t   "   T F.  qP                   '       d   \        P                  ! V4      MR x  K0  	  R # 5ir3   r  r  s   & r"   r  r    s)      $
FT???EQ<ns   68c              3  L   "   T F  qP                   '       g   K  Vx  K  	  R # 5ir3   r  r  s   & r"   r  r    s     J1//QQr  c              3  L   "   T F  qP                   '       g   K  Vx  K  	  R # 5ir3   r  r  s   & r"   r  r    s      T,@qOO,@r  c              3  0   "   T F  qf   K  Vx  K  	  R # 5ir3   r    r  s   & r"   r  r    s     &W2EQqq2Es   
c               @    V ^8  d   QhRRRRRRRRRR	R
RRRRRRRRR/
# )r   	fwd_graphr   	bwd_graphmodule_paramsztuple[torch.nn.Parameter, ...]len_user_argsro   output_unflatten_specztorch.utils._pytree.TreeSpecstatic_input_surfacer   static_outputsstatic_grad_outputsztuple[Tensor | None, ...]static_grad_inputsr   zCallable[..., object]r    )r!   s   "r"   r#   ,make_graphed_callables.<locals>.__annotate__  sl     = === 6= 	=
  <= 1= += 7= /= 
=r$   c	           	        a aaaaaaaaa
  ! VV VVVVV3R  lR\         P                  P                  4      o
R V
VV3R llp	V	# )c                     < ] tR tRt]R VVVV3R ll4       t]]P                  P                  P                  R V VV3R ll4       4       t
RtR# )Omake_graphed_callables.<locals>.make_graphed_autograd_function.<locals>.Graphedi  c               $    V ^8  d   QhRRRRRR/# )r   ctxr   r  r   r   r   r    )r!   s   "r"   r#   \make_graphed_callables.<locals>.make_graphed_autograd_function.<locals>.Graphed.__annotate__  s'     
A 
AV 
Af 
A9K 
Ar$   c                	  < \        S4       FS  pSV,          P                  4       W,          P                  4       8w  g   K5  SV,          P                  W,          4       KU  	  SP                  4        \	        S\
        4      '       g   \        R \        S4       24      h\
        ;QJ d    . R S 4       F  NK  	  5# ! R S 4       4      # )z"static_outputs must be tuple, got c              3  @   "   T F  qP                  4       x  K  	  R # 5ir3   detachr  s   & r"   r  jmake_graphed_callables.<locals>.make_graphed_autograd_function.<locals>.Graphed.forward.<locals>.<genexpr>  s     @AXXZZs   )r   data_ptrcopy_r[   r   tupler   type)r5  r  r   r'  r*  r,  r-  s   &* r"   forwardWmake_graphed_callables.<locals>.make_graphed_autograd_function.<locals>.Graphed.forward  s     }-A+A.779VY=O=O=QQ,Q/55fi@ .   "!.%88(<T.=Q<RS  u@@u@u@@@@r$   c               $    V ^8  d   QhRRRRRR/# )r   r5  r   gradsr   r   r   r    )r!   s   "r"   r#   r6    s"      f f 9K r$   c                	  < \        V4      \        S4      8w  d$   \        R \        V4       R\        S4       24      h\        SV4       FA  w  r#Vf   K  VP                  4       VP                  4       8w  g   K0  VP	                  V4       KC  	  SP                  4        \        S\        4      '       g   \        R\        S4       24      h\        ;QJ d    . R S 4       F  NK  	  5# ! R S 4       4      # )zlen(grads)=z != len(static_grad_outputs)=z&static_grad_inputs must be tuple, got c              3  N   "   T F  pVe   VP                  4       MTx  K  	  R # 5ir3   r9  r  s   & r"   r  kmake_graphed_callables.<locals>.make_graphed_autograd_function.<locals>.Graphed.backward.<locals>.<genexpr>  s(       0 #$-AHHJQ6/s   #%)	lenr   zipr<  r=  r[   r   r>  r?  )r5  rC  ggradr(  r/  r.  s   &*  r"   backwardXmake_graphed_callables.<locals>.make_graphed_autograd_function.<locals>.Graphed.backward  s     u:%8!99(%c%j\1NsSfOgNhi   ##6>GA} ::<4==?:GGDM  ?   " ""4e<<(@FXAY@Z[  u  0u u  0  r$   r    N)r   r   r   r   staticmethodr@  r*   autogradfunctiononce_differentiablerK  r   )r(  r'  r*  r/  r.  r,  r-  s   r"   Graphedr3    sN    
A 
A 
A ^^$$88  9 r$   rQ  c                    V ^8  d   QhRRRR/# )r   	user_argsr   r   r    )r!   s   "r"   r#   Tmake_graphed_callables.<locals>.make_graphed_autograd_function.<locals>.__annotate__  s     	R 	Rv 	R& 	Rr$   c                    < \         P                  P                  P                  ! V !  pSP                  ! \        V4      S,           !  p\         P                  P                  P                  VS4      # r3   )r*   utils_pytreearg_tree_leavesapplyr>  tree_unflatten)rS  flatten_user_argsoutrQ  r)  r+  s   *  r"   functionalizedVmake_graphed_callables.<locals>.make_graphed_autograd_function.<locals>.functionalized  sW     !& 3 3 C CY O--%(9":]"JLC;;&&55c;PQQr$   )r*   rN  Function)r'  r(  r)  r*  r+  r,  r-  r.  r/  r]  rQ  s   fffffffff @r"   make_graphed_autograd_function>make_graphed_callables.<locals>.make_graphed_autograd_function  s4    (	 (	enn-- (	T	R 	R r$   c          
     ,    V ^8  d   QhRRRRRRRRRR/# )	r   r   r   graph_training_stater   graphedzCallable[_P, _R]orig_fwdr   r    )r!   s   "r"   r#   r0  .  s:      %&* * +	
 "r$   c                &   a aaa R  V VVV3R llpV# )c               $    V ^8  d   QhRRRRRR/# )r   rS  z_P.argsuser_kwargsz	_P.kwargsr   r   r    )r!   s   "r"   r#   Jmake_graphed_callables.<locals>.make_graphed_forward.<locals>.__annotate__4  s&     C C C	 Cb Cr$   c                 F   < SP                   S8X  d	   S! V / VB # S! V / VB # r3   )training)rS  rh  r   rc  rd  re  s   *,r"   new_fwdEmake_graphed_callables.<locals>.make_graphed_forward.<locals>.new_fwd4  s2     }}(<<&	A[AA'BkBBr$   r    )r   rc  rd  re  rl  s   ffff r"   make_graphed_forward4make_graphed_callables.<locals>.make_graphed_forward.  s    C C r$   zModules must not have hooks registered at the time they are passed. However, registering hooks on modules after passing them through make_graphed_callables is allowed.zIn any :class:`~torch.nn.Module` passed to :func:`~make_graphed_callables`, only parameters may be trainable. All buffers must have ``requires_grad=False``.zfIn the beta API, sample_args for each callable must contain only Tensors. Other types are not allowed.r    )*r*   is_autocast_enabledis_autocast_cache_enabledr   r   r>  typingcastr   rH  nnModulerG  _backward_hooks_forward_hooks_forward_pre_hooksr   allbuffersrV  rW  rX  r   
parametersr   r+   r   r,   r   r   r   tree_leavesrN  rJ  r   tree_flattenreversedr  reverse	enumeraterk  r@  )+r   r   r  r  rE   just_one_callable_sample_argsflatten_sample_argscr   flatten_argper_callable_len_user_argsper_callable_module_paramsr   "per_callable_static_input_surfacesr   
fwd_graphs
bwd_graphsmempoolr   r,  grad_inputsr  outputs_gradvper_callable_static_outputs"per_callable_output_unflatten_specr'  func_outputsflatten_outputsspec per_callable_static_grad_outputsper_callable_static_grad_inputsr-  r(  r.  r/  grad_idxr  r`  retrd  rn  s+   &&&&&                                      r"   r  r    sr   T   ""u'F'F'H'Hm
 	
  i'' L	E&#+$6DF{{5vs{);S)@#A;Oy/a))A%%&!+(()Q.,,-2$a  3EE333EEEE$1 
 kk))994@""5#56sHKHsssHKHHH ^ ) 06 9L!L8K#d)8K!L "A ",Auxx!?!?allnRG  " s9~&*&A 	!;!>>>& ' *
 38I2GH2GQ%**&&(2GJH27I2GH2GQ%**&&(2GJH%)\!tG
 
JJ			5::,,.	/	/03%G1
,D$, 26tT,K+,++--99$+F$u$K$Kuu$K$KK|$q("'.."5"5 ,$u %';%uu %';%   &+U +9@+UU +9@+ & %)%7 #6 
#K	 - |[9 :'1
 
0. 
JJ #%)+&!$Yj!IdIZZig66;L 7 !& 3 3 @ @ N#**5+AB*11$7 "J (*$&(#;>34,-<7ni $e $
FT$
ee $
FT$
 
 uJJuuJJJ|q !!)'!::#nn11( 5 T,@ T55 T,@ TT!&&W2E&W&W2E&W!W $!3 2  ;  'C   [%<"))+h*?@A"))$/ ( ##56(//0CD'../ABK<P %,,.#++-=@ $&CY'40qMqM&)&).q1.1'*,Q/+A.

 dEHHOO,,  0dmmWdllDL JJtJJwI (L 1v:i "M"*
 IH 
0	/	/B 7660 ;::s]   b4Ab9b>;'c:'cBc($c*c8>cc!Ac5(c5c	!c25d)r&   r,   r   r   r  )   FN)1__conditional_annotations__
__future__r   r   rr  collections.abcr   r   r   r   r   typing_extensionsr	   r
   r   r*   r   torch.cuda._utilsr   cuda.bindingsr   r   r   r   ImportError
torch.cudar   rL   r   _utilsr   __all__r   r   hasattrr   __dict__torch._Cr   r   r   r&   r,   r   r   r   r   r   r  )r  s   @r"   <module>r     sb   " " 	  $ < < 6 6   2 'F   T]t_ uxx*++&1,&?EHHl#.9:N.OEHH*+:E(;EHH67 T S,9\

 \
~v1 v1t  %%6f8M%MN 9 N 
 
 
( 
(J JU  LMs    E E#"E#