
    ^jP                        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	 ddl
mZmZ dd	lmZmZmZmZmZ dd
lmZmZmZmZmZmZ  ej4                  e      Z ed      e G d de                    Z G d de      Z G d de      Z G d de      Z  G d de      Z! G d de      Z" G d de      Z# G d de      Z$ G d de      Z% G d d e      Z& G d! d"e      Z'g d#Z(y$)%    )strict   )PreTrainedConfig)CausalLMOutputWithPast)RopeParameters)Unpack)auto_docstringlogging   )DeepseekV3DecoderLayerDeepseekV3MLPDeepseekV3MoEDeepseekV3PreTrainedModelDeepseekV3TopkRouter)Qwen3AttentionQwen3ForCausalLM
Qwen3ModelQwen3RMSNormQwen3RotaryEmbeddingTransformersKwargszrednote-hilab/dots.llm1.base)
checkpointc                       e Zd ZU dZdZdgZdddddddddddddddd	Zd
gdgfddgdgfdgdgfdZdddddZddi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d z  ed!<   d Zed z  ed"<   d Zed z  ed<   d#Zed z  ed$<   d#Zed z  ed%<   d Zed z  ed&<   d'Zed z  ed(<   d)Zed z  ed*<   d+Zeed,<   d-Zeed.<   d/Zeed0<   d1Z eed2<   d3Z!eed4<   d)Z"eed5<   d Z#e$e%z  d z  ed6<   d)Z&eed7<   d8Z'eez  d z  ed9<   d:Z(eed;<   d<Z)ed z  ed=<   dZ*ed z  ed><   d Z+e,e   d z  ed?<   d Z-ed z  ed@<   d Z.ed z  edA<   d Z/ee,e   z  d z  edB<    fdCZ0 xZ1S )DDots1Configa  
    n_group (`int`, *optional*, defaults to 1):
        Number of groups for routed experts.
    first_k_dense_replace (`int`, *optional*, defaults to 0):
        Number of dense layers at the beginning of the model before the first MoE layer.

    Examples:

    ```python
    >>> from transformers import Dots1Model, Dots1Config
    >>> # Initializing a Dots1 style configuration
    >>> configuration = Dots1Config()
    >>> # Accessing the model configuration
    >>> configuration = model.config
    ```
    dots1past_key_valuescolwiserowwisereplicated_with_grad_allreducepacked_colwisemoe_tp_experts)zlayers.*.self_attn.q_projzlayers.*.self_attn.k_projzlayers.*.self_attn.v_projzlayers.*.self_attn.o_projzlayers.*.self_attn.q_normzlayers.*.self_attn.k_norm!layers.*.mlp.experts.gate_up_projlayers.*.mlp.experts.down_projlayers.*.mlp.expertsz%layers.*.mlp.shared_experts.gate_projz#layers.*.mlp.shared_experts.up_projz%layers.*.mlp.shared_experts.down_projzlayers.*.mlp.gate_projzlayers.*.mlp.up_projzlayers.*.mlp.down_proj	input_idsinputs_embedshidden_statesattention_mask)embed_tokenslayersnorm	ep_routergrouped_gemm)zlayers.*.mlp.gater!   r"   r#   num_local_expertsn_routed_expertsi R 
vocab_sizei   hidden_sizei*  intermediate_sizei  moe_intermediate_size>   num_hidden_layers    num_attention_headsNnum_key_value_headsn_shared_experts   n_group
topk_groupnum_experts_per_tokr   first_k_dense_replaceFnorm_topk_probsilu
hidden_acti   max_position_embeddingsg{Gz?initializer_rangegư>rms_norm_epsT	use_cachetie_word_embeddingsrope_parametersattention_biasg        attention_dropoutg      ?routed_scaling_factori   sliding_windowmax_window_layerslayer_typespad_token_idbos_token_ideos_token_idc                 
   | j                   | j                  | _         | j                  Et        | j                        D cg c]!  }| j
                  || j                  k\  rdnd# c}| _        t        |    di | y c c}w )Nsliding_attentionfull_attention )	r7   r6   rL   ranger4   rJ   rK   super__post_init__)selfkwargsi	__class__s      r/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/dots1/modular_dots1.pyrV   zDots1Config.__post_init__   s    ##+'+'?'?D$#
 t556	   &&2qD<R<R7R $%& D 	'' s   &B )2__name__
