o
    wi                     @   s   d dl mZ d dlmZmZmZmZm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mZmZmZ d d	lmZ erJd d
lmZ edZedddZG dd de
ZeededefddZdS )    )contextmanager)TYPE_CHECKING	GeneratorLiteralOptionalTypeVarN)MixedPrecision)nn)	Optimizer)	to_fabric)DtypeConfigMegatronMixedPrecisionget_optim_config"update_config_with_dtype_overrides)logging)ModelParallelConfigAnyTConfigTr   )boundc                5   @   s6  e Zd ZdZ																								
	d:ded dejdejdejdedede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d!ed"ed#ed$e
d%e
d&df4d'd(Zd)ed&efd*d+Zd)ed&efd,d-Zd.ed&efd/d0Zd1ejd&ejfd2d3Zd4ed&efd5d6Zed&ed7 fd8d9ZdS );FabricMegatronMixedPrecisionzFabric plugin for mixed precision training with Megatron models.

    Handles precision conversions and mixed precision training settings
    in the Fabric training framework.
    NFTr      most_recent              ?     	precision)16-mixed
bf16-mixed32params_dtypepipeline_dtypeautocast_dtypeautocast_enabledgrad_reduce_in_fp32fp8
fp8_recipefirst_last_layers_bf16num_layers_at_start_in_bf16num_layers_at_end_in_bf16!reuse_grad_buf_for_mxfp8_param_ag
fp8_marginfp8_amax_history_lenfp8_amax_compute_algo	fp8_wgradfp8_dot_product_attentionfp8_multi_head_attention
fp8_paramsfp8_param_gatherfp16_loss_scalefp16_initial_loss_scalefp16_min_loss_scalefp16_loss_scale_windowfp16_hysteresisreturnc                 C   sZ  |d urt d |d ur||krtd|}n|d u rd}t|tr't|}|dv r.tjntj}t	d%i d|dv d|dv d	|dv d
|pHtjd|pM|d|pR|d|d|d|d|d|	d|
d|d|d|d|d|d|d|d|d|d|d|d|d|d |d!|| _
| j
jrd"| _n| j
jrd#| _nd$| _d | _d S )&Nz^fp8_params is deprecated and will be removed in a future release, use fp8_param_gather insteadzaGetting conflicting values for fp8_params and fp8_param_gather. Please only set fp8_param_gather.F)bf16r   fp32)r:   r   fp16)r;   z
fp16-mixed16r   r9   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   	fp8_paramr2   
loss_scaleinitial_loss_scalemin_loss_scaleloss_scale_window
hysteresisr   r   z32-true )r   warning
ValueError
isinstanceintstrtorchbfloat16float32r   dtype_configr;   r   r9   scaler)selfr   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   dtyperC   rC   Z/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/nemo/lightning/fabric/plugins.py__init__1   s   
	

z%FabricMegatronMixedPrecision.__init__datac                 C      |S )zConvert model inputs (forward) to the floating point precision type of this plugin.

        Note: MegatronStrategy will take care of only doing this when:
            mpu.is_pipeline_first_stage()

        rC   rN   rR   rC   rC   rP   convert_input      z*FabricMegatronMixedPrecision.convert_inputc                 C   rS   )zConvert outputs to the floating point precision type expected after model's forward.

        Note: MegatronStrategy will take care of only doing this when:
            mpu.is_pipeline_first_stage()

        rC   rT   rC   rC   rP   convert_output   rV   z+FabricMegatronMixedPrecision.convert_outputconfigc                 C   s   t | j|S )zConvert the config to the precision type this plugin handles.

        This is optional and depends on the precision limitations during optimization.
        )r   rL   )rN   rX   rC   rC   rP   convert_config   s   z+FabricMegatronMixedPrecision.convert_configmodulec                 C   s   t |ds|S ddlm} ddlm} | jjs| jjrH||j}| jj|_| jj|_t |dr>t	|j|s<|||j|_|S t	||sH|||}|S )zConvert the module parameters to the precision type this plugin handles.

        This is optional and depends on the precision limitations during optimization.

        rZ   r   )Float16Module)get_model_config)
hasattr megatron.core.transformer.moduler[   megatron.core.utilsr\   rL   r;   r9   rZ   rF   )rN   rZ   r[   r\   rX   rC   rC   rP   convert_module   s   






z+FabricMegatronMixedPrecision.convert_module	optimizerc                 C   s>   t |D ]}|j| jjksJ d|j| jjksJ dq|S )zConvert the optimizer parameters to the precision type this plugin handles.

        This is optional and depends on the precision limitations during optimization.
        z BF16 model/optim config mismatchz FP16 model/optim config mismatch)r   r9   rL   r;   )rN   ra   optim_configrC   rC   rP   convert_optimizer   s   z.FabricMegatronMixedPrecision.convert_optimizer)NNNc                 c   s    zdV  W dS w )zINo explicit precision casting. Inputs are supposed to be manually casted.NrC   )rN   rC   rC   rP   forward_context   s   z,FabricMegatronMixedPrecision.forward_context)NNNFTNNFr   r   Fr   r   r   TFFNNNr   r   r   r   )__name__
__module____qualname____doc__r   rI   rO   boolrH   r   rG   floatrQ   r   rU   rW   r   rY   r	   Moduler`   r
   rc   r   r   rd   rC   rC   rC   rP   r   *   s    		

S		
r   pluginr8   c                 C   s   t | jdS )N)r   )r   r   )rl   rC   rC   rP   !_convert_megatron_mixed_precision   s   rm   )
contextlibr   typingr   r   r   r   r   rI   "lightning.fabric.plugins.precisionr   r	   torch.optimr
    nemo.lightning.fabric.conversionr   .nemo.lightning.pytorch.plugins.mixed_precisionr   r   r   r   
nemo.utilsr   #megatron.core.model_parallel_configr   r   r   r   registerrm   rC   rC   rC   rP   <module>   s"    !