o
    }oih                      @   s>   d dl mZmZ d dlZd dlZd dlmZ G dd dZdS )    )	MagicMockpatchN)NsysCallbackc                   @   s   e Zd Zejdddd Zejdd Zejdd Zd	d
 Zdd Z	e
de
ddd Ze
ddd Ze
de
ddd Ze
de
ddd Zdd Ze
ddd Zejdg de
de
ddd Ze
ddd  Zd!S )"TestNsysCallbackT)autousec                 c   s    t d| _t d| _t d| _t d| _| j  | j  | j  | j  tddtj_	tddtj_
t | _t| jdtj_d V  | j  | j  | j  | j  d S )Nz
torch.cudaztorch.cuda.cudart!torch.autograd.profiler.emit_nvtx.nemo.lightning.pytorch.callbacks.nsys.get_rankT)return_valuer   )r   	cuda_mockcudart_mockemit_nvtx_mockget_rank_mockstartr   torchcudais_availablecurrent_devicefixed_cudartcudartstopself r   _/home/ubuntu/.local/lib/python3.10/site-packages/tests/lightning/pytorch/callbacks/test_nsys.pysetup_mocks   s$   










zTestNsysCallback.setup_mocksc                 C   s   t  }d|jj_|S )Nr   )r   strategyroot_devicetype)r   trainerr   r   r   mock_trainer2   s   
zTestNsysCallback.mock_trainerc                 C   s   t  S )N)r   r   r   r   r   mock_pl_module8   s   zTestNsysCallback.mock_pl_modulec                 C   sT   t ddddgdd}|jdksJ |jdksJ |jddgks!J |jdks(J dS )z*Test initialization with valid parameters.
      r      T
start_stepend_stepranks	gen_shapeN)r   _nsys_profile_start_step_nsys_profile_end_step_nsys_profile_ranks_nsys_profile_gen_shape)r   callbackr   r   r   test_init_valid_params<   s
   z'TestNsysCallback.test_init_valid_paramsc                 C   s   t t tddd W d   n1 sw   Y  t t tddd W d   n1 s1w   Y  t t tddd W d   dS 1 sMw   Y  dS )z,Test initialization with invalid parameters.10r"   )r%   r&   Nr!   20)pytestraisesAssertionErrorr   r   r   r   r   test_init_invalid_paramsD   s   "z)TestNsysCallback.test_init_invalid_paramsr   r   c                 C   sL   d|_ tdddgdd}d|j_|||d d | jj  |jdd d S )Nr   r!   r"   Tr$   )record_shapes)	r	   r   r   current_epoch_stepon_train_batch_startr   cudaProfilerStartassert_called_onceassert_called_once_withr   mock_emit_nvtxmock_get_rankr   r    r-   r   r   r   #test_on_train_batch_start_profilingO   s   z4TestNsysCallback.test_on_train_batch_start_profilingc                 C   s>   d|_ tdddgd}d|j_|||d d | jj  d S )Nr   r!   r"   r%   r&   r'   	   )r	   r   r   r6   r7   r   r8   assert_not_calledr   r=   r   r    r-   r   r   r   &test_on_train_batch_start_no_profiling\   s
   z7TestNsysCallback.test_on_train_batch_start_no_profilingc                 C   sT   d|_ tdddgd}d|j_|jdksJ d|_|||d d d | jj  d S )Nr   r!   r"   r?   FT)	r	   r   r   r6   _has_nsys_enabledon_train_batch_endr   cudaProfilerStopr9   r;   r   r   r   !test_on_train_batch_end_profilingf   s   z2TestNsysCallback.test_on_train_batch_end_profilingc                 C   s8   d|_ tdddgd}|||d d d | jj  d S )Nr   r!   r"   r?      )r	   r   rE   r   rF   rA   r;   r   r   r   $test_on_train_batch_end_no_profilings   s   z5TestNsysCallback.test_on_train_batch_end_no_profilingc                 C   s@   d|j j_tdddgd}|||dd |||ddd dS )z*Test behavior when the device is not CUDA.cpur!   r"   r   r?   N)r   r   r   r   r7   rE   )r   r   r    r-   r   r   r   test_non_cuda_device}   s   
z%TestNsysCallback.test_non_cuda_devicec                 C   s<   d|_ tdddgd}|||dd |||ddd dS )z@Test behavior when the current rank is not in the profile ranks.r#   r!   r"   r   r?   N)r	   r   r7   rE   rB   r   r   r   test_rank_not_in_profile_ranks   s   z/TestNsysCallback.test_rank_not_in_profile_ranksz+start_step,end_step,batch_idx,expected_call))r!   r"   r@   F)r!   r"   r!   T)r!   r"      F)r!   r"   r"   F)r!   r"      Fc	           
      C   sb   d|_ t||dgd}	||j_|	||d | |r%| jj  |  d S | jj  |  d S )Nr   r?   )	r	   r   r   r6   r7   r   r8   r9   rA   )
r   r<   r=   r%   r&   	batch_idxexpected_callr   r    r-   r   r   r   test_profiling_range   s   z%TestNsysCallback.test_profiling_rangec                 C   s   d|_ tdddgd}d|jj_d|j_|||d d | jjj	dks'J dd|j_|||d d | jjj	dks>J d	d|j_|
||d d d | jjj	dksVJ d
d S )Nr   r!   (   r?   r   r#   z cudaProfilerStart was not called   z"cudaProfilerStart was called againzcudaProfilerStop was not called)r	   r   r   r   r   r6   r7   r   r8   
call_countrE   rF   rB   r   r   r   test_single_profile_range   s   
z*TestNsysCallback.test_single_profile_rangeN)__name__
__module____qualname__r1   fixturer   r   r    r.   r4   r   r>   rC   rG   rI   rK   rL   markparametrizerQ   rU   r   r   r   r   r      s@    




	



r   )unittest.mockr   r   r1   r   %nemo.lightning.pytorch.callbacks.nsysr   r   r   r   r   r   <module>   s
   