o
    }oiA                     @   s  d dl mZ d dlmZ d dlmZmZmZmZ d dl	Z	d dl
m  mZ d dl	mZ d dlmZmZmZ d dlmZ d dlmZmZmZ d d	lmZ d d
lmZ ered dlmZ d dlmZ d dl m!Z! eG dd deZ"eG dd de"Z#eG dd de"Z$G dd deZ%e&e%dG dd dej'de%f Z(e)e%dG dd dej'e%df Z*ej+ddddej,fd d!Z-ej+d"d#ddej,fd$d%Z.ej+ddddej,fd&d'Z/ej+d#d"ddej,fd(d)Z0g d*Z1dS )+    )	dataclass)Path)TYPE_CHECKING	AnnotatedCallableOptionalN)nn)	GPTConfigGPTModeltorch_dtype_from_mcore_config)Config)OptimizerModuleioteardown)TransformFns)dtype_from_hfAutoModelForCausalLMAutoTokenizer)TokenizerSpecc                   @   s   e Zd ZU dZdZeed< dZeed< dZeed< dZ	eed	< d
Z
eed< dZeed< dZeed< dZeed< dZeed< dZeed< dZeed< dZeed< dZeed< ejZeed< dZeed< dZeed< dZeed< dZeed < d!S )"ChatGLMConfigzP
    Configuration class for the ChatGLM Config, inheriting from GPTConfig.
       
num_layersi   hidden_sizei5  ffn_hidden_size    num_attention_heads   num_query_groupsg{Gz?init_method_stdg        hidden_dropoutattention_dropoutRMSNormnormalizationFadd_bias_linearTadd_qkv_biasg      ?rotary_percentrotary_interleavedactivation_funcgated_linear_unitropeposition_embedding_type#share_embeddings_and_output_weightsi   make_vocab_size_divisible_byN)__name__
__module____qualname____doc__r   int__annotations__r   r   r   r   r    floatr!   r"   r$   strr%   boolr&   r'   r(   Fsilur)   r   r*   r,   r-   r.    r:   r:   Z/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/llm/gpt/model/chatglm.pyr   $   s(   
 r   c                   @      e Zd ZU dZdZeed< dS )ChatGLM2Config6Bz]
    Configuration class for the ChatGLM2Config6B Config, inheriting from ChatGLMConfig.
    i   
seq_lengthNr/   r0   r1   r2   r>   r3   r4   r:   r:   r:   r;   r=   >      
 r=   c                   @   r<   )ChatGLM3Config6Bz]
    Configuration class for the ChatGLM3Config6B Config, inheriting from ChatGLMConfig.
    i    r>   Nr?   r:   r:   r:   r;   rA   G   r@   rA   c                       sf   e Zd ZdZ				d
deee ee f dee ded dee	e
jge
jf  f fdd	Z  ZS )ChatGLMModelz
    ChatGLM model implementation based on the GPT model architecture.

    This class provides a high-level interface for ChatGLM models,
    implementing the specific architecture and settings needed for ChatGLM models.
    Nconfigoptim	tokenizerr   model_transformc                    s   t  j|pt |||d d S )N)rD   rE   rF   )super__init__r   )selfrC   rD   rE   rF   	__class__r:   r;   rH   X   s   zChatGLMModel.__init__)NNNN)r/   r0   r1   r2   r   r   r   r   r   r   r   ModulerH   __classcell__r:   r:   rJ   r;   rB   P   s    	rB   hfc                   @   sX   e Zd ZdZdefddZdedefddZdd	 Ze	dddZ
e	defddZdS )HFChatGLMImporterz
    Importer for converting Hugging Face ChatGLM models to NeMo format.

    This class handles the conversion of Hugging Face's ChatGLMForCausalLM models
    to NeMo's ChatGLM format, including weight mapping and configuration translation.
    returnc                 C   s   t | j| jdS )z
        Initialize a NeMo ChatGLMModel instance.

        Returns:
            ChatGLMModel: Initialized NeMo Llama model with the appropriate configuration
                        and tokenizer.
        )rE   )rB   rC   rE   rI   r:   r:   r;   initk   s   zHFChatGLMImporter.initoutput_pathc                 C   sj   ddl m} |jt| ddd}|  }| |}| || | || td|  t	|| ~~|S )z
        Apply the conversion from HF to NeMo format.

        Args:
            output_path: Path where the converted model will be saved

        Returns:
            Path: Path to the saved NeMo model
        r   r   Tautotrust_remote_codetorch_dtypez0Converted ChatGLM model to Nemo, model saved to )
