a
    d                  	   @   s2  d dl Z d dlZd dlmZ d dlmZmZmZmZm	Z	m
Z
mZm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 dd	 Zd
d Zeeejee	ee
eef edddZe ejejedddZe ejedddZejejedddZdee eddddZ!ejej"j#ddddZ$dS )     N)OrderedDict)AnyCallablecastDictListSetTupleUnion)
_BatchNorm)_is_namedtuple)PackedSequence)no_dispatchc                 C   s   t dd |  D S )Nc                 s   s   | ]}t |tV  qd S N)
isinstancer   ).0mod r   f/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torch/distributed/fsdp/_utils.py	<genexpr>       z&_contains_batchnorm.<locals>.<genexpr>)anymodules)moduler   r   r   _contains_batchnorm   s    r   c                 C   s&   |   D ]}t|trdd i|_qd S )NZmixed_precision)r   r   r   Z_wrap_overrides)r   r   r   r   r   #_override_batchnorm_mixed_precision   s    
r   )fn	containerreturnc                    s2   t tjttttttf t	d fdd  |S )zFRecursively apply to all tensor in different kinds of container types.)xr   c                    s  t | r| S t| drVt| }t|D ] }|j}t|| t|| q0|S t	| t
r|  }|  D ]\}} |||< qp|S t	| tr | j | S t	| tr fdd|  D S t| r fdd| D }t| | S t	| tttfrt|  fdd| D S | S d S )NZ__dataclass_fields__c                    s   i | ]\}}| |qS r   r   )r   keyvalueapplyr   r   
<dictcomp>3   r   z4_apply_to_tensors.<locals>.apply.<locals>.<dictcomp>c                 3   s   | ]} |V  qd S r   r   r   elr"   r   r   r   5   r   z3_apply_to_tensors.<locals>.apply.<locals>.<genexpr>c                 3   s   | ]} |V  qd S r   r   r%   r"   r   r   r   8   r   )torchZ	is_tensorhasattrdataclassesreplacefieldsnamesetattrgetattrr   r   	__class__itemsr   datadictr   typelisttupleset)r   dcfr,   Zodr    r!   resr#   r   r   r   r#      s0    






z _apply_to_tensors.<locals>.apply)
r
   r'   Tensorr   r   r	   r   r   r   r   )r   r   r   r:   r   _apply_to_tensors   s    r<   )tensorsizer   c                 C   sP   |    | k}|sJ|    }t|dkd|  |   |  | S )z
    Allocate storage for ``tensor`` with the given size.

    Returns:
        bool: ``True`` if this method allocated storage and ``False`` if the
        storage was already allocated.
    r   z8Tensor storage should have been resized to be 0 but got )_typed_storage_sizeZnumelp_assert_resize_)r=   r>   Zalready_allocatedZtensor_storage_sizer   r   r   _alloc_storage?   s    	rC   )r=   r   c                 C   sZ   |    dk}|sTt|  dkd|   d|     d| j  |   d | S )z
    Frees the underlying storage of ``tensor``.

    Returns:
        bool: ``True`` if the method freed the storage and ``False`` if the
        storage was already freed.
    r   zVFreeing a tensor's storage is unsafe when it is not the sole occupant
storage offset: z
storage size: z
tensor shape: )r?   r@   rA   Zstorage_offsetshaperB   )r=   Zalready_freedr   r   r   _free_storageS   s    	

rE   )r   yr   c                 C   s   |    |   kS )z2Returns if ``x`` and ``y`` share the same storage.)r?   Z	_data_ptr)r   rF   r   r   r   _same_storagei   s    rG   T)condsraise_assertion_errorr   c                 C   s$   | s t | t  |r t|dS )zThis is used as an alternate to ``assert`` when in the backward context
    to print the error message ``s`` since otherwise, it is swallowed.N)print	tracebackprint_stackAssertionError)rH   rI   rJ   r   r   r   rA   o   s
    rA   )r=   streamr   c                 C   s>   t  $ | ttjj| W d    n1 s00    Y  d S r   )r   Zrecord_streamr   r'   Z_CStream)r=   rO   r   r   r   _no_dispatch_record_streamy   s    rQ   )T)%r)   rL   collectionsr   typingr   r   r   r   r   r   r	   r
   r'   Ztorch.nn.modules.batchnormr   Z torch.nn.parallel.scatter_gatherr   Ztorch.nn.utils.rnnr   Ztorch.utils._mode_utilsr   r   r   r;   r<   Zno_gradSizeboolrC   rE   rG   strrA   ZcudarP   rQ   r   r   r   r   <module>   s*   (&
