§
    ŠŠtj�  ã                   ó\   — d dl Z d dlmZ d dlmZ d dlZd dlmZ  G d„ de¦  «        ZdgZ	dS )é    N)ÚLiteral)ÚSelf)Ú_acceleratorGraphc            
       ó@  ‡ — e Zd ZdZ	 ddddœdedeeef         dz  ded	         d
efˆ fd„Z		 ddddœdedeeef         dz  ded	         d
dfˆ fd„Z
dˆ fd„Zdˆ fd„Zdˆ fd„Zdˆ fd„Zdˆ fd„Zd
eeef         fˆ fd„Zdˆ fd„Zded
dfˆ fd„Zdd„Zded
dfd„Zˆ xZS )ÚGrapha\  
    Wrapper around an :ref:`accelerator<accelerators>` graph that supports capture and replay.

    A graph captures a sequence of operations and their dependencies, allowing them to be
    replayed efficiently with reduced overhead. This class can be used as a context manager
    to automatically capture operations on the current stream.

    Arguments:
        keep_graph (bool, optional): If ``False``, the underlying graph is destroyed and the
            executable graph is instantiated on the GPU at the end of ``capture_end``.
            If ``True``, the underlying graph is preserved after ``capture_end``. In this case,
            the executable graph is not instantiated automatically; it must be explicitly created
            by calling ``instantiate``, or it will be instantiated on the first call to ``replay``.
            Defaults to ``False``.
        pool (tuple[int, int], optional): Memory pool identifier for this graph. Multiple graphs
            can share the same pool by passing the same identifier, which can reduce memory overhead.
            Defaults to ``None``.
        capture_error_mode (Literal["default", "global", "thread_local", "relaxed"], optional):
            Specifies the behavior of graph capture. The exact semantics are backend-specific.
            ``"default"``: backend-defined default capture behavior.
            ``"global"``: potentially unsafe API calls are prohibited. Errors may occur if capture
            in the current thread affects other threads.
            ``"thread_local"``: potentially unsafe API calls are prohibited. Errors occur only if
            capture in the current thread affects itself.
            ``"relaxed"``: the current thread is allowed to make potentially unsafe API calls, except
            for calls that inherently conflict with stream capture.
            Default: ``"default"``.

    Example::

        >>> # xdoctest: +SKIP
        >>> x = torch.zeros([2000], device=0)

        >>> stream = torch.Stream()
        >>> graph = torch.accelerator.Graph()
        >>> with stream, graph:
        ...     x += 1

        >>> graph.replay()
    FNÚdefault©ÚpoolÚcapture_error_modeÚ
keep_graphr
   r   )r   ÚglobalÚthread_localÚrelaxedÚreturnc                óH   •— t          ¦   «                              | |¦  «        S ©N)ÚsuperÚ__new__)Úclsr   r
   r   Ú	__class__s       €úV/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/torch/accelerator/graphs.pyr   zGraph.__new__3   s   ø€ õ ‰wŒw�Š˜s JÑ/Ô/Ð/ó    c                óf   •— t          ¦   «                              |¦  «         || _        || _        d S r   )r   Ú__init__Ú
graph_poolr   )Úselfr   r
   r   r   s       €r   r   zGraph.__init__>   s2   ø€ õ 	‰Œ×Ò˜Ñ$Ô$Ð$ØˆŒØ"4ˆÔÐÐr   c                 ób   •— t          ¦   «                              | j        | j        ¬¦  «         dS )a  
        Begin graph capture on the current stream.

        All operations on the current stream after this call will be recorded into the graph until
        ``capture_end`` is called, using the memory pool and capture error mode provided at construction time.
        r	   N)r   Úcapture_beginr   r   ©r   r   s    €r   r   zGraph.capture_beginL   s:   ø€ õ 	‰Œ×ÒØ”°TÔ5Lð 	ñ 	
