llmcompressor.modeling.kimi_k3.modeling_kimi_k3
Classes:
-
KimiK3ForConditionalGeneration– -
MLP2–Args:
-
MoonViT3dPretrainedModel– -
MoonViTEncoderLayer– -
MoonVision3dPatchEmbed– -
Rope2DPosEmbRepeated–2D rotary position embedding with multi-resolution support.
Functions:
-
apply_rope–Args: (The leading dimensions of all inputs should be the same)
-
get_1d_sincos_pos_embed–t_size: int of the temporal size
-
get_1d_sincos_pos_embed_from_grid–From:
-
multihead_attention–Multi-head attention using flash attention 2.
KimiK3ForConditionalGeneration
Bases: KimiK3PreTrainedModel
Methods:
-
forward–Args:
Source code in src/llmcompressor/modeling/kimi_k3/modeling_kimi_k3.py
forward
forward(
input_ids: LongTensor | None = None,
pixel_values: FloatTensor
| list[FloatTensor]
| None = None,
grid_thws: Tensor | None = 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,
return_dict: bool | None = None,
) -> tuple | LlavaCausalLMOutputWithPast
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_k3.py
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 | |
MLP2
Bases: Module
Args: dims: [in_dim, hidden_dim, out_dim] bias: whether to use bias in linear layer.
Source code in src/llmcompressor/modeling/kimi_k3/modeling_kimi_k3.py
MoonViT3dPretrainedModel
Bases: PreTrainedModel
Methods:
-
forward–Args:
Source code in src/llmcompressor/modeling/kimi_k3/modeling_kimi_k3.py
forward
Args: pixel_values (torch.Tensor): The input pixel values. grid_thws (torch.Tensor): Temporal, height and width.
Returns: torch.Tensor: The output tokens.
Source code in src/llmcompressor/modeling/kimi_k3/modeling_kimi_k3.py
MoonViTEncoderLayer
MoonViTEncoderLayer(
num_heads: int,
hidden_dim: int,
mlp_dim: int,
qkv_hidden_size: int | None = None,
norm_type: str = "layernorm",
mlp_type: str = "mlp2",
*,
attn_implementation: str = "flash_attention_2",
activation=F.gelu,
attn_bias: bool = False,
linear_bias: bool = True,
use_deterministic_attn: bool = False,
)
Bases: Module
Methods:
-
attention_qkvpacked–Args:
Source code in src/llmcompressor/modeling/kimi_k3/modeling_kimi_k3.py
attention_qkvpacked
attention_qkvpacked(
x: Tensor,
cu_seqlens: Tensor,
max_seqlen: Tensor,
rope_freqs_cis: Tensor | None = None,
)
Args: x (torch.Tensor): (batch_size, seqlen, hidden_dim) cu_seqlens (torch.Tensor):
Source code in src/llmcompressor/modeling/kimi_k3/modeling_kimi_k3.py
MoonVision3dPatchEmbed
MoonVision3dPatchEmbed(
out_dim: int,
in_dim: int = 3,
patch_size: int | tuple[int, int] = (14, 14),
pos_emb_height: int = 14,
pos_emb_width: int = 14,
pos_emb_time: int = 4,
pos_emb_type: str = "divided_fixed",
patch_embed_proj_bias: bool = True,
pos_emb_interpolation_mode: str = "bicubic",
)
Bases: Module
Methods:
-
forward–Args:
Source code in src/llmcompressor/modeling/kimi_k3/modeling_kimi_k3.py
forward
Args: x (L, Channels): input tensor grid_hws (N, 3): temporal, height and width
Returns: (L, Cout) tensor
Source code in src/llmcompressor/modeling/kimi_k3/modeling_kimi_k3.py
Rope2DPosEmbRepeated
Bases: Module
2D rotary position embedding with multi-resolution support.
This class is intended to be used in the following way:
1. Before training, create an instance of Rope2DPosEmb. This instance will hold the precomputed cis.
2. Before each forward pass, call get_freqs_cis_by_* to get the freqs_cis tensor for this iteration.
3. During the forward pass, pass the freqs_cis tensor to each attention layer, and call apply just before each attention operation.
The rope is shared across all attention layers and all heads.
Refs: - RoFormer: https://arxiv.org/abs/2104.09864 - VisionLLaMA: https://arxiv.org/abs/2403.00522 - https://github.com/Meituan-AutoML/VisionLLaMA/blob/main/dit/models.py
Args: dim (int): usually the multi-head attention dimension, should be divisible by 4 (TODO: relax this constraint if needed) max_height (int): the maximum height of the 2D grid max_width (int): the maximum width of the 2D grid theta_base (float): the base of the theta device (str): the device to store the precomputed cis
Methods:
-
get_freqs_cis–Args:
Source code in src/llmcompressor/modeling/kimi_k3/modeling_kimi_k3.py
get_freqs_cis
Args: grid_thws (torch.Tensor): grid time, height and width
Returns: freqs_cis: tensor of shape (sum(t * height * width), dim//2)
Source code in src/llmcompressor/modeling/kimi_k3/modeling_kimi_k3.py
apply_rope
Args: (The leading dimensions of all inputs should be the same) xq: query, tensor of shape (..., num_heads, head_dim) xk: key, tensor of shape (..., num_heads, head_dim) freqs_cis: tensor of shape (..., head_dim/2), dtype=torch.complex64. It contains the precomputed cis(freqs) for each position in the 2D grid. Returns: xq_out, xk_out: tensors of shape (..., num_heads, head_dim)
Source code in src/llmcompressor/modeling/kimi_k3/modeling_kimi_k3.py
get_1d_sincos_pos_embed
t_size: int of the temporal size return: pos_embed: [t_size, embed_dim] or [1+t_size, embed_dim] (w/ or w/o cls_token)
Source code in src/llmcompressor/modeling/kimi_k3/modeling_kimi_k3.py
get_1d_sincos_pos_embed_from_grid
From: https://github.com/OpenGVLab/InternVideo/blob/421f6d2361fc8f61a3394244571f2601a4e99e29/InternVideo2/multi_modality/models/backbones/internvideo2/pos_embed.py#L86 embed_dim: output dimension for each position pos: a list of positions to be encoded: size (M,) out: (M, D)
Source code in src/llmcompressor/modeling/kimi_k3/modeling_kimi_k3.py
multihead_attention
multihead_attention(
q: Tensor,
k: Tensor,
v: Tensor,
q_cu_seqlens: Tensor | None = None,
k_cu_seqlens: Tensor | None = None,
max_seqlen_q: int | None = None,
max_seqlen_k: int | None = None,
deterministic: bool = False,
)
Multi-head attention using flash attention 2.
Args: q, k, v: tensor of shape (batch_size, seqlen, num_heads, head_dim), or (tot_seqlens, num_heads, head_dim) if packing. q_cu_seqlens (torch.Tensor): cumulative sequence lengths of q. The first element should be 0 and the last element should be q.shape[0]. k_cu_seqlens (torch.Tensor): cumulative sequence lengths of k. The first element should be 0 and the last element should be k.shape[0].
Returns: output: shape (batch_size, seqlen, dim) or (tot_seqlens, dim) if packing, where dim = num_heads * head_dim