o
    _{j½  ã                   @   s`   d dl Z d dlZd dlZd dlmZmZ d dlmZ dd„ ZG dd„ deƒZ	G dd	„ d	eƒZ
dS )
é    N)ÚEpochBasedRunnerÚHook)Úget_host_infoc                 c   s8   � t | ƒ}	 zt|ƒV  W n ty   t | ƒ}Y nw q©N)ÚiterÚnextÚStopIteration)ÚiterableÚiterator© r   ú</root/disk_3tb/HAC/mmaction/core/runner/omnisource_runner.pyÚcycle
   s   €ÿýr   c                   @   s   e Zd Zdd„ ZdS )ÚOmniSourceDistSamplerSeedHookc                 C   sJ   |j D ]}t|jdƒr|j |j¡ qt|jjdƒr"|jj |j¡ qd S )NÚ	set_epoch)Údata_loadersÚhasattrÚsamplerr   ÚepochÚbatch_sampler)ÚselfÚrunnerÚdata_loaderr   r   r   Úbefore_epoch   s   
€úz*OmniSourceDistSamplerSeedHook.before_epochN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r      s    r   c                   @   s2   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	d
„ZdS )ÚOmniSourceRunnerz¥OmniSource Epoch-based Runner.

    This runner train models epoch by epoch, the epoch length is defined by the
    dataloader[0], which is the main dataloader.
    c                    s°   | j d ur| j | j|fd|i|¤Ž}n|r"| jj|| jfi |¤Ž}n| jj|| jfi |¤Ž}t|tƒs7tdƒ‚d|v rS|d }‡ fdd„| ¡ D ƒ}| j	 
||d ¡ || _d S )NÚ
train_modezT"batch_processor()" or "model.train_step()"and "model.val_step()" must return a dictÚlog_varsc                    s   i | ]	\}}|ˆ  |“qS r   r   )Ú.0ÚkÚv©Úsourcer   r   Ú
<dictcomp>6   s    z-OmniSourceRunner.run_iter.<locals>.<dictcomp>Únum_samples)Úbatch_processorÚmodelÚ
train_stepÚ	optimizerÚval_stepÚ
isinstanceÚdictÚ	TypeErrorÚitemsÚ
log_bufferÚupdateÚoutputs)r   Ú
data_batchr   r#   Úkwargsr1   r   r   r"   r   Úrun_iter&   s(   
ÿÿÿÿ

zOmniSourceRunner.run_iterc                 K   sŠ  | j  ¡  d| _|| _| jd | _| j| _| jdd … | _dd„ | jD ƒ| _dgt| jƒ }d}d|v rC| 	d¡}|d }|dd … }| j
t| jƒ | _|  d¡ t d¡ t| jƒD ][\}}|| _|  d	¡ | j|d
dd� |  d¡ | j| dkrƒ|  jd7  _q[t|ƒD ]'\}}	t|	ƒD ]}
t| j| ƒ}|  d	¡ | j|d
d|› �d� |  d¡ q�q‡|  jd7  _q[|  d¡ |  jd7  _d S )NÚtrainr   é   c                 S   s   g | ]}t |ƒ‘qS r   )r   )r   Úloaderr   r   r   Ú
<listcomp>C   s    z*OmniSourceRunner.train.<locals>.<listcomp>Útrain_ratioÚbefore_train_epoché   Úbefore_train_iterTÚ )r   r#   Úafter_train_iterz/auxÚafter_train_epoch)r'   r5   Úmoder   Zmain_loaderr   Zaux_loadersZ	aux_itersÚlenÚpopÚ_max_epochsÚ
_max_itersÚ	call_hookÚtimeÚsleepÚ	enumerateÚ_inner_iterr4   Ú_iterÚranger   Ú_epoch)r   r   r3   Zauxiliary_iter_timesZuse_aux_per_niterr9   Úir2   ÚidxZn_timesÚ_r   r   r   r5   ;   sH   






ÿû
zOmniSourceRunner.trainc                 K   s   t ‚r   )ÚNotImplementedError)r   r   r3   r   r   r   Úvalh   s   zOmniSourceRunner.valNc           
      K   sp  t |tƒsJ ‚t |t¡sJ ‚t|ƒdkr|d d dksJ ‚|dur,t dt¡ || _	| j	dus5J dƒ‚|d \}}| j	t|d ƒ | _
| jdurM| jnd}| j dtƒ |¡ | j d	|| j	¡ |  d
¡ | j| j	k r¬t |tƒr„t| |ƒs~td|› d�ƒ‚t| |ƒ}ntd|› �ƒ‚t|ƒD ]}	|dkr�| j| j	kr� n	||fi |¤Ž q�| j| j	k slt d¡ |  d¡ dS )a  Start running.

        Args:
            data_loaders (list[:obj:`DataLoader`]): Dataloaders for training.
                `data_loaders[0]` is the main data_loader, which contains
                target datasets and determines the epoch length.
                `data_loaders[1:]` are auxiliary data loaders, which contain
                auxiliary web datasets.
            workflow (list[tuple]): A list of (phase, epochs) to specify the
                running order and epochs. E.g, [('train', 2)] means running 2
                epochs for training iteratively. Note that val epoch is not
                supported for this runner for simplicity.
            max_epochs (int | None): The max epochs that training lasts,
                deprecated now. Default: None.
        r6   r   r5   NzOsetting max_epochs in run is deprecated, please set max_epochs in runner_configz1max_epochs must be specified during instantiationÚNONEz%Start running, host: %s, work_dir: %szworkflow: %s, max: %d epochsÚ
before_runzrunner has no method named "z" to run an epochz(mode in workflow must be a str, but got Ú	after_run)r+   ÚlistÚmmcvÚ
is_list_ofÚtuplerA   ÚwarningsÚwarnÚDeprecationWarningrC   rD   Úwork_dirÚloggerÚinfor   rE   r   Ústrr   Ú
ValueErrorÚgetattrr-   rK   rF   rG   )
r   r   ÚworkflowÚ
max_epochsr3   r@   Úepochsr\   Úepoch_runnerrO   r   r   r   Úrunk   sN    þÿÿ
ÿ



ÿÿò
zOmniSourceRunner.runr   )r   r   r   Ú__doc__r4   r5   rQ   rf   r   r   r   r   r      s    -r   )rF   rY   rV   Úmmcv.runnerr   r   Zmmcv.runner.utilsr   r   r   r   r   r   r   r   Ú<module>   s   	