U
    zh                     @   s`   d dl Z d dlZd dlmZ ddlmZ G dd dZG dd deZd	d
 Zdd Z	e	  dS )    N)Optional   )VariableTrackerc                   @   s    e Zd ZdZdd Zdd ZdS )	LazyCachez+Container to cache the real VariableTrackerc                 C   s   |st || _|| _d | _d S N)AssertionErrorvaluesourcevt)selfr   r	    r   N/var/www/html/venv/lib/python3.8/site-packages/torch/_dynamo/variables/lazy.py__init__   s    zLazyCache.__init__c                 C   sN   | j d kstddlm} ddlm} | }||| j| j| _ | `| `d S )N   )InstructionTranslatorr   )VariableBuilder)	r
   r   Zsymbolic_convertr   builderr   Z
current_txr	   r   )r   r   r   Ztxr   r   r   realize   s    zLazyCache.realizeN)__name__
__module____qualname____doc__r   r   r   r   r   r   r   	   s   r   c                       s   e Zd ZdZdhejZedd Z fddZeddd	Z	d
d Z
dd Zdd Zdd Zdd ZejZejZedddZ  ZS )LazyVariableTrackera  
    A structure that defers the creation of the actual VariableTracker
    for a given underlying value until it is accessed.

    The `realize` function invokes VariableBuilder to produce the real object.
    Once a LazyVariableTracker has been realized, internal bookkeeping will
    prevent double realization.

    This object should be utilized for processing containers, or objects that
    reference other objects where we may not want to take on creating all the
    VariableTrackers right away.
    _cachec                 K   s   t t| |fd|i|S )Nr	   )r   r   )r   r	   optionsr   r   r   create.   s    zLazyVariableTracker.createc                    s&   t |tstt jf | || _d S r   )
isinstancer   r   superr   r   )r   r   kwargs	__class__r   r   r   2   s    zLazyVariableTracker.__init__)returnc                 C   s   | j jdkr| j   | j jS )z.Force construction of the real VariableTrackerN)r   r
   r   r   r   r   r   r   7   s    
zLazyVariableTracker.realizec                 C   s   |   r| jjS | S )z4Return the real VariableTracker if it already exists)is_realizedr   r
   r"   r   r   r   unwrap=   s    zLazyVariableTracker.unwrapc                 C   s   | j jd k	S r   )r   r
   r"   r   r   r   r#   C   s    zLazyVariableTracker.is_realizedc                 K   sF   | d| j| jkst| d| j| jk	r4|   tj|  f|S )Nr   r	   )getr   r   r	   r   r   cloner$   )r   r   r   r   r   r&   F   s    zLazyVariableTracker.clonec                 C   s"   |   r|   S t|  S r   )r#   r$   __str__r   r"   r   r   r   r'   L   s    zLazyVariableTracker.__str__c                 C   s   t |  |S r   getattrr   )r   itemr   r   r   __getattr__Q   s    zLazyVariableTracker.__getattr__Nc           	         s   dkrt   t|}| kr* | d S t|}t|trN|  }nt|tr|}|j}|j	}|D ] }||krl||  ||< qlnp|t
kr fdd|D }nR|tkrt fdd|D }n0|t tjfkr fddt
| D }n|}||f |< |S )	zP
        Walk an object and realize all LazyVariableTrackers inside it.
        Nr   c                    s   g | ]} | qS r   realize_all.0vcacheclsr   r   
<listcomp>t   s     z3LazyVariableTracker.realize_all.<locals>.<listcomp>c                 3   s   | ]} | V  qd S r   r,   r.   r1   r   r   	<genexpr>v   s     z2LazyVariableTracker.realize_all.<locals>.<genexpr>c                    s   i | ]\}}| | qS r   r,   )r/   kr0   r1   r   r   
<dictcomp>x   s      z3LazyVariableTracker.realize_all.<locals>.<dictcomp>)dictidtype
issubclassr   r-   r   r   __dict___nonvar_fieldslisttuplecollectionsOrderedDictitems)	r3   r   r2   idxZ	value_clsresultZ
value_dictZnonvarskeyr   r1   r   r-   X   s0    	

zLazyVariableTracker.realize_all)N)r   r   r   r   r   r=   staticmethodr   r   r   r$   r#   r&   r'   r+   visit__repr__classmethodr-   __classcell__r   r   r   r   r      s    
 r   c                    s    t tt  fdd}|S )Nc                    s   t |   ||S r   r(   )r   argsr   namer   r   realize_and_forward   s    z8_create_realize_and_forward.<locals>.realize_and_forward)	functoolswrapsr)   r   )rM   rN   r   rL   r   _create_realize_and_forward   s    rQ   c                  C   s:   t j D ]*\} }| tjkr
t|r
tt| t|  q
d S r   )r   r<   rB   r   callablesetattrrQ   )rM   r   r   r   r   	_populate   s    
rT   )
r@   rO   typingr   baser   r   r   rQ   rT   r   r   r   r   <module>   s   c