o
    piY                     @   sj   d Z ddlmZ ddlmZmZ ddlmZ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)AnyTupleN   )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   U/home/ubuntu/SoloSpeech/.venv/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.

    <Tip warning={true}>

    You can't unpack a [`BaseOutput`] directly. Use the [`~utils.BaseOutput.to_tuple`] method to convert it to a tuple
    first.

    </Tip>
    r
   Nc                    sh   t  r2ddltddrjj jjj fdd dS jj jjj fd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>C       z.BaseOutput.__init_subclass__.<locals>.<lambda>c                    r   r   r   r   r   r   r   r!   I   r"   )r   torch.utils._pytreer	   r   r   _register_pytree_node_dict_flattenregister_pytree_node)r    r   r   r   __init_subclass__6   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>T   s    z+BaseOutput.__post_init__.<locals>.<genexpr>r   )r   len
ValueError	__class____name__r*   r+   allr   dictitems)r0   class_fieldsfirst_fieldother_fields_are_nonekeyvaluer.   vr   r/   r   __post_init__L   s   

zBaseOutput.__post_init__c                 O      t d| jj d)Nz$You cannot use ``__delitem__`` on a 
 instance.	Exceptionr4   r5   r0   argskwargsr   r   r   __delitem___      zBaseOutput.__delitem__c                 O   r@   )Nz#You cannot use ``setdefault`` on a rA   rB   rD   r   r   r   
setdefaultb   rH   zBaseOutput.setdefaultc                 O   r@   )NzYou cannot use ``pop`` on a rA   rB   rD   r   r   r   pope   rH   zBaseOutput.popc                 O   r@   )NzYou cannot use ``update`` on a rA   rB   rD   r   r   r   updateh   rH   zBaseOutput.updatekc                 C   s*   t |trt|  }|| S |  | S r(   )r   strr7   r8   to_tuple)r0   rL   
inner_dictr   r   r   __getitem__k   s   
zBaseOutput.__getitem__r+   r=   c                    s4   ||   v r|d urt || t || d S r(   )keyssuper__setitem____setattr__)r0   r+   r=   r4   r   r   rT   r   s   zBaseOutput.__setattr__c                    s    t  || t  || d S r(   )rR   rS   rT   )r0   r<   r=   rU   r   r   rS   x   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,   r/   r   r   r1      s    z(BaseOutput.__reduce__.<locals>.<genexpr>)r   rR   
__reduce__tupler   )r0   callable_args	remainingrE   rU   r/   r   rV   ~   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   )r-   rL   r/   r   r   r1      s    z&BaseOutput.to_tuple.<locals>.<genexpr>)rW   rQ   r/   r   r/   r   rN      s   zBaseOutput.to_tuple)r
   N)r5   
__module____qualname____doc__r'   r?   rG   rI   rJ   rK   r   rP   rT   rS   rV   r   rN   __classcell__r   r   rU   r   r   (   s    

r   )r]   collectionsr   dataclassesr   r   typingr   r   numpyr   import_utilsr   r	   boolr   r   r   r   r   r   <module>   s   