mirror of
https://github.com/ggml-org/llama.cpp.git
synced 2026-10-02 19:07:25 -05:00
* Rebase GLM-Next support onto master, and migrate to llama-memory-hybrid-idx * Add initial MTP support * Merge branch optimizations. Reduce allocated compute buffer size, speed up long context decode, fla, and slight MTP improvements. * Review driven changes, remove env vars, protect tensors * Strip MTP for initial PR * Clean up after mtp strip * Clean up after mtp strip * Update speculative.cpp * Update llama-context.h * Clean up after mtp strip * Fix tokenizer ignore merges * Improve quantization protection selection * Refactor mhc helpers, graph base * Lint Fixes * Apply suggestions from code review Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@huggingface.co> * Skip glm5-next in model saver, fix CRLF * Skip glm5-next in sweep * Remove T4 fallback * Review cleanup * Review suggestions * Defer separate MTP gguf handling to MTP PR, drop filter * Repad n_head_kv * kpool init apply * Order by descending score * Drop guard * read kpool from hparams, clarify kpool cache flags, remove kpool_build_state(nullptr) * Add glm5-next support to model saver and add arch test fixture * Review cleanup * Kpool pooled caching clarify * Add multi stream support * Finish Rebase * Sparse FA fir DSA prefill * Const * Update llama-model.cpp to fix rebase error * gguf-py : merge tensor map entries for HC tensors * model : use build_gdn_l2_norm in GLM5_NEXT implementation * chore : remove trailing whitespace * model : use new OP precision setting API in GLM5_NEXT implementation * mtmd : use ggml_swiglu_clamp in GLM5V and apply the image token limit The two clamps around swiglu_split are what ggml_swiglu_clamp already does, so the clamp bounds collapse back to one value. GLM5V also never called set_limit_image_tokens(), so --image-max-tokens had no effect. Assisted-by: Claude Opus 5 (cherry picked from commit 46d18e12d422be4cc04a70e4a9a9e0168bb3d5b7) * llama : keep the GLM5-Next k-pool layout across ubatches The layout was rebuilt from a full cell scan on every ubatch. Pools are fixed by the positions relative to the sequence's first one, so the layout now lives on the memory and a ubatch only appends to it. A sequence edit no longer stales every pooled key either, only the ones at or after the edited position, which makes a tail seq_rm free. The pooling subgraph is built unconditionally so the graph shape no longer changes every kpool tokens, and the pool axis is folded into rows before soft_max, which otherwise exceeds the CUDA gridDim.y limit past n_kv 262144. Assisted-by: Claude Opus 5 (cherry picked from commit 5d1c40b93e17fddbf73b785efe43e0d02ccb3977) * model : write the GLM5-Next recurrent rollback checkpoints The conv state and the delta net state were only written to the live row, so a rollback restored whatever the checkpoint rows happened to hold. Take the same route as kimi-k3: build_recurrent_attn for the state, and write all K_rs conv groups. That also drops a state view that assumed contiguous rows. Enroll the arch in test-recurrent-state-rollback, which catches this under its garbage-filled cache pass. Assisted-by: Claude Opus 5 (cherry picked from commit 5ace37e86d5d448e83ef5dde5632c748185b18cd) * llama: fix PR #27773 test-save-load-state restore failure Clear the attention and indexer cache data after a failed hybrid state restore so restored NaNs cannot affect a later sequence. Assisted-by: Codex * llama: fix PR #27773 gpu-rocm graph reallocation Reserve the full GLM5-Next pool capacity and dirty pool count. The gpu-rocm Test step aborts when n_new grows while the graph node count stays fixed; CUDA, Vulkan, Metal, and WebGPU checks report the same error. Assisted-by: Codex * llama : fix GLM5-Next k-pool layout staleness after edits and shared teardown Two defects in the cross-ubatch k-pool layout added by the k-pool commit: 1. Wrong results. An edited sequence only rebuilt its pool layout when its cell count changed, so if the first ubatch after an edit added back exactly as many cells as were removed, the stale position-to-cell list survived. With a unified cache and more than one sequence, where another sequence takes the freed cells, the reused layout points at the wrong cells (CPU: large logit drift, CUDA: NaN). Rebuild whenever the sequence is stale, not only on a size mismatch. 2. Slowdown. "shared" mode was assumed to end only with an edit that forces a rebuild, but sharing also ends when the other sequence is removed. The survivor kept shared = true, pinning cache_safe off and re-pooling every pool on every ubatch (server trigger: n>1 completions with -kvu, via the seq_cp in copy_state_to). In seq_rm, if the layout has shared cells, stale every sequence so one rebuild re-derives sharing and cache_safe returns to 1. Assisted-by: Claude Opus 5 * llama : fix build_attn_mha stream stride for non-contiguous q build_attn_mha split the batch into streams with a stream stride of q->nb[3]/n_stream. That only equals one stream's span, (ne[2]/n_stream)*nb[2], when q is contiguous. GLM5-Next is nope-only, so it does not concat a rope part and passes the permuted q_absorbed straight in, where nb[3] != ne[2]*nb[2]; the stride was then n_head times too large and every stream s >= 1 read another head's queries. Split-KV (-np N without --kv-unified) multi-stream prefill was wrong for every stream past the first. Unified KV and decode were unaffected (n_stream == 1, and decode takes the gather path). Other MLA models concat rope so q is contiguous and the computed value is unchanged for them. Compute the stride from the token dimension, which is identical for a contiguous q. Assisted-by: Claude Opus 5 * llama : re-derive GLM5-Next k-pool sharing on state_read/state_drop The shared-cell teardown added to seq_rm (stale every sequence when the layout has shared cells, so a survivor does not keep shared = true and pin cache_safe off) was missing from the other paths that can free shared cells: state_read and state_drop staled only the one sequence. Apply the same re-derivation there and correct the comment that claimed sharing ends only via an edit or seq_rm. Assisted-by: Claude Opus 5 * quant : drop duplicate GLM5-Next hc_ filter The hc_ name filter was listed twice in the GLM5_NEXT protection block. Assisted-by: Claude Opus 5 * glm5-next: scope K-pool cache access to indexed operations * glm5-next: keep K-pool access in hybrid index memory * glm5-next: keep mHC graph builders model-local * glm5-next: mark only touched pools per ubatch --------- Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@huggingface.co> Co-authored-by: Stanisław Szymczyk <sszymczy@gmail.com> Co-authored-by: Piotr Wilkin <ilintar@gmail.com>
397 lines
18 KiB
Python
397 lines
18 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
|
|
from typing import Any, Callable, Iterable, TYPE_CHECKING
|
|
|
|
if TYPE_CHECKING:
|
|
from torch import Tensor
|
|
|
|
from .base import MmprojModel, ModelBase, gguf, logger
|
|
|
|
from .qwen import Qwen3Model, Qwen3MoeModel
|
|
from .qwenvl import Qwen25AudioModel
|
|
|
|
|
|
@ModelBase.register("Qwen3VLForConditionalGeneration", "Qwen3VLMoeForConditionalGeneration", "Qwen3_5ForConditionalGeneration", "Qwen3_5MoeForConditionalGeneration")
|
|
@ModelBase.example("Qwen/Qwen3-VL-4B-Instruct", "Qwen/Qwen3-VL-30B-A3B-Instruct", "Qwen/Qwen3.5-9B", "Qwen/Qwen3.5-35B-A3B")
|
|
class Qwen3VLVisionModel(MmprojModel):
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
if self.hparams_vision is None:
|
|
logger.info("No vision config found, skipping vision tensor processing")
|
|
return
|
|
|
|
# Compute image_size if not present
|
|
if "image_size" not in self.hparams_vision:
|
|
# For Qwen3VL/Qwen3VLMoe, compute from num_position_embeddings
|
|
num_pos = self.hparams_vision.get("num_position_embeddings", 2304)
|
|
patch_size = self.hparams_vision.get("patch_size", 16)
|
|
# num_position_embeddings = (image_size / patch_size) ** 2
|
|
# So image_size = sqrt(num_position_embeddings) * patch_size
|
|
image_size = int(num_pos**0.5 * patch_size)
|
|
self.hparams_vision["image_size"] = image_size
|
|
|
|
# Rename config values for compatibility
|
|
self.hparams_vision["num_attention_heads"] = self.hparams_vision.get("num_heads")
|
|
self.hparams_vision["num_hidden_layers"] = self.hparams_vision.get("depth")
|
|
|
|
self.is_deepstack_layers = [False] * int(self.hparams_vision["num_hidden_layers"] or 0)
|
|
for idx in self.hparams_vision.get("deepstack_visual_indexes", []):
|
|
self.is_deepstack_layers[idx] = True
|
|
|
|
def set_gguf_parameters(self):
|
|
super().set_gguf_parameters()
|
|
# in case mixed modalities, the arch will be handled by subclass
|
|
if not self.has_audio_encoder:
|
|
self.gguf_writer.add_clip_projector_type(gguf.VisionProjectorType.QWEN3VL)
|
|
self.gguf_writer.add_vision_use_gelu(True)
|
|
|
|
if self.hparams_vision is not None:
|
|
merge_size = self.hparams_vision.get("spatial_merge_size")
|
|
if merge_size is not None:
|
|
self.gguf_writer.add_vision_spatial_merge_size(int(merge_size))
|
|
|
|
# Use text config's rms_norm_eps for vision attention layernorm eps
|
|
rms_norm_eps = self.global_config.get("text_config", {}).get("rms_norm_eps", 1e-6)
|
|
self.gguf_writer.add_vision_attention_layernorm_eps(rms_norm_eps)
|
|
|
|
if self.is_deepstack_layers:
|
|
self.gguf_writer.add_vision_is_deepstack_layers(self.is_deepstack_layers)
|
|
|
|
@classmethod
|
|
def filter_tensors(cls, item: tuple[str, Callable[[], Tensor]]) -> tuple[str, Callable[[], Tensor]] | None:
|
|
name, gen = item
|
|
|
|
# Skip text model tensors
|
|
if name.startswith("lm_head."):
|
|
return None
|
|
|
|
# Skip MTP tensors
|
|
if name.startswith("mtp."):
|
|
return None
|
|
|
|
if name.startswith("model.visual."):
|
|
name = name.replace("model.visual.", "visual.", 1)
|
|
|
|
if not name.startswith("visual."):
|
|
return None
|
|
|
|
return super().filter_tensors((name, gen))
|
|
|
|
def modify_tensors(self, data_torch: Tensor, name: str, bid: int | None) -> Iterable[tuple[str, Tensor]]:
|
|
assert self.hparams_vision is not None
|
|
|
|
if name.startswith("visual.deepstack_merger_list."):
|
|
prefix, rest = name.split(".", maxsplit=3)[2:]
|
|
# prefix is the layer index, convert to absolute clip layer index!
|
|
idx = self.hparams_vision.get("deepstack_visual_indexes", [])[int(prefix)]
|
|
target = rest
|
|
|
|
tensor_type: gguf.MODEL_TENSOR
|
|
if target.startswith("norm."):
|
|
tensor_type = gguf.MODEL_TENSOR.V_DS_NORM
|
|
suffix = target.split(".", 1)[1]
|
|
elif target.startswith("linear_fc1."):
|
|
tensor_type = gguf.MODEL_TENSOR.V_DS_FC1
|
|
suffix = target.split(".", 1)[1]
|
|
elif target.startswith("linear_fc2."):
|
|
tensor_type = gguf.MODEL_TENSOR.V_DS_FC2
|
|
suffix = target.split(".", 1)[1]
|
|
else:
|
|
raise ValueError(f"Unexpected deepstack tensor: {name}")
|
|
|
|
new_name = self.format_tensor_name(tensor_type, idx, suffix=f".{suffix}")
|
|
yield from super().modify_tensors(data_torch, new_name, bid)
|
|
return
|
|
|
|
if name.startswith("visual.merger."):
|
|
suffix = name.split(".", 2)[2]
|
|
if suffix.startswith("linear_fc"):
|
|
fc_idx_str, tail = suffix.split(".", 1)
|
|
fc_num = int(fc_idx_str.replace("linear_fc", ""))
|
|
# Qwen3VL has linear_fc1 and linear_fc2
|
|
# Map to indices 0 and 2 (matching Qwen2VL which uses indices 0 and 2)
|
|
if fc_num == 1:
|
|
fc_idx = 0
|
|
elif fc_num == 2:
|
|
fc_idx = 2
|
|
else:
|
|
raise ValueError(f"unexpected fc index {fc_num} in {name}")
|
|
new_name = self.format_tensor_name(gguf.MODEL_TENSOR.V_MMPROJ, fc_idx, suffix=f".{tail}")
|
|
elif suffix.startswith("norm."):
|
|
new_name = self.format_tensor_name(gguf.MODEL_TENSOR.V_POST_NORM, suffix=f".{suffix.split('.', 1)[1]}")
|
|
else:
|
|
raise ValueError(f"Unexpected merger tensor: {name}")
|
|
yield (new_name, data_torch)
|
|
return
|
|
|
|
if name == "visual.patch_embed.proj.weight":
|
|
# split Conv3D into Conv2Ds along temporal dimension
|
|
c1, c2, kt, _, _ = data_torch.shape
|
|
del c1, c2
|
|
if kt != 2:
|
|
raise ValueError("Current implementation only supports temporal_patch_size of 2")
|
|
yield (gguf.TENSOR_NAMES[gguf.MODEL_TENSOR.V_ENC_EMBD_PATCH] + ".weight", data_torch[:, :, 0, ...])
|
|
yield (gguf.TENSOR_NAMES[gguf.MODEL_TENSOR.V_ENC_EMBD_PATCH] + ".weight.1", data_torch[:, :, 1, ...])
|
|
return
|
|
|
|
if name == "visual.patch_embed.proj.bias":
|
|
# Include the bias - it's used by the C++ code
|
|
yield (gguf.TENSOR_NAMES[gguf.MODEL_TENSOR.V_ENC_EMBD_PATCH] + ".bias", data_torch)
|
|
return
|
|
|
|
yield from MmprojModel.modify_tensors(self, data_torch, name, bid)
|
|
|
|
|
|
@ModelBase.register("Qwen3OmniMoeForConditionalGeneration")
|
|
@ModelBase.example("Qwen/Qwen3-Omni-30B-A3B-Instruct")
|
|
class Qwen3OmniMmprojModel(Qwen3VLVisionModel, Qwen25AudioModel):
|
|
has_audio_encoder = True
|
|
has_vision_encoder = True
|
|
|
|
def get_vision_config(self) -> dict[str, Any] | None:
|
|
if self.has_vision_encoder:
|
|
return self.global_config["thinker_config"].get("vision_config")
|
|
else:
|
|
return None
|
|
|
|
def get_audio_config(self) -> dict[str, Any] | None:
|
|
if self.has_audio_encoder:
|
|
return self.global_config["thinker_config"].get("audio_config")
|
|
else:
|
|
return None
|
|
|
|
def set_gguf_parameters(self):
|
|
if self.has_vision_encoder:
|
|
Qwen3VLVisionModel.set_gguf_parameters(self)
|
|
self.gguf_writer.add_clip_vision_projector_type(gguf.VisionProjectorType.QWEN3VL)
|
|
if self.has_audio_encoder:
|
|
Qwen25AudioModel.set_gguf_parameters(self)
|
|
self.gguf_writer.add_clip_audio_projector_type(gguf.VisionProjectorType.QWEN3A)
|
|
|
|
@classmethod
|
|
def filter_tensors(cls, item: tuple[str, Callable[[], Tensor]]) -> tuple[str, Callable[[], Tensor]] | None:
|
|
name, gen = item
|
|
|
|
# Skip text model tensors
|
|
if name.startswith("lm_head."):
|
|
return None
|
|
|
|
# Skip MTP tensors
|
|
if name.startswith("mtp."):
|
|
return None
|
|
|
|
if name.startswith("model.visual."):
|
|
name = name.replace("model.visual.", "visual.", 1)
|
|
|
|
if name.startswith("thinker.audio_tower."):
|
|
name = name.replace("thinker.audio_tower.", "audio_tower.", 1)
|
|
|
|
if "visual." not in name and "audio_tower." not in name:
|
|
return None
|
|
|
|
return MmprojModel.filter_tensors((name, gen))
|
|
|
|
def modify_tensors(self, data_torch: Tensor, name: str, bid: int | None) -> Iterable[tuple[str, Tensor]]:
|
|
if "visual." in name:
|
|
if not self.has_vision_encoder:
|
|
raise ValueError(f"Model does not have vision encoder, but found tensor {name}")
|
|
# need to transform vision tensor naming, so that modify_tensors() logic can be used correctly
|
|
name = name.replace("thinker.visual.", "model.visual.")
|
|
if ".merger_list." in name:
|
|
name = name.replace(".merger_list.", ".deepstack_merger_list.")
|
|
name = name.replace(".ln_q", ".norm")
|
|
name = name.replace(".mlp.0", ".linear_fc1")
|
|
name = name.replace(".mlp.2", ".linear_fc2")
|
|
elif ".merger." in name:
|
|
name = name.replace(".ln_q", ".norm")
|
|
name = name.replace(".mlp.0", ".linear_fc1")
|
|
name = name.replace(".mlp.2", ".linear_fc2")
|
|
yield from Qwen3VLVisionModel.modify_tensors(self, data_torch, name, bid)
|
|
elif "audio_tower." in name:
|
|
if not self.has_audio_encoder:
|
|
raise ValueError(f"Model does not have audio encoder, but found tensor {name}")
|
|
if "conv2d" in name and name.endswith(".bias"):
|
|
# transform conv2d bias [n_embd] --> [1, 1, n_embd]
|
|
data_torch = data_torch.unsqueeze(-1).unsqueeze(-1)
|
|
yield from Qwen25AudioModel.modify_tensors(self, data_torch, name, bid)
|
|
|
|
|
|
@ModelBase.register("Qwen3ASRForConditionalGeneration")
|
|
@ModelBase.example("Qwen/Qwen3-ASR-0.6B-hf")
|
|
class Qwen3ASRMmprojModel(Qwen3OmniMmprojModel):
|
|
has_audio_encoder = True
|
|
has_vision_encoder = False
|
|
|
|
|
|
@ModelBase.register("Glm4vForConditionalGeneration", "Glm4vMoeForConditionalGeneration", "GlmOcrForConditionalGeneration")
|
|
@ModelBase.example("zai-org/GLM-4.1V-9B-Thinking", "zai-org/GLM-4.5V")
|
|
class Glm4VVisionModel(Qwen3VLVisionModel):
|
|
projector_type = gguf.VisionProjectorType.GLM4V
|
|
|
|
def set_gguf_parameters(self):
|
|
MmprojModel.set_gguf_parameters(self) # skip Qwen3VLVisionModel parameters
|
|
assert self.hparams_vision is not None
|
|
self.gguf_writer.add_clip_projector_type(self.projector_type)
|
|
|
|
hidden_act = str(self.hparams_vision.get("hidden_act", "")).lower()
|
|
if hidden_act == "gelu":
|
|
self.gguf_writer.add_vision_use_gelu(True)
|
|
elif hidden_act == "silu":
|
|
self.gguf_writer.add_vision_use_silu(True)
|
|
|
|
rms_norm_eps = self.hparams_vision.get("rms_norm_eps", 1e-5)
|
|
self.gguf_writer.add_vision_attention_layernorm_eps(rms_norm_eps)
|
|
|
|
def modify_tensors(self, data_torch: Tensor, name: str, bid: int | None) -> Iterable[tuple[str, Tensor]]:
|
|
if name.startswith("visual.merger."):
|
|
yield from ModelBase.modify_tensors(self, data_torch, name, bid)
|
|
return
|
|
yield from super().modify_tensors(data_torch, name, bid)
|
|
|
|
|
|
@ModelBase.register("Glm5NextForConditionalGeneration")
|
|
@ModelBase.example("zai-org/GLM-5.3-Flash")
|
|
class Glm5NextVisionModel(Glm4VVisionModel):
|
|
# GLM-5.3-Flash vision tower. glm4v layout with per-head qk-norm, no post-conv norm and no learned position embeddings.
|
|
# Images are placed on a ceil aligned canvas with padding.
|
|
|
|
projector_type = gguf.VisionProjectorType.GLM5V
|
|
|
|
def set_gguf_parameters(self):
|
|
super().set_gguf_parameters()
|
|
assert self.hparams_vision is not None
|
|
self.gguf_writer.add_vision_spatial_merge_size(int(self.hparams_vision.get("spatial_merge_size", 2)))
|
|
if (limit := self.hparams_vision.get("swiglu_limit")) is not None:
|
|
self.gguf_writer.add_vision_swiglu_clamp(float(limit))
|
|
|
|
# image token budget from the processor, stored as single-frame pixel counts
|
|
pc = self.preprocessor_config
|
|
patch = int(pc.get("patch_size", 14))
|
|
merge = int(pc.get("merge_size", 2))
|
|
pixels_per_token = (patch * merge) ** 2
|
|
if (min_tok := pc.get("min_image_tokens")) is not None:
|
|
self.gguf_writer.add_vision_min_pixels(int(min_tok) * pixels_per_token)
|
|
if (max_tok := pc.get("max_image_tokens")) is not None:
|
|
self.gguf_writer.add_vision_max_pixels(int(max_tok) * pixels_per_token)
|
|
|
|
|
|
@ModelBase.register("Qwen3VLForConditionalGeneration")
|
|
@ModelBase.example("Qwen/Qwen3-VL-4B-Instruct")
|
|
class Qwen3VLTextModel(Qwen3Model):
|
|
model_arch = gguf.MODEL_ARCH.QWEN3VL
|
|
|
|
def set_gguf_parameters(self):
|
|
super().set_gguf_parameters()
|
|
if "thinker_config" in self.hparams:
|
|
vision_config = self.hparams["thinker_config"].get("vision_config", {})
|
|
else:
|
|
vision_config = self.hparams.get("vision_config", {})
|
|
deepstack_layer_num = len(vision_config.get("deepstack_visual_indexes", []))
|
|
self.gguf_writer.add_num_deepstack_layers(deepstack_layer_num)
|
|
|
|
@classmethod
|
|
def filter_tensors(cls, item: tuple[str, Callable[[], Tensor]]) -> tuple[str, Callable[[], Tensor]] | None:
|
|
name, gen = item
|
|
|
|
name = name.replace("thinker.", "")
|
|
|
|
return super().filter_tensors((name, gen))
|
|
|
|
|
|
@ModelBase.register("Qwen3VLMoeForConditionalGeneration")
|
|
@ModelBase.example("Qwen/Qwen3-VL-30B-A3B-Instruct")
|
|
class Qwen3VLMoeTextModel(Qwen3MoeModel):
|
|
model_arch = gguf.MODEL_ARCH.QWEN3VLMOE
|
|
|
|
def set_gguf_parameters(self):
|
|
super().set_gguf_parameters()
|
|
vision_config = self.hparams.get("vision_config", {})
|
|
deepstack_layer_num = len(vision_config.get("deepstack_visual_indexes", []))
|
|
self.gguf_writer.add_num_deepstack_layers(deepstack_layer_num)
|
|
|
|
@classmethod
|
|
def filter_tensors(cls, item: tuple[str, Callable[[], Tensor]]) -> tuple[str, Callable[[], Tensor]] | None:
|
|
name, gen = item
|
|
|
|
name = name.replace("thinker.", "")
|
|
|
|
return super().filter_tensors((name, gen))
|
|
|
|
def modify_tensors(self, data_torch: Tensor, name: str, bid: int | None) -> Iterable[tuple[str, Tensor]]:
|
|
# Qwen3VL has transposed packed tensors, so we treat it differently from general Qwen2MoE packed tensors
|
|
if name.endswith("mlp.experts.down_proj") or name.endswith("mlp.experts.down_proj.weight"):
|
|
mapped = f"{name}.weight" if not name.endswith(".weight") else name
|
|
permuted = data_torch.permute(0, 2, 1).contiguous()
|
|
yield from ModelBase.modify_tensors(self, permuted, mapped, bid)
|
|
return
|
|
|
|
if name.endswith("mlp.experts.gate_up_proj") or name.endswith("mlp.experts.gate_up_proj.weight"):
|
|
if data_torch.ndim < 3 or data_torch.shape[-1] % 2 != 0:
|
|
raise ValueError(f"Unexpected gate_up_proj shape for {name}: {tuple(data_torch.shape)}")
|
|
split_dim = data_torch.shape[-1] // 2
|
|
gate = data_torch[..., :split_dim].contiguous()
|
|
up = data_torch[..., split_dim:].contiguous()
|
|
# Input gate/up: (n_expert=128, n_embd=2048, n_ff_exp=768)
|
|
# Want GGML ne: {n_embd, n_ff_exp, n_expert} = {2048, 768, 128}
|
|
# Need PyTorch: (128, 768, 2048) [reversed of GGML]
|
|
# So: permute(0, 2, 1): (128, 2048, 768) -> (128, 768, 2048)
|
|
base_name = name.removesuffix(".weight")
|
|
base = base_name.rsplit('.', 1)[0]
|
|
mapped_gate = f"{base}.gate_proj.weight"
|
|
mapped_up = f"{base}.up_proj.weight"
|
|
perm_gate = gate.permute(0, 2, 1).contiguous()
|
|
perm_up = up.permute(0, 2, 1).contiguous()
|
|
yield from ModelBase.modify_tensors(self, perm_gate, mapped_gate, bid)
|
|
yield from ModelBase.modify_tensors(self, perm_up, mapped_up, bid)
|
|
return
|
|
|
|
yield from super().modify_tensors(data_torch, name, bid)
|
|
|
|
|
|
@ModelBase.register("Qwen3OmniMoeForConditionalGeneration")
|
|
@ModelBase.example("Qwen/Qwen3-Omni-30B-A3B-Instruct")
|
|
class Qwen3OmniMoeTextModel(Qwen3VLMoeTextModel):
|
|
model_arch = gguf.MODEL_ARCH.QWEN3VLMOE
|
|
|
|
def set_vocab(self):
|
|
super().set_vocab()
|
|
# correct BOS/EOS tokens
|
|
with open(self.dir_model / "tokenizer_config.json", "r", encoding="utf-8") as f:
|
|
tokenizer_config = json.load(f)
|
|
added_tokens = tokenizer_config.get("added_tokens_decoder", {})
|
|
for token_id, data in added_tokens.items():
|
|
if data.get("content") == "<|im_end|>":
|
|
self.gguf_writer.add_bos_token_id(int(token_id))
|
|
self.gguf_writer.add_eos_token_id(int(token_id))
|
|
break
|
|
|
|
def set_gguf_parameters(self):
|
|
super().set_gguf_parameters()
|
|
self.gguf_writer.add_num_deepstack_layers(0)
|
|
|
|
|
|
@ModelBase.register("Qwen3ASRForConditionalGeneration")
|
|
@ModelBase.example("Qwen/Qwen3-ASR-0.6B-hf")
|
|
class Qwen3ASRTextModel(Qwen3VLTextModel):
|
|
model_arch = gguf.MODEL_ARCH.QWEN3VL
|
|
|
|
def set_gguf_parameters(self):
|
|
super().set_gguf_parameters()
|
|
self.gguf_writer.add_num_deepstack_layers(0)
|
|
|
|
def set_vocab(self):
|
|
super().set_vocab()
|
|
# fix chat template, use correct chatml format
|
|
self.gguf_writer.add_chat_template("{% for message in messages %}{{'<|im_start|>' + message['role'] + '\\n' + message['content'] + '<|im_end|>' + '\\n'}}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>assistant\\n' }}{% endif %}")
|
|
# correct BOS/EOS tokens
|
|
with open(self.dir_model / "tokenizer_config.json", "r", encoding="utf-8") as f:
|
|
tokenizer_config = json.load(f)
|
|
added_tokens = tokenizer_config.get("added_tokens_decoder", {})
|
|
for token_id, data in added_tokens.items():
|
|
if data.get("content") == "<|im_end|>":
|
|
self.gguf_writer.add_bos_token_id(int(token_id))
|
|
self.gguf_writer.add_eos_token_id(int(token_id))
|
|
break
|