o
    پi(                     @   s   d Z ddlZddlZddlZddlZddlZddlmZ ddlZddl	m
Z
 e
eZdZdZdZg dd	d
gdZddgiZee dd ZdddZdddZdefddZdee ddfddZdd Zedkrpe  dS dS )a  
Test runner for multimodal_gen that manages test suites and parallel execution.

Usage:
    python3 run_suite.py --suite <suite_name> --partition-id <id> --total-partitions <num>

Example:
    python3 run_suite.py --suite 1-gpu --partition-id 0 --total-partitions 4
    N)Path)init_logger test_update_weights_from_disk.py SGLANG_MMGEN_UPDATE_WEIGHTS_PAIR)zFLUX.2-klein-base-4Bz
Qwen-Image)ztest_server_a.pyztest_server_b.pyztest_lora_format_adapter.pyz ../cli/test_generate_t2i_perf.pyz#../test_sampling_params_validate.pyr   ztest_server_2_gpu_a.pyztest_server_2_gpu_b.py)z1-gpuz2-gpuz1-npuzascend/test_server_1_npu.pyc                  C   s   t jdd} | jdtdtt dd | jdtdd	d
 | jdtddd
 | jdtddd
 | jddtd dd
 | jddddd |  S )NzRun multimodal_gen test suite)descriptionz--suiteTz*The test suite to run (e.g., 1-gpu, 2-gpu))typerequiredchoiceshelpz--partition-idr   z7Index of the current partition (for parallel execution))r   defaultr
   z--total-partitions   zTotal number of partitionsz
--base-dirserverz9Base directory for tests relative to this script's parent-kz--filterz.Pytest filter expression (passed to pytest -k)z--continue-on-error
store_trueFzlContinue running remaining tests even if one fails (for CI consistency; pytest already continues by default))actionr   r
   )	argparseArgumentParseradd_argumentstrlistSUITESkeysint
parse_args)parser r   X/home/ubuntu/.local/lib/python3.10/site-packages/sglang/multimodal_gen/test/run_suite.pyr   <   sP   
r   c                 C   sB  t jddddg}|r|d|g ||  tdd|  tj|ddd	}|jd
vr[d|j dd| d}|jrF|d|j d7 }|j	rR|d|j	 d7 }t
| t||jdkrdtd g }|j	 dD ]&}| }|rd|v r|dsd|v r| d n|}d|v r|| qntdt| d |S )zLCollect test item node IDs from the given files using pytest --collect-only.-mpytestz--collect-onlyz-qr   zCollecting tests with command:  T)capture_outputtext)r      z,pytest --collect-only failed with exit code z

Command: 
zstderr:
zstdout:
r"   zINo tests were collected (exit code 5). This may be expected with filters.z::)=-r   r   z
Collected z test items)sys
executableextendprintjoin
subprocessrun
returncodestderrstdoutloggererrorRuntimeErrorstripsplit
startswithappendlen)filesfilter_exprcmdresult	error_msg
test_itemslinetest_idr   r   r   collect_test_itemsg   s>   






r@   c                 C   s  | st d dS tjddddg}|r|d|g d}t|d	 D ]}t|}|dkr0|d
 ||  |dkrDt d| d| d t dd|  tj	|tj
tjdd}t }	 |jd}|sfntjj| tjj  || q]|  |j}	|	dkr dS |	dkrt d  dS |jddd}
d|
v od|
v }d|
v pd|
v }d|
 v pd|
 v }|s|s|s|	  S q!t d |	S )NzNo files to run.r   r   r   z-sz-vr      r   z--last-failedz(Performance assertion failed. Retrying (/z) with --last-failed...zRunning command: r   )r/   r.   bufsizeTi   r"   zwNo tests collected (exit code 5). This is expected when filters deselect all tests in a partition. Treating as success.zutf-8replace)errorsz/multimodal_gen/test/server/test_server_utils.pyAssertionErrorSafetensorErrorFileNotFoundErrorzout of memoryz
oom killerzMax retry exceeded)r)   r&   r'   r(   ranger   r6   r*   r+   PopenPIPESTDOUT	bytearrayr/   readbufferwriteflushwaitr-   decodelower)r8   r9   base_cmdmax_retriesir:   processoutput_byteschunkr-   full_outputis_perf_assertionis_flaky_ci_assertionis_oom_errorr   r   r   
run_pytest   sn   



r_   returnc                   C   s   t jdd dv S )NSGLANG_IS_IN_CI )1trueyeson)osenvirongetrT   r   r   r   r   	_is_in_ci   s   rj   suite_files_relc                 C   sn   t  sd S t| vrd S tjtr tdt dtjt   d S tt	}|tjt< tdt d| d d S )NzUsing preset r$   	Selected z for this CI run)
rj   #_UPDATE_WEIGHTS_FROM_DISK_TEST_FILErg   rh   ri   _UPDATE_WEIGHTS_MODEL_PAIR_ENVr)   randomchoice_UPDATE_WEIGHTS_MODEL_PAIR_IDS)rk   selected_pairr   r   r   $_maybe_pin_update_weights_model_pair   s   

rs   c                     sr  t   tt } | j}| j }| s"td| d t	d t
 j }t| g }|D ]}|| }| sEtd| d| d q/|t| q/|s]td j d t	d	 t| jd
}|sttd j d t	d	  fddt|D } jd  d j d j d}	ddg}
 j|	gg}tj||
ddd }|dt| d7 }|D ]
}|d| d7 }qt|dd td j d j d j  tdt| d |D ]}tdtj|  q|std t	d	 td t| d!d"|  t|}dtj||
dd d }|d#t| d7 }|D ]}|d| d7 }q t|dd t	| d S )$NzError: Target directory z does not exist.r   zWarning: Test file z not found in z. Skipping.z%No valid test files found for suite 'z'.r   )r9   zNo test items found for suite 'c                    s$   g | ]\}}| j   jkr|qS r   )total_partitionspartition_id).0rW   itemargsr   r   
<listcomp>&  s
    zmain.<locals>.<listcomp>rB   z (0-based id=)Suite	Partitionpsql)headerstablefmtr#   u   ✅ Enabled z
 test(s):
z  - T)rQ   zSuite: z | Partition: rl   z files:z5No items assigned to this partition. Exiting success.zRunning z items in this shard: z, u   ✅ Executed )r   r   __file__resolveparentbase_direxistsr)   r&   exitr   suiters   r6   r   r@   filter	enumerateru   rt   tabulater7   rg   pathbasenamer*   r_   )current_file_pathtest_root_dir
target_dirrk   suite_files_absf_relf_absall_test_itemsmy_itemspartition_infor   rowsmsgrw   f	exit_coder   rx   r   main  sf   





 
r   __main__)N)__doc__r   rg   ro   r+   r&   pathlibr   r   1sglang.multimodal_gen.runtime.utils.logging_utilsr   __name__r0   rm   rn   rq   r   suites_ascendupdater   r@   r_   boolrj   r   r   rs   r   r   r   r   r   <module>   s@    


+
3SM
