Ë
    [^(h¡  ã                   ó|   — d dl Z d dlZd dlmZmZ d dlZerd dlZ ej                  d«      d„ «       Z G d„ d«      Z	y)é    N)ÚTYPE_CHECKINGÚUnionc                  ó@   — 	 dd l } dd l}ddl m} y# t        $ r Y yw xY w)Nr   )ÚtorchTF)ÚsafetensorsÚtransformersr   ÚImportError)r   r   Úsafetensors_torchs      ú]/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/torch/onnx/_internal/fx/patcher.pyÚ has_safetensors_and_transformersr      s%   € ðãÛÝ:àøÜò Ùðús   ‚ ‘	œc                   ó$   — e Zd ZdZdd„Zd„ Zd„ Zy)ÚONNXTorchPatcheraH  Context manager to temporarily patch PyTorch during FX-to-ONNX export.

    This class is a collection of "patches" required by FX-to-ONNX exporter.

    This context overrides several torch functions to support symbolic
    export of large scale models.

    torch.load:
        This function is patched to record the files PyTorch stores model
        parameters and buffers. Downstream FX-to-ONNX exporter can create
        initializers from these files.
    torch.fx._symbolic_trace._wrapped_methods_to_patch:
        This list is extended with (torch.Tensor, "__getitem__") so that
        weight[x, :, y] becomes exportable with torch.fx.symbolic_trace.
    safetensors.torch.load_file:
        This function is patched to allow safetensors to be loaded within
        FakeTensorMode. Remove after https://github.com/huggingface/safetensors/pull/318

    Search for ONNXTorchPatcher in test_fx_to_onnx_with_onnxruntime.py for
    example usage.

    TODO: Should this really be a global patcher? Can we make it a local patcher?
        A reason for splitting this into several patchers is to patch one part of the code
        as a collateral damage of patching another part of the code. For example, we
        for tracing model with torch._dynamo.export, we don't need to patch
        `torch.fx._symbolic_trace._wrapped_methods_to_patch`
    Nc                 ó  ‡ ‡— g ‰ _         ˆ fd„}t        j                  ‰ _        |‰ _        t        «       rMdd lŠdd l}dˆˆ fd„	}‰j                  j                  ‰ _	        |‰ _
        |j                  j                  ‰ _        y y )Nc                 óf   •— ‰j                   j                  | «        ‰j                  | g|¢­i |¤ŽS )N)ÚpathsÚappendÚ
torch_load)ÚfÚargsÚkwargsÚselfs      €r   Útorch_load_wrapperz5ONNXTorchPatcher.__init__.<locals>.torch_load_wrapper<   s0   ø€ à�J‰J×Ñ˜aÔ à"�4—?‘? 1Ð6 tÒ6¨vÑ6Ð6ó    r   c           	      ó  •— ‰j                   j                  | «       i }‰j                  j                  | d|¬«      5 }|j	                  «       D ]   }t        j
                  j                  «       }|s|j                  |«      ||<   Œ8|j                  |«      }t        j                  t        |j                  «       «      ‰j                  j                  |j                  «       «      ¬«      ||<   Œ¢ 	 d d d «       |S # 1 sw Y   |S xY w)NÚpt)Ú	frameworkÚdevice)Údtype)r   r   r   Ú	safe_openÚkeysÚ_guardsÚdetect_fake_modeÚ
get_tensorÚ	get_sliceÚemptyÚtupleÚ	get_shapeÚ	_getdtypeÚ	get_dtype)	Úfilenamer   Úresultr   ÚkÚ	fake_modeÚempty_tensorr   r   s	          €€r   Úsafetensors_load_file_wrapperz@ONNXTorchPatcher.__init__.<locals>.safetensors_load_file_wrapperL   sæ   ø€ à—
‘
×!Ñ! (Ô+Ø�Ø ×&Ñ&×0Ñ0Ø¨°Vð 1ó ð àØŸV™V›Xò ˜Ü$)§M¡M×$BÑ$BÓ$D˜	Ù(Ø()¯©°Q«˜F 1šIà+,¯;©;°q«>˜LÜ(-¯©Ü % l×&<Ñ&<Ó&>Ó ?Ø&1×&7Ñ&7×&AÑ&AØ$0×$:Ñ$:Ó$<ó'"ô)˜F 1šIñ÷ð �÷ð �ús   ½B4C<Ã<D)Úcpu)r   r   Úloadr   r   r   r   r   Ú	load_fileÚsafetensors_torch_load_fileÚ#safetensors_torch_load_file_wrapperÚmodeling_utilsÚsafe_load_fileÚ*transformers_modeling_utils_safe_load_file)r   r   r   r/   r   s   `   @r   Ú__init__zONNXTorchPatcher.__init__8   sq   ù€ à:<ˆŒ
ô	7ô  Ÿ*™*ˆŒð #5ˆÔä+Ô-ÛÛöð* 0;×/@Ñ/@×/JÑ/JˆDÔ,Ø7TˆDÔ4à×+Ñ+×:Ñ:ð Õ;ð7 .r   c                 ó(  — | j                   t        _        t        j                  j                  j
                  | _        t        j                  t        j                  j                  j
                  «      }t        j                  df|vr!|j                  t        j                  df«       |t        j                  j                  _        t        «       r?dd l}dd l}| j                  |j                  _        | j                  |j                   _        y y )NÚ__getitem__r   )r   r   r1   ÚfxÚ_symbolic_traceÚ_wrapped_methods_to_patchÚ2torch_fx__symbolic_trace__wrapped_methods_to_patchÚcopyÚdeepcopyÚTensorr   r   r   r   r4   r2   r5   r6   )r   Údesired_wrapped_methodsr   r   s       r   Ú	__enter__zONNXTorchPatcher.__enter__g   sË   € Ø×,Ñ,ŒŒ
ô �H‰H×$Ñ$×>Ñ>ð 	Ô?ô #'§-¡-Ü�H‰H×$Ñ$×>Ñ>ó#
Ðô �L‰L˜-Ð(Ð0GÑGð $×*Ñ*¬E¯L©L¸-Ð+HÔIØ=TŒ�‰× Ñ Ô:ä+Ô-ÛÛà*.×*RÑ*RˆK×ÑÔ'à×8Ñ8ð ×'Ñ'Õ6ð .r   c                 ó  — | j                   t        _        | j                  t        j                  j
                  _        t        «       r?dd l}dd l	}| j                  |j                  _        | j                  |j                  _        y y )Nr   )r   r   r1   r>   r;   r<   r=   r   r   r   r3   r2   r7   r5   r6   )r   Úexc_typeÚ	exc_valueÚ	tracebackr   r   s         r   Ú__exit__zONNXTorchPatcher.__exit__ƒ   sf   € Ø—_‘_ŒŒ
à×CÑCô 	�‰× Ñ Ô:ô ,Ô-ÛÛà*.×*JÑ*JˆK×ÑÔ'à×?Ñ?ð ×'Ñ'Õ6ð .r   )ÚreturnN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r8   rC   rH   © r   r   r   r      s   „ ñó8-ò^ó8r   r   )
r?   Ú	functoolsÚtypingr   r   r   ÚioÚ	lru_cacher   r   rN   r   r   ú<module>rS      sF   ðã Û ß 'ã ñ Ûð €×Ñ�TÓñ	ó ð	÷tò tr   