U
    zhv(                     @   s^  d dl Z d dlmZmZ d dlmZmZ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"eedddZ#edddZ$ee dddZ%eeed f  e&d!d"d#Z'd$d% Z(dS )&    N)ABCabstractmethod)AnyListTuple)	CFunctionalizeInterpreterPtrCGradInterpreterPtrCInterpreterCJvpInterpreterPtrCVmapInterpreterPtrpop_dynamic_layer_stackpush_dynamic_layer_stackRandomnessTypeTransformType)_set_fwd_grad_enabledc                   @   sN   e Zd Zed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   N/var/www/html/venv/lib/python3.8/site-packages/torch/_functorch/pyfunctorch.py__init__6   s    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   lowerB   s    zFuncTorchInterpreter.lowerc                 C   s
   | j  S r   )r   levelr   r   r   r   r    E   s    zFuncTorchInterpreter.levelc                 C   s
   | j  S r   )r   keyr   r   r   r   r!   H   s    zFuncTorchInterpreter.keyc                 C   s   t d S r   )NotImplementedErrorr   r   r   r   	get_stateK   s    zFuncTorchInterpreter.get_statec                 C   s   ||   kS r   )r#   )r   stater   r   r   check_stateN   s    z FuncTorchInterpreter.check_stateN)__name__
__module____qualname__r   r   r   r   r   r    r!   r#   r%   r   r   r   r   r   5   s   
r   c                  c   s    zt } d V  W 5 t |  X d S r   )r   r   )Zsavedr   r   r   r   R   s    
r   c               	   c   sJ   g } z,tjj d k	r$| t  qt| V  W 5 | rDt |   q2X d S r   )	r   poptorch_C
_functorchpeek_interpreter_stackappendr   list)stackr   r   r   #temporarily_clear_interpreter_stack[   s    r1   c                 c   sJ   g }z*t | D ]}t| || qd V  W 5 t |D ]
}t  q8X d S r   )reversedr   r   r.   )r0   Zpushedsr   r   r   %temporarily_restore_interpreter_stackg   s    
r4   c                   @   s:   e Zd ZedddZdd Zdd Zdd	 Zd
d ZdS )VmapInterpretercdatac                 C   s&   |  tjkst|| _t|| _d S r   )r!   r   VmapAssertionError_cdatar   r   r   r7   r   r   r   r   w   s    zVmapInterpreter.__init__c                 C   s   |j tj }|| f||S r   )functorch_tabler   r8   r   r   r   r   Zkernelr   r   r   r      s    zVmapInterpreter.processc                 C   s
   | j  S r   )r   Z	batchSizer   r   r   r   
batch_size   s    zVmapInterpreter.batch_sizec                 C   sF   | j  }|tjkrdS |tjkr&dS |tjkr4dS td| d S )NerrorZsameZ	differentzUnknown RandomnessType: )r   
randomnessr   ErrorZSameZ	DifferentRuntimeError)r   typr   r   r   r@      s    



zVmapInterpreter.randomnessc                 C   s   |   j|  |  fS r   )r!   namer    r@   r   r   r   r   r#      s    zVmapInterpreter.get_stateN)	r&   r'   r(   r	   r   r   r>   r@   r#   r   r   r   r   r5   v   s
   
r5   c               	   g   s2   t   }| D ]}|| q| V  W 5 Q R X d S r   )
contextlib	ExitStackenter_context)Zcontextsr0   ctxr   r   r   nested   s    
rI   c                       sJ   e Zd ZedddZdd Zdd Z fdd	Zd
d Zdd Z	  Z
S )GradInterpreterr6   c                 C   s&   |  tjkst|| _t|| _d S r   )r!   r   Gradr9   r:   r   r   r;   r   r   r   r      s    zGradInterpreter.__init__c                 C   s$   t tj| jj||g\}}||fS r   pytreetree_map_onlyr*   Tensorr   liftr   r   r   r   r   r   rP      s      zGradInterpreter.liftc                 C   s,   |j tj }| ||\}}|| f||S r   )r<   r   rK   rP   r=   r   r   r   r      s    zGradInterpreter.processc                    s*   |   }|s tt t  S t  S r   )prev_grad_moderI   r*   Zno_gradsuperr   )r   rR   	__class__r   r   r      s    zGradInterpreter.lowerc                 C   s
   | j  S r   )r   ZprevGradModer   r   r   r   rR      s    zGradInterpreter.prev_grad_modec                 C   s   |   j|  |  fS r   )r!   rD   r    rR   r   r   r   r   r#      s    zGradInterpreter.get_state)r&   r'   r(   r	   r   rP   r   r   rR   r#   __classcell__r   r   rT   r   rJ      s   rJ   c                       sJ   e Zd ZedddZdd Zdd Z fdd	Zd
