o
    ߗiHK                     @   s$  d dl Z d dlZd dlZd dlmZmZmZmZmZ ddl	m
Z
mZ ddlmZmZ ddlmZmZmZmZmZ ddlmZmZ dd	lmZ erNd d
lmZ dZG dd deZG dd deZG dd deZG dd deZ G dd deZ!G dd deZ"G dd de"Z#G dd deZ$dS )    N)DictListOptionalTYPE_CHECKINGUnion   )	polyfills	variables)create_call_functioncreate_instruction)handle_observed_exceptionObservedUserStopIterationraise_observed_exceptionunimplemented	UserError   )ValueMutationNewVariableTracker)ConstantVariable)InstructionTranslatori  c                       sR   e Zd Zd fddZdefddZdd Z			
						d fddZ  ZS )ItertoolsVariablereturnNc                       t  jdi | || _d S N )super__init__value)selfr   kwargs	__class__r   Z/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/torch/_dynamo/variables/iter.pyr         
zItertoolsVariable.__init__c                 C   s   d| j  dS )NzItertoolsVariable()r   r   r   r   r"   __repr__!   s   zItertoolsVariable.__repr__c                 C      | j S Nr%   r&   r   r   r"   as_python_constant$      z$ItertoolsVariable.as_python_constanttxr   argsList[VariableTracker]r   Dict[str, VariableTracker]r   c                    s  | j tju r. s.tfdd|D r.fdd|D }dd tj| D }tj|t dS | j tju rddlm	} t
d	d   D rWtd
dt  ddh    d}t|dv r|d r|d }d v rt|dkr d j}	n"t|dkr|d j}	nt|dkr|tjj}	n	td ntd g }|d ur|| |D ]:}
|d u r|
}n,z
|	||
gi }W n! ty } ztd|	 d|
 | d|d W Y d }~nd }~ww || qtj|t dS | j tju r= s=t|dkr=|d r=|d  r=|d }|d  }g }t||D ]}
|tt|
 q'tj|t dS | j tju rt
dd   D ratddt  dh   dd t|dkr|d r|d }d v r fddnd }ntd g }z1tj||dD ]'\}}|tjtj|rtj |n|tjt|t dgt d qW n ty } ztd |d W Y d }~nd }~ww tj|t dS | j tj!u rt|dk rtj"|d!t iS #t$%t&j!| S | j tj'u rtj(|d!t iS | j tj)u r'tj*|d!t iS | j tj+u r9t,t&j+| S | j tj-u rKt,t&j-| S t. | S )"Nc                 3   s    | ]}|  V  qd S r)   )has_unpack_var_sequence.0argr,   r   r"   	<genexpr>0   s    z2ItertoolsVariable.call_function.<locals>.<genexpr>c                    s   g | ]}|  qS r   )unpack_var_sequencer1   r4   r   r"   
<listcomp>2   s    z3ItertoolsVariable.call_function.<locals>.<listcomp>c                 S      g | ]	}t t|qS r   r	   TupleVariablelist)r2   itemr   r   r"   r7   3   s    )mutation_typer   )BuiltinVariablec                 s   s    | ]}|d vV  qdS ))initialfuncNr   )r2   keyr   r   r"   r5   <       z-Unsupported kwargs for itertools.accumulate: ,r?   r@   )r   r   r   r   zDitertools.accumulate can only accept one of: `func` kwarg, pos 2 argz.Unsupported arguments for itertools.accumulatezOUnexpected failure in invoking function during accumulate. Failed running func (r$   )from_excc                 s   s    | ]}|d kV  qdS )rA   Nr   )r2   kwr   r   r"   r5   y   rB   z*Unsupported kwargs for itertools.groupby: rA   c                 S   s@   t | tjr
|  S t | tjr|  S tdtt|   d S )Nz,Unsupported key type for itertools.groupby: )	
isinstancer	   SymNodeVariableevaluate_exprr   r*   r   strtyperA   r   r   r"   retrieve_const_key   s   z;ItertoolsVariable.call_function.<locals>.retrieve_const_keyc                    s     d| gi S )NrA   )getcall_function)xr   rM   r,   r   r"   <lambda>   s   z1ItertoolsVariable.call_function.<locals>.<lambda>z+Unsupported arguments for itertools.groupbyrL   z1Unexpected failure when calling itertools.groupbyr=   )/r   	itertoolsproductallr	   ListIteratorVariabler   
accumulatebuiltinr>   anykeysr   joinsetrN   lenr0   r6   rO   operatoraddappend	Exceptioncombinationsis_python_constantr*   r:   r;   groupbyr   
is_literalcreaterepeatRepeatIteratorVariableinline_user_function_returnr   buildr   countCountIteratorVariablecycleCycleIteratorVariable	dropwhileUserFunctionVariablezip_longestr   )r   r,   r-   r   seqsitemsr>   accseqr@   r<   eiterablerkeyfuncresultkvr    rQ   r"   rO   '   s  




	

