
    fThH~              	       h   S r SSKrSSKrSSKrSSKrSSKrSSKJrJr  SSK	J
r
Jr  SSKJrJr  SSKJrJr  SSKJr  SSKJrJr  SS	KJrJrJrJr  SSKrSS
KJr  SSK J!r!J"r"J#r#J$r$J%r%J&r&  \%" 5       (       a  SSK'r' " S S\(5      r)S r*S r+S r,S r-S r.S r/S r0S r1S r2S r3S r4S r5S r6S r7S r8S r9S r:S  r;S! r<S" r=S# r>S$ r? " S% S&\5      r@\%" 5       (       a  SSKAJBs  JCrD  S'\@S(\E\F\   S)4   4S* jrG S]S+\
\   S,S)S(\@4S- jjrH\R                  " \!" 5       5      \R                  " S.5      :  a2  \DR                  " \@\G\" \H\@S/9\@R                   S0\@R                   3S19  O\DR                  " \@\G\" \H\@S/95         " S2 S3\N\5      rO " S4 S5\O5      rP " S6 S7\O5      rQ " S8 S95      rRS: rSS; rTS^S<\S=\NS>\N4S? jjrU\S_SA\V4SB jj5       rWS]SC jrXSD rYS]SE jrZSF r[SG r\SH r]SI r^SJ r_SK r`SL raS]SM\\F   4SN jjrb " SO SP\S@SQ9rcSR\d\N\4   S(\V4SS jreST\NS(\V4SU jrfSVSWSX\NSY\4SZ jrgSVSWSX\N4S[ jrhS\ rig)`z
Generic utilities
    N)OrderedDictUserDict)IterableMutableMapping)	ExitStackcontextmanager)fieldsis_dataclass)Enum)partialwraps)AnyContextManagerOptional	TypedDict)version   )get_torch_versionis_flax_availableis_mlx_availableis_tf_availableis_torch_availableis_torch_fx_proxyc                   "    \ rS rSrSrSS jrSrg)cached_property1   z
Descriptor that mimics @property but caches output in member variable.

From tensorflow_datasets

Built-in in functools from Python 3.8.
Nc                     Uc  U $ U R                   c  [        S5      eSU R                   R                  -   n[        XS 5      nUc  U R                  U5      n[	        XU5        U$ )Nzunreadable attribute	__cached_)fgetAttributeError__name__getattrsetattr)selfobjobjtypeattrcacheds        R/var/www/auris/envauris/lib/python3.13/site-packages/transformers/utils/generic.py__get__cached_property.__get__:   sc    ;K99 !788TYY///D)>YYs^FCv&     N)r!   
__module____qualname____firstlineno____doc__r*   __static_attributes__r-   r,   r)   r   r   1   s    r,   r   c                 \    U R                  5       n U S;   a  gU S;   a  g[        SU < 35      e)zConvert a string representation of truth to true (1) or false (0).

True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values are 'n', 'no', 'f', 'false', 'off', and '0'.
Raises ValueError if 'val' is anything else.
>   1tyonyestruer   >   0fnnoofffalser   zinvalid truth value )lower
ValueError)vals    r)   	strtoboolrD   I   s:     ))+C
22
33
+C73
44r,   c                    [        [        U 5      5      nUR                  S5      (       a  gUR                  S5      (       a  gUR                  S5      (       a  gUR                  S5      (       a  gUR                  S	5      (       a  g
g)z
Tries to guess the framework of an object `x` from its repr (brittle but will help in `is_tensor` to try the
frameworks in a smart order, without the need to import the frameworks).
z<class 'torch.ptz<class 'tensorflow.tfz<class 'jaxjaxz<class 'numpy.npz<class 'mlx.mlxN)strtype
startswith)xrepresentations     r)   infer_framework_from_reprrP   W   s}    
 a\N  !122		"	"#8	9	9		"	"=	1	1		"	"#3	4	4		"	">	2	2 
3r,   c                 "   [         [        [        [        [        S.n[        U 5      nUc  / OU/nUS:w  a  UR                  S5        UR                  U Vs/ s H  oDUS4;  d  M  UPM     sn5        U Vs0 s H  oDX   _M	     sn$ s  snf s  snf )z
Returns an (ordered since we are in Python 3.7+) dictionary framework to test function, which places the framework
we can guess from the repr first, then Numpy, then the others.
)rF   rG   rH   rI   rJ   rI   )is_torch_tensoris_tf_tensoris_jax_tensoris_numpy_arrayis_mlx_arrayrP   appendextend)rN   framework_to_testpreferred_framework
frameworksr<   s        r)   _get_frameworks_and_test_funcr\   i   s      4A6*29L8MJd"$"3\"3QATVZ@[7[q"3\]-78Z ##Z88 ]8s   B%B6Bc                     [        U 5      nUR                  5        H  nU" U 5      (       d  M    g   [        U 5      (       a  g[        5       (       a  SSKJn  [        X5      (       a  gg)z
Tests if `x` is a `torch.Tensor`, `tf.Tensor`, `jaxlib.xla_extension.DeviceArray`, `np.ndarray` or `mlx.array`
in the order defined by `infer_framework_from_repr`
Tr   )TracerF)r\   valuesr   r   jax.corer^   
isinstance)rN   framework_to_test_func	test_funcr^   s       r)   	is_tensorrd   ~   s[     ;1=+224	Q<< 5
 #a  r,   c                 6    [        U [        R                  5      $ r.   )ra   rI   ndarrayrN   s    r)   	_is_numpyrh      s    a$$r,   c                     [        U 5      $ )z'
Tests if `x` is a numpy array or not.
)rh   rg   s    r)   rU   rU      s     Q<r,   c                 4    SS K n[        XR                  5      $ Nr   )torchra   TensorrN   rl   s     r)   	_is_torchro          a&&r,   c                 :    [        5       (       d  S$ [        U 5      $ )zU
Tests if `x` is a torch tensor or not. Safe to call even if torch is not installed.
F)r   ro   rg   s    r)   rR   rR      s     +,,5>)A,>r,   c                 4    SS K n[        XR                  5      $ rk   )rl   ra   devicern   s     r)   _is_torch_devicert      rp   r,   c                 :    [        5       (       d  S$ [        U 5      $ )zU
Tests if `x` is a torch device or not. Safe to call even if torch is not installed.
F)r   rt   rg   s    r)   is_torch_devicerv      s     +,,5E2B12EEr,   c                     SS K n[        U [        5      (       a  [        X5      (       a  [	        X5      n Og[        XR
                  5      $ )Nr   F)rl   ra   rK   hasattrr"   dtypern   s     r)   _is_torch_dtyperz      s9    !S5!Aa%%r,   c                 :    [        5       (       d  S$ [        U 5      $ )zT
Tests if `x` is a torch dtype or not. Safe to call even if torch is not installed.
F)r   rz   rg   s    r)   is_torch_dtyper|      s     +,,5D/!2DDr,   c                 4    SS K n[        XR                  5      $ rk   )
tensorflowra   rm   rN   rG   s     r)   _is_tensorflowr      s    a##r,   c                 :    [        5       (       d  S$ [        U 5      $ )z_
Tests if `x` is a tensorflow tensor or not. Safe to call even if tensorflow is not installed.
F)r   r   rg   s    r)   rS   rS      s     ())5@~a/@@r,   c                 x    SS K n[        US5      (       a  UR                  U 5      $ [        XR                  5      $ )Nr   is_symbolic_tensor)r~   rx   r   ra   rm   r   s     r)   _is_tf_symbolic_tensorr      s5     r'(($$Q''a##r,   c                 :    [        5       (       d  S$ [        U 5      $ )zx
Tests if `x` is a tensorflow symbolic tensor or not (ie. not eager). Safe to call even if tensorflow is not
installed.
F)r   r   rg   s    r)   is_tf_symbolic_tensorr      s    
 ())5H/Ea/HHr,   c                 8    SS K Jn  [        XR                  5      $ rk   )	jax.numpynumpyra   rf   )rN   jnps     r)   _is_jaxr      s    a%%r,   c                 :    [        5       (       d  S$ [        U 5      $ )zQ
Tests if `x` is a Jax tensor or not. Safe to call even if jax is not installed.
F)r   r   rg   s    r)   rT   rT      s     *++5;;r,   c                 8    SS K Jn  [        XR                  5      $ rk   )mlx.corecorera   array)rN   mxs     r)   _is_mlxr      s    a""r,   c                 :    [        5       (       d  S$ [        U 5      $ )zR
Tests if `x` is a mlx array or not. Safe to call even when mlx is not installed.
F)r   r   rg   s    r)   rV   rV      s     )**5:
:r,   c                    [        U [        [        45      (       a  U $ [        U [        [        45      (       a/  U R                  5        VVs0 s H  u  pU[        U5      _M     snn$ [        U [        [        45      (       a   [        R                  " U 5      n[        R                  " UR                  [        R                  5      (       d4  [        R                  " UR                  [        R                  5      (       a  UR                  5       $  U  Vs/ s H  n[        U5      PM     sn$ S S S S S.n[#        U 5      nUR                  5        H  u  pxU" U 5      (       d  M  XW   " U 5      s  $    [        U [        R$                  5      (       a  U R                  5       $ U $ s  snnf ! [          a     Nf = fs  snf )z[
Convert a TensorFlow tensor, PyTorch tensor, Numpy array or python list to a python list.
c                 "    U R                  5       $ r.   tolistr%   s    r)   <lambda>to_py_obj.<locals>.<lambda>  
    #**,r,   c                 >    U R                  5       R                  5       $ r.   )r   r   r   s    r)   r   r     s    #))+,,.r,   c                 J    [         R                  " U 5      R                  5       $ r.   )rI   asarrayr   r   s    r)   r   r     s    2::c?113r,   c                 "    U R                  5       $ r.   r   r   s    r)   r   r     r   r,   rF   rG   rH   rI   )ra   intfloatdictr   items	to_py_objlisttuplerI   r   
issubdtypery   integerfloatingr   	Exceptionr\   number)	r%   kvarroframework_to_py_objrb   	frameworkrc   s	            r)   r   r     sd    #U|$$
	C$)	*	*,/IIK8KDA9Q<K88	C$	'	'	((3-C}}SYY

33r}}SYYPRP[P[7\7\zz|# 8] '**c	!c** '.3&	 ;3? 6 < < >	S>>&1#66 !?
 #ryy!!zz|
7 9  		*s   F(BF. F>.
F;:F;c                    S S S S S.n[        U [        [        45      (       a/  U R                  5        VVs0 s H  u  p#U[	        U5      _M     snn$ [        U [
        [        45      (       a  [        R                  " U 5      $ [        U 5      nUR                  5        H  u  pVU" U 5      (       d  M  X   " U 5      s  $    U $ s  snnf )z[
Convert a TensorFlow tensor, PyTorch tensor, Numpy array or python list to a Numpy array.
c                 Z    U R                  5       R                  5       R                  5       $ r.   )detachcpur   r   s    r)   r   to_numpy.<locals>.<lambda>0  s    #**,**,224r,   c                 "    U R                  5       $ r.   )r   r   s    r)   r   r   1  s
    #))+r,   c                 .    [         R                  " U 5      $ r.   )rI   r   r   s    r)   r   r   2  s    2::c?r,   c                     U $ r.   r-   r   s    r)   r   r   3  s    #r,   r   )
ra   r   r   r   to_numpyr   r   rI   r   r\   )r%   framework_to_numpyr   r   rb   r   rc   s          r)   r   r   *  s     5%*	 #h'((+.99;7;418A;;77	C$	'	'xx} ;3? 6 < < >	S>>%055 !? J 8s   Cc                      ^  \ rS rSrSrSS jrU 4S jrS rS rS r	S	 r
S
 rS rU 4S jrU 4S jrU 4S jrS\\   4S jrSrU =r$ )ModelOutputiD  a  
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 `ModelOutput` directly. Use the [`~utils.ModelOutput.to_tuple`] method to convert it to a tuple
before.

</Tip>
returnc           	      B   [        5       (       a  [        R                  " [        5       5      [        R                  " S5      :  a9  SSKJn  U" U [        [        [        U S9U R                   SU R                   3S9  gSSKJn  U" U [        [        [        U S95        gg)	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.
2.2r   )register_pytree_nodeoutput_type.serialized_type_name)_register_pytree_nodeN)r   r   parser   torch.utils._pytreer   _model_output_flattenr   _model_output_unflattenr/   r!   r   )clsr   r   s      r)   __init_subclass__ModelOutput.__init_subclass__R  s     }}.01W]]55IID$)3E,/NN+;1S\\N)K	 F%)3E  r,   c                    > [         TU ]  " U0 UD6  U R                  [        :g  nU(       a@  [	        U 5      (       d/  [        U R                   SU R                  R                   S35      eg g )Nr   z` is not a dataclass. This is a subclass of ModelOutput and so must use the @dataclass decorator.)super__init__	__class__r   r
   	TypeErrorr/   r!   )r$   argskwargsis_modeloutput_subclassr   s       r)   r   ModelOutput.__init__k  so    $)&) #'..K"?"<+=+=??#1T^^%<%<$= >_ _  ,>"r,   c                   ^  [        T 5      n[        U5      (       d"  [        T R                  R                   S35      e[        S USS  5       5      (       d"  [        T R                  R                   S35      e[        T US   R                  5      n[        U 4S jUSS  5       5      nU(       Ga  [        U5      (       d  [        U[        5      (       a  UR                  5       nSnO [        U5      nSnU(       a  [        W5       H  u  pg[        U[        [         45      (       a'  [        U5      S