transformersr   from_pretrainedr6   rR   
nemo_setupconvert_state	nemo_saveprintr   )rI   rS   r   sourcetargettrainerr:   r:   r;   applyu   s   


zHFChatGLMImporter.applyc              	   C   s,   ddddddddd	}t j|||ttgd
S )aS  
        Convert state dict from HF format to NeMo format.

        Maps the weights from the HF model to the NeMo model according to
        the appropriate mapping scheme.

        Args:
            source: Source HF model
            target: Target NeMo model

        Returns:
            The result of applying the transforms
         embedding.word_embeddings.weight2decoder.layers.*.self_attention.linear_proj.weight&decoder.layers.*.mlp.linear_fc1.weight&decoder.layers.*.mlp.linear_fc2.weight<decoder.layers.*.self_attention.linear_qkv.layer_norm_weight1decoder.layers.*.mlp.linear_fc1.layer_norm_weightdecoder.final_layernorm.weightoutput_layer.weight),transformer.embedding.word_embeddings.weight8transformer.encoder.layers.*.self_attention.dense.weight5transformer.encoder.layers.*.mlp.dense_h_to_4h.weight5transformer.encoder.layers.*.mlp.dense_4h_to_h.weight3transformer.encoder.layers.*.input_layernorm.weight<transformer.encoder.layers.*.post_attention_layernorm.weight*transformer.encoder.final_layernorm.weighttransformer.output_layer.weightmapping
transforms)r   apply_transforms_import_qkv_weight_import_qkv_bias)rI   r^   r_   rs   r:   r:   r;   r[      s   zHFChatGLMImporter.convert_stater   c                 C   s"   ddl m} || t| ddS )z
        Get the tokenizer for the HF model.

        Returns:
            AutoTokenizer: Tokenizer instance initialized from the HF model's tokenizer
        r   r   TrV   )=nemo.collections.common.tokenizers.huggingface.auto_tokenizerr   save_hf_tokenizer_assetsr6   )rI   r   r:   r:   r;   rE      s   zHFChatGLMImporter.tokenizerc                 C   sd   ddl m} |jt| dd}t|j|j|j|j|j	|j
|jt|tjkt|tjkt|d
}|S )a   
        Create a NeMo Baichuan2Config from the HF model config.

        Translates the HF configuration parameters to the equivalent NeMo
        configuration.

        Returns:
            ChatGLMConfig: NeMo configuration for Baichuan2 models
        r   )
AutoConfigTrx   )
r   r   r   r   r>   r   r.   fp16bf16params_dtype)rX   r{   rY   r6   r   r   r   r   r   r>   multi_query_group_numpadded_vocab_sizer   torchfloat16bfloat16)rI   HFAutoConfigr^   outputr:   r:   r;   rC      s   zHFChatGLMImporter.configN)rP   r   )r/   r0   r1   r2   rB   rR   r   ra   r[   propertyrE   r   rC   r:   r:   r:   r;   rO   b   s    
rO   r   c                   @   sJ   e Zd ZdZejdfdddZddedefdd	Zd
d Z	e
dd ZdS )HFChatGLMExporterz
    Exporter for converting NeMo ChatGLMModel to Hugging Face format.

    This class handles the conversion of NeMo's ChatGLMModel to Hugging Face's
    ChatGLMForCausalLM format, including weight mapping and configuration translation.
    NrP   r   c                 C   s   ddl m}m} ddlm} |d u rd}|   |j|dd}|j|d|d}t|d |W  d    S 1 s:w   Y  d S )	Nr   )r{   r   )no_init_weightszTHUDM/chatglm3-6bTrx   rU   r   )	rX   r{   r   transformers.modeling_utilsr   rY   from_configtyperegister_for_auto_class)rI   dtype
