o
    kiW                     @  sz  U d dl mZ d dlZd dlmZ d dlmZmZmZmZm	Z	 d dl
mZmZmZ d dlZd dlmZ er;d dlmZ dd	lmZ g d
ZedZedZeejdskedejjd< edejjd< edejjd< d dlmZmZmZ d3ddZd4ddZG dd deZ G dd dZ!e	dede"f f Z#de$d < e	!	"	d5d6d+d,Z%e	!	"	d5d7d/d,Z%	!	"	d5d8d2d,Z%dS )9    )annotationsN)Callable)OptionaloverloadTYPE_CHECKING	TypeAliasUnion)	ParamSpecSelfTypeVar)Tensor)_POOL_HANDLE   )_dummy_type)is_current_stream_capturinggraph_pool_handleXPUGraphgraphmake_graphed_callables_R_P_XpuStreamBase	_XPUGraph_xpu_graph_pool_handle_xpu_isCurrentStreamCapturing)r   r   r   returnboolc                   C  s   t  S )zReturn True if XPU graph capture is underway on the current XPU stream, False otherwise.

    If a XPU context does not exist on the current device, returns False without initializing the context.
    )r    r   r   Z/var/www/addictedbytheproject.nl/epg/venv/lib/python3.10/site-packages/torch/xpu/graphs.pyr   )   s   r   r   c                   C  s   t jt S )zBReturn an opaque token representing the id of a graph memory pool.)torchxpur   r   r   r   r   r   r   1      r   c                      s   e Zd ZdZd%d& fddZd'd( fddZd) fddZd) fddZd) fddZd) fddZ	d* fddZ
d) fddZd+ fddZd, fd!d"Zd, fd#d$Z  ZS )-r   a  Wrapper around a XPU graph.

    Arguments:
        keep_graph (bool, optional): If ``keep_graph=False``, the
            executable command graph will be instantiated on GPU at the end of
            ``capture_end`` and the underlying modifiable command graph will be
            destroyed. Note that the executable command graph 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``.

    F
keep_graphr   r   r
   c                   s   t  | |S N)super__new__)clsr"   	__class__r   r   r%   G   s   zXPUGraph.__new__NpoolOptional[_POOL_HANDLE]Nonec                   s   t  j|d dS )a  Begin capturing XPU work on the current xpu stream.

        Typically, you shouldn't call ``capture_begin`` yourself.
        Use :class:`~torch.xpu.graph`, which call ``capture_begin`` internally.

        Arguments:
            pool (optional): Token (returned by :func:`~torch.xpu.graph_pool_handle` or
                :meth:`other_Graph_instance.pool()<torch.xpu.XPUGraph.pool>`) that hints this graph may share memory
                with the indicated pool.
        r)   N)r$   capture_begin)selfr)   r'   r   r   r-   J   s   zXPUGraph.capture_beginc                      t    dS )a  End XPU 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.xpu.graph`, which call ``capture_end`` internally.
        N)r$   capture_endr.   r'   r   r   r0   W   s   zXPUGraph.capture_endc                   r/   )a/  Instantiate the XPU 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 xpu modify command graph returned
        by ``raw_xpu_graph``.
        N)r$   instantiater1   r'   r   r   r2   a   s   zXPUGraph.instantiatec                   r/   )z+Replay the XPU work captured by this graph.N)r$   replayr1   r'   r   r   r3   j   r!   zXPUGraph.replayc                   r/   )z1Delete the graph currently held by this instance.N)r$   resetr1   r'   r   r   r4   n   r!   zXPUGraph.resetr   c                   
   t   S )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.
        )r$   r)   r1   r'   r   r   r)   r   s   
zXPUGraph.poolc                   r5   )z.Enable debugging mode for XPUGraph.debug_dump.)r$   enable_debug_moder1   r'   r   r   r6   z   s   
zXPUGraph.enable_debug_mode
debug_pathstrc                   s   t  |S )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 XPUGraph.enable_debug_mode()
        )r$   
