Instructions to use tiny-random/kimi-k3.1-pro with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use tiny-random/kimi-k3.1-pro with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="tiny-random/kimi-k3.1-pro", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForSeq2SeqLM model = AutoModelForSeq2SeqLM.from_pretrained("tiny-random/kimi-k3.1-pro", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use tiny-random/kimi-k3.1-pro with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "tiny-random/kimi-k3.1-pro" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "tiny-random/kimi-k3.1-pro", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/tiny-random/kimi-k3.1-pro
- SGLang
How to use tiny-random/kimi-k3.1-pro with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "tiny-random/kimi-k3.1-pro" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "tiny-random/kimi-k3.1-pro", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "tiny-random/kimi-k3.1-pro" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "tiny-random/kimi-k3.1-pro", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use tiny-random/kimi-k3.1-pro with Docker Model Runner:
docker model run hf.co/tiny-random/kimi-k3.1-pro
Upload folder using huggingface_hub
Browse files- .meta/create_model.py +40 -0
- .meta/env.json +22 -1
- README.md +1 -1
- modeling_kimi_k3.py +6 -0
- modeling_kimi_linear.py +5 -2
.meta/create_model.py
CHANGED
|
@@ -107,6 +107,25 @@ def create_model(source_model_id: str, source_revision: str, save_folder: str) -
|
|
| 107 |
past_key_values=past_key_values,
|
| 108 |
position_ids=position_ids,
|
| 109 |
)"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
replace_file(
|
| 112 |
f"{folder}/modeling_kimi_linear.py",
|
|
@@ -128,6 +147,7 @@ def create_model(source_model_id: str, source_revision: str, save_folder: str) -
|
|
| 128 |
)""",
|
| 129 |
),
|
| 130 |
(causal_mask_code, causal_mask_compat_code),
|
|
|
|
| 131 |
(
|
| 132 |
force_flash_code,
|
| 133 |
' config._attn_implementation = getattr(config, "_attn_implementation", "eager")',
|
|
@@ -163,6 +183,26 @@ def create_model(source_model_id: str, source_revision: str, save_folder: str) -
|
|
| 163 |
else:
|
| 164 |
first_layer_past_key_value = past_key_values[0][0][:, :, :, 0]""",
|
| 165 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
],
|
| 167 |
)
|
| 168 |
|
|
|
|
| 107 |
past_key_values=past_key_values,
|
| 108 |
position_ids=position_ids,
|
| 109 |
)"""
|
| 110 |
+
cache_api_code = ''' def get_mask_sizes(self, cache_position: torch.Tensor, layer_idx: int) -> tuple[int, int]:
|
| 111 |
+
"""
|
| 112 |
+
Return a tuple (kv_length, kv_offset) corresponding to the length and offset that will be returned for
|
| 113 |
+
the given layer at `layer_idx`.
|
| 114 |
+
The masks are then prepared according to the given lengths (kv_length, kv_offset) and patterns for each layer.
|
| 115 |
+
"""
|
| 116 |
+
kv_offset = 0
|
| 117 |
+
query_length = cache_position.shape[0]'''
|
| 118 |
+
cache_api_compat_code = ''' def get_query_offset(self, layer_idx: int) -> int:
|
| 119 |
+
return self.get_seq_length(layer_idx)
|
| 120 |
+
|
| 121 |
+
def get_mask_sizes(self, cache_position: torch.Tensor | int, layer_idx: int) -> tuple[int, int]:
|
| 122 |
+
"""
|
| 123 |
+
Return a tuple (kv_length, kv_offset) corresponding to the length and offset that will be returned for
|
| 124 |
+
the given layer at `layer_idx`.
|
| 125 |
+
The masks are then prepared according to the given lengths (kv_length, kv_offset) and patterns for each layer.
|
| 126 |
+
"""
|
| 127 |
+
kv_offset = 0
|
| 128 |
+
query_length = cache_position if isinstance(cache_position, int) else cache_position.shape[0]'''
|
| 129 |
|
| 130 |
replace_file(
|
| 131 |
f"{folder}/modeling_kimi_linear.py",
|
|
|
|
| 147 |
)""",
|
| 148 |
),
|
| 149 |
(causal_mask_code, causal_mask_compat_code),
|
| 150 |
+
(cache_api_code, cache_api_compat_code),
|
| 151 |
(
|
| 152 |
force_flash_code,
|
| 153 |
' config._attn_implementation = getattr(config, "_attn_implementation", "eager")',
|
|
|
|
| 183 |
else:
|
| 184 |
first_layer_past_key_value = past_key_values[0][0][:, :, :, 0]""",
|
| 185 |
),
|
| 186 |
+
# Transformers 5.x drops an all-ones attention mask and passes None; the
|
| 187 |
+
# multimodal merge and the cached generation branch still expect a concrete mask.
|
| 188 |
+
(
|
| 189 |
+
" # Get the target length\n"
|
| 190 |
+
" target_length = input_ids.shape[1]\n"
|
| 191 |
+
" past_length = first_layer_past_key_value.shape[-1]\n",
|
| 192 |
+
" # Get the target length\n"
|
| 193 |
+
" target_length = input_ids.shape[1]\n"
|
| 194 |
+
" past_length = first_layer_past_key_value.shape[-1]\n\n"
|
| 195 |
+
" if attention_mask is None:\n"
|
| 196 |
+
" attention_mask = torch.ones_like(input_ids)\n",
|
| 197 |
+
),
|
| 198 |
+
(
|
| 199 |
+
" _, embed_dim = image_features[0].shape\n"
|
| 200 |
+
" feature_lengths = [x.shape[0] for x in image_features]",
|
| 201 |
+
" if attention_mask is None:\n"
|
| 202 |
+
" attention_mask = torch.ones_like(input_ids)\n\n"
|
| 203 |
+
" _, embed_dim = image_features[0].shape\n"
|
| 204 |
+
" feature_lengths = [x.shape[0] for x in image_features]",
|
| 205 |
+
),
|
| 206 |
],
|
| 207 |
)
|
| 208 |
|
.meta/env.json
CHANGED
|
@@ -18,7 +18,28 @@
|
|
| 18 |
"torch": "2.13.0+cu126",
|
| 19 |
"transformers": "5.22.0.dev0"
|
| 20 |
},
|
| 21 |
-
"cuda": "12.6"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
}
|
| 23 |
}
|
| 24 |
}
|
|
|
|
| 18 |
"torch": "2.13.0+cu126",
|
| 19 |
"transformers": "5.22.0.dev0"
|
| 20 |
},
|
| 21 |
+
"cuda": "12.6",
|
| 22 |
+
"hardware": {
|
| 23 |
+
"gpu_model": "NVIDIA H20"
|
| 24 |
+
}
|
| 25 |
+
},
|
| 26 |
+
"transformers": {
|
| 27 |
+
"python": "3.12.13",
|
| 28 |
+
"packages": {
|
| 29 |
+
"einops": "0.9.0.dev0",
|
| 30 |
+
"fla-core": "0.5.2",
|
| 31 |
+
"huggingface_hub": "2.3.0",
|
| 32 |
+
"numpy": "2.5.2",
|
| 33 |
+
"pillow": "12.3.0",
|
| 34 |
+
"safetensors": "0.8.0",
|
| 35 |
+
"tokenizers": "0.23.3",
|
| 36 |
+
"torch": "2.13.0+cu126",
|
| 37 |
+
"transformers": "5.22.0.dev0"
|
| 38 |
+
},
|
| 39 |
+
"cuda": "12.6",
|
| 40 |
+
"hardware": {
|
| 41 |
+
"gpu_model": "NVIDIA H20"
|
| 42 |
+
}
|
| 43 |
}
|
| 44 |
}
|
| 45 |
}
|
README.md
CHANGED
|
@@ -35,7 +35,7 @@ K3.1-Pro ships no modeling code; the included pinned K3 `modeling_kimi_k3.py`/`m
|
|
| 35 |
|
| 36 |
### Example usage
|
| 37 |
|
| 38 |
-
#### Transformers (
|
| 39 |
|
| 40 |
Loads with the pinned K3 reference modeling on GPU (KDA delta attention uses Triton via fla-core). The K3.1 gated-DSA layers run their MLA attention core; the LSA indexer tensors are present but not consumed because released Transformers lacks the gated-DSA operator.
|
| 41 |
|
|
|
|
| 35 |
|
| 36 |
### Example usage
|
| 37 |
|
| 38 |
+
#### Transformers (✅ verified)
|
| 39 |
|
| 40 |
Loads with the pinned K3 reference modeling on GPU (KDA delta attention uses Triton via fla-core). The K3.1 gated-DSA layers run their MLA attention core; the LSA indexer tensors are present but not consumed because released Transformers lacks the gated-DSA operator.
|
| 41 |
|
modeling_kimi_k3.py
CHANGED
|
@@ -976,6 +976,9 @@ class KimiK3ForConditionalGeneration(KimiK3PreTrainedModel):
|
|
| 976 |
labels (:obj:`torch.Tensor` of shape :obj:`(batch_size, sequence_length)`, *optional*):
|
| 977 |
The labels.
|
| 978 |
"""
|
|
|
|
|
|
|
|
|
|
| 979 |
_, embed_dim = image_features[0].shape
|
| 980 |
feature_lengths = [x.shape[0] for x in image_features]
|
| 981 |
image_features = torch.cat(image_features, dim=0)
|
|
@@ -1187,6 +1190,9 @@ class KimiK3ForConditionalGeneration(KimiK3PreTrainedModel):
|
|
| 1187 |
target_length = input_ids.shape[1]
|
| 1188 |
past_length = first_layer_past_key_value.shape[-1]
|
| 1189 |
|
|
|
|
|
|
|
|
|
|
| 1190 |
extended_attention_mask = torch.ones(
|
| 1191 |
(attention_mask.shape[0], past_length),
|
| 1192 |
dtype=attention_mask.dtype,
|
|
|
|
| 976 |
labels (:obj:`torch.Tensor` of shape :obj:`(batch_size, sequence_length)`, *optional*):
|
| 977 |
The labels.
|
| 978 |
"""
|
| 979 |
+
if attention_mask is None:
|
| 980 |
+
attention_mask = torch.ones_like(input_ids)
|
| 981 |
+
|
| 982 |
_, embed_dim = image_features[0].shape
|
| 983 |
feature_lengths = [x.shape[0] for x in image_features]
|
| 984 |
image_features = torch.cat(image_features, dim=0)
|
|
|
|
| 1190 |
target_length = input_ids.shape[1]
|
| 1191 |
past_length = first_layer_past_key_value.shape[-1]
|
| 1192 |
|
| 1193 |
+
if attention_mask is None:
|
| 1194 |
+
attention_mask = torch.ones_like(input_ids)
|
| 1195 |
+
|
| 1196 |
extended_attention_mask = torch.ones(
|
| 1197 |
(attention_mask.shape[0], past_length),
|
| 1198 |
dtype=attention_mask.dtype,
|
modeling_kimi_linear.py
CHANGED
|
@@ -203,14 +203,17 @@ class KimiDynamicCache:
|
|
| 203 |
return 0
|
| 204 |
return self.key_cache[layer_idx].shape[-2]
|
| 205 |
|
| 206 |
-
def
|
|
|
|
|
|
|
|
|
|
| 207 |
"""
|
| 208 |
Return a tuple (kv_length, kv_offset) corresponding to the length and offset that will be returned for
|
| 209 |
the given layer at `layer_idx`.
|
| 210 |
The masks are then prepared according to the given lengths (kv_length, kv_offset) and patterns for each layer.
|
| 211 |
"""
|
| 212 |
kv_offset = 0
|
| 213 |
-
query_length = cache_position.shape[0]
|
| 214 |
past_seen_tokens = self.get_seq_length(layer_idx)
|
| 215 |
kv_length = query_length + past_seen_tokens
|
| 216 |
return kv_length, kv_offset
|
|
|
|
| 203 |
return 0
|
| 204 |
return self.key_cache[layer_idx].shape[-2]
|
| 205 |
|
| 206 |
+
def get_query_offset(self, layer_idx: int) -> int:
|
| 207 |
+
return self.get_seq_length(layer_idx)
|
| 208 |
+
|
| 209 |
+
def get_mask_sizes(self, cache_position: torch.Tensor | int, layer_idx: int) -> tuple[int, int]:
|
| 210 |
"""
|
| 211 |
Return a tuple (kv_length, kv_offset) corresponding to the length and offset that will be returned for
|
| 212 |
the given layer at `layer_idx`.
|
| 213 |
The masks are then prepared according to the given lengths (kv_length, kv_offset) and patterns for each layer.
|
| 214 |
"""
|
| 215 |
kv_offset = 0
|
| 216 |
+
query_length = cache_position if isinstance(cache_position, int) else cache_position.shape[0]
|
| 217 |
past_seen_tokens = self.get_seq_length(layer_idx)
|
| 218 |
kv_length = query_length + past_seen_tokens
|
| 219 |
return kv_length, kv_offset
|