o
    ?߱i                     @   st   d dl Z d dlmZ d dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ G dd	 d	eZdS )
    N)datetime)EveryN)ImaginaireModel)ImaginaireTrainer)distributed)easy_ioc                       s  e Zd ZdZddedededef fd	d
Zejdde	deddfddZ
	dde	deeejf deeejf dejdeddf fddZejdede	deeejf deeejf dejdeddfddZddefddZejdde	deddfddZ  ZS ) 	HeartBeata  
    A callback that logs a heartbeat message at regular intervals to indicate that the training process is still running.

    Args:
        every_n (int): The frequency at which the callback is invoked.
        step_size (int, optional): The step size for the callback. Defaults to 1.
        update_interval_in_minute (int, optional): The interval in minutes for logging the heartbeat. Defaults to 20 minutes.
        save_s3 (bool, optional): Whether to save the heartbeat information to S3. Defaults to False.
          Fevery_n	step_sizeupdate_interval_in_minutesave_s3c                    s<   t  j||d | jj| _|| _|| _td| _	d| _
d S )N)r   r   zAmerica/Los_AngelesF)super__init__	__class____name__namer   r   pytztimezonepst	is_hitted)selfr   r   r   r   r    \/data/cameron/vidgen/cosmos-predict2.5/cosmos_predict2/_src/predict2/callbacks/heart_beat.pyr   (   s   

zHeartBeat.__init__r   model	iterationreturnNc                 C   s`   t   | _ | jr.t| jd}||d}t|d| j d t|d| j d d S d S )N%Y_%m_%d-%H_%M_%Sr   times3://rundir/z_start.yamls3://timestamps_rundir/	r!   r   r   nowr   strftimer   dumpr   r   r   r   current_time_pstinfor   r   r   on_train_start0   s   
zHeartBeat.on_train_start
data_batchoutput_batchlossc                    s:   | j sd| _ t dkr| | t ||||| d S )NTr   )r   r   get_rankreportr   on_training_step_end)r   r   r,   r-   r.   r   r   r   r   r1   <   s
   
zHeartBeat.on_training_step_endtrainerc                 C   s*   t   | j  d| j kr| | d S d S )N<   )r!   r   r0   )r   r2   r   r,   r-   r.   r   r   r   r   every_n_implJ   s   
zHeartBeat.every_n_implc                 C   sJ   t   | _ | jr#t| jd}||d}t|d| j d d S d S )Nr   r    r"   z.yamlr$   )r   r   r)   r*   r   r   r   r0   W   s   
zHeartBeat.reportc                 C   sV   | j r)t| jd}||d}t|d| j d t|d| j d d S d S )Nr   r    r"   z	_end.yamlr#   )r   r   r%   r   r&   r   r'   r   r(   r   r   r   on_train_enda   s   zHeartBeat.on_train_end)r	   r
   F)r   )r   
__module____qualname____doc__intboolr   r   
rank0_onlyr   r+   dictstrtorchTensorr1   r   r4   r0   r5   __classcell__r   r   r   r   r      sL     

"r   )r!   r   r   r>   1cosmos_predict2._src.imaginaire.callbacks.every_nr   %cosmos_predict2._src.imaginaire.modelr   'cosmos_predict2._src.imaginaire.trainerr   %cosmos_predict2._src.imaginaire.utilsr   -cosmos_predict2._src.imaginaire.utils.easy_ior   r   r   r   r   r   <module>   s   