o
    Ih                     @   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 d dlmZmZ d d	lmZ G d
d deZe 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d Z dS )    )contextmanagerN)DispatchKeystrict_mode)autograd_not_implemented)HigherOrderOperator)FakeTensorMode)ProxyTorchDispatchModetrack_tensor_tree)_pytreec                       s(   e Zd Z fddZ fddZ  ZS )ExportTracepointc                    s   t  d d S )N_export_tracepoint)super__init__)self	__class__ J/var/www/vscode/kcb/lib/python3.10/site-packages/torch/_export/wrappers.pyr      s   zExportTracepoint.__init__c                    s   t  j|i |S N)r   __call__)r   argskwargsr   r   r   r      s   zExportTracepoint.__call__)__name__
__module____qualname__r   r   __classcell__r   r   r   r   r      s    r   c                 O   s<   t | jj||f\}}| jdt||}t||d | jdS )Ncall_function)constanttracer)pytreetree_mapr   unwrap_proxycreate_proxyr   r
   )moder   r   p_argsp_kwargsproxyr   r   r   export_tracepoint_dispatch_mode   s
   r(   c                 O   s,   | 
 |W  d    S 1 sw   Y  d S r   r   )r$   r   r   r   r   r   "export_tracepoint_fake_tensor_mode#   s   $r)   c                 O   sR   |  |}|  |}|   t|i | |W  d    S 1 s"w   Y  d S r   )unwrap_tensorsredispatch_to_nextr   )ctxr   r   unwrapped_argsunwrapped_kwargsr   r   r   export_tracepoint_functional)   s   


$r/   T)deferred_errorc                  O   s   | S r   r   )r   r   r   r   r   export_tracepoint_cpu8   s   r1   c                    s   t | tjjs	J dksJ tjj| }fdddd   fdd} fdd	}|j|d
d}|j|d
d}||fS )N c                    sB   |  v r |  d |ksJ  |  d |ksJ ||d | < d S )Nin_specout_spec)r3   r4   r   )pathr3   r4   )module_call_specsr   r   update_module_call_signaturesB   s   z6_wrap_submodule.<locals>.update_module_call_signaturesc                 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torchTensorstrintfloatboolAssertionError)	flat_argsar   r   r   check_flattenedH   s   z(_wrap_submodule.<locals>.check_flattenedc                    sB   t ||f\}} | t|dd}t ||\}}||fS )Nmodule_call_inputskindr5   r    tree_flattenr   tree_unflatten)moduler   r   r@   r3   )rB   r5   r   r   pre_hookO   s
   z!_wrap_submodule.<locals>.pre_hookc                    sP   t ||f\}}t |\}} | t|dd}|| t ||S )Nmodule_call_outputsrD   rF   )rI   r   r   res_r3   flat_resr4   )rB   r5   r7   r   r   	post_hookV   s   z"_wrap_submodule.<locals>.post_hookT)with_kwargs)	r8   r9   nnModulefxgraph_module	_get_attrregister_forward_pre_hookregister_forward_hook)modr5   r6   	submodulerJ   rO   
pre_handlepost_handler   )rB   r6   r5   r7   r   _wrap_submodule=   s   r\   c              	   c   sV    g }z|D ]}| t| || qd V  W |D ]}|  qd S |D ]}|  q#w r   )extendr\   remove)fpreserve_signaturemodule_call_signatureshandlesr5   handler   r   r   _wrap_submodulesc   s   

rd   c                 C   s   dd }|| _ | S )Nc                 W   s
   t | |S r   r   )r   r   r   r   r   callq   s   
z'_mark_strict_experimental.<locals>.call)r   )clsre   r   r   r   _mark_strict_experimentalp   s   rg   )!
contextlibr   r9   torch._custom_opstorch._Cr   #torch._higher_order_ops.strict_moder   torch._higher_order_ops.utilsr   
torch._opsr   torch._subclasses.fake_tensorr   "torch.fx.experimental.proxy_tensorr	   r
   torch.utilsr   r    r   r   py_implr(   r)   py_functionalize_implr/   AutogradCPUr1   r\   rd   rg   r   r   r   r   <module>   s6   



	


&
