o
    h}                     @   s  d Z ddlZddlZddlZddlZddlZddlmZmZ ddl	m
Z
mZ ddlmZmZ ddlmZmZ ddlmZ ddlmZmZ dd	lmZmZmZmZ ddlZdd
lmZ ddl m!Z!m"Z"m#Z#m$Z$m%Z%m&Z& e% rqddl'Z'G dd de(Z)dd Z*dd Z+dd Z,dd Z-dd Z.dd Z/dd Z0dd Z1dd  Z2d!d" Z3d#d$ Z4d%d& Z5d'd( Z6d)d* Z7d+d, Z8d-d. Z9d/d0 Z:d1d2 Z;d3d4 Z<d5d6 Z=d7d8 Z>d9d: Z?G d;d< d<eZ@e% r0ddlAmB  mCZD d=e@d>eEeFe d?f fd@dAZG	ddBe
e dCd?d>e@fdDdEZHeIe! eIdFkr%eDjJe@eGeeHe@dGe@jK dHe@jL dI neDMe@eGeeHe@dG G dJdK dKeNeZOG dLdM dMeOZPG dNdO dOeOZQG dPdQ dQZRdRdS ZSdTdU ZTddWedXeNdYeNfdZd[ZUedd]eVfd^d_ZWdd`daZXdbdc ZYddddeZZdfdg Z[dhdi Z\djdk Z]dldm Z^dndo Z_dpdq Z`drds ZaddteeF fdudvZbG dwdx dxed\dyZcdzedeNef d>eVfd{d|Zed}eNd>eVfd~dZfdddeNdefddZgdddeNfddZhdd ZidS )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                   @   s   e Zd ZdZdddZdS )cached_propertyz
    Descriptor that mimics @property but caches output in member variable.

    From tensorflow_datasets

    Built-in in functools from Python 3.8.
    Nc                 C   sX   |d u r| S | j d u rtdd| j j }t||d }|d u r*|  |}t||| |S )Nzunreadable attribute	__cached_)fgetAttributeError__name__getattrsetattr)selfobjobjtypeattrcached r%   n/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/transformers/utils/generic.py__get__:   s   

zcached_property.__get__N)r   
__module____qualname____doc__r'   r%   r%   r%   r&   r   1   s    r   c                 C   s.   |   } | dv r
dS | dv rdS td| )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)valr%   r%   r&   	strtoboolI   s   r;   c                 C   sV   t t| }|drdS |drdS |drdS |dr"dS |d	r)d
S dS )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representationr%   r%   r&   infer_framework_from_reprW   s   




rF   c                    sf   t ttttd t| du rg ng}dkr|d |fdd D   fdd|D S )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.
    )r<   r=   r>   r?   r@   Nr?   c                    s   g | ]
}| d fvr|qS )r?   r%   .0r3   )preferred_frameworkr%   r&   
<listcomp>z   s    z1_get_frameworks_and_test_func.<locals>.<listcomp>c                    s   i | ]}| | qS r%   r%   rG   )framework_to_testr%   r&   
<dictcomp>{   s    z1_get_frameworks_and_test_func.<locals>.<dictcomp>)is_torch_tensoris_tf_tensoris_jax_tensoris_numpy_arrayis_mlx_arrayrF   appendextend)rD   
frameworksr%   )rK   rI   r&   _get_frameworks_and_test_funci   s   
rU   c                 C   sT   t | }| D ]	}|| r dS qt| rdS t r(ddlm} t| |r(dS dS )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)rU   valuesr   r   jax.corerV   
isinstance)rD   framework_to_test_func	test_funcrV   r%   r%   r&   	is_tensor~   s   
r\   c                 C   s   t | tjS r(   )rY   r?   ndarrayrD   r%   r%   r&   	_is_numpy   s   r_   c                 C   s   t | S )z/
    Tests if `x` is a numpy array or not.
    )r_   r^   r%   r%   r&   rP      s   rP   c                 C      dd l }t| |jS Nr   )torchrY   TensorrD   rb   r%   r%   r&   	_is_torch      re   c                 C      t  sdS t| S )z]
    Tests if `x` is a torch tensor or not. Safe to call even if torch is not installed.
    F)r   re   r^   r%   r%   r&   rM         rM   c                 C   r`   ra   )rb   rY   devicerd   r%   r%   r&   _is_torch_device   rf   rj   c                 C   rg   )z]
    Tests if `x` is a torch device or not. Safe to call even if torch is not installed.
    F)r   rj   r^   r%   r%   r&   is_torch_device   rh   rk   c                 C   s8   dd l }t| trt|| rt|| } ndS t| |jS )Nr   F)rb   rY   rA   hasattrr   dtyperd   r%   r%   r&   _is_torch_dtype   s   

rn   c                 C   rg   )z\
    Tests if `x` is a torch dtype or not. Safe to call even if torch is not installed.
    F)r   rn   r^   r%   r%   r&   is_torch_dtype   rh   ro   c                 C   r`   ra   )