zItertoolsVariable.call_functionr   N)r,   r   r-   r.   r   r/   r   r   )	__name__
__module____qualname__r   rJ   r'   r*   rO   __classcell__r   r   r    r"   r      s    r   c                       sF   e Zd Zd fddZdd Zdee fddZdefd	d
Z	  Z
S )IteratorVariabler   Nc                    s   t  jdi | d S r   )r   r   )r   r   r    r   r"   r      s   zIteratorVariable.__init__c                 C   s   t d d S )Nzabstract method, must implement)r   r   r,   r   r   r"   next_variable   s   zIteratorVariable.next_variablec                 C   s:   g }	 z
| | | W n ty   t| Y |S w qr)   )r`   r   r   r   )r   r,   rz   r   r   r"   force_unpack_var_sequence   s   z*IteratorVariable.force_unpack_var_sequencec                 C      dS )NTr   r   r   r   r"   has_force_unpack_var_sequence      z.IteratorVariable.has_force_unpack_var_sequencer}   )r~   r   r   r   r   r   r   r   boolr   r   r   r   r    r"   r      s
    r   c                       s6   e Zd Zdeddf fddZdd Zdd	 Z  ZS )
rh   r<   r   Nc                    r   r   )r   r   r<   )r   r<   r   r    r   r"   r      r#   zRepeatIteratorVariable.__init__c                 C   r(   r)   )r<   r   r   r   r"   r      r+   z$RepeatIteratorVariable.next_variablec                    s0      fdd  | j  tdd d S )Nc                            t dgS )Nrg   extend_outputcreate_load_python_modulerS   create_load_attrr   codegenr   r"   rR      
    z4RepeatIteratorVariable.reconstruct.<locals>.<lambda>r   F)add_push_nullr<   r   r
   r   r   r   r   r"   reconstruct   s
   

z"RepeatIteratorVariable.reconstruct)r~   r   r   r   r   r   r   r   r   r   r    r"   rh      s    rh   c                       s<   e Zd Zddededdf fddZd	d
 Zdd Z  ZS )rl   r   r   r<   stepr   Nc                    sJ   t  jdi | t|tst|}t|tst|}|| _|| _d S r   )r   r   rG   r   r   rf   r<   r   )r   r<   r   r   r    r   r"   r      s   




zCountIteratorVariable.__init__c                 C   s<   |   sJ | j}|jj|  | j|d| jgi | _|S )N__add__)
is_mutabler<   outputside_effectsmutationcall_methodr   )r   r,   old_itemr   r   r"   r   	  s
   z#CountIteratorVariable.next_variablec                    s:      fdd  | j  | j  tdd d S )Nc                      r   )Nrk   r   r   r   r   r"   rR     r   z3CountIteratorVariable.reconstruct.<locals>.<lambda>r   F)r   r<   r   r   r
   r   r   r   r"   r     s   


z!CountIteratorVariable.reconstruct)r   r   )r~   r   r   intr   r   r   r   r   r   r    r"   rl      s    	rl   c                       sN   e Zd Z			ddedeee  dedee ddf
 fdd	Zd
d Z	  Z
S )rn   Nr   iteratorsavedsaved_indexr<   r   c                    s:   |d u rg }t  jdi | || _|| _|| _|| _d S r   )r   r   r   r   r   r<   )r   r   r   r   r<   r   r    r   r"   r     s   
zCycleIteratorVariable.__init__c                 C   s   |   sJ | jd urQz0| j|}t| jtkrtd |jj	|  | j
| || _| jd u r8| |W S | jW S  tyP   t| d | _| | Y S w t| jdkrm|jj	|  | jd t| j | _| jS tt| d S )Nz4input iterator to itertools.cycle has too many itemsr   r   )r   r   r   r]   r   MAX_ITERATOR_LIMITr   r   r   r   r`   r<   r   r   r   r   StopIteration)r   r,   new_itemr   r   r"   r   /  s0   

