o
    Y۷i                      @   s   d dl mZ d dlZd dlmZ d dlmZmZm	Z	m
Z
 d dlmZ d dlmZ ede	dG d	d
 d
e
Zede	dG dd de
ZG dd de
ZedG dd de
Zedkrfe  dS dS )    )contextmanagerN)cuda)unittestskip_on_cudasimskip_if_external_memmgrCUDATestCase)captured_stderr)configznot supported on CUDASIMz0Deallocation specific to Numba memory managementc                   @   s   e Zd Zdd Zdd ZdS )TestDeallocationc                 C   s|   t  jj}|  | t|d ttj	D ]}t 
td | t||d  qt 
td | t|d d S )Nr      )r   current_contextmemory_managerdeallocationsclearassertEquallenranger	   CUDA_DEALLOCS_COUNT	to_devicenparange)selfdeallocsi r   a/home/ubuntu/vllm_env/lib/python3.10/site-packages/numba/cuda/tests/cudadrv/test_deallocations.pytest_max_pending_count   s   z'TestDeallocation.test_max_pending_countc                 C   s   t  }|jj}|  | t|d | }d}tj	}zT||j
 t_	| j|j|dd t tj|d tjd | t|d t tj|j|j tjd | t|d t tjdtjd | t|d W |t_	d S |t_	w )Nr   i@B r   )delta   )dtype)r   r   r   r   r   r   r   get_memory_infor	   CUDA_DEALLOCS_RATIOtotalassertAlmostEqual_max_pending_bytesr   r   onesint8_size)r   ctxr   mimax_pending	old_ratior   r   r   test_max_pending_bytes   s.   
z'TestDeallocation.test_max_pending_bytesN)__name__
__module____qualname__r   r,   r   r   r   r   r
      s    r
   z&defer_cleanup has no effect in CUDASIMc                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestDeferCleanupc                 C   s   t d}t|}t jj}|  | t	|d t
 + t|}~| t	|d ~| t	|d |  | t	|d W d    n1 sNw   Y  |  | t	|d d S N   r   r   r   r   r   r   r   r   r   r   r   r   r   defer_cleanupr   harrdarr1r   darr2r   r   r   
test_basicE   s    



	zTestDeferCleanup.test_basicc              	   C   s  t d}t|}t jj}|  | t	|d t
 K t
 + t|}~| t	|d ~| t	|d |  | t	|d W d    n1 sSw   Y  |  | t	|d W d    n1 snw   Y  |  | t	|d d S r1   r3   r5   r   r   r   test_nestedW   s(   




zTestDeferCleanup.test_nestedc              	   C   s
  t d}t|}t jj}|  | t	|d G dd dt
}| |1 t  t|}~| t	|d |  | t	|d |1 sNw   Y  W d    n1 s]w   Y  |  | t	|d ~| t	|d |  | t	|d d S )Nr2   r   c                   @      e Zd ZdS )z4TestDeferCleanup.test_exception.<locals>.CustomErrorNr-   r.   r/   r   r   r   r   CustomErrors       r=   r   )r   r   r   r   r   r   r   r   r   r   	ExceptionassertRaisesr4   )r   r6   r7   r   r=   r8   r   r   r   test_exceptionl   s,   



zTestDeferCleanup.test_exceptionN)r-   r.   r/   r9   r:   rA   r   r   r   r   r0   B   s    r0   c                   @   s   e Zd Zdd ZdS )TestDeferCleanupAvailc                 C   s.   t  	 W d    d S 1 sw   Y  d S N)r   r4   )r   r   r   r   test_context_manager   s   
"z*TestDeferCleanupAvail.test_context_managerN)r-   r.   r/   rD   r   r   r   r   rB      s    rB   c                   @   s\   e Zd ZdZ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S )TestDelzJ
    Ensure resources are deleted properly without ignored exception.
    c                 c   sJ    t  }d V  |j  W d    n1 sw   Y  | |  d S rC   )r   r   r   assertFalsegetvalue)r   r(   capr   r   r   check_ignored_exception   s   zTestDel.check_ignored_exceptionc                 C   B   t  }| }| |
 ~W d    d S 1 sw   Y  d S rC   )r   r   create_streamrI   )r   r(   streamr   r   r   test_stream   
   "zTestDel.test_streamc                 C   rJ   rC   )r   r   create_eventrI   )r   r(   eventr   r   r   
test_event   rN   zTestDel.test_eventc                 C   D   t  }|d}| |
 ~W d    d S 1 sw   Y  d S N    r   r   memhostallocrI   r   r(   memr   r   r   test_pinned_memory   
   
"zTestDel.test_pinned_memoryc                 C   sH   t  }|jddd}| |
 ~W d    d S 1 sw   Y  d S )NrT   T)mappedrU   rW   r   r   r   test_mapped_memory   s
   "zTestDel.test_mapped_memoryc                 C   rR   rS   )r   r   memallocrI   rW   r   r   r   test_device_memory   rZ   zTestDel.test_device_memoryc                 C   rR   rS   )r   r   memallocmanagedrI   rW   r   r   r   test_managed_memory   rZ   zTestDel.test_managed_memoryc                 C     G dd dt }td}t }|j  | | t| W d    n1 s,w   Y  t| W d    n1 sAw   Y  t	 2 t| W d    n1 s[w   Y  t| W d    n1 spw   Y  W d    n1 sw   Y  zt| |1 sw   Y  W n |y   t| W d    n1 sw   Y  Y n	w W d    d S W d    d S 1 sw   Y  d S )Nc                   @   r;   )z;TestDel.test_pinned_contextmanager.<locals>.PinnedExceptionNr<   r   r   r   r   PinnedException   r>   rb   r   )
r?   r   zerosr   r   r   r   rI   pinnedr4   )r   rb   arrr(   r   r   r   test_pinned_contextmanager   D   


" z"TestDel.test_pinned_contextmanagerc                 C   ra   )Nc                   @   r;   )z;TestDel.test_mapped_contextmanager.<locals>.MappedExceptionNr<   r   r   r   r   MappedException   r>   rh   r   )
r?   r   rc   r   r   r   r   rI   r[   r4   )r   rh   re   r(   r   r   r   test_mapped_contextmanager   rg   z"TestDel.test_mapped_contextmanagerN)r-   r.   r/   __doc__r   rI   rM   rQ   rY   r\   r^   r`   rf   ri   r   r   r   r   rE      s    
rE   __main__)
contextlibr   numpyr   numbar   numba.cuda.testingr   r   r   r   numba.tests.supportr   
numba.corer	   r
   r0   rB   rE   r-   mainr   r   r   r   <module>   s$    4Bj