
    sh                         S SK r S SKrS SKJrJr  S SKr\(       a  S SKr\R                  " S5      S 5       r " S S5      r	g)    N)TYPE_CHECKINGUnionc                  B     SS K n SS KnSSK Jn  g! [         a     gf = f)Nr   )torchTF)safetensorstransformersr   ImportError)r   r   safetensors_torchs      s/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/torch/onnx/_internal/fx/patcher.py has_safetensors_and_transformersr      s%    : s    
c                   .    \ rS rSrSrSS jrS rS rSrg)	ONNXTorchPatcher   a  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                   ^ ^ / T l         U 4S jn[        R                  T l        UT l        [        5       (       aO  SS KmSS KnSUU 4S jjnTR                  R                  T l	        UT l
        UR                  R                  T l        g g )Nc                 f   > TR                   R                  U 5        TR                  " U /UQ70 UD6$ )N)pathsappend
torch_load)fargskwargsselfs      r   torch_load_wrapper5ONNXTorchPatcher.__init__.<locals>.torch_load_wrapper<   s.    JJa ??16t6v66    r   c           	        > TR                   R                  U 5        0 nTR                  R                  U SUS9 nUR	                  5        H  n[        R
                  R                  5       nU(       d  UR                  U5      X$'   M=  UR                  U5      n[        R                  " [        UR                  5       5      TR                  R                  UR                  5       5      S9X$'   M     S S S 5        U$ ! , (       d  f       U$ = f)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_wrapper@ONNXTorchPatcher.__init__.<locals>.safetensors_load_file_wrapperL   s    

!!(+ &&00V 1 VVX$)MM$B$B$D	(()QFI+,;;q>L(- %l&<&<&> ?&1&7&7&A&A$0$:$:$<'")FI &   s   B8C<<
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   r1   r   s   `   @r   __init__ONNXTorchPatcher.__init__8   sy    :<
	7  ** #5+-- * 0;/@/@/J/JD,7TD4++:: ;7 .r   c                 8   U R                   [        l        [        R                  R                  R
                  U l        [        R                  " [        R                  R                  R
                  5      n[        R                  S4U;  a!  UR                  [        R                  S45        U[        R                  R                  l        [        5       (       a?  SS KnSS KnU R                  UR                  l        U R                  UR                   l        g g )N__getitem__r   )r   r   r4   fx_symbolic_trace_wrapped_methods_to_patch2torch_fx__symbolic_trace__wrapped_methods_to_patchcopydeepcopyTensorr   r   r   r   r7   r5   r8   r9   )r   desired_wrapped_methodsr   r   s       r   	__enter__ONNXTorchPatcher.__enter__g   s    ,,
 HH$$>> 	? #'--HH$$>>#
 LL-(0GG $**ELL-+HI=T  :+--*.*R*RK'88 ''6 .r   c                    U R                   [        l        U R                  [        R                  R
                  l        [        5       (       a?  SS KnSS K	nU R                  UR                  l        U R                  UR                  l        g g )Nr   )r   r   r4   rB   r?   r@   rA   r   r   r   r6   r5   r:   r8   r9   )r   exc_type	exc_value	tracebackr   r   s         r   __exit__ONNXTorchPatcher.__exit__   si    __
CC 	  : ,--*.*J*JK'?? ''6 .r   )r   r6   r7   rB   r   r   r:   )returnN)	__name__
__module____qualname____firstlineno____doc__r;   rG   rM   __static_attributes__ r   r   r   r      s    8-^8r   r   )
rC   	functoolstypingr   r   r   io	lru_cacher   r   rV   r   r   <module>r[      sD      '   T	 	t tr   