d Zdd Z	  Z
S )JvpInterpreterr6   c                 C   s&   |  tjkst|| _t|| _d S r   )r!   r   Jvpr9   r:   r
   r   r;   r   r   r   r      s    zJvpInterpreter.__init__c                 C   s$   t tj| jj||g\}}||fS r   rL   rQ   r   r   r   rP      s      zJvpInterpreter.liftc                 C   s,   |j tj }| ||\}}|| f||S r   )r<   r   rX   rP   r=   r   r   r   r      s    zJvpInterpreter.processc                    s*   |   }|s ttdt  S t  S )NF)prev_fwd_grad_moderI   r   rS   r   )r   rY   rT   r   r   r      s    zJvpInterpreter.lowerc                 C   s
   | j  S r   )r   ZprevFwdGradModer   r   r   r   rY      s    z!JvpInterpreter.prev_fwd_grad_modec                 C   s   |   j|  |  fS r   )r!   rD   r    rY   r   r   r   r   r#      s    zJvpInterpreter.get_state)r&   r'   r(   r	   r   rP   r   r   rY   r#   rV   r   r   rT   r   rW      s   rW   c                   @   s2   e Zd ZedddZdd Zdd Zdd	 Zd
S )FunctionalizeInterpreterr6   c                 C   s&   |  tjkst|| _t|| _d S r   )r!   r   Functionalizer9   r:   r   r   r;   r   r   r   r      s    z!FunctionalizeInterpreter.__init__c                 C   s   |j tj }|| f||S r   )r<   r   r[   r=   r   r   r   r      s    z FunctionalizeInterpreter.processc                 C   s
   | j  S r   )r   ZfunctionalizeAddBackViewsr   r   r   r   functionalize_add_back_views   s    z5FunctionalizeInterpreter.functionalize_add_back_viewsc                 C   s   |   j|  fS r   )r!   rD   r    r   r   r   r   r#      s    z"FunctionalizeInterpreter.get_stateN)r&   r'   r(   r	   r   r   r\   r#   r   r   r   r   rZ      s   rZ   )cinterpreterreturnc                 C   sb   |   }|tjkrt| S |tjkr,t| S |tjkr>t| S |tjkrPt	| S t
d| d S )Nz2NYI: PyDispatcher has not implemented support for )r!   r   rK   rJ   r8   r5   rX   rW   r[   rZ   rB   )r]   r!   r   r   r   coerce_cinterpreter   s    



r_   )r^   c                  C   s    t jj } | d k	stt| S r   )r*   r+   r,   r-   r9   r_   )interpreterr   r   r   &retrieve_current_functorch_interpreter   s    ra   c                  C   s&   t jj } | d krg S dd | D S )Nc                 S   s   g | ]}t |qS r   )r_   ).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    rf   .)statesr^   c                 C   sf   t jj }|d kr t| dks4|d k	r8t| dkr8dS t }t|t| kodtdd t|| D S )Nr   Fc                 s   s   | ]\}}| |V  qd S r   )r%   )rb   rc   r$   r   r   r   	<genexpr>  s    z*compare_functorch_state.<locals>.<genexpr>)r*   r+   r,   r-   lenrf   allzip)rg   peekre   r   r   r   compare_functorch_state  s    (rm   c                 C   s2   t  }ttjtjjj||f\}}|| ||S r   )	ra   rM   rN   r*   rO   r+   r,   Zunwrap_if_deadr   )r   r   r   r`   r   r   r   dispatch_functorch  s      rn   ))rE   abcr   r   typingr   r   r   r*   Ztorch.utils._pytreeutilsZ_pytreerM   Ztorch._C._functorchr   r   r	   r
   r   r   r   r   r   Ztorch.autograd.forward_adr   r   contextmanagerr   r1   r4   r5   rI   rJ   rW   rZ   r_   ra   rf   boolrm   rn   r   r   r   r   <module>   s0   ,"



""