o
    ªôjY  ã                   @   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   új/var/www/html/academy/tools/voices/clone/cosy-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 )zìRegister 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   Z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)r/   Ú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.©Ú	Exceptionr3   r4   ©r/   ÚargsÚkwargsr   r   r   Ú__delitem___   ó   zBaseOutput.__delitem__c                 O   r?   )Nz#You cannot use ``setdefault`` on a r@   rA   rC   r   r   r   Ú
setdefaultb   rG   zBaseOutput.setdefaultc                 O   r?   )NzYou cannot use ``pop`` on a r@   rA   rC   r   r   r   Úpope   rG   zBaseOutput.popc                 O   r?   )NzYou cannot use ``update`` on a r@   rA   rC   r   r   r   Úupdateh   rG   zBaseOutput.updateÚkc                 C   s*   t |tƒrt|  ¡ ƒ}|| S |  ¡ | S r'   )r   Ústrr6   r7   Úto_tuple)r/   rK   Ú
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__)r/   r*   r<   ©r3   r   r   rS   r   s   zBaseOutput.__setattr__c                    s    t ƒ  ||¡ t ƒ  ||¡ d S r'   )rQ   rR   rS   )r/   r;   r<   rT   r   r   rR   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   r0   ‚   s   € z(BaseOutput.__reduce__.<locals>.<genexpr>)r   rQ   Ú
__reduce__Útupler   )r/   ÚcallableÚ_argsÚ	remainingrD   rT   r.   r   rU   ~   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,   rK   r.   r   r   r0   ‰   s   € z&BaseOutput.to_tuple.<locals>.<genexpr>)rV   rP   r.   r   r.   r   rM   …   s   zBaseOutput.to_tuple)r
   N)r4   Ú
__module__Ú__qualname__Ú__doc__r&   r>   rF   rH   rI   rJ   r   rO   rS   rR   rU   r   rM   Ú__classcell__r   r   rT   r   r   (   s    

r   )r\   Úcollectionsr   Údataclassesr   r   Útypingr   r   Únumpyr   Úimport_utilsr   r	   Úboolr   r   r   r   r   r   Ú<module>   s   