o
    wiL                  $   @   s  d dl m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mZmZmZ d dlmZmZ dedejej fd	d
Z																d&dededeej dee dedededededededed ed!ed"ed#eeeje	   dejej f"d$d%ZdS )'    )OptionalN)Callback)	lightning)Starcoder2Config3BStarcoder2Config7BStarcoder2Config15BStarcoder2Model)
bf16_mixed
fp16_mixedversionreturnc                 C   sb   d}| dkrt t}n| dkrt t}n	| dkrt t}|dus*J d|  t jt|dS )a+  
    A function to create a Starcoder2 models.

    Args:
        version (str): The version of the Starcoder2 model to create. one of ["starcoder2_3b", "starcoder2_7b",
            "starcoder2_15b"].

    Returns:
        run.Config[pl.LightningModule]: Configuration for the Starcoder2 model.
    Nstarcoder2_3bstarcoder2_7bstarcoder2_15bzInvalid version: )config)runConfigr   r   r   r   )r   r    r   d/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/nemo/collections/llm/recipes/starcoder2.pystarcoder2_model    s   
r         F   { 
bf16-mixed    
     tensor_parallelismpipeline_parallelismpipeline_parallelism_typevirtual_pipeline_parallelismcontext_parallelismsequence_parallelism	num_nodesnum_gpus_per_node	max_steps	precisionaccumulate_grad_batcheslimit_test_batcheslimit_val_batcheslog_every_n_stepsval_check_interval	callbacksc                 C   sp   t jtj| |||||ddddd}d}|	dkrt }n|	dkr"t }t jtjd|||
|||||||d|d}|S )	a  
    Configure the NeMo Lightning Trainer for Starcoder2 models.

    This function sets up the distributed training strategy and other training parameters.

    Args:
        tensor_parallelism (int): Degree of tensor model parallelism.
        pipeline_parallelism (int): Degree of pipeline model parallelism.
        pipeline_parallelism_type (Optional[torch.dtype]): Data type for pipeline parallelism.
        virtual_pipeline_parallelism (Optional[int]): Size of virtual pipeline parallelism.
        context_parallelism (int): Degree of context parallelism.
        sequence_parallelism (bool): Whether to use sequence parallelism.
        num_nodes (int): Number of compute nodes to use.
        num_gpus_per_node (int): Number of GPUs per node.
        max_steps (int): Maximum number of training steps.
        precision (str): Precision configuration, one of fp32, 16-mixed or bf16-mixed.
        accumulate_grad_batches (int): Number of steps per gradient accumulation.
        limit_test_batches (int): Limit the number of test batches.
        limit_val_batches (int): Limit the number of validation batches.
        log_every_n_steps (int): Log every n steps.
        val_check_interval (int): Run validation every N steps.
        callbacks (Optional[list[run.Config[Callback]]]): List of callback configurations.

    Returns:
        run.Config[nl.Trainer]: Configuration for the NeMo Lightning Trainer.
    T)
tensor_model_parallel_sizepipeline_model_parallel_sizepipeline_dtype$virtual_pipeline_model_parallel_sizecontext_parallel_sizesequence_parallelgradient_as_bucket_viewckpt_include_optimizerckpt_async_saveckpt_parallel_loadNz16-mixedr   gpuF)acceleratorr-   devicesr(   r)   r*   r+   r&   r$   pluginsstrategyuse_distributed_samplerr,   )r   r   nlMegatronStrategyr
   r	   Trainer)r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r<   precision_plugintrainerr   r   r   starcoder2_trainer7   sF   ,rC   )r   r   NNr   Fr   r   r   r   r   r   r   r   r   N)typingr   lightning.pytorchpytorchplnemo_runr   torch$lightning.pytorch.callbacks.callbackr   nemor   r>   )nemo.collections.llm.gpt.model.starcoder2r   r   r   r   6nemo.collections.llm.recipes.precision.mixed_precisionr	   r
   strr   LightningModuler   intdtypeboollistr@   rC   r   r   r   r   <module>   sx   	

