
    Z jB                       % S r SSKJr  SSKrSSK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Jr  SSKJr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   SSK!r"SSK#J$r$  SSK%J&r&J'r'J(r(  \(       a
  SSK)r)SSK)J*r*  \$RV                  " \,5      r-Sr.\'" 5       (       a  Sr.\/" 5       r0S\1S'   SMS jr2Sr3\&" 5       (       a  Sr3SNS jr4SOS jr5S r6SPS jr7SPS jr8SPS jr9SPS jr:SPS jr;S r<   SQ       SRS jjr=S r>SPS  jr? SS     STS! jjr@SUS" jrAS# rBS$ rCSVS% jrD " S& S'\5      rESWS( jrF SX       SYS) jjrG " S* S+\H\5      rI " S, S-\I5      rJ " S. S/\I5      rK " S0 S15      rLS2 rMS3 rNSZS[S4 jjrOSXS5 jrPS6 rQSXS7 jrRS8 rSS9 rTS: rUS; rVSXS\S< jjrW " S= S>\ SS?9rXS]S@ jrYS^SA jrZS_SB jr[S`SC jr\SD r]SE r^SF r_ " SG SH\5      r`SISJSKS\a44SL jrbg)az
Generic utilities
    )annotationsN)OrderedDictUserDict)CallableIterableMutableMapping)AbstractContextManager	ExitStacknullcontext)fieldsis_dataclass)Enum)partialwraps)TYPE_CHECKINGAny	TypedDict   )logging   )is_mlx_availableis_torch_availableis_torch_fx_proxy)nnFTzset[type[Any]]_registered_model_output_typesc           	     ,   [         (       d  g SS KnUR                  R                  5       (       a  g U [        ;   a  g SS KJs  Jn  UR                  U [        [        [        U S9U R                   SU R                   3S9  [        R                  U 5        g )Nr   )output_type.)serialized_type_name)_is_torch_availabletorchcompileris_compilingr   torch.utils._pytreeutils_pytreeregister_pytree_node_model_output_flattenr   _model_output_unflatten
__module____name__add)r   r!   torch_pytrees      k/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/utils/generic.py"_register_model_output_pytree_noder/   8   s    
 ~~""$$44..%%'[A + 6 67q9M9M8NO	 &  #&&{3    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.   	strtoboolrA   W   s:     ))+C
22
33
+C73
44r0   c                    [        [        U 5      5      n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 'numpy.npz<class 'mlx.mlxN)strtype
startswith)xrepresentations     r.   infer_framework_from_reprrK   e   sT    
 a\N  !122		"	"#3	4	4		"	">	2	2 
3r0   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.
)rC   rD   rE   rD   )is_torch_tensoris_numpy_arrayis_mlx_arrayrK   appendextend)rI   framework_to_testpreferred_framework
frameworksr9   s        r.   _get_frameworks_and_test_funcrU   s   s     
 4A6*29L8MJd"$"3\"3QATVZ@[7[q"3\]-78Z ##Z88 ]8s   A=A=,Bc                    [        U 5      nUR                  5        H  nU" U 5      (       d  M    g   [        U 5      (       a  gg)zs
