Instructions to use tiny-random/kimi-k2.5 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use tiny-random/kimi-k2.5 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="tiny-random/kimi-k2.5", trust_remote_code=True)# Load model directly from transformers import AutoProcessor, AutoModel processor = AutoProcessor.from_pretrained("tiny-random/kimi-k2.5", trust_remote_code=True) model = AutoModel.from_pretrained("tiny-random/kimi-k2.5", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
| library_name: transformers | |
| base_model: | |
| - moonshotai/Kimi-K2.5 | |
| This tiny model is intended for debugging. It is randomly initialized using the configuration adapted from [moonshotai/Kimi-K2.5](https://huggingface.co/moonshotai/Kimi-K2.5). | |
| | File path | Size | | |
| |------|------| | |
| | model.safetensors | 6.2MB | | |
| ### Example usage: | |
| - vLLM | |
| ```bash | |
| vllm serve tiny-random/kimi-k2.5 --trust-remote-code | |
| ``` | |
| - Transformers | |
| ```python | |
| import base64 | |
| import requests | |
| import torch | |
| from transformers import AutoModel, AutoProcessor | |
| model_id = "tiny-random/kimi-k2.5" | |
| image_url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG" | |
| image_base64 = base64.b64encode(requests.get(image_url).content).decode() | |
| messages = [ | |
| { | |
| 'role': 'user', | |
| 'content': [ | |
| {'type': 'text', 'text': 'Describe this image in detail.'}, | |
| { | |
| 'type': 'image_url', | |
| '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, | |
| torch_dtype=torch.bfloat16, | |
| device_map="cuda", | |
| trust_remote_code=True, | |
| ) | |
| inputs = processor( | |
| messages, | |
| add_generation_prompt=True, | |
| return_tensors="pt" | |
| ).to(model.device) | |
| inputs.pop("token_type_ids", None) | |
| generated_ids = model.generate(**inputs, max_new_tokens=16) | |
| output_text = processor.decode( | |
| generated_ids[0][inputs["input_ids"].shape[1]:], skip_special_tokens=False) | |
| print(output_text) | |
| ``` | |
| ### Codes to create this repo: | |
| <details> | |
| <summary>Python codes</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.5" | |
| save_folder = "/tmp/tiny-random/kimi-k25" | |
| Path(save_folder).mkdir(parents=True, exist_ok=True) | |
| for f in list_repo_files(source_model_id, repo_type="model"): | |
| if (f.endswith('.json') or f.endswith('.py') or f.endswith('.model') or f.endswith('.jinja')) 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', | |
| "use_deterministic_attn=self.use_deterministic_attn", | |
| "") | |
| 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': 64, | |
| 'kv_lora_rank': 384, | |
| 'moe_intermediate_size': 64, | |
| 'n_routed_experts': 32, | |
| 'n_shared_experts': 1, | |
| 'num_attention_heads': 1, | |
| 'num_experts_per_tok': 8, | |
| 'num_key_value_heads': 1, | |
| '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, | |
| }) | |
| del config_json['vision_config']['_attn_implementation'] | |
| 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) | |
| 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() | |
| with torch.no_grad(): | |
| for name, p in sorted(model.named_parameters()): | |
| torch.nn.init.normal_(p, 0, 0.1) | |
| print(name, p.shape) | |
| 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', | |
| "use_deterministic_attn=self.use_deterministic_attn", | |
| "") | |
| ``` | |
| </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): PytorchGELUTanh() | |
| ) | |
| (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() | |
| (q_b_proj): Linear(in_features=32, out_features=256, bias=False) | |
| (kv_a_proj_with_mqa): Linear(in_features=8, out_features=576, bias=False) | |
| (kv_a_layernorm): DeepseekV3RMSNorm() | |
| (kv_b_proj): Linear(in_features=384, out_features=128, bias=False) | |
| (o_proj): Linear(in_features=64, out_features=8, bias=False) | |
| (rotary_emb): DeepseekV3YarnRotaryEmbedding() | |
| ) | |
| (mlp): DeepseekV3MLP( | |
| (gate_proj): Linear(in_features=8, out_features=64, bias=False) | |
| (up_proj): Linear(in_features=8, out_features=64, bias=False) | |
| (down_proj): Linear(in_features=64, out_features=8, bias=False) | |
| (act_fn): SiLU() | |
| ) | |
| (input_layernorm): DeepseekV3RMSNorm() | |
| (post_attention_layernorm): DeepseekV3RMSNorm() | |
| ) | |
| (1): DeepseekV3DecoderLayer( | |
| (self_attn): DeepseekV3Attention( | |
| (q_a_proj): Linear(in_features=8, out_features=32, bias=False) | |
| (q_a_layernorm): DeepseekV3RMSNorm() | |
| (q_b_proj): Linear(in_features=32, out_features=256, bias=False) | |
| (kv_a_proj_with_mqa): Linear(in_features=8, out_features=576, bias=False) | |
| (kv_a_layernorm): DeepseekV3RMSNorm() | |
| (kv_b_proj): Linear(in_features=384, out_features=128, bias=False) | |
| (o_proj): Linear(in_features=64, out_features=8, bias=False) | |
| (rotary_emb): DeepseekV3YarnRotaryEmbedding() | |
| ) | |
| (mlp): DeepseekV3MoE( | |
| (experts): ModuleList( | |
| (0-31): 32 x DeepseekV3MLP( | |
| (gate_proj): Linear(in_features=8, out_features=64, bias=False) | |
| (up_proj): Linear(in_features=8, out_features=64, bias=False) | |
| (down_proj): Linear(in_features=64, out_features=8, bias=False) | |
| (act_fn): SiLU() | |
| ) | |
| ) | |
| (gate): MoEGate() | |
| (shared_experts): DeepseekV3MLP( | |
| (gate_proj): Linear(in_features=8, out_features=64, bias=False) | |
| (up_proj): Linear(in_features=8, out_features=64, bias=False) | |
| (down_proj): Linear(in_features=64, out_features=8, bias=False) | |
| (act_fn): SiLU() | |
| ) | |
| ) | |
| (input_layernorm): DeepseekV3RMSNorm() | |
| (post_attention_layernorm): DeepseekV3RMSNorm() | |
| ) | |
| ) | |
| (norm): DeepseekV3RMSNorm() | |
| ) | |
| (lm_head): Linear(in_features=8, out_features=163840, bias=False) | |
| ) | |
| ) | |
| ``` | |
| </details> |