o
    ei{                     @   s4  U d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlmZ d dlm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ddd	Zdd
dZdd Zdd Zdd Z dd Z!dd Z"dd Z#	dddZ$	dddZ%g Z&e'd e(d< dd Z)d d! Z*d"d# Z+d$d% Z,d&d' Z-e-Z.d(d) Z/d*d+ Z0d,d- Z1d.d/ Z2d0d1 Z3d2d3 Z4d4d5 Z5d6d7 Z6d8d9 Z7d:d; Z8d<d= Z9d>d? Z:d@dA Z;dBdC Z<dDdE Z=dFdG Z>G dHdI dIe?Z@G dJdK dKZAdLdM ZBdNdO ZCdPdQ ZDdRdS ZEdTdU ZFdVeGfdWdXZH		ddYedZeId[eIdVeGfd\d]ZJd^d_ ZKd`da ZLG dbdc dcZMddde ZNerEedfeOdgdVeIfdhdiZPndVeIfdjdiZPdkdl ZQeRdmdne?fdodpZSdqe?dVeTfdrdsZUG dtdu duZVeWeXZYedvZZG dwdx dxeeZ Z[dye?dVedB fdzd{Z\d|d}d~dddddddddZ]i ddddddddddddddddddddddddddddddddddZ^dddeGdeGdVdfddZ_G dd deZ`G dd deZaG dd deZbG dd deZcG dd deZdde'e` dVeGfddZede?edB dVedfddZfdS )    N)defaultdict)Callable)
ModuleType)AnycastGenericTYPE_CHECKING	TypedDict)
deprecatedNotRequired	ParamSpecFc           	      K   s   t d||}|du r| jd | jj S t|trt|}|t| u r$| S | jr\|js.t	d|j
dd}|d |j }tj| ||}|d }tj| ||}||||  S |jrct	d||  | |S )	a  Returns the type if `dtype` is not provided, else casts this object to
    the specified type.

    If this is already of the correct type, no copy is performed and the
    original object is returned.

    Args:
        dtype (type or string): The desired type
        non_blocking (bool): If ``True``, and the source is in pinned memory
            and destination is on the GPU or vice versa, the copy is performed
            asynchronously with respect to the host. Otherwise, the argument
            has no effect.
        **kwargs: For compatibility, may contain the key ``async`` in place of
            the ``non_blocking`` argument. The ``async`` arg is deprecated.
    typeN.z)Cannot cast sparse tensor to dense tensorz.sparse z.LongTensorz)Cannot cast dense tensor to sparse tensor)_get_async_or_non_blocking
__module__	__class____name__
isinstancestr_import_dotted_namer   	is_sparseRuntimeErrorreplacetorchTensor_values_indicessizecopy_)	selfdtypenon_blockingkwargsnew_module_namenew_values_type_name
new_valuesnew_indices_type_namenew_indices r)   V/var/www/addictedbytheproject.nl/epg/venv/lib/python3.10/site-packages/torch/_utils.py_type   s*   
r+   c           	      C   sf  | j |kr| S |jdkr/|o| j jdtj fv }tj|  tj||d }|	| | |S t
t|jd}|du rDt|j  d| |` | jrt|drt
|j| jj}t
tj| |j||}t
tj| |j||}||||  W  d   S | jrtd|j  dtj|  |d	}|	| | |W  d   S 1 sw   Y  dS )
a  Returns a copy of this object in device memory.

    If this object is already on the correct device, then no copy is performed
    and the original object is returned.

    Args:
        device (int): The destination device.
        non_blocking (bool): If ``True`` and the source is in pinned memory,
            the copy will be asynchronous with respect to the host. Otherwise,
            the argument has no effect.
    cpucuda)r!   device
pin_memoryNz device module is not loadedsparsez$sparse storage is not supported for z tensors)r.   )r.   r   r   _C_get_privateuse1_backend_nameemptynbytesuint8untyped_storager   getattrAssertionErrorupperr   hasattrr0   r   r   r   r   r   r   UntypedStorage)	r    r.   r"   r/   r6   device_modulenew_typeindicesvaluesr)   r)   r*   _to>   sH   


$r@   c                 C   sV   |s|S t |dksd|vr d}t|  }t|| |tjddd |d S )zReturn the non-blocking flag given the function name and kwargs.

    Args:
        function_name (str): the name of the function being used.
        non_blocking (bool): the default value.
        **kwargs (dict): the kwargs passed to the function.
       asyncz,{}() got an unexpected keyword argument '{}'z)'async' is deprecated; use 'non_blocking'   
stacklevel)lenlistkeyspop	TypeErrorformatwarningswarn)function_namer"   r#   messageargumentr)   r)   r*   r   o   s   r   c                 C   sd   t jjj}|du r| S t|tr|| | S t|tt jfr |S t	|s.t
dt|j td)z{Return the map_location location.

    Used for rebuild functions where the tensor device is distinct from the storage
    Nz$expected callable map_location, got zgCallable map_location not supported with _rebuild_wrapper_subclass or _rebuild_device_tensor_from_numpy)r   serialization_serialization_tlsmap_locationr   dictgetr   r.   callabler8   r   r   r   )r.   rS   r)   r)   r*   _get_restore_location   s   

rW   c                 C   s(   t jd| j| jjd}|| j|||S )Nr   r!   r.   )r   r3   r!   _untyped_storager.   set_)storagestorage_offsetr   stridetr)   r)   r*   _rebuild_tensor   s   r`   c                 C   s,   t | tjstdt| j tj| S )Nexpected torch.Tensor, got )r   r   r   r8   r   r   r1   _get_tensor_metadatatensorr)   r)   r*   get_tensor_metadata   s   re   c                 C   sP   t |tstdt|j t | tjstdt| j tj| | d S )Nzexpected dict, got ra   )	r   rT   r8   r   r   r   r   r1   _set_tensor_metadata)rd   metadatar)   r)   r*   set_tensor_metadata   s
   