Tests if `x` is a `torch.Tensor`, `np.ndarray` or `mlx.array` in the order defined by `infer_framework_from_repr`
TF)rU   valuesr   )rI   framework_to_test_func	test_funcs      r.   	is_tensorrZ      sA    
 ;1=+224	Q<< 5
 r0   c                6    [        U [        R                  5      $ )z'
Tests if `x` is a numpy array or not.
)
isinstancerD   ndarrayrI   s    r.   rN   rN      s     a$$r0   c                L    [         (       d  gSSKn[        XR                  5      $ )zU
Tests if `x` is a torch tensor or not. Safe to call even if torch is not installed.
Fr   N)r    r!   r\   TensorrI   r!   s     r.   rM   rM           a&&r0   c                L    [         (       d  gSSKn[        XR                  5      $ )zU
Tests if `x` is a torch device or not. Safe to call even if torch is not installed.
Fr   N)r    r!   r\   devicera   s     r.   is_torch_devicere      rb   r0   c                    [         (       d  gSSKn[        U [        5      (       a  [	        X5      (       a  [        X5      n Og[        XR                  5      $ )zT
Tests if `x` is a torch dtype or not. Safe to call even if torch is not installed.
Fr   N)r    r!   r\   rF   hasattrgetattrdtypera   s     r.   is_torch_dtyperj      sD     !S5!Aa%%r0   c                   [        U 5      (       a  g[        U 5      (       a  g[        U [        [        [
        [        R                  45      (       a  g[        U [        [        45      (       a  [        U 5      S:X  a  g[        U S   5      $ g)z9
Check if a value is array-like (includes ragged arrays)
Tr   F)rN   rM   r\   intfloatboolrD   numberlisttuplelen_is_tensor_or_array_like)values    r.   rs   rs      sn     eu%#udBII677%$''u:?'a11r0   c                    [         (       d  [        S5      eSSKnU S:X  a
  [        5       $ UR                  " U 5      (       d  U(       a  UR
                  " XX#S9$ [        5       $ )an  
Context manager that only autocasts if:

- `autocast` is already enabled in this context
- Or this call to `maybe_autocast` has `enabled=True`

This prevents `autocast` being added to the graph when it is effectively a no-op.
Which makes graph splitting in `torch.compile` more flexible as it removes the
requirement that partition IDs be monotonically increasing.
z2`maybe_autocast` requires PyTorch to be installed.r   Nmeta)ri   enabledcache_enabled)r    ImportErrorr!   r   is_autocast_enabledautocast)device_typeri   rw   rx   r!   s        r.   maybe_autocastr}      sU      NOOf}  --~~kee}r0   c                8    SS K Jn  [        XR                  5      $ )Nr   )mlx.corecorer\   array)rI   mxs     r.   _is_mlxr      s    a""r0   c                2    [         (       d  S$ [        U 5      $ )zR
Tests if `x` is a mlx array or not. Safe to call even when mlx is not installed.
F)_is_mlx_availabler   r^   s    r.   rO   rO      s     *)59wqz9r0   c                    U b  Ub  [        S5      eU b  U R                  nOUnUc  gUb%  [        R                  " S[	        U5      -   U5      SL$ SU;   $ )a@  
Checks whether some flavor of flash attention is requested or not. Optionally, checks for a specific version of
flash attention.

This is checked against one of the two arguments, i.e. either the `config` or the directly passed value
`requested_attention_implementation`. Otherwise, an error will be raised (ambiguity).

The different versions of flash attention are usually
- Implementations based on the original flash attention repo: https://github.com/Dao-AILab/flash-attention
- Kernels implementations such as: https://huggingface.co/kernels-community/vllm-flash-attn3
NzRequested attention implementation is ambiguous: Please pass either the config or the name of the attention implementation, not both.Fz	.*flash.*flash)r?   _attn_implementationrematchrF   )config"requested_attention_implementationversion checked_attention_implementations       r.   is_flash_attention_requestedr     s}     @Lc
 	

 +1+F+F(+M( (/ xxs7|35UV^bbb666r0   c                P    U c  gU R                  S5      nXR                  S5      4$ )z
Split the optional `paged|` prefix from an attention implementation string.

Note that `None` means using the default attention implementation, which is either torch's native `sdpa` or `eager` (if `sdpa` is not implemented for that model).
)FNzpaged|)rH   removeprefix)implementationis_pageds     r.   split_attention_implementationr   )  s1     ((2H00:::r0   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>  [        S U  5       5      (       a  [        U 5      $ U  Vs/ s H  n[        U5      PM     sn$ S S S.n[        U 5      nUR                  5        H  u  pgU" U 5      (       d  M  XF   " U 5      s  $    [        U [        R                  5      (       a  U R                  5       $ U $ s  snnf s  snf )zH
Convert a PyTorch tensor, Numpy array or python list to a python list.
c              3  l   #    U  H*  n[        U[        [        [        R                  45      v   M,     g 7fN)r\   rl   rm   rD   ro   ).0rI   s     r.   	<genexpr>to_py_obj.<locals>.<genexpr>@  s%     Cs!z!c5"))455ss   24c                "    U R                  5       $ r   tolistobjs    r.   <lambda>to_py_obj.<locals>.<lambda>G  
    #**,r0   c                "    U R                  5       $ r   r   r   s    r.   r   r   H  r   r0   rC   rD   )r\   rl   rm   dictr   items	to_py_objrp   rq   allrU   rD   ro   r   )r   kvoframework_to_py_objrX   	frameworkrY   s           r.   r   r   6  s    #U|$$
	C$)	*	*,/IIK8KDA9Q<K88	C$	'	'CsCCC9 '**c	!c** '& ;3? 6 < < >	S>>&1#66 !?
 #ryy!!zz|
1 9 +s   D6)D<c                   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 )zH
Convert a PyTorch tensor, Numpy array or python list to a Numpy array.
c                Z    U R                  5       R                  5       R                  5       $ r   )detachcpunumpyr   s    r.   r   to_numpy.<locals>.<lambda>^  s    #**,**,224r0   c                    U $ r    r   s    r.   r   r   _  s    #r0   r   )
r\   r   r   r   to_numpyrp   rq   rD   r   rU   )r   framework_to_numpyr   r   rX   r   rY   s          r.   r   r   X  s     5
 #h'((+.99;7;418A;;77	C$	'	'xx} ;3? 6 < < >	S>>%055 !? J 8s   Cc                     [        U SS9 nUR                  5       nSSS5        [        R                  " W5      nU$ ! , (       d  f       N&= f! [        R                   a    [        SU  S35      ef = f)zeA helper to load safe config files and raise a proper error message if it wasn't serialized correctlyzutf-8)encodingNz"It looks like the config file at 'z' is not a valid JSON file.)openreadjsonloadsJSONDecodeErrorOSError)	json_filereadertextconfig_dicts       r.   safe_load_json_filer   p  sq    c)g.&;;=D /jj&  /.  c:9+E`abbcs   
A =A 
AA $A2c                     ^  \ rS rSrSrSS jrU 4S jrU 4S j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S jrSrU =r$ )ModelOutputi{  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>
c                    [        U 5        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.
N)r/   )clss    r.   __init_subclass__ModelOutput.__init_subclass__  s     	+3/r0   c                  > [         TU ]  " U0 UD6  [        [        U 5      5        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__r/   rG   	__class__r   r   	TypeErrorr*   r+   )selfargskwargsis_modeloutput_subclassr   s       r.   r   ModelOutput.__init__  s{    $)&)*4:6 #'..K"?"<+=+=??#1T^^%<%<$= >_ _  ,>"r0   c                  >^  [        [        T 5      5        [        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aB  [        U5      (       Gd1  [        U[        5      (       a  UR                  5       nSnO [        U5      nSnU(       a  [!        T US   R                  S5        ["        T
T ]I  US   R                  5        ['        W5       H  u  pg[        U[(        [*        45      (       a'  [        U5      S
:w  d  [        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)r   fields     r.   r   ,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   rh   namer   r   r   s     r.   r   r     s#     #dSc%GD%**$=$EScs   %(TFr   zCannot set key/value for z&. It needs to be a tuple (key, value).)r/   rG   r   rr   r?   r   r+   r   rh   r   rZ   r\   r   r   iterr   setattrr   __delitem__	enumeraterp   rq   rF   )r   class_fieldsfirst_fieldother_fields_are_noneiteratorfirst_field_iteratoridxelementr   r   r   s   `         r.   __post_init__ModelOutput.__post_init__  s6   
 	+4:6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+/( $l1o22D9#LO$8$89$-h$7LC%ge}==WQRARZdelmneoqtZuZu!89DDa!5!56 #-";G9Dj k#  D'!*gaj9qz-+21:WQZ( %8 (-8\!_))* ) &D%**-='(D$ &5 ! 1+0(1s   H? ?IIc                H    [        SU R                  R                   S35      e)Nz$You cannot use ``__delitem__`` on a 
 instance.	Exceptionr   r+   r   r   r   s      r.   r   ModelOutput.__delitem__  s#    >t~~?V?V>WWabccr0   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br0   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[[r0   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\]^^r0   c                    [        U[        5      (       a  [        U R                  5       5      nX!   $ U R	                  5       U   $ r   )r\   rF   r   r   to_tuple)r   r   
inner_dicts      r.   __getitem__ModelOutput.__getitem__  s8    adjjl+J= ==?1%%r0   c                   > [        U 5       Vs1 s H  o3R                  iM     nnX;   a  Ub  [        TU ]  X5        [        TU ]  X5        g s  snf r   )r   r   r   __setitem____setattr__)r   r   rt   r   field_namesr   s        r.   r  ModelOutput.__setattr__  sG    /5d|<|ezz|<5#4G,D(	 =s   Ac                B   > [         TU ]  X5        [         TU ]	  X5        g r   )r   r   r  )r   keyrt   r   s      r.   r   ModelOutput.__setitem__  s    C'C'r0   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__rq   r   )r   callable_args	remainingr   r   s   `    r.   r
  ModelOutput.__reduce__  sP    D!!7%''&+g&8&:#)IF4LII)	))r0   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   )rq   keysr   s   `r.   r   ModelOutput.to_tuple   s     2diik222r0   r   )returnNone)r  rq   )r+   r*   __qualname____firstlineno____doc__r   r   r   r   r   r   r   r   r  r   r
  r   __static_attributes____classcell__)r   s   @r.   r   r   {  sN    0 6)pdc\_&)(*3 3r0   r   c                f    [        U R                  5       5      [        U R                  5       5      4$ r   )rp   rW   r  )outputs    r.   r(   r(     s#     $v{{}"555r0   c           
     6    U" S0 [        [        X5      5      D6$ )Nr   )r   zip)rW   contextr   s      r.   r)   r)     s    
 4c'2344r0   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 )r?   r+   rp   _value2member_map_r  )r   rt   s     r.   	_missing_ExplicitEnum._missing_  s?    g%cll^3J4PSPfPfPkPkPmKnJop
 	
r0   r   N)r+   r*   r  r  r  classmethodr%  r  r   r0   r.   r"  r"    s     
 
r0   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*   r  r  r  LONGEST
MAX_LENGTH
DO_NOT_PADr  r   r0   r.   r)  r)    s    
 GJJr0   r)  c                  $    \ 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.
rC   rD   rE   r   N)	r+   r*   r  r  r  PYTORCHNUMPYMLXr  r   r0   r.   r1  r1  *  s    
 GE
Cr0   r1  c                  .    \ rS rSrSrSS jrS rS rSrg)	ContextManagersi5  z
Wrapper for `contextlib.ExitStack` which enters a collection of context managers. Adaptation of `ContextManagers`
in the `fastcore` library.
c                .    Xl         [        5       U l        g r   )context_managersr
   stack)r   r8  s     r.   r   ContextManagers.__init__;  s     0[
r0   c                `    U R                    H  nU R                  R                  U5        M      g r   )r8  r9  enter_context)r   context_managers     r.   	__enter__ContextManagers.__enter__?  s$    #44OJJ$$_5  5r0   c                <    U R                   R                  " U0 UD6  g r   )r9  __exit__r   s      r.   rA  ContextManagers.__exit__C  s    

T,V,r0   )r8  r9  N)r8  zlist[AbstractContextManager])	r+   r*   r  r  r  r   r>  rA  r  r   r0   r.   r6  r6  5  s    
!6-r0   r6  c                    [         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.
return_lossTF)inspect	signatureforward
parametersr   )model_classrF  ps      r.   can_return_lossrK  G  sR     !!+"5"56I!!)"6"6q"9"A"AT"I " r0   c                   U R                   n[        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.
QuestionAnsweringlabel)start_positionsend_positions)r+   rE  rF  rG  rH  )rI  
model_namerF  rJ  s       r.   find_labelsrR  W  s     %%J!!+"5"56Ij($//m/a7a<1HlCl/mm$//@/a7a</@@ n@s   BB,
B:Bc                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)	delimiter)r   rF   r\   r   flatten_dict)d
parent_keyrU  r   r   r  s         r.   _flatten_dict#flatten_dict.<locals>._flatten_dictj  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   )rW  rX  rU  rY  s       r.   rV  rV  g  s     aY788r0   c                    [        U 5      (       a  [        R                  " XS9$ [        U 5      (       a  Uc  U R                  $ U R
                  " U6 $ [        S[        U 5       S35      e)z4
Framework-agnostic version of transpose operation.
)axesz"Type not supported for transpose: r   )rN   rD   	transposerM   Tpermuter?   rG   )r   r^  s     r.   r_  r_  u  s^     e||E--			,uww@EMM4,@@=d5k]!LMMr0   c                    [        U 5      (       a  [        R                  " X5      $ [        U 5      (       a  U R                  " U6 $ [	        S[        U 5       S35      e)z2
Framework-agnostic version of reshape operation.
z Type not supported for reshape: r   )rN   rD   reshaperM   r?   rG   )r   newshapes     r.   rc  rc    sQ     ezz%**			}}h'';DK=JKKr0   c                    [        U 5      (       a  [        R                  " XS9$ [        U 5      (       a"  Uc  U R                  5       $ U R                  US9$ [	        S[        U 5       S35      e)z2
Framework-agnostic version of squeeze operation.
)axisdimz Type not supported for squeeze: r   )rN   rD   squeezerM   r?   rG   r   rf  s     r.   ri  ri    sb     ezz%++			"&,u}}KEMMdM4KK;DK=JKKr0   c                    [        U 5      (       a  [        R                  " X5      $ [        U 5      (       a  U R	                  US9$ [        S[        U 5       S35      e)z6
Framework-agnostic version of expand_dims operation.
rg  z$Type not supported for expand_dims: r   )rN   rD   expand_dimsrM   	unsqueezer?   rG   rj  s     r.   rl  rl    sS     e~~e**			4((?U}ANOOr0   c                    [        U 5      (       a  [        R                  " U 5      $ [        U 5      (       a  U R	                  5       $ [        S[        U 5       S35      e)z/
Framework-agnostic version of size operation.
z$Type not supported for tensor_size: r   )rN   rD   sizerM   numelr?   rG   )r   s    r.   tensor_sizerq    sM     ewwu~			{{}?U}ANOOr0   c                    [         (       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    rl   r!   jit
is_tracingr\   r`   toint64ra   s     r.   	torch_intrw    sU     1v %		 4 4 6 6:a;V;V144b\_`a\bbr0   c                    [         (       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    rl   r!   rs  rt  r\   r`   ru  float32ra   s     r.   torch_floatrz    sU     1v"'))"6"6"8"8Z<<=X=X144d^abc^ddr0   c                @   ^ 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 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   r\  z1The following named arguments are not valid for `z` and were ignored: r   )
stacklevel)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>.wrapper  s    LN,,&'O()1%	 ' :H'I.Q!A3a.$'I'+yy1E'F$ &!%a!2!2!;!;c!AJ$!%a!1!1C!7J!#JG
|TXTaTaSb c**>)?A 	 ...% (Js   C)rE  rF  setrH  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	/ 
	/> r0   )r  )extrar  r  s     @r.   filter_out_non_signature_kwargsr    s&    6 KREu:,\ r0   c                  ~    \ rS rSr% SrS\S'   S\S'   S\S'   S\S'   S	\S
'   S	\S'   S\S'   S\S'   S	\S'   S\S'   Srg)TransformersKwargsi  a)  
Keyword arguments to be passed to the forward pass of a `PreTrainedModel`.

Attributes:
    num_items_in_batch (`Optional[torch.Tensor]`, *optional*):
        Number of items in the batch. It is recommended to pass it when you are doing gradient accumulation.
    output_hidden_states (`Optional[bool]`, *optional*):
        Most of the models support outputting all hidden states computed during the forward pass.
    output_attentions (`Optional[bool]`, *optional*):
        Turn this on to return the intermediary attention scores.
    output_router_logits (`Optional[bool]`, *optional*):
        For MoE models, this allows returning the router logits to compute the loss.
    cu_seq_lens_q (`torch.LongTensor`, *optional*)
        Gets cumulative sequence length for query state.
    cu_seq_lens_k (`torch.LongTensor`, *optional*)
        Gets cumulative sequence length for key state.
    max_length_q (`int`, *optional*):
        Maximum sequence length for query state.
    max_length_k (`int`, *optional*):
        Maximum sequence length for key state.
    position_ids (`torch.LongTensor`, *optional*)
        Indices of positions of each input sequence tokens.
    is_causal (`bool`, *optional*)
        Can be set to False to enable bi-directional attention, i.e. use decoder Attention modules as encoders.
ztorch.Tensor | Nonenum_items_in_batchbool | Noneoutput_hidden_statesoutput_attentionsoutput_router_logitsztorch.LongTensor | Nonecu_seq_lens_qcu_seq_lens_k
int | Nonemax_length_qmax_length_kposition_ids	is_causalr   N)r+   r*   r  r  r  __annotations__r  r   r0   r.   r  r    sE    4 ,+%%""%%****))r0   r  )totalc                    SU ;   $ )z3Checks whether a config dict is a timm config dict.pretrained_cfgr   )r   s    r.   is_timm_config_dictr  ?  s    {**r0   c                   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)rF   ospathisfileisdirendswithr   r   loadr  existsr  )pretrained_model_pathis_fileis_dirr9   r   s        r.   is_timm_local_checkpointr  D  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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)moduler  rt   	submodules       r.   r  r  `  s)     F__&	!)%8 'r0   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)rg   delattrr  del_attribute_from_modules)r  r  r  s      r.   r  r  i  s0    
 v__&	"92 'r0   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
return_dict=False is set in the config.

Note:
    output.to_tuple() convert output to tuple skipping all `None` values.
c                   > [        U S5      (       a  U R                  R                  OSnUR                  SU5      nUb  UnT" U /UQ70 UD6nU(       d%  [	        U[
        5      (       d  UR                  5       nU$ )Nr   Treturn_dict)rg   r   r  r   r\   rq   r   )r   r   r   r  return_dict_passedr  r  s         r.   r  !can_return_tuple.<locals>.wrapper~  sp    18x1H1Hdkk--d#ZZ{C),Kd,T,V,:fe#<#<__&Fr0   r   r  r  s   ` r.   can_return_tupler  u  s"     4[  Nr0   c                0   ^  [        T 5      U 4S j5       nU$ )z
Decorator using config field (if they exist) as default value for some args and kwargs. Precedence is always
given to the args/kwargs that are explicitly passed.
c                  > / SQnU GH3  nS nUTR                   R                  ;   a(  TR                   R                  R                  U5      S-
  nUb  [        U5      U:  a
  X   b  X   nO.UR	                  U5      b  X$   nO[        U R                  US 5      nUc  M  US:X  aB  [        U SS5      (       a/  U R                  (       a  U(       a  [        R                  S5        SnO!US:X  a  SS	/nXg;  a  [        S
U SU S35      eUb,  [        U5      U:  a  [        U5      nXaU'   [        U5      nGM/  XbU'   GM6     UR	                  S[        U R                  SS 5      5      nUbG  [        U R                  S5      n	U	(       a  U R                  R                  n
XR                  l        XS'    UR	                  SS5      (       aD  SSKJn  U" XR	                  SS5      UR	                  S5      5         T" U /UQ70 UD6nS S S 5        OT" U /UQ70 UD6nUb&  W	(       a  W
U R                  l        W$ U R                  ?W$ ! , (       d  f       N9= f! Ub&  W	(       a  W
U R                  l        f U R                  ?f f = f)N)	use_cachevision_feature_layervision_feature_select_strategyvision_aspect_ratior   r  gradient_checkpointingFzX`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`.r  r   fullz%`Unexpected select feature strategy: z. Please select from r   r  debug_ior   )model_addition_debugger_contextdebug_io_dirmodel_debugprune_layers)__code__co_varnamesindexrr   getrh   r   trainingloggerwarning_oncer?   rp   rq   rg   r  model_debugging_utilsr  )r   r   r   args_with_config_defaultsarg_name	arg_index	arg_valuevalid_strategiesr  is_causal_in_configis_causal_original_valuer  r  r  s                r.   r  +merge_with_config_defaults.<locals>.wrapper  su   %
! 2HI4==444 MM55;;HEI	$TY)>4?C^ O	H%1",	#DKK4@	${*t%=uEE$--\e++v %*	!AA(16':$ 8(CI;Ncdtcuuvw  (SY-B:D&/O ;D'08$A 2F JJ{GDKKd,ST	 ")$++{"C"+/;;+@+@($-KK!"+;	.zz*e,,S4**^]CVZZP^E_ "$888F 
 d4T4V4 $&,DDKK)  -  $&,DDKK)-	 %s%   2AI 7II 
II +J r  r  s   ` r.   merge_with_config_defaultsr    s%     4[F FP Nr0   c                B    [         R                  S5        [        U 5      $ )NzZThe `check_model_inputs` decorator is deprecated in favor of `merge_with_config_defaults`.)r  r  r  )r  s    r.   check_model_inputsr    s    
tu%d++r0   c                  ^    \ rS rSrSr0 rS rS rS rS r	S r
S r\SS	 j5       rSS
 jrSrg)GeneralInterfacei  z
Dict-like object keeping track of a class-wide mapping, as well as a local one. Allows to have library-wide
modifications through the class mapping, as well as local modifications in a single file with the local mapping.
c                    0 U l         g r   _local_mappingr  s    r.   r   GeneralInterface.__init__  s
     r0   c                \    XR                   ;   a  U R                   U   $ U R                  U   $ r   )r  _global_mappingr   r  s     r.   r   GeneralInterface.__getitem__  s0    %%%&&s++##C((r0   c                <    U R                   R                  X05        g r   )r  r   )r   r  rt   s      r.   r   GeneralInterface.__setitem__  s    ""C<0r0   c                    U R                   U	 g r   r  r  s     r.   r   GeneralInterface.__delitem__  s    $r0   c                H    [        0 U R                  EU R                  E5      $ r   )r   r  r  r  s    r.   __iter__GeneralInterface.__iter__   s$    Ct++Ct/B/BCDDr0   c                ~    [        U R                  R                  5       U R                  R                  5       -  5      $ r   )rr   r  r  r  r  s    r.   __len__GeneralInterface.__len__  s0    4'',,.1D1D1I1I1KKLLr0   c                <    U R                   R                  X05        g r   )r  r   )r   r  rt   s      r.   registerGeneralInterface.register  s    ""C<0r0   c                4    [        U R                  5       5      $ r   )rp   r  r  s    r.   
valid_keysGeneralInterface.valid_keys  s    DIIK  r0   r  N)r  rF   rt   r   )r  	list[str])r+   r*   r  r  r  r  r   r   r   r   r  r  r'  r  r  r  r   r0   r.   r  r    sG     O!)1%EM 1 1!r0   r     g      ?g      >@c                $   ^ ^^^^ UUUUU 4S jnU$ )ae  
Decorator that retries a function call with exponential backoff.

Args:
    max_retries (`int`, *optional*, defaults to 5):
        Maximum number of retry attempts.
    initial_delay (`float`, *optional*, defaults to 1.0):
        Initial delay in seconds before the first retry.
    max_delay (`float`, *optional*, defaults to 30.0):
        Maximum delay in seconds between retries.
    jitter (`bool`, *optional*, defaults to `True`):
        Whether to add random jitter to the delay.
    exceptions (`tuple`, *optional*, defaults to `(Exception,)`):
        Tuple of exception types to catch and retry on.
c                <   >^  [        T 5      UU UUUU4S j5       nU$ )Nc                   > Tn[        STS-   5       H  n T" U 0 UD6s  $    g ! T a  nUT:X  a  e [        UT
5      nT	(       a  U[        R                  " SS5      -  n[        R                  STR                   SU ST SU SUS	 S
35        [        R                  " U5        [        US-  T
5      n S nAM  S nAff = f)Nr   g?g333333?[z
] attempt /z	 failed: z
Retrying in z.1fzs...r   )	rangeminrandomuniformr  infor+   timesleep)r   r   delayattemptexc	sleep_for
exceptionsr  initial_delayjitter	max_delaymax_retriess         r.   r  )retry.<locals>.decorator.<locals>.wrapper'  s    !E K!O46000 5 " 6+- #E9 5I!V^^C%==	KKDMM?*WIQ{m9UXTY Z''0oT; JJy)	95E6s   $B>B	B99B>r  )r  r  r  r  r  r  r  s   ` r.   r  retry.<locals>.decorator&  s%    	t	6 	6 
	6* r0   r   )r  r  r  r  r  r  s   ````` r.   retryr    s    . 2 r0   )r   ztype[ModelOutput]r  r  )r  rl   )r  
str | None)r  rn   )NTN)r|   rF   ri   ztorch.dtype | Nonerw   rn   rx   r  )NNN)r   r  r   r  r  rn   )r   r  r  ztuple[bool, str | None])r   rF   )r  r   r  ztuple[list[Any], list[str]]r   )rW   zIterable[Any]r   r  r   ztype[ModelOutput] | Noner  r   r[  )rW  r   rX  rF   rU  rF   )r  zlist | None)r   zdict[str, Any]r  rn   )r  rF   r  rn   )r  	nn.Moduler  rF   rt   r   )r  r  r  rF   )cr  
__future__r   rE  r   r  r
  r   r  r  collectionsr   r   collections.abcr   r   r   
contextlibr	   r
   r   dataclassesr   r   enumr   	functoolsr   r   typingr   r   r   r   rD   r%   r   import_utilsr   r   r   r!   r   
get_loggerr+   r  r    r  r   r  r/   r   rA   rK   rU   rZ   rN   rM   re   rj   rs   r}   r   rO   r   r   r   r   r   r   r(   r)   rF   r"  r)  r1  r6  rK  rR  rV  r_  rc  ri  rl  rq  rw  rz  r  r  r  r  r  r  r  r  r  r  r   r  r   r0   r.   <module>r'     s\   #   	  	   - > > E E ,  $ 0 0   Q Q  
		H	%  14  642  59&"%	'	'&", !%!%	  	:#: _c!75?!7Q[!7	!7H
;D0I3+ I3X6 -1555 *5 	5	
3 	
l  - -$ A 9	N	L	L	P	P	c	eL^$% $N+
89	3.Oj,
&!~ &!T |0r0   