tensorflowrY   rc   rD   r=   r%   r%   r&   _is_tensorflow   rf   rr   c                 C   rg   )zg
    Tests if `x` is a tensorflow tensor or not. Safe to call even if tensorflow is not installed.
    F)r   rr   r^   r%   r%   r&   rN      rh   rN   c                 C   s(   dd l }t|dr|| S t| |jS )Nr   is_symbolic_tensor)rp   rl   rs   rY   rc   rq   r%   r%   r&   _is_tf_symbolic_tensor   s   

rt   c                 C   rg   )z
    Tests if `x` is a tensorflow symbolic tensor or not (ie. not eager). Safe to call even if tensorflow is not
    installed.
    F)r   rt   r^   r%   r%   r&   is_tf_symbolic_tensor   s   ru   c                 C      dd l m} t| |jS ra   )	jax.numpynumpyrY   r]   )rD   jnpr%   r%   r&   _is_jax      rz   c                 C   rg   )zY
    Tests if `x` is a Jax tensor or not. Safe to call even if jax is not installed.
    F)r   rz   r^   r%   r%   r&   rO      rh   rO   c                 C   rv   ra   )mlx.corecorerY   array)rD   mxr%   r%   r&   _is_mlx   r{   r   c                 C   rg   )zZ
    Tests if `x` is a mlx array or not. Safe to call even when mlx is not installed.
    F)r   r   r^   r%   r%   r&   rQ      rh   rQ   c                 C   s  t | ttfr	| S t | ttfrdd |  D S t | ttfrMzt	| }t
|jtjs6t
|jtjr;| W S W n	 tyE   Y nw dd | D S dd dd dd d	d d
}t| }| D ]\}}|| rt|| |   S qdt | tjr|  S | S )zc
    Convert a TensorFlow tensor, PyTorch tensor, Numpy array or python list to a python list.
    c                 S      i | ]	\}}|t |qS r%   	to_py_objrH   kvr%   r%   r&   rL         zto_py_obj.<locals>.<dictcomp>c                 S   s   g | ]}t |qS r%   r   )rH   or%   r%   r&   rJ     s    zto_py_obj.<locals>.<listcomp>c                 S      |   S r(   tolistr!   r%   r%   r&   <lambda>      zto_py_obj.<locals>.<lambda>c                 S   s   |    S r(   )rx   r   r   r%   r%   r&   r     s    c                 S   s   t |  S r(   )r?   asarrayr   r   r%   r%   r&   r     s    c                 S   r   r(   r   r   r%   r%   r&   r     r   r<   r=   r>   r?   )rY   intfloatdictr   itemslisttupler?   r~   
issubdtyperm   integerfloatingr   	ExceptionrU   number)r!   arrframework_to_py_objrZ   	frameworkr[   r%   r%   r&   r     s6   
 
r   c                 C   s   dd dd dd dd d}t | ttfrdd |  D S t | ttfr+t| S t| }| D ]\}}|| rC|| |   S q3| S )	zc
    Convert a TensorFlow tensor, PyTorch tensor, Numpy array or python list to a Numpy array.
    c                 S   s   |     S r(   )detachcpurx   r   r%   r%   r&   r   0  s    zto_numpy.<locals>.<lambda>c                 S   r   r(   )rx   r   r%   r%   r&   r   1  r   c                 S   s
   t | S r(   )r?   r   r   r%   r%   r&   r   2  s   
 c                 S   s   | S r(   r%   r   r%   r%   r&   r   3  s    r   c                 S   r   r%   )to_numpyr   r%   r%   r&   rL   7  r   zto_numpy.<locals>.<dictcomp>)	rY   r   r   r   r   r   r?   r~   rU   )r!   framework_to_numpyrZ   r   r[   r%   r%   r&   r   *  s   
r   c                       s   e Zd ZdZdddZ fddZdd	 Zd
d Zdd Zdd Z	dd Z
dd Z fddZ fddZ fddZdee fddZ  ZS )ModelOutputa  
    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>
    returnNc                 C   sf   t  r1tt tdkr$tj| ttt| d| j	 d| j
 d dS t| ttt| 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.
        2.2output_type.serialized_type_nameN)r   r   parser   _torch_pytreeregister_pytree_node_model_output_flattenr   _model_output_unflattenr)   r   _register_pytree_node)clsr%   r%   r&   __init_subclass__R  s   


