wL i@ddlZddlZddlZddlmZmZmZmZddlm Z ddl m Z m Z m Z mZmZmZmZmZmZmZmZddlZddlmZddlmZmZddlmZdd lm Z dd l!m"Z"m#Z#dd l$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,e)rddl-Z-e'rddl.Z.dd l/m0Z1dd l/m2Z3e*jhe5Z6GddeZ7eddZ8edZ9e e9ge fZ:e e ge9fZ;ee:e;fZGddZ?Gdde?Z@dee7deAde7fdZBy)N)Fieldasdict dataclass is_dataclass)Path) AnyCallableClassVarDictListOptionalProtocolTupleTypeTypeVarUnion) constants)EntryNotFoundErrorHfHubHTTPError)hf_hub_download)HfApi) ModelCard ModelCardData)SoftTemporaryDirectory is_jsonableis_safetensors_availableis_simple_optional_typeis_torch_availableloggingunwrap_simple_optional_typevalidate_hf_hub_args) load_model) save_modelc(eZdZUeeeefed<y)DataclassInstance__dataclass_fields__N)__name__ __module__ __qualname__r r strr__annotations___/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/huggingface_hub/hub_mixin.pyr&r&(s"4U #344r.r&T ModelHubMixin)boundARGS_Ta_ --- # For reference on model card metadata, see the spec: https://github.com/huggingface/hub-docs/blob/main/modelcard.md?plain=1 # Doc / guide: https://huggingface.co/docs/hub/model-cards {{ card_data }} --- This model has been pushed to the Hub using the [PytorchModelHubMixin](https://huggingface.co/docs/huggingface_hub/package_reference/mixins#huggingface_hub.PyTorchModelHubMixin) integration: - Code: {{ repo_url | default("[More Information Needed]", true) }} - Paper: {{ paper_url | default("[More Information Needed]", true) }} - Docs: {{ docs_url | default("[More Information Needed]", true) }} c^eZdZUeed<eed<dZeeed<dZeeed<dZ eeed<y) MixinInfomodel_card_templatemodel_card_dataNdocs_url paper_urlrepo_url) r(r)r*r+r,rr8r r9r:r-r.r/r5r5Cs8"""Hhsm"#Ix}#"Hhsm"r.r5c8eZdZUdZdZeeeefe d<e e d<e e d<e e ejfe d<e e efe d<eedfe d <e eefe d <dddeddddddddd d ee d ee dee de deee dee dee dee dee dee deee dee eefddffdZdeedeffd Zedede fdZededefdZed eededeefd!Zddd"dd#d$ee efd%eeeefd&ee d'e d(ee e efdee f d)Zd$eddfd*Z ee!d"ddddd"dd+deed,ee efd-e d.ee d/ee d0eee e fd1eee efd2e d3ee defd4Z"edeed5e d3ee d1eee efd-e d/ee d.ee d2e d0eee e fdefd6Z#e!dd7ddddddddd8 d&e d%eeeefd9e d:ee d0ee d;ee deeee e fd?eeee e fd(ee e efde fd@Z$de%fdAZ&xZ'S)Br1a A generic mixin to integrate ANY machine learning framework with the Hub. To integrate your framework, your model class must inherit from this class. Custom logic for saving/loading models have to be overwritten in [`_from_pretrained`] and [`_save_pretrained`]. [`PyTorchModelHubMixin`] is a good example of mixin integration with the Hub. Check out our [integration guide](../guides/integrations) for more instructions. When inheriting from [`ModelHubMixin`], you can define class-level attributes. These attributes are not passed to `__init__` but to the class definition itself. This is useful to define metadata about the library integrating [`ModelHubMixin`]. For more details on how to integrate the mixin with your library, checkout the [integration guide](../guides/integrations). Args: repo_url (`str`, *optional*): URL of the library repository. Used to generate model card. paper_url (`str`, *optional*): URL of the library paper. Used to generate model card. docs_url (`str`, *optional*): URL of the library documentation. Used to generate model card. model_card_template (`str`, *optional*): Template of the model card. Used to generate model card. Defaults to a generic template. language (`str` or `List[str]`, *optional*): Language supported by the library. Used to generate model card. library_name (`str`, *optional*): Name of the library integrating ModelHubMixin. Used to generate model card. license (`str`, *optional*): License of the library integrating ModelHubMixin. Used to generate model card. E.g: "apache-2.0" license_name (`str`, *optional*): Name of the library integrating ModelHubMixin. Used to generate model card. Only used if `license` is set to `other`. E.g: "coqui-public-model-license". license_link (`str`, *optional*): URL to the license of the library integrating ModelHubMixin. Used to generate model card. Only used if `license` is set to `other` and `license_name` is set. E.g: "https://coqui.ai/cpml". pipeline_tag (`str`, *optional*): Tag of the pipeline. Used to generate model card. E.g. "text-classification". tags (`List[str]`, *optional*): Tags to be added to the model card. Used to generate model card. E.g. ["computer-vision"] coders (`Dict[Type, Tuple[Callable, Callable]]`, *optional*): Dictionary of custom types and their encoders/decoders. Used to encode/decode arguments that are not jsonable by default. E.g dataclasses, argparse.Namespace, OmegaConf, etc. Example: ```python >>> from huggingface_hub import ModelHubMixin # Inherit from ModelHubMixin >>> class MyCustomModel( ... ModelHubMixin, ... library_name="my-library", ... tags=["computer-vision"], ... repo_url="https://github.com/huggingface/my-cool-library", ... paper_url="https://arxiv.org/abs/2304.12244", ... docs_url="https://huggingface.co/docs/my-cool-library", ... # ^ optional metadata to generate model card ... ): ... def __init__(self, size: int = 512, device: str = "cpu"): ... # define how to initialize your model ... super().__init__() ... ... ... ... def _save_pretrained(self, save_directory: Path) -> None: ... # define how to serialize your model ... ... ... ... @classmethod ... def from_pretrained( ... cls: Type[T], ... pretrained_model_name_or_path: Union[str, Path], ... *, ... force_download: bool = False, ... resume_download: Optional[bool] = None, ... proxies: Optional[Dict] = None, ... token: Optional[Union[str, bool]] = None, ... cache_dir: Optional[Union[str, Path]] = None, ... local_files_only: bool = False, ... revision: Optional[str] = None, ... **model_kwargs, ... ) -> T: ... # define how to deserialize your model ... ... >>> model = MyCustomModel(size=256, device="gpu") # Save model weights to local directory >>> model.save_pretrained("my-awesome-model") # Push model weights to the Hub >>> model.push_to_hub("my-awesome-model") # Download and initialize weights from the Hub >>> reloaded_model = MyCustomModel.from_pretrained("username/my-awesome-model") >>> reloaded_model.size 256 # Model card has been correctly populated >>> from huggingface_hub import ModelCard >>> card = ModelCard.load("username/my-awesome-model") >>> card.data.tags ["x-custom-tag", "pytorch_model_hub_mixin", "model_hub_mixin"] >>> card.data.library_name "my-library" ``` N_hub_mixin_config_hub_mixin_info_hub_mixin_inject_config_hub_mixin_init_parameters"_hub_mixin_jsonable_default_values. _hub_mixin_jsonable_custom_types_hub_mixin_coders) r:r9r8r6language library_namelicense license_name license_link pipeline_tagtagscodersr:r9r8r6rCrDrErFrGrHrIrJreturnc t|| xsg} | jdt|t } t |dr|t k(r|jj| _t di|jjj| _ |jj| _ |jj| _ |jj| _ | |_||t k7r|| _||| _ ||| _ ||| _ ||| j_||| j_||| j_||| j_| | | j_| | | j_| Xt)| }| jj*&| jj*j-|n|| j_| jj*7t/t1| jj*| j_| xsi|_t5|j2j7|_t;t=j>|j@jB|_"|jDjGDcic]i}|jHt<jJjLurA|jO|jHr&|jP|jS|jHkc}|_*dt=j>|jVjBv|_,ycc}w)zIInspect __init__ signature only once when subclassing + handle modelcard.model_hub_mixin)r6r7r=Nconfigr-)-super__init_subclass__appendr5rhasattrDEFAULT_MODEL_CARDr=r6r7to_dictr8r9r:rCrDrErFrGrHlistrIextendsortedsetrBtuplekeysrAdictinspect signature__init__ parametersr?valuesdefault Parameterempty _is_jsonablename _encode_argr@_from_pretrainedr>)clsr:r9r8r6rCrDrErFrGrHrIrJinfonormalized_tagsparam __class__s r/rPzModelHubMixin.__init_subclass__s4 !#zr %&-@R_Rab 3) *"&88+.+>+>+R+R($1#a33F3F3V3V3^3^3`#aD  //88DM 00::DN//88DM"  */BFX/X':D $  $DM  &DN  $DM  ,4D )  #0>@2 }}G$5$5$;$;;@P@PQVQ^Q^@_ JJ 6 62 . (073D3DSEYEY3Z3e3e'e$ 2 sA.M)rhc <t ||}|j|Sitt |j dd|Dcic]\}}|| c}}|}t |jdr |d|_|Si|j|jDcic](\}}|j|r||j|*c}}}|jdi}t|tr|j||ik7r||_|Scc}}wcc}}w)aACreate a new instance of the class and handle config. 3 cases: - If `self._hub_mixin_config` is already set, do nothing. - If `config` is passed as a dataclass, set it as `self._hub_mixin_config`. - Otherwise, build `self._hub_mixin_config` from default values and passed values. NrrN)rO__new__r<ziprUr?rgetr@itemsrdrfpop isinstancer[update) rhargskwargsinstancekeyvalue passed_values init_config passed_configrls r/rnzModelHubMixin.__new__#sI7?3'  % % 1O  #&778<#CU       ))(3 4)6x)@H &O  44  #0"5"5"7C((/S__U++  $"5  mT *   } - " )4H &I*s  D-Dryc^t|ryt||jryt|S)z&Check if a value is JSON serializable.T)rrsrAr)rhrys r/rdzModelHubMixin._is_jsonableYs-   eSAA B5!!r.argct|r t|S|jjD]"\}\}}t ||s|y||cS|S)z3Encode an argument into a JSON serializable format.N)rrrBrqrs)rhr~type_encoder_s r/rfzModelHubMixin._encode_argbs`  #; ##8#>#>#@ $ E#>#@ & E++F  F#%%'!ZZ$qI &&z2);61B1N-TV%%' $D $ $ 9'8 9 > >~P[?[ \ ',,.F!#)x (--#4##cGGXc\bc cr.ct)aD Overwrite this method in subclass to define how to save your model. Check out our [integration guide](../guides/integrations) for instructions. Args: save_directory (`str` or `Path`): Path to directory in which the model weights and configuration will be saved. NotImplementedError)rrs r/rzModelHubMixin._save_pretraineds "!r.)force_downloadresume_downloadproxiestoken cache_dirlocal_files_onlyrevisionpretrained_model_name_or_pathrrrrrrrc t|} d} tjj| rtj tj | vr/tjj| tj } nctjtj dt| jn# t| tj ||||||| } d} | t| dd5}t!j"|} ddd| j%D]_\}}||j&vs|j&|j(}|t*j,j.usK|j1||| |<a|j&j3D]<}|j4| vs|j4| vs!| |j4| |j4<>d|j&vr4d| vr0|j&dj(}|j1|| } | | d<t7|r$|j8D]}|| vs|| vs | || |<nLt;d |j&j3Dr"| j%D]\}}|| vs || |<|j<r d| vr| | d<|j>d t| |||||||d | }| tA|d ddifvr| |_!|S#t$r<} tjtj dt| Yd} ~ Nd} ~ wwxYw#1swY/xYw) a Download a model from the Huggingface Hub and instantiate it. Args: pretrained_model_name_or_path (`str`, `Path`): - Either the `model_id` (string) of a model hosted on the Hub, e.g. `bigscience/bloom`. - Or a path to a `directory` containing model weights saved using [`~transformers.PreTrainedModel.save_pretrained`], e.g., `../path/to/my_model_directory/`. revision (`str`, *optional*): Revision of the model on the Hub. Can be a branch name, a git tag or any commit id. Defaults to the latest commit on `main` branch. force_download (`bool`, *optional*, defaults to `False`): Whether to force (re-)downloading the model weights and configuration files from the Hub, overriding the existing cache. proxies (`Dict[str, str]`, *optional*): A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}`. The proxies are used on every request. token (`str` or `bool`, *optional*): The token to use as HTTP bearer authorization for remote files. By default, it will use the token cached when running `hf auth login`. cache_dir (`str`, `Path`, *optional*): Path to the folder where cached files are stored. local_files_only (`bool`, *optional*, defaults to `False`): If `True`, avoid downloading the file and return the path to the local cached file if it exists. model_kwargs (`Dict`, *optional*): Additional kwargs to pass to the model during initialization. Nz not found in rfilenamerrrrrrrz# not found on the HuggingFace Hub: rzutf-8)encodingrNc3jK|]+}|jtjjk(-yw)N)kindr\rb VAR_KEYWORD).0rks r/ z0ModelHubMixin.from_pretrained../s%vUUZZ7#4#4#@#@@vs13)model_idrrrrrrrr<r-)"r+ospathisdirrrlistdirjoinloggerwarningrresolverrriopenrloadrqr? annotationr\rbrcrr`rerr'anyr>rggetattrr<)rhrrrrrrrr model_kwargsr config_fileerNfrxryrrkconfig_annotationrws r/from_pretrainedzModelHubMixin.from_pretraineds>T45%) 77== "$$ 8(<< ggll8Y5J5JK )"7"7!8tH~G]G]G_F`ab c-$&22%'#1#$3%5    "k39 &Q1 &%lln L U#888$'$B$B3$G$R$RM$G,=,=,C,CC&)oomU&Ks  L77>>@ B::\1ejjF6J/5ejj/AL, B 3999hl>Z$'$B$B8$L$W$W!):FC*0 X&C 338C,.3&=,23K S)8vcNlNlNsNsNuvv"(,,.2JC,.,1 S)2 ++ 0L)/ X&'3''  ])+-      785H$#OTXZ\S]#])/H &{" c y4455XY\]^Y_X`abb c  & &s$"K27L:2 L7;1L22L7:Mrc t)aOverwrite this method in subclass to define how to load your model from pretrained. Use [`hf_hub_download`] or [`snapshot_download`] to download files from the Hub before loading them. Most args taken as input can be directly passed to those 2 methods. If needed, you can add more arguments to this method using "model_kwargs". For example [`PyTorchModelHubMixin._from_pretrained`] takes as input a `map_location` parameter to set on which device the model should be loaded. Check out our [integration guide](../guides/integrations) for more instructions. Args: model_id (`str`): ID of the model to load from the Huggingface Hub (e.g. `bigscience/bloom`). revision (`str`, *optional*): Revision of the model on the Hub. Can be a branch name, a git tag or any commit id. Defaults to the latest commit on `main` branch. force_download (`bool`, *optional*, defaults to `False`): Whether to force (re-)downloading the model weights and configuration files from the Hub, overriding the existing cache. proxies (`Dict[str, str]`, *optional*): A dictionary of proxy servers to use by protocol or endpoint (e.g., `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}`). token (`str` or `bool`, *optional*): The token to use as HTTP bearer authorization for remote files. By default, it will use the token cached when running `hf auth login`. cache_dir (`str`, `Path`, *optional*): Path to the folder where cached files are stored. local_files_only (`bool`, *optional*, defaults to `False`): If `True`, avoid downloading the file and return the path to the local cached file if it exists. model_kwargs: Additional keyword arguments passed along to the [`~ModelHubMixin._from_pretrained`] method. r) rhrrrrrrrrrs r/rgzModelHubMixin._from_pretrainedKs Z"!r.z!Push model using huggingface_hub.) rNcommit_messageprivaterbranch create_prallow_patternsignore_patternsdelete_patternsrrrrrrrrc t|} | j||dj}t5} t | |z }|j ||| | j |d|||||| |  cdddS#1swYyxYw)a5 Upload model checkpoint to the Hub. Use `allow_patterns` and `ignore_patterns` to precisely filter which files should be pushed to the hub. Use `delete_patterns` to delete existing remote files in the same commit. See [`upload_folder`] reference for more details. Args: repo_id (`str`): ID of the repository to push to (example: `"username/my-model"`). config (`dict` or `DataclassInstance`, *optional*): Model configuration specified as a key/value dictionary or a dataclass instance. commit_message (`str`, *optional*): Message to commit while pushing. private (`bool`, *optional*): Whether the repository created should be private. If `None` (default), the repo will be public unless the organization's default is private. token (`str`, *optional*): The token to use as HTTP bearer authorization for remote files. By default, it will use the token cached when running `hf auth login`. branch (`str`, *optional*): The git branch on which to push the model. This defaults to `"main"`. create_pr (`boolean`, *optional*): Whether or not to create a Pull Request from `branch` with that commit. Defaults to `False`. allow_patterns (`List[str]` or `str`, *optional*): If provided, only files matching at least one pattern are pushed. ignore_patterns (`List[str]` or `str`, *optional*): If provided, files matching any of the patterns are not pushed. delete_patterns (`List[str]` or `str`, *optional*): If provided, remote files matching any of the patterns will be deleted from the repo. model_card_kwargs (`Dict[str, Any]`, *optional*): Additional arguments passed to the model card template to customize the model card. Returns: The url of the commit of your model in the given repository. )rT)rrr)rNrmodel) r repo_type folder_pathrrrrrrN)r create_reporrrr upload_folder)rrrNrrrrrrrrrapitmp saved_paths r/rzModelHubMixin.push_to_hubzsh% //'7T/RZZ$ % cW,J  FN_ `$$!&-#- / /%    s ?F"" $S'*;*;%; <<(,S#X6&+D#I&66D'M**#'#'"(,&*!%&*&*&*$( /\f3- \f C= \f 3- \f!\f49%\fsm\f#\fsm\fsm\fsm\f tCy!!\f$ w  %\f0 1\f|4T!W4!4l"""" c c  V SXfEU(<@!%!6:Bc4i(Bt%6678 B # B  B$DcN3B #BH "t " " %*."&,004!&"&y !Wy',S$Y'7y y "$ y $ yc4i()yE#t),-yy3-y yyv," !W,","3- ," E#t),- ,"  ,"$,""$,","c4i()," ,","\ <@A"&# $$(:>;?;?6:DDt%6678 D  D $ D}D DD>D!tCy#~!67D"%S 3"78D"%S 3"78D$DcN3D DDL i r.c eZdZdZdddeeeddffdZdeddfdZ e d d d d ed eedee eefde dee dee de de ee dfdede fdZe dededede def dZe dededede def dZxZS)PyTorchModelHubMixina~ Implementation of [`ModelHubMixin`] to provide model Hub upload/download capabilities to PyTorch models. The model is set in evaluation mode by default using `model.eval()` (dropout modules are deactivated). To train the model, you should first set it back in training mode with `model.train()`. See [`ModelHubMixin`] for more details on how to use the mixin. Example: ```python >>> import torch >>> import torch.nn as nn >>> from huggingface_hub import PyTorchModelHubMixin >>> class MyModel( ... nn.Module, ... PyTorchModelHubMixin, ... library_name="keras-nlp", ... repo_url="https://github.com/keras-team/keras-nlp", ... paper_url="https://arxiv.org/abs/2304.12244", ... docs_url="https://keras.io/keras_nlp/", ... # ^ optional metadata to generate model card ... ): ... def __init__(self, hidden_size: int = 512, vocab_size: int = 30000, output_size: int = 4): ... super().__init__() ... self.param = nn.Parameter(torch.rand(hidden_size, vocab_size)) ... self.linear = nn.Linear(output_size, vocab_size) ... def forward(self, x): ... return self.linear(x + self.param) >>> model = MyModel(hidden_size=256) # Save model weights to local directory >>> model.save_pretrained("my-awesome-model") # Push model weights to the Hub >>> model.push_to_hub("my-awesome-model") # Download and initialize weights from the Hub >>> model = MyModel.from_pretrained("username/my-awesome-model") >>> model.hidden_size 256 ``` N)rIrIrKcZ|xsg}|jd||d<t||i|S)Npytorch_model_hub_mixinrI)rQrOrP)rhrIrurvrls r/rPz&PyTorchModelHubMixin.__init_subclass__s7zr -.vw($9&99r.rct|dr |jn|}t|t|tj z y)z7Save weights from a Pytorch model to a local directory.moduleN)rRrsave_model_as_safetensorr+rSAFETENSORS_SINGLE_FILE)rr model_to_saves r/rz%PyTorchModelHubMixin._save_pretraineds0'.tX'> D  NYEfEf4f0ghr.cpuF) map_locationstrictrrrrrrrrrrc |di| } tjj|rMtdtjj |t j } |j| | | | S t|t j ||||||| } |j| | | | S#t$r9t|t j||||||| } |j| | | | cYSwxYw)z>%'#1#$3%5  ..uj,PVWW% T,$&;;%'#1#$3%5  **5*lFSS Ts65B,,?C.-C.rrctj|tj|d}|j|||j |S)NT)r weights_onlyr)torchrdeviceload_state_dicteval)rhrrrr state_dicts r/rz$PyTorchModelHubMixin._load_as_pickle8s?ZZ l9Sbfg  j8  r.c\tjjtjtjjdkr;t ||||dk7r&t jd|j||Stjj|||||S)Nz0.4.3rra0Loading model weights on other devices than 'cpu' is not supported natively in your version of safetensors. This means that the model is loaded on 'cpu' first and then copied to the device. This leads to a slower loading time. Please update safetensors to version 0.4.3 or above for improved performance.)rr) packagingversionparse safetensors __version__load_model_as_safetensorrrtorr#)rhrrrrs r/rz(PyTorchModelHubMixin._load_as_safetensor?s    " ";#:#: ;i>O>O>U>UV]>^ ^ $UJv Fu$e &     ( ( 6R^ ( _ r.)r(r)r*rr r r+rPrrrrrr rgr0rrrrs@r/rrsg+ZCG:HT#Y,?:UY: itii "/T/T3- /T E#t),- /T  /T$/T"$/T/TS$_%/T/T/T/TbA3cSW\]   s # W[ `a  r.rdataclsdatarKc |di|jDcic]\}}||jvs||c}}Scc}}w)zhLoad a dataclass instance from a dictionary. Fields not expected by the dataclass are ignored. r-)rqr')r rkvs r/rrPs;  Ztzz|Ytq!qGr s' >>gggg6*.   HH   H %55  C'   fXs] # cUF] #  9$ %  ## #~ ~ B@=@F[T"34[D[EV[r.