rh   c                 C   sR   t jd d ur'|  jd ur't|  j}t|t js!t |}t || _| S N)	r   _guardsdetect_fake_moder6   _fake_devicerW   r   r.   fake_device)rd   r.   r)   r)   r*   _restore_device_fake_mode   s   
rn   c                 C   s4   t | |||}||_|rt|| ||_t|}|S ri   )r`   requires_gradrh   _backward_hooksrn   )r\   r]   r   r^   ro   backward_hooksrg   rd   r)   r)   r*   _rebuild_tensor_v2   s   	
rr   c           	      C   sH   t jd|| jj|d}|| j||| |rt|| ||_t|}|S )NrX   r!   r.   ro   )r   r3   rZ   r.   r[   rh   rp   rn   )	r\   r]   r   r^   ro   rq   r!   rg   r_   r)   r)   r*   _rebuild_tensor_v3   s   

rt   ztorch.Tensor_sparse_tensors_to_validatec               	   C   s   t j  st  d S zgtD ]]} | jt ju r+t j| 	 | 
 |  |  dd q| jt jt jt jt jhv re| jt jt jhv rK|  |  }}n	|  |  }}t j|||  |  | jdd qtd| j dW t  d S t  w )NF)check_pinningz,_validate_loaded_sparse_tensors for layout ``)r   r0   check_sparse_tensor_invariants
is_enabledru   clearlayout
sparse_coo _validate_sparse_coo_tensor_argsr   r   r   is_coalesced
sparse_csr
sparse_csc
sparse_bsr
sparse_bsccrow_indicescol_indicesccol_indicesrow_indices'_validate_sparse_compressed_tensor_argsr?   NotImplementedError)r_   compressed_indicesplain_indicesr)   r)   r*   _validate_loaded_sparse_tensors  sN   	)r   c           	      C   s   | t jkr*t|dkr|\}}}d}n|\}}}}t j|||d|d}t| |S | t jt jt jt j	hv rN|\}}}}t j
||||| dd}t| |S td|  )z
    Rebuilds a sparse tensor from its sparse storage representation.

    Args:
        layout (str): The sparse storage layout of the tensor.
        data (tuple): The tensor's sparse storage representation.
       NF)check_invariantsr~   )r{   r   z$rebuilding sparse tensor for layout )r   r|   rF   sparse_coo_tensorru   appendr   r   r   r   sparse_compressed_tensorr   )	r{   datar>   r?   r   r~   resultr   r   r)   r)   r*   _rebuild_sparse_tensorW  s8   




r   c                 C   s   t | |||S ri   )r   _nested_view_from_buffer)buffersizesstridesstorage_offsetsr)   r)   r*   _rebuild_nested_tensor     r   c                 C   s    t |}| j||d}||_|S NrY   )rW   toro   r   r!   r.   ro   rd   r)   r)   r*   &_rebuild_device_tensor_from_cpu_tensor  s   r   c                 C   s&   t |}t| j||d}||_|S r   )rW   r   
from_numpyr   ro   r   r)   r)   r*   !_rebuild_device_tensor_from_numpy  s   r   c                 C   s   t j||| d|dS )Nmetars   )r   empty_strided)r!   r   r^   ro   r)   r)   r*   _rebuild_meta_tensor_no_storage  s   
r   c              
   C   s$   t |}tjj| |||||||dS )N)r   r!   r]   r{   r.   ro   )rW   r   r   _make_wrapper_subclass)clsr!   r   r^   r]   r{   r.   ro   r)   r)   r*   _rebuild_wrapper_subclass  s   
r   c                 C   s  |d }|t jkr|\}}	}
t j||	|
| j| jd}n]|t jt jfv rq|\}}}}t|tu rct|tu rc|t jkrOt j	|t j
| jd}t j	|t j| jd}nt j	|t j| jd}t j	|t j| jd}t j||||| j| jd}ntd| || ||| ||_||_|S )Nr   )scale
zero_pointr!   r.   rY   )scaleszero_pointsaxisr!   r.   z0Can't deserialize quantized tensor with qscheme )r   per_tensor_affine_empty_affine_quantizedr!   r.   per_channel_affine per_channel_affine_float_qparamsr   rG   rd   doublelongfloat#_empty_per_channel_affine_quantizedr   r[   ro   rp   )r\   r]   r   r^   quantizer_paramsro   rq   qscheme_r   r   rd   r   r   r   r)   r)   r*   _rebuild_qtensor  sF   	




	r   c                 C   s   t j| |}||_|S ri   )r   nn	Parameterrp   )r   ro   rq   paramr)   r)   r*   _rebuild_parameter  s   r   c                 C   s"   t j| |}||_t||}|S ri   )r   r   r   rp   _set_obj_state)r   ro   rq   stater   r)   r)   r*   _rebuild_parameter_with_state  s   
r   c                    sP   t  dd }|r| }|S t j}|r# j fdd|D f}|S  j}|S )N__getstate__c                    s"   i | ]}t  |r|t |qS r)   )r:   r7   ).0nameobjr)   r*   
<dictcomp>  s    
z"_get_obj_state.<locals>.<dictcomp>)r7   copyreg
_slotnamesr   __dict__)r   getstate_fnr   slots_to_saver)   r   r*   _get_obj_state  s   
r   c                 C   s   t |trt|dkstd| |d }|d }n|}d }|r0| D ]
\}}t| || q%|rA| D ]
\}}t| || q6| S )NrC   zInvalid serialized state: r   rA   )r   tuplerF   r   itemssetattr)r   r   
dict_stateslots_statekvr)   r)   r*   r     s   

r   c                 C   s6   |  d}t|d }|dd  D ]}t||}q|S )Nr   r   rA   )split
__import__r7   )r   
componentsr   	componentr)   r)   r*   r   0  s
   
r   c                 C   s   t jj| S )a  Flatten dense tensors into a contiguous 1D buffer. Assume tensors are of
    same dense type.

    Since inputs are dense, the resulting tensor will be a concatenated 1D
    buffer. Element-wise operation on this buffer will be equivalent to
    operating individually.

    Args:
        tensors (Iterable[Tensor]): dense tensors to flatten.

    Returns:
        A contiguous 1D buffer containing input tensors.
    r   r1   _nnflatten_dense_tensors)tensorsr)   r)   r*   _flatten_dense_tensors8  s   r   c                 C   s8   t jjdd | D }t jjdd | D }||fS )ab  Flatten sparse tensors into two contiguous 1D buffers, one of indices and
    one of values. Assume tensors are of same sparse type.

    Args:
        tensors (Iterable[Tensor]): sparse tensors to flatten.

    Returns:
        A tuple of two contiguous 1D buffers, one containing input tensors'
        indices and the other containing the values.
    c                 S      g | ]}t j|qS r)   r   r   r   r   r_   r)   r)   r*   
<listcomp>U      z+_flatten_sparse_tensors.<locals>.<listcomp>c                 S   r   r)   r   r   r   r   r)   r)   r*   r   X  r   r   )r   flat_indicesflat_valuesr)   r)   r*   _flatten_sparse_tensorsI  s   r   c                 C   s   t jj| |S )a  View a flat buffer using the sizes of tensors. Assume that tensors are of
    same dense type, and that flat is given by _flatten_dense_tensors.

    Args:
        flat (Tensor): flattened dense tensors to unflatten.
        tensors (Iterable[Tensor]): dense tensors whose sizes will be used to
          unflatten flat.

    Returns:
        Unflattened dense tensors with sizes same as tensors and values from
        flat.
    )r   r1   r   unflatten_dense_tensors)flatr   r)   r)   r*   _unflatten_dense_tensors]  s   r   c           
   	   C   sx   | \}}t jj|dd |D }t jj|dd |D }g }t|||D ]\}}}	||||	|  q&t|S )a  View flat buffer (containing indices and values) using the sizes of
    tensors. Assume that tensors are of same sparse type, and that flat is given
    by _flatten_sparse_tensors.

    Args:
        flat (tuple(Tensor, Tensor)): flattened indices and values of sparse
          tensors to unflatten.
        tensors (Iterable[Tensor]): sparse tensors whose sizes will be used to
          unflatten flat.

    Returns:
        Unflattened sparse tensors with sizes same as tensors and values from
        flat.
    c                 S   r   r)   r   r   r)   r)   r*   r   ~  r   z-_unflatten_sparse_tensors.<locals>.<listcomp>c                 S   r   r)   r   r   r)   r)   r*   r     r   )	r   r1   r   r   zipr   newr   r   )
r   r   r   r   r>   r?   outputsr_   ir   r)   r)   r*   _unflatten_sparse_tensorsm  s   r   c                    sL   t t}| D ]}||  | qdd | D  t fdd|D S )a  Assume that tensors are of same order as ordered_tensors within their
    types, e.g., from _take_tensors. Reorder them to be of same order as
    ordered_tensors.

    Args:
        tensors (Iterable[Tensor]): tensors to be reordered. They should be of
          the same order as ordered_tensors within their own types.
        ordered_tensors (Iterable[Tensor]): tensors whose order will be the
          reference.

    Returns:
        Ordered tuple of tensors with contents from tensors and order of
        ordered_tensors.
    c                 S   s   i | ]	\}}|t |qS r)   )iter)r   r_   collr)   r)   r*   r     s    z'_reorder_tensors_as.<locals>.<dictcomp>c                 3   s     | ]}t  |  V  qd S ri   )nextr   )r   rd   
type_dict_r)   r*   	<genexpr>  s    z&_reorder_tensors_as.<locals>.<genexpr>)r   rG   r   r   r   r   )r   ordered_tensors	type_dictrd   r)   r   r*   _reorder_tensors_as  s
   r   c                 c   s    t dd }| D ]\}| }|jr/tj|}tj|}| |  | |   }n| |  }|| }|d | |krV|d dkrV|d V  g dg }||< |d 	| |d  |7  < q	|
 D ]\}	}
t|	dkrw|	V  qjdS )a  Group tensors into chunks. This generator yields a chunk at each time,
    each containing tensors of same type up to certain byte limit in total size.

    Args:
        tensors (Sequence): A sequence of tensors to be separated into chunks.
        size_limit (int): The limit of each chunk in bytes.

    Yields:
        Blocks of tensors of same type and within size_limit. The yielded
        tensors are only ordered as the original sequence within its types.
    c                   S   s   g dgS )Nr   r)   r)   r)   r)   r*   <lambda>      z_take_tensors.<locals>.<lambda>rA   r   N)r   r   r   r   r   r   r   numelelement_sizer   r?   rF   )r   
size_limitbuf_dictrd   r_   r>   r?   r   buf_and_sizebufr   r)   r)   r*   _take_tensors  s.   
r   c                    s    fdd}|S )Nc                    s   t  | _| jd< | S )Nreturn)rT   __annotations__)funr#   retr)   r*   dec  s   

zannotate.<locals>.decr)   )r  r#   r  r)   r  r*   annotate  s   r  c                 C   s   t j| }|d u rt| }g }t jjddd* |dd |D  |dd | D  | dd| d}W d    |S 1 sEw   Y  |S )	Nr   )	threshold	edgeitemsc                 s   s    | ]}t |V  qd S ri   repr)r   ar)   r)   r*   r     s    zrender_call.<locals>.<genexpr>c                 s   s&    | ]\}}| d t | V  qdS )=Nr  )r   r   r   r)   r)   r*   r     s   $ (z, ))	r   	overridesresolve_namer   _tensor_strprintoptionsextendr   join)fnargsr#   str_fnstr_argsrr)   r)   r*   render_call  s   
r  c                   @   s   e Zd ZdZdZdd ZdS )KeyErrorMessagez(str subclass that returns itself in reprr)   c                 C   s   | S ri   r)   r    r)   r)   r*   __repr__  s   zKeyErrorMessage.__repr__N)r   r   __qualname____doc__	__slots__r  r)   r)   r)   r*   r    s    r  c                   @   s"   e Zd ZdZdddZdd ZdS )	ExceptionWrapperz?Wraps an exception plus traceback to communicate across threadsNin backgroundc                 C   s6   |d u rt  }|d | _dtj| | _|| _d S )Nr   r   )sysexc_infoexc_typer  	tracebackformat_exceptionexc_msgwhere)r    r"  r'  r)   r)   r*   __init__  s
   

zExceptionWrapper.__init__c                 C   sv   d| j j d| j d| j }| j tu rt|}nt| j ddr&| j |dz|  |}W | ty:   t|dw )z4Reraises the wrapped exception in the current threadzCaught  z.
Original rO   N)rO   )	r#  r   r'  r&  KeyErrorr  r7   	Exceptionr   )r    msg	exceptionr)   r)   r*   reraise  s   


zExceptionWrapper.reraise)Nr   )r   r   r  r  r(  r.  r)   r)   r)   r*   r    s    

r  c                  C   sx   t j rdS t jj rdS tt drt j rdS tt dr't j r'dS t j	 } t
t | d }|r:| r:| S d S Nr-   mpsxpumtia)r   r-   is_availablebackendsr0  r:   r1  r2  r1   r2   r7   )custom_backend_namecustom_device_modr)   r)   r*   _get_available_device_type  s   

r7  c                 C   s   t  }|r| dkr| tjS |r| dkr| tjS |r*| dkr*| tjS |r7| dkr7| tjS |tj krE| t	t|S d S r/  )
r7  lowerr   r-   r0  r1  r2  r1   r2   r7   )
get_memberdevice_typer)   r)   r*   _get_device_attr%  s   



r;  c                   C      t dd S )Nc                 S   s   |   S ri   )current_devicemr)   r)   r*   r   7  r   z+_get_current_device_index.<locals>.<lambda>r;  r)   r)   r)   r*   _get_current_device_index5     rA  c                   C   r<  )Nc                 S   s   t t|  S ri   )rG   rangedevice_countr>  r)   r)   r*   r   <  s    z)_get_all_device_indices.<locals>.<lambda>r@  r)   r)   r)   r*   _get_all_device_indices:  rB  rE  c                 C   s   dd | D S )Nc                    s   g | ]
 t  fd dqS )c                    s
   |   S ri   )get_device_propertiesr>  r   r)   r*   r   A  s   
 z4_get_devices_properties.<locals>.<listcomp>.<lambda>r@  )r   r)   rG  r*   r   A  s    z+_get_devices_properties.<locals>.<listcomp>r)   )
device_idsr)   r)   r*   _get_devices_properties?  s   rI  r   c                   C   s   t j dkrt j S dS )zChecks if there are CUDA devices available and
    returns the device index of the current default CUDA device.
    Returns -1 in case there are no CUDA devices available.
    Arguments: ``None``
    r   )r   r-   rD  r=  r)   r)   r)   r*   get_current_device_indexD  s   
rK  r.   optional	allow_cpuc                 C   s   t | tr
t| } d}t | tjr*|s | jdkr td|  | jdkr'dn| j}t | tr1| }|du rM|rFtj	 rAt
 }|S t }|S td|  |S )a'  Gets the device index from :attr:`device`, which can be a torch.device
    object, a Python integer, or ``None``.

    If :attr:`device` is a torch.device object, returns the device index if it
    has index. Note that for a device without a specified index,
    i.e., ``torch.device('xxx')``, this will return the current default
    device of that type if :attr:`optional` is ``True``. If :attr:`allow_cpu` is ``True``,
    CPU devices will be accepted and ``-1`` will be returned in this case.

    If :attr:`device` is a Python integer, it is returned as is.

    If :attr:`device` is ``None``, this will return the current default
    device of the supported runtime platform if :attr:`optional` is ``True``.
    i.e., the current default CUDA device will be returned if CUDA runtime is supported.
    Nr,   z$Expected a non cpu device, but got: rJ  zFExpected a torch.device with a specified index or an integer, but got:)r   r   r   r.   r   
ValueErrorindexintjitis_scriptingrK  rA  )r.   rL  rM  
device_idxr)   r)   r*   _get_device_indexO  s(   



rT  c                 C   s$   t | tjjs|  rt| S | S )z
    Returns a real view of a tensor if complex dtype else just the tensor
    need to check if a UninitializedParameter because otherwise checking is_complex is an error for a LazyModule
    )r   r   r   UninitializedParameter
is_complexview_as_realrc   r)   r)   r*   _handle_complex~  s   
rX  c                 C   sh   t | tjstdt|  | jrt| jd? S | jr%t| jd? S | tj	kr,dS t
| jd? S )z8
    Returns the element size for a dtype, in bytes
    zexpected torch.dtype, but got rC   r   rA   )r   r   r!   r   r   rV  finfobitsis_floating_pointbooliinfo)r!   r)   r)   r*   _element_size  s   
r^  c                   @   s    e Zd ZdddZdddZdS )_ClassPropertyDescriptorNc                 C   s
   || _ d S ri   )fget)r    r`  fsetr)   r)   r*   r(    s   
z!_ClassPropertyDescriptor.__init__c                 C   s    |d u rt |}| j|| S ri   )r   r`  __get__)r    instanceownerr)   r)   r*   rb    s   z _ClassPropertyDescriptor.__get__ri   )r   r   r  r(  rb  r)   r)   r)   r*   r_    s    
r_  c                 C   s   t | ttfst| } t| S ri   )r   classmethodstaticmethodr_  )funcr)   r)   r*   classproperty  s   rh  U`torch._utils.is_compiling` is deprecated. Use `torch.compiler.is_compiling` instead.)categoryc                   C   s
   t j S ri   )r   compileris_compilingr)   r)   r)   r*   rl    s   
rl  c                   C   s   t jddd tj S )zd
        Indicates whether we are tracing/compiling with torch.compile() or torch.export().
        ri  rC   rD   )rL   rM   r   rk  rl  r)   r)   r)   r*   rl    s
   
c              	   C   sz   ddl m} t| |r6tjtjjj}zt| j	 W |d ur(tj
| d S d S |d ur5tj
| w w t|  d S )Nr   )FunctionalTensor)#torch._subclasses.functional_tensorrm  r   r   r1   _unset_dispatch_mode_TorchDispatchModeKey
FUNCTIONAL_functionalize_syncelem_set_dispatch_mode)r_   rm  maybe_functional_moder)   r)   r*   rr    s   
rr  rC   r:  c                 C   s.   t t| d }|d u rtd|  d|  d|S )NzDevice 'z<' does not have a corresponding module registered as 'torch.z'.)r7   r   r   )r:  r<   r)   r)   r*   _get_device_module  s   rv  r   c                 C   s*   dt fdd}t| tf|d|ddS )Nis_initc                    s    fdd}|S )Nc                    s"    r| j j}n| j}td| )Nz&Tried to instantiate dummy base class )r   r   r   )r   r  r#   
class_namerw  r)   r*   err_fn  s   
z/_dummy_type.<locals>.get_err_fn.<locals>.err_fnr)   )rw  rz  r)   ry  r*   
get_err_fn  s   z_dummy_type.<locals>.get_err_fnTF)r(  __new__)r\  r   object)r   r{  r)   r)   r*   _dummy_type  s   
r~  c                   @   s2   e Zd Zdd Zdd Zdd Zdefdd	Zd
S )_LazySeedTrackerc                 C   s   d | _ d | _g | _d S ri   manual_seed_all_cbmanual_seed_cb
call_orderr  r)   r)   r*   r(    s   
z_LazySeedTracker.__init__c                 C      ||f| _ | j| j g| _d S ri   r  r    cbr$  r)   r)   r*   queue_seed_all     
z_LazySeedTracker.queue_seed_allc                 C   r  ri   )r  r  r  r  r)   r)   r*   
queue_seed  r  z_LazySeedTracker.queue_seedr   c                 C   s   | j S ri   )r  r  r)   r)   r*   	get_calls  s   z_LazySeedTracker.get_callsN)r   r   r  r(  r  r  rG   r  r)   r)   r)   r*   r    s
    r  Pc                   @   sN   e Zd ZdefddZdeedf ddfddZd	ejd
ej	ddfddZ
dS )CallbackRegistryr   c                 C   s   || _ g | _d S ri   )r   callback_list)r    r   r)   r)   r*   r(    s   
zCallbackRegistry.__init__r  Nr   c                 C   s   | j | d S ri   )r  r   )r    r  r)   r)   r*   add_callback  r   zCallbackRegistry.add_callbackr  r#   c              	   O   sB   | j D ]}z	||i | W q ty   td| j Y qw d S )Nz6Exception in callback for %s registered with gpu trace)r  r+  loggerr-  r   )r    r  r#   r  r)   r)   r*   fire_callbacks!  s   
zCallbackRegistry.fire_callbacks)r   r   r  r   r(  r   r  r  r  r#   r  r)   r)   r)   r*   r    s    r  module_namec                 C   sj   t j| d  }d ur|S tj|  }d ur3tj|}|t j| < |jd u r+td|j	| |S d S )Nz)The loader attribute should always be set)
r!  modulesrU   	importlibutil	find_specmodule_from_specloaderr8   exec_module)r  modulespecr)   r)   r*   
try_import+  s   

r  builtinsr   queuereprlibzcollections.abccollectionsdbmio)__builtin__copy_regQueuer  _abcollUserDictUserList
UserStringwhichdbStringIO	cStringIO)r  xrange)r  rC  )r  reduce)	functoolsr  )r  intern)r!  r  )r  unichr)r  chr)r  unicode)r  r   )r  r   )r  rP  )	itertoolsizip)r  r   )r  imap)r  map)r  ifilter)r  filter)r  ifilterfalse)r  filterfalse)r  izip_longest)r  zip_longest)r  IterableUserDict)r  r  )r  r  )r  r  )r  r  )r  r  )r  
basestring)
exceptionsStandardError)r  r+  )r  r  res
group_size
gather_dimc                 C   s   |dkr| S t | j}|dkrt|d| nd}|d |krC|dkrCdg|d|  |d ||  g ||d d  }| |S tjtj| |dd|dS )a  
    This is intuitively the same as torch.cat(torch.chunk(res, group_size,
    dim=0), dim=gather_dim), but returns a view if data movement is not
    necessary.  This operation arises in NCCL all_gather, where you always get
    a result which is concatenated on dim=0, even though actually you may need
    to undo this concatenation and then re-cat on the gather dim.

    When is data-movement not necessary?  Intuitively, we need to understand if
    the unflatten in this reference implementation of this code triggers a
    copy or not:

        chunks = torch.unflatten(res, 0, [group_size, -1])
        return torch.flatten(torch.movedim(chunks, 0, gather_dim), gather_dim, gather_dim + 1)

    Assume res is contiguous (it will be coming out of the collective).  We
    essentially need to know if the movedim maintains the contiguity of the
    tensor.  Moving a dimension typically does NOT preserve contiguity, unless
    EVERY dimension it is moved across is size 1.

    Example: shape [4, d1, d2] with group_size=4, gather_dim=1 -> [1, 4*d1, d2]

        [4, d1, d2] -> [4, 1, d1, d2] -> [1, 4, d1, d2] (contiguous!)

    Example: shape [4, 2, d2] with group_size=4, gather_dim=2 -> [1, 2, 4*d2]

        [4, 2, d2] -> [4, 1, 2, d2] -> [1, 2, 4, d2] (not contiguous!)

    Args:
        res: Tensor with gathered data in dim 0, shape [group_size, ...]
        group_size: Number of ranks in the group
        gather_dim: Dimension to gather along in the output

    Returns:
        Tensor with data rearranged to gather along gather_dim
    r   rA   N)dim)rG   shapemathprodviewr   catchunk)r  r  r  r  numel_betweenfinal_shaper)   r)   r*   _maybe_view_chunk_catk  s   '


r  c                   @   sZ   e Zd ZU dZeed< eed< eed< ee ed< ee ed< ee ed< ee ed< d	S )
_Framez1Frame information from memory profiler snapshots.filenameliner   
fx_node_opfx_node_namefx_node_targetfx_original_traceN)r   r   r  r  r   r   rP  r   r)   r)   r)   r*   r    s   
 r  c                   @   s>   e Zd ZU dZeed< eed< eed< eed< ee ed< dS )_BlockzMemory block information.r   requested_sizeaddressr   framesN)	r   r   r  r  rP  r   r   rG   r  r)   r)   r)   r*   r    s   
 r  c                   @   sN   e Zd ZU dZeed< eed< eed< eed< eed< eed< ee ed< d	S )
_SegmentzMemory segment information.r  
total_sizestreamsegment_typeallocated_sizeactive_sizeblocksN)	r   r   r  r  rP  r   r   rG   r  r)   r)   r)   r*   r    s   
 r  c                   @   sN   e Zd ZU dZeed< ee ed< ee	 ed< eed< eed< ee ed< dS )	_TraceEntryzMemory trace entry information.actionaddrr  r   r  device_freeN)
r   r   r  r  r   r   r   rP  rG   r  r)   r)   r)   r*   r    s   
 r  c                   @   s2   e Zd ZU dZee ed< eeee   ed< dS )	_SnapshotzMemory snapshot structure.segmentsdevice_tracesN)	r   r   r  r  rG   r  r   r   r  r)   r)   r)   r*   r    s   
 r  r  c                 C   s"  ddl m} ddlm} tt| d}d}| D ]s}|d}|d}|r+|s,q|t	j
|s6q||}|du r@q|di }	|d	i }
|d
d}|	|| }|du r^q|
|}|du rhq|d|d< |d|d< t|d|d< |d}|r||d< |d7 }q|S )aq  
    Augment a list of frames with FX debug information. For each frame corresponding
    to an FX-generated Python file, this function attaches additional FX node
    metadata (op, name, target, and original trace).

    Args:
        frames (list[_Frame]): List of frame dictionaries to augment

    Returns:
        int: The count of frames that were augmented.
    r   )FX_GRAPH_MODULE_FILE_PREFIX)_FX_METADATA_REGISTRYz.*\.py$r  r  N
lineno_mapnode_metadataprologue_startopr  r   r  targetr  stack_tracer  rA   )torch.fx.graph_moduler  torch.fx.tracebackr  recompileescaperU   searchospathbasenamer   )r  r  r  _FX_GENERATED_PATTERNcountframer  linenorg   r  r  r  node_idx	node_infooriginal_tracer)   r)   r*   _augment_frames  sB   





r  snapshotc                 C   s   t | tr#t| d}ttt|}W d   n1 sw   Y  n| }|dg D ]}|dg D ]}d|v r?t|d  q3q+|dg D ]}|D ]}t |t	r\d|v r\t|d  qKqG|S )a  
    Augment a memory snapshot with original source stack traces from FX metadata.

    IMPORTANT: This function reads from a global in-memory registry (_FX_METADATA_REGISTRY)
    that is populated during graph module compilation. It must be called in the same
    Python process where the FX graphs were compiled. It cannot be used to augment
    snapshots loaded from disk in a different process.

    Args:
        snapshot (str or _Snapshot): Either a memory snapshot dict or path to a snapshot pickle file

    Returns:
        _Snapshot: The augmented snapshot dictionary with fx_node_op, fx_node_name,
            fx_original_trace, and fx_node_info fields added to frames
    rbNr  r  r  r  )
r   r   openr   r  pickleloadrU   r  rT   )r   fsnapshot_dictsegmentblock
trace_listtrace_entryr)   r)   r*   %_augment_memory_snapshot_stack_traces"  s&   
r  )NF)Fri   )FF)gr   r  r  loggingr  r  r  r  r!  r$  rL   r  r   collections.abcr   typesr   typingr   r   r   r   r	   typing_extensionsr
   r   r   r   r+   r@   r   rW   r`   re   rh   rn   rr   rt   ru   rG   r   r   r   r   r   r   _rebuild_xla_tensorr   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r   r  r  r7  r;  rA  rE  rI  rP  rK  r\  rT  rX  r^  r_  rh  FutureWarningrl  rr  	lru_cacherv  r   r~  r  	getLoggerr   r  r  r  r  IMPORT_MAPPINGNAME_MAPPINGr  r  r  r  r  r  r  r  r)   r)   r)   r*   <module>   s<  


(1@		

8*3
%		(
/
	
	

B
@