zModelOutput.__init_subclass__c                    sJ   t  j|i | | jtk}|r!t| s#t| j d| jj dd S d S )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_subclassr   r%   r&   r   g  s   
zModelOutput.__init__c           
         s  t  }t|st jj dtdd |dd D s't jj dt |d j}t fdd|dd D }|rt|st	|t
rP| }d	}nzt|}d	}W n tyc   d
}Y nw |rt|D ]F\}}t	|ttfrt|dkrt	|d ts|dkr| |d j< ntd| d nt |d |d  |d dur|d  |d < qjdS dS |dur| |d j< dS dS |D ]}t |j}	|	dur|	 |j< qdS )zeCheck the ModelOutput dataclass.

        Only occurs if @dataclass decorator has been used.
        z has no fields.c                 s   s    | ]}|j d u V  qd S r(   )defaultrH   fieldr%   r%   r&   	<genexpr>  s    z,ModelOutput.__post_init__.<locals>.<genexpr>r   Nz. should not have more than one required field.r   c                 3   s     | ]}t  |jd u V  qd S r(   r   namer   r    r%   r&   r     s    TF   zCannot set key/value for z&. It needs to be a tuple (key, value).)r   lenr9   r   r   allr   r   r\   rY   r   r   iterr   	enumerater   r   rA   r   )
r    class_fieldsfirst_fieldother_fields_are_noneiteratorfirst_field_iteratoridxelementr   r   r%   r   r&   __post_init__v  sZ   


zModelOutput.__post_init__c                 O      t d| jj d)Nz$You cannot use ``__delitem__`` on a 
 instance.r   r   r   r    r   r   r%   r%   r&   __delitem__     zModelOutput.__delitem__c                 O   r   )Nz#You cannot use ``setdefault`` on a r   r   r   r%   r%   r&   
setdefault  r   zModelOutput.setdefaultc                 O   r   )NzYou cannot use ``pop`` on a r   r   r   r%   r%   r&   pop  r   zModelOutput.popc                 O   r   )NzYou cannot use ``update`` on a r   r   r   r%   r%   r&   update  r   zModelOutput.updatec                 C   s*   t |trt|  }|| S |  | S r(   )rY   rA   r   r   to_tuple)r    r   
inner_dictr%   r%   r&   __getitem__  s   
zModelOutput.__getitem__c                    s4   ||   v r|d urt || t || d S r(   )keysr   __setitem____setattr__)r    r   valuer   r%   r&   r     s   zModelOutput.__setattr__c                    s    t  || t  || d S r(   )r   r   r   )r    keyr   r   r%   r&   r     s   zModelOutput.__setitem__c                    sJ   t  s	t  S t  ^}}}t fddt D }||g|R S )Nc                 3   s    | ]	}t  |jV  qd S r(   r   r   r   r%   r&   r     s    z)ModelOutput.__reduce__.<locals>.<genexpr>)r	   r   
__reduce__r   r   )r    callable_args	remainingr   r   r   r&   r     s
   
