o
    Zhj(                     @   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   K/var/www/auris/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   Zpushedsr   r   r   %temporarily_restore_interpreter_stackh   s   r5   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   r7   r   r   r   r   x   s   zVmapInterpreter.__init__c                 C   "   |j tj }|| g|R i |S r   )functorch_tabler   r9   r   r   r   r   Zkernelr   r   r   r         zVmapInterpreter.processc                 C   r   r   )r   Z	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errorZsameZ	differentzUnknown RandomnessType: )r   
randomnessr   ErrorZSameZ	DifferentRuntimeError)r   typr   r   r   rB      s   



zVmapInterpreter.randomnessc                 C      |   j|  |  fS r   )r!   namer    rB   r   r   r   r   r#         zVmapInterpreter.get_stateN)	r&   r'   r(   r   r   r   r@   rB   r#   r   r   r   r   r6   w   s    
r6   c                  g   sJ    t  }| D ]}|| q| V  W d    d S 1 sw   Y  d S r   )
contextlib	ExitStackenter_context)Zcontextsr1   ctxr   r   r   nested   s   
"rM   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 )GradInterpreterr7   c                 C   r8   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   rW         zGradInterpreter.liftc                 C   2   |j tj }| ||\}}|| g|R i |S r   )r=   r   rP   rW   r>   r   r   r   r         zGradInterpreter.processc                    s*   |   }|stt t  S t  S r   )prev_grad_moderM   r*   Zno_gradsuperr   )r   r\   	__class__r   r   r         
zGradInterpreter.lowerc                 C   r   r   )r   ZprevGradModer   r   r   r   r\      r   zGradInterpreter.prev_grad_modec                 C   rF   r   )r!   rG   r    r\   r   r   r   r   r#      rH   zGradInterpreter.get_state)r&   r'   r(   r   r   rW   r   r   r\   r#   __classcell__r   r   r^   r   rO          rO   c                       rN   )JvpInterpreterr7   c                 C   r8   r   )r!   r   Jvpr:   r   r   r;   r   r   r   r      rQ   zJvpInterpreter.__init__c                 C   rR   r   rS   rX   r   r   r   rW      rY   zJvpInterpreter.liftc                 C   rZ   r   )r=   r   rd   rW   r>   r   r   r   r      r[   zJvpInterpreter.processc                    s*   |   }|sttdt  S t  S )NF)prev_fwd_grad_moderM   r   r]   r   )r   re   r^   r   r   r      r`   zJvpInterpreter.lowerc                 C   r   r   )r   ZprevFwdGradModer   r   r   r   re      r   z!JvpInterpreter.prev_fwd_grad_modec                 C   rF   r   )r!   rG   r    re   r   r   r   r   r#      rH   zJvpInterpreter.get_state)r&   r'   r(   r   r   rW   r   r   re   r#   ra   r   r   r^   r   rc      rb   rc   c                   @   s2   e Zd ZdefddZdd Zdd Zdd	 Zd
S )FunctionalizeInterpreterr7   c                 C   r8   r   )r!   r   Functionalizer:   r   r   r;   r   r   r   r      s   z!FunctionalizeInterpreter.__init__c                 C   r<   r   )r=   r   rg   r>   r   r   r   r      r?   z FunctionalizeInterpreter.processc                 C   r   r   )r   ZfunctionalizeAddBackViewsr   r   r   r   functionalize_add_back_views   r   z5FunctionalizeInterpreter.functionalize_add_back_viewsc                 C   s   |   j|  fS r   )r!   rG   r    r   r   r   r   r#      s   z"FunctionalizeInterpreter.get_stateN)r&   r'   r(   r   r   r   rh   r#   r   r   r   r   rf      s
    rf   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   rP   rO   r9   r6   rd   rc   rg   rf   rD   )ri   r!   r   r   r   coerce_cinterpreter   s   



rk   c                  C   s    t jj } | d usJ t| S r   )r*   r+   r,   r-   rk   )interpreterr   r   r   &retrieve_current_functorch_interpreter   s   rm   c                  C   s&   t jj } | d u rg S dd | D S )Nc                 S   s   g | ]}t |qS r   )rk   ).0cir   r   r   
<listcomp>	  s    z7retrieve_all_functorch_interpreters.<locals>.<listcomp>)r*   r+   r,   Zget_interpreter_stack)cisr   r   r   #retrieve_all_functorch_interpreters  s   rr   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%   )rn   ro   r$   r   r   r   	<genexpr>  s    
z*compare_functorch_state.<locals>.<genexpr>)r*   r+   r,   r-   lenrr   allzip)rs   peekrq   r   r   r   compare_functorch_state  s   (ry   c                 C   s2   t  }ttjtjjj||f\}}|| ||S r   )	rm   rT   rU   r*   rV   r+   r,   Zunwrap_if_deadr   )r   r   r   rl   r   r   r   dispatch_functorch  s
   rz   ))rI   abcr   r   typingr   r*   Ztorch.utils._pytreeutilsZ_pytreerT   Ztorch._C._functorchr   r   r   r   r	   r
   r   r   r   Ztorch.autograd.forward_adr   r   contextmanagerr   r2   r5   r6   rM   rO   rc   rf   rk   rm   r/   rr   tupleboolry   rz   r   r   r   r   <module>   s4   , 



""