
    1;jiY                     v    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	Z G d
 de          ZdS )z
Generic utilities
    )OrderedDict)fieldsis_dataclass)AnyTupleN   )is_torch_availableis_torch_versionreturnc                     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   s     Q/root/voice-cloning/.venv/lib/python3.11/site-packages/diffusers/utils/outputs.py	is_tensorr      sF      a&& 	4a$$$    c                        e Zd ZdZddZddZd Zd Zd Zd	 Z	d
e
de
fdZde
de
ddf fdZ fdZ fdZdee
df         fdZ x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                 &    t                      rddlt          dd          r6j        j                             j        j        j         fd           dS j        j                             j        j        j         f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                 P     di j         j                            | |          S N utils_pytree_dict_unflattenvaluescontextclsr   s     r   <lambda>z.BaseOutput.__init_subclass__.<locals>.<lambda>C   .    CC,g,g%+2E2U2UV\^e2f2f,g,g r   c                 P     di j         j                            | |          S r   r   r!   s     r   r%   z.BaseOutput.__init_subclass__.<locals>.<lambda>I   r&   r   )r	   torch.utils._pytreer
   r   r   _register_pytree_node_dict_flattenregister_pytree_node)r$   r   s   `@r   __init_subclass__zBaseOutput.__init_subclass__6   s      	&&&&U++ #99K'5ggggg     #88K'5ggggg    	 	r   c                     t                     }t          |          st           j        j         d          t           |d         j                  }t           fd|dd          D                       }|r6t          |t                    r!|
                                D ]
\  }}| |<   d S |D ]#}t           |j                  }|
| |j        <   $d S )Nz has no fields.r   c              3   F   K   | ]}t          |j                  d u V  d S Ngetattrname.0fieldselfs     r   	<genexpr>z+BaseOutput.__post_init__.<locals>.<genexpr>T   s4      #d#d%GD%*$=$=$E#d#d#d#d#d#dr   r   )r   len
ValueError	__class____name__r1   r2   allr   dictitems)r6   class_fieldsfirst_fieldother_fields_are_nonekeyvaluer5   vs   `       r   __post_init__zBaseOutput.__post_init__L   s   d|| <   	J 7HHHIIIdLO$899 ##d#d#d#dS_`a`b`bSc#d#d#d d d  	)ZT%B%B 	))//11 " "
U!S		" " & ) )D%*--='(D$) )r   c                 <    t          d| j        j         d          )Nz$You cannot use ``__delitem__`` on a 
 instance.	Exceptionr:   r;   r6   argskwargss      r   __delitem__zBaseOutput.__delitem___   s!    bt~?Vbbbcccr   c                 <    t          d| j        j         d          )Nz#You cannot use ``setdefault`` on a rG   rH   rJ   s      r   
setdefaultzBaseOutput.setdefaultb   s!    adn>Uaaabbbr   c                 <    t          d| j        j         d          )NzYou cannot use ``pop`` on a rG   rH   rJ   s      r   popzBaseOutput.pope   s!    Zt~7NZZZ[[[r   c                 <    t          d| j        j         d          )NzYou cannot use ``update`` on a rG   rH   rJ   s      r   updatezBaseOutput.updateh   s!    ]$.:Q]]]^^^r   kc                     t          |t                    r)t          |                                           }||         S |                                 |         S r/   )r   strr=   r>   to_tuple)r6   rT   
inner_dicts      r   __getitem__zBaseOutput.__getitem__k   sF    a 	&djjll++Ja= ==??1%%r   r2   rC   c                     ||                                  v r$|"t                                          ||           t                                          ||           d S r/   )keyssuper__setitem____setattr__)r6   r2   rC   r:   s      r   r^   zBaseOutput.__setattr__r   sU    499;;5#4GGe,,,D%(((((r   c                     t                                          ||           t                                          ||           d S r/   )r\   r]   r^   )r6   rB   rC   r:   s      r   r]   zBaseOutput.__setitem__x   s=    C'''C'''''r   c                     t                     s t                                                      S t                                                      ^}}}t           fdt	                     D                       }||g|R S )Nc              3   B   K   | ]}t          |j                  V  d S r/   r0   r3   s     r   r7   z(BaseOutput.__reduce__.<locals>.<genexpr>   s/      II5WT5:..IIIIIIr   )r   r\   
__reduce__tupler   )r6   callable_args	remainingrK   r:   s   `    r   rb   zBaseOutput.__reduce__~   s    D!! 	(77%%'''&+gg&8&8&:&:#%)IIIIF4LLIIIII)	)))r   .c                 ^     t           fd                                 D                       S )za
        Convert self to a tuple containing all the attributes/keys that are not `None`.
        c              3   (   K   | ]}|         V  d S r/   r   )r4   rT   r6   s     r   r7   z&BaseOutput.to_tuple.<locals>.<genexpr>   s'      22T!W222222r   )rc   r[   )r6   s   `r   rW   zBaseOutput.to_tuple   s0     2222diikk222222r   )r   N)r;   
__module____qualname____doc__r,   rE   rM   rO   rQ   rS   r   rY   r^   r]   rb   r   rW   __classcell__)r:   s   @r   r   r   (   sC           ,) ) ) )&d d dc c c\ \ \_ _ _&S &S & & & &) )C )D ) ) ) ) ) )( ( ( ( (* * * * *3%S/ 3 3 3 3 3 3 3 3r   r   )rk   collectionsr   dataclassesr   r   typingr   r   numpyr   import_utilsr	   r
   boolr   r   r   r   r   <module>rs      s     $ # # # # # , , , , , , , ,             > > > > > > > >
%D 
% 
% 
% 
%a3 a3 a3 a3 a3 a3 a3 a3 a3 a3r   