o
    I&i                     @   s   d dl mZ d dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZmZ d dlmZ e	d	Zeed
d Zeedd Zejdd Zeejeedd eejdd Zdd Zedd ZdS )    )contextmanagerN)DispatchKey)autograd_not_implemented)HigherOrderOperator)FakeTensorMode)ProxyTorchDispatchModetrack_tensor_tree)_pytree_export_tracepointc                 O   sP   | j s
t|i |S t| jj||f\}}| jdt||}t||d | jdS )Ncall_function)Zconstanttracer)Zenable_tracingr
   pytreeZtree_mapr   Zunwrap_proxyZcreate_proxyr   )modeargskwargsZp_argsZp_kwargsproxy r   AC:\wamp64\www\opt\env\Lib\site-packages\torch/_export/wrappers.pyexport_tracepoint_dispatch_mode   s   r   c                 O   s,   | 
 |W  d    S 1 sw   Y  d S Nr   )r   r   r   r   r   r   "export_tracepoint_fake_tensor_mode   s   $r   c                 O   sX   |  |}|  |}|   t|i |}| |W  d    S 1 s%w   Y  d S r   )Zunwrap_tensorsZredispatch_to_nextr
   Zwrap_tensors)ctxr   r   Zunwrapped_argsZunwrapped_kwargsoutr   r   r   export_tracepoint_functional!   s   


$r   T)Zdeferred_errorc                  O   s   | S r   r   )r   r   r   r   r   export_tracepoint_cpu0   s   r   c                    s   t | tjjs	J dksJ | }dD ]}t||s$td t||}qfddd|jvs7J |j	dd   fd	d
}|
|t||_	|S )N .z Couldn't find submodule at path c                    s   |  vsJ ||d | < d S )N)in_specout_specr   )pathr   r   )module_call_specsr   r   update_module_call_signatures>   s   z6_wrap_submodule.<locals>.update_module_call_signaturesforwardc                 S   s:   | D ]}t |tjttttfs|d u std| qd S )NzGOnly Tensors or scalars are supported as pytree flattened inputs, got: )
isinstancetorchZTensorstrintfloatboolAssertionError)	flat_argsar   r   r   check_flattenedE   s   z(_wrap_submodule.<locals>.check_flattenedc                    s   t ||f\}} | t|dd}t ||\}}|i |}t |\}} | t|dd}|| t ||S )NZmodule_call_inputs)kindr   Zmodule_call_outputs)r   Ztree_flattenr
   Ztree_unflatten)selfr   r   r*   r   resZflat_resr   )r,   r   r!   wrapped_forwardr   r   wrapperL   s   z _wrap_submodule.<locals>.wrapper)r#   r$   nnModulesplithasattrRuntimeErrorgetattr__dict__r"   __get__type)modr   r    	submodulenamer1   r   )r,   r    r   r!   r0   r   _wrap_submodule5   s   
r>   c              	   c   sV    g }z|D ]}| t| || qd V  W |D ]}|jd= qd S |D ]}|jd= q#w )Nr"   )appendr>   r8   )fZpreserve_signatureZmodule_call_signaturestasksr   r<   r   r   r   _wrap_submodules\   s   

rB   )
contextlibr   r$   Ztorch._custom_opsZtorch._Cr   Ztorch._higher_order_ops.utilsr   Z
torch._opsr   Ztorch._subclasses.fake_tensorr   Z"torch.fx.experimental.proxy_tensorr   r   Ztorch.utilsr	   r   r
   Zpy_implr   r   Zpy_functionalize_implr   ZAutogradZCPUr   r>   rB   r   r   r   r   <module>   s0    




	


'