debug_dump)r.   r7   r'   r   r   r9   ~   s   zXPUGraph.debug_dumpintc                   r5   )zReturns the underlying xpuGraph_t. ``keep_graph`` must be True.

        XPU doesn't provide APIs to manipulate this object.
        )r$   raw_xpu_graphr1   r'   r   r   r;         
zXPUGraph.raw_xpu_graphc                   r5   )a  Returns the underlying xpuGraphExec_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_xpu_graph_exec()``, the previously returned xpuGraphExec_t will be destroyed. It is your responsibility not to use this object after destruction.

        XPU doesn't provide APIs to manipulate this object.
        )r$   raw_xpu_graph_execr1   r'   r   r   r=      r<   zXPUGraph.raw_xpu_graph_exec)F)r"   r   r   r
   r#   )r)   r*   r   r+   r   r+   r   r   )r7   r8   r   r+   )r   r:   )__name__
__module____qualname____doc__r%   r-   r0   r2   r3   r4   r)   r6   r9   r;   r=   __classcell__r   r   r'   r   r   6   s    
	
r   c                   @  sB   e Zd ZU dZdZded< 		ddd
dZdddZdddZdS )r   a  Context-manager that captures XPU work into a :class:`torch.xpu.XPUGraph` object for later replay.

    Arguments:
        xpu_graph (torch.xpu.XPUGraph): Graph object used for capture.
        pool (optional): Opaque token (returned by a call to :func:`~torch.xpu.graph_pool_handle()` or
            :meth:`other_Graph_instance.pool()<torch.xpu.XPUGraph.pool>`) hinting this graph's capture
            may share memory from the specified pool.
        stream (torch.xpu.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.

    .. 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.

    NOptional[torch.xpu.Stream]default_capture_stream	xpu_graphr   r)   r*   streamc                 C  sh   | j jd u rtj | j _|d u rdn|f| _|d ur|n| j j| _| jd u r+td| j| _|| _	d S )Nr   zcapture_stream must not be None)
r(   rF   r   r    Streamr)   capture_streamAssertionError
stream_ctxrG   )r.   rG   r)   rH   r   r   r   __init__   s   	

zgraph.__init__r   r+   c                 C  s0   t j  t j  | j  | jj| j  d S r#   )	r   r    synchronizeempty_cacherL   	__enter__rG   r-   r)   r1   r   r   r   rP      s   


zgraph.__enter__argsobjectc                 G  s   | j   | jj|  d S r#   )rG   r0   rL   __exit__)r.   rQ   r   r   r   rS      s   
zgraph.__exit__)NN)rG   r   r)   r*   rH   rE   r>   )rQ   rR   r   r+   )	r@   rA   rB   rC   rF   __annotations__rM   rP   rS   r   r   r   r   r      s   
 
	r   torch.nn.Module.r   _ModuleOrCallable   F	callablessample_argstuple[Tensor, ...]num_warmup_itersr:   allow_unused_inputr)   r*   c                 C     d S r#   r   rX   rY   r[   r\   r)   r   r   r   r         r   tuple[_ModuleOrCallable, ...]tuple[tuple[Tensor, ...], ...]c                 C  r]   r#   r   r^   r   r   r   r      r_   7Union[_ModuleOrCallable, tuple[_ModuleOrCallable, ...]]9Union[tuple[Tensor, ...], tuple[tuple[Tensor, ...], ...]]c           )        s  t  rt  rtdd}t| ts$d}| f} tttdf |f}nttttdf df |}g  t	| |D ]N\}}t|t j
