o
    ?ß±i%  ã                   @   s<   d dl mZ d dlZd dlmZmZ G dd„ dejjƒZdS )é    )ÚAnyN)ÚLazyDictÚinstantiatec                
       s*  e Zd ZdZd‡ fdd„Zdededeejj	ejj
jf fdd	„Zd
eeejf dedeeeejf ejf fdd„Ze ¡ d
eeejf dedeeeejf ejf fdd„ƒZe ¡ dededefdd„ƒZejfdejddfdd„Zdejj	dejj
jdeddfdd„Zddeddfdd„Z‡  ZS ) ÚImaginaireModela§  The base model class of Imaginaire. It is inherited from torch.nn.Module.

    All models in Imaginaire should inherit ImaginaireModel. It should include the implementions for all the
    computation graphs. All inheriting child classes should implement the following methods:
    - training_step(): The training step of the model, including the loss computation.
    - validation_step(): The validation step of the model, including the loss computation.
    - forward(): The computation graph for model inference.
    The following methods have default implementations in ImaginaireModel:
    - init_optimizer_scheduler(): Creates the optimizer and scheduler for the model.
    ÚreturnNc                    s   t ƒ  ¡  d | _d S )N)ÚsuperÚ__init__Úparallel_dims)Úself©Ú	__class__© úO/data/cameron/vidgen/cosmos-predict2.5/cosmos_predict2/_src/imaginaire/model.pyr   #   s   

zImaginaireModel.__init__Úoptimizer_configÚscheduler_configc                 C   s(   |   ¡ |_t|ƒ}||_t|ƒ}||fS )aC  Creates the optimizer and scheduler for the model.

        Args:
            config_model (ModelConfig): The config object for the model.

        Returns:
            optimizer (torch.optim.Optimizer): The model optimizer.
            scheduler (torch.optim.lr_scheduler.LRScheduler): The optimization scheduler.
        )Ú
parametersÚparamsr   Ú	optimizer)r
   r   r   r   Ú	schedulerr   r   r   Úinit_optimizer_scheduler'   s
   
z(ImaginaireModel.init_optimizer_schedulerÚ
data_batchÚ	iterationc                 C   ó   t ‚)a³  The training step of the model, including the loss computation.

        Args:
            data (dict[str, torch.Tensor]): Data batch (dictionary of tensors).
            iteration (int): Current iteration number.

        Returns:
            output_batch (dict[str, torch.Tensor]): Auxiliary model output from the training batch.
            loss (torch.Tensor): The total loss for backprop (weighted sum of various losses).
        ©ÚNotImplementedError©r
   r   r   r   r   r   Útraining_step9   s   zImaginaireModel.training_stepc                 C   r   )aª  The validation step of the model, including the loss computation.

        Args:
            data (dict[str, torch.Tensor]): Data batch (dictionary of tensors).
            iteration (int): Current iteration number.

        Returns:
            output_batch (dict[str, torch.Tensor]): Auxiliary model output from the validation batch.
            loss (torch.Tensor): The total loss (weighted sum of various losses).
        r   r   r   r   r   Úvalidation_stepH   s   zImaginaireModel.validation_stepÚargsÚkwargsc                 O   r   )z÷The computation graph for model inference.

        Args:
            *args: Whatever you decide to pass into the forward method.
            **kwargs: Keyword arguments are also possible.

        Return:
            Your model's output.
        r   )r
   r   r   r   r   r   ÚforwardX   s   zImaginaireModel.forwardÚmemory_formatc                 C   ó   dS )zšThe model preparation before the training is launched

        Args:
            memory_format (torch.memory_format): Memory format of the model.
        Nr   )r
   r!   r   r   r   Úon_train_starte   s   zImaginaireModel.on_train_startr   r   c                 C   r"   )a  Hook before zero_grad() is called.

        Args:
            optimizer (torch.optim.Optimizer): The model optimizer.
            scheduler (torch.optim.lr_scheduler.LRScheduler): The optimization scheduler.
            iteration (int): Current iteration number.
        Nr   )r
   r   r   r   r   r   r   Úon_before_zero_gradm   s   
z#ImaginaireModel.on_before_zero_gradr   c                 C   r"   )a,  Hook after loss.backward() is called.

        This method is called immediately after the backward pass, allowing for custom operations
        or modifications to be performed on the gradients before the optimizer step.

        Args:
            iteration (int): Current iteration number.
        Nr   )r
   r   r   r   r   Úon_after_backwardy   s   	z!ImaginaireModel.on_after_backward)r   N)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   ÚtupleÚtorchÚoptimÚ	OptimizerÚlr_schedulerÚLRSchedulerr   ÚdictÚstrÚTensorÚintr   Úno_gradr   Úinference_moder   r    Úpreserve_formatr!   r#   r$   r%   Ú__classcell__r   r   r   r   r      sL    ÿÿ
þÿÿ
þÿÿþÿÿÿ
þr   )	Útypingr   r+   Ú+cosmos_predict2._src.imaginaire.lazy_configr   r   ÚnnÚModuler   r   r   r   r   Ú<module>   s   