o
    }oi                     @   s   d dl mZmZ d dlmZmZmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ eG dd dZeG d	d
 d
ZeG dd dZeG dd dZeG dd dZG dd dZdS )    )	dataclassfield)AnyDictOptional)MISSING)config)DatasetConfig)exp_managerc                   @   s2   e Zd ZU eZeed< dZeed< dZ	e
ed< dS )SchedConfignameg        min_lr
last_epochN)__name__
__module____qualname__r   r   str__annotations__r   floatr   int r   r   L/home/ubuntu/.local/lib/python3.10/site-packages/nemo/core/config/modelPT.pyr      s   
 r   c                   @   s*   e Zd ZU eZeed< dZee	 ed< dS )OptimConfigr   Nsched)
r   r   r   r   r   r   r   r   r   r   r   r   r   r   r       s   
 r   c                   @   sR   e Zd ZU dZdZee ed< dZee ed< dZ	ee ed< dZ
ee ed< dS )ModelConfigz(
    Model component inside ModelPT
    Ntrain_dsvalidation_dstest_dsoptim)r   r   r   __doc__r   r   r	   r   r   r   r   r   r   r   r   r   r   &   s   
 r   c                   @   sJ   e Zd ZU edd dZeeef ed< edd dZ	eeef ed< dS )HydraConfigc                   C   s   ddiS )Ndir.r   r   r   r   r   <lambda>5       zHydraConfig.<lambda>default_factoryrunc                   C   s   ddd iiS )Nroothandlersr   r   r   r   r   r$   6   s    job_loggingN)
r   r   r   r   r(   r   r   r   r   r+   r   r   r   r   r!   3   s   
 "r!   c                   @   sn   e Zd ZU eZeed< eZeed< e	dd dZ
ejed< e	dd dZee ed< e	d	d dZeed
< dS )
NemoConfigr   modelc                   C   s   t jddddddS )NddpF   gpu)strategyenable_checkpointingloggerlog_every_n_stepsaccelerator)r   TrainerConfigr   r   r   r   r$   >   s    
zNemoConfig.<lambda>r&   trainerc                   C   s   t  S N)r
   ExpManagerConfigr   r   r   r   r$   B   r%   r
   c                   C   s   t  S r8   )r!   r   r   r   r   r$   C   s    hydraN)r   r   r   r   r   r   r   r-   r   r   r7   r   r6   r
   r   r   r:   r!   r   r   r   r   r,   9   s   
 r,   c                   @   s   e Zd ZdefddZddee fddZddee fdd	Zddee fd
dZ	dde
jdee
j fddZdd ZdefddZdS )ModelConfigBuilder	model_cfgc                 C   s"   || _ d| _d| _d| _d| _dS )a  
        Base class for any Model Config Builder.

        A Model Config Builder is a utility class that accepts a ModelConfig dataclass,
        and via a set of utility methods (that are implemented by the subclassed ModelConfigBuilder),
        builds a finalized ModelConfig that can be supplied to a NemoModel dataclass as
        the `model` component.

        Subclasses *must* implement the private method `_finalize_cfg`.
            Inside this method, they must update `self.model_cfg` with all interdependent config
            options that need to be set (either updated by user explicitly or with their default value).

            The updated model config must then be preserved in `self.model_cfg`.

        Example:
            # Create the config builder
            config_builder = <subclass>ModelConfigBuilder()

            # Update the components of the config that are modifiable
            config_builder.set_X(X)
            config_builder.set_Y(Y)

            # Create a "finalized" config dataclass that will contain all the updates
            # that were specified by the builder
            model_config = config_builder.build()

            # Use model config as is (or further update values), then create a new Model
            model = nemo.<domain>.models.<ModelName>Model(cfg=model_config, trainer=Trainer())

        Supported build methods:
        -   set_train_ds: All model configs can accept a subclass of `DatasetConfig` as their
                training config. Subclasses can override this method to enable auto-complete
                by replacing `Optional[DatasetConfig]` with `Optional[<subclass of DatasetConfig>]`.

        -   set_validation_ds: All model configs can accept a subclass of `DatasetConfig` as their
                validation config. Subclasses can override this method to enable auto-complete
                by replacing `Optional[DatasetConfig]` with `Optional[<subclass of DatasetConfig>]`.

        -   set_test_ds: All model configs can accept a subclass of `DatasetConfig` as their
                test config. Subclasses can override this method to enable auto-complete
                by replacing `Optional[DatasetConfig]` with `Optional[<subclass of DatasetConfig>]`.

        -   set_optim: A build method that supports changes to the Optimizer (and optionally,
                the Scheduler) used for training the model. The function accepts two inputs -

                `cfg`: A subclass of `OptimizerParams` - any OptimizerParams subclass can be used,
                    in order to select an appropriate Optimizer. Examples: AdamParams.

                `sched_cfg`: A subclass of `SchedulerParams` - any SchedulerParams subclass can be used,
                    in order to select an appropriate Scheduler. Examples: CosineAnnealingParams.
                    Note that this argument is optional.

        -   build(): The method which should return a "finalized" ModelConfig dataclass.
                Subclasses *should* always override this method, and update the signature
                of this method with the return type of the Dataclass, so that it enables
                autocomplete for the user.

                Example:
                    def build(self) -> EncDecCTCConfig:
                        return super().build()

        Any additional build methods must be added by subclasses of ModelConfigBuilder.

        Args:
            model_cfg:
        N)r<   train_ds_cfgvalidation_ds_cfgtest_ds_cfg	optim_cfg)selfr<   r   r   r   __init__G   s
   C
zModelConfigBuilder.__init__Ncfgc                 C      || j _d S r8   )r<   r   rA   rC   r   r   r   set_train_ds      zModelConfigBuilder.set_train_dsc                 C   rD   r8   )r<   r   rE   r   r   r   set_validation_ds   rG   z$ModelConfigBuilder.set_validation_dsc                 C   rD   r8   )r<   r   rE   r   r   r   set_test_ds   rG   zModelConfigBuilder.set_test_ds	sched_cfgc           	      C   s   t G dd dt|j}|jjdd }|d	|d dt|}|d urHt G dd dt|j}|jjdd}|d	d|it|}||_|| j	_
d S )
Nc                   @      e Zd ZdS )z8ModelConfigBuilder.set_optim.<locals>.WrappedOptimConfigNr   r   r   r   r   r   r   WrappedOptimConfig       rM   Params )r   r   c                   @   rK   )z8ModelConfigBuilder.set_optim.<locals>.WrappedSchedConfigNrL   r   r   r   r   WrappedSchedConfig   rN   rQ   r   r   )r   r   	__class__r   replacelowervarsr   r   r<   r   )	rA   rC   rJ   rM   
optim_namewrapped_cfgrQ   
sched_namewrapped_sched_cfgr   r   r   	set_optim   s   zModelConfigBuilder.set_optimc                 C   s   t  r8   )NotImplementedErrorrA   r   r   r   _finalize_cfg   s   z ModelConfigBuilder._finalize_cfgreturnc                 C   s   |    | jS r8   )r]   r<   r\   r   r   r   build   s   zModelConfigBuilder.buildr8   )r   r   r   r   rB   r   r	   rF   rH   rI   r   OptimizerParamsSchedulerParamsrZ   r]   r_   r   r   r   r   r;   F   s    Ir;   N)dataclassesr   r   typingr   r   r   	omegaconfr   	nemo.corer   nemo.core.classes.datasetr	   
nemo.utilsr
   r   r   r   r!   r,   r;   r   r   r   r   <module>   s"   