:X  a  [        US   ["        5      (       d*  US:X  a  UT US   R                  '   O[        SU S35      e  g[%        T US   US   5        US   c  M  US   T US   '   M     gUb  UT US   R                  '   ggU H-  n[        T UR                  5      n	U	c  M  U	T UR                  '   M/     g! [         a    S	n GNf = f)zUCheck the ModelOutput dataclass.

Only occurs if @dataclass decorator has been used.
z has no fields.c              3   <   #    U  H  oR                   S L v   M     g 7fr.   )default).0fields     r)   	<genexpr>,ModelOutput.__post_init__.<locals>.<genexpr>  s     G6FU==D(6Fs   r   Nz. should not have more than one required field.r   c              3   T   >#    U  H  n[        TUR                  5      S L v   M     g 7fr.   r"   namer   r   r$   s     r)   r   r     s#     #dSc%GD%**$=$EScs   %(TF   zCannot set key/value for z&. It needs to be a tuple (key, value).)r	   lenrB   r   r!   allr"   r   rd   ra   r   r   iterr   	enumerater   r   rK   r#   )
r$   class_fieldsfirst_fieldother_fields_are_noneiteratorfirst_field_iteratoridxelementr   r   s
   `         r)   __post_init__ModelOutput.__post_init__z  s   
 d| <   7 78HIIGl126FGGG 7 788fghhdLO$8$89 ##dS_`a`bSc#d d ;)?)?+t,,&,,.'+$1#K0H+/( $$-h$7LC&wu>>"7|q0)'!*c::!89DDa!5!56 #-";G9Dj k#  D'!*gaj9qz-+21:WQZ(# %8$ (-8\!_))* ) &D%**-='(D$ &7 ! 1+0(1s   -G3 3HHc                 H    [        SU R                  R                   S35      e)Nz$You cannot use ``__delitem__`` on a 
 instance.r   r   r!   r$   r   r   s      r)   __delitem__ModelOutput.__delitem__  s#    >t~~?V?V>WWabccr,   c                 H    [        SU R                  R                   S35      e)Nz#You cannot use ``setdefault`` on a r   r   r   s      r)   
setdefaultModelOutput.setdefault  s#    =dnn>U>U=VV`abbr,   c                 H    [        SU R                  R                   S35      e)NzYou cannot use ``pop`` on a r   r   r   s      r)   popModelOutput.pop  s"    6t~~7N7N6OzZ[[r,   c                 H    [        SU R                  R                   S35      e)NzYou cannot use ``update`` on a r   r   r   s      r)   updateModelOutput.update  s#    9$..:Q:Q9RR\]^^r,   c                     [        U[        5      (       a  [        U R                  5       5      nX!   $ U R	                  5       U   $ r.   )ra   rK   r   r   to_tuple)r$   r   
inner_dicts      r)   __getitem__ModelOutput.__getitem__  s8    adjjl+J= ==?1%%r,   c                 n   > XR                  5       ;   a  Ub  [        TU ]	  X5        [        TU ]  X5        g r.   )keysr   __setitem____setattr__)r$   r   valuer   s      r)   r	  ModelOutput.__setattr__  s.    99;5#4G,D(r,   c                 B   > [         TU ]  X5        [         TU ]	  X5        g r.   )r   r  r	  )r$   keyr
  r   s      r)   r  ModelOutput.__setitem__  s    C'C'r,   c                    >^  [        T 5      (       d  [        TT ]	  5       $ [        TT ]	  5       tpn[        U 4S j[	        T 5       5       5      nX/UQ7$ )Nc              3   P   >#    U  H  n[        TUR                  5      v   M     g 7fr.   r   r   s     r)   r   )ModelOutput.__reduce__.<locals>.<genexpr>  s     IL5WT5::..Ls   #&)r
   r   
__reduce__r   r	   )r$   callable_args	remainingr   r   s   `    r)   r  ModelOutput.__reduce__  sP    D!!7%''&+g&8&:#)IF4LII)	))r,   c                 J   ^  [        U 4S jT R                  5        5       5      $ )zQ
Convert self to a tuple containing all the attributes/keys that are not `None`.
c              3   .   >#    U  H
  nTU   v   M     g 7fr.   r-   )r   r   r$   s     r)   r   'ModelOutput.to_tuple.<locals>.<genexpr>  s     2kT!Wks   )r   r  )r$   s   `r)   r  ModelOutput.to_tuple  s     2diik222r,   r-   )r   N)r!   r/   r0   r1   r2   r   r   r   r   r   r   r   r  r	  r  r  r   r   r  r3   __classcell__)r   s   @r)   r   r   D  sY    26)pdc\_&)(*3%* 3 3r,   r   outputr   z_torch_pytree.Contextc                 f    [        U R                  5       5      [        U R                  5       5      4$ r.   )r   r_   r  )r  s    r)   r   r     s#    FMMO$d6;;=&999r,   r_   contextc           
      6    U" S0 [        [        X5      5      D6$ )Nr-   )r   zip)r_   r  r   s      r)   r   r     s    
 8T#g"6788r,   r   r   r   r   c                   (    \ rS rSrSr\S 5       rSrg)ExplicitEnumi  z;