jrlt|jdkrYt|jdkrYt|jdks]tdtdd | D sltd	t jjj| }	 t|	 td
d |	D stdq9dd  D }
dd | D  fddtt| D }dd tt| D }dd tt| D }|du rt n|}t j  t jt j \ t	| ||D ]M\}}}d\}}}t|D ]4}t jj|| }tdd |D }t|dkrt jj|tdd |D tdd |D d|d}q|||fD ]}~q qW d   n	1 s1w   Y  t j  g }g }t	| ||D ]8\}}}t jj ||d || }W d   n	1 sbw   Y  t jj!|\}}|t| || qEg }g }t	t"|t"|t"|D ]\}}}tdd |D } tdd |D }d}t|dkrt jj ||d! t jj|tdd |D tdd | D d|d}W d   n	1 sw   Y  g }!d}"|D ]}#|#j#r|dur|!||"  |"d7 }"q|!d qt|!}!||  ||! q|$  |$  d:d/d0}$g }%t%| D ]F\}&}|$||& ||& |& |
|& ||& ||& ||& ||& ||& 	}'t|t j
jrhd;d8d9}(|(||j&|'|j'|_'|%| q(|%|' q(|rv|%d S t|%S )<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 XPU work as a XPU 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 XPU 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.

    Arguments:
        callables (torch.nn.Module or Python function, or tuple of these): Callable or callables to graph.
            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.xpu.graph_pool_handle` or
            :meth:`other_Graph_instance.pool()<torch.xpu.XPUGraph.pool>`) that hints this graph may share memory
            with the indicated pool.
    .. 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.xpu.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.xpu.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.xpu.make_graphed_callables` only with disabled
        caching. The context manager `torch.amp.autocast()` must have `cache_enabled=False`.
    z_make_graphed_callables does not support the autocast caching. Please set `cache_enabled=False`.FT.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.c                 s  s    | ]}|j d u V  qdS )FNrequires_grad.0br   r   r   	<genexpr>I      z)make_graphed_callables.<locals>.<genexpr>zIn any :class:`~torch.nn.Module` passed to :func:`~make_graphed_callables`, only parameters may be trainable. All buffers must have ``requires_grad=False``.c                 s  s    | ]	}t |tjV  qd S r#   )
isinstancer   r   )rg   argr   r   r   ri   Q      zfIn the beta API, sample_args for each callable must contain only Tensors. Other types are not allowed.c                 S  s   g | ]}t |qS r   )len)rg   rQ   r   r   r   
<listcomp>Y  s    z*make_graphed_callables.<locals>.<listcomp>c                 S  s*   g | ]}t |tjjrt| nd qS )r   )rk   r   nnModuletuple
parameters)rg   cr   r   r   ro   Z  s    c                   s   g | ]
} | |  qS r   r   rg   iflatten_sample_argsper_callable_module_paramsr   r   ro   ^  s    c                 S     g | ]}t j qS r   r   r    r   rg   _r   r   r   ro   c      c                 S  rz   r   r{   r|   r   r   r   ro   d  r~   N)NNNc                 s      | ]}|j r|V  qd S r#   rd   rg   or   r   r   ri   q  rj   c                 s  r   r#   rd   ru   r   r   r   ri   u  s    
c                 s  s     | ]}|j rt|V  qd S r#   re   r   
empty_liker   r   r   r   ri   x  s    
)outputsinputsgrad_outputsonly_inputsallow_unusedr,   c                 s  s$    | ]}|j rt|nd V  qd S r#   r   r   r   r   r   ri     s    
c                 s  r   r#   rd   r   r   r   r   ri     rj   c                 s  r   r#   rd   ru   r   r   r   ri     rj   c                 s  s    | ]	}|d ur|V  qd S r#   r   r   r   r   r   ri     rm      	fwd_graphr   	bwd_graphmodule_paramstuple[torch.nn.Parameter, ...]len_user_argsr:   output_unflatten_spectorch.utils._pytree.TreeSpecstatic_input_surfacerZ   static_outputsstatic_grad_outputstuple[Optional[Tensor], ...]static_grad_inputsr   Callable[..., object]c	           
        s:   G 	fdddt jj d fdd}	|	S )	Nc                      sD   e Zd ZedfddZeejjjd fd
