o
    ZhbL                     @   s   d Z ddlZddlZddlZddl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rLd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 )a  
This module provides iterator-related variable tracking functionality for Dynamo.
It implements variable classes for handling Python iterators and itertools functions
during symbolic execution and tracing.

The module includes:
- Base iterator variable classes for tracking iterator state
- Implementations of built-in iterators (zip, map, filter)
- Support for itertools functions (product, accumulate, combinations, etc.)
- Mutation tracking and reconstruction capabilities for iterator operations

These classes integrate with Dynamo's variable tracking system to enable proper
handling of iterator operations during code transformation and optimization.
    N)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   K/var/www/auris/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__0   s   zItertoolsVariable.__repr__c                 C      | j S Nr#   r$   r   r   r    as_python_constant3      z$ItertoolsVariable.as_python_constanttxr   argslist[VariableTracker]r   dict[str, VariableTracker]r   c                    sh  | 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 }ntd d v r fdd}nfd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 r t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 t+ | S )#Nc                 3   s    | ]}|  V  qd S r'   )has_unpack_var_sequence.0argr*   r   r    	<genexpr>A   s    z2ItertoolsVariable.call_function.<locals>.<genexpr>c                    s   g | ]}|  qS r   )unpack_var_sequencer/   r2   r   r    
<listcomp>C   s    z3ItertoolsVariable.call_function.<locals>.<listcomp>c                 S      g | ]	}t t|qS r   r   TupleVariablelist)r0   itemr   r   r    r5   D   s    )mutation_typer   )BuiltinVariablec                 s   s    | ]}|d vV  qdS ))initialfuncNr   )r0   keyr   r   r    r3   M       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"   )Zfrom_excc                 s   s    | ]}|d kV  qdS )r?   Nr   )r0   kwr   r   r    r3      r@   z*Unsupported kwargs for itertools.groupby: r?   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   ZSymNodeVariableZevaluate_exprr   r(   r   strtyper?   r   r   r    retrieve_const_key   s   z;ItertoolsVariable.call_function.<locals>.retrieve_const_keyz+Unsupported arguments for itertools.groupbyc                    s     d| gi S )Nr?   )getcall_functionxr   rH   r*   r   r    keyfunc   s   z0ItertoolsVariable.call_function.<locals>.keyfuncc                    s    | S r'   r   rK   )rH   r   r    rN      s   rG   z1Unexpected failure when calling itertools.groupbyr;   ),r   	itertoolsproductallr   ZListIteratorVariabler   
accumulatebuiltinr<   anykeysr   joinsetrI   lenr.   r4   rJ   operatoraddappend	ExceptioncombinationsZis_python_constantr(   r8   r9   groupbyr   Z
is_literalcreaterepeatRepeatIteratorVariableZinline_user_function_returnr   buildr   countCountIteratorVariablecycleCycleIteratorVariabler   )r   r*   r+   r   Zseqsitemsr<   accseqr>   r:   eiterablerrN   resultkvr   rM   r    rJ   6   s  	






zItertoolsVariable.call_functionr   N)r*   r   r+   r,   r   r-   r   r   )	__name__
__module____qualname__r   rE   r%   r(   rJ   __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[   rw   r   r
   )r   r*   rm   r   r   r    force_unpack_var_sequence   s   z*IteratorVariable.force_unpack_var_sequencec                 C      dS )NTr   rv   r   r   r    has_force_unpack_var_sequence      z.IteratorVariable.has_force_unpack_var_sequencerp   )rq   rr   rs   r   rw   r9   r   rx   boolrz   rt   r   r   r   r    ru      s
    ru   c                       s6   e Zd Zdeddf fddZdd Zdd	 Z  ZS )
ra   r:   r   Nc                    r   r   )r   r   r:   )r   r:   r   r   r   r    r      r!   zRepeatIteratorVariable.__init__c                 C   r&   r'   )r:   rv   r   r   r    rw      r)   z$RepeatIteratorVariable.next_variablec                    s0      fdd  | j  tdd d S )Nc                            t dgS )Nr`   extend_outputZcreate_load_python_modulerO   Zcreate_load_attrr   codegenr   r    <lambda>   
    z4RepeatIteratorVariable.reconstruct.<locals>.<lambda>r   F)add_push_nullr:   r   r   r   r   r   r   r    reconstruct   s
   

