o
    0 i?                     @   s  d dl mZ d dlZd dlmZ d dlmZmZ d dl	m
Z
mZmZ d dlZe Zde_de_e Zde_dd Zd	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZG dd  d ee
Z G d!d" d"e
Z!G d#d$ d$ee
Z"e#d%kre$  dS dS )&    )StringION)types)compile_extraFlags)TestCasetagMemoryLeakMixinTc                 K   s>   ddl m} |dd  |di  t|j|j| |fi |S )Nr   
cpu_targetreturn_typelocals)numba.core.registryr
   
setdefaultr   typing_contexttarget_context)pyfuncargtypeskwargsr
    r   h/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/numba/tests/test_looplifting.pycompile_isolated   s   r   c                 C   s&   t d}t|jD ]}| ||< q
|S )N   )npemptyrangesizexair   r   r   lift1    s   

r    c                 C   sB   t d}t|jd D ]}t|jd D ]}| |||f< qq|S )N)r      r      )r   r   r   shape)r   r   r   jr   r   r   lift2)   s   
r%   c                 C   sB   t  }tjdtjd}d}t|jd D ]
}||| |  7 }q|S N   Zdtyper   objectr   arangeZint64r   r#   )r   _r   cr   r   r   r   lift33   s   r.   c                 C   sR   t  }tjdtjd}d}d}t|jd D ]}||| |  7 }||7 }q|| S r&   r)   )r   r,   r   r-   dr   r   r   r   lift4<   s   
r0   c                 C   s6   t  }td}t|jd D ]	}|dkr |S q|S )Nr!   r      r*   r   r+   r   r#   r   r,   r   r   r   r   r   lift5G   s   
r4   c                 c   s:    t d}dV  t|jD ]}| ||< qt |V  d S )Nr   r   )r   r   r   r   sumr   r   r   r   	lift_gen1P   s   

r6   c                  C   s0   t d tdD ]} tdD ]}  dS q	dS )Nr"   
   r1   )r   r   r   )r   r$   r   r   r   lift_issue2561Y   s   
r8   c                 C   s(   t d}t|jd D ]}|  S |S Nr!   r   )r   r+   r   r#   r   r   r   r   reject1`   s   
r:   c                 c   s4    t  }td}t|jd D ]}|| V  qd S r9   r2   r3   r   r   r   reject_gen1h   s   
r;   c                 c   sN    t  }td}t|jD ]}|| |  }t|D ]}|d }q|V  qd S )Nr   r1   )r*   r   r+   r   r   )r   r,   r   r   resr$   r   r   r   reject_gen2o   s   

r=   c                 C   s@   t jdt jd}t|jD ]}t }t |d | ||< q|S )Nr   r(   r"   )r   r   int32r   r   r*   r+   )r   r   r   r,   r   r   r   reject_npm1{   s
   r?   c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!S )"TestLoopLiftingc              	   C   s<   ddl m} t|j|j||d ti d}| t|jd |S )Nr   r	   )r   flagsr   r"   )	r   r
   r   r   r   looplift_flagsassertEquallenlifted)selfr   r   r
   cresr   r   r   try_lift   s   zTestLoopLifting.try_liftc                 C   s(   |j d }|j \}| |jj d S Nr   )rE   	overloadsvalues
assertTruefndescnative)rF   rG   jitlooploopcresr   r   r   assert_lifted_native   s   
z$TestLoopLifting.assert_lifted_nativec                 C   s8   |  ||}|| }|j| }| | | || dS )zH
        Check that pyfunc can loop-lift even in nopython mode.
        N)rH   entry_pointrQ   assertPreciseEqualrF   r   r   argsrG   expectedgotr   r   r   check_lift_ok   s
   

zTestLoopLifting.check_lift_okc                 C   s@   |  ||}t|| }t|j| }| | | || dS )zg
        Check that pyfunc (a generator function) can loop-lift even in
        nopython mode.
        N)rH   listrR   rQ   rS   rT   r   r   r   check_lift_generator_ok   s
   
z'TestLoopLifting.check_lift_generator_okc                 C   s<   t ||td}| |j || }|j| }| || dS )z4
        Check that pyfunc can't loop-lift.
        rA   N)r   rB   assertFalserE   rR   rS   rT   r   r   r   check_no_lift   s   
