o
    Gi                     @   sf   d Z ddlmZ ddlmZmZ ddlmZ ddlZ	ddl
mZmZ defd	d
ZG dd deZdS )z
Generic utilities
    )OrderedDict)fieldsis_dataclass)AnyN   )is_torch_availableis_torch_versionreturnc                 C   s*   t  rddl}t| |jrdS t| tjS )z;
    Tests if `x` is a `torch.Tensor` or `np.ndarray`.
    r   NT)r   torch
isinstanceTensornpndarray)xr
    r   K/home/ubuntu/.local/lib/python3.10/site-packages/diffusers/utils/outputs.py	is_tensor   s
   r   c                       s   e Zd ZdZdddZdddZdd	 Zd
d Zdd Zdd Z	de
de
fddZde
de
ddf fddZ fddZ fddZdee
df fddZ  ZS )
BaseOutputa  
    Base class for all model outputs as dataclass. Has a `__getitem__` that allows indexing by integer or slice (like a
    tuple) or strings (like a dictionary) that will ignore the `None` attributes. Otherwise behaves like a regular
    Python dictionary.

    > [!WARNING] > You can't unpack a [`BaseOutput`] directly. Use the [`~utils.BaseOutput.to_tuple`] method to convert
    it to a tuple > first.
    r	   Nc                    sz   t  r;ddltddrjj jjj fdd dS jjj jjj fdd j d j	 d	 dS dS )
zRegister subclasses as pytree nodes.

        This is necessary to synchronize gradients when using `torch.nn.parallel.DistributedDataParallel` with
        `static_graph=True` with modules that output `ModelOutput` subclasses.
        r   N<z2.2c                        di j j| |S Nr   utils_pytree_dict_unflattenvaluescontextclsr
   r   r   <lambda>?       z.BaseOutput.__init_subclass__.<locals>.<lambda>c                    r   r   r   r   r   r   r   r    E   r!   .)serialized_type_name)
r   torch.utils._pytreer   r   r   _register_pytree_node_dict_flattenregister_pytree_node
__module____name__)r   r   r   r   __init_subclass__2   s   

zBaseOutput.__init_subclass__c                    s   t  }t|st jj dt |d j}t fdd|dd  D }|r>t|t	r>|
 D ]\}}| |< q3d S |D ]}t |j}|d urQ| |j< q@d S )Nz has no fields.r   c                 3   s     | ]}t  |jd u V  qd S Ngetattrname.0fieldselfr   r   	<genexpr>Q   s    z+BaseOutput.__post_init__.<locals>.<genexpr>r   )r   len
ValueError	__class__r)   r-   r.   allr   dictitems)r3   class_fieldsfirst_fieldother_fields_are_nonekeyvaluer1   vr   r2   r   __post_init__I   s   

zBaseOutput.__post_init__c                 O      t d| jj d)Nz$You cannot use ``__delitem__`` on a 
 instance.	Exceptionr7   r)   r3   argskwargsr   r   r   __delitem__\      zBaseOutput.__delitem__c                 O   rB   )Nz#You cannot use ``setdefault`` on a rC   rD   rF   r   r   r   
setdefault_   rJ   zBaseOutput.setdefaultc                 O   rB   )NzYou cannot use ``pop`` on a rC   rD   rF   r   r   r   popb   rJ   zBaseOutput.popc                 O   rB   )NzYou cannot use ``update`` on a rC   rD   rF   r   r   r   updatee   rJ   zBaseOutput.updatekc                 C   s*   t |trt|  }|| S |  | S r+   )r   strr9   r:   to_tuple)r3   rN   
inner_dictr   r   r   __getitem__h   s   
zBaseOutput.__getitem__r.   r?   c                    s4   ||   v r|d urt || t || d S r+   )keyssuper__setitem____setattr__)r3   r.   r?   r7   r   r   rV   o   s   zBaseOutput.__setattr__c                    s    t  || t  || d S r+   )rT   rU   rV   )r3   r>   r?   rW   r   r   rU   u   s   zBaseOutput.__setitem__c                    sJ   t  s	t  S t  ^}}}t fddt D }||g|R S )Nc                 3   s    | ]	}t  |jV  qd S r+   r,   r/   r2   r   r   r4      s    z(BaseOutput.__reduce__.<locals>.<genexpr>)r   rT   
__reduce__tupler   )r3   callable_args	remainingrG   rW   r2   r   rX   {   s
   
zBaseOutput.__reduce__.c                    s   t  fdd  D S )za
        Convert self to a tuple containing all the attributes/keys that are not `None`.
        c                 3   s    | ]} | V  qd S r+   r   )r0   rN   r2   r   r   r4      s    z&BaseOutput.to_tuple.<locals>.<genexpr>)rY   rS   r2   r   r2   r   rP      s   zBaseOutput.to_tuple)r	   N)r)   r(   __qualname____doc__r*   rA   rI   rK   rL   rM   r   rR   rV   rU   rX   rY   rP   __classcell__r   r   rW   r   r   (   s    
	
r   )r^   collectionsr   dataclassesr   r   typingr   numpyr   import_utilsr   r   boolr   r   r   r   r   r   <module>   s   