z#CycleIteratorVariable.next_variable)Nr   N)r~   r   r   r   r   r   r   r   r   r   r   r   r   r    r"   rn     s     
rn   c                       s   e Zd ZdZddhejZ	ddeeee ef  de	ddf fdd	Z
d
d Zde	fddZded fddZdd Zdd Zdd Z  ZS )ZipVariablez$
    Represents zip(*iterables)
    indexstrictF	iterablesr   Nc                    s6   t  jdi | t|tsJ || _d| _|| _d S Nr   r   )r   r   rG   r;   r   r   r   )r   r   r   r   r    r   r"   r   V  s
   
zZipVariable.__init__c                 C      t S r)   )zipr&   r   r   r"   python_typec  r   zZipVariable.python_typec                    s   t  fdd| jD S )Nc                 3   s$    | ]}t |tp| V  qd S r)   )rG   r;   r0   )r2   itr4   r   r"   r5   g  s
    
z6ZipVariable.has_unpack_var_sequence.<locals>.<genexpr>)rU   r   r   r   r4   r"   r0   f  s   z#ZipVariable.has_unpack_var_sequencer   c                 C   s~   |  |sJ g }| jD ]}t|tr||| jd   q||| q| jr/d| jini }t|i |}dd |D S )Nr   c                 S   r8   r   r9   )r2   varr   r   r"   r7   v  s    z3ZipVariable.unpack_var_sequence.<locals>.<listcomp>)	r0   r   rG   r;   r`   r   r6   r   r   )r   r,   r   r   r   zippedr   r   r"   r6   l  s   

zZipVariable.unpack_var_sequencec                    s   |   sJ | j g } fdd}zt| jD ]\}}||| qW n5 tyZ   | jrY|dkrO| jD ]}z|| W n tyK   t Y q6w  n t tt	dd  w j
j|  |  jd7  _t|S )Nc                    s2   t | tr t| krtt |   S | S r)   )rG   r;   r]   r   r   r   )r   	old_indexr,   r   r"   get_item}  s
   


z+ZipVariable.next_variable.<locals>.get_itemr   z3zip() has one argument of len differing from othersr   )r   r   	enumerater   r`   r   r   r   r   
ValueErrorr   r   r   r	   r:   )r   r,   r-   r   idxr   r   r   r"   r   x  s@   

zZipVariable.next_variablec                 C   sR   | j D ]#}t|tr"|| jd  }|| |tdt|d q|| qd S NBUILD_TUPLEr3   )r   rG   r;   r   foreachappend_outputr   r]   )r   r   r   remaining_itemsr   r   r"   reconstruct_items  s   



zZipVariable.reconstruct_itemsc              	      s    j  fdddd |    tdt| jd tjdkr;  	d 	| j
td	d
dtdd
dg d S  tddd d S )Nc                           ddS )Nbuiltinsr   load_import_fromr   r   r   r"   rR         z)ZipVariable.reconstruct.<locals>.<lambda>Tcall_function_exr   r   )   
   r   	BUILD_MAPr   CALL_FUNCTION_EXr   )r   r   r   r   r]   r   sysversion_infor   create_load_constr   r   r   r   r"   r     s    




	zZipVariable.reconstruct)F)r~   r   r   __doc__r   _nonvar_fieldsr   r   r   r   r   r   r0   r6   r   r   r   r   r   r   r    r"   r   K  s*    	*r   c                       sh   e Zd ZdZdedeeee ef  ddf fddZdd	 Zde	fd
