o
    _{j~  ã                   @   st   d dl m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mZ dd„ Zddd„Zddd	„Zd
d„ ZdS )é    N)Úget_dist_infoc           	   	   C   sœ   |   ¡  g }|j}t t|ƒ¡}|D ]9}t ¡ � | dddi|¤Ž}W d  ƒ n1 s,w   Y  | |¡ ttt	| 
¡ ƒƒƒ}t|ƒD ]}| ¡  qDq|S )a  Test model with a single gpu.

    This method tests model with a single gpu and displays test progress bar.

    Args:
        model (nn.Module): Model to be tested.
        data_loader (nn.Dataloader): Pytorch data loader.

    Returns:
        list: The prediction results.
    Úreturn_lossFN© )ÚevalÚdatasetÚmmcvÚProgressBarÚlenÚtorchÚno_gradÚextendÚnextÚiterÚvaluesÚrangeÚupdate)	ÚmodelÚdata_loaderÚresultsr   Úprog_barÚdataÚresultÚ
batch_sizeÚ_r   r   ú(/root/disk_3tb/HAC/mmaction/apis/test.pyÚsingle_gpu_test   s   
ÿ

ÿr   Tc              	   C   sà   |   ¡  g }|j}tƒ \}}|dkrt t|ƒ¡}|D ]?}	t ¡ � | dddi|	¤Ž}
W d  ƒ n1 s5w   Y  | |
¡ |dkrZtt	t
|	 ¡ ƒƒƒ}t|| ƒD ]}| ¡  qSq|rft|t|ƒƒ}|S t|t|ƒ|ƒ}|S )a  Test model with multiple gpus.

    This method tests model with multiple gpus and collects the results
    under two different modes: gpu and cpu modes. By setting 'gpu_collect=True'
    it encodes results to gpu tensors and use gpu communication for results
    collection. On cpu mode it saves the results on different gpus to 'tmpdir'
    and collects them by the rank 0 worker.

    Args:
        model (nn.Module): Model to be tested.
        data_loader (nn.Dataloader): Pytorch data loader.
        tmpdir (str): Path of directory to save the temporary results from
            different gpus under cpu mode. Default: None
        gpu_collect (bool): Option to use either gpu or cpu to collect results.
            Default: True

    Returns:
        list: The prediction results.
    r   r   FNr   )r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   Úcollect_results_gpuÚcollect_results_cpu)r   r   ÚtmpdirÚgpu_collectr   r   ÚrankÚ
world_sizer   r   r   r   r   r   r   r   Úmulti_gpu_test(   s*   

ÿ

€ÿr"   c              	   C   sL  t ƒ \}}|du rMd}tj|fdtjdd�}|dkr:t d¡ tjdd�}tjt	| 
¡ ƒtjdd�}||dt|ƒ…< t |d¡ | ¡  ¡  ¡  ¡  ¡ }nt |¡ t ¡  t | t |d	|› d
�¡¡ t ¡  |dkrndS g }t|ƒD ]}t |d	|› d
�¡}	| t |	¡¡ qtg }
t|Ž D ]	}|
 t|ƒ¡ q�|
d|… }
t |¡ |
S )a–  Collect results in cpu mode.

    It saves the results on different gpus to 'tmpdir' and collects
    them by the rank 0 worker.

    Args:
        result_part (list): Results to be collected
        size (int): Result size.
        tmpdir (str): Path of directory to save the temporary results from
            different gpus under cpu mode. Default: None

    Returns:
        list: Ordered results.
    Ni   é    Úcuda©ÚdtypeÚdevicer   z
.dist_test)ÚdirZpart_z.pkl)r   r
   ÚfullÚuint8r   Úmkdir_or_existÚtempfileÚmkdtempÚtensorÚ	bytearrayÚencoder	   ÚdistÚ	broadcastÚcpuÚnumpyÚtobytesÚdecodeÚrstripÚbarrierÚdumpÚospÚjoinr   ÚappendÚloadÚzipr   ÚlistÚshutilÚrmtree)Úresult_partÚsizer   r    r!   ZMAX_LENZ
dir_tensorÚ	part_listÚiZ	part_fileÚordered_resultsÚresr   r   r   r   U   sB   
ý
ÿ

r   c              	      s(  t ƒ \}}tjtt | ¡ƒtjdd�‰ tjˆ jdd�‰‡fdd„t|ƒD ƒ}t	 
|ˆ¡ t |¡ ¡ ‰tjˆtjdd�}ˆ |dˆd …< ‡ ‡fdd„t|ƒD ƒ}t	 
||¡ |dkr’g }t||ƒD ]\}}	| t |d|	d …  ¡  ¡  ¡ ¡¡ qag }
t|Ž D ]	}|
 t|ƒ¡ q€|
d|… }
|
S dS )	a  Collect results in gpu mode.

    It encodes results to gpu tensors and use gpu communication for results
    collection.

    Args:
        result_part (list): Results to be collected
        size (int): Result size.

    Returns:
        list: Ordered results.
    r$   r%   )r'   c                    s   g | ]}ˆ   ¡ ‘qS r   )Úclone©Ú.0r   )Úshape_tensorr   r   Ú
<listcomp>£   s    z'collect_results_gpu.<locals>.<listcomp>Nr   c                    s   g | ]}ˆ   ˆ¡‘qS r   )Ú	new_zerosrI   )Úpart_tensorÚ	shape_maxr   r   rL   ©   s    
ÿ)r   r
   r.   r/   ÚpickleÚdumpsr*   Úshaper   r1   Ú
all_gatherÚmaxÚzerosr>   r<   Úloadsr3   r4   r5   r   r?   )rB   rC   r    r!   Z
shape_listZ	part_sendZpart_recv_listrD   ÚrecvrR   rF   rG   r   )rN   rO   rK   r   r   �   s4   
ÿÿ ÿr   )NT)N)Úos.pathÚpathr:   rP   r@   r,   r   r
   Útorch.distributedÚdistributedr1   Úmmcv.runnerr   r   r"   r   r   r   r   r   r   Ú<module>   s    

-;