o
    TÃi´	  ã                   @   sH   d dl Z d dlmZmZmZ d dlmZmZ dd„ Zdd„ Zdd	„ Z	dS )
é    N)Úget_full_hp_paramÚget_full_hp_gradÚget_hp_fragment_mapping)Úset_full_hp_paramÚset_full_hp_gradc	                 C   s:   t | |||ƒ}	|	D ]\}
}t|
||||||||ƒ	|
_q	d S ©N)Ú_init_lp_to_hp_mappingr   Ú_hp_mapping)Úlp_param_listÚflat_hp_partitionÚgradient_dictÚoffload_gradient_dictÚuse_offloadÚparam_group_indexÚpartition_startÚpartition_sizeÚdp_groupÚlocal_lp_param_and_offsetÚlp_paramÚlp_start© r   ú[/home/ubuntu/.local/lib/python3.10/site-packages/deepspeed/utils/mixed_precision_linkage.pyÚlink_hp_params   s   
þÿr   c                 C   s*   | D ]}|j d ur|j  ||| ¡ qd S r   )r	   Úset_optim_state_fragment)r
   r   Úoptimizer_stateÚlpr   r   r   Ú#lazy_init_hp_params_optimizer_state   s
   
€þr   c                 C   s²   d}g }|| }d}t | ƒD ]H\}}	d |	_||	_t t|	¡|	_t t|	¡|	_t t|	¡|	_t t|	¡|	_||	 	¡  }
||k rP|
|krP| 
|	|f¡ ||	_|d7 }||	 	¡ 7 }q|S )Nr   é   )Ú	enumerater	   Ú	_dp_groupÚtypesÚ
MethodTyper   r   r   r   ÚnumelÚappendÚ_index_in_param_group)r
   r   r   r   Úcurrent_offsetÚparam_and_offset_listÚpartition_endÚindex_in_param_groupÚir   Úlp_param_endr   r   r   r      s$   r   )
r    Údeepspeed.utilsr   r   r   r   r   r   r   r   r   r   r   r   Ú<module>   s   