zTestLoopLifting.check_no_liftc                 C   sD   t ||td}| |j t|| }t|j| }| || dS )zK
        Check that pyfunc (a generator function) can't loop-lift.
        r[   N)r   rB   r\   rE   rY   rR   rS   rT   r   r   r   check_no_lift_generator   s   z'TestLoopLifting.check_no_lift_generatorc                 C      |  ttjfd d S N){   )rX   r    r   intprF   r   r   r   
test_lift1      zTestLoopLifting.test_lift1c                 C   r_   r`   )rX   r%   r   rb   rc   r   r   r   
test_lift2   re   zTestLoopLifting.test_lift2c                 C   r_   r`   )rX   r.   r   rb   rc   r   r   r   
test_lift3   re   zTestLoopLifting.test_lift3c                 C   r_   r`   )rX   r0   r   rb   rc   r   r   r   
test_lift4   re   zTestLoopLifting.test_lift4c                 C   r_   r`   )rX   r4   r   rb   rc   r   r   r   
test_lift5   re   zTestLoopLifting.test_lift5c                 C   s   |  tdd d S )Nr   )rX   r8   rc   r   r   r   test_lift_issue2561   s   z#TestLoopLifting.test_lift_issue2561c                 C   r_   r`   )rZ   r6   r   rb   rc   r   r   r   test_lift_gen1   re   zTestLoopLifting.test_lift_gen1c                 C   r_   r`   )r]   r:   r   rb   rc   r   r   r   test_reject1   re   zTestLoopLifting.test_reject1c                 C   r_   r`   )r^   r;   r   rb   rc   r   r   r   test_reject_gen1   re   z TestLoopLifting.test_reject_gen1c                 C   r_   r`   )r^   r=   r   rb   rc   r   r   r   test_reject_gen2   re   z TestLoopLifting.test_reject_gen2N)__name__
__module____qualname__rH   rQ   rX   rZ   r]   r^   rd   rf   rg   rh   ri   rj   rk   rl   rm   rn   r   r   r   r   r@      s"    r@   c                   @   s   e Zd Zdd Zdd ZdS )TestLoopLiftingAnnotatec           
         s   ddl m} dd   fdd}|dd|}td	}| }||}tj||d
  t }|j|d |	 }|
  | d| |jjd }	| dj|	d| | d| dS )zO
        Verify that annotation works as expected with one lifted loop
        r   jitc                   S      d S Nr   r   r   r   r   bar      z4TestLoopLiftingAnnotate.test_annotate_1.<locals>.barc                    s*      t | jD ]
}| |  d7  < q| S )Nr"   r   r   )r   r   rw   r   r   foo   s   z4TestLoopLiftingAnnotate.test_annotate_1.<locals>.fooTZforceobjr7   r"   file"The function contains lifted loopsr1   Loop at line {line}linezHas 1 overloadsNnumbart   r   r+   copytestingassert_equalr   Zinspect_typesgetvaluecloseZassertIn__code__co_firstlinenoformat)
rF   rt   r{   cfoor   xcopyrbuf
annotationr   r   rz   r   test_annotate_1   s    
z'TestLoopLiftingAnnotate.test_annotate_1c                    s   ddl m} dd   fdd}|dd|}td	}| }||}tj||d
 d  t }|j|d |	 }|
  | d| |jjd }	|jjd }
| dj|	d| | dj|
d| dS )zP
        Verify that annotation works as expected with two lifted loops
        r   rs   c                   S   ru   rv   r   r   r   r   r   rw     rx   z4TestLoopLiftingAnnotate.test_annotate_2.<locals>.barc                    sJ      t | jD ]
}| |  d7  < qt | jD ]
}| |  d9  < q| S )Nr"   r1   ry   )r   r   r$   rz   r   r   r{     s   z4TestLoopLiftingAnnotate.test_annotate_2.<locals>.fooTr|   r7   r"   r1   r}   r   r      r   r   Nr   )rF   rt   r{   r   r   r   r   r   r   line1line2r   rz   r   test_annotate_2
  s"   

z'TestLoopLiftingAnnotate.test_annotate_2N)ro   rp   rq   r   r   r   r   r   r   rr      s    "rr   c                   @   st   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd ZdS )TestLoopLiftingInActionc                 C   s&   |j |jd  j}| t|| d S rI   )rJ   
signaturesrE   rC   rD   )rF   jitted	loopcountrE   r   r   r   assert_has_lifted2  s   z)TestLoopLiftingInAction.assert_has_liftedc           	      C   s   ddl m}m}m}m} ||||d d  dddd }dD ]!}tjdd	d
}tjdd	d
}||| ||| | || qd S )Nr   )rt   voidr>   doubleTr|   c                 S   sX   | dkrt |jd D ]
}|| d ||< qd S t |jd D ]
}|| d ||< qd S )Nr   g       @      ?)r   r#   )ur   r   r   r   r   forloop_with_if9  s   z?TestLoopLiftingInAction.test_issue_734.<locals>.forloop_with_if)r   r"   r7   r>   r(   )	r   rt   r   r>   r   r   r+   py_funcrS   )	rF   rt   r   r>   r   r   r   Znb_aZnp_ar   r   r   test_issue_7346  s   