dZ
 fddZdd Z  ZS )MapVariablez(
    Represents map(fn, *iterables)
    fnr   r   Nc                    s   t  j|fi | || _d S r)   )r   r   r   )r   r   r   r   r    r   r"   r     s   
zMapVariable.__init__c                 C   r   r)   )mapr&   r   r   r"   r     r   zMapVariable.python_typec                 C   r   )NFr   r   r   r   r"   r0     r   z#MapVariable.has_unpack_var_sequencec                    s   t  |}| j||ji S r)   )r   r   r   rO   rs   )r   r,   r-   r    r   r"   r     s   zMapVariable.next_variablec                    sV    j  fdddd  | j |    tdt| jd dtdd	dg d S )
Nc                      r   )Nr   r   r   r   r   r   r"   rR     r   z)MapVariable.reconstruct.<locals>.<lambda>Tr   r   r   r   r   r   )r   r   r   r   r   r]   r   r   r   r   r"   r     s   


zMapVariable.reconstruct)r~   r   r   r   r   r   r   r   r   r   r0   r   r   r   r   r   r    r"   r     s    	r   c                       s   e Zd ZdZdhejZdedeee ef ddf fddZ	d	d
 Z
defddZded fddZdd Zdd Zdd Z  ZS )FilterVariablez)
    Represents filter(fn, iterable)
    r   r   rw   r   Nc                    s(   t  jdi | || _|| _d| _d S r   )r   r   r   rw   r   )r   r   rw   r   r    r   r"   r     s   
zFilterVariable.__init__c                 C   r   r)   )filterr&   r   r   r"   r     r   zFilterVariable.python_typec                 C   s   t | jtp| j|S r)   )rG   rw   r;   r0   r   r   r   r"   r0      s   z&FilterVariable.has_unpack_var_sequencer   c                 C   sZ   |  |sJ d }t| jtr| j| jd  }n| j|}| j||i }t	|ggS r)   )
r0   rG   rw   r;   r   r6   r   rO   r	   r:   )r   r,   r   filteredr   r   r"   r6     s   z"FilterVariable.unpack_var_sequencec                    s\    fdd}	 | }  j d7  _  j|gi }ttj|gi }| r-|S q)Nc                     s@    j } t jtr| t jkrtt  j|  S  jS r)   )r   rG   rw   r;   r]   r   r   r   )r   r   r   r"   _next  s   

z+FilterVariable.next_variable.<locals>._nextTr   )r   r   rO   r	   rp   r   	predicater*   )r   r,   r   r<   respred_resr   r   r"   r     s   
zFilterVariable.next_variablec                 C   sN   t | jtr | j| jd  }|| |tdt|d d S || j d S r   )rG   rw   r;   r   r   r   r   r]   )r   r   r   r   r   r"   r   $  s   
z FilterVariable.reconstruct_itemsc                    s:      fdd  | j |    tdd d S )Nc                      r   )Nr   r   r   r   r   r   r"   rR   /  r   z,FilterVariable.reconstruct.<locals>.<lambda>r   F)r   r   r   r   r
   r   r   r   r"   r   .  s   

zFilterVariable.reconstruct)r~   r   r   r   r   r   r   r   r   r   r   r   r0   r6   r   r   r   r   r   r   r    r"   r     s&    

r   )%rS   r^   r   typingr   r   r   r   r    r   r	   bytecode_transformationr
   r   excr   r   r   r   r   baser   r   constantr   torch._dynamo.symbolic_convertr   r   r   r   rh   rl   rn   r   r   r   r   r   r   r"   <module>   s*    3-w&