U
    qh                     @  s4   d dl mZ d dlZd dlmZ G dd deZdS )    )annotationsN)OpRunc                   @  s4   e Zd Zdd ZddddZddddd	d
ZdS )Loopc                 C  sv   t | || d| jkr tdd|kr0tddd t| jjD | _t| jj	d | _
t| jj| j
 d | _d S )	NZopsetsz&run_params must contains key 'opsets'.verbosez'run_params must contains key 'verbose'.c                 S  s   i | ]\}}||qS  r   ).0inr   r   >/tmp/pip-unpacked-wheel-xnis5xre/onnx/reference/ops/op_loop.py
<dictcomp>   s      z!Loop.__init__.<locals>.<dictcomp>      )r   __init__
run_paramsKeyError	enumeratebodyoutput_namesoutput_indexleninput_namesNK)self	onnx_noder   r   r   r
   r      s    
zLoop.__init__bool)returnc                 C  s   dS )zThe operator Loop needs to know all results produced
        so far as the loop may silently access one of them.
        Some information are not always referred in the list of inputs
        (kind of static variables).
        Tr   )r   r   r   r
   need_context   s    zLoop.need_contextN)contextr   
attributesc                  s  |r|d }|dd  }nd }|d k	rDt |dsDtdt| d| j}|j}dd |D  |d k	rr| |d < |jd }	|rt|t| }
||
d  }t||D ]\}}| |< q|d k	r|D ]}||  |< qd	d
 t| j	D }d}|rL|d ks||k rL| 
d| t|jdkr\|jd d k	r\tj||d krHd n|jd |jd < t|jdkr|jd d k	r| |jd < | j |d}| j	dkrt| j	D ] }|| || j	 |   q| j|	 }|| }|d krtd|	dt|jdd  |jdd  D ]\}}|| j|   |< q|d7 }| 
d q|dkrt fdd
|jdd  D }n|dd| j  }|dd
 |D  t|t| jjk r|tjdd qt|}| |S )Nr   r   dtypez,M must be empty or an array but its type is .c                 S  s   i | ]
}|d qS )Nr   )r   namer   r   r
   r   (   s      zLoop._run.<locals>.<dictcomp>r   c                 S  s   g | ]}g qS r   r   r   r   r   r   r
   
<listcomp>5   s     zLoop._run.<locals>.<listcomp>z  -- loop> {%r})r    )r   z
Condition z) returned by the subgraph cannot be None.z
  -- loop<c                   s   g | ]} | qS r   r   r#   inputsr   r
   r$   O   s     c                 S  s   g | ]}t |qS r   )npZvstack)r   xr   r   r
   r$   R   s     r   )shape)hasattr	TypeErrortyper   r   r   r   zipranger   _logr'   arrayr    Z	_run_bodyappendr   RuntimeErrorr   extendr   outputemptytupleZ_check_and_fix_outputs)r   MZcondr   r   r   argsZ	v_initialZloop_inputsZ	cond_namebeginZ
all_inputsr"   valaZk_carried_awayitoutputskZ
index_condr   oresr   r%   r
   _run   sj    

   


&
z	Loop._run)__name__
__module____qualname__r   r   rA   r   r   r   r
   r      s   
r   )
__future__r   Znumpyr'   Zonnx.reference.op_runr   r   r   r   r   r
   <module>   s   