llmcompressor.modeling.kimi_k3.modeling_kimi_linear
Classes:
-
KimiDynamicCache–Dynamic cache for Kimi model.
-
KimiLinearForCausalLM– -
KimiLinearModel– -
KimiMLAAttention–Multi-Latent Attention adapted from deepseek-v3
-
KimiMoEGate–MoEGate adapted from Deepseek-V3.
-
KimiSparseMoeBlock–Adapted from Deepseek-V3's MOE implementation
-
SituAndMul–SituAndMul activation: beta * tanh(gate / beta) * sigmoid(gate) * up
Functions:
-
repeat_kv–Expand the key/value heads from
num_key_value_headstonum_attention_heads.
KimiDynamicCache
Dynamic cache for Kimi model. Inspired by Qwen3-Next
Methods:
-
get_mask_sizes–Return a tuple (kv_length, kv_offset) corresponding to the length and offset that will be returned for
-
get_seq_length–Returns the sequence length of the cached states. A layer index can be optionally passed.
-
reorder_cache–Reorders the cache for beam search, given the selected beam indices.
Attributes:
-
has_previous_state–We have a previous state if the last linear (conv) layer was already updated.
Source code in src/llmcompressor/modeling/kimi_k3/modeling_kimi_linear.py
has_previous_state
property
We have a previous state if the last linear (conv) layer was already updated.
get_mask_sizes
Return a tuple (kv_length, kv_offset) corresponding to the length and offset that will be returned for
the given layer at layer_idx.
The masks are then prepared according to the given lengths (kv_length, kv_offset) and patterns for each layer.
Source code in src/llmcompressor/modeling/kimi_k3/modeling_kimi_linear.py
get_seq_length
Returns the sequence length of the cached states. A layer index can be optionally passed.
Source code in src/llmcompressor/modeling/kimi_k3/modeling_kimi_linear.py
reorder_cache
Reorders the cache for beam search, given the selected beam indices.
Source code in src/llmcompressor/modeling/kimi_k3/modeling_kimi_linear.py
KimiLinearForCausalLM
Bases: KimiPreTrainedModel, GenerationMixin
Methods:
-
forward–Args:
Source code in src/llmcompressor/modeling/kimi_k3/modeling_kimi_linear.py
forward
forward(
input_ids: LongTensor = None,
attention_mask: Tensor | None = None,
position_ids: LongTensor | None = None,
past_key_values: list[FloatTensor] | None = None,
inputs_embeds: FloatTensor | None = None,
labels: LongTensor | None = None,
use_cache: bool | None = None,
output_attentions: bool | None = None,
output_hidden_states: bool | None = None,
generation_mode: bool | None = None,
return_dict: bool | None = None,
cache_position: LongTensor | None = None,
**kwargs: Unpack[TransformersKwargs],
) -> tuple | CausalLMOutputWithPast
Args:
labels (torch.LongTensor of shape (batch_size, sequence_length), optional):
Labels for computing the masked language modeling loss. Indices should either be in [0, ...,
config.vocab_size] or -100 (see input_ids docstring). Tokens with indices set to -100 are ignored
(masked), the loss is only computed for the tokens with labels in [0, ..., config.vocab_size].
Source code in src/llmcompressor/modeling/kimi_k3/modeling_kimi_linear.py
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 | |
KimiLinearModel
Bases: KimiPreTrainedModel
Source code in src/llmcompressor/modeling/kimi_k3/modeling_kimi_linear.py
KimiMLAAttention
Bases: Module
Multi-Latent Attention adapted from deepseek-v3
Source code in src/llmcompressor/modeling/kimi_k3/modeling_kimi_linear.py
KimiMoEGate
Bases: Module
MoEGate adapted from Deepseek-V3. Parameter correspondences: num_experts -> n_routed_experts num_experts_per_token -> num_experts_per_tok num_expert_group -> n_group moe_router_activation_func -> scoring_func
Source code in src/llmcompressor/modeling/kimi_k3/modeling_kimi_linear.py
KimiSparseMoeBlock
Bases: Module
Adapted from Deepseek-V3's MOE implementation The namings are consistent with Kimi's version.
Methods:
-
moe_train–Calibration-compatible MoE dispatch with gradient flow.
Source code in src/llmcompressor/modeling/kimi_k3/modeling_kimi_linear.py
moe_train
Calibration-compatible MoE dispatch with gradient flow.
Source code in src/llmcompressor/modeling/kimi_k3/modeling_kimi_linear.py
SituAndMul
Bases: Module
SituAndMul activation: beta * tanh(gate / beta) * sigmoid(gate) * up When linear_beta is set, up is also transformed by linear_beta * tanh(up / linear_beta).
Source code in src/llmcompressor/modeling/kimi_k3/modeling_kimi_linear.py
repeat_kv
Expand the key/value heads from num_key_value_heads to num_attention_heads.