
    Ph                     |    d dl Z d dlZd dlmZmZ d dlZ	 d dlZd dlZd dlmZ dZ	 G d d      Zy# e
$ r dZ	Y w xY w)    N)ListUnion)torchTFc                   "    e Zd ZdZd Zd Zd Zy)ONNXTorchPatcheraR  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._util._rebuild_tensor:
        This function is patched to avoid creating real tensors during
        model loading. FakeTensor's are created instead. Real tensors
        cannot be fitted into single machine's memory for the targeted
        model scale.
    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`
    c                 H    g  _          fd}d }t        j                   _        t        j                  j
                   _        | _        | _        t        rJdd}t        j                  j                   _        | _        t        j                  j                    _        y y )Nc                 f    j                   j                  |         j                  | g|i |S N)pathsappend
torch_load)fargskwargsselfs      jC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/onnx/_internal/fx/patcher.pytorch_load_wrapperz5ONNXTorchPatcher.__init__.<locals>.torch_load_wrapper9   s0    JJa "4??16t6v66    c                     ddl m} ddlm} ddlm} d } |       }t        ||      r, |       5   || |||      }	d d d        |j                  	      S  || |||      S # 1 sw Y   %xY w)Nr   )FakeTensorMode)no_dispatch)_get_current_dispatch_modec                     t        j                  g | j                  | j                  j                        }|j                  | j                  |||      S )N)dtypedevice)r   tensorr   _untyped_storager   set_)storagestorage_offsetsizestridets        r   _rebuild_real_tensorzdONNXTorchPatcher.__init__.<locals>.torch__util__rebuild_tensor_wrapper.<locals>._rebuild_real_tensorD   sD    LLgmmG4L4L4S4S vvg66fUUr   )torch._subclasses.fake_tensorr   torch.utils._mode_utilsr   torch.utils._python_dispatchr   
isinstancefrom_tensor)
r   r    r!   r"   r   r   r   r$   moder#   s
             r   #torch__util__rebuild_tensor_wrapperzFONNXTorchPatcher.__init__.<locals>.torch__util__rebuild_tensor_wrapper?   sk    D;OV ./D$/
 !],WndFSA #''**'vNN	 #]s   A  A)c           	         i }t         j                  j                  | d|      5 }|j                         D ]  }t        j                  j                         }|s|j                  |      ||<   8|j                  |      }t        j                  t        |j                               t         j                  j                  |j                                     ||<    	 d d d        |S # 1 sw Y   |S xY w)Npt)	frameworkr   )r   )safetensorsr   	safe_openkeys_guardsdetect_fake_mode
get_tensor	get_sliceemptytuple	get_shape	_getdtype	get_dtype)filenamer   resultr   k	fake_modeempty_tensors          r   safetensors_load_file_wrapperz@ONNXTorchPatcher.__init__.<locals>.safetensors_load_file_wrapper`   s     &&00V 1 VVX$)MM$B$B$D	(()QF1I+,;;q>L(- %l&<&<&> ?&1&7&7&A&A$0$:$:$<'")F1I &  s   B8C((C2)cpu)r   r   loadr   _utils_rebuild_tensortorch__util_rebuild_tensorr   "torch__util_rebuild_tensor_wrapper has_safetensors_and_transformersr/   	load_filesafetensors_torch_load_file#safetensors_torch_load_file_wrappertransformersmodeling_utilssafe_load_file*transformers_modeling_utils_safe_load_file)r   r   r+   r@   s   `   r   __init__zONNXTorchPatcher.__init__5   s    :<
	7	O0  ***/,,*F*F' #52U/+& 0;/@/@/J/JD,7TD4++:: ;/ ,r   c                 ^   | j                   t        _        | j                  t        j                  _        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?| j                  t         j                  _        | j                  t$        j&                  _        y y )N__getitem__)r   r   rB   rF   rC   rD   fx_symbolic_trace_wrapped_methods_to_patch2torch_fx__symbolic_trace__wrapped_methods_to_patchcopydeepcopyTensorr   rG   rJ   r/   rH   rK   rL   rM   )r   desired_wrapped_methodss     r   	__enter__zONNXTorchPatcher.__enter__y   s    ,,
'+'N'N$ HH$$>> 	? #'--HH$$>>#
 LL-(0GG $**ELL-+HI=T  :+*.*R*RK'88 ''6 ,r   c                 H   | j                   t        _        | j                  t        j                  _        | j                  t        j                  j                  _	        t        r?| j                  t        j                  _        | j                  t        j                   _        y y r
   )r   r   rB   rE   rC   rD   rU   rR   rS   rT   rG   rI   r/   rH   rN   rK   rL   rM   )r   exc_type	exc_value	tracebacks       r   __exit__zONNXTorchPatcher.__exit__   so    __
'+'F'F$CC 	  : ,*.*J*JK'?? ''6 ,r   N)__name__
__module____qualname____doc__rO   rZ   r_    r   r   r   r      s    BBH4
r   r   )rV   iotypingr   r   r   r/   rK   safetensors_torchrG   ImportErrorr   rd   r   r   <module>ri      sH     	  -6'+$
J J	  -',$-s   1 ;;