Enum with more explicit error message for missing values.
c           
      ~    [        U SU R                   S[        U R                  R	                  5       5       35      e)Nz is not a valid z, please select one of )rB   r!   r   _value2member_map_r  )r   r
  s     r)   	_missing_ExplicitEnum._missing_   s?    g%cll^3J4PSPfPfPkPkPmKnJop
 	
r,   r-   N)r!   r/   r0   r1   r2   classmethodr%  r3   r-   r,   r)   r"  r"    s     
 
r,   r"  c                   $    \ rS rSrSrSrSrSrSrg)PaddingStrategyi  zz
Possible values for the `padding` argument in [`PreTrainedTokenizerBase.__call__`]. Useful for tab-completion in an
IDE.
longest
max_length
do_not_padr-   N)	r!   r/   r0   r1   r2   LONGEST
MAX_LENGTH
DO_NOT_PADr3   r-   r,   r)   r)  r)    s    
 GJJr,   r)  c                   ,    \ rS rSrSrSrSrSrSrSr	Sr
g	)

TensorTypei  z
Possible values for the `return_tensors` argument in [`PreTrainedTokenizerBase.__call__`]. Useful for
tab-completion in an IDE.
rF   rG   rI   rH   rJ   r-   N)r!   r/   r0   r1   r2   PYTORCH
TENSORFLOWNUMPYJAXMLXr3   r-   r,   r)   r1  r1    s"    
 GJE