ô 	
ð 	
ð 	
ð 	
r   c                 óH   •— t          ¦   «                              ¦   «          dS )z”
        End graph capture on the current stream of the current device.

        After this call, the graph can be replayed via ``replay``.
        N)r   Úcapture_endr   s    €r   r!   zGraph.capture_endW   ó!   ø€ õ 	‰Œ×ÒÑÔÐÐÐr   c                 óH   •— t          ¦   «                              ¦   «          dS )zâ
        Instantiate the underlying 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.
        N)r   Úinstantiater   s    €r   r$   zGraph.instantiate_   r"   r   c                 óH   •— t          ¦   «                              ¦   «          dS )z'Replay the work captured by this graph.N)r   Úreplayr   s    €r   r&   zGraph.replayg   s   ø€ å‰Œ�ŠÑÔÐÐÐr   c                 óH   •— t          ¦   «                              ¦   «          dS )zñ
        Delete the graph currently held by this instance.

        After this call, the graph can be recaptured. Set :attr:`graph_pool` or
        :attr:`capture_error_mode` beforehand to use different settings on the next capture.
        N)r   Úresetr   s    €r   r(   zGraph.resetk   s   ø€ õ 	‰Œ�Š‰Œˆˆˆr   c                 óD   •— t          ¦   «                              ¦   «         S )aT  
        Return 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.

        Example::
            >>> # xdoctest: +SKIP
            >>> g1 = torch.accelerator.Graph()
            >>> g1.capture_begin()
            >>> # ... operations ...
            >>> g1.capture_end()

            >>> # Share g1's memory pool with a new graph
            >>> pool_id = g1.pool()
            >>> g2 = torch.accelerator.Graph(pool=pool_id)
        )r   r
   r   s    €r   r
   z
Graph.poolt   s   ø€ õ$ ‰wŒw�|Š|‰~Œ~Ðr   c                 óD   •— t          ¦   «                              ¦   «         S )z)Enable debugging mode for ``debug_dump``.)r   Úenable_debug_moder   s    €r   r+   zGraph.enable_debug_modeˆ   s   ø€ å‰wŒw×(Ò(Ñ*Ô*Ð*r   Úpathc                 óF   •— t          ¦   «                              |¦  «        S )a.  
        Dump the captured graph to a file for debugging purposes if the debugging is
        enabled via ``enable_debug_mode``.

        Arguments:
            path (str): Path to dump the graph to.

        Example::
            >>> # xdoctest: +SKIP
            >>> s = torch.Stream()
            >>> g = torch.accelerator.Graph()
            >>> g.enable_debug_mode()

            >>> with s, g:
            >>> # ... operations ...

            >>> # Dump captured graph to a file "graph_dump.dot"
            >>> g.debug_dump("graph_dump.dot")
        )r   Ú
debug_dump)r   r,   r   s     €r   r.   zGraph.debug_dumpŒ   s   ø€ õ( ‰wŒw×!Ò! $Ñ'Ô'Ð'r   c                 ó4  — t           j                             ¦   «          t           j        j        j        rt          j        ¦   «          t           j                             ¦   «          t           j         	                    ¦   «          |  
                    ¦   «          d S r   )ÚtorchÚacceleratorÚsynchronizeÚcompilerÚconfigÚforce_cudagraph_gcÚgcÚcollectÚempty_cacheÚempty_host_cacher   )r   s    r   Ú	__enter__zGraph.__enter__¢   sv   € ÝÔ×%Ò%Ñ'Ô'Ð'ÝŒ>Ô Ô3ð 	õ ŒJ‰LŒLˆLÝÔ×%Ò%Ñ'Ô'Ð'ÝÔ×*Ò*Ñ,Ô,Ð,Ø×ÒÑÔÐÐÐr   Úexc_infoc                 ó.   — |                       ¦   «          d S r   )r!   )r   r;   s     r   Ú__exit__zGraph.__exit__­   s   € Ø×ÒÑÔÐÐÐr   )F)r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚboolÚtupleÚintr   r   r   r   r   r!   r$   r&   r(   r
   r+   Ústrr.   r:   Úobjectr=   Ú__classcell__)r   s   @r   r   r   	   s[  ø€ € € € € ð'ð 'ðV !ð	0ð (,ð ð	0ð 	0ð 	0àð	0ð �C˜�HŒo Ñ$ð		0ð
 $Ø:ô
ð	0ð 
ð	0ð 	0ð 	0ð 	0ð 	0ð 	0ð !ð5ð (,ð ð5ð 5ð 5àð5ð �C˜�HŒo Ñ$ð	5ð
 $Ø:ô
ð5ð 
ð5ð 5ð 5ð 5ð 5ð 5ð	
ð 	
ð 	
ð 	
ð 	
ð 	
ðð ð ð ð ð ðð ð ð ð ð ðð ð ð ð ð ðð ð ð ð ð ð�e˜C ˜H”oð ð ð ð ð ð ð(+ð +ð +ð +ð +ð +ð(˜sð ( tð (ð (ð (ð (ð (ð (ð,	ð 	ð 	ð 	ð &ð ¨Tð ð ð ð ð ð ð ð r   r   )
r6   Útypingr   Útyping_extensionsr   r0   Útorch._Cr   r   Ú__all__© r   r   ú<module>rM      sŽ   ðØ 	€	€	€	Ø Ð Ð Ð Ð Ð Ø "Ð "Ð "Ð "Ð "Ð "à €€€Ø &Ð &Ð &Ð &Ð &Ð &ðeð eð eð eð eÐñ eô eð eðP ˆ)€€€r   