/usr/local/lib/python3.6/site-packages/transformers/utils/__pycache__
NameSizeModeActions
doc.cpython-36.pyc345520644editdlrm
dummy_detectron2_objects.cpython-36.pyc7650644editdlrm
dummy_flax_objects.cpython-36.pyc476390644editdlrm
dummy_pt_objects.cpython-36.pyc2099100644editdlrm
dummy_pytorch_quantization_and_torch_objects.cpython-36.pyc38530644editdlrm
dummy_scatter_objects.cpython-36.pyc20440644editdlrm
dummy_sentencepiece_and_speech_objects.cpython-36.pyc6410644editdlrm
dummy_sentencepiece_and_tokenizers_objects.cpython-36.pyc4580644editdlrm
dummy_sentencepiece_objects.cpython-36.pyc76300644editdlrm
dummy_speech_objects.cpython-36.pyc6130644editdlrm
dummy_tf_objects.cpython-36.pyc985960644editdlrm
dummy_timm_and_vision_objects.cpython-36.pyc16240644editdlrm
dummy_timm_objects.cpython-36.pyc16470644editdlrm
dummy_tokenizers_objects.cpython-36.pyc147460644editdlrm
dummy_vision_objects.cpython-36.pyc65940644editdlrm
fx.cpython-36.pyc179760644editdlrm
fx_transformations.cpython-36.pyc95630644editdlrm
generic.cpython-36.pyc104930644editdlrm
hp_naming.cpython-36.pyc37510644editdlrm
hub.cpython-36.pyc316490644editdlrm
import_utils.cpython-36.pyc227740644editdlrm
logging.cpython-36.pyc95570644editdlrm
model_parallel_utils.cpython-36.pyc19760644editdlrm
notebook.cpython-36.pyc121900644editdlrm
sentencepiece_model_pb2.cpython-36.pyc128230644editdlrm
versions.cpython-36.pyc30620644editdlrm
__init__.cpython-36.pyc46450644editdlrm
Edit: /usr/local/lib/python3.6/site-packages/transformers/utils/__pycache__/fx.cpython-36.pyc (17976B)
3 EgZ @sddlZddlZddlZddlZddlmZddlmZmZm Z m Z m Z m Z m Z mZddlZddlmZddlmZddlmZmZmZmZmZddlmZdd lmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,dd l-m.Z.dd l/m0Z0m1Z1dd l2m3Z3e,j4e5Z6d7e e)e ee7e e7fe e e*d ddZ8dddddddddddg Z9gZ:x:e9D]2Z;ee8fe;ne:j>e8e;qnWe(gZ?e@eAeBeCe:e?dddZDGdddeZEed efed efd!d"d#ZFe7e e7ed effed efd$d%d&ZGeEe7e7eHd'd(d)ZIe e7ed effd*d+d,ZJd8eKeKe e eKd/d0d1ZLGd2d3d3eZMd9e*e e e7ed4d5d6ZNdS):N) ModuleType)AnyCallableDictIterableListOptionalTypeUnion)version)nn)Graph GraphModuleNodeProxyTracer)Argument)CONFIG_MAPPINGMODEL_FOR_CAUSAL_LM_MAPPING&MODEL_FOR_IMAGE_CLASSIFICATION_MAPPINGMODEL_FOR_MASKED_LM_MAPPING!MODEL_FOR_MULTIPLE_CHOICE_MAPPING*MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPINGMODEL_FOR_PRETRAINING_MAPPING$MODEL_FOR_QUESTION_ANSWERING_MAPPING&MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING)MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING&MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING MODEL_MAPPINGGPT2DoubleHeadsModelPretrainedConfigPreTrainedModelXLNetForQuestionAnsweringlogging) get_values)TORCH_FX_REQUIRED_VERSIONis_torch_fx_available)importlib_metadata) model_namesupported_tasksreturnc Csxt|}ttttttttt t t d }|dkr4|j }t |trD|g}g}x*|D]"}||j|d}|rN|j|qNW|S)N) defaultZ pretrainingznext-sentence-predictionz masked-lmz causal-lmz seq2seq-lmzmultiple-choicezquestion-answeringzsequence-classificationztoken-classificationzimage-classification)rrrrrrrrrrrrkeys isinstancestrgetappend)r)r*Zmodel_config_classZ task_mappingZ model_classestask model_classr4?/usr/local/lib/python3.6/site-packages/transformers/utils/fx.py!_generate_supported_model_classes8s,  r6ZalbertZbertZ distilbertZ mobilebertZelectraz megatron-bertZgpt2ZgptjZgpt_neoZt5ZrobertacCs|jS)N)__name__)cr4r4r5xsr9)keycsxeZdZdZdeeedfdd ZeddZ dd Z d d Z fd d Z ddZ fddZdfdd ZZS)HFProxyz Proxy that is able to provide the proper ranks, shapes and boolean values during symbolic tracing by implementing the dim, size and __bool__ methods. It can be easily extended by either adding new methods or extending the existing ones. N)nodetracercsNtj||dt|drJ|jdk rJ|jjj|_t|jjjj|_d|_ dS)N)r=r=) super__init__hasattrr=rootdevicenext parametersdtypecache)selfr<r=) __class__r4r5r?s  zHFProxy.__init__cCs|jS)N)size)rGr4r4r5shapesz HFProxy.shapecCsdS)Nr4)rGr:valuer4r4r5 __setitem__szHFProxy.__setitem__cCsdS)NFr4)rGr:r4r4r5 __contains__szHFProxy.__contains__cs2|jdk r|j|kSt|tr"dStj|SdS)NT)rFr.r;r>__eq__)rGother)rHr4r5rNs    zHFProxy.__eq__cCs ||k S)Nr4)rGrOr4r4r5__ne__szHFProxy.__ne__csR|jdk rFt|jtr|jSt|jtjttfr:t|jStj |Stj |S)N) rFr.inttorchSizelisttuplelenr>__len__)rG)rHr4r5rWs    zHFProxy.__len__cs tj||||d}|j|_|S)N)argskwargs)r>__torch_function__rF)rGZ orig_methodtypesrXrYproxy)rHr4r5rZszHFProxy.__torch_function__)N)NN)r7 __module__ __qualname____doc__rrrr?propertyrJrLrMrNrPrWrZ __classcell__r4r4)rHr5r;|s   r;.)funcr+cstjfdd}|S)zcWrapper that marks func as a leaf function, meaning that it will not be traced through by HFTracer.cs ||S)Nr4)rXrY)rbr4r5wrappersz"_function_to_leaf..wrapper) functoolswraps)rbrcr4)rbr5_function_to_leafsrf) func_namemappingr+cs tjfdd}|S)Ncs||S)Nr4)rXrY)rgrhr4r5rcsz&_function_leaf_getter..wrapper)rdre)rgrhrcr4)rgrhr5_function_leaf_gettersri)r\ method_name cache_name return_proxycsJttj|tjfdd}||_|j||j}t|||dS)z Helper function that sets a recorded torch.Tensor method as a HFProxy method that will use the recorded values during symbolic tracing. csDt|djj}|jd}r@|djd||d}||_|S|S)Nr)rXrY)getattrr=rApoprZrF)rXrYrFresr\)rkoriginal_methodrlr4r5methods z-_create_recorded_proxy_method..methodN) rmrRTensorrdrer7__get__rHsetattr)r\rjrkrlrqZ bound_methodr4)rkrprlr5_create_recorded_proxy_methods  ru)original_methodscCs(x"|jD]\}}ttj||q WdS)z]Helper function that resets the monkey patched torch.Tensor methods to their original values.N)itemsrtrRrr)rvnamerqr4r4r5_reset_tensor_methodssry )lowhighforbidden_valuescCs6|dkr g}tj||}x||kr0tj||}qW|S)N)randomrandint)r|r}r~rKr4r4r5_generate_random_ints   rcseZdZdZddddZddlmZeddd d d hejd hiZ e ffdffd d Z e e dddZd8eedddZeeedddZee e eedddZeee dddZee eeee ejfddd Zd9eee eee d"d#d$Zd%d&Zed'd(d)Zd:eeee e feee e!d*fd+d, Z"e#j$e d-d.d/Z%e#j$e d-d0d1Z&e#j$e ed2fd3d4 Z'e e(d5fd6d7 Z)Z*S);HFTracerz Tracer that is able to symbolically trace models from the library. To do that, it uses the HFProxy instead of the regular PyTorch torch.fx.Proxy. FT)__bool__rIZdimr)modeling_utilsZarangezerosZonesZ full_likeZeyeZ*create_extended_attention_mask_for_decodercsi|_x0|jjD]"\}}x|D]}|j||q WqWtj|||dtsvtjt jd}t d|dt dd|_ d|_ dS)N)autowrap_modulesautowrap_functionsenable_cpatchingrRz6Found an incompatible version of torch. Found version z, but only version z is supported.)_leaf_functions_register_FUNCTIONS_TO_AUTOWRAPrw_register_leaf_functionr>r?r'r parser( ImportErrorr& prev_modulerecorded_methods)rGrrrmodulenamesrxZ torch_version)rHr4r5r?s  zHFTracer.__init__)rrxcCs,t||}t|}t|_|||f|j|<dS)z@Registers the function called name in module as a leaf function.N)rmrfr7r]r)rGrrx orig_func patched_funcr4r4r5rs z HFTracer._register_leaf_function)rArestorecCsvxp|jD]f}|j|\}}}|r>|jjjj|t|||q||jjj|<t||jjj}t|_t|||qWdS)z-Patches leaf functions specifically for root.N) rrHforward __globals__rnrtrir7r])rGrArrxrrrZ leaf_getterr4r4r5_patch_leaf_functions_for_roots z'HFTracer._patch_leaf_functions_for_root) module_idsr+cCsRtj}xD|rL|dkrdS|jjdd}t||krD|j|drDdS|j}q WdS)z Finds out if the method (that is being recorded) is called inside a leaf module, this allows to not record outputs that will not be encountered by the tracer. NFrGzNot used anywayT)inspect currentframef_localsr0idis_leaf_modulef_back)rGrrrr4r4r5 _method_is_called_in_leaf_module)s z)HFTracer._method_is_called_in_leaf_module)modelrjrkrcs.ttj|tjfdd}|S)z[Helper function that wraps a torch.Tensor method to record its outputs during forward pass.csLjr||Sts*tgt}||}|j||S)N)rr@rtrmr1)rXrYrFro)rkrqrrrGr4r5wrapped?s       z:HFTracer._wrap_method_for_model_recording..wrapped)rmrRrrrdre)rGrrjrkrrr4)rkrqrrrGr5 _wrap_method_for_model_recording9s  z)HFTracer._wrap_method_for_model_recording)r method_namesc Csi}i}tdd|jD}x|D]}d|}|||<ttj|sZtjd|dq$ttj|||<ttj||j |||||dkr$tjj |d<ttjdt ttj|q$W||fS)z Helper function that patches torch.Tensor methods (specified by the method_names list) to record model inference before symbolic tracing. css|]}t|VqdS)N)r).0modr4r4r5 SszLHFTracer._monkey_patch_tensor_methods_for_model_recording..Zcache_z"torch.Tensor has no method called z, skipping patching.rIrJ) setmodulesr@rRrrloggerinformrtrrJr`)rGrr cache_namesrvrrjrkr4r4r50_monkey_patch_tensor_methods_for_model_recordingLs$    z9HFTracer._monkey_patch_tensor_methods_for_model_recording)r input_namerJr+c Cs`|j}|j}i}|d kr |d}|ttkrFtj|tj|d|d<n|tttfkrtj|tj|d|d<tj|tj|d|d<n|tt tt tt krtj|tj|d|d<nR|tt tt tttttttfkrtj|tj|d|d<nt|dnPd|ks d|kr8tj|tj|d||<n$||jjg}tj|tj|d||<|S) z4Generates dummy input for model inference recording.labelsstart_positions end_positionsr)rErBz not supported yet.maskids)rrr)rHrBr%rrRrlongrr#rrrrrrrrr NotImplementedErrorconfigZ hidden_sizefloat) rGrrrJr3rBZ inputs_dict batch_sizeZshape_with_hidden_sizer4r4r5_generate_dummy_inputgs>     zHFTracer._generate_dummy_inputN)r input_namesrc s|dkr|j}t}t}||g}jttkrHtddd}|jd|i}x |D]} |j|j| |qRW|j|\} } | |_ f|t | fdd| j D|_ dS)z Records torch.Tensor method outputs (specified by method_names) that will then be used during symbolic tracing. Nr)r|r}cs i|]\}}t|r||qSr4)r@)rrjrk)rr4r5 sz#HFTracer.record..) _DEFAULT_METHODS_TO_RECORDrrHr%rinsertupdaterrrvryrwr) rGrrrrZsequence_lengthrJZ num_choicesinputsrrrvr4)rr5records"     zHFTracer.recordcCst|tjjrRxB|jjD]4\}}||kr||krF|jd|fi||<||SqWt|tjrxB|jjD]4\}}||krj||kr|jd|fi||<||SqjW|S)NZget_attr) r.rRr ParameterrAZnamed_parametersZ create_proxyrrZ named_buffers)rGattrZattr_valZparameter_proxy_cachenpr4r4r5_module_getattrs   zHFTracer._module_getattr)r<cCsDt||}|jr@x.|jjD] \}}|j|}t||||qW|S)N)r;rrwrru)rGr<rrjrkrlr4r4r5r\s   zHFTracer.proxy)rA concrete_argsrr+cs|dkr i}tj|j}|jj|j}|j|||ddd|jjD}|jj t dd|D|j |t j ||d|_|j |ddt|jx8|jjD],}|jdkr|j|krf|_q|jj|qW|jS) N)rcSsg|]\}}}|qSr4r4)r_Zpatchedr4r4r5 sz"HFTracer.trace..cSsg|] }t|qSr4)r)rfr4r4r5rs)rT)r placeholder)r signaturerrDr-rrvaluesZ_autowrap_function_idsrrrr>tracegraphryrvZnodesoptargetrXZ erase_node)rGrArrsigrrr<)rHr4r5rs"     zHFTracer.trace)rr+cCsXd}|jjj}|d|}x&t|j|rD|d|}|d7}q W|jj|||S)zb Helper method which tries to insert a module that was not declared as submodule. rrr)rHr7lowerr@rAZ add_module)rGridxmod_namepathr4r4r5_insert_module_as_submodules  z$HFTracer._insert_module_as_submodulecCst|drT|jrT|jj|}|dkr.|j|}|dkrJtd|jd||_|Sx(|jjD]\}}||kr`||_|Sq`W|j|}|dkrtd|jd||_|SdS)ag Helper method to find the qualified name of `mod` in the Module hierarchy of `root`. For example, if `root` has a submodule named `foo`, which has a submodule named `bar`, passing `bar` into this function will return the string "foo.bar". Args: mod (str): The `Module` to retrieve the qualified name for. submodule_pathsNz Module named z is not installed as a submodulezModule ) r@rr0r NameErrorZ _get_namerrAZ named_modules)rGrrrrr4r4r5path_of_modules"    zHFTracer.path_of_module)mmodule_qualified_namer+cs |jjd}| otj||S)Nztorch.nn.modules.loss)r] startswithr>r)rGrrZis_loss_module)rHr4r5r!s zHFTracer.is_leaf_module)ar+cs&t|trtjt|Stj|S)N)r.ranger> create_argrT)rGr)rHr4r5r%s zHFTracer.create_arg)F)N)NN)+r7r]r^r_rZ transformersrrRZModuleUtilsMixinrmathr?rr/rr"boolrrrQrrrrrrrrrrrrr\rr rr Modulerrrrrrar4r4)rHr5rs.     *  $ r)rrr+csdkr|jjtj|j}fdd|jjD}t|tsldj ddtD}t d|j j d|t }|j||d }tjj||}|S) ap Performs symbolic tracing on the model. Args: model ([`PretrainedModel`]): The model to trace. input_names (`List[str]`, *optional*): The names of the inputs of the traced model. If unset, model.dummy_inputs.keys() are used instead. Returns: `torch.fx.GraphModule`: A GraphModule constructed by recording operations seen while tracing the model. Example: ```python from transformers.utils.fx import symbolic_trace traced_model = symbolic_trace(model, input_names=["input_ids", "attention_mask", "token_type_ids"]) ``` Ncs i|]}|jkr|j|jqSr4)rxr,)rr)rr4r5rHsz"symbolic_trace..z, css|] }|jVqdS)N)r7)rclsr4r4r5rKsz!symbolic_trace..zModel z) is not supported yet, supported models: )r)Z dummy_inputsr-rrrrDrr._SUPPORTED_MODELSjoinrrHr7rrrRZfxr)rrrrZsupported_model_namesr=Z traced_graphZtracedr4)rr5symbolic_trace+s   r)N)rzr{N)N)Ordrrrr[rtypingrrrrrrr r rR packagingr r Ztorch.fxr rrrrZ torch.fx.noderrrrrrrrrrrrrr r!r"r#r$Z models.autor%utilsr&r'Zutils.versionsr( get_loggerr7rr/r6Z(_REGULAR_SUPPORTED_MODEL_NAMES_AND_TASKSZ_REGULAR_SUPPORTED_MODELSitemr.dictextendZ_SPECIAL_SUPPORTED_MODELSrUsortedrTrrr;rfrirruryrQrrrr4r4r4r5s^ (   L      3 * C