__module____qualname____doc__
model_typekeys_to_ignore_at_inferencebase_model_tp_planbase_model_pp_planbase_model_ep_planattribute_mapr/   int__annotations__r0   r1   r2   r4   r6   r7   r8   r.   r:   r;   r<   r=   r>   boolr@   strrA   rB   floatrC   rD   rE   rF   r   dictrG   rH   rI   rJ   rK   rL   listrM   rN   rO   rV   __classcell__rZ   s   @r[   r   r   )   sJ   " J#4"5 &/%.%.%.%E%E-=*3 01:/81:"+ )"+& &(9:#%568IJ!"_$56 )-;*8 0	 	/M JK"s"!%3%s!!&(t(#'cDj'#'cDj'GS4ZJd
&*t*()3:)"'ND4K'J#'S'#u#L%It %%48O^d*T18 ND ,/us{T)/#&5&!%NC$J%$&sTz&$(KcT!(#L#*##L#*#+/L#S	/D(/( (    r   c                       e Zd Zy)Dots1RMSNormNr\   r]   r^   rS   ro   r[   rq   rq          ro   rq   c                       e Zd Zy)Dots1RotaryEmbeddingNrr   rS   ro   r[   ru   ru      rs   ro   ru   c                       e Zd Zy)Dots1AttentionNrr   rS   ro   r[   rw   rw      rs   ro   rw   c                       e Zd Zy)Dots1MLPNrr   rS   ro   r[   ry   ry      rs   ro   ry   c                       e Zd Zy)Dots1TopkRouterNrr   rS   ro   r[   r{   r{      rs   ro   r{   c                       e Zd Zy)Dots1MoENrr   rS   ro   r[   r}   r}      rs   ro   r}   c                       e Zd Zy)Dots1DecoderLayerNrr   rS   ro   r[   r   r      rs   ro   r   c                       e Zd ZdZy)Dots1PreTrainedModelN)r\   r]   r^   "_keys_to_ignore_on_load_unexpectedrS   ro   r[   r   r      s    )-&ro   r   c                       e Zd Zy)
Dots1ModelNrr   rS   ro   r[   r   r      rs   ro   r   c                   .     e Zd Zdee   def fdZ xZS )Dots1ForCausalLMsuper_kwargsreturnc                 "    t        |   di |S )a~  
        labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
            config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
            (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.

        Example:

        ```python
        >>> from transformers import AutoTokenizer, Dots1ForCausalLM

        >>> model = Dots1ForCausalLM.from_pretrained("rednote-hilab/dots1.llm1.inst")
        >>> tokenizer = AutoTokenizer.from_pretrained("rednote-hilab/dots1.llm1.inst")

        >>> prompt = "Hey, are you conscious? Can you talk to me?"
        >>> inputs = tokenizer(prompt, return_tensors="pt")

        >>> # Generate
        >>> generate_ids = model.generate(inputs.input_ids, max_length=30)
        >>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
        "Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."
        ```rS   )rU   forward)rW   r   rZ   s     r[   r   zDots1ForCausalLM.forward   s    4 w...ro   )r\   r]   r^   r   r   r   r   rm   rn   s   @r[   r   r      s%    /12/ 
 / /ro   r   )r   r   r   r   N))huggingface_hub.dataclassesr   configuration_utilsr   modeling_outputsr   modeling_rope_utilsr   processing_utilsr   utilsr	   r
    deepseek_v3.modeling_deepseek_v3r   r   r   r   r   qwen3.modeling_qwen3r   r   r   r   r   r   
get_loggerr\   loggerr   rq   ru   rw   ry   r{   r}   r   r   r   r   __all__rS   ro   r[   <module>r      s    / 3 6 1 & ,   
		H	% 9:a(" a(  ;a(H	< 		/ 		^ 		} 		* 		} 		. 	.4 .	 	/' /<ro   