z"RepeatIteratorVariable.reconstruct)rq   rr   rs   r   r   rw   r   rt   r   r   r   r    ra      s    ra   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 )rd   r   r   r:   stepr   Nc                    sJ   t  jdi | t|tst|}t|tst|}|| _|| _d S r   )r   r   rD   r   r   r_   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mutationZcall_methodr   )r   r*   Zold_itemr   r   r    rw     s
   z#CountIteratorVariable.next_variablec                    s:      fdd  | j  | j  tdd d S )Nc                      r}   )Nrc   r~   r   r   r   r    r     r   z3CountIteratorVariable.reconstruct.<locals>.<lambda>r   F)r   r:   r   r   r   r   r   r   r    r     s   


z!CountIteratorVariable.reconstruct)r   r   )rq   rr   rs   intr   rw   r   rt   r   r   r   r    rd   	  s    	rd   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 )rf   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   rw   rX   r   MAX_ITERATOR_LIMITr   r   r   r   r[   r:   r   r
   r   r   StopIteration)r   r*   Znew_itemr   r   r    rw   9  s0   

z#CycleIteratorVariable.next_variable)Nr   N)rq   rr   rs   ru   r   r9   r   r   r   rw   rt   r   r   r   r    rf   (  s     
rf   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   rD   r9   r   r   r   )r   r   r   r   r   r   r    r   `  s
   
zZipVariable.__init__c                 C      t S r'   )zipr$   r   r   r    python_typem  r{   zZipVariable.python_typec                    s   t  fdd| jD S )Nc                 3   s$    | ]}t |tp| V  qd S r'   )rD   r9   r.   )r0   itr2   r   r    r3   q  s
    
z6ZipVariable.has_unpack_var_sequence.<locals>.<genexpr>)rQ   r   rv   r   r2   r    r.   p  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   r6   r   r7   )r0   varr   r   r    r5     s    z3ZipVariable.unpack_var_sequence.<locals>.<listcomp>)	r.   r   rD   r9   r[   r   r4   r   r   )r   r*   r   r   r   Zzippedr   r   r    r4   v  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'   )rD   r9   rX   r   r   rw   )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   r8   )r   r*   r+   r   idxr   r   r   r    rw     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r1   )r   rD   r9   r   foreachappend_outputr	   rX   )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   Zload_import_fromr   r   r   r    r         z)ZipVariable.reconstruct.<locals>.<lambda>TZcall_function_exr   r   )   
   r   	BUILD_MAPr   CALL_FUNCTION_EXr   )r   r   r   r	   rX   r   sysversion_infor   Zcreate_load_constr   r   r   r   r    r     s    




	zZipVariable.reconstruct)F)rq   rr   rs   __doc__ru   _nonvar_fieldsr9   r   r   r|   r   r   r.   r4   rw   r   r   rt   r   r   r   r    r   U  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   ry   )NFr   rv   r   r   r    r.     r{   z#MapVariable.has_unpack_var_sequencec                    s   t  |}| j||ji S r'   )r   rw   r   rJ   rg   )r   r*   r+   r   r   r    rw     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    r     r   z)MapVariable.reconstruct.<locals>.<lambda>Tr   r   r   r   r   r   )r   r   r   r   r	   rX   r   r   r   r   r    r     s   


zMapVariable.reconstruct)rq   rr   rs   r   r   r9   r   r   r   r|   r.   rw   r   rt   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   rk   r   Nc                    s(   t  jdi | || _|| _d| _d S r   )r   r   r   rk   r   )r   r   rk   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'   )rD   rk   r9   r.   rv   r   r   r    r.   
  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'   )
r.   rD   rk   r9   r   r4   r   rJ   r   r8   )r   r*   r   filteredr   r   r    r4     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   rD   rk   r9   rX   r   r   rw   )r   rv   r   r    _next  s   

z+FilterVariable.next_variable.<locals>._nextTr   )r   r   rJ   r   ZUserFunctionVariabler   	predicater(   )r   r*   r   r:   resZpred_resr   rv   r    rw     s   
zFilterVariable.next_variablec                 C   sN   t | jtr | j| jd  }|| |tdt|d d S || j d S r   )rD   rk   r9   r   r   r   r	   rX   )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    r   9  r   z,FilterVariable.reconstruct.<locals>.<lambda>r   F)r   r   r   r   r   r   r   r   r    r   8  s   

zFilterVariable.reconstruct)rq   rr   rs   r   ru   r   r   r   r9   r   r   r|   r.   r4   rw   r   r   rt   r   r   r   r    r     s&    

r   )$r   rO   rY   r   typingr   r   r    r   r   Zbytecode_transformationr   r	   excr
   r   r   r   r   baser   r   Zconstantr   Ztorch._dynamo.symbolic_convertr   r   r   ru   ra   rd   rf   r   r   r   r   r   r   r    <module>   s,    .-w&