o
    Ihj(                     @   s\  d dl Z d dlmZmZ d dlmZ d dlZd dlm  m	Z
 d dlmZmZmZmZmZmZmZmZmZ d dlmZ 	 G dd deZe jdd	 Ze jd
d Ze jdd ZG dd deZe jdd ZG dd deZG dd deZG dd deZ dedefddZ!defddZ"de#e fddZ$d e#e%ed!f  de&fd"d#Z'd$d% Z(dS )&    N)ABCabstractmethod)Any)	CFunctionalizeInterpreterPtrCGradInterpreterPtrCInterpreterCJvpInterpreterPtrCVmapInterpreterPtrpop_dynamic_layer_stackpush_dynamic_layer_stackRandomnessTypeTransformType)_set_fwd_grad_enabledc                   @   sN   e Zd ZdefddZedd Zdd Zdd	 Zd
d Z	dd Z
dd ZdS )FuncTorchInterpretercptrc                 C   s
   || _ d S N)_cptr)selfr    r   P/var/www/vscode/kcb/lib/python3.10/site-packages/torch/_functorch/pyfunctorch.py__init__7      
zFuncTorchInterpreter.__init__c                 C   s   d S r   r   )r   opargskwargsr   r   r   process<   s   zFuncTorchInterpreter.processc                 C   s   t  S r   )!temporarily_pop_interpreter_stackr   r   r   r   lowerC   s   zFuncTorchInterpreter.lowerc                 C   
   | j  S r   )r   levelr   r   r   r   r    F   r   zFuncTorchInterpreter.levelc                 C   r   r   )r   keyr   r   r   r   r!   I   r   zFuncTorchInterpreter.keyc                 C   s   t r   )NotImplementedErrorr   r   r   r   	get_stateL   s   zFuncTorchInterpreter.get_statec                 C   s   ||   kS r   )r#   )r   stater   r   r   check_stateO   s   z FuncTorchInterpreter.check_stateN)__name__
__module____qualname__r   r   r   r   r   r    r!   r#   r%   r   r   r   r   r   6   s    
r   c                  c   s(    zt  } d V  W t|  d S t|  w r   )r
   r   )savedr   r   r   r   S   s
   r   c               	   c   sr    g } z*t jj d ur| t  t jj d ust| V  W | r,t|   | s"d S d S | r8t|   | s0w r   )	torch_C
_functorchpeek_interpreter_stackappendr
   listr   pop)stackr   r   r   #temporarily_clear_interpreter_stack\   s   r2   c                 c   s^    g }z t | D ]}t| || qd V  W t |D ]}t  qd S t |D ]}t  q(w r   )reversedr   r.   r
   )r1   pushedsr   r   r   %temporarily_restore_interpreter_stackh   s   r6   c                   @   s:   e Zd ZdefddZdd Zdd Zdd	 Zd
d ZdS )VmapInterpretercdatac                 C   &   |  tjks	J || _t|| _d S r   )r!   r   Vmap_cdatar	   r   r   r8   r   r   r   r   x   s   zVmapInterpreter.__init__c                 C   "   |j tj }|| g|R i |S r   )functorch_tabler   r:   r   r   r   r   kernelr   r   r   r         zVmapInterpreter.processc                 C   r   r   )r   	batchSizer   r   r   r   
batch_size   r   zVmapInterpreter.batch_sizec                 C   sB   | j  }|tjkrdS |tjkrdS |tjkrdS td| )Nerrorsame	differentzUnknown RandomnessType: )r   
randomnessr   ErrorSame	DifferentRuntimeError)r   typr   r   r   rG      s   



