Restore Model Metadata and Tokenizer Logic
Browse files- config.json +192 -0
- configuration_kimi_k25.py +123 -0
- generation_config.json +4 -0
- modeling_kimi_k25.py +1248 -0
- preprocessor_config.json +30 -0
- tokenizer_config.json +216 -0
config.json
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"KimiK25ForConditionalGeneration"
|
| 4 |
+
],
|
| 5 |
+
"auto_map": {
|
| 6 |
+
"AutoConfig": "configuration_kimi_k25.KimiK25Config",
|
| 7 |
+
"AutoModel": "modeling_kimi_k25.KimiK25ForConditionalGeneration",
|
| 8 |
+
"AutoModelForCausalLM": "modeling_kimi_k25.KimiK25ForConditionalGeneration"
|
| 9 |
+
},
|
| 10 |
+
"bos_token_id": 163584,
|
| 11 |
+
"dtype": "bfloat16",
|
| 12 |
+
"eos_token_id": 163585,
|
| 13 |
+
"ignore_index": -100,
|
| 14 |
+
"media_placeholder_token_id": 163605,
|
| 15 |
+
"model_type": "kimi_k25",
|
| 16 |
+
"pad_token_id": 163839,
|
| 17 |
+
"text_config": {
|
| 18 |
+
"_name_or_path": "",
|
| 19 |
+
"add_cross_attention": false,
|
| 20 |
+
"architectures": [
|
| 21 |
+
"DeepseekV3ForCausalLM"
|
| 22 |
+
],
|
| 23 |
+
"attention_bias": false,
|
| 24 |
+
"attention_dropout": 0.0,
|
| 25 |
+
"auto_map": {
|
| 26 |
+
"AutoConfig": "configuration_deepseek.DeepseekV3Config",
|
| 27 |
+
"AutoModel": "modeling_deepseek.DeepseekV3Model",
|
| 28 |
+
"AutoModelForCausalLM": "modeling_deepseek.DeepseekV3ForCausalLM"
|
| 29 |
+
},
|
| 30 |
+
"aux_loss_alpha": 0.001,
|
| 31 |
+
"bad_words_ids": null,
|
| 32 |
+
"begin_suppress_tokens": null,
|
| 33 |
+
"bos_token_id": 163584,
|
| 34 |
+
"chunk_size_feed_forward": 0,
|
| 35 |
+
"cross_attention_hidden_size": null,
|
| 36 |
+
"decoder_start_token_id": null,
|
| 37 |
+
"diversity_penalty": 0.0,
|
| 38 |
+
"do_sample": false,
|
| 39 |
+
"dtype": "bfloat16",
|
| 40 |
+
"early_stopping": false,
|
| 41 |
+
"encoder_no_repeat_ngram_size": 0,
|
| 42 |
+
"eos_token_id": 163585,
|
| 43 |
+
"ep_size": 1,
|
| 44 |
+
"exponential_decay_length_penalty": null,
|
| 45 |
+
"finetuning_task": null,
|
| 46 |
+
"first_k_dense_replace": 1,
|
| 47 |
+
"forced_bos_token_id": null,
|
| 48 |
+
"forced_eos_token_id": null,
|
| 49 |
+
"hidden_act": "silu",
|
| 50 |
+
"hidden_size": 7168,
|
| 51 |
+
"id2label": {
|
| 52 |
+
"0": "LABEL_0",
|
| 53 |
+
"1": "LABEL_1"
|
| 54 |
+
},
|
| 55 |
+
"initializer_range": 0.02,
|
| 56 |
+
"intermediate_size": 18432,
|
| 57 |
+
"is_decoder": false,
|
| 58 |
+
"is_encoder_decoder": false,
|
| 59 |
+
"kv_lora_rank": 512,
|
| 60 |
+
"label2id": {
|
| 61 |
+
"LABEL_0": 0,
|
| 62 |
+
"LABEL_1": 1
|
| 63 |
+
},
|
| 64 |
+
"length_penalty": 1.0,
|
| 65 |
+
"max_length": 20,
|
| 66 |
+
"max_position_embeddings": 262144,
|
| 67 |
+
"min_length": 0,
|
| 68 |
+
"model_type": "kimi_k2",
|
| 69 |
+
"moe_intermediate_size": 2048,
|
| 70 |
+
"moe_layer_freq": 1,
|
| 71 |
+
"n_group": 1,
|
| 72 |
+
"n_routed_experts": 384,
|
| 73 |
+
"n_shared_experts": 1,
|
| 74 |
+
"no_repeat_ngram_size": 0,
|
| 75 |
+
"norm_topk_prob": true,
|
| 76 |
+
"num_attention_heads": 64,
|
| 77 |
+
"num_beam_groups": 1,
|
| 78 |
+
"num_beams": 1,
|
| 79 |
+
"num_experts_per_tok": 8,
|
| 80 |
+
"num_hidden_layers": 61,
|
| 81 |
+
"num_key_value_heads": 64,
|
| 82 |
+
"num_nextn_predict_layers": 0,
|
| 83 |
+
"num_return_sequences": 1,
|
| 84 |
+
"output_attentions": false,
|
| 85 |
+
"output_hidden_states": false,
|
| 86 |
+
"output_scores": false,
|
| 87 |
+
"pad_token_id": 163839,
|
| 88 |
+
"prefix": null,
|
| 89 |
+
"pretraining_tp": 1,
|
| 90 |
+
"problem_type": null,
|
| 91 |
+
"pruned_heads": {},
|
| 92 |
+
"q_lora_rank": 1536,
|
| 93 |
+
"qk_nope_head_dim": 128,
|
| 94 |
+
"qk_rope_head_dim": 64,
|
| 95 |
+
"quantization_config": {
|
| 96 |
+
"config_groups": {
|
| 97 |
+
"group_0": {
|
| 98 |
+
"input_activations": null,
|
| 99 |
+
"output_activations": null,
|
| 100 |
+
"targets": [
|
| 101 |
+
"Linear"
|
| 102 |
+
],
|
| 103 |
+
"weights": {
|
| 104 |
+
"actorder": null,
|
| 105 |
+
"block_structure": null,
|
| 106 |
+
"dynamic": false,
|
| 107 |
+
"group_size": 32,
|
| 108 |
+
"num_bits": 4,
|
| 109 |
+
"observer": "minmax",
|
| 110 |
+
"observer_kwargs": {},
|
| 111 |
+
"strategy": "group",
|
| 112 |
+
"symmetric": true,
|
| 113 |
+
"type": "int"
|
| 114 |
+
}
|
| 115 |
+
}
|
| 116 |
+
},
|
| 117 |
+
"format": "pack-quantized",
|
| 118 |
+
"ignore": [
|
| 119 |
+
"lm_head",
|
| 120 |
+
"re:.*self_attn.*",
|
| 121 |
+
"re:.*shared_experts.*",
|
| 122 |
+
"re:.*mlp\\.(gate|up|gate_up|down)_proj.*"
|
| 123 |
+
],
|
| 124 |
+
"kv_cache_scheme": null,
|
| 125 |
+
"quant_method": "compressed-tensors",
|
| 126 |
+
"quantization_status": "compressed"
|
| 127 |
+
},
|
| 128 |
+
"remove_invalid_values": false,
|
| 129 |
+
"repetition_penalty": 1.0,
|
| 130 |
+
"return_dict": true,
|
| 131 |
+
"return_dict_in_generate": false,
|
| 132 |
+
"rms_norm_eps": 1e-05,
|
| 133 |
+
"rope_scaling": {
|
| 134 |
+
"beta_fast": 32.0,
|
| 135 |
+
"beta_slow": 1.0,
|
| 136 |
+
"factor": 64.0,
|
| 137 |
+
"mscale": 1.0,
|
| 138 |
+
"mscale_all_dim": 1.0,
|
| 139 |
+
"original_max_position_embeddings": 4096,
|
| 140 |
+
"type": "yarn"
|
| 141 |
+
},
|
| 142 |
+
"rope_theta": 50000.0,
|
| 143 |
+
"routed_scaling_factor": 2.827,
|
| 144 |
+
"scoring_func": "sigmoid",
|
| 145 |
+
"sep_token_id": null,
|
| 146 |
+
"seq_aux": true,
|
| 147 |
+
"suppress_tokens": null,
|
| 148 |
+
"task_specific_params": null,
|
| 149 |
+
"temperature": 1.0,
|
| 150 |
+
"tf_legacy_loss": false,
|
| 151 |
+
"tie_encoder_decoder": false,
|
| 152 |
+
"tie_word_embeddings": false,
|
| 153 |
+
"tokenizer_class": null,
|
| 154 |
+
"top_k": 50,
|
| 155 |
+
"top_p": 1.0,
|
| 156 |
+
"topk_group": 1,
|
| 157 |
+
"topk_method": "noaux_tc",
|
| 158 |
+
"torchscript": false,
|
| 159 |
+
"transformers_version": "4.56.2",
|
| 160 |
+
"typical_p": 1.0,
|
| 161 |
+
"use_bfloat16": false,
|
| 162 |
+
"use_cache": true,
|
| 163 |
+
"v_head_dim": 128,
|
| 164 |
+
"vocab_size": 163840
|
| 165 |
+
},
|
| 166 |
+
"tie_word_embeddings": false,
|
| 167 |
+
"use_unified_vision_chunk": true,
|
| 168 |
+
"video_placeholder": "<|kimi_k25_video_placeholder|>",
|
| 169 |
+
"vision_config": {
|
| 170 |
+
"_attn_implementation": "flash_attention_2",
|
| 171 |
+
"init_pos_emb_height": 64,
|
| 172 |
+
"init_pos_emb_time": 4,
|
| 173 |
+
"init_pos_emb_width": 64,
|
| 174 |
+
"merge_kernel_size": [
|
| 175 |
+
2,
|
| 176 |
+
2
|
| 177 |
+
],
|
| 178 |
+
"merge_type": "sd2_tpool",
|
| 179 |
+
"mm_hidden_size": 1152,
|
| 180 |
+
"mm_projector_type": "patchmerger",
|
| 181 |
+
"patch_size": 14,
|
| 182 |
+
"pos_emb_type": "divided_fixed",
|
| 183 |
+
"projector_hidden_act": "gelu",
|
| 184 |
+
"projector_ln_eps": 1e-05,
|
| 185 |
+
"text_hidden_size": 7168,
|
| 186 |
+
"video_attn_type": "spatial_temporal",
|
| 187 |
+
"vt_hidden_size": 1152,
|
| 188 |
+
"vt_intermediate_size": 4304,
|
| 189 |
+
"vt_num_attention_heads": 16,
|
| 190 |
+
"vt_num_hidden_layers": 27
|
| 191 |
+
}
|
| 192 |
+
}
|
configuration_kimi_k25.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers.configuration_utils import PretrainedConfig
|
| 2 |
+
|
| 3 |
+
try:
|
| 4 |
+
from configuration_deepseek import DeepseekV3Config
|
| 5 |
+
except ImportError:
|
| 6 |
+
from .configuration_deepseek import DeepseekV3Config
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class KimiK25VisionConfig(PretrainedConfig):
|
| 10 |
+
|
| 11 |
+
def __init__(
|
| 12 |
+
self,
|
| 13 |
+
patch_size: int = 14,
|
| 14 |
+
init_pos_emb_height: int = 64,
|
| 15 |
+
init_pos_emb_width: int = 64,
|
| 16 |
+
init_pos_emb_time: int = 4,
|
| 17 |
+
pos_emb_type: str = 'divided_fixed',
|
| 18 |
+
vt_num_attention_heads: int = 16,
|
| 19 |
+
vt_num_hidden_layers: int = 27,
|
| 20 |
+
vt_hidden_size: int = 1152,
|
| 21 |
+
vt_intermediate_size: int = 4304,
|
| 22 |
+
merge_kernel_size: tuple = (2, 2),
|
| 23 |
+
video_attn_type: str = 'spatial_temporal',
|
| 24 |
+
merge_type: str = 'sd2_tpool',
|
| 25 |
+
_attn_implementation: str = 'flash_attention_2',
|
| 26 |
+
# MM Projector parameters
|
| 27 |
+
mm_projector_type: str = 'patchmerger',
|
| 28 |
+
mm_hidden_size: int | None = None,
|
| 29 |
+
projector_hidden_act: str = "gelu",
|
| 30 |
+
projector_ln_eps: float = 1e-5,
|
| 31 |
+
# Other parameters
|
| 32 |
+
ignore_index: int = -100,
|
| 33 |
+
media_placeholder_token_id: int = 163605,
|
| 34 |
+
pad_token_id: int = 0,
|
| 35 |
+
use_unified_vision_chunk: bool = True,
|
| 36 |
+
video_placeholder="<|kimi_k25_video_placeholder|>",
|
| 37 |
+
text_hidden_size=7168,
|
| 38 |
+
**vision_config_kwargs):
|
| 39 |
+
|
| 40 |
+
self.patch_size = patch_size
|
| 41 |
+
self.init_pos_emb_height = init_pos_emb_height
|
| 42 |
+
self.init_pos_emb_width = init_pos_emb_width
|
| 43 |
+
self.init_pos_emb_time = init_pos_emb_time
|
| 44 |
+
self.pos_emb_type = pos_emb_type
|
| 45 |
+
self.vt_num_attention_heads = vt_num_attention_heads
|
| 46 |
+
self.vt_num_hidden_layers = vt_num_hidden_layers
|
| 47 |
+
self.vt_hidden_size = vt_hidden_size
|
| 48 |
+
self.vt_intermediate_size = vt_intermediate_size
|
| 49 |
+
self.merge_kernel_size = merge_kernel_size
|
| 50 |
+
self.video_attn_type = video_attn_type
|
| 51 |
+
self.merge_type = merge_type
|
| 52 |
+
self._attn_implementation = _attn_implementation
|
| 53 |
+
|
| 54 |
+
# MM Projector config
|
| 55 |
+
self.mm_projector_type = mm_projector_type
|
| 56 |
+
self.mm_hidden_size = mm_hidden_size if mm_hidden_size is not None else vt_hidden_size
|
| 57 |
+
self.projector_hidden_act = projector_hidden_act
|
| 58 |
+
self.projector_ln_eps = projector_ln_eps
|
| 59 |
+
self.text_hidden_size = text_hidden_size
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
class KimiK25Config(PretrainedConfig):
|
| 63 |
+
"""Kimi-K2.5 model configuration.
|
| 64 |
+
|
| 65 |
+
Args:
|
| 66 |
+
text_config (dict | DeepseekV3Config): Configuration for the text model.
|
| 67 |
+
|
| 68 |
+
Vision Tower Parameters (from MoonViT3dConfig):
|
| 69 |
+
patch_size (int): Patch size for vision tower.
|
| 70 |
+
init_pos_emb_height (int): Initial position embedding height.
|
| 71 |
+
init_pos_emb_width (int): Initial position embedding width.
|
| 72 |
+
init_pos_emb_time (int): Initial position embedding time dimension.
|
| 73 |
+
pos_emb_type (str): Type of position embedding.
|
| 74 |
+
vt_num_attention_heads (int): Number of attention heads in vision tower.
|
| 75 |
+
vt_num_hidden_layers (int): Number of hidden layers in vision tower.
|
| 76 |
+
vt_hidden_size (int): Hidden size of vision tower.
|
| 77 |
+
vt_intermediate_size (int): Intermediate size in vision tower FFN.
|
| 78 |
+
merge_kernel_size (tuple): Kernel size for patch merging.
|
| 79 |
+
video_attn_type (str): Type of video attention.
|
| 80 |
+
merge_type (str): Type of merge operation.
|
| 81 |
+
_attn_implementation (str): Attention implementation type.
|
| 82 |
+
|
| 83 |
+
MM Projector Parameters (from MultiModalProjectorConfig):
|
| 84 |
+
mm_projector_type (str): Type of multimodal projector.
|
| 85 |
+
mm_hidden_size (int): Hidden size from vision tower (should match vt_hidden_size).
|
| 86 |
+
projector_hidden_act (str): Activation function for projector.
|
| 87 |
+
projector_ln_eps (float): Layer norm epsilon for projector.
|
| 88 |
+
|
| 89 |
+
Other Parameters:
|
| 90 |
+
ignore_index (int): The ignore index for the loss function.
|
| 91 |
+
media_placeholder_token_id (int): The token ID to use for media placeholders.
|
| 92 |
+
pad_token_id (int): The token ID to use for padding.
|
| 93 |
+
"""
|
| 94 |
+
|
| 95 |
+
model_type = "kimi_k25"
|
| 96 |
+
|
| 97 |
+
def __init__(
|
| 98 |
+
self,
|
| 99 |
+
text_config: dict | DeepseekV3Config = None,
|
| 100 |
+
vision_config: dict | KimiK25VisionConfig = None,
|
| 101 |
+
# Other parameters
|
| 102 |
+
ignore_index: int = -100,
|
| 103 |
+
media_placeholder_token_id: int = 163605,
|
| 104 |
+
pad_token_id: int = 0,
|
| 105 |
+
use_unified_vision_chunk: bool = True,
|
| 106 |
+
video_placeholder="<|kimi_k25_video_placeholder|>",
|
| 107 |
+
**kwargs,
|
| 108 |
+
):
|
| 109 |
+
if isinstance(text_config, dict):
|
| 110 |
+
text_config = DeepseekV3Config(**text_config)
|
| 111 |
+
if isinstance(vision_config, dict):
|
| 112 |
+
vision_config = KimiK25VisionConfig(**vision_config)
|
| 113 |
+
self.text_config = text_config
|
| 114 |
+
self.vision_config = vision_config
|
| 115 |
+
# Other config
|
| 116 |
+
self.ignore_index = ignore_index
|
| 117 |
+
self.media_placeholder_token_id = media_placeholder_token_id
|
| 118 |
+
self.use_unified_vision_chunk = use_unified_vision_chunk
|
| 119 |
+
self.video_placeholder = video_placeholder
|
| 120 |
+
if getattr(self.text_config, "quantization_config", None) is not None:
|
| 121 |
+
self.quantization_config = self.text_config.quantization_config
|
| 122 |
+
|
| 123 |
+
super().__init__(pad_token_id=pad_token_id, **kwargs)
|
generation_config.json
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"max_length": 262144,
|
| 3 |
+
"eos_token_id": 163586
|
| 4 |
+
}
|
modeling_kimi_k25.py
ADDED
|
@@ -0,0 +1,1248 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2025-2026 The Moonshot AI Team, DeepSeek-AI, and HuggingFace Inc. team. All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# The code is based on llava (llava/modeling_llava.py) and DeepSeek-V3 (DeepSeek-V3/modeling_deepseek.py), but modified for Kimi-K2.5.
|
| 5 |
+
#
|
| 6 |
+
# Licensing Information:
|
| 7 |
+
# - Code derived from llava (llava/modeling_llava.py) and DeepSeek-V3 (DeepSeek-V3/modeling_deepseek.py) is licensed under the Apache License, Version 2.0.
|
| 8 |
+
# - Other parts of the code are licensed under the MIT License.
|
| 9 |
+
#
|
| 10 |
+
# Apache License, Version 2.0:
|
| 11 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 12 |
+
# you may not use this file except in compliance with the License.
|
| 13 |
+
# You may obtain a copy of the License at
|
| 14 |
+
#
|
| 15 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 16 |
+
#
|
| 17 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 18 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 19 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 20 |
+
# See the License for the specific language governing permissions and
|
| 21 |
+
# limitations under the License.
|
| 22 |
+
#
|
| 23 |
+
# MIT License:
|
| 24 |
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 25 |
+
# of this software and associated documentation files (the "Software"), to deal
|
| 26 |
+
# in the Software without restriction, including without limitation the rights
|
| 27 |
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 28 |
+
# copies of the Software, and to permit persons to whom the Software is
|
| 29 |
+
# furnished to do so, subject to the following conditions:
|
| 30 |
+
#
|
| 31 |
+
# The above copyright notice and this permission notice shall be included in all
|
| 32 |
+
# copies or substantial portions of the Software.
|
| 33 |
+
#
|
| 34 |
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 35 |
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 36 |
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 37 |
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 38 |
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 39 |
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 40 |
+
# SOFTWARE.
|
| 41 |
+
import math
|
| 42 |
+
from collections.abc import Sequence
|
| 43 |
+
from copy import deepcopy
|
| 44 |
+
from typing import Optional
|
| 45 |
+
|
| 46 |
+
import numpy as np
|
| 47 |
+
import torch
|
| 48 |
+
import torch.nn as nn
|
| 49 |
+
import torch.nn.functional as F
|
| 50 |
+
from transformers import activations
|
| 51 |
+
|
| 52 |
+
try:
|
| 53 |
+
from transformers.activations import PytorchGELUTanh
|
| 54 |
+
except ImportError:
|
| 55 |
+
from transformers.activations import GELUTanh
|
| 56 |
+
activations.PytorchGELUTanh = GELUTanh
|
| 57 |
+
PytorchGELUTanh = GELUTanh
|
| 58 |
+
from transformers.activations import PytorchGELUTanh
|
| 59 |
+
from transformers.cache_utils import Cache
|
| 60 |
+
from transformers.configuration_utils import PretrainedConfig
|
| 61 |
+
from transformers.modeling_utils import PreTrainedModel
|
| 62 |
+
from transformers.models.llava.modeling_llava import \
|
| 63 |
+
LlavaCausalLMOutputWithPast
|
| 64 |
+
from transformers.utils import is_flash_attn_2_available
|
| 65 |
+
|
| 66 |
+
from .configuration_kimi_k25 import KimiK25Config
|
| 67 |
+
from .modeling_deepseek import DeepseekV3ForCausalLM
|
| 68 |
+
|
| 69 |
+
# Flash attention imports
|
| 70 |
+
if is_flash_attn_2_available():
|
| 71 |
+
from flash_attn import flash_attn_varlen_func
|
| 72 |
+
else:
|
| 73 |
+
flash_attn_varlen_func = None
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def multihead_attention(
|
| 77 |
+
q: torch.Tensor,
|
| 78 |
+
k: torch.Tensor,
|
| 79 |
+
v: torch.Tensor,
|
| 80 |
+
q_cu_seqlens: torch.Tensor | None = None,
|
| 81 |
+
k_cu_seqlens: torch.Tensor | None = None,
|
| 82 |
+
max_seqlen_q: int | None = None,
|
| 83 |
+
max_seqlen_k: int | None = None,
|
| 84 |
+
deterministic: bool = False,
|
| 85 |
+
):
|
| 86 |
+
"""Multi-head attention using flash attention 2.
|
| 87 |
+
|
| 88 |
+
Args:
|
| 89 |
+
q, k, v: tensor of shape (batch_size, seqlen, num_heads, head_dim),
|
| 90 |
+
or (tot_seqlens, num_heads, head_dim) if packing.
|
| 91 |
+
q_cu_seqlens (torch.Tensor): cumulative sequence lengths of q.
|
| 92 |
+
The first element should be 0 and the last element should be q.shape[0].
|
| 93 |
+
k_cu_seqlens (torch.Tensor): cumulative sequence lengths of k.
|
| 94 |
+
The first element should be 0 and the last element should be k.shape[0].
|
| 95 |
+
|
| 96 |
+
Returns:
|
| 97 |
+
output: shape (batch_size, seqlen, dim) or (tot_seqlens, dim) if packing,
|
| 98 |
+
where dim = num_heads * head_dim
|
| 99 |
+
"""
|
| 100 |
+
attn_out = flash_attn_varlen_func(
|
| 101 |
+
q,
|
| 102 |
+
k,
|
| 103 |
+
v,
|
| 104 |
+
q_cu_seqlens,
|
| 105 |
+
k_cu_seqlens,
|
| 106 |
+
max_seqlen_q,
|
| 107 |
+
max_seqlen_k,
|
| 108 |
+
causal=False,
|
| 109 |
+
deterministic=deterministic,
|
| 110 |
+
)
|
| 111 |
+
if isinstance(attn_out, tuple):
|
| 112 |
+
attn_out = attn_out[0]
|
| 113 |
+
|
| 114 |
+
attn_out = attn_out.flatten(start_dim=-2)
|
| 115 |
+
|
| 116 |
+
return attn_out
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
def eager_attention(
|
| 120 |
+
q: torch.Tensor,
|
| 121 |
+
k: torch.Tensor,
|
| 122 |
+
v: torch.Tensor,
|
| 123 |
+
q_cu_seqlens: Optional[torch.Tensor] = None,
|
| 124 |
+
k_cu_seqlens: Optional[torch.Tensor] = None,
|
| 125 |
+
**kwargs,
|
| 126 |
+
) -> torch.Tensor:
|
| 127 |
+
seq_length = q.shape[0]
|
| 128 |
+
attention_mask = torch.zeros([1, seq_length, seq_length],
|
| 129 |
+
device=q.device,
|
| 130 |
+
dtype=torch.bool)
|
| 131 |
+
for i in range(1, len(q_cu_seqlens)):
|
| 132 |
+
attention_mask[
|
| 133 |
+
...,
|
| 134 |
+
q_cu_seqlens[i - 1]:q_cu_seqlens[i],
|
| 135 |
+
q_cu_seqlens[i - 1]:q_cu_seqlens[i],
|
| 136 |
+
] = True
|
| 137 |
+
q = q.transpose(0, 1)
|
| 138 |
+
k = k.transpose(0, 1)
|
| 139 |
+
v = v.transpose(0, 1)
|
| 140 |
+
|
| 141 |
+
attn_weight = q @ k.transpose(-2, -1) / math.sqrt(q.shape[-1])
|
| 142 |
+
attn_weight += attention_mask
|
| 143 |
+
attn_weight = torch.softmax(attn_weight, dim=-1,
|
| 144 |
+
dtype=torch.float32).to(q.dtype)
|
| 145 |
+
|
| 146 |
+
attn_output = attn_weight @ v
|
| 147 |
+
attn_output = attn_output.transpose(0, 1)
|
| 148 |
+
attn_output = attn_output.reshape(seq_length, -1)
|
| 149 |
+
return attn_output
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
VL_VISION_ATTENTION_FUNCTIONS = {
|
| 153 |
+
"flash_attention_2": multihead_attention,
|
| 154 |
+
"eager": eager_attention,
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
def _apply_rope_input_validation(x, freqs_cis):
|
| 159 |
+
assert x.ndim == freqs_cis.ndim + 1, (x.shape, freqs_cis.shape)
|
| 160 |
+
assert x.shape[:-2] == freqs_cis.shape[:-1], (x.shape, freqs_cis.shape)
|
| 161 |
+
assert x.shape[-1] == 2 * freqs_cis.shape[-1], (x.shape, freqs_cis.shape)
|
| 162 |
+
assert freqs_cis.dtype == torch.complex64, freqs_cis.dtype
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
def get_rope_shape_decorate(func):
|
| 166 |
+
_get_rope_shape_first_call_flag = set()
|
| 167 |
+
|
| 168 |
+
def wrapper(org, interpolation_mode, shape):
|
| 169 |
+
key = (org.requires_grad, torch.is_grad_enabled(), interpolation_mode)
|
| 170 |
+
if key not in _get_rope_shape_first_call_flag:
|
| 171 |
+
_get_rope_shape_first_call_flag.add(key)
|
| 172 |
+
_ = func(org, interpolation_mode, shape=(64, 64))
|
| 173 |
+
return func(org, interpolation_mode, shape)
|
| 174 |
+
|
| 175 |
+
return wrapper
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
@get_rope_shape_decorate
|
| 179 |
+
@torch.compile(dynamic=True)
|
| 180 |
+
def get_rope_shape(org, interpolation_mode, shape):
|
| 181 |
+
return (F.interpolate(
|
| 182 |
+
org.permute((2, 0, 1)).unsqueeze(0),
|
| 183 |
+
size=shape,
|
| 184 |
+
mode=interpolation_mode,
|
| 185 |
+
).squeeze(0).permute((1, 2, 0)).flatten(end_dim=1))
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
def apply_rope(xq: torch.Tensor, xk: torch.Tensor,
|
| 189 |
+
freqs_cis: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
|
| 190 |
+
"""
|
| 191 |
+
Args: (The leading dimensions of all inputs should be the same)
|
| 192 |
+
xq: query, tensor of shape (..., num_heads, head_dim)
|
| 193 |
+
xk: key, tensor of shape (..., num_heads, head_dim)
|
| 194 |
+
freqs_cis: tensor of shape (..., head_dim/2), dtype=torch.complex64. It contains the precomputed cis(freqs) for each position in the 2D grid.
|
| 195 |
+
Returns:
|
| 196 |
+
xq_out, xk_out: tensors of shape (..., num_heads, head_dim)
|
| 197 |
+
"""
|
| 198 |
+
_apply_rope_input_validation(xq, freqs_cis)
|
| 199 |
+
_apply_rope_input_validation(xk, freqs_cis)
|
| 200 |
+
|
| 201 |
+
freqs_cis = freqs_cis.unsqueeze(-2) # ..., 1, head_dim/2
|
| 202 |
+
# ..., num_heads, head_dim/2
|
| 203 |
+
xq_ = torch.view_as_complex(xq.float().view(*xq.shape[:-1], -1, 2))
|
| 204 |
+
xk_ = torch.view_as_complex(xk.float().view(*xq.shape[:-1], -1, 2))
|
| 205 |
+
xq_out = torch.view_as_real(xq_ * freqs_cis).flatten(
|
| 206 |
+
-2) # ..., num_heads, head_dim
|
| 207 |
+
xk_out = torch.view_as_real(xk_ * freqs_cis).flatten(
|
| 208 |
+
-2) # ..., num_heads, head_dim
|
| 209 |
+
return xq_out.type_as(xq), xk_out.type_as(xk)
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
def get_1d_sincos_pos_embed_from_grid(embed_dim, pos):
|
| 213 |
+
"""
|
| 214 |
+
From:
|
| 215 |
+
https://github.com/OpenGVLab/InternVideo/blob/421f6d2361fc8f61a3394244571f2601a4e99e29/InternVideo2/multi_modality/models/backbones/internvideo2/pos_embed.py#L86
|
| 216 |
+
embed_dim: output dimension for each position
|
| 217 |
+
pos: a list of positions to be encoded: size (M,)
|
| 218 |
+
out: (M, D)
|
| 219 |
+
"""
|
| 220 |
+
assert embed_dim % 2 == 0
|
| 221 |
+
omega = np.arange(embed_dim // 2, dtype=np.float32)
|
| 222 |
+
omega /= embed_dim / 2.0
|
| 223 |
+
omega = 1.0 / 10000**omega # (D/2,)
|
| 224 |
+
|
| 225 |
+
pos = pos.reshape(-1) # (M,)
|
| 226 |
+
out = np.einsum('m,d->md', pos, omega) # (M, D/2), outer product
|
| 227 |
+
|
| 228 |
+
emb_sin = np.sin(out) # (M, D/2)
|
| 229 |
+
emb_cos = np.cos(out) # (M, D/2)
|
| 230 |
+
|
| 231 |
+
emb = np.concatenate([emb_sin, emb_cos], axis=1) # (M, D)
|
| 232 |
+
return emb
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
def get_1d_sincos_pos_embed(embed_dim, t_size, cls_token=False):
|
| 236 |
+
"""
|
| 237 |
+
t_size: int of the temporal size
|
| 238 |
+
return:
|
| 239 |
+
pos_embed: [t_size, embed_dim] or [1+t_size, embed_dim] (w/ or w/o cls_token)
|
| 240 |
+
"""
|
| 241 |
+
grid_t = np.arange(t_size, dtype=np.float32)
|
| 242 |
+
pos_embed = get_1d_sincos_pos_embed_from_grid(embed_dim, grid_t)
|
| 243 |
+
if cls_token:
|
| 244 |
+
pos_embed = np.concatenate([np.zeros([1, embed_dim]), pos_embed],
|
| 245 |
+
axis=0)
|
| 246 |
+
return pos_embed
|
| 247 |
+
|
| 248 |
+
|
| 249 |
+
class Learnable2DInterpPosEmbDivided_fixed(nn.Module):
|
| 250 |
+
|
| 251 |
+
def __init__(self,
|
| 252 |
+
height: int,
|
| 253 |
+
width: int,
|
| 254 |
+
num_frames: int,
|
| 255 |
+
dim: int,
|
| 256 |
+
interpolation_mode: str = 'bicubic') -> None:
|
| 257 |
+
super().__init__()
|
| 258 |
+
self.height = height
|
| 259 |
+
self.width = width
|
| 260 |
+
self.num_frames = num_frames
|
| 261 |
+
self.dim = dim
|
| 262 |
+
self.interpolation_mode = interpolation_mode
|
| 263 |
+
self.weight = nn.Parameter(torch.empty(height, width, dim))
|
| 264 |
+
self.register_buffer('time_weight',
|
| 265 |
+
torch.from_numpy(
|
| 266 |
+
get_1d_sincos_pos_embed(
|
| 267 |
+
self.dim,
|
| 268 |
+
self.num_frames)).float().unsqueeze(1),
|
| 269 |
+
persistent=False)
|
| 270 |
+
|
| 271 |
+
self.reset_parameters()
|
| 272 |
+
|
| 273 |
+
def reset_parameters(self):
|
| 274 |
+
nn.init.normal_(self.weight)
|
| 275 |
+
|
| 276 |
+
def forward(self, x: torch.Tensor,
|
| 277 |
+
grid_thws: torch.Tensor) -> torch.Tensor:
|
| 278 |
+
pos_embs = []
|
| 279 |
+
for t, h, w in grid_thws.tolist():
|
| 280 |
+
assert t <= self.num_frames, f't:{t} > self.num_frames:{self.num_frames}'
|
| 281 |
+
if (h, w) == self.weight.shape[:-1]:
|
| 282 |
+
pos_emb_2d = self.weight.flatten(end_dim=1)
|
| 283 |
+
else:
|
| 284 |
+
pos_emb_2d = get_rope_shape(
|
| 285 |
+
self.weight,
|
| 286 |
+
interpolation_mode=self.interpolation_mode,
|
| 287 |
+
shape=(h, w),
|
| 288 |
+
)
|
| 289 |
+
|
| 290 |
+
if t == 1:
|
| 291 |
+
pos_emb_3d = pos_emb_2d
|
| 292 |
+
else:
|
| 293 |
+
pos_emb_3d = pos_emb_2d.unsqueeze(0).repeat(
|
| 294 |
+
t, 1, 1) + self.time_weight[0:t]
|
| 295 |
+
|
| 296 |
+
pos_embs.append(pos_emb_3d.reshape(-1, pos_emb_3d.shape[-1]))
|
| 297 |
+
|
| 298 |
+
out = x + torch.cat(pos_embs)
|
| 299 |
+
return out
|
| 300 |
+
|
| 301 |
+
|
| 302 |
+
class MoonVision3dPatchEmbed(nn.Module):
|
| 303 |
+
|
| 304 |
+
def __init__(self,
|
| 305 |
+
out_dim: int,
|
| 306 |
+
in_dim: int = 3,
|
| 307 |
+
patch_size: int | tuple[int, int] = (14, 14),
|
| 308 |
+
pos_emb_height: int = 14,
|
| 309 |
+
pos_emb_width: int = 14,
|
| 310 |
+
pos_emb_time: int = 4,
|
| 311 |
+
pos_emb_type: str = 'divided_fixed'):
|
| 312 |
+
super().__init__()
|
| 313 |
+
assert isinstance(
|
| 314 |
+
patch_size,
|
| 315 |
+
int | Sequence), f'Invalid patch_size type: {type(patch_size)}'
|
| 316 |
+
if isinstance(patch_size, int):
|
| 317 |
+
patch_size = (patch_size, patch_size)
|
| 318 |
+
assert (len(patch_size) == 2
|
| 319 |
+
), f'Expected patch_size to be a tuple of 2, got {patch_size}'
|
| 320 |
+
self.patch_size = patch_size
|
| 321 |
+
|
| 322 |
+
self.proj = nn.Conv2d(in_dim,
|
| 323 |
+
out_dim,
|
| 324 |
+
kernel_size=patch_size,
|
| 325 |
+
stride=patch_size)
|
| 326 |
+
|
| 327 |
+
if pos_emb_type == 'divided_fixed':
|
| 328 |
+
self.pos_emb = Learnable2DInterpPosEmbDivided_fixed(
|
| 329 |
+
height=pos_emb_height,
|
| 330 |
+
width=pos_emb_width,
|
| 331 |
+
num_frames=pos_emb_time,
|
| 332 |
+
dim=out_dim)
|
| 333 |
+
else:
|
| 334 |
+
raise NotImplementedError(
|
| 335 |
+
f'Not support pos_emb_type: {pos_emb_type}')
|
| 336 |
+
|
| 337 |
+
def forward(self, x: torch.Tensor,
|
| 338 |
+
grid_thws: torch.Tensor) -> torch.Tensor:
|
| 339 |
+
"""
|
| 340 |
+
Args:
|
| 341 |
+
x (L, Channels): input tensor
|
| 342 |
+
grid_hws (N, 3): temporal, height and width
|
| 343 |
+
|
| 344 |
+
Returns:
|
| 345 |
+
(L, Cout) tensor
|
| 346 |
+
"""
|
| 347 |
+
x = self.proj(x).view(x.size(0), -1)
|
| 348 |
+
# apply positional embedding
|
| 349 |
+
x = self.pos_emb(x, grid_thws)
|
| 350 |
+
return x
|
| 351 |
+
|
| 352 |
+
|
| 353 |
+
class Rope2DPosEmbRepeated(nn.Module):
|
| 354 |
+
"""2D rotary position embedding with multi-resolution support.
|
| 355 |
+
|
| 356 |
+
This class is intended to be used in the following way:
|
| 357 |
+
1. Before training, create an instance of Rope2DPosEmb. This instance will hold the precomputed cis.
|
| 358 |
+
2. Before each forward pass, call `get_freqs_cis_by_*` to get the `freqs_cis` tensor for this iteration.
|
| 359 |
+
3. During the forward pass, pass the `freqs_cis` tensor to each attention layer, and call `apply` just before each attention operation.
|
| 360 |
+
The rope is shared across all attention layers and all heads.
|
| 361 |
+
|
| 362 |
+
Refs:
|
| 363 |
+
- RoFormer: https://arxiv.org/abs/2104.09864
|
| 364 |
+
- VisionLLaMA: https://arxiv.org/abs/2403.00522
|
| 365 |
+
- https://github.com/Meituan-AutoML/VisionLLaMA/blob/main/dit/models.py
|
| 366 |
+
|
| 367 |
+
Args:
|
| 368 |
+
dim (int): usually the multi-head attention dimension, should be divisible by 4 (TODO: relax this constraint if needed)
|
| 369 |
+
max_height (int): the maximum height of the 2D grid
|
| 370 |
+
max_width (int): the maximum width of the 2D grid
|
| 371 |
+
theta_base (float): the base of the theta
|
| 372 |
+
device (str): the device to store the precomputed cis
|
| 373 |
+
"""
|
| 374 |
+
|
| 375 |
+
def __init__(self,
|
| 376 |
+
dim: int,
|
| 377 |
+
max_height: int,
|
| 378 |
+
max_width: int,
|
| 379 |
+
theta_base=10000):
|
| 380 |
+
super().__init__()
|
| 381 |
+
self.dim = dim
|
| 382 |
+
assert self.dim % 4 == 0, 'dim must be divisible by 4'
|
| 383 |
+
self.max_height = max_height
|
| 384 |
+
self.max_width = max_width
|
| 385 |
+
self.theta_base = theta_base
|
| 386 |
+
|
| 387 |
+
def extra_repr(self):
|
| 388 |
+
return f'dim={self.dim}, max_height={self.max_height}, max_width={self.max_width}, theta_base={self.theta_base}'
|
| 389 |
+
|
| 390 |
+
def _precompute_freqs_cis(self, device: torch.device) -> torch.Tensor:
|
| 391 |
+
"""Calculate the cis(freqs) for each position in the 2D grid.
|
| 392 |
+
|
| 393 |
+
Return: complex tensor of shape (max_height, max_width, dim//2) and value:
|
| 394 |
+
height axis: ret[h, w, 2*i] = cis(h * theta_base**(-4*i/dim))
|
| 395 |
+
weight axis: ret[h, w, 2*i+1] = cis(w * theta_base**(-4*i/dim)) with (i in [0, dim//4))
|
| 396 |
+
note: `cis` is a mathematical notation defined by cis x = cos x + i sin x,
|
| 397 |
+
"""
|
| 398 |
+
N = self.max_height * self.max_width
|
| 399 |
+
flat_pos = torch.arange(0, N).float().to(device)
|
| 400 |
+
x_pos = flat_pos % self.max_width
|
| 401 |
+
y_pos = flat_pos // self.max_width
|
| 402 |
+
dim_range = (torch.arange(0, self.dim,
|
| 403 |
+
4)[:(self.dim // 4)].float().to(device)
|
| 404 |
+
) # C/4
|
| 405 |
+
freqs = 1.0 / (self.theta_base**(dim_range / self.dim))
|
| 406 |
+
x_freqs = torch.outer(x_pos, freqs).float() # N, C/4
|
| 407 |
+
y_freqs = torch.outer(y_pos, freqs).float() # N, C/4
|
| 408 |
+
x_cis = torch.polar(torch.ones_like(x_freqs), x_freqs) # N, C/4
|
| 409 |
+
y_cis = torch.polar(torch.ones_like(y_freqs), y_freqs) # N, C/4
|
| 410 |
+
# N, C/4, 2
|
| 411 |
+
freqs_cis = torch.cat(
|
| 412 |
+
[x_cis.unsqueeze(dim=-1),
|
| 413 |
+
y_cis.unsqueeze(dim=-1)], dim=-1)
|
| 414 |
+
# max_height, max_width, C/2
|
| 415 |
+
freqs_cis = freqs_cis.reshape(self.max_height, self.max_width, -1)
|
| 416 |
+
return freqs_cis
|
| 417 |
+
|
| 418 |
+
def get_freqs_cis(self, grid_thws: torch.Tensor,
|
| 419 |
+
device: torch.device) -> torch.Tensor:
|
| 420 |
+
"""
|
| 421 |
+
Args:
|
| 422 |
+
grid_thws (torch.Tensor): grid time, height and width
|
| 423 |
+
|
| 424 |
+
Returns:
|
| 425 |
+
freqs_cis: tensor of shape (sum(t * height * width), dim//2)
|
| 426 |
+
"""
|
| 427 |
+
if not hasattr(self, 'freqs_cis'):
|
| 428 |
+
self.register_buffer('freqs_cis',
|
| 429 |
+
self._precompute_freqs_cis(device),
|
| 430 |
+
persistent=False)
|
| 431 |
+
|
| 432 |
+
shapes = grid_thws.tolist()
|
| 433 |
+
assert all(1 <= h <= self.max_height and 1 <= w <= self.max_width
|
| 434 |
+
for t, h, w in shapes), (
|
| 435 |
+
shapes,
|
| 436 |
+
self.max_height,
|
| 437 |
+
self.max_width,
|
| 438 |
+
)
|
| 439 |
+
freqs_cis = torch.cat(
|
| 440 |
+
[
|
| 441 |
+
self.freqs_cis[:h, :w].reshape(-1, self.dim // 2).repeat(t, 1)
|
| 442 |
+
for t, h, w in shapes
|
| 443 |
+
],
|
| 444 |
+
dim=0,
|
| 445 |
+
)
|
| 446 |
+
return freqs_cis
|
| 447 |
+
|
| 448 |
+
|
| 449 |
+
class MLP2(nn.Module):
|
| 450 |
+
"""
|
| 451 |
+
Args:
|
| 452 |
+
dims: [in_dim, hidden_dim, out_dim]
|
| 453 |
+
bias: whether to use bias in linear layer.
|
| 454 |
+
"""
|
| 455 |
+
|
| 456 |
+
def __init__(self, dims: list[int], activation, bias=True):
|
| 457 |
+
super().__init__()
|
| 458 |
+
assert len(dims) == 3
|
| 459 |
+
self.fc0 = nn.Linear(dims[0], dims[1], bias=bias)
|
| 460 |
+
self.fc1 = nn.Linear(dims[1], dims[2], bias=bias)
|
| 461 |
+
self.activation = activation
|
| 462 |
+
for m in [self.fc0, self.fc1]:
|
| 463 |
+
nn.init.trunc_normal_(m.weight, std=math.sqrt(2 / m.in_features))
|
| 464 |
+
if m.bias is not None:
|
| 465 |
+
nn.init.zeros_(m.bias)
|
| 466 |
+
|
| 467 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 468 |
+
x = self.fc0(x)
|
| 469 |
+
x = self.activation(x)
|
| 470 |
+
return self.fc1(x)
|
| 471 |
+
|
| 472 |
+
|
| 473 |
+
class MoonViTEncoderLayer(nn.Module):
|
| 474 |
+
|
| 475 |
+
def __init__(
|
| 476 |
+
self,
|
| 477 |
+
num_heads: int,
|
| 478 |
+
hidden_dim: int,
|
| 479 |
+
mlp_dim: int,
|
| 480 |
+
*,
|
| 481 |
+
attn_implementation: str = 'flash_attention_2',
|
| 482 |
+
activation=F.gelu,
|
| 483 |
+
attn_bias: bool = False,
|
| 484 |
+
use_deterministic_attn: bool = False,
|
| 485 |
+
):
|
| 486 |
+
super().__init__()
|
| 487 |
+
self.num_heads = num_heads
|
| 488 |
+
self.hidden_dim = hidden_dim
|
| 489 |
+
self.hidden_size_per_attention_head = self.hidden_dim // self.num_heads
|
| 490 |
+
self.attn_implementation = attn_implementation
|
| 491 |
+
self.use_deterministic_attn = use_deterministic_attn
|
| 492 |
+
|
| 493 |
+
self.norm0 = nn.LayerNorm(hidden_dim)
|
| 494 |
+
self.norm1 = nn.LayerNorm(hidden_dim)
|
| 495 |
+
self.mlp = MLP2([hidden_dim, mlp_dim, hidden_dim], activation)
|
| 496 |
+
self.wqkv = nn.Linear(hidden_dim, hidden_dim * 3, bias=attn_bias)
|
| 497 |
+
self.wo = nn.Linear(hidden_dim, hidden_dim, bias=attn_bias)
|
| 498 |
+
|
| 499 |
+
def attention_qkvpacked(
|
| 500 |
+
self,
|
| 501 |
+
x: torch.Tensor,
|
| 502 |
+
cu_seqlens: torch.Tensor,
|
| 503 |
+
max_seqlen: torch.Tensor,
|
| 504 |
+
rope_freqs_cis: torch.Tensor | None = None,
|
| 505 |
+
):
|
| 506 |
+
"""
|
| 507 |
+
Args:
|
| 508 |
+
x (torch.Tensor): (batch_size, seqlen, hidden_dim)
|
| 509 |
+
cu_seqlens (torch.Tensor):
|
| 510 |
+
"""
|
| 511 |
+
xqkv = self.wqkv(x)
|
| 512 |
+
|
| 513 |
+
qkv_shape = xqkv.size()[:-1] + (
|
| 514 |
+
3,
|
| 515 |
+
self.num_heads,
|
| 516 |
+
self.hidden_size_per_attention_head,
|
| 517 |
+
)
|
| 518 |
+
# xqkv: (batch_size, seqlen, 3, nheads, headdim)
|
| 519 |
+
xqkv = xqkv.view(*qkv_shape)
|
| 520 |
+
xq, xk, xv = torch.unbind(xqkv, dim=-3)
|
| 521 |
+
|
| 522 |
+
xq, xk = apply_rope(xq, xk, rope_freqs_cis)
|
| 523 |
+
|
| 524 |
+
attn_func = VL_VISION_ATTENTION_FUNCTIONS[self.attn_implementation]
|
| 525 |
+
attn_out = attn_func(xq,
|
| 526 |
+
xk,
|
| 527 |
+
xv,
|
| 528 |
+
q_cu_seqlens=cu_seqlens,
|
| 529 |
+
k_cu_seqlens=cu_seqlens,
|
| 530 |
+
max_seqlen_k=max_seqlen,
|
| 531 |
+
max_seqlen_q=max_seqlen,
|
| 532 |
+
deterministic=self.use_deterministic_attn)
|
| 533 |
+
|
| 534 |
+
attn_out = self.wo(attn_out)
|
| 535 |
+
return attn_out
|
| 536 |
+
|
| 537 |
+
def forward(
|
| 538 |
+
self,
|
| 539 |
+
hidden_states: torch.Tensor,
|
| 540 |
+
cu_seqlens: torch.Tensor,
|
| 541 |
+
max_seqlen: int,
|
| 542 |
+
rope_freqs_cis: torch.Tensor | None = None,
|
| 543 |
+
):
|
| 544 |
+
residual = hidden_states
|
| 545 |
+
hidden_states = self.norm0(hidden_states)
|
| 546 |
+
|
| 547 |
+
hidden_states = self.attention_qkvpacked(hidden_states, cu_seqlens,
|
| 548 |
+
max_seqlen, rope_freqs_cis)
|
| 549 |
+
hidden_states = residual + hidden_states
|
| 550 |
+
|
| 551 |
+
residual = hidden_states
|
| 552 |
+
hidden_states = self.norm1(hidden_states)
|
| 553 |
+
hidden_states = self.mlp(hidden_states)
|
| 554 |
+
hidden_states = residual + hidden_states
|
| 555 |
+
|
| 556 |
+
return hidden_states
|
| 557 |
+
|
| 558 |
+
|
| 559 |
+
class MoonViT3dEncoder(nn.Module):
|
| 560 |
+
|
| 561 |
+
def __init__(self,
|
| 562 |
+
hidden_dim: int,
|
| 563 |
+
num_layers: int,
|
| 564 |
+
block_cfg: dict,
|
| 565 |
+
video_attn_type: str = 'spatial_temporal') -> None:
|
| 566 |
+
super().__init__()
|
| 567 |
+
|
| 568 |
+
assert video_attn_type == 'spatial_temporal', f'video_attn_type must be "spatial_temporal", got {video_attn_type}'
|
| 569 |
+
self.video_attn_type = video_attn_type
|
| 570 |
+
self.rope_2d = Rope2DPosEmbRepeated(
|
| 571 |
+
block_cfg['hidden_dim'] // block_cfg['num_heads'], 512, 512)
|
| 572 |
+
self.blocks = nn.ModuleList([
|
| 573 |
+
MoonViTEncoderLayer(
|
| 574 |
+
**block_cfg,
|
| 575 |
+
use_deterministic_attn=self.use_deterministic_attn)
|
| 576 |
+
for _ in range(num_layers)
|
| 577 |
+
])
|
| 578 |
+
self.final_layernorm = nn.LayerNorm(hidden_dim)
|
| 579 |
+
|
| 580 |
+
def forward(
|
| 581 |
+
self,
|
| 582 |
+
hidden_states: torch.Tensor,
|
| 583 |
+
grid_thws: torch.Tensor,
|
| 584 |
+
) -> torch.Tensor:
|
| 585 |
+
rope_freqs_cis = self.rope_2d.get_freqs_cis(
|
| 586 |
+
grid_thws=grid_thws, device=hidden_states.device)
|
| 587 |
+
|
| 588 |
+
lengths = torch.cat((
|
| 589 |
+
torch.zeros(1, dtype=grid_thws.dtype, device=grid_thws.device),
|
| 590 |
+
grid_thws[:, 0] * grid_thws[:, 1] * grid_thws[:, 2],
|
| 591 |
+
))
|
| 592 |
+
|
| 593 |
+
max_seqlen = lengths.max()
|
| 594 |
+
cu_seqlens = lengths.to(hidden_states.device).cumsum(dim=0,
|
| 595 |
+
dtype=torch.int32)
|
| 596 |
+
for block in self.blocks:
|
| 597 |
+
hidden_states = block(hidden_states,
|
| 598 |
+
cu_seqlens,
|
| 599 |
+
max_seqlen,
|
| 600 |
+
rope_freqs_cis=rope_freqs_cis)
|
| 601 |
+
|
| 602 |
+
hidden_states = self.final_layernorm(hidden_states)
|
| 603 |
+
return hidden_states
|
| 604 |
+
|
| 605 |
+
|
| 606 |
+
def tpool_patch_merger(
|
| 607 |
+
x: torch.Tensor,
|
| 608 |
+
grid_thws: torch.Tensor,
|
| 609 |
+
merge_kernel_size: tuple[int, int] = (2, 2),
|
| 610 |
+
) -> list[torch.Tensor]:
|
| 611 |
+
d_model = x.size(-1)
|
| 612 |
+
|
| 613 |
+
outputs = []
|
| 614 |
+
pre_sum = 0
|
| 615 |
+
for t, h, w in grid_thws.tolist():
|
| 616 |
+
# Get the current sequence
|
| 617 |
+
seq = x[pre_sum:pre_sum + t * h * w]
|
| 618 |
+
# Reshape along self.merge_kernel_size and concat to the last dimension
|
| 619 |
+
kernel_height, kernel_width = merge_kernel_size
|
| 620 |
+
new_height, new_width = h // kernel_height, w // kernel_width
|
| 621 |
+
reshaped_seq = seq.view(t, new_height, kernel_height, new_width,
|
| 622 |
+
kernel_width, d_model)
|
| 623 |
+
reshaped_seq = reshaped_seq.permute(0, 1,
|
| 624 |
+
3, 2, 4, 5).contiguous().mean(
|
| 625 |
+
dim=0) # temporal pooling
|
| 626 |
+
padded_seq = reshaped_seq.view(new_height * new_width,
|
| 627 |
+
kernel_height * kernel_width, -1)
|
| 628 |
+
outputs.append(padded_seq)
|
| 629 |
+
pre_sum += t * h * w
|
| 630 |
+
|
| 631 |
+
return outputs
|
| 632 |
+
|
| 633 |
+
|
| 634 |
+
class MoonViT3dPretrainedModel(PreTrainedModel):
|
| 635 |
+
config_class = None
|
| 636 |
+
model_type = 'moonvit3d'
|
| 637 |
+
_no_split_modules = ['PackingTransformer']
|
| 638 |
+
_supports_flash_attn_2 = True
|
| 639 |
+
_supports_sdpa = True
|
| 640 |
+
|
| 641 |
+
def __init__(self, config, *inputs, **kwargs):
|
| 642 |
+
super().__init__(config, *inputs, **kwargs)
|
| 643 |
+
config = deepcopy(config)
|
| 644 |
+
self.merge_kernel_size = config.merge_kernel_size
|
| 645 |
+
self.patch_size = config.patch_size
|
| 646 |
+
self.merge_type = config.merge_type
|
| 647 |
+
|
| 648 |
+
self.patch_embed = MoonVision3dPatchEmbed(
|
| 649 |
+
out_dim=config.hidden_size,
|
| 650 |
+
patch_size=config.patch_size,
|
| 651 |
+
pos_emb_height=config.init_pos_emb_height,
|
| 652 |
+
pos_emb_width=config.init_pos_emb_width,
|
| 653 |
+
pos_emb_time=config.init_pos_emb_time,
|
| 654 |
+
pos_emb_type=config.pos_emb_type,
|
| 655 |
+
)
|
| 656 |
+
|
| 657 |
+
self.encoder = MoonViT3dEncoder(hidden_dim=config.hidden_size,
|
| 658 |
+
num_layers=config.num_hidden_layers,
|
| 659 |
+
block_cfg={
|
| 660 |
+
'num_heads':
|
| 661 |
+
config.num_attention_heads,
|
| 662 |
+
'hidden_dim':
|
| 663 |
+
config.hidden_size,
|
| 664 |
+
'mlp_dim':
|
| 665 |
+
config.intermediate_size,
|
| 666 |
+
'activation':
|
| 667 |
+
PytorchGELUTanh(),
|
| 668 |
+
'attn_bias':
|
| 669 |
+
True,
|
| 670 |
+
'attn_implementation':
|
| 671 |
+
config._attn_implementation,
|
| 672 |
+
},
|
| 673 |
+
video_attn_type=config.video_attn_type)
|
| 674 |
+
|
| 675 |
+
def forward(self, pixel_values: torch.Tensor,
|
| 676 |
+
grid_thws: torch.Tensor) -> torch.Tensor:
|
| 677 |
+
"""
|
| 678 |
+
Args:
|
| 679 |
+
pixel_values (torch.Tensor): The input pixel values.
|
| 680 |
+
grid_thws (torch.Tensor): Temporal, height and width.
|
| 681 |
+
|
| 682 |
+
Returns:
|
| 683 |
+
torch.Tensor: The output tokens.
|
| 684 |
+
"""
|
| 685 |
+
# grid_thws = grid_thws.to('cpu')
|
| 686 |
+
assert grid_thws.ndim == 2, f'grid_thws should be 2D, got {grid_thws.ndim}'
|
| 687 |
+
assert grid_thws.size(1) == 3, f'No support for thw: {grid_thws}'
|
| 688 |
+
hidden_states = self.patch_embed(pixel_values, grid_thws)
|
| 689 |
+
hidden_states = self.encoder(hidden_states, grid_thws)
|
| 690 |
+
if self.merge_type == 'sd2_tpool': # spatial downsampling 2x with temporal pooling all
|
| 691 |
+
hidden_states = tpool_patch_merger(
|
| 692 |
+
hidden_states,
|
| 693 |
+
grid_thws,
|
| 694 |
+
merge_kernel_size=self.merge_kernel_size)
|
| 695 |
+
else:
|
| 696 |
+
raise NotImplementedError(f'Not support {self.merge_type}')
|
| 697 |
+
|
| 698 |
+
return hidden_states
|
| 699 |
+
|
| 700 |
+
|
| 701 |
+
# ============================================================================
|
| 702 |
+
# MM Projector Helper Classes (from mm_projector/modeling_mm_projectors.py)
|
| 703 |
+
# ============================================================================
|
| 704 |
+
|
| 705 |
+
|
| 706 |
+
class IdentityMap(nn.Module):
|
| 707 |
+
|
| 708 |
+
def __init__(self):
|
| 709 |
+
super().__init__()
|
| 710 |
+
|
| 711 |
+
def forward(self, x, *args, **kwargs):
|
| 712 |
+
return x
|
| 713 |
+
|
| 714 |
+
|
| 715 |
+
class MLP(nn.Module):
|
| 716 |
+
|
| 717 |
+
def __init__(self, config):
|
| 718 |
+
super().__init__()
|
| 719 |
+
# TODO, use faster LayerNorm
|
| 720 |
+
self.pre_norm = nn.LayerNorm(config.mm_hidden_size)
|
| 721 |
+
self.proj = nn.Sequential(
|
| 722 |
+
nn.Linear(config.mm_hidden_size, config.hidden_size), nn.GELU(),
|
| 723 |
+
nn.Linear(config.hidden_size, config.hidden_size))
|
| 724 |
+
|
| 725 |
+
def forward(self, x, *args, **kwargs):
|
| 726 |
+
assert isinstance(x,
|
| 727 |
+
list | tuple), f'x is not a list or tuple: {type(x)}'
|
| 728 |
+
lengths = [item.shape[0] for item in x]
|
| 729 |
+
x = torch.cat(x, dim=0)
|
| 730 |
+
x = self.pre_norm(x)
|
| 731 |
+
x = self.proj(x)
|
| 732 |
+
x = torch.split(x, lengths, dim=0)
|
| 733 |
+
|
| 734 |
+
return x
|
| 735 |
+
|
| 736 |
+
|
| 737 |
+
class PatchMergerMLP(nn.Module):
|
| 738 |
+
|
| 739 |
+
def __init__(self, config):
|
| 740 |
+
super().__init__()
|
| 741 |
+
eps = config.projector_ln_eps
|
| 742 |
+
self.hidden_size = config.mm_hidden_size * (
|
| 743 |
+
config.merge_kernel_size[0] * config.merge_kernel_size[1])
|
| 744 |
+
self.pre_norm = nn.LayerNorm(config.mm_hidden_size, eps=eps)
|
| 745 |
+
self.proj = nn.Sequential(
|
| 746 |
+
nn.Linear(self.hidden_size, self.hidden_size),
|
| 747 |
+
nn.GELU(),
|
| 748 |
+
nn.Linear(self.hidden_size, config.hidden_size),
|
| 749 |
+
)
|
| 750 |
+
|
| 751 |
+
def forward(self, x, *args, **kwargs):
|
| 752 |
+
if isinstance(x, list) or isinstance(x, tuple):
|
| 753 |
+
x = [
|
| 754 |
+
self.proj(self.pre_norm(item).view(item.shape[0], -1))
|
| 755 |
+
for item in x
|
| 756 |
+
]
|
| 757 |
+
else:
|
| 758 |
+
# B, N, N_k, C = x.shape
|
| 759 |
+
B = x.shape[0]
|
| 760 |
+
x = self.proj(self.pre_norm(x).view(B, -1, self.hidden_size))
|
| 761 |
+
return x
|
| 762 |
+
|
| 763 |
+
|
| 764 |
+
class KimiK25PreTrainedModel(PreTrainedModel):
|
| 765 |
+
config_class = KimiK25Config
|
| 766 |
+
base_model_prefix = "model"
|
| 767 |
+
_no_split_modules = [
|
| 768 |
+
"MoonViT3dPretrainedModel",
|
| 769 |
+
"MoonViTEncoderLayer",
|
| 770 |
+
"DeepseekDecoderLayer",
|
| 771 |
+
"PatchMergerMLP",
|
| 772 |
+
]
|
| 773 |
+
_skip_keys_device_placement = "past_key_values"
|
| 774 |
+
_supports_flash_attn_2 = True
|
| 775 |
+
_supports_sdpa = False
|
| 776 |
+
|
| 777 |
+
def _init_weights(self, module):
|
| 778 |
+
# important: this ported version of Llava isn't meant for training from scratch - only
|
| 779 |
+
# inference and fine-tuning - so the proper init weights code has been removed - the original codebase
|
| 780 |
+
# https://github.com/haotian-liu/LLaVA/tree/main/llava should serve for that purpose
|
| 781 |
+
std = (self.config.initializer_range if hasattr(
|
| 782 |
+
self.config, "initializer_range") else
|
| 783 |
+
self.config.text_config.initializer_range)
|
| 784 |
+
|
| 785 |
+
if hasattr(module, "class_embedding"):
|
| 786 |
+
module.class_embedding.data.normal_(mean=0.0, std=std)
|
| 787 |
+
|
| 788 |
+
if isinstance(module, (nn.Linear, nn.Conv2d)):
|
| 789 |
+
module.weight.data.normal_(mean=0.0, std=std)
|
| 790 |
+
if module.bias is not None:
|
| 791 |
+
module.bias.data.zero_()
|
| 792 |
+
elif isinstance(module, nn.Embedding):
|
| 793 |
+
module.weight.data.normal_(mean=0.0, std=std)
|
| 794 |
+
if module.padding_idx is not None:
|
| 795 |
+
module.weight.data[module.padding_idx].zero_()
|
| 796 |
+
|
| 797 |
+
|
| 798 |
+
class VisionTowerConfig(PretrainedConfig):
|
| 799 |
+
model_type = 'moonvit3d'
|
| 800 |
+
|
| 801 |
+
def __init__(self, config: KimiK25Config, **kwargs):
|
| 802 |
+
super().__init__(**kwargs)
|
| 803 |
+
self.patch_size = config.patch_size
|
| 804 |
+
self.init_pos_emb_height = config.init_pos_emb_height
|
| 805 |
+
self.init_pos_emb_width = config.init_pos_emb_width
|
| 806 |
+
self.init_pos_emb_time = config.init_pos_emb_time
|
| 807 |
+
self.pos_emb_type = config.pos_emb_type
|
| 808 |
+
self.num_attention_heads = config.vt_num_attention_heads
|
| 809 |
+
self.num_hidden_layers = config.vt_num_hidden_layers
|
| 810 |
+
self.hidden_size = config.vt_hidden_size
|
| 811 |
+
self.intermediate_size = config.vt_intermediate_size
|
| 812 |
+
self.merge_kernel_size = config.merge_kernel_size
|
| 813 |
+
self.video_attn_type = config.video_attn_type
|
| 814 |
+
self.merge_type = config.merge_type
|
| 815 |
+
self._attn_implementation = config._attn_implementation
|
| 816 |
+
|
| 817 |
+
|
| 818 |
+
class ProjectorConfig:
|
| 819 |
+
|
| 820 |
+
def __init__(self, config: KimiK25Config):
|
| 821 |
+
self.mm_projector_type = config.mm_projector_type
|
| 822 |
+
self.mm_hidden_size = config.mm_hidden_size
|
| 823 |
+
self.hidden_size = config.text_hidden_size
|
| 824 |
+
self.merge_kernel_size = config.merge_kernel_size
|
| 825 |
+
self.projector_hidden_act = config.projector_hidden_act
|
| 826 |
+
self.projector_ln_eps = config.projector_ln_eps
|
| 827 |
+
|
| 828 |
+
|
| 829 |
+
# ref https://github.com/huggingface/transformers/blob/78b2929c0554b79e0489b451ce4ece14d265ead2/src/transformers/models/llava/modeling_llava.py#L240
|
| 830 |
+
class KimiK25ForConditionalGeneration(KimiK25PreTrainedModel):
|
| 831 |
+
|
| 832 |
+
def __init__(self, config: KimiK25Config):
|
| 833 |
+
super().__init__(config)
|
| 834 |
+
|
| 835 |
+
vt_config = VisionTowerConfig(config.vision_config)
|
| 836 |
+
self.vision_tower = MoonViT3dPretrainedModel(vt_config)
|
| 837 |
+
|
| 838 |
+
proj_config = ProjectorConfig(config.vision_config)
|
| 839 |
+
if proj_config.mm_projector_type == 'identity':
|
| 840 |
+
self.mm_projector = IdentityMap()
|
| 841 |
+
elif proj_config.mm_projector_type == 'mlp':
|
| 842 |
+
self.mm_projector = MLP(proj_config)
|
| 843 |
+
elif proj_config.mm_projector_type == 'patchmerger':
|
| 844 |
+
self.mm_projector = PatchMergerMLP(proj_config)
|
| 845 |
+
else:
|
| 846 |
+
raise ValueError(
|
| 847 |
+
f"Unsupported mm_projector_type: {proj_config.mm_projector_type}"
|
| 848 |
+
)
|
| 849 |
+
|
| 850 |
+
self.language_model = DeepseekV3ForCausalLM(config.text_config)
|
| 851 |
+
self.post_init()
|
| 852 |
+
|
| 853 |
+
if hasattr(self.language_model, 'dtype'):
|
| 854 |
+
target_dtype = self.language_model.dtype
|
| 855 |
+
self.vision_tower = self.vision_tower.to(dtype=target_dtype)
|
| 856 |
+
self.mm_projector = self.mm_projector.to(dtype=target_dtype)
|
| 857 |
+
|
| 858 |
+
def get_input_embeddings(self):
|
| 859 |
+
return self.language_model.get_input_embeddings()
|
| 860 |
+
|
| 861 |
+
def set_input_embeddings(self, value):
|
| 862 |
+
self.language_model.set_input_embeddings(value)
|
| 863 |
+
|
| 864 |
+
def get_output_embeddings(self):
|
| 865 |
+
return self.language_model.get_output_embeddings()
|
| 866 |
+
|
| 867 |
+
def set_output_embeddings(self, new_embeddings):
|
| 868 |
+
self.language_model.set_output_embeddings(new_embeddings)
|
| 869 |
+
|
| 870 |
+
def set_decoder(self, decoder):
|
| 871 |
+
self.language_model.set_decoder(decoder)
|
| 872 |
+
|
| 873 |
+
def get_decoder(self):
|
| 874 |
+
return self.language_model.get_decoder()
|
| 875 |
+
|
| 876 |
+
def tie_weights(self):
|
| 877 |
+
return self.language_model.tie_weights()
|
| 878 |
+
|
| 879 |
+
def resize_token_embeddings(self,
|
| 880 |
+
new_num_tokens: int | None = None,
|
| 881 |
+
pad_to_multiple_of=None) -> nn.Embedding:
|
| 882 |
+
model_embeds = self.language_model.resize_token_embeddings(
|
| 883 |
+
new_num_tokens, pad_to_multiple_of)
|
| 884 |
+
# update vocab size
|
| 885 |
+
self.config.text_config.vocab_size = model_embeds.num_embeddings
|
| 886 |
+
self.vocab_size = model_embeds.num_embeddings
|
| 887 |
+
return model_embeds
|
| 888 |
+
|
| 889 |
+
def _merge_input_ids_with_image_features(
|
| 890 |
+
self,
|
| 891 |
+
image_features: list[torch.Tensor],
|
| 892 |
+
inputs_embeds: torch.Tensor,
|
| 893 |
+
input_ids: torch.Tensor,
|
| 894 |
+
attention_mask: torch.Tensor,
|
| 895 |
+
labels: torch.Tensor | None = None,
|
| 896 |
+
):
|
| 897 |
+
"""
|
| 898 |
+
Args:
|
| 899 |
+
image_features (:obj:`torch.Tensor` of shape :obj:`(num_image_tokens, embed_dim)`):
|
| 900 |
+
The image features to merge with the input embeddings.
|
| 901 |
+
inputs_embeds (:obj:`torch.Tensor` of shape :obj:`(batch_size, sequence_length, embed_dim)`):
|
| 902 |
+
The input embeddings.
|
| 903 |
+
input_ids (:obj:`torch.Tensor` of shape :obj:`(batch_size, sequence_length)`):
|
| 904 |
+
The input ids.
|
| 905 |
+
attention_mask (:obj:`torch.Tensor` of shape :obj:`(batch_size, sequence_length)`):
|
| 906 |
+
The attention mask.
|
| 907 |
+
labels (:obj:`torch.Tensor` of shape :obj:`(batch_size, sequence_length)`, *optional*):
|
| 908 |
+
The labels.
|
| 909 |
+
"""
|
| 910 |
+
_, embed_dim = image_features[0].shape
|
| 911 |
+
feature_lengths = [x.shape[0] for x in image_features]
|
| 912 |
+
image_features = torch.cat(image_features, dim=0)
|
| 913 |
+
|
| 914 |
+
image_token_index: int = self.config.media_placeholder_token_id
|
| 915 |
+
pad_token_id: int = self.config.pad_token_id
|
| 916 |
+
ignore_index: int = self.config.ignore_index
|
| 917 |
+
|
| 918 |
+
batch_size, sequence_length = input_ids.shape
|
| 919 |
+
left_padding = not torch.sum(
|
| 920 |
+
input_ids[:, -1] == torch.tensor(pad_token_id))
|
| 921 |
+
|
| 922 |
+
# 1. Create a mask to know where special image tokens are
|
| 923 |
+
_token_occupation_table = torch.ones_like(input_ids.flatten())
|
| 924 |
+
_token_occupation_table[input_ids.flatten() ==
|
| 925 |
+
image_token_index] = torch.tensor(
|
| 926 |
+
feature_lengths,
|
| 927 |
+
dtype=torch.long,
|
| 928 |
+
device=input_ids.device)
|
| 929 |
+
_token_occupation_table = _token_occupation_table.reshape(
|
| 930 |
+
input_ids.shape)
|
| 931 |
+
|
| 932 |
+
max_embed_dim = _token_occupation_table.sum(-1).max().item()
|
| 933 |
+
assert (
|
| 934 |
+
max_embed_dim >= sequence_length
|
| 935 |
+
), f"The maximum embedding dimension ({max_embed_dim}) is less than the sequence length ({sequence_length})"
|
| 936 |
+
batch_indices, non_image_indices = torch.where(
|
| 937 |
+
input_ids != image_token_index)
|
| 938 |
+
|
| 939 |
+
# 2. Compute the positions where text should be written
|
| 940 |
+
# Calculate new positions for text tokens in merged image-text sequence.
|
| 941 |
+
new_token_positions = torch.cumsum(_token_occupation_table, -1) - 1
|
| 942 |
+
nb_image_pad = max_embed_dim - 1 - new_token_positions[:, -1]
|
| 943 |
+
if left_padding:
|
| 944 |
+
new_token_positions += nb_image_pad[:,
|
| 945 |
+
None] # offset for left padding
|
| 946 |
+
text_to_overwrite = new_token_positions[batch_indices,
|
| 947 |
+
non_image_indices]
|
| 948 |
+
|
| 949 |
+
# 3. Create the full embedding, already padded to the maximum position
|
| 950 |
+
final_embedding = torch.zeros(
|
| 951 |
+
batch_size,
|
| 952 |
+
max_embed_dim,
|
| 953 |
+
embed_dim,
|
| 954 |
+
dtype=inputs_embeds.dtype,
|
| 955 |
+
device=inputs_embeds.device,
|
| 956 |
+
)
|
| 957 |
+
final_attention_mask = torch.zeros(batch_size,
|
| 958 |
+
max_embed_dim,
|
| 959 |
+
dtype=attention_mask.dtype,
|
| 960 |
+
device=inputs_embeds.device)
|
| 961 |
+
if labels is not None:
|
| 962 |
+
final_labels = torch.full(
|
| 963 |
+
(batch_size, max_embed_dim),
|
| 964 |
+
ignore_index,
|
| 965 |
+
dtype=input_ids.dtype,
|
| 966 |
+
device=input_ids.device,
|
| 967 |
+
)
|
| 968 |
+
# In case the Vision model or the Language model has been offloaded to CPU, we need to manually
|
| 969 |
+
# set the corresponding tensors into their correct target device.
|
| 970 |
+
target_device = inputs_embeds.device
|
| 971 |
+
batch_indices, non_image_indices, text_to_overwrite = (
|
| 972 |
+
batch_indices.to(target_device),
|
| 973 |
+
non_image_indices.to(target_device),
|
| 974 |
+
text_to_overwrite.to(target_device),
|
| 975 |
+
)
|
| 976 |
+
attention_mask = attention_mask.to(target_device)
|
| 977 |
+
|
| 978 |
+
# 4. Fill the embeddings based on the mask.
|
| 979 |
+
final_embedding[batch_indices,
|
| 980 |
+
text_to_overwrite] = inputs_embeds[batch_indices,
|
| 981 |
+
non_image_indices]
|
| 982 |
+
final_attention_mask[batch_indices,
|
| 983 |
+
text_to_overwrite] = attention_mask[
|
| 984 |
+
batch_indices, non_image_indices]
|
| 985 |
+
if labels is not None:
|
| 986 |
+
final_labels[batch_indices,
|
| 987 |
+
text_to_overwrite] = labels[batch_indices,
|
| 988 |
+
non_image_indices]
|
| 989 |
+
|
| 990 |
+
# 5. Fill the embeddings corresponding to the images. Anything that is not `text_positions` needs filling (#29835)
|
| 991 |
+
image_to_overwrite = torch.full((batch_size, max_embed_dim),
|
| 992 |
+
True,
|
| 993 |
+
dtype=torch.bool,
|
| 994 |
+
device=inputs_embeds.device)
|
| 995 |
+
image_to_overwrite[batch_indices, text_to_overwrite] = False
|
| 996 |
+
image_to_overwrite &= image_to_overwrite.cumsum(
|
| 997 |
+
-1) - 1 >= nb_image_pad[:, None].to(target_device)
|
| 998 |
+
|
| 999 |
+
if image_to_overwrite.sum() != image_features.shape[:-1].numel():
|
| 1000 |
+
raise ValueError(
|
| 1001 |
+
f"The input provided to the model are wrong. The number of image tokens is {image_to_overwrite.sum()} while"
|
| 1002 |
+
f" the number of image features given to the model is {image_features.shape[:-1].numel()}. "
|
| 1003 |
+
"This prevents correct indexing and breaks batch generation.")
|
| 1004 |
+
|
| 1005 |
+
final_embedding[image_to_overwrite] = (
|
| 1006 |
+
image_features.contiguous().reshape(-1,
|
| 1007 |
+
embed_dim).to(target_device))
|
| 1008 |
+
final_attention_mask |= image_to_overwrite
|
| 1009 |
+
position_ids = (final_attention_mask.cumsum(-1) - 1).masked_fill_(
|
| 1010 |
+
(final_attention_mask == 0), 1)
|
| 1011 |
+
|
| 1012 |
+
# 6. Mask out the embedding at padding positions, as we later use the past_key_value value to determine the non-attended tokens.
|
| 1013 |
+
batch_indices, pad_indices = torch.where(input_ids == pad_token_id)
|
| 1014 |
+
indices_to_mask = new_token_positions[batch_indices, pad_indices]
|
| 1015 |
+
|
| 1016 |
+
final_embedding[batch_indices, indices_to_mask] = 0
|
| 1017 |
+
|
| 1018 |
+
if labels is None:
|
| 1019 |
+
final_labels = None
|
| 1020 |
+
|
| 1021 |
+
return final_embedding, final_attention_mask, final_labels, position_ids
|
| 1022 |
+
|
| 1023 |
+
def _extract_image_features(self, pixel_values: torch.Tensor,
|
| 1024 |
+
grid_thws: torch.Tensor) -> list[torch.Tensor]:
|
| 1025 |
+
"""
|
| 1026 |
+
Args:
|
| 1027 |
+
pixel_values (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, num_channels, height, width)`):
|
| 1028 |
+
The pixel values of the images processed by image processor.
|
| 1029 |
+
grid_thws (:obj:`torch.Tensor` of shape :obj:`(batch_size, 3)`):
|
| 1030 |
+
The grid, height, width of the images.
|
| 1031 |
+
|
| 1032 |
+
Returns:
|
| 1033 |
+
selected_image_feature (:obj:`torch.FloatTensor` of shape :obj:`(num_image_tokens, embed_dim)`):
|
| 1034 |
+
The selected image features to use as input to the projector head.
|
| 1035 |
+
|
| 1036 |
+
"""
|
| 1037 |
+
|
| 1038 |
+
target_dtype = self.vision_tower.patch_embed.proj.weight.dtype
|
| 1039 |
+
pixel_values = pixel_values.to(target_dtype)
|
| 1040 |
+
|
| 1041 |
+
image_features = self.vision_tower(pixel_values, grid_thws)
|
| 1042 |
+
return image_features
|
| 1043 |
+
|
| 1044 |
+
def forward(
|
| 1045 |
+
self,
|
| 1046 |
+
input_ids: torch.LongTensor | None = None,
|
| 1047 |
+
pixel_values: torch.FloatTensor | list[torch.FloatTensor]
|
| 1048 |
+
| None = None,
|
| 1049 |
+
grid_thws: torch.Tensor | None = None,
|
| 1050 |
+
attention_mask: torch.Tensor | None = None,
|
| 1051 |
+
position_ids: torch.LongTensor | None = None,
|
| 1052 |
+
past_key_values: list[torch.FloatTensor] | None = None,
|
| 1053 |
+
inputs_embeds: torch.FloatTensor | None = None,
|
| 1054 |
+
labels: torch.LongTensor | None = None,
|
| 1055 |
+
use_cache: bool | None = None,
|
| 1056 |
+
output_attentions: bool | None = None,
|
| 1057 |
+
output_hidden_states: bool | None = None,
|
| 1058 |
+
return_dict: bool | None = None,
|
| 1059 |
+
) -> tuple | LlavaCausalLMOutputWithPast:
|
| 1060 |
+
r"""
|
| 1061 |
+
Args:
|
| 1062 |
+
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
| 1063 |
+
Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
|
| 1064 |
+
config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
|
| 1065 |
+
(masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
|
| 1066 |
+
|
| 1067 |
+
```"""
|
| 1068 |
+
assert self.vision_tower is not None, "vision_tower is not loaded"
|
| 1069 |
+
output_attentions = (output_attentions if output_attentions is not None
|
| 1070 |
+
else self.config.output_attentions)
|
| 1071 |
+
output_hidden_states = (output_hidden_states
|
| 1072 |
+
if output_hidden_states is not None else
|
| 1073 |
+
self.config.output_hidden_states)
|
| 1074 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 1075 |
+
|
| 1076 |
+
if inputs_embeds is None:
|
| 1077 |
+
# 1. Extra the input embeddings
|
| 1078 |
+
inputs_embeds = self.get_input_embeddings()(input_ids)
|
| 1079 |
+
|
| 1080 |
+
# 2. Merge text and images
|
| 1081 |
+
if pixel_values is not None and len(
|
| 1082 |
+
pixel_values) > 0 and input_ids.shape[1] != 1:
|
| 1083 |
+
image_features = self._extract_image_features(
|
| 1084 |
+
pixel_values, grid_thws)
|
| 1085 |
+
if self.mm_projector:
|
| 1086 |
+
image_features = self.mm_projector(image_features)
|
| 1087 |
+
|
| 1088 |
+
inputs_embeds = inputs_embeds.to(
|
| 1089 |
+
image_features[0].dtype) # num_tokens, embed_dim
|
| 1090 |
+
inputs_embeds, attention_mask, labels, position_ids = (
|
| 1091 |
+
self._merge_input_ids_with_image_features(
|
| 1092 |
+
image_features,
|
| 1093 |
+
inputs_embeds,
|
| 1094 |
+
input_ids,
|
| 1095 |
+
attention_mask,
|
| 1096 |
+
labels,
|
| 1097 |
+
))
|
| 1098 |
+
|
| 1099 |
+
# In case input_ids.shape[1] == 1 & pixel_values==None & past_key_values != None, we are in the case of
|
| 1100 |
+
# generation with cache
|
| 1101 |
+
elif (past_key_values is not None and pixel_values is not None
|
| 1102 |
+
and input_ids.shape[1] == 1):
|
| 1103 |
+
# Retrieve the first layer to inspect the logits and mask out the hidden states
|
| 1104 |
+
# that are set to 0
|
| 1105 |
+
first_layer_past_key_value = past_key_values[0][0][:, :, :, 0]
|
| 1106 |
+
|
| 1107 |
+
# Sum all dimensions of head_dim (-2) to avoid random errors such as: https://github.com/huggingface/transformers/pull/28032#issuecomment-1863691941
|
| 1108 |
+
batch_index, non_attended_tokens = torch.where(
|
| 1109 |
+
first_layer_past_key_value.float().sum(-2) == 0)
|
| 1110 |
+
|
| 1111 |
+
# Get the target length
|
| 1112 |
+
target_length = input_ids.shape[1]
|
| 1113 |
+
past_length = first_layer_past_key_value.shape[-1]
|
| 1114 |
+
|
| 1115 |
+
extended_attention_mask = torch.ones(
|
| 1116 |
+
(attention_mask.shape[0], past_length),
|
| 1117 |
+
dtype=attention_mask.dtype,
|
| 1118 |
+
device=attention_mask.device,
|
| 1119 |
+
)
|
| 1120 |
+
|
| 1121 |
+
# Filter out only the tokens that can be un-attended, this can happen
|
| 1122 |
+
# if one uses Llava + Fused modules where the cache on the
|
| 1123 |
+
# first iteration is already big enough, or if one passes custom cache
|
| 1124 |
+
valid_indices = non_attended_tokens < extended_attention_mask.size(
|
| 1125 |
+
-1)
|
| 1126 |
+
new_batch_index = batch_index[valid_indices]
|
| 1127 |
+
new_non_attended_tokens = non_attended_tokens[valid_indices]
|
| 1128 |
+
|
| 1129 |
+
# Zero-out the places where we don't need to attend
|
| 1130 |
+
extended_attention_mask[new_batch_index,
|
| 1131 |
+
new_non_attended_tokens] = 0
|
| 1132 |
+
|
| 1133 |
+
attention_mask = torch.cat(
|
| 1134 |
+
(extended_attention_mask, attention_mask[:,
|
| 1135 |
+
-target_length:]),
|
| 1136 |
+
dim=1)
|
| 1137 |
+
position_ids = torch.sum(attention_mask,
|
| 1138 |
+
dim=1).unsqueeze(-1) - 1
|
| 1139 |
+
|
| 1140 |
+
outputs = self.language_model(
|
| 1141 |
+
attention_mask=attention_mask,
|
| 1142 |
+
position_ids=position_ids,
|
| 1143 |
+
past_key_values=past_key_values,
|
| 1144 |
+
inputs_embeds=inputs_embeds,
|
| 1145 |
+
use_cache=use_cache,
|
| 1146 |
+
output_attentions=output_attentions,
|
| 1147 |
+
output_hidden_states=output_hidden_states,
|
| 1148 |
+
return_dict=return_dict,
|
| 1149 |
+
)
|
| 1150 |
+
|
| 1151 |
+
logits = outputs[0]
|
| 1152 |
+
|
| 1153 |
+
loss = None
|
| 1154 |
+
if labels is not None:
|
| 1155 |
+
# Shift so that tokens < n predict n
|
| 1156 |
+
if attention_mask is not None:
|
| 1157 |
+
shift_attention_mask = attention_mask[..., 1:]
|
| 1158 |
+
shift_logits = logits[..., :-1, :][shift_attention_mask.to(
|
| 1159 |
+
logits.device) != 0].contiguous()
|
| 1160 |
+
shift_labels = labels[..., 1:][shift_attention_mask.to(
|
| 1161 |
+
labels.device) != 0].contiguous()
|
| 1162 |
+
else:
|
| 1163 |
+
shift_logits = logits[..., :-1, :].contiguous()
|
| 1164 |
+
shift_labels = labels[..., 1:].contiguous()
|
| 1165 |
+
# Flatten the tokens
|
| 1166 |
+
loss_fct = nn.CrossEntropyLoss()
|
| 1167 |
+
loss = loss_fct(
|
| 1168 |
+
shift_logits.view(-1, shift_logits.size(-1)),
|
| 1169 |
+
shift_labels.view(-1).to(shift_logits.device),
|
| 1170 |
+
)
|
| 1171 |
+
|
| 1172 |
+
if not return_dict:
|
| 1173 |
+
output = (logits, ) + outputs[1:]
|
| 1174 |
+
return (loss, ) + output if loss is not None else output
|
| 1175 |
+
|
| 1176 |
+
return LlavaCausalLMOutputWithPast(
|
| 1177 |
+
loss=loss,
|
| 1178 |
+
logits=logits,
|
| 1179 |
+
past_key_values=outputs.past_key_values,
|
| 1180 |
+
hidden_states=outputs.hidden_states,
|
| 1181 |
+
attentions=outputs.attentions,
|
| 1182 |
+
)
|
| 1183 |
+
|
| 1184 |
+
def prepare_inputs_for_generation(
|
| 1185 |
+
self,
|
| 1186 |
+
input_ids,
|
| 1187 |
+
past_key_values=None,
|
| 1188 |
+
inputs_embeds=None,
|
| 1189 |
+
pixel_values=None,
|
| 1190 |
+
grid_thws=None,
|
| 1191 |
+
attention_mask=None,
|
| 1192 |
+
**kwargs,
|
| 1193 |
+
):
|
| 1194 |
+
if past_key_values is not None:
|
| 1195 |
+
if isinstance(past_key_values, Cache):
|
| 1196 |
+
cache_length = past_key_values.get_seq_length()
|
| 1197 |
+
past_length = getattr(past_key_values, 'seen_tokens',
|
| 1198 |
+
cache_length)
|
| 1199 |
+
else:
|
| 1200 |
+
cache_length = past_length = past_key_values[0][0].shape[2]
|
| 1201 |
+
|
| 1202 |
+
# Keep only the unprocessed tokens:
|
| 1203 |
+
# 1 - If the length of the attention_mask exceeds the length of input_ids, then we are in a setting where
|
| 1204 |
+
# some of the inputs are exclusively passed as part of the cache (e.g. when passing input_embeds as
|
| 1205 |
+
# input)
|
| 1206 |
+
if attention_mask is not None and attention_mask.shape[
|
| 1207 |
+
1] > input_ids.shape[1]:
|
| 1208 |
+
input_ids = input_ids[:, -(attention_mask.shape[1] -
|
| 1209 |
+
past_length):]
|
| 1210 |
+
# 2 - If the past_length is smaller than input_ids', then input_ids holds all input tokens. We can discard
|
| 1211 |
+
# input_ids based on the past_length.
|
| 1212 |
+
elif past_length < input_ids.shape[1]:
|
| 1213 |
+
input_ids = input_ids[:, past_length:]
|
| 1214 |
+
# 3 - Otherwise (past_length >= input_ids.shape[1]), let's assume input_ids only has unprocessed tokens.
|
| 1215 |
+
elif self.config.media_placeholder_token_id in input_ids:
|
| 1216 |
+
input_ids = input_ids[:, input_ids.shape[1] - 1:]
|
| 1217 |
+
# If the cache has seen more tokens than it can hold, then the cache has a size limit. Let's discard the
|
| 1218 |
+
# older attention values, as their corresponding values are not part of the input.
|
| 1219 |
+
if cache_length < past_length and attention_mask is not None:
|
| 1220 |
+
attention_mask = attention_mask[:, -(cache_length +
|
| 1221 |
+
input_ids.shape[1]):]
|
| 1222 |
+
|
| 1223 |
+
position_ids = kwargs.get("position_ids", None)
|
| 1224 |
+
if attention_mask is not None and position_ids is None:
|
| 1225 |
+
# create position_ids on the fly for batch generation
|
| 1226 |
+
position_ids = attention_mask.long().cumsum(-1) - 1
|
| 1227 |
+
position_ids.masked_fill_(attention_mask == 0, 1)
|
| 1228 |
+
if past_key_values:
|
| 1229 |
+
position_ids = position_ids[:, -input_ids.shape[1]:]
|
| 1230 |
+
|
| 1231 |
+
# if `inputs_embeds` are passed, we only want to use them in the 1st generation step
|
| 1232 |
+
if inputs_embeds is not None and past_key_values is None:
|
| 1233 |
+
model_inputs = {"inputs_embeds": inputs_embeds}
|
| 1234 |
+
else:
|
| 1235 |
+
model_inputs = {"input_ids": input_ids}
|
| 1236 |
+
|
| 1237 |
+
model_inputs.update({
|
| 1238 |
+
"position_ids": position_ids,
|
| 1239 |
+
"past_key_values": past_key_values,
|
| 1240 |
+
"use_cache": kwargs.get("use_cache"),
|
| 1241 |
+
"attention_mask": attention_mask,
|
| 1242 |
+
"pixel_values": pixel_values,
|
| 1243 |
+
"grid_thws": grid_thws,
|
| 1244 |
+
})
|
| 1245 |
+
return model_inputs
|
| 1246 |
+
|
| 1247 |
+
def _reorder_cache(self, *args, **kwargs):
|
| 1248 |
+
return self.language_model._reorder_cache(*args, **kwargs)
|
preprocessor_config.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"auto_map": {
|
| 3 |
+
"AutoProcessor": "kimi_k25_processor.KimiK25Processor",
|
| 4 |
+
"AutoImageProcessor": "kimi_k25_vision_processing.KimiK25VisionProcessor"
|
| 5 |
+
},
|
| 6 |
+
"media_proc_cfg": {
|
| 7 |
+
"in_patch_limit": 16384,
|
| 8 |
+
"patch_size": 14,
|
| 9 |
+
"image_mean": [
|
| 10 |
+
0.5,
|
| 11 |
+
0.5,
|
| 12 |
+
0.5
|
| 13 |
+
],
|
| 14 |
+
"image_std": [
|
| 15 |
+
0.5,
|
| 16 |
+
0.5,
|
| 17 |
+
0.5
|
| 18 |
+
],
|
| 19 |
+
"merge_kernel_size": 2,
|
| 20 |
+
"fixed_output_tokens": null,
|
| 21 |
+
"patch_limit_on_one_side": 512,
|
| 22 |
+
"in_patch_limit_each_frame": 4096,
|
| 23 |
+
"in_patch_limit_video": null,
|
| 24 |
+
"sample_fps": 2.0,
|
| 25 |
+
"max_num_frames_each_video": null,
|
| 26 |
+
"temporal_merge_kernel_size": 4,
|
| 27 |
+
"timestamp_mode": "hh:mm:ss.fff",
|
| 28 |
+
"config_type": "media_proc.processors.moonvit.MoonViTMediaProcessorConfig"
|
| 29 |
+
}
|
| 30 |
+
}
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"added_tokens_decoder": {
|
| 3 |
+
"163584": {
|
| 4 |
+
"content": "[BOS]",
|
| 5 |
+
"lstrip": false,
|
| 6 |
+
"normalized": false,
|
| 7 |
+
"rstrip": false,
|
| 8 |
+
"single_word": false,
|
| 9 |
+
"special": true
|
| 10 |
+
},
|
| 11 |
+
"163585": {
|
| 12 |
+
"content": "[EOS]",
|
| 13 |
+
"lstrip": false,
|
| 14 |
+
"normalized": false,
|
| 15 |
+
"rstrip": false,
|
| 16 |
+
"single_word": false,
|
| 17 |
+
"special": true
|
| 18 |
+
},
|
| 19 |
+
"163586": {
|
| 20 |
+
"content": "<|im_end|>",
|
| 21 |
+
"lstrip": false,
|
| 22 |
+
"normalized": false,
|
| 23 |
+
"rstrip": false,
|
| 24 |
+
"single_word": false,
|
| 25 |
+
"special": true
|
| 26 |
+
},
|
| 27 |
+
"163587": {
|
| 28 |
+
"content": "<|im_user|>",
|
| 29 |
+
"lstrip": false,
|
| 30 |
+
"normalized": false,
|
| 31 |
+
"rstrip": false,
|
| 32 |
+
"single_word": false,
|
| 33 |
+
"special": true
|
| 34 |
+
},
|
| 35 |
+
"163588": {
|
| 36 |
+
"content": "<|im_assistant|>",
|
| 37 |
+
"lstrip": false,
|
| 38 |
+
"normalized": false,
|
| 39 |
+
"rstrip": false,
|
| 40 |
+
"single_word": false,
|
| 41 |
+
"special": true
|
| 42 |
+
},
|
| 43 |
+
"163590": {
|
| 44 |
+
"content": "<|start_header_id|>",
|
| 45 |
+
"lstrip": false,
|
| 46 |
+
"normalized": false,
|
| 47 |
+
"rstrip": false,
|
| 48 |
+
"single_word": false,
|
| 49 |
+
"special": true
|
| 50 |
+
},
|
| 51 |
+
"163591": {
|
| 52 |
+
"content": "<|end_header_id|>",
|
| 53 |
+
"lstrip": false,
|
| 54 |
+
"normalized": false,
|
| 55 |
+
"rstrip": false,
|
| 56 |
+
"single_word": false,
|
| 57 |
+
"special": true
|
| 58 |
+
},
|
| 59 |
+
"163593": {
|
| 60 |
+
"content": "[EOT]",
|
| 61 |
+
"lstrip": false,
|
| 62 |
+
"normalized": false,
|
| 63 |
+
"rstrip": false,
|
| 64 |
+
"single_word": false,
|
| 65 |
+
"special": true
|
| 66 |
+
},
|
| 67 |
+
"163594": {
|
| 68 |
+
"content": "<|im_system|>",
|
| 69 |
+
"lstrip": false,
|
| 70 |
+
"normalized": false,
|
| 71 |
+
"rstrip": false,
|
| 72 |
+
"single_word": false,
|
| 73 |
+
"special": true
|
| 74 |
+
},
|
| 75 |
+
"163595": {
|
| 76 |
+
"content": "<|tool_calls_section_begin|>",
|
| 77 |
+
"lstrip": false,
|
| 78 |
+
"normalized": false,
|
| 79 |
+
"rstrip": false,
|
| 80 |
+
"single_word": false,
|
| 81 |
+
"special": false
|
| 82 |
+
},
|
| 83 |
+
"163596": {
|
| 84 |
+
"content": "<|tool_calls_section_end|>",
|
| 85 |
+
"lstrip": false,
|
| 86 |
+
"normalized": false,
|
| 87 |
+
"rstrip": false,
|
| 88 |
+
"single_word": false,
|
| 89 |
+
"special": false
|
| 90 |
+
},
|
| 91 |
+
"163597": {
|
| 92 |
+
"content": "<|tool_call_begin|>",
|
| 93 |
+
"lstrip": false,
|
| 94 |
+
"normalized": false,
|
| 95 |
+
"rstrip": false,
|
| 96 |
+
"single_word": false,
|
| 97 |
+
"special": false
|
| 98 |
+
},
|
| 99 |
+
"163598": {
|
| 100 |
+
"content": "<|tool_call_argument_begin|>",
|
| 101 |
+
"lstrip": false,
|
| 102 |
+
"normalized": false,
|
| 103 |
+
"rstrip": false,
|
| 104 |
+
"single_word": false,
|
| 105 |
+
"special": false
|
| 106 |
+
},
|
| 107 |
+
"163599": {
|
| 108 |
+
"content": "<|tool_call_end|>",
|
| 109 |
+
"lstrip": false,
|
| 110 |
+
"normalized": false,
|
| 111 |
+
"rstrip": false,
|
| 112 |
+
"single_word": false,
|
| 113 |
+
"special": false
|
| 114 |
+
},
|
| 115 |
+
"163601": {
|
| 116 |
+
"content": "<|im_middle|>",
|
| 117 |
+
"lstrip": false,
|
| 118 |
+
"normalized": false,
|
| 119 |
+
"rstrip": false,
|
| 120 |
+
"single_word": false,
|
| 121 |
+
"special": true
|
| 122 |
+
},
|
| 123 |
+
"163602": {
|
| 124 |
+
"content": "<|media_begin|>",
|
| 125 |
+
"lstrip": false,
|
| 126 |
+
"normalized": false,
|
| 127 |
+
"rstrip": false,
|
| 128 |
+
"single_word": false,
|
| 129 |
+
"special": true
|
| 130 |
+
},
|
| 131 |
+
"163603": {
|
| 132 |
+
"content": "<|media_content|>",
|
| 133 |
+
"lstrip": false,
|
| 134 |
+
"normalized": false,
|
| 135 |
+
"rstrip": false,
|
| 136 |
+
"single_word": false,
|
| 137 |
+
"special": true
|
| 138 |
+
},
|
| 139 |
+
"163604": {
|
| 140 |
+
"content": "<|media_end|>",
|
| 141 |
+
"lstrip": false,
|
| 142 |
+
"normalized": false,
|
| 143 |
+
"rstrip": false,
|
| 144 |
+
"single_word": false,
|
| 145 |
+
"special": true
|
| 146 |
+
},
|
| 147 |
+
"163605": {
|
| 148 |
+
"content": "<|media_pad|>",
|
| 149 |
+
"lstrip": false,
|
| 150 |
+
"normalized": false,
|
| 151 |
+
"rstrip": false,
|
| 152 |
+
"single_word": false,
|
| 153 |
+
"special": true
|
| 154 |
+
},
|
| 155 |
+
"163606": {
|
| 156 |
+
"content": "<think>",
|
| 157 |
+
"lstrip": false,
|
| 158 |
+
"normalized": false,
|
| 159 |
+
"rstrip": false,
|
| 160 |
+
"single_word": false,
|
| 161 |
+
"special": false
|
| 162 |
+
},
|
| 163 |
+
"163607": {
|
| 164 |
+
"content": "</think>",
|
| 165 |
+
"lstrip": false,
|
| 166 |
+
"normalized": false,
|
| 167 |
+
"rstrip": false,
|
| 168 |
+
"single_word": false,
|
| 169 |
+
"special": false
|
| 170 |
+
},
|
| 171 |
+
"163838": {
|
| 172 |
+
"content": "[UNK]",
|
| 173 |
+
"lstrip": false,
|
| 174 |
+
"normalized": false,
|
| 175 |
+
"rstrip": false,
|
| 176 |
+
"single_word": false,
|
| 177 |
+
"special": true
|
| 178 |
+
},
|
| 179 |
+
"163839": {
|
| 180 |
+
"content": "[PAD]",
|
| 181 |
+
"lstrip": false,
|
| 182 |
+
"normalized": false,
|
| 183 |
+
"rstrip": false,
|
| 184 |
+
"single_word": false,
|
| 185 |
+
"special": true
|
| 186 |
+
}
|
| 187 |
+
},
|
| 188 |
+
"additional_special_tokens": [
|
| 189 |
+
"<|im_end|>",
|
| 190 |
+
"<|im_user|>",
|
| 191 |
+
"<|im_assistant|>",
|
| 192 |
+
"<|start_header_id|>",
|
| 193 |
+
"<|end_header_id|>",
|
| 194 |
+
"[EOT]",
|
| 195 |
+
"<|im_system|>",
|
| 196 |
+
"<|im_middle|>",
|
| 197 |
+
"<|media_begin|>",
|
| 198 |
+
"<|media_content|>",
|
| 199 |
+
"<|media_end|>",
|
| 200 |
+
"<|media_pad|>"
|
| 201 |
+
],
|
| 202 |
+
"bos_token": "[BOS]",
|
| 203 |
+
"clean_up_tokenization_spaces": false,
|
| 204 |
+
"eos_token": "[EOS]",
|
| 205 |
+
"extra_special_tokens": {},
|
| 206 |
+
"model_max_length": 1000000000000000019884624838656,
|
| 207 |
+
"pad_token": "[PAD]",
|
| 208 |
+
"tokenizer_class": "TikTokenTokenizer",
|
| 209 |
+
"unk_token": "[UNK]",
|
| 210 |
+
"auto_map": {
|
| 211 |
+
"AutoTokenizer": [
|
| 212 |
+
"tokenization_kimi.TikTokenTokenizer",
|
| 213 |
+
null
|
| 214 |
+
]
|
| 215 |
+
}
|
| 216 |
+
}
|