| --- |
| library_name: transformers |
| base_model: |
| - moonshotai/Kimi-K2.6 |
| --- |
| |
| This tiny model is intended for debugging. It is randomly initialized using the configuration adapted from [moonshotai/Kimi-K2.6](https://huggingface.co/moonshotai/Kimi-K2.6). |
|
|
| | File path | Size | |
| |------|------| |
| | model.safetensors | 9.0MB | |
|
|
|
|
| ### Example usage: |
|
|
| - vLLM |
|
|
| ```bash |
| vllm serve tiny-random/kimi-k2.6 --trust-remote-code |
| ``` |
|
|
| - Transformers |
|
|
| ```python |
| import base64 |
| import requests |
| import torch |
| from transformers import AutoModel, AutoProcessor |
| |
| model_id = "tiny-random/kimi-k2.6" |
| image_url = "https://avatars.githubusercontent.com/u/0" |
| image_base64 = base64.b64encode(requests.get(image_url).content).decode() |
| messages = [ |
| { |
| 'role': 'user', |
| 'content': [ |
| {'type': 'text', 'text': 'Describe this image in detail.'}, |
| { |
| 'type': 'image', |
| 'image_url': f'data:image/png;base64,{image_base64}', |
| }, |
| ], |
| } |
| ] |
| processor = AutoProcessor.from_pretrained( |
| model_id, |
| trust_remote_code=True, |
| ) |
| model = AutoModel.from_pretrained( |
| model_id, |
| dtype=torch.bfloat16, |
| device_map="cuda" if torch.cuda.is_available() else "cpu", |
| trust_remote_code=True, |
| ).eval() |
| |
| # Text generation is not compatible with the latest version of transformers (v5.5) |
| # so we only show a dummy model forward step here |
| inputs = processor( |
| messages=messages, |
| tokenize=False, |
| return_tensors="pt" |
| ).to(model.device) |
| inputs.input_ids[0, -1] = model.config.media_placeholder_token_id |
| print(inputs.keys()) |
| result = model(**inputs) |
| print(result) |
| ``` |
|
|
| ### Codes to create this repo: |
|
|
| <details> |
| <summary>Click to expand</summary> |
|
|
| ```python |
| import json |
| from pathlib import Path |
| |
| import accelerate |
| import torch |
| from huggingface_hub import file_exists, hf_hub_download, list_repo_files |
| from transformers import ( |
| AutoConfig, |
| AutoModel, |
| AutoModelForCausalLM, |
| AutoProcessor, |
| AutoTokenizer, |
| GenerationConfig, |
| set_seed, |
| ) |
| |
| source_model_id = "moonshotai/Kimi-K2.6" |
| save_folder = "/tmp/tiny-random/kimi-k26" |
| |
| Path(save_folder).mkdir(parents=True, exist_ok=True) |
| |
| suffixes = ['.json', '.py', '.model', '.jinja'] |
| for f in list_repo_files(source_model_id, repo_type="model"): |
| if any(f.endswith(suffix) for suffix in suffixes) and not f.endswith('.index.json'): |
| hf_hub_download( |
| repo_id=source_model_id, |
| filename=f, |
| repo_type="model", |
| local_dir=save_folder |
| ) |
| |
| def replace_file(filepath, old_string, new_string): |
| with open(filepath, 'r', encoding='utf-8') as f: |
| code = f.read() |
| code = code.replace(old_string, new_string) |
| with open(filepath, 'w', encoding='utf-8') as f: |
| f.write(code) |
| |
| replace_file(f'{save_folder}/configuration_kimi_k25.py', |
| "from configuration_deepseek import DeepseekV3Config", |
| "from transformers import DeepseekV3Config") |
| replace_file(f'{save_folder}/modeling_kimi_k25.py', |
| "from .modeling_deepseek import DeepseekV3ForCausalLM", |
| "from transformers import DeepseekV3ForCausalLM") |
| replace_file(f'{save_folder}/modeling_kimi_k25.py', |
| "use_deterministic_attn=self.use_deterministic_attn", |
| "") |
| replace_file(f'{save_folder}/modeling_kimi_k25.py', |
| "def tie_weights(self):", |
| "def tie_weights(self, *args, **kwargs):") |
| replace_file(f'{save_folder}/modeling_kimi_k25.py', |
| "_supports_flash_attn_2 = True", |
| "_supports_flash_attn_2 = True\n _supports_flash_attn = True") |
| with open(f'{save_folder}/config.json') as f: |
| config_json = json.load(f) |
| |
| config_json['text_config'].update({ |
| 'first_k_dense_replace': 1, |
| 'num_hidden_layers': 2, |
| 'hidden_size': 8, |
| 'intermediate_size': 32, |
| 'moe_intermediate_size': 32, |
| # 'n_routed_experts': 32, |
| # 'n_shared_experts': 1, |
| 'num_attention_heads': 4, |
| # 'num_experts_per_tok': 8, |
| 'num_key_value_heads': 4, |
| 'q_lora_rank': 32, |
| # 'qk_nope_head_dim': 64, |
| # 'qk_rope_head_dim': 192, |
| # 'v_head_dim': 64, |
| 'tie_word_embeddings': False, |
| }) |
| del config_json['text_config']['quantization_config'] |
| config_json['vision_config'].update({ |
| 'mm_hidden_size': 64, |
| 'text_hidden_size': 8, |
| 'vt_hidden_size': 64, |
| 'vt_intermediate_size': 128, |
| 'vt_num_attention_heads': 2, |
| 'vt_num_hidden_layers': 2, |
| }) |
| config_json['vision_config']['_attn_implementation'] = 'eager' |
| with open(f"{save_folder}/config.json", "w", encoding='utf-8') as f: |
| json.dump(config_json, f, indent=2) |
| |
| config = AutoConfig.from_pretrained( |
| save_folder, |
| trust_remote_code=True, |
| ) |
| print(config) |
| torch.set_default_dtype(torch.bfloat16) |
| model = AutoModel.from_config(config, trust_remote_code=True, attn_implementation='eager') |
| torch.set_default_dtype(torch.float32) |
| if file_exists(filename="generation_config.json", repo_id=source_model_id, repo_type='model'): |
| model.generation_config = GenerationConfig.from_pretrained( |
| source_model_id, trust_remote_code=True, |
| ) |
| set_seed(42) |
| model = model.cpu() |
| num_params = sum(p.numel() for p in model.parameters()) |
| with torch.no_grad(): |
| for name, p in sorted(model.named_parameters()): |
| torch.nn.init.normal_(p, 0, 0.2) |
| print(name, p.shape, p.dtype, p.device, f'{p.numel() / num_params * 100: .2f}%') |
| model.save_pretrained(save_folder) |
| replace_file(f'{save_folder}/configuration_kimi_k25.py', |
| "from configuration_deepseek import DeepseekV3Config", |
| "from transformers import DeepseekV3Config") |
| replace_file(f'{save_folder}/modeling_kimi_k25.py', |
| "from .modeling_deepseek import DeepseekV3ForCausalLM", |
| "from transformers import DeepseekV3ForCausalLM") |
| replace_file(f'{save_folder}/modeling_kimi_k25.py', |
| "use_deterministic_attn=self.use_deterministic_attn", |
| "") |
| replace_file(f'{save_folder}/modeling_kimi_k25.py', |
| "def tie_weights(self):", |
| "def tie_weights(self, *args, **kwargs):") |
| replace_file(f'{save_folder}/modeling_kimi_k25.py', |
| "_supports_flash_attn_2 = True", |
| "_supports_flash_attn_2 = True\n _supports_flash_attn = True") |
| ``` |
|
|
| </details> |
|
|
| ### Printing the model: |
|
|
| <details><summary>Click to expand</summary> |
|
|
| ```text |
| KimiK25ForConditionalGeneration( |
| (vision_tower): MoonViT3dPretrainedModel( |
| (patch_embed): MoonVision3dPatchEmbed( |
| (proj): Conv2d(3, 64, kernel_size=(14, 14), stride=(14, 14)) |
| (pos_emb): Learnable2DInterpPosEmbDivided_fixed() |
| ) |
| (encoder): MoonViT3dEncoder( |
| (rope_2d): Rope2DPosEmbRepeated(dim=32, max_height=512, max_width=512, theta_base=10000) |
| (blocks): ModuleList( |
| (0-1): 2 x MoonViTEncoderLayer( |
| (norm0): LayerNorm((64,), eps=1e-05, elementwise_affine=True) |
| (norm1): LayerNorm((64,), eps=1e-05, elementwise_affine=True) |
| (mlp): MLP2( |
| (fc0): Linear(in_features=64, out_features=128, bias=True) |
| (fc1): Linear(in_features=128, out_features=64, bias=True) |
| (activation): GELUTanh() |
| ) |
| (wqkv): Linear(in_features=64, out_features=192, bias=True) |
| (wo): Linear(in_features=64, out_features=64, bias=True) |
| ) |
| ) |
| (final_layernorm): LayerNorm((64,), eps=1e-05, elementwise_affine=True) |
| ) |
| ) |
| (mm_projector): PatchMergerMLP( |
| (pre_norm): LayerNorm((64,), eps=1e-05, elementwise_affine=True) |
| (proj): Sequential( |
| (0): Linear(in_features=256, out_features=256, bias=True) |
| (1): GELU(approximate='none') |
| (2): Linear(in_features=256, out_features=8, bias=True) |
| ) |
| ) |
| (language_model): DeepseekV3ForCausalLM( |
| (model): DeepseekV3Model( |
| (embed_tokens): Embedding(163840, 8, padding_idx=163839) |
| (layers): ModuleList( |
| (0): DeepseekV3DecoderLayer( |
| (self_attn): DeepseekV3Attention( |
| (q_a_proj): Linear(in_features=8, out_features=32, bias=False) |
| (q_a_layernorm): DeepseekV3RMSNorm((32,), eps=1e-06) |
| (q_b_proj): Linear(in_features=32, out_features=768, bias=False) |
| (kv_a_proj_with_mqa): Linear(in_features=8, out_features=576, bias=False) |
| (kv_a_layernorm): DeepseekV3RMSNorm((512,), eps=1e-06) |
| (kv_b_proj): Linear(in_features=512, out_features=1024, bias=False) |
| (o_proj): Linear(in_features=512, out_features=8, bias=False) |
| ) |
| (mlp): DeepseekV3MLP( |
| (gate_proj): Linear(in_features=8, out_features=32, bias=False) |
| (up_proj): Linear(in_features=8, out_features=32, bias=False) |
| (down_proj): Linear(in_features=32, out_features=8, bias=False) |
| (act_fn): SiLUActivation() |
| ) |
| (input_layernorm): DeepseekV3RMSNorm((8,), eps=1e-05) |
| (post_attention_layernorm): DeepseekV3RMSNorm((8,), eps=1e-05) |
| ) |
| (1): DeepseekV3DecoderLayer( |
| (self_attn): DeepseekV3Attention( |
| (q_a_proj): Linear(in_features=8, out_features=32, bias=False) |
| (q_a_layernorm): DeepseekV3RMSNorm((32,), eps=1e-06) |
| (q_b_proj): Linear(in_features=32, out_features=768, bias=False) |
| (kv_a_proj_with_mqa): Linear(in_features=8, out_features=576, bias=False) |
| (kv_a_layernorm): DeepseekV3RMSNorm((512,), eps=1e-06) |
| (kv_b_proj): Linear(in_features=512, out_features=1024, bias=False) |
| (o_proj): Linear(in_features=512, out_features=8, bias=False) |
| ) |
| (mlp): DeepseekV3MoE( |
| (experts): DeepseekV3NaiveMoe( |
| (act_fn): SiLUActivation() |
| ) |
| (gate): DeepseekV3TopkRouter() |
| (shared_experts): DeepseekV3MLP( |
| (gate_proj): Linear(in_features=8, out_features=32, bias=False) |
| (up_proj): Linear(in_features=8, out_features=32, bias=False) |
| (down_proj): Linear(in_features=32, out_features=8, bias=False) |
| (act_fn): SiLUActivation() |
| ) |
| ) |
| (input_layernorm): DeepseekV3RMSNorm((8,), eps=1e-05) |
| (post_attention_layernorm): DeepseekV3RMSNorm((8,), eps=1e-05) |
| ) |
| ) |
| (norm): DeepseekV3RMSNorm((8,), eps=1e-05) |
| (rotary_emb): DeepseekV3RotaryEmbedding() |
| ) |
| (lm_head): Linear(in_features=8, out_features=163840, bias=False) |
| ) |
| ) |
| ``` |
|
|
| </details> |
|
|
| ### Test environment: |
|
|
| - torch: 2.11.0 |
| - transformers: 5.5.0 |