model_namer{   r   r   rC   hf_modelr:   r:   r;   rR      s   $zHFChatGLMExporter.initrS   c                 C   sT   |  t| \}}| jt|j|d}| ||}| }|| | j| |S )N)r   )		nemo_loadr6   rR   r   rC   r[   cpusave_pretrainedrE   )rI   rS   target_model_namer^   _r_   r:   r:   r;   ra      s   
zHFChatGLMExporter.applyc                 C   sL   ddddddd}t ttjdd	tjd
tjddtjd
g}tj||||dS )ac  
        Convert state dict from NeMo format to HF format.

        Maps the weights from the NeMo model to the HF model according to
        the appropriate mapping scheme.

        Args:
            source: Source NeMo model
            target: Target HF model

        Returns:
            The target model with weights transferred from source
        rk   rl   rm   rn   ro   rp   )rc   rd   re   rf   rg   rh   rb   rj   )
source_key
target_keyfnri   rq   rr   )_export_qkv_weight_export_qkv_biasr   state_transformr   prune_paddingru   )rI   r^   r_   rs   rt   r:   r:   r;   r[      s4   
zHFChatGLMExporter.convert_statec                 C   s   t t| jjjS )z
        Get the tokenizer from the NeMo model.

        Returns:
            TokenizerSpec: Tokenizer from the NeMo model
        )r   load_contextr6   modelrE   rQ   r:   r:   r;   rE   )  s   zHFChatGLMExporter.tokenizer)rP   r   )N)r/   r0   r1   r2   r   r   rR   r   ra   r[   r   rE   r:   r:   r:   r;   r      s    -r   zBtransformer.encoder.layers.*.self_attention.query_key_value.weightz1decoder.layers.*.self_attention.linear_qkv.weight)r   r   ctxc              	   C   sV  | j j}|j}|j}|| }|j}|j}| }|||d f}	|||d f}
|j|| || || gdd\}}}|j|	 }|j|
 }|j|
 }t	
d||d f|}t|D ]B}t	|||| |d | d d d d f f}t	||||d d d d d f f}t	||||d d d d d f f}qY|||d|   |g}|S )N   r   dimr   )r_   rC   r   r   r   kv_channelssizesplitviewr   emptytype_asrangecatreshape)r   hf_qkv_weightsmegatron_confighead_numr   heads_per_groupr   	head_sizeold_tensor_shapenew_q_tensor_shapenew_kv_tensor_shapeqkvqkv_weightsir:   r:   r;   rv   4  s,   


0(*rv   z@transformer.encoder.layers.*.self_attention.query_key_value.biasz/decoder.layers.*.self_attention.linear_qkv.biasc                 C   s"  | j j}|j}|j}|| }|j}||f}||f}|j|| || || gdd\}	}
}|	j| }	|
j| }
|j| }td|f	|}t
|D ]9}t||	|| |d | d d f f}t||
||d d d f f}t||||d d d f f}qI|||d|   g}|S )Nr   r   r   r   )r_   rC   r   r   r   r   r   r   r   r   r   r   r   )r   hf_qkv_biasr   r   r   r   r   r   r   r   r   r   qkv_biasr   r:   r:   r;   rw   U  s.   


*"$rw   c                    s   | j j}|j}|j}||  |j}|j}|d|  }||||g}t fddt	|D }t
 | d }	t
 d | d }
|| d|}||	 d|}||
 d|}tj|||fddS )Nr   c                    ,   g | ]}t  d  |  d  |   qS r   r   arange.0r   r   r:   r;   
<listcomp>      z&_export_qkv_weight.<locals>.<listcomp>r   r   r   )r^   rC   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   qkv_total_dimq_slicek_slicev_sliceq_weightk_weightv_weightr:   r   r;   r   v  s&   
r   c                    s   | j j}|j}|j}||  |j}|d|  }|||g}t fddt|D }t	 | d }t	 d | d }	|| d}
|| d}||	 d}t|
||fS )Nr   c                    r   r   r   r   r   r:   r;   r     r   z$_export_qkv_bias.<locals>.<listcomp>r   r   )
r^   rC   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   q_biask_biasv_biasr:   r   r;   r     s0   
r   )r   r=   rA   rB   )2dataclassesr   pathlibr   typingr   r   r   r   r   torch.nn.functionalr   
functionalr8   #nemo.collections.llm.gpt.model.baser	   r
   r   nemo.collections.llm.utilsr   nemo.lightningr   r   r   nemo.lightning.io.stater   nemo.lightning.pytorch.utilsr   rX   r   ry   r   1nemo.collections.common.tokenizers.tokenizer_specr   r   r=   rA   rB   model_importerModelConnectorrO   model_exporterr   r   TransformCTXrv   rw   r   r   __all__r:   r:   r:   r;   <module>   s^   

q_ 