zVmapInterpreter.randomnessc                 C      |   j|  |  fS r   )r!   namer    rG   r   r   r   r   r#         zVmapInterpreter.get_stateN)	r&   r'   r(   r   r   r   rC   rG   r#   r   r   r   r   r7   w   s    
r7   c                  g   sJ    t  }| D ]}|| q| V  W d    d S 1 sw   Y  d S r   )
contextlib	ExitStackenter_context)contextsr1   ctxr   r   r   nested   s   
"rU   c                       J   e Zd ZdefddZdd Zdd Z fdd	Zd
d Zdd Z	  Z
S )GradInterpreterr8   c                 C   r9   r   )r!   r   Gradr;   r   r   r<   r   r   r   r         zGradInterpreter.__init__c                 C   $   t tj| jj||g\}}||fS r   pytreetree_map_onlyr*   Tensorr   liftr   r   r   r   r   r   r_         zGradInterpreter.liftc                 C   2   |j tj }| ||\}}|| g|R i |S r   )r>   r   rX   r_   r?   r   r   r   r         zGradInterpreter.processc                    s*   |   }|stt t  S t  S r   )prev_grad_moderU   r*   no_gradsuperr   )r   rd   	__class__r   r   r         
zGradInterpreter.lowerc                 C   r   r   )r   prevGradModer   r   r   r   rd      r   zGradInterpreter.prev_grad_modec                 C   rM   r   )r!   rN   r    rd   r   r   r   r   r#      rO   zGradInterpreter.get_state)r&   r'   r(   r   r   r_   r   r   rd   r#   __classcell__r   r   rg   r   rW          rW   c                       rV   )JvpInterpreterr8   c                 C   r9   r   )r!   r   Jvpr;   r   r   r<   r   r   r   r      rY   zJvpInterpreter.__init__c                 C   rZ   r   r[   r`   r   r   r   r_      ra   zJvpInterpreter.liftc                 C   rb   r   )r>   r   rn   r_   r?   r   r   r   r      rc   zJvpInterpreter.processc                    s*   |   }|sttdt  S t  S )NF)prev_fwd_grad_moderU   r   rf   r   )r   ro   rg   r   r   r      ri   zJvpInterpreter.lowerc                 C   r   r   )r   prevFwdGradModer   r   r   r   ro      r   z!JvpInterpreter.prev_fwd_grad_modec                 C   rM   r   )r!   rN   r    ro   r   r   r   r   r#      rO   zJvpInterpreter.get_state)r&   r'   r(   r   r   r_   r   r   ro   r#   rk   r   r   rg   r   rm      rl   rm   c                   @   s2   e Zd ZdefddZdd Zdd Zdd	 Zd
S )FunctionalizeInterpreterr8   c                 C   r9   r   )r!   r   Functionalizer;   r   r   r<   r   r   r   r      s   z!FunctionalizeInterpreter.__init__c                 C   r=   r   )r>   r   rr   r?   r   r   r   r      rA   z FunctionalizeInterpreter.processc                 C   r   r   )r   functionalizeAddBackViewsr   r   r   r   functionalize_add_back_views   r   z5FunctionalizeInterpreter.functionalize_add_back_viewsc                 C   s   |   j|  fS r   )r!   rN   r    r   r   r   r   r#      s   z"FunctionalizeInterpreter.get_stateN)r&   r'   r(   r   r   r   rt   r#   r   r   r   r   rq      s
    rq   cinterpreterreturnc                 C   s^   |   }|tjkrt| S |tjkrt| S |tjkrt| S |tjkr(t	| S t
d| )Nz2NYI: PyDispatcher has not implemented support for )r!   r   rX   rW   r:   r7   rn   rm   rr   rq   rK   )ru   r!   r   r   r   coerce_cinterpreter   s   



rw   c                  C   s    t jj } | d usJ t| S r   )r*   r+   r,   r-   rw   )interpreterr   r   r   &retrieve_current_functorch_interpreter   s   ry   c                  C   s&   t jj } | d u rg S dd | D S )Nc                 S   s   g | ]}t |qS r   )rw   ).0cir   r   r   
<listcomp>	  s    z7retrieve_all_functorch_interpreters.<locals>.<listcomp>)r*   r+   r,   get_interpreter_stack)cisr   r   r   #retrieve_all_functorch_interpreters  s   r   states.c                 C   sf   t jj }|d u rt| dks|d urt| dkrdS t }t|t| ko2tdd t|| D S )Nr   Fc                 s   s    | ]
\}}| |V  qd S r   )r%   )rz   r{   r$   r   r   r   	<genexpr>  s    
z*compare_functorch_state.<locals>.<genexpr>)r*   r+   r,   r-   lenr   allzip)r   peekr~   r   r   r   compare_functorch_state  s   (r   c                 C   s2   t  }ttjtjjj||f\}}|| ||S r   )	ry   r\   r]   r*   r^   r+   r,   unwrap_if_deadr   )r   r   r   rx   r   r   r   dispatch_functorch  s
   r   ))rP   abcr   r   typingr   r*   torch.utils._pytreeutils_pytreer\   torch._C._functorchr   r   r   r   r	   r
   r   r   r   torch.autograd.forward_adr   r   contextmanagerr   r2   r6   r7   rU   rW   rm   rq   rw   ry   r/   r   tupleboolr   r   r   r   r   r   <module>   s4   , 



""