z&TestLoopLiftingInAction.test_issue_734c                 C   sJ   ddl m} |ddddd }tg d}tj|||| d S )	Nr   rs   zf8[:](f8[:])Tr|   c                 S   sl   t t| }d}tt| D ]}|d7 }| | ||< | | dkr# nqt|d t| D ]}d||< q-|S )Nr   r"   r7   )r   zerosrD   r   )r   r<   indiir   r   r   testL  s   
z4TestLoopLiftingInAction.test_issue_812.<locals>.test)
r   r!   r1   r'   r1   r7   r'   r1   r   )r   rt   r   arrayr   r   r   )rF   rt   r   r   r   r   r   test_issue_812I  s
   

z&TestLoopLiftingInAction.test_issue_812c           
      C   s   ddl m} dd }td}t }|dd|}|||}|||}| |d |d  | |d |d  |j|jd  jd }|j	 \}	| 
|	jj d S )	Nr   rs   c                 S   s&   d}| D ]}||7 }q|  }||fS rI   )__hash__)r   bsehr   r   r   lift_issue2368b  s
   
z?TestLoopLiftingInAction.test_issue_2368.<locals>.lift_issue2368r7   Tr|   r"   )r   rt   r   Zonesr*   rC   rJ   r   rE   rK   rL   rM   rN   )
rF   rt   r   r   r   r   rV   rW   rO   rP   r   r   r   test_issue_2368_  s   


z'TestLoopLiftingInAction.test_issue_2368c                 C   j   ddl m} |dddd }| |d|d | j|dd	 | |d|d | j|dd	 d S )
Nr   rs   Tr|   c                 S   s   d}t | D ]}|}q|S rI   r   nr<   r   r   r   r   r   }  s   z?TestLoopLiftingInAction.test_no_iteration_w_redef.<locals>.testr"   r   r   rt   rC   r   r   rF   rt   r   r   r   r   test_no_iteration_w_redefx  s   
z1TestLoopLiftingInAction.test_no_iteration_w_redefc                 C   r   )
Nr   rs   Tr|   c                 S   s   d}t | D ]}||7 }q|S rI   r   r   r   r   r   r     s   
z7TestLoopLiftingInAction.test_no_iteration.<locals>.testr   r"   r   r   r   r   r   r   test_no_iteration  s   
z)TestLoopLiftingInAction.test_no_iterationc                 C   sF   ddl m} |dddd }| |d|d | j|dd d S )	Nr   rs   Tr|   c                 S   s   t | D ]}|}q|S rv   r   )r   r   r<   r   r   r   r     s   z>TestLoopLiftingInAction.test_define_in_loop_body.<locals>.testr"   r   r   r   r   r   r   test_define_in_loop_body  s
   
z0TestLoopLiftingInAction.test_define_in_loop_bodyc                 C   sB   ddl m} |dddd }td}| |||| dS )	zTest a problem caused by invalid discovery of loop argument
        when a variable is used afterwards but not before.

        Before the fix, this will result in::

        numba.ir.NotDefinedError: 'i' is not defined
        r   rs   Tr|   c                 S   sL   t | tjkr| jdkrd}d}t| jd D ]}qd S td}d\}}|S )Nr"           r   )r   r   )typer   Zndarrayndimr   r#   	Exception)argresultr$   r   r   r   r   r     s   
z;TestLoopLiftingInAction.test_invalid_argument.<locals>.testr7   N)r   rt   r   r+   rC   r   )rF   rt   r   r   r   r   r   test_invalid_argument  s
   

