
    bi                     n    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y)z
Generic utilities
    )OrderedDict)fieldsis_dataclass)AnyTupleN   )is_torch_availableis_torch_versionreturnc                     t               rddl}t        | |j                        ry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     R/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/diffusers/utils/outputs.py	is_tensorr      s/     a&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      rJj                  j                  j                   j                  j                  j                   fd       yj                  j                  j                   j                  j                  j                   fd j                   d j                          yy)	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                  j                  | |      S N utils_pytree_dict_unflattenvaluescontextclsr   s     r   <lambda>z.BaseOutput.__init_subclass__.<locals>.<lambda>C   (    C,g%++2E2E2U2UV\^e2f,g r   c                 \     di j                   j                  j                  | |      S r   r   r!   s     r   r%   z.BaseOutput.__init_subclass__.<locals>.<lambda>I   r&   r   .)serialized_type_name)
r	   torch.utils._pytreer
   r   r   _register_pytree_node_dict_flattenregister_pytree_node
__module____name__)r$   r   s   `@r   __init_subclass__zBaseOutput.__init_subclass__6   s     &U+##99KK''55g ##88KK''55g,/NN+;1S\\N)K	 9   r   c                     t               }t        |      s"t         j                  j                   d      t         |d   j                        }t         fd|dd  D              }|r.t        |t              r|j                         D ]
  \  }}| |<    y |D ]*  }t         |j                        }|| |j                  <   , y )Nz has no fields.r   c              3   N   K   | ]  }t        |j                        d u   y wNgetattrname.0fieldselfs     r   	<genexpr>z+BaseOutput.__post_init__.<locals>.<genexpr>U   s!     #d%GD%**$=$E#ds   "%r   )r   len
ValueError	__class__r/   r5   r6   allr   dictitems)r:   class_fieldsfirst_fieldother_fields_are_nonekeyvaluer9   vs   `       r   __post_init__zBaseOutput.__post_init__M   s    d| <  7 78HIIdLO$8$89 ##dS_`a`bSc#d d ZT%B)//1 "
U!S	" & )D%**-='(D$)r   c                 H    t        d| j                  j                   d      )Nz$You cannot use ``__delitem__`` on a 
 instance.	Exceptionr>   r/   r:   argskwargss      r   __delitem__zBaseOutput.__delitem__`   s#    >t~~?V?V>WWabccr   c                 H    t        d| j                  j                   d      )Nz#You cannot use ``setdefault`` on a rJ   rK   rM   s      r   
setdefaultzBaseOutput.setdefaultc   s#    =dnn>U>U=VV`abbr   c                 H    t        d| j                  j                   d      )NzYou cannot use ``pop`` on a rJ   rK   rM   s      r   popzBaseOutput.popf   s"    6t~~7N7N6OzZ[[r   c                 H    t        d| j                  j                   d      )NzYou cannot use ``update`` on a rJ   rK   rM   s      r   updatezBaseOutput.updatei   s#    9$..:Q:Q9RR\]^^r   kc                     t        |t              rt        | j                               }||   S | j	                         |   S r3   )r   strr@   rA   to_tuple)r:   rW   
inner_dicts      r   __getitem__zBaseOutput.__getitem__l   s7    adjjl+Ja= ==?1%%r   r6   rF   c                 n    || j                         v r|t        | 	  ||       t        |   ||       y r3   )keyssuper__setitem____setattr__)r:   r6   rF   r>   s      r   ra   zBaseOutput.__setattr__s   s4    499;5#4Ge,D%(r   c                 F    t         |   ||       t         | 	  ||       y r3   )r_   r`   ra   )r:   rE   rF   r>   s      r   r`   zBaseOutput.__setitem__y   s!    C'C'r   c                      t               st         	         S t         	         ^}}}t         fdt	               D              }||g|S )Nc              3   J   K   | ]  }t        |j                          y wr3   r4   r7   s     r   r;   z(BaseOutput.__reduce__.<locals>.<genexpr>   s     I5WT5::.Is    #)r   r_   
__reduce__tupler   )r:   callable_args	remainingrN   r>   s   `    r   re   zBaseOutput.__reduce__   sQ    D!7%''&+g&8&:#%)IF4LII)	))r   .c                 H     t         fd j                         D              S )za
        Convert self to a tuple containing all the attributes/keys that are not `None`.
        c              3   (   K   | ]	  }|     y wr3   r   )r8   rW   r:   s     r   r;   z&BaseOutput.to_tuple.<locals>.<genexpr>   s     2T!W2s   )rf   r^   )r:   s   `r   rZ   zBaseOutput.to_tuple   s     2diik222r   )r   N)r/   r.   __qualname____doc__r0   rH   rP   rR   rT   rV   r   r\   ra   r`   re   r   rZ   __classcell__)r>   s   @r   r   r   (   su    .)&dc\_&S &S &) )C )D )(*3%S/ 3r   r   )rm   collectionsr   dataclassesr   r   typingr   r   numpyr   import_utilsr	   r
   boolr   r   r   r   r   <module>ru      s5    $ ,   >
%D 
%b3 b3r   