zModelOutput.__reduce__c                    s   t  fdd  D S )za
        Convert self to a tuple containing all the attributes/keys that are not `None`.
        c                 3   s    | ]} | V  qd S r(   r%   rH   r   r   r%   r&   r     s    z'ModelOutput.to_tuple.<locals>.<genexpr>)r   r   r   r%   r   r&   r     s   zModelOutput.to_tuple)r   N)r   r)   r*   r+   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r%   r%   r   r&   r   D  s    
8r   outputr   z_torch_pytree.Contextc                 C   s   t |  t |  fS r(   )r   rW   r   )r   r%   r%   r&   r     s   r   rW   contextc                 C   s   |di t t|| S )Nr%   )r   zip)rW   r   r   r%   r%   r&   r     s   r   r   r   r   r   c                   @   s   e Zd ZdZedd ZdS )ExplicitEnumzC
    Enum with more explicit error message for missing values.
    c                 C   s$   t | d| j dt| j  )Nz is not a valid z, please select one of )r9   r   r   _value2member_map_r   )r   r   r%   r%   r&   	_missing_  s   zExplicitEnum._missing_N)r   r)   r*   r+   classmethodr   r%   r%   r%   r&   r     s    r   c                   @   s   e Zd ZdZdZdZdZdS )PaddingStrategyz
    Possible values for the `padding` argument in [`PreTrainedTokenizerBase.__call__`]. Useful for tab-completion in an
    IDE.
    longest
max_length
do_not_padN)r   r)   r*   r+   LONGEST
MAX_LENGTH
DO_NOT_PADr%   r%   r%   r&   r     s
    r   c                   @   s$   e Zd ZdZdZdZdZdZdZdS )
TensorTypez
    Possible values for the `return_tensors` argument in [`PreTrainedTokenizerBase.__call__`]. Useful for
    tab-completion in an IDE.
    r<   r=   r?   r>   r@   N)	r   r)   r*   r+   PYTORCH
TENSORFLOWNUMPYJAXMLXr%   r%   r%   r&   r     s    r   c                   @   s2   e Zd ZdZdee fddZdd Zdd Zd	S )
ContextManagersz
    Wrapper for `contextlib.ExitStack` which enters a collection of context managers. Adaptation of `ContextManagers`
    in the `fastcore` library.
    context_managersc                 C   s   || _ t | _d S r(   )r   r   stack)r    r   r%   r%   r&   r   !  s   zContextManagers.__init__c                 C   s   | j D ]}| j| qd S r(   )r   r   enter_context)r    context_managerr%   r%   r&   	__enter__%  s   
zContextManagers.__enter__c                 O   s   | j j|i | d S r(   )r   __exit__r   r%   r%   r&   r   )  s   zContextManagers.__exit__N)	r   r)   r*   r+   r   r   r   r   r   r%   r%   r%   r&   r     s
    r   c                 C   sn   t | }|dkrt| j}n|dkrt| j}nt| j}|jD ]}|dkr4|j| jdu r4 dS q#dS )zr
    Check if a given model can return loss.

    Args:
        model_class (`type`): The class of the model.
    r=   r<   return_lossTF)infer_frameworkinspect	signaturecallforward__call__
parametersr   )model_classr   r  pr%   r%   r&   can_return_loss-  s   
r  c                 C   sn   | j }t| }|dkrt| j}n|dkrt| j}nt| j}d|v r/dd |jD S dd |jD S )zq
    Find the labels used by a given model.

    Args:
        model_class (`type`): The class of the model.
    r=   r<   QuestionAnsweringc                 S   s    g | ]}d |v s|dv r|qS )label)start_positionsend_positionsr%   rH   r
  r%   r%   r&   rJ   T  s     zfind_labels.<locals>.<listcomp>c                 S   s   g | ]}d |v r|qS )r  r%   r  r%   r%   r&   rJ   V      )r   r  r  r  r  r  r  r  )r	  
