
    sg                         d dl Z d dlZd dlmZ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)List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      R/var/www/html/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    JJa "4??16t6v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  VVX $)MM$B$B$D	(()QF1I+,;;q>L(- %l&<&<&> ?&1&7&7&A&A$0$:$:$<'")F1I  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	   r0   r   s   `   @r   __init__zONNXTorchPatcher.__init__8   sq    :<
	7  ** #5+-* 0;/@/@/J/JD,7TD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   r2   fx_symbolic_trace_wrapped_methods_to_patch2torch_fx__symbolic_trace__wrapped_methods_to_patchcopydeepcopyTensorr   r   r   r	   r5   r3   r6   r7   )r   desired_wrapped_methodsr   r	   s       r   	__enter__zONNXTorchPatcher.__enter__g   s    ,,
 HH$$>> 	? #'--HH$$>>#
 LL-(0GG $**ELL-+HI=T  :+-*.*R*RK'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   r2   r?   r<   r=   r>   r   r   r	   r4   r3   r8   r6   r7   )r   exc_type	exc_value	tracebackr   r	   s         r   __exit__zONNXTorchPatcher.__exit__   sf    __
CC 	  : ,-*.*J*JK'?? ''6 .r   )returnN)__name__
__module____qualname____doc__r9   rD   rI    r   r   r   r      s    8-^8r   r   )r@   	functoolstypingr   r   r   r   io	lru_cacher   r   rO   r   r   <module>rT      sI      - -   T	 	t tr   