C
Cr,   r1  c                   8    \ rS rSrSrS\\   4S jrS rS r	Sr
g)	ContextManagersi  z
Wrapper for `contextlib.ExitStack` which enters a collection of context managers. Adaptation of `ContextManagers`
in the `fastcore` library.
context_managersc                 .    Xl         [        5       U l        g r.   )r9  r   stack)r$   r9  s     r)   r   ContextManagers.__init__%  s     0[
r,   c                 `    U R                    H  nU R                  R                  U5        M      g r.   )r9  r;  enter_context)r$   context_managers     r)   	__enter__ContextManagers.__enter__)  s$    #44OJJ$$_5  5r,   c                 <    U R                   R                  " U0 UD6  g r.   )r;  __exit__r   s      r)   rC  ContextManagers.__exit__-  s    

T,V,r,   )r9  r;  N)r!   r/   r0   r1   r2   r   r   r   r@  rC  r3   r-   r,   r)   r8  r8    s"    
!n)= !6-r,   r8  c                 h   [        U 5      nUS:X  a!  [        R                  " U R                  5      nOGUS:X  a!  [        R                  " U R                  5      nO [        R                  " U R
                  5      nUR                   H)  nUS:X  d  M  UR                  U   R                  SL d  M)    g   g)zb
Check if a given model can return loss.

Args:
    model_class (`type`): The class of the model.
rG   rF   return_lossTF)infer_frameworkinspect	signaturecallforward__call__
parametersr   )model_classr   rI  ps       r)   can_return_lossrP  1  s      ,ID%%k&6&67		d	%%k&9&9:	%%k&:&:;	!!)"6"6q"9"A"AT"I " r,   c                    U R                   n[        U 5      nUS:X  a!  [        R                  " U R                  5      nOGUS:X  a!  [        R                  " U R
                  5      nO [        R                  " U R                  5      nSU;   a+  UR                   Vs/ s H  nSU;   d  US;   d  M  UPM     sn$ UR                   Vs/ s H  nSU;   d  M  UPM     sn$ s  snf s  snf )za
Find the labels used by a given model.

Args:
    model_class (`type`): The class of the model.
rG   rF   QuestionAnsweringlabel)start_positionsend_positions)r!   rG  rH  rI  rJ  rK  rL  rM  )rN  
model_namer   rI  rO  s        r)   find_labelsrW  G  s     %%J,ID%%k&6&67		d	%%k&9&9:	%%k&:&:;	j($//m/a7a<1HlCl/mm$//@/a7a</@@ n@s   C.C
C!C!d
parent_key	delimiterc                 0    SS jn[        U" XU5      5      $ )z/Flatten a nested dict into a single level dict.c              3     #    U R                  5        Hk  u  p4U(       a  [        U5      U-   [        U5      -   OUnU(       a7  [        U[        5      (       a"  [	        XEUS9R                  5        S h  vN   Mf  XT4v   Mm     g  N7f)N)rZ  )r   rK   ra   r   flatten_dict)rX  rY  rZ  r   r   r  s         r)   _flatten_dict#flatten_dict.<locals>._flatten_dict`  sg     GGIDA:D#j/I-A6!CZ>22')DJJLLLf  Ms   A0B2B3B r   )r   )rX  rY  rZ  r^  s       r)   r]  r]  ]  s     aY788r,   Fuse_temp_dirc              #      #    U(       a#  [         R                  " 5        nUv   S S S 5        g U v   g ! , (       d  f       g = f7fr.   )tempfileTemporaryDirectory)working_dirrb  tmp_dirs      r)   working_or_temp_dirrh  k  s3     ((*gM +*  +*s   A2A
A Ac                 f   [        U 5      (       a  [        R                  " XS9$ [        U 5      (       a  Uc  U R                  $ U R
                  " U6 $ [        U 5      (       a  SSKnUR                  XS9$ [        U 5      (       a  SSK	J
n  UR                  XS9$ [        S[        U 5       S35      e)zy
Framework-agnostic version of `numpy.transpose` that will work on torch/TensorFlow/Jax tensors as well as NumPy
arrays.
)axesNr   )permz"Type not supported for transpose: r   )rU   rI   	transposerR   TpermuterS   r~   rT   r   r   rB   rL   )r   rj  rG   r   s       r)   rl  rl  t  s    
 e||E--			,uww@EMM4,@@	e		||E|--	u		}}U}..=d5k]!LMMr,   c                 T   [        U 5      (       a  [        R                  " X5      $ [        U 5      (       a  U R                  " U6 $ [	        U 5      (       a  SSKnUR                  X5      $ [        U 5      (       a  SSKJn  UR                  X5      $ [        S[        U 5       S35      e)zw
Framework-agnostic version of `numpy.reshape` that will work on torch/TensorFlow/Jax tensors as well as NumPy
arrays.
r   Nz Type not supported for reshape: r   )rU   rI   reshaperR   rS   r~   rT   r   r   rB   rL   )r   newshaperG   r   s       r)   rp  rp    s    
 ezz%**			}}h''	e		zz%**	u		{{5++;DK=JKKr,   c                 n   [        U 5      (       a  [        R                  " XS9$ [        U 5      (       a"  Uc  U R                  5       $ U R                  US9$ [	        U 5      (       a  SSKnUR                  XS9$ [        U 5      (       a  SSKJn  UR                  XS9$ [        S[        U 5       S35      e)zw
Framework-agnostic version of `numpy.squeeze` that will work on torch/TensorFlow/Jax tensors as well as NumPy
arrays.
axisNdimr   z Type not supported for squeeze: r   )rU   rI   squeezerR   rS   r~   rT   r   r   rB   rL   r   rt  rG   r   s       r)   rw  rw    s    
 ezz%++			"&,u}}KEMMdM4KK	e		zz%z++	u		{{5{,,;DK=JKKr,   c                 L   [        U 5      (       a  [        R                  " X5      $ [        U 5      (       a  U R	                  US9$ [        U 5      (       a  SSKnUR                  XS9$ [        U 5      (       a  SSKJ	n  UR                  XS9$ [        S[        U 5       S35      e)z{
Framework-agnostic version of `numpy.expand_dims` that will work on torch/TensorFlow/Jax tensors as well as NumPy
arrays.
ru  r   Nrs  z$Type not supported for expand_dims: r   )rU   rI   expand_dimsrR   	unsqueezerS   r~   rT   r   r   rB   rL   rx  s       r)   rz  rz    s    
 e~~e**			4((	e		~~e~//	u		u00?U}ANOOr,   c                 @   [        U 5      (       a  [        R                  " U 5      $ [        U 5      (       a  U R	                  5       $ [        U 5      (       a  SSKnUR                  U 5      $ [        U 5      (       a  U R                  $ [        S[        U 5       S35      e)zt
Framework-agnostic version of `numpy.size` that will work on torch/TensorFlow/Jax tensors as well as NumPy arrays.
r   Nz$Type not supported for tensor_size: r   )
rU   rI   sizerR   numelrS   r~   rT   rB   rL   )r   rG   s     r)   tensor_sizer    s~     ewwu~			{{}	e		wwu~	u		zz?U}ANOOr,   c                     U R                  5        H\  u  p#[        U[        [        45      (       a&  U Vs/ s H  oDb  SU;  a  U SU 3OUPM     snX'   MF  Uc  MK  SU;  d  MS  U SU 3X'   M^     U $ s  snf )z:
Adds the information of the repo_id to a given auto map.
--)r   ra   r   r   )auto_maprepo_idr  r
  r   s        r)   add_model_info_to_auto_mapr    s     nn&
eeT]++dijdi_`=TQR]y1#.YZZdijHM4u#4&ir%1HM	 ' O	 ks   A3c                 ~    U R                  5        H(  nSX   ;   d  M  X   S   nSU;  d  M  U SU 3X   S'   M*     U $ )zA
Adds the information of the repo_id to a given custom pipeline.
implr  )r  )custom_pipeliner  taskmodules       r)   "add_model_info_to_custom_pipelinesr    sW    
  $$&_**$*62F6!3:)2fX0F%f-	 '
 r,   c                    [         R                  " U 5       H  nUR                  nUR                  nUR	                  S5      (       d  UR	                  S5      (       d  US:X  a    gUR	                  S5      (       d  US:X  a    gUR	                  S5      (       d  UR	                  S	5      (       d  US
:X  d  M    g   [        SU  S35      e)z
Infers the framework of a given model without using isinstance(), because we cannot guarantee that the relevant
classes are imported or available.
r~   kerasTFPreTrainedModelrG   rl   PreTrainedModelrF   flaxrH   FlaxPreTrainedModelz%Could not infer framework from class r   )rH  getmror/   r!   rM   r   )rN  
base_classr  r   s       r)   rG  rG    s    
 nn[1
&&""\**f.?.?.H.HDTgLgw''43D+Dv&&&*;*;E*B*BdNcFc 2 ?}ANOOr,   c                     [        5       (       d  [        U 5      $ SSKnUR                  R	                  5       (       a5  [        XR                  5      (       a  U R                  UR                  5      $ [        U 5      $ )zc
Casts an input to a torch int64 tensor if we are in a tracing context, otherwise to a Python int.
r   N)	r   r   rl   jit
is_tracingra   rm   toint64rn   s     r)   	torch_intr    sX     1v %		 4 4 6 6:a;V;V144b\_`a\bbr,   c                     [        5       (       d  [        U 5      $ SSKnUR                  R	                  5       (       a5  [        XR                  5      (       a  U R                  UR                  5      $ [        U 5      $ )zg
Casts an input to a torch float32 tensor if we are in a tracing context, otherwise to a Python float.
r   N)	r   r   rl   r  r  ra   rm   r  float32rn   s     r)   torch_floatr    sX     1v"'))"6"6"8"8Z<<=X=X144d^abc^ddr,   extrac                 @   ^ U =(       d    / n [        U 5      mU4S jnU$ )a  
Decorator to filter out named arguments that are not in the function signature.

This decorator ensures that only the keyword arguments that match the function's signature, or are specified in the
`extra` list, are passed to the function. Any additional keyword arguments are filtered out and a warning is issued.

Parameters:
    extra (`Optional[list]`, *optional*):
        A list of extra keyword argument names that are allowed even if they are not in the function's signature.

Returns:
    Callable:
        A decorator that wraps the function and filters out invalid keyword arguments.

Example usage:

    ```python
    @filter_out_non_signature_kwargs(extra=["allowed_extra_arg"])
    def my_function(arg1, arg2, **kwargs):
        print(arg1, arg2, kwargs)

    my_function(arg1=1, arg2=2, allowed_extra_arg=3, invalid_arg=4)
    # This will print: 1 2 {"allowed_extra_arg": 3}
    # And issue a warning: "The following named arguments are not valid for `my_function` and were ignored: 'invalid_arg'"
    ```
c                    >^ ^^^ [         R                  " T 5      n[        UR                  R	                  5       5      nUR                  T5      mSU;   mSU;   mST l        [        T 5      U UUU4S j5       nU$ )Nr$   r   Tc                    > 0 n0 nUR                  5        H  u  pEUT;   a  XRU'   M  XSU'   M     U(       a  UR                  5        Vs/ s H	  nSU S3PM     nnSR                  U5      nT
(       a  U S   R                  R                  S-   nOT	(       a  U S   R                  S-   nOSn[
        R                  " SU TR                   SU 3[        SS	9  T" U 0 UD6$ s  snf )
N'z, r   r   ra  z1The following named arguments are not valid for `z` and were ignored: r   )
stacklevel)r   r  joinr   r!   warningswarnUserWarning)r   r   valid_kwargsinvalid_kwargsr   r   invalid_kwargs_names
cls_prefixfuncis_class_methodis_instance_methodvalid_kwargs_to_passs           r)   wrapperCfilter_out_non_signature_kwargs.<locals>.decorator.<locals>.wrapperH  s    LN,,&'O()1%	 ' :H:M:M:O'P:OQ!A3a:O$'P'+yy1E'F$ &!%a!2!2!;!;c!AJ$!%a!1!1C!7J!#JG
|TXTaTaSb c**>)?A 	 ...% (Qs   C)rH  rI  setrM  r  union _filter_out_non_signature_kwargsr   )r  sigfunction_named_argsr  r  r  r  extra_params_to_passs   `   @@@r)   	decorator2filter_out_non_signature_kwargs.<locals>.decorator<  s}    %!#.."5"5"782889MN $'::#66 15-	t	/ 
	/> r,   )r  )r  r  r  s     @r)   filter_out_non_signature_kwargsr    s&    6 KREu:,\ r,   c                   *    \ rS rSr% Sr\\   \S'   Srg)
LossKwargsim  z
Keyword arguments to be passed to the loss function

Attributes:
    num_items_in_batch (`int`, *optional*):
        Number of items in the batch. It is recommended to pass it when
        you are doing gradient accumulation.
num_items_in_batchr-   N)	r!   r/   r0   r1   r2   r   r   __annotations__r3   r-   r,   r)   r  r  m  s     !%r,   r  )totalconfig_dictc                     SU ;   $ )z3Checks whether a config dict is a timm config dict.pretrained_cfgr-   )r  s    r)   is_timm_config_dictr  z  s    {**r,   pretrained_model_pathc                    U c  g[        U 5      n [        R                  R                  U 5      n[        R                  R	                  U 5      nU(       aK  U R                  S5      (       a5  [        U 5       n[        R                  " U5      nSSS5        [        W5      $ U(       a  [        R                  R                  [        R                  R                  U S5      5      (       aS  [        [        R                  R                  U S5      5       n[        R                  " U5      nSSS5        [        W5      $ g! , (       d  f       N= f! , (       d  f       N+= f)z9
Checks whether a checkpoint is a timm model checkpoint.
NFz.jsonzconfig.json)rK   ospathisfileisdirendswithopenjsonloadr  existsr  )r  is_fileis_dirr<   r  s        r)   is_timm_local_checkpointr    s     $   56ggnn23GWW]]01F (11'::'(A))A,K )";// "''...C]!STT"'',,4mDE))A,K F";// )( FEs   6D=E=
E
Er  ztorch.nn.Moduler  r
  c                 b    [        XU5        U R                  5        H  n[        X1U5        M     g)z-
Set a value to a module and all submodules.
N)r#   childrenset_attribute_for_modules)r  r  r
  	submodules       r)   r  r    s)     F__&	!)%8 'r,   c                 ~    [        X5      (       a  [        X5        U R                  5        H  n[        X!5        M     g)z2
Delete a value from a module and all submodules.
N)rx   delattrr  del_attribute_from_modules)r  r  r  s      r)   r  r    s0    
 v__&	"92 'r,   c                 0   ^  [        T 5      U 4S j5       nU$ )z
Decorator to wrap model method, to call output.to_tuple() if return_dict=False passed as a kwarg or
use_return_dict=False is set in the config.

Note:
    output.to_tuple() convert output to tuple skipping all `None` values.
c                   > UR                  SS5      SL n[        U S5      (       a  U R                  R                  SL OSn[	        U SS5      nU(       a  U(       a  [        U SS5         T" U /UQ70 UD6nU(       d  U(       a  U(       a  UR                  5       nU(       a  U(       a  [        U S5        U$ ! U(       a  U(       a  [        U S5        f f f = f)Nreturn_dictTFconfig_is_top_level_module)r   rx   r  use_return_dictr"   r  r  r  )r$   r   r   is_requested_to_return_tupleis_configured_to_return_tupleis_top_level_moduler  r  s          r)   r  !can_return_tuple.<locals>.wrapper  s    '-zz-'F%'O$PWX\^fPgPg(C(Cu(Lmr% &d,BDI(-@%d,BEJ	I$000F+0MRe* -1D*41GH -1D*41GH 2E,s   *1B7 7C)r   )r  r  s   ` r)   can_return_tupler    s"     4[ 2 Nr,   r.   r`  )F)jr2   rH  r  r  rd  r  collectionsr   r   collections.abcr   r   
contextlibr   r   dataclassesr	   r
   enumr   	functoolsr   r   typingr   r   r   r   r   rI   	packagingr   import_utilsr   r   r   r   r   r   rl   propertyr   rD   rP   r\   rd   rh   rU   ro   rR   rt   rv   rz   r|   r   rS   r   r   r   rT   r   rV   r   r   r   r   utils_pytree_torch_pytreer   r   r   r   r   r   r/   r!   r   rK   r"  r)  r1  r8  rP  rW  r]  boolrh  rl  rp  rw  rz  r  r  r  rG  r  r  r  r  r   r  r  r  r  r  r-   r,   r)   <module>r     s     	   - 4 0 ,  $ ; ;    h 05$9*0%'?'F&E$A$I&<#;"J4X3+ X3v //:k :eDIG^<^6_ : 99(9 
	9 }}&()W]]5-AA**!+E$/$:$:#;1[=Q=Q<R!S		
 	++!+E	
	
3 	
l 
 
- -$,A,9N 9 9S 9 4  N*L*L*P*P$

P$	c	eL8D> L^
&% 
&+T#s(^ + +
C D 89&7 9c 9# 9	3'8 	3s 	3#r,   