model_namer   r  r%   r%   r&   find_labelsC  s   r   d
parent_key	delimiterc                 C   s   ddd}t || ||S )z/Flatten a nested dict into a single level dict.r  r   c                 s   sf    |   D ]+\}}|rt|| t| n|}|r+t|tr+t|||d  E d H  q||fV  qd S )N)r  )r   rA   rY   r   flatten_dict)r  r  r  r   r   r   r%   r%   r&   _flatten_dict\  s   z#flatten_dict.<locals>._flatten_dictNr  r   )r   )r  r  r  r  r%   r%   r&   r  Y  s   
r  Fuse_temp_dirc                 c   sD    |rt  }|V  W d    d S 1 sw   Y  d S | V  d S r(   )tempfileTemporaryDirectory)working_dirr  tmp_dirr%   r%   r&   working_or_temp_dirg  s   
"
r   c                 C   s   t | rtj| |dS t| r|du r| jS | j| S t| r*ddl}|j| |dS t| r;ddl	m
} |j| |dS tdt|  d)z
    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   )rP   r?   	transposerM   TpermuterN   rp   rO   rw   rx   r9   rB   )r~   r!  r=   ry   r%   r%   r&   r#  p  s   r#  c                 C   sv   t | r
t| |S t| r| j| S t| r!ddl}|| |S t| r1ddlm} || |S t	dt
|  d)z
    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   )rP   r?   reshaperM   rN   rp   rO   rw   rx   r9   rB   )r~   newshaper=   ry   r%   r%   r&   r&    s   
r&  c                 C   s   t | rtj| |dS t| r|du r|  S | j|dS t| r,ddl}|j| |dS t| r=ddlm} |j| |dS t	dt
|  d)z
    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   )rP   r?   squeezerM   rN   rp   rO   rw   rx   r9   rB   r~   r)  r=   ry   r%   r%   r&   r,    s   r,  c                 C   s|   t | r
t| |S t| r| j|dS t| r#ddl}|j| |dS t| r4ddlm	} |j| |dS t
dt|  d)z
    Framework-agnostic version of `numpy.expand_dims` that will work on torch/TensorFlow/Jax tensors as well as NumPy
    arrays.
    r*  r   Nr(  z$Type not supported for expand_dims: r   )rP   r?   expand_dimsrM   	unsqueezerN   rp   rO   rw   rx   r9   rB   r-  r%   r%   r&   r.    s   r.  c                 C   s^   t | r	t| S t| r|  S t| rddl}|| S t| r%| jS tdt	|  d)z|
    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   )
rP   r?   sizerM   numelrN   rp   rO   r9   rB   )r~   r=   r%   r%   r&   tensor_size  s   

r2  c                    s^   |   D ](\}}t|ttfr fdd|D | |< q|dur,d|vr,  d| | |< q| S )zB
    Adds the information of the repo_id to a given auto map.
    c                    s.   g | ]}|d urd|vr  d| n|qS )N--r%   )rH   r   repo_idr%   r&   rJ     s   . z.add_model_info_to_auto_map.<locals>.<listcomp>Nr3  )r   rY   r   r   )auto_mapr5  r   r   r%   r4  r&   add_model_info_to_auto_map  s   r7  c                 C   sH   |   D ]}d| | v r!| | d }d|vr!| d| | | d< q| S )zI
    Adds the information of the repo_id to a given custom pipeline.
    implr3  )r   )custom_pipeliner5  taskmoduler%   r%   r&   "add_model_info_to_custom_pipelines  s   r<  c                 C   s   t | D ]6}|j}|j}|ds|ds|dkr dS |ds'|dkr* dS |ds8|d	s8|d
kr; dS qtd|  d)z
    Infers the framework of a given model without using isinstance(), because we cannot guarantee that the relevant
    classes are imported or available.
    rp   kerasTFPreTrainedModelr=   rb   PreTrainedModelr<   flaxr>   FlaxPreTrainedModelz%Could not infer framework from class r   )r  getmror)   r   rC   r   )r	  
base_classr;  r   r%   r%   r&   r    s   r  c                 C   @   t  st| S ddl}|j rt| |jr| |jS t| S )zk
    Casts an input to a torch int64 tensor if we are in a tracing context, otherwise to a Python int.
    r   N)	r   r   rb   jit
