L itn ddlZddlmZmZddlmZmZmZmZddl Z ddl m Z m Z m Z ddlmZmZmZddlmZerdd l mZmZej,eZe r"dd lmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(e rdd lm)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1e se sejed dd de3dee3de4e3ee gefffdZ5GddZ6y)N)partialreduce) TYPE_CHECKINGCallableOptionalUnion)PretrainedConfigis_tf_availableis_torch_available)TF2_WEIGHTS_NAME WEIGHTS_NAMElogging) OnnxConfigPreTrainedModelTFPreTrainedModel) AutoModelAutoModelForCausalLMAutoModelForImageClassificationAutoModelForImageSegmentationAutoModelForMaskedImageModelingAutoModelForMaskedLMAutoModelForMultipleChoiceAutoModelForObjectDetectionAutoModelForQuestionAnswering AutoModelForSemanticSegmentationAutoModelForSeq2SeqLM"AutoModelForSequenceClassificationAutoModelForSpeechSeq2SeqAutoModelForTokenClassificationAutoModelForVision2Seq) TFAutoModelTFAutoModelForCausalLMTFAutoModelForMaskedLMTFAutoModelForMultipleChoiceTFAutoModelForQuestionAnswering"TFAutoModelForSemanticSegmentationTFAutoModelForSeq2SeqLM$TFAutoModelForSequenceClassification!TFAutoModelForTokenClassificationzThe ONNX export features are only supported for PyTorch or TensorFlow. You will not be able to export models without one of these libraries installed.onnx_config_clssupported_featuresr.returnc| tdt}|jdD]}t||}i}|D]M}d|vr-|j dd}t |j |||<4t |j|||<O|S)ac Generate the mapping between supported the features and their corresponding OnnxConfig for a given model. Args: *supported_features: The names of the supported features. onnx_config_cls: The OnnxConfig full name corresponding to the model. Returns: The dictionary mapping a feature to an OnnxConfig constructor. z#A OnnxConfig class must be provided. -with-past)task) ValueError transformerssplitgetattrreplacer with_pastfrom_model_config)r.r/ config_cls attr_namemappingfeaturer5s `/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/transformers/onnx/features.pysupported_features_mappingrB7s>??J$**3/4 Z3 4G%S 7 "??<4D&z';';$GGG &z'C'C'RGG  S NceZdZiZiZereeee e e e e eeeeeeedZer eeeeeeeeed Zidedddddd d d edd dddddd d deddd deddddddd d deddddddd d dedd dddddd d dedd ddddd dedd ddddd d edd ddddd! d"eddddddd d# d$edd% d&eddd' d(edddddd d) d*eddd+ d,edddddd d- d.eddd/ d0eddddd d1 id2edddddd d3 d4eddd5 d6edd7d8d9 d:edddddd d; d<eddddddd d= d>eddddddd d? d@edd dddddA dBedd ddd ddC dDedd ddddE dFeddG dHedddddd dI dJedddK dLedddddM dNedd dddO dPedddQ dRedd dddS dTedddd dddU idVedd dddddW dXedd dddddd dY dZedddddd d[ d\eddd] d^eddd_ d`eddda dbedd dddc ddedd ddde dfeddg dheddddi djedddk dleddddddd dm dnedddo dpeddddddd dq dreddddddd dds dtedddudv dwedddddd dx edddy edd dddz ed{d| eddd} edd d~dd eddddddd d eddddddd d edd7d dZ e!e"dde jGDZ$e% dde&de'e&de(e&e)e*ge+fffdZ,e%de&de&fdZ-e%de&fdZ.e%dde&de&de/fdZ0e%dde&de'e&de&fdZ1e% dde&de&de'e&de'e&de2df dZ3e% dde2dde&de4e&e)ffdZ5de&de&de+fdZ6y)FeaturesManager)default masked-lm causal-lm seq2seq-lmsequence-classificationtoken-classificationmultiple-choiceobject-detectionquestion-answeringimage-classificationimage-segmentationz masked-imsemantic-segmentation vision2seq-lm speech2seq-lm) rFrGrHrIrJrKrLrNrQalbertrFrGrJrLrKrNzmodels.albert.AlbertOnnxConfigr-bartzdefault-with-pastrHzcausal-lm-with-pastrIzseq2seq-lm-with-pastzmodels.bart.BartOnnxConfigbeitrOzmodels.beit.BeitOnnxConfigbertzmodels.bert.BertOnnxConfigzbig-birdz!models.big_bird.BigBirdOnnxConfigzbigbird-pegasusz/models.bigbird_pegasus.BigBirdPegasusOnnxConfig blenderbotz&models.blenderbot.BlenderbotOnnxConfigzblenderbot-smallz1models.blenderbot_small.BlenderbotSmallOnnxConfigbloomzmodels.bloom.BloomOnnxConfig camembertz$models.camembert.CamembertOnnxConfigclipzmodels.clip.CLIPOnnxConfigcodegenz models.codegen.CodeGenOnnxConfigconvbertz"models.convbert.ConvBertOnnxConfigconvnextz"models.convnext.ConvNextOnnxConfigz data2vec-textz&models.data2vec.Data2VecTextOnnxConfigzdata2vec-visionz(models.data2vec.Data2VecVisionOnnxConfigdebertaz models.deberta.DebertaOnnxConfigz deberta-v2z%models.deberta_v2.DebertaV2OnnxConfigdeitzmodels.deit.DeiTOnnxConfigdetrrMrPzmodels.detr.DetrOnnxConfig distilbertz&models.distilbert.DistilBertOnnxConfigelectraz models.electra.ElectraOnnxConfigflaubertz"models.flaubert.FlaubertOnnxConfiggpt2zmodels.gpt2.GPT2OnnxConfiggptjzmodels.gptj.GPTJOnnxConfigzgpt-neozmodels.gpt_neo.GPTNeoOnnxConfiggroupvitz"models.groupvit.GroupViTOnnxConfigibertzmodels.ibert.IBertOnnxConfigimagegptz"models.imagegpt.ImageGPTOnnxConfiglayoutlmz"models.layoutlm.LayoutLMOnnxConfig layoutlmv3z&models.layoutlmv3.LayoutLMv3OnnxConfiglevitzmodels.levit.LevitOnnxConfiglongt5zmodels.longt5.LongT5OnnxConfig longformerz&models.longformer.LongformerOnnxConfigmarianzmodels.marian.MarianOnnxConfigmbartzmodels.mbart.MBartOnnxConfig mobilebertz&models.mobilebert.MobileBertOnnxConfigz mobilenet-v1z)models.mobilenet_v1.MobileNetV1OnnxConfigz mobilenet-v2z)models.mobilenet_v2.MobileNetV2OnnxConfig mobilevitz$models.mobilevit.MobileViTOnnxConfigmt5zmodels.mt5.MT5OnnxConfigzm2m-100zmodels.m2m_100.M2M100OnnxConfigowlvitzmodels.owlvit.OwlViTOnnxConfig perceiverz$models.perceiver.PerceiverOnnxConfig poolformerz&models.poolformer.PoolFormerOnnxConfigrembertz models.rembert.RemBertOnnxConfigresnetzmodels.resnet.ResNetOnnxConfigrobertaz models.roberta.RobertaOnnxConfigroformerz"models.roformer.RoFormerOnnxConfig segformerrQz$models.segformer.SegformerOnnxConfig squeezebertz(models.squeezebert.SqueezeBertOnnxConfigzmodels.swin.SwinOnnxConfigzmodels.t5.T5OnnxConfigrRzzFeaturesManager.*s b2grCc#<K|]}|jywr)keys).0vs rA zFeaturesManager.*s?qQ?qsN model_type model_namer0c|j}|tjvrE|r|d|dn|}t|dt tjj d|dtj|S)a Tries to retrieve the feature -> OnnxConfig constructor map from the model type. Args: model_type (`str`): The model type to retrieve the supported features for. model_name (`str`, *optional*): The name attribute of the model object, only used for the exception message. Returns: The dictionary mapping each feature to a corresponding OnnxConfig constructor. z ()z is not supported yet. Only z' are supported. If you want to support z) please propose a PR or open up an issue.)lowerrE_SUPPORTED_MODEL_TYPEKeyErrorlistr)rrmodel_type_and_model_names rA%get_supported_features_for_model_typez5FeaturesManager.get_supported_features_for_model_type,s  %%' _BB BJT:,b A(FZd %,-._BBGGIJKL**45^`  44Z@@rCr@c&|jddS)Nr3r4)r:)r@s rAfeature_to_taskzFeaturesManager.feature_to_taskFs|R00rC frameworkc|dvrtd|d|dk(rts td|dk(rts tdyy) z Validates if the framework requested for the export is both correct and available, otherwise throws an exception. pttfzAOnly two frameworks are supported for ONNX export: pt or tf, but z was provided.rzOCannot export model to ONNX using PyTorch because no PyTorch package was found.rzUCannot export model to ONNX using TensorFlow because no TensorFlow package was found.N)r6r RuntimeErrorr )rs rA_validate_framework_choicez*FeaturesManager._validate_framework_choiceJse L (ST]S^^lm $ '9';pq q $ '8vw w(9 rCc  tj|}tj||dk(rtj}ntj}||vr6t d|dt tjj||S)ad Attempts to retrieve an AutoModel class from a feature name. Args: feature (`str`): The feature required. framework (`str`, *optional*, defaults to `"pt"`): The framework to use for the export. Returns: The AutoModel class corresponding to the feature. rzUnknown task: z. Possible values are )rErr_TASKS_TO_AUTOMODELS_TASKS_TO_TF_AUTOMODELSrrvalues)r@rr5task_to_automodels rAget_model_class_for_featurez+FeaturesManager.get_model_class_for_featureYs..w7229=   / D D  / G G  ( (  )?_EiEiEpEpEr@s?tu !&&rCmodelc`||Sddd}ddd}tjj|rtjjtjj |t rd}n^tjjtjj |t rd}ntdt d t d tjd ||d n%trd}ntrd}n td tjd||d|S)a Determines the framework to use for the export. The priority is in the following order: 1. User input via `framework`. 2. If local checkpoint is provided, use the same framework as the checkpoint. 3. Available framework in environment, with priority given to PyTorch Args: model (`str`): The name of the model to export. framework (`str`, *optional*, defaults to `None`): The framework to use for the export. See above for priority if none provided. Returns: The framework to use for the export. PyTorch TensorFlowrtorchtf2onnxrrzMCannot determine framework from given checkpoint location. There should be a z for PyTorch or z for TensorFlow.zLocal z model found.zKNeither PyTorch nor TensorFlow found in environment. Cannot export to ONNX.zFramework not requested. Using z to export to ONNX.) ospathisdirisfilejoinrr FileNotFoundErrorloggerinfor r OSError)rr framework_map exporter_maps rAdetermine_frameworkz#FeaturesManager.determine_frameworkts (   ( = %Y7 77== ww~~bggll5,?@  U4D EF '**68+,,<> KK&y!9 :-H I!#  " kll 5l96M5NNabcrC cache_dirrcZtj||}tj||} |j||}|S#t$r^|dk(r,t j d|j|d|}Y|St j d|j|d|}Y|SwxYw)a Attempts to retrieve a model from a model's name and the feature to be enabled. Args: feature (`str`): The feature required. model (`str`): The name of the model to export. framework (`str`, *optional*, defaults to `None`): The framework to use for the export. See `FeaturesManager.determine_framework` for the priority should none be provided. Returns: The instance of the model. )rrz=Loading TensorFlow model in PyTorch before exporting to ONNX.T)from_tfrz=Loading PyTorch model in TensorFlow before exporting to ONNX.)from_ptr)rErrfrom_pretrainedrrr)r@rrr model_classs rAget_model_from_featurez&FeaturesManager.get_model_from_features($77yI %AA'9U  ^///KE  ^D  [\#33E4S\3]  [\#33E4S\3]  ^sA7B*=)B*)B*cD|jjjdd}t|dd}tj ||}||vr't |jjd|d||jjtj||fS)aR Check whether or not the model has the requested features. Args: model: The model to export. feature: The name of the feature to check if it is available. Returns: (str) The type of the model (OnnxConfig) The OnnxConfig instance holding the model export properties. _-namer4)rz doesn't support feature z. Supported values are: )configrr:r9rErr6r)rr@rrmodel_featuress rAcheck_supported_model_or_raisez.FeaturesManager.check_supported_model_or_raises\\,,44S#> UFB/ (NNzfpNq . (<<**++DWIMefteuv ||&&(M(Mj(YZa(bbbrCc.tj||S)aU Gets the OnnxConfig for a model_type and feature combination. Args: model_type (`str`): The model type to retrieve the config for. feature (`str`): The feature to retrieve the config for. Returns: `OnnxConfig`: config for the combination )rEr)rr@s rA get_configzFeaturesManager.get_configs44Z@IIrCr)r)NN)rF)7__name__ __module__ __qualname__rrr rrrrr r"rrrrrrrr#r!r r$r&r%r*r+r,r'r(r)rBrsortedrrAVAILABLE_FEATURES staticmethodstrrdictrr rrrrtyperrrrtuplerrrrCrArErEUs   --/'I$C9 ;"?$C"?8%E36 ""//1'K$E;"A%G # p,   %  " < p *    !  " % 8  p, * -?[ -p2 *    %  " 8  3pF .    %  " ?  GpZ 5    !  " % M  [pp 0    !  "D qpB 6    !  "O CpT +    ! % ": Upf /    %  " B  gpz * 8 {pB -  > CpL .   %  " @ Mp^ .  "@ _ph 3   %  " D ipz 5  "G  {pH -   % " >  IpX 0   %  " C Ypj * -?[ kpp *   8  qp| 0   %  " D }pN -    %  " >  Opb .    %  " @  cpv *    ! % "8 wpH *    ! %8 IpZ -    ! %=  [pj . @ kpr +   %  " : spD . -?c EpJ .   % "@  KpX 0  % "D  Ypf + -?] gpl ,    "<  mpz 0    % "D {pL ,    "  !< Mp^ +    !  " % :  _pt 0   %  " D upF 2  "G G pP 2  "G Q pZ /  "B [ pd )    "6  e pr -    "=  s p@ , < A pH / "  %B  I pT 0 -?g U pZ -    %  " >  [ pn ,  "< o px -    %  " >  y pL .    % "  "@  M pb /  " #B  c pn 1   %  " F o p@ + -?[ )    "4  #= -k# * -?Y .    %>  *    %  " 6  2    %  " E  ,  : W pd  '=?qRgRnRnRp?q rs59AA%-c]A c8-. :;; <AA21111 xc x x'S'S'D''4/3/8C=/C//b^b -5c]NVWZm 5 6@MVc;<cGJc sH} cc0 Js JS JZ JrCrE)7r functoolsrrtypingrrrrr7r4r r r utilsr rrrrrr get_loggerrrtransformers.models.autorrrrrrrrrrrr r!r"r#r$r%r&r'r(r)r*r+r,warningrrrBrErrCrArs %;;DD;;?   H %"   O$5 NN 5@D/7} #x)*J67 78