dZ	dS )zOmake_graphed_callables.<locals>.make_graphed_autograd_function.<locals>.GraphedctxrR   r   r   r   rZ   c                   sd   t D ]}|  ||  kr| ||  q   tts)tdtdd D S )Nzstatic_outputs must be a tuplec                 s  s    | ]}|  V  qd S r#   detachr   r   r   r   ri     s    zjmake_graphed_callables.<locals>.make_graphed_autograd_function.<locals>.Graphed.forward.<locals>.<genexpr>)rangedata_ptrcopy_r3   rk   rr   RuntimeError)r   r   rv   )r   r   r   r   r   r   forward  s   
zWmake_graphed_callables.<locals>.make_graphed_autograd_function.<locals>.Graphed.forwardgradsc                   s   t |t krtdt  dt | t|D ]\}}|d ur0| | kr0|| q   tts>tdtdd D S )Nz	Expected z gradients but got z"static_grad_inputs must be a tuplec                 s  s$    | ]}|d ur|  n|V  qd S r#   r   rf   r   r   r   ri     s
    
zkmake_graphed_callables.<locals>.make_graphed_autograd_function.<locals>.Graphed.backward.<locals>.<genexpr>)rn   r   zipr   r   r3   rk   rr   )r   r   ggrad)r   r   r   r   r   backward  s   

zXmake_graphed_callables.<locals>.make_graphed_autograd_function.<locals>.Graphed.backwardN)r   rR   r   r   r   rZ   )r   rR   r   r   r   rZ   )
r@   rA   rB   staticmethodr   r   autogradfunctiononce_differentiabler   r   )r   r   r   r   r   r   r   r   r   Graphed  s    
r   	user_argsrR   r   c                    s0   t jjj|  } jt|  }t jj|S r#   )r   utils_pytreearg_tree_leavesapplyrr   tree_unflatten)r   flatten_user_argsout)r   r   r   r   r   functionalized  s   zVmake_graphed_callables.<locals>.make_graphed_autograd_function.<locals>.functionalized)r   rR   r   rR   )r   r   Function)
r   r   r   r   r   r   r   r   r   r   r   )
r   r   r   r   r   r   r   r   r   r   r   make_graphed_autograd_function  s   $#z>make_graphed_callables.<locals>.make_graphed_autograd_functionfuncrU   graph_training_stater   graphedCallable[_P, _R]orig_fwdc                   s   d	 fdd}|S )
Nr   _P.argsuser_kwargs	_P.kwargsr   r   c                    s&    j kr| i |S | i |S r#   )training)r   r   r   r   r   r   r   r   new_fwd  s   
zEmake_graphed_callables.<locals>.make_graphed_forward.<locals>.new_fwd)r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   make_graphed_forward  s   z4make_graphed_callables.<locals>.make_graphed_forward)r   r   r   r   r   r   r   r:   r   r   r   rZ   r   rZ   r   r   r   rZ   r   r   )
r   rU   r   r   r   r   r   r   r   r   )(r   is_autocast_enabledis_autocast_cache_enabledr   rk   rr   typingcastr   r   rp   rq   rn   _backward_hooks_forward_hooks_forward_pre_hooksallbuffersr   r   r   append	TypeErrorr   r   r    rN   rH   rI   tree_leavesr   r   r   tree_flattenreversedre   reverse	enumerater   r   ))rX   rY   r[   r\   r)   just_one_callable_sample_argsrt   rQ   flatten_argper_callable_len_user_args"per_callable_static_input_surfaces
fwd_graphs
bwd_graphsmempoolr   r   grad_inputsr   outputs_gradr}   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_idxrl   r   retrv   r   r   r   rw   r   r      s  G





	

6
)r   r   r?   )rW   FN)rX   rV   rY   rZ   r[   r:   r\   r   r)   r*   r   rV   )rX   r`   rY   ra   r[   r:   r\   r   r)   r*   r   r`   )rX   rb   rY   rc   r[   r:   r\   r   r)   r*   r   rb   )&
__future__r   r   collections.abcr   r   r   r   r   r   typing_extensionsr	   r
   r   r   r   	torch.xpur   _utilsr   __all__r   r   hasattr_C__dict__torch._Cr   r   r   r   r   r   r   rR   rV   rT   r   r   r   r   r   <module>   sN    

a8	