z-TestLoopLiftingInAction.test_invalid_argumentc                 C   sB   ddl m} |dddd }| | |  | j|dd d S )	Nr   rs   Tr|   c                  S   s2   d} d}t dD ]}|dkrd} || 7 }q|| fS )Nr'   r   r1   r   r   )r   yr   r   r   r   r     s   
zHTestLoopLiftingInAction.test_conditionally_defined_in_loop.<locals>.testr"   r   r   r   r   r   r   "test_conditionally_defined_in_loop  s
   
	z:TestLoopLiftingInAction.test_conditionally_defined_in_loopc                 C   sv   ddl m} dd l}dd }|  |d |dd|}| |d|d W d    d S 1 s4w   Y  d S )Nr   rs   c                 S   s   | r	t dD ]}qd S d S )Nr7   r   )r   r   r   r   r   r     s
   zRTestLoopLiftingInAction.test_stack_offset_error_when_has_no_return.<locals>.pyfuncerrorTr|   )r   rt   warningscatch_warningssimplefilterrC   )rF   rt   r   r   Zcfuncr   r   r   *test_stack_offset_error_when_has_no_return  s   

"zBTestLoopLiftingInAction.test_stack_offset_error_when_has_no_returnc                    sF   ddl m} dd   fdd}|dd|}| |d	|d	 d
S )z
        https://github.com/numba/numba/issues/2179

        Looplifting transformation is using the wrong version of variable `h`.
        r   rs   c                 S   s   | S rv   r   )r   r   r   r   rw     rx   z<TestLoopLiftingInAction.test_variable_scope_bug.<locals>.barc                    s*   d}t | D ]}|| }q| |  }|S )Nr   r   )r   r   krz   r   r   r{     s
   
z<TestLoopLiftingInAction.test_variable_scope_bug.<locals>.fooTr|   r7   N)r   rt   rC   )rF   rt   r{   r   r   rz   r   test_variable_scope_bug  s
   z/TestLoopLiftingInAction.test_variable_scope_bugc                 C   s   ddl m} dd }|dd|}tddf}| || ||  | t|j|jd  jd |j|jd  jd }| t|jd tdd	f}| || ||  | t|jd
 dS )z<
        https://github.com/numba/numba/issues/2481
        r   rs   c                 S   sB   | d d | }d}t |jD ]}t  ||d d d |  }q|S )Nr"   r   )r   r   r*   )r   r   Ar-   r   r   r   r   r{     s   z<TestLoopLiftingInAction.test_recompilation_loop.<locals>.fooTr|   r7   r"   r   r1   N)	r   rt   r   r+   rC   rD   rJ   r   rE   )rF   rt   r{   r   rU   rE   r   r   r   test_recompilation_loop  s   
z/TestLoopLiftingInAction.test_recompilation_loopc              	   C   s   ddl m} |dddd }tdddd	ddd
d}|di |}|jdi |}| |d |d  | |d |d  |j|jd  j\}| t	|j
d d S )Nr   rs   Tr|   c           
      S   s   t |t |}}|||d< |d< t |d D ]2}	t | ||	  |t | ||	    ||	d < t |||	  |t |||	    ||	d < qt  ||fS )Nr   r"   )r   r   r+   sincosr*   )
r   r   r-   r/   x0y0r   xsZysr   r   r   r   r{     s   02zDTestLoopLiftingInAction.test_lift_objectmode_issue_4223.<locals>.foog333333?g333333?g333333?   )r   r   r-   r/   r   r   r   r"   r   )r   rt   dictr   rS   rJ   r   rE   rC   rD   Znopython_signatures)rF   rt   r{   r   rW   rV   rE   r   r   r   test_lift_objectmode_issue_4223  s   
	z7TestLoopLiftingInAction.test_lift_objectmode_issue_4223N)ro   rp   rq   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   1  s     r   __main__)%ior   numpyr   Z
numba.corer   Znumba.core.compilerr   r   Znumba.tests.supportr   r   r   ZunittestrB   Zforce_pyobjectZenable_loopliftr   Zpyobject_looplift_flagsZenable_pyobject_loopliftr   r    r%   r.   r0   r4   r6   r8   r:   r;   r=   r?   r@   rr   r   ro   mainr   r   r   r   <module>   s<    	
			
bJ 