is_tracingrY   rc   toint64rd   r%   r%   r&   	torch_int     *rI  c                 C   rD  )zo
    Casts an input to a torch float32 tensor if we are in a tracing context, otherwise to a Python float.
    r   N)	r   r   rb   rE  rF  rY   rc   rG  float32rd   r%   r%   r&   torch_float  rJ  rL  extrac                    s    | pg } t |   fdd}|S )aI  
    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                    sV   t  }t|j }|d|v d|v d _t  fdd}|S )Nr    r   Tc                     s   i }i }|  D ]\}}|v r|||< q|||< q|rRdd | D }d|}r5| d jjd }nr?| d jd }nd}tjd|  j d| td	d
  | i |S )Nc                 S   s   g | ]}d | d qS )'r%   r   r%   r%   r&   rJ   P  r  zWfilter_out_non_signature_kwargs.<locals>.decorator.<locals>.wrapper.<locals>.<listcomp>z, r   r   r  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_passr%   r&   wrapperD  s,   


zCfilter_out_non_signature_kwargs.<locals>.decorator.<locals>.wrapper)r  r  setr  r   union _filter_out_non_signature_kwargsr   )rY  sigfunction_named_argsr]  extra_params_to_passrX  r&   	decorator8  s   

z2filter_out_non_signature_kwargs.<locals>.decorator)r^  )rM  re  r%   rc  r&   filter_out_non_signature_kwargs  s   .rf  c                   @   s   e Zd ZU dZee ed< dS )
LossKwargsz
    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_batchN)r   r)   r*   r+   r   r   __annotations__r%   r%   r%   r&   rg  i  s   
 	rg  )totalconfig_dictc                 C   s   d| v S )z3Checks whether a config dict is a timm config dict.pretrained_cfgr%   )rk  r%   r%   r&   is_timm_config_dictv  s   rm  pretrained_model_pathc                 C   s   | du rdS t | } tj| }tj| }|r=| dr=t| }t|}W d   t	|S 1 s4w   Y  t	|S |rotj
tj| drottj| d}t|}W d   t	|S 1 sfw   Y  t	|S dS )zA
    Checks whether a checkpoint is a timm model checkpoint.
    NFz.jsonzconfig.json)rA   ospathisfileisdirendswithopenjsonloadrm  existsrP  )rn  is_fileis_dirr3   rk  r%   r%   r&   is_timm_local_checkpoint{  s(   


rz  r;  ztorch.nn.Moduler   r   c                 C   s*   t | || |  D ]}t||| q
dS )z5
    Set a value to a module and all submodules.
    N)r   childrenset_attribute_for_modules)r;  r   r   	submoduler%   r%   r&   r|    s   r|  c                 C   s0   t | |r
t| | |  D ]}t|| qdS )z:
    Delete a value from a module and all submodules.
    N)rl   delattrr{  del_attribute_from_modules)r;  r   r}  r%   r%   r&   r    s
   

r  c                    s   t   fdd}|S )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              	      s   | dddu }t| dr| jjdu nd}t| dd}|r%|r%t| dd z! | g|R i |}|s7|r;|r;| }W |rE|rEt| d |S |rQ|rRt| d w w w )Nreturn_dictTFconfig_is_top_level_module)r   rl   r  use_return_dictr   r|  r   r  )r    r   r   is_requested_to_return_tupleis_configured_to_return_tupleis_top_level_moduler   rY  r%   r&   r]    s    
z!can_return_tuple.<locals>.wrapper)r   )rY  r]  r%   r  r&   can_return_tuple  s   	r  r(   r  )F)jr+   r  ru  ro  r  rQ  collectionsr   r   collections.abcr   r   
contextlibr   r   dataclassesr   r	   enumr
   	functoolsr   r   typingr   r   r   r   rx   r?   	packagingr   import_utilsr   r   r   r   r   r   rb   propertyr   r;   rF   rU   r\   r_   rP   re   rM   rj   rk   rn   ro   rr   rN   rt   ru   rz   rO   r   rQ   r   r   r   torch.utils._pytreeutils_pytreer   r   r   r   r   r   r   r)   r   r   rA   r   r   r   r   r  r  r  boolr   r#  r&  r,  r.  r2  r7  r<  r  rI  rL  rf  rg  r   rm  rz  r|  r  r  r%   r%   r%   r&   <module>   s    
	% 




O	