Commit a38695013 for llama.cpp
commit a386950136a817a6e0368446922a48431920103c
Author: Ruben Ortlam <rortlam@redhat.com>
Date: Fri Nov 27 14:51:55 2026 +0100
ggml: support quant scales for fp8 and nvfp4 (#28898)
* ggml: add tensor scaling (per-tensor, per-channel, per-expert, per-expert-per-channel) support
* add meta backend scale split
* add optional activation scales to mul_mat and mul_mat_id tensors
* handle bitnet scales too, disable in backends
* Don't treat single expert as per-channel-per-expert
* Temp workaround for moe-cache with scaled data-types
* Also apply scales in CPU backend 's tiled mul_mat
* Handle strided views on scales
* Make *scale tensors use existing GGML_OP_MUL buffer probe
* Make scale tensors required for ggml_type with needs_scale=true
* Make needs_scale=true for FP8
* Use ggml_need_scale in code-base
* Transform companion FP8 scales
* Add comment explaining intended use of activation scales
* Fix pre-existing view-handling failure in meta backend
eae135bd4 exposed an issue with single-ton dimension ambiguitiy + views
in meta backend, see https://github.com/ggml-org/llama.cpp/actions/runs/39765669007/job/119780422102
* Enforce scale side-cars for scaled data-types during conversion
* tq quants also need scales
* llama: use map for scale tensors instead of manual handling
Assisted-by: Codex
* Fix conversion edge-case for FP8 block-scales
* Revert "tq quants also need scales"
This reverts commit 5920e55b0170bf376318935dd814a47823e5efb3.
* Disable dedicated kernels also for kleidiai in requant test
We want to test requant errors so we should be using the same kernels
to avoid a cofounder
* Add separate tests for scaled and unscaled DSV2
* Also remove IQ from needs_scale related tests/behvaior
* Wire activation scales and disallow broadcasting over K
* Adjust build_* in lfm3.cpp
* Avoid internal includes in test-llama-arch for moe scales
see https://github.com/ggml-org/llama.cpp/pull/27512#discussion_r4410518158
* djust build_* in limite.c
pp as well
* Adjust build_* for mistral 4
* More build_*_ffn changes
* fp8 : fix integration after rebase
* Another round of model scale map fixes
* Only read scales for valid experts
* handle input scales through cache similar to weight scales
* check supports_op in cache slots
* use is_scalar/is_vector functions
* fix Windows CI cleanup failure
* exclude BLOOM, JAIS and REFACT from sm_tensor due to Alibi not handled in TP correctly yet
* increase error threshold for hunyuan and maincoder context shifting
* adapt CUDA fusion code to scale change
* fix MoE cache too small error
* increase error threshold for hrm_text
* replace missing model matmul cases
* clean up unused CUDA code
* more missed matmuls
* handle per-tensor (scalar) scales in moe cache
* place scales with their weights
* increase error threshold for moe scales check
* restore CUDA cublaslt scale path
* convert model matmuls, handle weight views, add tests
* fix comments and error style
* cuda: use global scale functions
* fix kleidiai
* cuda: honour f32 prec in cublaslt call
* update Metal fusion count
* fix gemma4 scale issue
* don't remove trivial scale if required
* adapt new models changes
* adapt conversion qwen4exp changes and tests
* improve error message when scale unsupported
* address feedback
* adapt new models
* metal : apply the weight scale of MUL_MAT and MUL_MAT_ID
* ggml : avoid `ggml_type_traits` ABI change + TODOs
* metal : drop the comments of the weight scale epilogue
* undo moe-cache-specific changes
* tests : update the Metal fusion baseline
* handle scale in repack and kleidiai, fix scale buffer placement in loader
* llama : fix weight scale lookup, validate MUL_MAT_ID scales
- only look up a weight scale for .weight tensors (k_scale/v_scale
tensors were checked against the weight scale and failed to load)
- check MUL_MAT_ID weight scale shapes at load time instead of asserting
- llama-quantize: error out on nvfp4/f8_e4m3 targets (no scale computed)
- llama-quant: NULL -> nullptr
* clean up unnecessary tests
---------
Co-authored-by: Oliver Simons <osimons@nvidia.com>
Co-authored-by: Pascal <admin@serveurperso.com>
Co-authored-by: Georgi Gerganov <ggerganov@gmail.com>
diff --git a/conversion/base.py b/conversion/base.py
index f55d85600..bf7843294 100644
--- a/conversion/base.py
+++ b/conversion/base.py
@@ -333,13 +333,13 @@ class ModelBase:
return scale.numel() <= 1 and abs(float(scale.float().sum()) - 1.0) < 1e-6
def _write_scale_tensor(self, scale_name: str, scale: Tensor):
- if not self._scale_is_trivial(scale):
+ if not scale_name.endswith(".input_scale") or not self._scale_is_trivial(scale):
scale_f32 = scale.float().numpy().flatten()
logger.info(f" + {scale_name} (per-tensor scale, shape [{scale_f32.size}])")
self.gguf_writer.add_tensor(scale_name, scale_f32)
def _write_scales_tensor(self, scale_name: str, scales: list[float]):
- if not np.allclose(scales, 1.0, atol=1e-6):
+ if not scale_name.endswith(".input_scale") or not np.allclose(scales, 1.0, atol=1e-6):
scale_vals = np.array(scales, dtype=np.float32)
logger.info(f" + {scale_name} (per-expert scale, shape [{len(scales)}])")
self.gguf_writer.add_tensor(scale_name, scale_vals)
@@ -689,14 +689,34 @@ class ModelBase:
new_name = self.tensor_map.get_name(name, try_suffixes=(".weight",))
return (new_name,) if new_name is not None else ()
+ def _transform_fp8_scale(self, name: str, scale: Tensor) -> tuple[Tensor, ...] | None:
+ # Return scales in mapped weight order, or None to dequantize.
+ # A new weight transform must also provide scale handling.
+ if type(self).modify_tensors is ModelBase.modify_tensors and len(self._map_fp8_weight_names(name)) == 1:
+ return (scale,)
+ return None
+
def _prepare_fp8_e4m3_tensors(self):
if not self._preserve_fp8:
return
+ quant_config = self.hparams.get("quantization_config") or {}
+ fp8_configs = [
+ group["weights"] for group in (quant_config.get("config_groups") or {}).values()
+ if isinstance(group, dict) and group.get("weights", {}).get("type") == "float"
+ and group.get("weights", {}).get("num_bits") == 8
+ ]
+ unsupported_layout = quant_config.get("weight_block_size") is not None or any(
+ config.get("strategy") not in (None, "tensor", "channel")
+ or config.get("block_structure") is not None or config.get("group_size") is not None
+ for config in fp8_configs
+ )
+
scale_tensors: dict[str, list[tuple[int, float]] | np.ndarray] = {}
input_scale_tensors: dict[str, list[tuple[int, float]] | np.ndarray] = {}
consumed: list[str] = []
candidates: dict[str, tuple[str, Tensor, tuple[str, ...], torch.Size]] = {}
+ channel_scales: dict[str, tuple[Tensor, ...]] = {}
weight_dtypes: dict[str, torch.dtype] = {}
scaled_weights: set[str] = set()
expert_pattern = re.compile(r"(\.[^.]*experts)\.(\d+)\.")
@@ -729,8 +749,19 @@ class ModelBase:
continue
scale = self.model_tensors[scale_name]()
- if prod(scale.shape) != 1:
- continue
+ # Accept per-tensor or per-output-channel scale layouts.
+ scale_size = prod(scale.shape)
+ is_channel_scale = 1 <= scale.ndim <= weight.ndim and scale.shape[0] == weight.shape[0] and all(size == 1 for size in scale.shape[1:])
+ if unsupported_layout or (scale_size != 1 and not is_channel_scale):
+ if quant_config.get("quant_method") in ("modelopt", "modelopt_mixed") and scale_name.endswith("_scale_inv"):
+ continue
+ if self._is_nvfp4_compressed_tensors(quant_config.get("quant_method"), quant_config.get("format"), quant_config.get("config_groups") or {}):
+ continue
+ raise ValueError(
+ f"FP8 weight {weight_name!r} has an unsupported block/group scale "
+ f"(scale shape {list(scale.shape)}, weight shape {list(weight.shape)}); "
+ f"only per-tensor or per-output-channel scales are supported. "
+ f"Re-run with --fp8-as-q8 to dequantize instead.")
merged_name = expert_pattern.sub(r"\1.", weight_name)
new_names = self._map_fp8_weight_names(merged_name)
@@ -743,6 +774,12 @@ class ModelBase:
if any(new_name.endswith("_norm.weight") or self.tensor_force_quant(weight_name, new_name, bid, weight.ndim) != gguf.GGMLQuantizationType.F8_E4M3 for new_name in new_names):
self._fp8_e4m3_preserved.discard(weight_name)
continue
+ if scale_size != 1:
+ transformed = self._transform_fp8_scale(weight_name, scale.reshape(-1, 1)) if weight.ndim == 2 else None
+ if transformed is None or len(transformed) != len(new_names):
+ self._fp8_e4m3_preserved.discard(weight_name)
+ continue
+ channel_scales[weight_name] = transformed
candidates[weight_name] = (scale_name, scale, new_names, weight.shape)
expert_groups: dict[str, list[str]] = {}
@@ -758,8 +795,13 @@ class ModelBase:
continue
if dtypes == {torch.float8_e4m3fn} and not any(name in scaled_weights for name in names):
continue
+ scale_sizes: dict[str, set[int]] = {}
+ for name in names:
+ if name in candidates:
+ proj = name.rsplit(".", 2)[1]
+ scale_sizes.setdefault(proj, set()).add(prod(candidates[name][1].shape))
# The converter merges expert projections together.
- if not all(name in candidates for name in names):
+ if not all(name in candidates for name in names) or any(len(sizes) > 1 for sizes in scale_sizes.values()):
for name in names:
candidates.pop(name, None)
self._fp8_e4m3_preserved.discard(name)
@@ -769,6 +811,7 @@ class ModelBase:
for weight_name, (scale_name, scale, new_names, shape) in candidates.items():
scale = LazyTorchTensor.to_eager(scale).float().flatten()
+ output_scales = tuple(LazyTorchTensor.to_eager(s).float().flatten() for s in channel_scales.get(weight_name, (scale,) * len(new_names)))
weight_prefix = weight_name.removesuffix(".weight")
# Transformers fine-grained FP8 uses activation_scale while ModelOpt uses input_scale.
@@ -792,25 +835,25 @@ class ModelBase:
expert_match = expert_pattern.search(weight_name)
if expert_match:
expert_id = int(expert_match.group(2))
- for new_name in new_names:
+ for new_name, target_scale in zip(new_names, output_scales):
target_name = new_name.replace(".weight", ".scale")
entries = scale_tensors.setdefault(target_name, [])
assert isinstance(entries, list)
- cast(list[tuple[int, float]], entries).append((expert_id, float(scale[0])))
+ cast(list, entries).append((expert_id, target_scale.numpy().astype(np.float32)))
if input_scale is not None:
target_name = new_name.replace(".weight", ".input_scale")
entries = input_scale_tensors.setdefault(target_name, [])
assert isinstance(entries, list)
cast(list[tuple[int, float]], entries).append((expert_id, float(input_scale[0])))
else:
- for new_name in new_names:
+ for new_name, target_scale in zip(new_names, output_scales):
mapped = self.tensor_map.get_type_and_name(new_name, try_suffixes=(".weight",)) if len(shape) == 3 else None
is_packed_expert = mapped is not None and mapped[0] in (
gguf.MODEL_TENSOR.FFN_GATE_EXP, gguf.MODEL_TENSOR.FFN_UP_EXP, gguf.MODEL_TENSOR.FFN_DOWN_EXP,
)
# Packed expert weights need one scale value per expert.
n_scales = shape[0] if is_packed_expert else 1
- scale_tensors[new_name.replace(".weight", ".scale")] = np.repeat(scale.numpy(), n_scales)
+ scale_tensors[new_name.replace(".weight", ".scale")] = np.repeat(target_scale.numpy(), n_scales)
if input_scale is not None:
input_scale_tensors[new_name.replace(".weight", ".input_scale")] = np.repeat(input_scale.numpy(), n_scales)
@@ -820,11 +863,15 @@ class ModelBase:
for name, values in chain(scale_tensors.items(), input_scale_tensors.items()):
if isinstance(values, list):
values.sort(key=lambda item: item[0])
- scale = np.array([item[1] for item in values], dtype=np.float32)
+ arrays = [np.asarray(item[1], dtype=np.float32).reshape(-1) for item in values]
+ if all(a.size == 1 for a in arrays):
+ scale = np.array([a[0] for a in arrays], dtype=np.float32)
+ else:
+ scale = np.stack(arrays, axis=0)
else:
scale = values.astype(np.float32)
- if not np.allclose(scale, 1.0, atol=1e-6):
- logger.info(f" + {name} (FP8 scale, shape [{scale.size}])")
+ if not name.endswith(".input_scale") or not np.allclose(scale, 1.0, atol=1e-6):
+ logger.info(f" + {name} (FP8 scale, shape {list(scale.shape)})")
self.gguf_writer.add_tensor(name, scale)
@classmethod
@@ -1359,6 +1406,8 @@ class ModelBase:
scale2_name = name.replace(".weight", ".weight_scale_2")
input_scale_name = name.replace(".weight", ".input_scale")
if scale_name not in self.model_tensors:
+ if self.model_tensors[name]().dtype == torch.uint8:
+ raise ValueError(f"Cannot convert NVFP4 weight {name!r}: source block scale {scale_name!r} is missing")
continue
weight = self.model_tensors[name]()
scale = self.model_tensors[scale_name]()
@@ -1369,9 +1418,11 @@ class ModelBase:
if weight.ndim != 2 or scale.shape[0] != weight.shape[0] or scale.shape[1] * 8 != weight.shape[1]:
raise ValueError(f"NVFP4 weight {name!r} has incompatible shapes: weight {list(weight.shape)}, scale {list(scale.shape)}")
+ if scale2_name not in self.model_tensors:
+ raise ValueError(f"Cannot convert NVFP4 weight {name!r}: source weight scale is missing ({scale2_name!r})")
weight = LazyTorchTensor.to_eager(weight)
scale = LazyTorchTensor.to_eager(scale)
- scale2 = LazyTorchTensor.to_eager(self.model_tensors.get(scale2_name, lambda: torch.tensor(1.0))())
+ scale2 = LazyTorchTensor.to_eager(self.model_tensors[scale2_name]())
input_scale = LazyTorchTensor.to_eager(self.model_tensors.get(input_scale_name, lambda: torch.tensor(1.0))())
# Mark tensors for removal from model_tensors (already written to gguf)
@@ -1450,7 +1501,19 @@ class ModelBase:
self._has_ple |= is_ple
return is_ple if self.ple_only else not is_ple
+ def _has_fp8_source_scale(self, name: str) -> bool:
+ scale_names = (name + "_scale", name + "_scale_inv", name.removesuffix("weight") + "qscale_weight")
+ return any(scale_name in self.model_tensors for scale_name in scale_names)
+
def prepare_tensors(self):
+ for name, load in self.model_tensors.items():
+ if not name.endswith(".weight"):
+ continue
+ if load().dtype not in (torch.float8_e4m3fn, torch.float8_e5m2):
+ continue
+ if not self._has_fp8_source_scale(name):
+ raise ValueError(f"Cannot convert FP8 weight {name!r}: source weight scale is missing")
+
# detect NVFP4 quantization (ModelOpt and Compressed-tensors formats)
quantization_config = self.hparams.get("quantization_config") or {}
quant_algo = quantization_config.get("quant_algo")
@@ -1666,6 +1729,18 @@ class ModelBase:
if any(qkv_buffers):
raise ValueError("QKV fusion did not consume all buffered tensors")
+ tensor_names = {name for shard in self.gguf_writer.tensors for name in shard}
+ for shard in self.gguf_writer.tensors:
+ for name, tensor in shard.items():
+ # a PLE-only file holds just the table, its scale stays in the backbone
+ if self.ple_only and self._include_ple_tensor(name):
+ continue
+ # Keep in sync with needs_scale in ggml.c.
+ if tensor.dtype in (gguf.GGMLQuantizationType.F8_E4M3, gguf.GGMLQuantizationType.NVFP4):
+ scale_name = name.removesuffix(".weight") + ".scale"
+ if scale_name not in tensor_names:
+ raise ValueError(f"Cannot export {name!r} as {tensor.dtype.name}: required scale tensor {scale_name!r} is missing (implicit unit scales are not supported)")
+
def set_type(self):
self.gguf_writer.add_type(gguf.GGUFType.MODEL)
diff --git a/conversion/llama.py b/conversion/llama.py
index b04f54d41..6cb859cb9 100644
--- a/conversion/llama.py
+++ b/conversion/llama.py
@@ -181,6 +181,18 @@ class LlamaModel(TextModel):
.swapaxes(1, 2)
.reshape(weights.shape))
+ def _transform_fp8_scale(self, name: str, scale: Tensor) -> tuple[Tensor, ...] | None:
+ if type(self).modify_tensors is not LlamaModel.modify_tensors:
+ return None
+ if self.undo_permute:
+ n_head = self.find_hparam(["n_heads", "num_attention_heads"])
+ n_kv_head = self.find_hparam(["n_kv_heads", "num_key_value_heads"])
+ if name.endswith("q_proj.weight"):
+ scale = self.permute(scale, n_head, n_head)
+ elif name.endswith("k_proj.weight"):
+ scale = self.permute(scale, n_head, n_kv_head)
+ return (scale,)
+
def _repack_nvfp4(self, name: str, weight: Tensor, scale: Tensor, scale2: Tensor, input_scale: Tensor):
# Mirror the BF16 Q/K RoPE permutation site in modify_tensors; the NVFP4 path bypasses it.
if self.undo_permute:
diff --git a/conversion/qwen.py b/conversion/qwen.py
index 836d1f3dc..02e4cb3ed 100644
--- a/conversion/qwen.py
+++ b/conversion/qwen.py
@@ -58,6 +58,11 @@ class QwenModel(TextModel):
class Qwen2Model(TextModel):
model_arch = gguf.MODEL_ARCH.QWEN2
+ def _transform_fp8_scale(self, name: str, scale: Tensor) -> tuple[Tensor, ...] | None:
+ if type(self).modify_tensors is Qwen2Model.modify_tensors and len(self._map_fp8_weight_names(name)) == 1:
+ return (scale,)
+ return None
+
def set_vocab(self):
# HuggingFaceBio/Carbon-14B: Qwen3 with the HybridDNATokenizer (Qwen3 BPE plus DNA 6-mers)
if self._is_hybriddna():
@@ -92,6 +97,11 @@ class Qwen2MoeModel(TextModel):
)
return super()._map_fp8_weight_names(name)
+ def _transform_fp8_scale(self, name: str, scale: Tensor) -> tuple[Tensor, ...] | None:
+ if type(self).modify_tensors is Qwen2MoeModel.modify_tensors and len(self._map_fp8_weight_names(name)) == 1:
+ return (scale,)
+ return None
+
def set_gguf_parameters(self):
super().set_gguf_parameters()
if (moe_intermediate_size := self.hparams.get("moe_intermediate_size")) is not None:
@@ -395,13 +405,50 @@ class Qwen3NextModel(_QwenMtpMixin, Qwen2MoeModel):
def _map_fp8_weight_names(self, name: str) -> tuple[str, ...]:
if name.endswith(".linear_attn.in_proj_qkvz.weight"):
bid = next(int(part) for part in self.map_tensor_name(name).split(".") if part.isdecimal())
- # Both split projections keep the original per-tensor scales.
return (
self.format_tensor_name(gguf.MODEL_TENSOR.ATTN_QKV, bid),
self.format_tensor_name(gguf.MODEL_TENSOR.ATTN_GATE, bid),
)
return super()._map_fp8_weight_names(name)
+ def _transform_fp8_scale(self, name: str, scale: Tensor) -> tuple[Tensor, ...] | None:
+ if type(self).modify_tensors is not Qwen3NextModel.modify_tensors:
+ return None
+ if name.endswith(".linear_attn.in_proj_qkvz.weight"):
+ return self._split_qkvz(scale)
+ if len(self._map_fp8_weight_names(name)) == 1:
+ return (scale,)
+ return None
+
+ def _split_qkvz(self, data_torch: Tensor) -> tuple[Tensor, Tensor]:
+ # original order: [q, k, v, z] * head_count
+ # corrected order: [q * head_count, k * head_count, v * head_count, z * head_count]
+ head_k_dim = self.hparams["linear_key_head_dim"]
+ head_v_dim = self.hparams["linear_value_head_dim"]
+ num_v_heads = self.hparams["linear_num_value_heads"]
+ num_k_heads = self.hparams["linear_num_key_heads"]
+ hidden_size = data_torch.shape[1]
+ split_arg_list_qkvz = [
+ head_k_dim, # q partition
+ head_k_dim, # k partition
+ (num_v_heads // num_k_heads * head_v_dim), # v partition
+ (num_v_heads // num_k_heads * head_v_dim), # z partition
+ ]
+ # view as (n_embd, head_count, [q+k+v+z])
+ data_torch = data_torch.permute(1, 0).contiguous()
+ data_torch = data_torch.view(-1, num_k_heads, sum(split_arg_list_qkvz))
+ # split into q, k, v, z
+ q, k, v, z = torch.split(data_torch, split_arg_list_qkvz, dim=-1)
+ # flatten dim + head_count
+ q = q.contiguous().view(hidden_size, -1)
+ k = k.contiguous().view(hidden_size, -1)
+ v = v.contiguous().view(hidden_size, -1)
+ z = z.contiguous().view(hidden_size, -1)
+ # stack back
+ qkv = torch.cat([q, k, v], dim=-1).permute(1, 0).contiguous()
+ z = z.permute(1, 0).contiguous()
+ return qkv, z
+
def set_gguf_parameters(self):
super().set_gguf_parameters()
self.gguf_writer.add_ssm_conv_kernel(self.hparams["linear_conv_kernel_dim"])
@@ -432,32 +479,7 @@ class Qwen3NextModel(_QwenMtpMixin, Qwen2MoeModel):
data_torch = data_torch + 1
if "in_proj_qkvz.weight" in name:
- # original order: [q, k, v, z] * head_count
- # corrected order: [q * head_count, k * head_count, v * head_count, z * head_count]
- head_k_dim = self.hparams["linear_key_head_dim"]
- head_v_dim = self.hparams["linear_value_head_dim"]
- num_v_heads = self.hparams["linear_num_value_heads"]
- num_k_heads = self.hparams["linear_num_key_heads"]
- hidden_size = self.hparams["hidden_size"]
- split_arg_list_qkvz = [
- head_k_dim, # q partition
- head_k_dim, # k partition
- (num_v_heads // num_k_heads * head_v_dim), # v partition
- (num_v_heads // num_k_heads * head_v_dim), # z partition
- ]
- # view as (n_embd, head_count, [q+k+v+z])
- data_torch = data_torch.permute(1, 0).contiguous()
- data_torch = data_torch.view(-1, num_k_heads, sum(split_arg_list_qkvz))
- # split into q, k, v, z
- q, k, v, z = torch.split(data_torch, split_arg_list_qkvz, dim=-1)
- # flatten dim + head_count
- q = q.contiguous().view(hidden_size, -1)
- k = k.contiguous().view(hidden_size, -1)
- v = v.contiguous().view(hidden_size, -1)
- z = z.contiguous().view(hidden_size, -1)
- # stack back
- qkv = torch.cat([q, k, v], dim=-1).permute(1, 0).contiguous()
- z = z.permute(1, 0).contiguous()
+ qkv, z = self._split_qkvz(data_torch)
yield (self.format_tensor_name(gguf.MODEL_TENSOR.ATTN_QKV, bid, ".weight"), qkv)
yield (self.format_tensor_name(gguf.MODEL_TENSOR.ATTN_GATE, bid, ".weight"), z)
else:
diff --git a/conversion/qwen4exp.py b/conversion/qwen4exp.py
index fe49cc03d..8eac47275 100644
--- a/conversion/qwen4exp.py
+++ b/conversion/qwen4exp.py
@@ -54,6 +54,12 @@ class Qwen4ExpTextModel(_Qwen35MRopeMixin, _LinearAttentionVReorderBase):
self._ple_scale = float(scale[0])
super().dequant_model()
+ def _has_fp8_source_scale(self, name: str) -> bool:
+ if ".ngram_embedding.shard_" in name:
+ prefix = name.split(".ngram_embedding.shard_")[0]
+ return prefix + ".ngram_embedding.weight_scale" in self.model_tensors or self._ple_scale is not None
+ return super()._has_fp8_source_scale(name)
+
@classmethod
def filter_tensors(cls, item):
name, gen = item
diff --git a/ggml/include/ggml.h b/ggml/include/ggml.h
index 0007fa18f..4666b82d9 100644
--- a/ggml/include/ggml.h
+++ b/ggml/include/ggml.h
@@ -607,6 +607,7 @@ extern "C" {
GGML_API size_t ggml_element_size(const struct ggml_tensor * tensor);
GGML_API bool ggml_is_quantized(enum ggml_type type);
+ GGML_API bool ggml_needs_scale(enum ggml_type type);
// TODO: temporary until model loading of ggml examples is refactored
GGML_API enum ggml_type ggml_ftype_to_ggml_type(enum ggml_ftype ftype);
@@ -1322,6 +1323,16 @@ extern "C" {
struct ggml_tensor * a,
struct ggml_tensor * b);
+ // scale_weight is F32, either a scalar or one value per output channel.
+ // scale_activations is an optional F32 scalar for activation quantization.
+ // Divide activations by the scale before quantization, then multiply the matmul result by the scale.
+ GGML_API struct ggml_tensor * ggml_mul_mat_ext(
+ struct ggml_context * ctx,
+ struct ggml_tensor * a,
+ struct ggml_tensor * b,
+ struct ggml_tensor * scale_weight,
+ struct ggml_tensor * scale_activations);
+
// change the hint of a matrix multiplication
GGML_API void ggml_mul_mat_set_hint(
struct ggml_tensor * a,
@@ -1335,6 +1346,16 @@ extern "C" {
struct ggml_tensor * b,
struct ggml_tensor * ids);
+ // scale_weight: [1], [n_as] or [n_out, n_as], selected by ids
+ // scale_activations: [1] or [n_as], selected by ids
+ GGML_API struct ggml_tensor * ggml_mul_mat_id_ext(
+ struct ggml_context * ctx,
+ struct ggml_tensor * as,
+ struct ggml_tensor * b,
+ struct ggml_tensor * ids,
+ struct ggml_tensor * scale_weight,
+ struct ggml_tensor * scale_activations);
+
// A: m columns, n rows,
// B: p columns, n rows,
// result is m columns, p rows
@@ -2813,12 +2834,14 @@ extern "C" {
typedef void (*ggml_to_float_t) (const void * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k);
typedef void (*ggml_from_float_t)(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k);
+ // TODO: deprecate struct definition from the public API, replace with functions [TAG_GGML_AVOID_PUBLIC_STRUCTS]
struct ggml_type_traits {
const char * type_name;
int64_t blck_size;
int64_t blck_size_interleave; // interleave elements in blocks
size_t type_size;
- bool is_quantized;
+ bool is_quantized; // whether it's a block-quantized data-type
+ //bool needs_scale; // add this flag in the next update [TAG_GGML_VERSION_MAJOR_UPDATE]
ggml_to_float_t to_float;
ggml_from_float_t from_float_ref;
};
@@ -2840,6 +2863,7 @@ extern "C" {
// threadpool params
// Use ggml_threadpool_params_default() or ggml_threadpool_params_init() to populate the defaults
+ // TODO: deprecate struct definition from the public API, replace with functions [TAG_GGML_AVOID_PUBLIC_STRUCTS]
struct ggml_threadpool_params {
bool cpumask[GGML_MAX_N_THREADS]; // mask of cpu cores (all-zeros means use default affinity settings)
int n_threads; // number of threads
diff --git a/ggml/src/ggml-backend-meta.cpp b/ggml/src/ggml-backend-meta.cpp
index c442f603f..c31fea876 100644
--- a/ggml/src/ggml-backend-meta.cpp
+++ b/ggml/src/ggml-backend-meta.cpp
@@ -704,11 +704,9 @@ static struct ggml_backend_meta_split_state ggml_backend_meta_get_split_state(
};
auto handle_view = [&](const std::vector<ggml_backend_meta_split_state> & src_ss) -> ggml_backend_meta_split_state {
- if (ggml_is_contiguous(tensor) && ggml_is_contiguous(tensor->src[0])) {
- return handle_reshape(src_ss);
- }
const int axis = src_ss[0].axis;
{
+ // Keep the split axis when strides match; reshape is ambiguous across singleton dimensions.
bool all_strides_the_same = true;
for (int dim = 0; dim < GGML_MAX_DIMS; dim++) {
if (tensor->ne[dim] == 1 && tensor->src[0]->ne[dim] == 1) {
@@ -723,6 +721,9 @@ static struct ggml_backend_meta_split_state ggml_backend_meta_get_split_state(
return src_ss[0];
}
}
+ if (ggml_is_contiguous(tensor) && ggml_is_contiguous(tensor->src[0])) {
+ return handle_reshape(src_ss);
+ }
if (!ggml_is_permuted(tensor) && !ggml_is_permuted(tensor->src[0]) && axis >= 0 && axis < GGML_MAX_DIMS-1) {
for (int dim = 0; dim < GGML_MAX_DIMS-1; dim++) {
if (tensor->nb[dim+1] == tensor->src[0]->nb[axis+1]) {
diff --git a/ggml/src/ggml-blas/ggml-blas.cpp b/ggml/src/ggml-blas/ggml-blas.cpp
index 285c96848..140b86d81 100644
--- a/ggml/src/ggml-blas/ggml-blas.cpp
+++ b/ggml/src/ggml-blas/ggml-blas.cpp
@@ -398,6 +398,10 @@ static bool ggml_backend_blas_device_supports_op(ggml_backend_dev_t dev, const s
const struct ggml_tensor * src0 = op->src[0];
const struct ggml_tensor * src1 = op->src[1];
+ if (ggml_get_src_scale(op, 0)) {
+ return false;
+ }
+
switch (op->op) {
case GGML_OP_NONE:
case GGML_OP_RESHAPE:
diff --git a/ggml/src/ggml-cann/ggml-cann.cpp b/ggml/src/ggml-cann/ggml-cann.cpp
index d42f19b0a..d0e220c73 100644
--- a/ggml/src/ggml-cann/ggml-cann.cpp
+++ b/ggml/src/ggml-cann/ggml-cann.cpp
@@ -2405,6 +2405,10 @@ static enum ggml_status ggml_backend_cann_graph_compute(ggml_backend_t backend,
* otherwise false.
*/
static bool ggml_backend_cann_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
+ if (ggml_get_src_scale(op, 0)) {
+ return false;
+ }
+
switch (op->op) {
case GGML_OP_UNARY:
switch (ggml_get_unary_op(op)) {
diff --git a/ggml/src/ggml-cpu/ggml-cpu.c b/ggml/src/ggml-cpu/ggml-cpu.c
index b27752714..39920c77b 100644
--- a/ggml/src/ggml-cpu/ggml-cpu.c
+++ b/ggml/src/ggml-cpu/ggml-cpu.c
@@ -1306,6 +1306,38 @@ static void ggml_cpu_bf16_to_vec_dot_type(const ggml_bf16_t * x, void * y, int64
}
}
+// apply the weight scale (src[2]) carried by ggml_mul_mat_ext, broadcast over dst
+static void ggml_compute_forward_mul_mat_scale(
+ const struct ggml_compute_params * params,
+ struct ggml_tensor * dst) {
+ const struct ggml_tensor * scale = dst->src[2];
+ if (!scale) {
+ return;
+ }
+
+ const int ith = params->ith;
+ const int nth = params->nth;
+
+ const int64_t ne0 = dst->ne[0], ne1 = dst->ne[1], ne2 = dst->ne[2], ne3 = dst->ne[3];
+ const size_t nb1 = dst->nb[1], nb2 = dst->nb[2], nb3 = dst->nb[3];
+
+ ggml_barrier(params->threadpool);
+
+ const char * sd = (const char *) scale->data;
+ const int64_t sne0 = scale->ne[0], sne1 = scale->ne[1], sne2 = scale->ne[2], sne3 = scale->ne[3];
+ const int64_t nr = ne1 * ne2 * ne3;
+ for (int64_t ir = ith; ir < nr; ir += nth) {
+ const int64_t i1 = ir % ne1;
+ const int64_t i2 = (ir / ne1) % ne2;
+ const int64_t i3 = ir / (ne1 * ne2);
+ float * dp = (float *) ((char *) dst->data + i1*nb1 + i2*nb2 + i3*nb3);
+ const size_t so = (i1 % sne1)*scale->nb[1] + (i2 % sne2)*scale->nb[2] + (i3 % sne3)*scale->nb[3];
+ for (int64_t i0 = 0; i0 < ne0; i0++) {
+ dp[i0] *= *(const float *) (sd + (i0 % sne0)*scale->nb[0] + so);
+ }
+ }
+}
+
void ggml_compute_forward_mul_mat(
const struct ggml_compute_params * params,
struct ggml_tensor * dst) {
@@ -1321,6 +1353,7 @@ void ggml_compute_forward_mul_mat(
// If tiled is supported, it will execute the full op here and we return
if (ggml_compute_forward_mul_mat_tiled(params, dst)) {
+ ggml_compute_forward_mul_mat_scale(params, dst);
return;
}
@@ -1377,6 +1410,7 @@ void ggml_compute_forward_mul_mat(
src1->type,
dst->type))
goto UseGgmlGemm1;
+ ggml_compute_forward_mul_mat_scale(params, dst);
return;
}
UseGgmlGemm1:;
@@ -1457,6 +1491,7 @@ UseGgmlGemm1:;
vec_dot_type,
dst->type))
goto UseGgmlGemm2;
+ ggml_compute_forward_mul_mat_scale(params, dst);
return;
}
UseGgmlGemm2:;
@@ -1524,6 +1559,8 @@ UseGgmlGemm2:;
current_chunk = atomic_fetch_add_explicit(¶ms->threadpool->current_chunk, 1, memory_order_relaxed);
}
+
+ ggml_compute_forward_mul_mat_scale(params, dst);
}
// ggml_compute_forward_mul_mat_id
@@ -1606,6 +1643,53 @@ static void * incr_ptr_aligned(void ** p, size_t size, size_t align) {
return ptr;
}
+// apply the weight scale (src[3]) carried by ggml_mul_mat_id_ext: per-tensor [1],
+// per-expert [n_expert] or per-channel-per-expert [n_out, n_expert], indexed via ids
+static void ggml_compute_forward_mul_mat_id_scale(
+ const struct ggml_compute_params * params,
+ struct ggml_tensor * dst) {
+ const struct ggml_tensor * scale = dst->src[3];
+ if (!scale) {
+ return;
+ }
+
+ const struct ggml_tensor * ids = dst->src[2];
+
+ const int ith = params->ith;
+ const int nth = params->nth;
+
+ const int64_t ne0 = dst->ne[0], ne1 = dst->ne[1], ne2 = dst->ne[2];
+ const int64_t ne02 = dst->src[0]->ne[2];
+ const size_t nb1 = dst->nb[1], nb2 = dst->nb[2];
+
+ ggml_barrier(params->threadpool);
+
+ const char * sd = (const char *) scale->data;
+ const bool per_ch_exp = scale->ne[0] == ne0 && scale->ne[1] == ne02;
+ const bool per_expert = ggml_nelements(scale) == ne02;
+ const int64_t nr = ne1 * ne2;
+ for (int64_t ir = ith; ir < nr; ir += nth) {
+ const int64_t i1 = ir % ne1;
+ const int64_t i2 = ir / ne1;
+ const int32_t expert = *(const int32_t *) ((const char *) ids->data + i1*ids->nb[0] + i2*ids->nb[1]);
+ if (expert < 0) {
+ continue;
+ }
+ float * dp = (float *) ((char *) dst->data + i1*nb1 + i2*nb2);
+ if (per_ch_exp) {
+ const char * sp = sd + expert*scale->nb[1];
+ for (int64_t i0 = 0; i0 < ne0; i0++) {
+ dp[i0] *= *(const float *) (sp + i0*scale->nb[0]);
+ }
+ } else {
+ const float s = *(const float *) (sd + (per_expert ? expert*scale->nb[0] : 0));
+ for (int64_t i0 = 0; i0 < ne0; i0++) {
+ dp[i0] *= s;
+ }
+ }
+ }
+}
+
static void ggml_compute_forward_mul_mat_id(
const struct ggml_compute_params * params,
struct ggml_tensor * dst) {
@@ -1800,6 +1884,8 @@ static void ggml_compute_forward_mul_mat_id(
current_chunk = atomic_fetch_add_explicit(current_chunk_ctr, 1, memory_order_relaxed);
}
}
+
+ ggml_compute_forward_mul_mat_id_scale(params, dst);
}
/////////////////////////////////
@@ -1813,6 +1899,11 @@ static void ggml_compute_forward(struct ggml_compute_params * params, struct ggm
// extra_buffer op?
if (ggml_cpu_extra_compute_forward(params, tensor)) {
+ if (tensor->op == GGML_OP_MUL_MAT) {
+ ggml_compute_forward_mul_mat_scale(params, tensor);
+ } else if (tensor->op == GGML_OP_MUL_MAT_ID) {
+ ggml_compute_forward_mul_mat_id_scale(params, tensor);
+ }
return;
}
@@ -2930,7 +3021,7 @@ struct ggml_cplan ggml_graph_plan(
case GGML_OP_ADD:
case GGML_OP_ADD_ID:
{
- if (ggml_is_quantized(node->src[0]->type) || node->src[0]->type == GGML_TYPE_F8_E4M3) {
+ if (ggml_is_quantized(node->src[0]->type) || ggml_needs_scale(node->src[0]->type)) {
cur = ggml_type_size(GGML_TYPE_F32) * node->src[0]->ne[0] * n_tasks;
}
} break;
@@ -2978,7 +3069,7 @@ struct ggml_cplan ggml_graph_plan(
} break;
case GGML_OP_OUT_PROD:
{
- if (ggml_is_quantized(node->src[0]->type) || node->src[0]->type == GGML_TYPE_F8_E4M3 ||
+ if (ggml_is_quantized(node->src[0]->type) || ggml_needs_scale(node->src[0]->type) ||
node->src[0]->type == GGML_TYPE_F16) {
cur = ggml_type_size(GGML_TYPE_F32) * node->src[0]->ne[0] * n_tasks;
}
diff --git a/ggml/src/ggml-cpu/ops.cpp b/ggml/src/ggml-cpu/ops.cpp
index 61dfd220c..d01b6c11a 100644
--- a/ggml/src/ggml-cpu/ops.cpp
+++ b/ggml/src/ggml-cpu/ops.cpp
@@ -615,7 +615,7 @@ static void ggml_compute_forward_add_q_f32(
GGML_ASSERT(nb1 <= nb2);
GGML_ASSERT(nb2 <= nb3);
- GGML_ASSERT(ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F8_E4M3);
+ GGML_ASSERT(ggml_is_quantized(src0->type) || ggml_needs_scale(src0->type));
GGML_ASSERT(src1->type == GGML_TYPE_F32);
GGML_ASSERT(ne00 % ggml_blck_size(type) == 0);
@@ -965,7 +965,7 @@ static void ggml_compute_forward_add1_q_f32(
GGML_ASSERT(nb1 <= nb2);
GGML_ASSERT(nb2 <= nb3);
- GGML_ASSERT(ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F8_E4M3);
+ GGML_ASSERT(ggml_is_quantized(src0->type) || ggml_needs_scale(src0->type));
GGML_ASSERT(dst->type == src0->type);
GGML_ASSERT(src1->type == GGML_TYPE_F32);
diff --git a/ggml/src/ggml-cuda/fusion.cu b/ggml/src/ggml-cuda/fusion.cu
index bbbb50b4f..343857579 100644
--- a/ggml/src/ggml-cuda/fusion.cu
+++ b/ggml/src/ggml-cuda/fusion.cu
@@ -22,22 +22,24 @@
#include <cstring>
#include <initializer_list>
+// the weight scale forms that the fused gate/up/GLU kernels apply: per-tensor or per-expert, NVFP4 (mmvq/mmq) or F8 (mmvf)
+static bool ggml_cuda_mm_weight_scale_fusable(const ggml_tensor * mm) {
+ const ggml_tensor * s = ggml_get_src_scale(mm, 0);
+ return s && s->type == GGML_TYPE_F32 && ggml_is_contiguous(s) &&
+ ggml_nelements(s) == (mm->op == GGML_OP_MUL_MAT_ID ? mm->src[0]->ne[2] : 1) &&
+ (mm->src[0]->type == GGML_TYPE_NVFP4 || mm->src[0]->type == GGML_TYPE_F8_E4M3);
+}
+
static bool ggml_cuda_should_fuse_mul_mat(const ggml_tensor * ffn_up,
const ggml_tensor * ffn_gate,
const ggml_tensor * glu,
const ggml_tensor * ffn_up_bias = nullptr,
- const ggml_tensor * ffn_gate_bias = nullptr,
- const ggml_tensor * ffn_up_scale = nullptr,
- const ggml_tensor * ffn_gate_scale = nullptr) {
+ const ggml_tensor * ffn_gate_bias = nullptr) {
const bool has_bias = ffn_up_bias != nullptr || ffn_gate_bias != nullptr;
- const bool has_scale = ffn_up_scale != nullptr || ffn_gate_scale != nullptr;
if (has_bias && (!ffn_up_bias || !ffn_gate_bias)) {
return false;
}
- if (has_scale && (!ffn_up_scale || !ffn_gate_scale)) {
- return false;
- }
const bool is_mul_mat = ffn_up->op == GGML_OP_MUL_MAT && ffn_gate->op == GGML_OP_MUL_MAT && glu->op == GGML_OP_GLU;
const bool is_mul_mat_id = ffn_up->op == GGML_OP_MUL_MAT_ID && ffn_gate->op == GGML_OP_MUL_MAT_ID && glu->op == GGML_OP_GLU;
@@ -48,40 +50,33 @@ static bool ggml_cuda_should_fuse_mul_mat(const ggml_tensor * ffn_up,
return false;
}
+ if (ggml_get_src_scale(ffn_up, 0) || ggml_get_src_scale(ffn_gate, 0)) {
+ if (!ggml_cuda_mm_weight_scale_fusable(ffn_up) || !ggml_cuda_mm_weight_scale_fusable(ffn_gate)) {
+ return false;
+ }
+ }
+
const ggml_op expected_bias_op = is_mul_mat ? GGML_OP_ADD : GGML_OP_ADD_ID;
- const ggml_tensor * ffn_up_bias_src = has_scale ? ffn_up_scale : ffn_up;
- const ggml_tensor * ffn_gate_bias_src = has_scale ? ffn_gate_scale : ffn_gate;
- const ggml_tensor * ffn_up_out = has_bias ? ffn_up_bias : ffn_up_bias_src;
- const ggml_tensor * ffn_gate_out = has_bias ? ffn_gate_bias : ffn_gate_bias_src;
+ const ggml_tensor * ffn_up_out = has_bias ? ffn_up_bias : ffn_up;
+ const ggml_tensor * ffn_gate_out = has_bias ? ffn_gate_bias : ffn_gate;
if (glu->src[0] != ffn_gate_out || glu->src[1] != ffn_up_out) {
return false;
}
- if (has_scale) {
- if (ffn_up_scale->op != GGML_OP_MUL || ffn_gate_scale->op != GGML_OP_MUL) {
- return false;
- }
- const bool up_has_mm = ffn_up_scale->src[0] == ffn_up || ffn_up_scale->src[1] == ffn_up;
- const bool gate_has_mm = ffn_gate_scale->src[0] == ffn_gate || ffn_gate_scale->src[1] == ffn_gate;
- if (!up_has_mm || !gate_has_mm) {
- return false;
- }
- }
-
if (has_bias) {
if (ffn_up_bias->op != expected_bias_op || ffn_gate_bias->op != expected_bias_op) {
return false;
}
if (expected_bias_op == GGML_OP_ADD) {
- const bool up_has_mul = ffn_up_bias->src[0] == ffn_up_bias_src || ffn_up_bias->src[1] == ffn_up_bias_src;
- const bool gate_has_mul = ffn_gate_bias->src[0] == ffn_gate_bias_src || ffn_gate_bias->src[1] == ffn_gate_bias_src;
+ const bool up_has_mul = ffn_up_bias->src[0] == ffn_up || ffn_up_bias->src[1] == ffn_up;
+ const bool gate_has_mul = ffn_gate_bias->src[0] == ffn_gate || ffn_gate_bias->src[1] == ffn_gate;
if (!up_has_mul || !gate_has_mul) {
return false;
}
} else { // GGML_OP_ADD_ID
- if (ffn_up_bias->src[0] != ffn_up_bias_src || ffn_gate_bias->src[0] != ffn_gate_bias_src) {
+ if (ffn_up_bias->src[0] != ffn_up || ffn_gate_bias->src[0] != ffn_gate) {
return false;
}
if (ffn_up_bias->src[2] != ffn_up->src[2] || ffn_gate_bias->src[2] != ffn_gate->src[2]) {
@@ -153,7 +148,7 @@ static bool ggml_cuda_should_fuse_mul_mat_vec_f(const ggml_tensor * tensor, cons
return use_mul_mat_vec_f;
}
-static bool ggml_cuda_should_fuse_mul_mat_vec_q(const ggml_tensor * tensor) {
+bool ggml_cuda_should_fuse_mul_mat_vec_q(const ggml_tensor * tensor) {
ggml_tensor * src0 = tensor->src[0];
ggml_tensor * src1 = tensor->src[1];
const ggml_tensor * dst = tensor;
@@ -215,6 +210,7 @@ static bool ggml_cuda_match_shared_expert(const ggml_cgraph * graph, int routed_
if (!is_pair(gate, up, routed_idx) || !is_pair(shared_gate, shared_up, shared_idx) ||
!ggml_cuda_should_fuse_mul_mat(up, gate, routed) ||
!ggml_cuda_should_fuse_mul_mat(shared_up, shared_gate, shared) ||
+ ggml_get_src_scale(up, 0) || ggml_get_src_scale(shared_up, 0) ||
!up->src[0]->buffer ||
!ggml_is_quantized(up->src[0]->type)) {
return false;
@@ -239,50 +235,8 @@ static bool ggml_cuda_match_shared_expert(const ggml_cgraph * graph, int routed_
return true;
}
-// The weight scale that scale_node (MUL) applies to the output of mm_node (MUL_MAT), or nullptr.
-// Shared by ggml_cuda_try_fuse and graph_optimize so that both accept the same patterns.
-static const ggml_tensor * ggml_cuda_get_mul_mat_scale(const ggml_tensor * scale_node, const ggml_tensor * mm_node) {
- const bool scale_lhs_mm = scale_node->src[0] == mm_node;
- const bool scale_rhs_mm = scale_node->src[1] == mm_node;
- if (!scale_lhs_mm && !scale_rhs_mm) {
- return nullptr;
- }
-
- const ggml_tensor * scale = scale_lhs_mm ? scale_node->src[1] : scale_node->src[0];
- if ((mm_node->src[0]->type != GGML_TYPE_NVFP4 && mm_node->src[0]->type != GGML_TYPE_F8_E4M3) || scale_node->type != GGML_TYPE_F32 ||
- scale->type != GGML_TYPE_F32 || !ggml_is_contiguous(scale) || ggml_nelements(scale) != 1 ||
- !ggml_are_same_shape(scale_node, mm_node)) {
- return nullptr;
- }
-
- return scale;
-}
-
-// The same for MUL_MAT_ID with one scale per expert: RESHAPE, REPEAT and GET_ROWS (by the ids) of the scales, then the MUL.
-static const ggml_tensor * ggml_cuda_get_mul_mat_id_scale(const ggml_tensor * reshape, const ggml_tensor * repeat, const ggml_tensor * getrows,
- const ggml_tensor * scale_node, const ggml_tensor * mm_node) {
- if (repeat->src[0] != reshape || getrows->src[0] != repeat || getrows->src[1] != mm_node->src[2]) {
- return nullptr;
- }
- if (!((scale_node->src[0] == mm_node && scale_node->src[1] == getrows) ||
- (scale_node->src[0] == getrows && scale_node->src[1] == mm_node))) {
- return nullptr;
- }
-
- const ggml_tensor * scale = reshape->src[0];
- if ((mm_node->src[0]->type != GGML_TYPE_NVFP4 && mm_node->src[0]->type != GGML_TYPE_F8_E4M3) || scale_node->type != GGML_TYPE_F32 ||
- scale->type != GGML_TYPE_F32 || !ggml_is_contiguous(scale) || ggml_nelements(scale) != mm_node->src[0]->ne[2] ||
- !ggml_are_same_shape(scale_node, mm_node)) {
- return nullptr;
- }
-
- return scale;
-}
-
// Buffer- and batch-size independent, so graph_optimize produces the same graph topology for every ubatch size (see #29986).
-// up_scale/gate_scale: the MUL nodes that apply the NVFP4 weight scales (per tensor or per expert) between the MUL_MATs and the GLU.
-static bool ggml_cuda_mul_mat_q_fusion_matches(const ggml_tensor * up, const ggml_tensor * gate, const ggml_tensor * glu, const int device,
- const ggml_tensor * up_scale = nullptr, const ggml_tensor * gate_scale = nullptr) {
+static bool ggml_cuda_mul_mat_q_fusion_matches(const ggml_tensor * up, const ggml_tensor * gate, const ggml_tensor * glu, const int device) {
const ggml_tensor * x_up = up->src[0];
const ggml_tensor * y = up->src[1];
@@ -296,7 +250,7 @@ static bool ggml_cuda_mul_mat_q_fusion_matches(const ggml_tensor * up, const ggm
return false;
}
- if (!ggml_cuda_should_fuse_mul_mat(up, gate, glu, nullptr, nullptr, up_scale, gate_scale)) {
+ if (!ggml_cuda_should_fuse_mul_mat(up, gate, glu)) {
return false;
}
@@ -314,8 +268,7 @@ static bool ggml_cuda_mul_mat_q_fusion_matches(const ggml_tensor * up, const ggm
return turing_mma_available(ggml_cuda_info().devices[device].cc);
}
-static bool ggml_cuda_should_fuse_mul_mat_q(const ggml_tensor * up, const ggml_tensor * gate, const ggml_tensor * glu,
- const ggml_tensor * up_scale = nullptr, const ggml_tensor * gate_scale = nullptr) {
+static bool ggml_cuda_should_fuse_mul_mat_q(const ggml_tensor * up, const ggml_tensor * gate, const ggml_tensor * glu) {
// A hinted matmul can be fused, but its unfused path may be faster (e.g. FWHT for GGML_HINT_SRC0_IS_HADAMARD).
if (ggml_get_op_params_i32(up, 1) != GGML_HINT_NONE || ggml_get_op_params_i32(gate, 1) != GGML_HINT_NONE) {
return false;
@@ -327,7 +280,7 @@ static bool ggml_cuda_should_fuse_mul_mat_q(const ggml_tensor * up, const ggml_t
ggml_nbytes(x) != ggml_backend_buffer_get_alloc_size(x->buffer, x) && x->view_src;
};
const int device = ggml_cuda_get_device();
- if (!ggml_cuda_mul_mat_q_fusion_matches(up, gate, glu, device, up_scale, gate_scale) || bad_padding_clear(up->src[0]) || bad_padding_clear(gate->src[0])) {
+ if (!ggml_cuda_mul_mat_q_fusion_matches(up, gate, glu, device) || bad_padding_clear(up->src[0]) || bad_padding_clear(gate->src[0])) {
return false;
}
@@ -1997,6 +1950,9 @@ int ggml_cuda_try_fuse(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph * cgrap
ggml_tensor * node = cgraph->nodes[i];
+ // a scaled matmul only takes part in the gate/up/GLU fusion, which passes its weight scale on
+ const bool scaled_mm = (node->op == GGML_OP_MUL_MAT || node->op == GGML_OP_MUL_MAT_ID) && ggml_get_src_scale(node, 0);
+
if (node->op == GGML_OP_MUL_MAT_ID && cuda_ctx->stream_context().concurrent_events.empty() &&
ggml_cuda_match_shared_expert(cgraph, i, i + 3) &&
ggml_cuda_should_fuse_mul_mat_vec_q(cgraph->nodes[i + 2]->src[1])) {
@@ -2245,220 +2201,10 @@ int ggml_cuda_try_fuse(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph * cgrap
return bias_node->src[1];
};
- // gate + glu + up, with optional scale/bias on both lanes.
+ // gate + glu + up, with optional bias on both lanes.
for (ggml_op op : { GGML_OP_MUL_MAT, GGML_OP_MUL_MAT_ID }) {
const ggml_op bias_op = op == GGML_OP_MUL_MAT ? GGML_OP_ADD : GGML_OP_ADD_ID;
- if (op == GGML_OP_MUL_MAT) {
- for (const bool with_bias : { false, true }) {
- const int gate_idx = i;
- const int gate_scale_idx = i + 1;
- const int gate_bias_idx = with_bias ? i + 2 : -1;
- const int up_idx = with_bias ? i + 3 : i + 2;
- const int up_scale_idx = up_idx + 1;
- const int up_bias_idx = with_bias ? up_idx + 2 : -1;
- const int glu_idx = with_bias ? up_idx + 3 : up_idx + 2;
-
- const int out_nodes[] = { glu_idx };
- ggml_op ops[7];
- if (with_bias) {
- ops[0] = op;
- ops[1] = GGML_OP_MUL;
- ops[2] = bias_op;
- ops[3] = op;
- ops[4] = GGML_OP_MUL;
- ops[5] = bias_op;
- ops[6] = GGML_OP_GLU;
- } else {
- ops[0] = op;
- ops[1] = GGML_OP_MUL;
- ops[2] = op;
- ops[3] = GGML_OP_MUL;
- ops[4] = GGML_OP_GLU;
- }
- const int n_ops = with_bias ? 7 : 5;
-
- if (!ggml_can_fuse_subgraph(cgraph, i, n_ops, ops, out_nodes, 1) ||
- !ggml_cuda_check_fusion_memory_ranges(cgraph, i, n_ops, out_nodes, 1)) {
- continue;
- }
-
- ggml_tensor * gate_n = cgraph->nodes[gate_idx];
- ggml_tensor * gate_scale_n = cgraph->nodes[gate_scale_idx];
- ggml_tensor * gate_out_n = with_bias ? cgraph->nodes[gate_bias_idx] : gate_scale_n;
- ggml_tensor * up_n = cgraph->nodes[up_idx];
- ggml_tensor * up_scale_n = cgraph->nodes[up_scale_idx];
- ggml_tensor * up_out_n = with_bias ? cgraph->nodes[up_bias_idx] : up_scale_n;
- const ggml_tensor * glu = cgraph->nodes[glu_idx];
-
- if (!ggml_cuda_should_fuse_mul_mat(up_n, gate_n, glu,
- with_bias ? up_out_n : nullptr, with_bias ? gate_out_n : nullptr, up_scale_n, gate_scale_n)) {
- continue;
- }
-
- const ggml_tensor * gate_scale = ggml_cuda_get_mul_mat_scale(gate_scale_n, gate_n);
- const ggml_tensor * up_scale = ggml_cuda_get_mul_mat_scale(up_scale_n, up_n);
- if (!gate_scale || !up_scale) {
- continue;
- }
-
- const ggml_tensor * up_bias = with_bias ? get_bias_tensor(up_out_n, up_scale_n, bias_op) : nullptr;
- const ggml_tensor * gate_bias = with_bias ? get_bias_tensor(gate_out_n, gate_scale_n, bias_op) : nullptr;
- if (with_bias && (!ggml_are_same_shape(gate_out_n->src[0], gate_out_n->src[1]) ||
- !ggml_are_same_shape(up_out_n->src[0], up_out_n->src[1]))) {
- continue;
- }
-
- const ggml_tensor * src0 = up_n->src[0];
- const ggml_tensor * src1 = up_n->src[1];
- const ggml_tensor * ids = up_n->src[2];
-
- ggml_cuda_mm_fusion_args_host fusion_data{};
- fusion_data.gate = gate_n->src[0];
- fusion_data.x_bias = up_bias;
- fusion_data.gate_bias = gate_bias;
- fusion_data.x_scale = up_scale;
- fusion_data.gate_scale = gate_scale;
- fusion_data.glu_op = ggml_get_glu_op(glu);
- fusion_data.glu_limit = ggml_get_op_params_f32(glu, 3);
-
- if (ggml_cuda_should_fuse_mul_mat_vec_f(up_n, gate_n)) {
- ggml_cuda_mul_mat_vec_f(*cuda_ctx, src0, src1, ids, cgraph->nodes[glu_idx], &fusion_data);
- fused_mul_mat_vec = true;
- fused_node_count = n_ops;
- break;
- }
-
- if (ggml_cuda_should_fuse_mul_mat_vec_q(up_n)) {
- ggml_cuda_mul_mat_vec_q(*cuda_ctx, src0, src1, ids, cgraph->nodes[glu_idx], &fusion_data);
- fused_mul_mat_vec = true;
- fused_node_count = n_ops;
- break;
- }
-
- if (!with_bias && ggml_cuda_should_fuse_mul_mat_q(up_n, gate_n, glu, up_scale_n, gate_scale_n)) {
- fusion_data.prec_src1 = ggml_prec_get_src(up_n, 1);
- ggml_cuda_mul_mat_q(*cuda_ctx, src0, src1, ids, cgraph->nodes[glu_idx], &fusion_data);
- fused_mul_mat_vec = true;
- fused_node_count = n_ops;
- break;
- }
- }
-
- if (fused_mul_mat_vec) {
- break;
- }
- } else {
- for (const bool with_bias : { false, true }) {
- const int gate_idx = i;
- const int gate_scale_idx = i + 4;
- const int gate_bias_idx = with_bias ? i + 5 : -1;
- const int up_idx = with_bias ? i + 6 : i + 5;
- const int up_scale_idx = up_idx + 4;
- const int up_bias_idx = with_bias ? up_idx + 5 : -1;
- const int glu_idx = with_bias ? up_idx + 6 : up_idx + 5;
-
- const int out_nodes[] = { glu_idx };
- ggml_op ops[13];
- if (with_bias) {
- ops[0] = op;
- ops[1] = GGML_OP_RESHAPE;
- ops[2] = GGML_OP_REPEAT;
- ops[3] = GGML_OP_GET_ROWS;
- ops[4] = GGML_OP_MUL;
- ops[5] = bias_op;
- ops[6] = op;
- ops[7] = GGML_OP_RESHAPE;
- ops[8] = GGML_OP_REPEAT;
- ops[9] = GGML_OP_GET_ROWS;
- ops[10] = GGML_OP_MUL;
- ops[11] = bias_op;
- ops[12] = GGML_OP_GLU;
- } else {
- ops[0] = op;
- ops[1] = GGML_OP_RESHAPE;
- ops[2] = GGML_OP_REPEAT;
- ops[3] = GGML_OP_GET_ROWS;
- ops[4] = GGML_OP_MUL;
- ops[5] = op;
- ops[6] = GGML_OP_RESHAPE;
- ops[7] = GGML_OP_REPEAT;
- ops[8] = GGML_OP_GET_ROWS;
- ops[9] = GGML_OP_MUL;
- ops[10] = GGML_OP_GLU;
- }
- const int n_ops = with_bias ? 13 : 11;
-
- if (!ggml_can_fuse_subgraph(cgraph, i, n_ops, ops, out_nodes, 1) ||
- !ggml_cuda_check_fusion_memory_ranges(cgraph, i, n_ops, out_nodes, 1)) {
- continue;
- }
-
- ggml_tensor * gate_n = cgraph->nodes[gate_idx];
- ggml_tensor * gate_scale_n = cgraph->nodes[gate_scale_idx];
- ggml_tensor * gate_out_n = with_bias ? cgraph->nodes[gate_bias_idx] : gate_scale_n;
- ggml_tensor * up_n = cgraph->nodes[up_idx];
- ggml_tensor * up_scale_n = cgraph->nodes[up_scale_idx];
- ggml_tensor * up_out_n = with_bias ? cgraph->nodes[up_bias_idx] : up_scale_n;
- const ggml_tensor * glu = cgraph->nodes[glu_idx];
-
- if (!ggml_cuda_should_fuse_mul_mat(up_n, gate_n, glu,
- with_bias ? up_out_n : nullptr, with_bias ? gate_out_n : nullptr, up_scale_n, gate_scale_n)) {
- continue;
- }
-
- const ggml_tensor * gate_scale = ggml_cuda_get_mul_mat_id_scale(cgraph->nodes[gate_idx + 1], cgraph->nodes[gate_idx + 2],
- cgraph->nodes[gate_idx + 3], gate_scale_n, gate_n);
- const ggml_tensor * up_scale = ggml_cuda_get_mul_mat_id_scale(cgraph->nodes[up_idx + 1], cgraph->nodes[up_idx + 2],
- cgraph->nodes[up_idx + 3], up_scale_n, up_n);
- if (!gate_scale || !up_scale) {
- continue;
- }
-
- const ggml_tensor * up_bias = with_bias ? get_bias_tensor(up_out_n, up_scale_n, bias_op) : nullptr;
- const ggml_tensor * gate_bias = with_bias ? get_bias_tensor(gate_out_n, gate_scale_n, bias_op) : nullptr;
-
- const ggml_tensor * src0 = up_n->src[0];
- const ggml_tensor * src1 = up_n->src[1];
- const ggml_tensor * ids = up_n->src[2];
-
- ggml_cuda_mm_fusion_args_host fusion_data{};
- fusion_data.gate = gate_n->src[0];
- fusion_data.x_bias = up_bias;
- fusion_data.gate_bias = gate_bias;
- fusion_data.x_scale = up_scale;
- fusion_data.gate_scale = gate_scale;
- fusion_data.glu_op = ggml_get_glu_op(glu);
- fusion_data.glu_limit = ggml_get_op_params_f32(glu, 3);
-
- if (ggml_cuda_should_fuse_mul_mat_vec_f(up_n, gate_n)) {
- ggml_cuda_mul_mat_vec_f(*cuda_ctx, src0, src1, ids, cgraph->nodes[glu_idx], &fusion_data);
- fused_mul_mat_vec = true;
- fused_node_count = n_ops;
- break;
- }
-
- if (ggml_cuda_should_fuse_mul_mat_vec_q(up_n)) {
- ggml_cuda_mul_mat_vec_q(*cuda_ctx, src0, src1, ids, cgraph->nodes[glu_idx], &fusion_data);
- fused_mul_mat_vec = true;
- fused_node_count = n_ops;
- break;
- }
-
- if (!with_bias && ggml_cuda_should_fuse_mul_mat_q(up_n, gate_n, glu, up_scale_n, gate_scale_n)) {
- fusion_data.prec_src1 = ggml_prec_get_src(up_n, 1);
- ggml_cuda_mul_mat_q(*cuda_ctx, src0, src1, ids, cgraph->nodes[glu_idx], &fusion_data);
- fused_mul_mat_vec = true;
- fused_node_count = n_ops;
- break;
- }
- }
-
- if (fused_mul_mat_vec) {
- break;
- }
- }
-
if (ggml_cuda_can_fuse(cgraph, i, { op, bias_op, op, bias_op, GGML_OP_GLU }, {})) {
ggml_tensor * glu = cgraph->nodes[i + 4];
ggml_tensor * gate_bias_n = glu->src[0];
@@ -2493,15 +2239,17 @@ int ggml_cuda_try_fuse(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph * cgrap
const ggml_tensor * src0 = up_n->src[0];
const ggml_tensor * src1 = up_n->src[1];
- const ggml_tensor * ids = up_n->src[2];
+ const ggml_tensor * ids = op == GGML_OP_MUL_MAT_ID ? up_n->src[2] : nullptr;
if (ggml_cuda_should_fuse_mul_mat_vec_f(up_n, gate_n)) {
ggml_cuda_mm_fusion_args_host fusion_data{};
- fusion_data.gate = gate_n->src[0];
- fusion_data.x_bias = up_bias_tensor;
- fusion_data.gate_bias = gate_bias_tensor;
- fusion_data.glu_op = ggml_get_glu_op(glu);
- fusion_data.glu_limit = ggml_get_op_params_f32(glu, 3);
+ fusion_data.gate = gate_n->src[0];
+ fusion_data.x_bias = up_bias_tensor;
+ fusion_data.gate_bias = gate_bias_tensor;
+ fusion_data.x_scale = ggml_get_src_scale(up_n, 0);
+ fusion_data.gate_scale = ggml_get_src_scale(gate_n, 0);
+ fusion_data.glu_op = ggml_get_glu_op(glu);
+ fusion_data.glu_limit = ggml_get_op_params_f32(glu, 3);
ggml_cuda_mul_mat_vec_f(*cuda_ctx, src0, src1, ids, glu, &fusion_data);
fused_mul_mat_vec = true;
@@ -2511,11 +2259,13 @@ int ggml_cuda_try_fuse(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph * cgrap
if (ggml_cuda_should_fuse_mul_mat_vec_q(up_n)) {
ggml_cuda_mm_fusion_args_host fusion_data{};
- fusion_data.gate = gate_n->src[0];
- fusion_data.x_bias = up_bias_tensor;
- fusion_data.gate_bias = gate_bias_tensor;
- fusion_data.glu_op = ggml_get_glu_op(glu);
- fusion_data.glu_limit = ggml_get_op_params_f32(glu, 3);
+ fusion_data.gate = gate_n->src[0];
+ fusion_data.x_bias = up_bias_tensor;
+ fusion_data.gate_bias = gate_bias_tensor;
+ fusion_data.x_scale = ggml_get_src_scale(up_n, 0);
+ fusion_data.gate_scale = ggml_get_src_scale(gate_n, 0);
+ fusion_data.glu_op = ggml_get_glu_op(glu);
+ fusion_data.glu_limit = ggml_get_op_params_f32(glu, 3);
ggml_cuda_mul_mat_vec_q(*cuda_ctx, src0, src1, ids, glu, &fusion_data);
fused_mul_mat_vec = true;
@@ -2536,13 +2286,15 @@ int ggml_cuda_try_fuse(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph * cgrap
const ggml_tensor * src0 = up->src[0];
const ggml_tensor * src1 = up->src[1];
- const ggml_tensor * ids = up->src[2];
+ const ggml_tensor * ids = op == GGML_OP_MUL_MAT_ID ? up->src[2] : nullptr;
if (ggml_cuda_should_fuse_mul_mat_vec_f(up, gate)) {
ggml_cuda_mm_fusion_args_host fusion_data{};
- fusion_data.gate = gate->src[0];
- fusion_data.glu_op = ggml_get_glu_op(glu);
- fusion_data.glu_limit = ggml_get_op_params_f32(glu, 3);
+ fusion_data.gate = gate->src[0];
+ fusion_data.x_scale = ggml_get_src_scale(up, 0);
+ fusion_data.gate_scale = ggml_get_src_scale(gate, 0);
+ fusion_data.glu_op = ggml_get_glu_op(glu);
+ fusion_data.glu_limit = ggml_get_op_params_f32(glu, 3);
ggml_cuda_mul_mat_vec_f(*cuda_ctx, src0, src1, ids, glu, &fusion_data);
fused_mul_mat_vec = true;
@@ -2552,9 +2304,11 @@ int ggml_cuda_try_fuse(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph * cgrap
if (ggml_cuda_should_fuse_mul_mat_vec_q(up)) {
ggml_cuda_mm_fusion_args_host fusion_data{};
- fusion_data.gate = gate->src[0];
- fusion_data.glu_op = ggml_get_glu_op(glu);
- fusion_data.glu_limit = ggml_get_op_params_f32(glu, 3);
+ fusion_data.gate = gate->src[0];
+ fusion_data.x_scale = ggml_get_src_scale(up, 0);
+ fusion_data.gate_scale = ggml_get_src_scale(gate, 0);
+ fusion_data.glu_op = ggml_get_glu_op(glu);
+ fusion_data.glu_limit = ggml_get_op_params_f32(glu, 3);
ggml_cuda_mul_mat_vec_q(*cuda_ctx, src0, src1, ids, glu, &fusion_data);
fused_mul_mat_vec = true;
@@ -2564,10 +2318,12 @@ int ggml_cuda_try_fuse(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph * cgrap
if (ggml_cuda_should_fuse_mul_mat_q(up, gate, glu)) {
ggml_cuda_mm_fusion_args_host fusion_data{};
- fusion_data.gate = gate->src[0];
- fusion_data.glu_op = ggml_get_glu_op(glu);
- fusion_data.glu_limit = ggml_get_op_params_f32(glu, 3);
- fusion_data.prec_src1 = ggml_prec_get_src(up, 1);
+ fusion_data.gate = gate->src[0];
+ fusion_data.x_scale = ggml_get_src_scale(up, 0);
+ fusion_data.gate_scale = ggml_get_src_scale(gate, 0);
+ fusion_data.glu_op = ggml_get_glu_op(glu);
+ fusion_data.glu_limit = ggml_get_op_params_f32(glu, 3);
+ fusion_data.prec_src1 = ggml_prec_get_src(up, 1);
ggml_cuda_mul_mat_q(*cuda_ctx, src0, src1, ids, glu, &fusion_data);
fused_mul_mat_vec = true;
@@ -2581,126 +2337,8 @@ int ggml_cuda_try_fuse(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph * cgrap
return fused_node_count - 1;
}
- fused_mul_mat_vec = false;
- fused_node_count = 0;
-
- // mul_mat + scale + optional bias
- for (ggml_op op : { GGML_OP_MUL_MAT, GGML_OP_MUL_MAT_ID }) {
- const ggml_op bias_op = op == GGML_OP_MUL_MAT ? GGML_OP_ADD : GGML_OP_ADD_ID;
-
- for (const bool with_bias : { false, true }) {
- const int n_ops = op == GGML_OP_MUL_MAT ? (with_bias ? 3 : 2) : (with_bias ? 6 : 5);
- const int out_nodes[] = { i + n_ops - 1 };
- ggml_op ops[6];
- if (op == GGML_OP_MUL_MAT) {
- if (with_bias) {
- ops[0] = op;
- ops[1] = GGML_OP_MUL;
- ops[2] = bias_op;
- } else {
- ops[0] = op;
- ops[1] = GGML_OP_MUL;
- }
- } else {
- if (with_bias) {
- ops[0] = op;
- ops[1] = GGML_OP_RESHAPE;
- ops[2] = GGML_OP_REPEAT;
- ops[3] = GGML_OP_GET_ROWS;
- ops[4] = GGML_OP_MUL;
- ops[5] = bias_op;
- } else {
- ops[0] = op;
- ops[1] = GGML_OP_RESHAPE;
- ops[2] = GGML_OP_REPEAT;
- ops[3] = GGML_OP_GET_ROWS;
- ops[4] = GGML_OP_MUL;
- }
- }
-
- if (!ggml_can_fuse_subgraph(cgraph, i, n_ops, ops, out_nodes, 1) ||
- !ggml_cuda_check_fusion_memory_ranges(cgraph, i, n_ops, out_nodes, 1)) {
- continue;
- }
-
- ggml_tensor * mm_node = cgraph->nodes[i];
- ggml_tensor * scale_node = op == GGML_OP_MUL_MAT ? cgraph->nodes[i + 1] : cgraph->nodes[i + 4];
- ggml_tensor * out_node = with_bias ? cgraph->nodes[i + n_ops - 1] : scale_node;
-
- const ggml_tensor * scale = nullptr;
- if (op == GGML_OP_MUL_MAT) {
- scale = ggml_cuda_get_mul_mat_scale(scale_node, mm_node);
- } else {
- scale = ggml_cuda_get_mul_mat_id_scale(cgraph->nodes[i + 1], cgraph->nodes[i + 2], cgraph->nodes[i + 3], scale_node, mm_node);
- }
- if (!scale) {
- continue;
- }
-
- const ggml_tensor * bias = with_bias ? get_bias_tensor(out_node, scale_node, bias_op) : nullptr;
- if (with_bias && !bias) {
- continue;
- }
- if (with_bias && bias_op == GGML_OP_ADD && !ggml_are_same_shape(out_node->src[0], out_node->src[1])) {
- continue;
- }
- if (with_bias && bias_op == GGML_OP_ADD_ID && out_node->src[2] != mm_node->src[2]) {
- continue;
- }
-
- const ggml_tensor * src0 = mm_node->src[0];
- const ggml_tensor * src1 = mm_node->src[1];
- const ggml_tensor * ids = mm_node->src[2];
-
- ggml_cuda_mm_fusion_args_host fusion_data{};
- fusion_data.x_bias = bias;
- fusion_data.x_scale = scale;
-
- if (ggml_cuda_should_fuse_mul_mat_vec_f(mm_node)) {
- ggml_cuda_mul_mat_vec_f(*cuda_ctx, src0, src1, ids, out_node, &fusion_data);
- fused_mul_mat_vec = true;
- fused_node_count = n_ops;
- break;
- }
-
- if (op == GGML_OP_MUL_MAT && !with_bias && src0->type == GGML_TYPE_F8_E4M3 &&
- ggml_get_op_params_i32(mm_node, 3) != GGML_PREC_F32 &&
- ggml_cuda_mul_mat_fp8(*cuda_ctx, src0, src1, scale_node, scale)) {
- return 1;
- }
-
- if (!with_bias && src0->type == GGML_TYPE_NVFP4 && (src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_BF16) &&
- mm_node->type == GGML_TYPE_F32) {
- const int cc = ggml_cuda_info().devices[cuda_ctx->device].cc;
- const bool is_id = op == GGML_OP_MUL_MAT_ID;
- const int64_t ncols = is_id ? src1->ne[2] : src1->ne[1];
- const bool use_mmvq = is_id ? ncols <= get_mmvq_mmid_max_batch(src0->type, cc) :
- ggml_cuda_should_use_mmvq(src0->type, cc, ncols);
- const bool bad_padding_clear = ggml_backend_buffer_get_usage(src0->buffer) == GGML_BACKEND_BUFFER_USAGE_COMPUTE &&
- ggml_nbytes(src0) != ggml_backend_buffer_get_alloc_size(src0->buffer, src0) &&
- src0->view_src;
- if (!use_mmvq && !bad_padding_clear &&
- ggml_cuda_should_use_mmq(src0->type, cc, ncols, /*n_experts =*/ is_id ? src0->ne[2] : 0)) {
- fusion_data.prec_src1 = ggml_prec_get_src(mm_node, 1);
- ggml_cuda_mul_mat_q(*cuda_ctx, src0, src1, ids, out_node, &fusion_data);
- return n_ops - 1;
- }
- }
-
- if (ggml_cuda_should_fuse_mul_mat_vec_q(mm_node)) {
- ggml_cuda_mul_mat_vec_q(*cuda_ctx, src0, src1, ids, out_node, &fusion_data);
- fused_mul_mat_vec = true;
- fused_node_count = n_ops;
- break;
- }
- }
- if (fused_mul_mat_vec) {
- break;
- }
- }
-
- if (fused_mul_mat_vec) {
- return fused_node_count - 1;
+ if (scaled_mm) {
+ return 0;
}
// mul_mat + add
@@ -2882,33 +2520,6 @@ void ggml_cuda_fusion_add_alloc_deps(ggml_backend_cuda_context * cuda_ctx, ggml_
continue;
}
}
- // Same with the NVFP4 scales: MUL_MAT(gate), MUL(gate scale), MUL_MAT(up), MUL(up scale), GLU.
- if (i + 4 < cgraph->n_nodes && cgraph->nodes[i + 4]->op == GGML_OP_GLU) {
- ggml_tensor * glu = cgraph->nodes[i + 4];
- ggml_tensor * gate_scale = cgraph->nodes[i + 1];
- ggml_tensor * up_scale = cgraph->nodes[i + 3];
- if (glu->src[0] == gate_scale && glu->src[1] == up_scale &&
- ggml_cuda_mul_mat_q_fusion_matches(cgraph->nodes[i + 2], cgraph->nodes[i], glu, cuda_ctx->device, up_scale, gate_scale) &&
- ggml_cuda_get_mul_mat_scale(gate_scale, cgraph->nodes[i]) && ggml_cuda_get_mul_mat_scale(up_scale, cgraph->nodes[i + 2])) {
- add_alloc_deps(i, i + 4);
- i += 4;
- continue;
- }
- }
- // Same with per-expert NVFP4 scales: MUL_MAT_ID, RESHAPE, REPEAT, GET_ROWS, MUL for gate and for up, GLU.
- if (i + 10 < cgraph->n_nodes && cgraph->nodes[i + 10]->op == GGML_OP_GLU) {
- ggml_tensor * glu = cgraph->nodes[i + 10];
- ggml_tensor * gate_scale = cgraph->nodes[i + 4];
- ggml_tensor * up_scale = cgraph->nodes[i + 9];
- if (glu->src[0] == gate_scale && glu->src[1] == up_scale &&
- ggml_cuda_mul_mat_q_fusion_matches(cgraph->nodes[i + 5], cgraph->nodes[i], glu, cuda_ctx->device, up_scale, gate_scale) &&
- ggml_cuda_get_mul_mat_id_scale(cgraph->nodes[i + 1], cgraph->nodes[i + 2], cgraph->nodes[i + 3], gate_scale, cgraph->nodes[i]) &&
- ggml_cuda_get_mul_mat_id_scale(cgraph->nodes[i + 6], cgraph->nodes[i + 7], cgraph->nodes[i + 8], up_scale, cgraph->nodes[i + 5])) {
- add_alloc_deps(i, i + 10);
- i += 10;
- continue;
- }
- }
ggml_cuda_moe_weighted_reduction_match match;
if (ggml_cuda_match_moe_weighted_reduction(cgraph, i, match)) {
diff --git a/ggml/src/ggml-cuda/fusion.cuh b/ggml/src/ggml-cuda/fusion.cuh
index 38173b2f7..c214fe56e 100644
--- a/ggml/src/ggml-cuda/fusion.cuh
+++ b/ggml/src/ggml-cuda/fusion.cuh
@@ -3,6 +3,9 @@
#include "common.cuh"
#include "ggml-backend-impl.h"
+// whether a mat-vec node can use the fused MMVQ path
+bool ggml_cuda_should_fuse_mul_mat_vec_q(const ggml_tensor * tensor);
+
// try and fuse nodes starting at node i and return the number of nodes to skip
int ggml_cuda_try_fuse(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph * cgraph, int i);
diff --git a/ggml/src/ggml-cuda/ggml-cuda.cu b/ggml/src/ggml-cuda/ggml-cuda.cu
index f2b3740a5..7c8057ba5 100644
--- a/ggml/src/ggml-cuda/ggml-cuda.cu
+++ b/ggml/src/ggml-cuda/ggml-cuda.cu
@@ -2123,7 +2123,7 @@ static void ggml_cuda_mul_mat_cublas(ggml_backend_cuda_context & ctx, const ggml
const int cc = ggml_cuda_info().devices[ctx.device].cc;
const ggml_prec prec = ggml_prec_get_acc(dst);
ggml_type compute_type = src0->type;
- if (ggml_is_quantized(compute_type) || compute_type == GGML_TYPE_F8_E4M3) {
+ if (ggml_is_quantized(compute_type) || ggml_needs_scale(compute_type)) {
compute_type = fast_fp16_hardware_available(cc) ? GGML_TYPE_F16 : GGML_TYPE_F32;
if (ggml_cuda_info().devices[ctx.device].prefer_fp32_acc) {
compute_type = GGML_TYPE_BF16;
@@ -2203,11 +2203,22 @@ static void ggml_cuda_mul_mat_cublas(ggml_backend_cuda_context & ctx, const ggml
}
}
-static void ggml_cuda_mul_mat(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
+// returns true if a scaled matmul applied its weight scale in-kernel (mmvq/mmq epilogue or cuBLASLt fp8 fold),
+// so the caller can skip the separate ggml_cuda_op_mul_mat_scale pass
+static bool ggml_cuda_mul_mat(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
GGML_TENSOR_BINARY_OP_LOCALS
if (ggml_cuda_op_mul_mat_use_fwht(dst) && ggml_cuda_op_fwht(ctx, src1, dst)) {
- return;
+ return false;
+ }
+
+ // mmvq and mmq can fold the weight scale (src[2]) into their epilogue for NVFP4 per-tensor scales
+ const bool scale_fold = ggml_get_src_scale(dst, 0) != NULL &&
+ src0->type == GGML_TYPE_NVFP4 && ggml_nelements(ggml_get_src_scale(dst, 0)) == 1;
+ ggml_cuda_mm_fusion_args_host fusion = {};
+ if (scale_fold) {
+ fusion.x_scale = ggml_get_src_scale(dst, 0);
+ fusion.prec_src1 = ggml_prec_get_src(dst, 1);
}
// If src0 is a temporary compute buffer it may have some padding that needs to be cleared for mul_mat_vec_q or mul_mat_q.
@@ -2218,7 +2229,7 @@ static void ggml_cuda_mul_mat(ggml_backend_cuda_context & ctx, const ggml_tensor
// MMVQ, MMQ and MMVF (BF16 and FP8 weights) also read a BF16 src1, MMF only an F32 src1
if (bad_padding_clear || (src1->type != GGML_TYPE_F32 && src1->type != GGML_TYPE_BF16) || dst->type != GGML_TYPE_F32) {
ggml_cuda_mul_mat_cublas(ctx, src0, src1, dst);
- return;
+ return false;
}
const int cc = ggml_cuda_info().devices[ctx.device].cc;
@@ -2228,7 +2239,7 @@ static void ggml_cuda_mul_mat(ggml_backend_cuda_context & ctx, const ggml_tensor
// The custom vector kernel can be used over batched cuBLAS GEMM.
// But this is only faster for GPUs without tensor cores or with a thin src0 matrix (particularly KQV in attention)
ggml_cuda_mul_mat_vec_f(ctx, src0, src1, nullptr, dst);
- return;
+ return false;
}
// A transposed vector can still use MMVF (i.e. ne01 == 1)
if (ne01 == 1 && ne11 > MMVF_MAX_BATCH_SIZE && ne2 == 1 && ne3 == 1
@@ -2242,30 +2253,36 @@ static void ggml_cuda_mul_mat(ggml_backend_cuda_context & ctx, const ggml_tensor
dst_vec.nb[2] = dst_vec.nb[1];
dst_vec.nb[3] = dst_vec.nb[1];
ggml_cuda_mul_mat_vec_f(ctx, src1, src0, nullptr, &dst_vec);
- return;
+ return false;
}
if (src1->type == GGML_TYPE_F32 && ggml_cuda_should_use_mmf(src0->type, cc, warp_size, src0->ne, src0->nb, ne11, /*mul_mat_id =*/ false)) {
ggml_cuda_mul_mat_f(ctx, src0, src1, nullptr, dst);
- return;
+ return false;
}
// from a per-type number of src1 columns up to 24, few-row MMQ is faster than MMVQ and MMQ
const bool use_mmq_few = ggml_cuda_should_use_mmq_few(src0, src1, nullptr, cc);
if (!use_mmq_few && ggml_cuda_should_use_mmvq(src0->type, cc, ne11)) {
- ggml_cuda_mul_mat_vec_q(ctx, src0, src1, nullptr, dst);
- return;
+ const bool fold = scale_fold && ggml_cuda_should_fuse_mul_mat_vec_q(dst);
+ ggml_cuda_mul_mat_vec_q(ctx, src0, src1, nullptr, dst, fold ? &fusion : nullptr);
+ return fold;
}
if (use_mmq_few) {
ggml_cuda_mul_mat_q_few(ctx, src0, src1, nullptr, dst);
- return;
+ return false;
}
if (ggml_cuda_should_use_mmq(src0->type, cc, ne11, /*n_experts =*/ 0)) {
- ggml_cuda_mul_mat_q(ctx, src0, src1, nullptr, dst);
- return;
- }
- if (src0->type == GGML_TYPE_F8_E4M3 && ggml_cuda_mul_mat_fp8(ctx, src0, src1, dst)) {
- return;
+ ggml_cuda_mul_mat_q(ctx, src0, src1, nullptr, dst, scale_fold ? &fusion : nullptr);
+ return scale_fold;
+ }
+ if (src0->type == GGML_TYPE_F8_E4M3 && ggml_prec_get_acc(dst) != GGML_PREC_F32) {
+ const ggml_tensor * w_scale = ggml_get_src_scale(dst, 0);
+ const ggml_tensor * weight_scale = w_scale && ggml_nelements(w_scale) == 1 ? w_scale : nullptr;
+ if (ggml_cuda_mul_mat_fp8(ctx, src0, src1, dst, weight_scale)) {
+ return weight_scale != nullptr;
+ }
}
ggml_cuda_mul_mat_cublas(ctx, src0, src1, dst);
+ return false;
}
// returns true when ggml_cuda_mul_mat_id takes the fallback path that requires stream synchronization
@@ -2304,7 +2321,8 @@ static bool ggml_cuda_mul_mat_id_needs_sync(const ggml_tensor * dst, const int c
return true;
}
-static void ggml_cuda_mul_mat_id(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
+// returns true if a scaled matmul applied its weight scale in-kernel (mmvq/mmq epilogue fold)
+static bool ggml_cuda_mul_mat_id(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
const ggml_tensor * src0 = dst->src[0];
const ggml_tensor * src1 = dst->src[1];
const ggml_tensor * ids = dst->src[2];
@@ -2317,6 +2335,16 @@ static void ggml_cuda_mul_mat_id(ggml_backend_cuda_context & ctx, ggml_tensor *
const int cc = ggml_cuda_info().devices[ggml_cuda_get_device()].cc;
const int warp_size = ggml_cuda_info().devices[ggml_cuda_get_device()].warp_size;
+ // mmvq and mmq can fold the per-expert weight scale (src[3]) into their epilogue for NVFP4
+ const bool scale_fold = ggml_get_src_scale(dst, 0) != NULL &&
+ src0->type == GGML_TYPE_NVFP4 && ggml_nelements(ggml_get_src_scale(dst, 0)) == src0->ne[2] &&
+ ggml_is_contiguous(ggml_get_src_scale(dst, 0));
+ ggml_cuda_mm_fusion_args_host fusion = {};
+ if (scale_fold) {
+ fusion.x_scale = ggml_get_src_scale(dst, 0);
+ fusion.prec_src1 = ggml_prec_get_src(dst, 1);
+ }
+
// [TAG_MUL_MAT_ID_CUDA_GRAPHS]
if ((src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_BF16) && dst->type == GGML_TYPE_F32) {
// up to 24 tokens few-row MMQ is faster than MMQ, on DGX Spark also faster than MMVQ
@@ -2327,28 +2355,29 @@ static void ggml_cuda_mul_mat_id(ggml_backend_cuda_context & ctx, ggml_tensor *
if (ggml_is_quantized(src0->type)) {
const int mmvq_mmid_max = get_mmvq_mmid_max_batch(src0->type, cc);
if (ne2 <= mmvq_mmid_max) {
- ggml_cuda_mul_mat_vec_q(ctx, src0, src1, ids, dst);
- return;
+ const bool fold = scale_fold && ggml_cuda_should_fuse_mul_mat_vec_q(dst);
+ ggml_cuda_mul_mat_vec_q(ctx, src0, src1, ids, dst, fold ? &fusion : nullptr);
+ return fold;
}
} else if (ggml_cuda_should_use_mmvf(src0, src1->type, cc, warp_size, ne2, /*mul_mat_id =*/ true)) {
ggml_cuda_mul_mat_vec_f(ctx, src0, src1, ids, dst);
- return;
+ return false;
}
}
if (use_mmq_few) {
ggml_cuda_mul_mat_q_few(ctx, src0, src1, ids, dst);
- return;
+ return false;
}
if (ggml_cuda_should_use_mmq(src0->type, cc, ne12, /*n_experts=*/ne02)) {
- ggml_cuda_mul_mat_q(ctx, src0, src1, ids, dst);
- return;
+ ggml_cuda_mul_mat_q(ctx, src0, src1, ids, dst, scale_fold ? &fusion : nullptr);
+ return scale_fold;
}
if (src1->type == GGML_TYPE_F32 && ggml_cuda_should_use_mmf(src0->type, cc, warp_size, src0->ne, src0->nb, src1->ne[2], /*mul_mat_id=*/true)) {
ggml_cuda_mul_mat_f(ctx, src0, src1, ids, dst);
- return;
+ return false;
}
}
@@ -2360,7 +2389,7 @@ static void ggml_cuda_mul_mat_id(ggml_backend_cuda_context & ctx, ggml_tensor *
GGML_ASSERT(nb2 % nb1 == 0);
const ggml_type type_src1_sorted = (src0->type == GGML_TYPE_F16 && !fast_fp16_hardware_available(cc))
- || ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F8_E4M3 ? GGML_TYPE_F32 : src0->type;
+ || ggml_is_quantized(src0->type) || ggml_needs_scale(src0->type) ? GGML_TYPE_F32 : src0->type;
const ggml_type type_dst_sorted = GGML_TYPE_F32;
const size_t ts_src1_sorted = ggml_type_size(type_src1_sorted);
const size_t ts_dst_sorted = ggml_type_size(type_dst_sorted);
@@ -2469,6 +2498,79 @@ static void ggml_cuda_mul_mat_id(ggml_backend_cuda_context & ctx, ggml_tensor *
ne0, ne0*ts_dst_sorted, ne_get_rows*ne0*ts_dst_sorted, ne_get_rows*ne0*ts_dst_sorted,
ne_get_rows, 1, 1, sizeof(int32_t), ne_get_rows*sizeof(int32_t), ne_get_rows*sizeof(int32_t),
nb1, nb2, nb3, stream);
+ return false;
+}
+
+// epilogue for a scaled MUL_MAT/MUL_MAT_ID (weight scale in src[2] dense / src[3] id)
+#define CUDA_MUL_MAT_SCALE_BLOCK_SIZE 256
+static __global__ void mul_mat_scale_dense(float * dst, const float * scale,
+ int64_t ne0, int64_t ne1, int64_t ne2, int64_t ne3,
+ int64_t sne0, int64_t sne1, int64_t sne2, int64_t sne3,
+ size_t snb0, size_t snb1, size_t snb2, size_t snb3) {
+ const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x;
+ if (i >= ne0*ne1*ne2*ne3) {
+ return;
+ }
+ const int64_t i0 = i % ne0;
+ const int64_t i1 = (i / ne0) % ne1;
+ const int64_t i2 = (i / (ne0*ne1)) % ne2;
+ const int64_t i3 = i / (ne0*ne1*ne2);
+ const size_t si = (i0 % sne0)*snb0 + (i1 % sne1)*snb1 + (i2 % sne2)*snb2 + (i3 % sne3)*snb3;
+ dst[i] *= *(const float *) ((const char *) scale + si);
+}
+
+// strides are in bytes; channel_stride is zero for per-expert scalar scales
+static __global__ void mul_mat_scale_id(float * dst, const float * scale, const int32_t * ids,
+ int64_t ne0, int64_t ne1, int64_t ne2, int64_t ids_s0, int64_t ids_s1,
+ size_t channel_stride, size_t expert_stride) {
+ const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x;
+ if (i >= ne0*ne1*ne2) {
+ return;
+ }
+ const int64_t i0 = i % ne0;
+ const int64_t i1 = (i / ne0) % ne1;
+ const int64_t i2 = i / (ne0*ne1);
+ const int64_t expert = ids[i1*ids_s0 + i2*ids_s1];
+ if (expert < 0) {
+ return;
+ }
+ dst[i] *= *(const float *) ((const char *) scale + i0*channel_stride + expert*expert_stride);
+}
+
+static void ggml_cuda_op_mul_mat_scale(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
+ const bool is_id = dst->op == GGML_OP_MUL_MAT_ID;
+ const ggml_tensor * scale = ggml_get_src_scale(dst, 0);
+ GGML_ASSERT(scale != NULL && scale->type == GGML_TYPE_F32);
+ GGML_ASSERT(ggml_is_contiguous(dst));
+
+ float * dst_d = (float *) dst->data;
+ const float * scale_d = (const float *) scale->data;
+ cudaStream_t stream = ctx.stream();
+
+ const int64_t ne0 = dst->ne[0], ne1 = dst->ne[1], ne2 = dst->ne[2], ne3 = dst->ne[3];
+
+ // per-expert scale is indexed through ids (scalar [n_expert] or 2D per-channel [n_out, n_expert]);
+ // per-tensor / dense scale is a plain broadcast
+ const bool per_channel_expert = is_id && scale->ne[0] == ne0 && scale->ne[1] == dst->src[0]->ne[2];
+ const bool per_expert = is_id && (ggml_nelements(scale) == dst->src[0]->ne[2] || per_channel_expert);
+
+ if (per_expert) {
+ const ggml_tensor * ids = dst->src[2];
+ const int64_t total = ne0*ne1*ne2;
+ const int64_t blocks = (total + CUDA_MUL_MAT_SCALE_BLOCK_SIZE - 1) / CUDA_MUL_MAT_SCALE_BLOCK_SIZE;
+ mul_mat_scale_id<<<blocks, CUDA_MUL_MAT_SCALE_BLOCK_SIZE, 0, stream>>>(
+ dst_d, scale_d, (const int32_t *) ids->data, ne0, ne1, ne2,
+ ids->nb[0]/sizeof(int32_t), ids->nb[1]/sizeof(int32_t),
+ per_channel_expert ? scale->nb[0] : 0, per_channel_expert ? scale->nb[1] : scale->nb[0]);
+ } else {
+ const int64_t total = ne0*ne1*ne2*ne3;
+ const int64_t blocks = (total + CUDA_MUL_MAT_SCALE_BLOCK_SIZE - 1) / CUDA_MUL_MAT_SCALE_BLOCK_SIZE;
+ mul_mat_scale_dense<<<blocks, CUDA_MUL_MAT_SCALE_BLOCK_SIZE, 0, stream>>>(
+ dst_d, scale_d, ne0, ne1, ne2, ne3,
+ scale->ne[0], scale->ne[1], scale->ne[2], scale->ne[3],
+ scale->nb[0], scale->nb[1], scale->nb[2], scale->nb[3]);
+ }
+ CUDA_CHECK(cudaGetLastError());
}
static bool ggml_cuda_compute_forward(ggml_backend_cuda_context & ctx, struct ggml_tensor * dst) {
@@ -2662,12 +2764,18 @@ static bool ggml_cuda_compute_forward(ggml_backend_cuda_context & ctx, struct gg
case GGML_OP_RMS_NORM_BACK:
ggml_cuda_op_rms_norm_back(ctx, dst);
break;
- case GGML_OP_MUL_MAT:
- ggml_cuda_mul_mat(ctx, dst->src[0], dst->src[1], dst);
- break;
- case GGML_OP_MUL_MAT_ID:
- ggml_cuda_mul_mat_id(ctx, dst);
- break;
+ case GGML_OP_MUL_MAT: {
+ const bool scale_folded = ggml_cuda_mul_mat(ctx, dst->src[0], dst->src[1], dst);
+ if (ggml_get_src_scale(dst, 0) != NULL && !scale_folded) {
+ ggml_cuda_op_mul_mat_scale(ctx, dst);
+ }
+ } break;
+ case GGML_OP_MUL_MAT_ID: {
+ const bool scale_folded = ggml_cuda_mul_mat_id(ctx, dst);
+ if (ggml_get_src_scale(dst, 0) != NULL && !scale_folded) {
+ ggml_cuda_op_mul_mat_scale(ctx, dst);
+ }
+ } break;
case GGML_OP_OUT_PROD:
ggml_cuda_out_prod(ctx, dst);
break;
diff --git a/ggml/src/ggml-cuda/mmq.cu b/ggml/src/ggml-cuda/mmq.cu
index 3623119d9..ecdeb3466 100644
--- a/ggml/src/ggml-cuda/mmq.cu
+++ b/ggml/src/ggml-cuda/mmq.cu
@@ -169,7 +169,7 @@ void ggml_cuda_mul_mat_q(
GGML_ASSERT(!ids || ids->type == GGML_TYPE_I32); // Optional, used for batched GGML_MUL_MAT_ID.
// Fused gate/up/GLU: src0 is the up weight, dst the GLU output.
- // Fused scale (no gate): dst is the MUL that applies the weight scale to the MUL_MAT.
+ // Fused scale (no gate): dst is the MUL_MAT whose attached weight scale is applied.
const ggml_tensor * gate = fusion ? fusion->gate : nullptr;
ggml_cuda_mm_fusion_args_device fusion_device = {};
if (fusion) {
@@ -236,7 +236,7 @@ void ggml_cuda_mul_mat_q(
const bool fallback = ggml_cuda_mmq_needs_fallback(ne01);
- // With fusion dst is the GLU output or the scale node, not a MUL_MAT; the src1 precision requested by the fused MUL_MATs is in the fusion args.
+ // With fusion dst can be the GLU output, not a MUL_MAT; the src1 precision requested by the fused MUL_MATs is in the fusion args.
const ggml_prec prec_src1 = ggml_cuda_mmq_get_prec_src1(src0->type, fusion ? fusion->prec_src1 : ggml_prec_get_src(dst, 1), cc);
const bool use_native_fp4 = prec_src1 == GGML_PREC_Q4;
diff --git a/ggml/src/ggml-cuda/mmq.cuh b/ggml/src/ggml-cuda/mmq.cuh
index 5030ae1da..637e91363 100644
--- a/ggml/src/ggml-cuda/mmq.cuh
+++ b/ggml/src/ggml-cuda/mmq.cuh
@@ -467,7 +467,7 @@ template <ggml_type type, int J, bool fallback, ggml_prec prec_src1> static __de
const ggml_glu_op glu_op, const float glu_limit, const float * __restrict__ x_scale, const float * __restrict__ gate_scale) {
GGML_UNUSED_VARS(glu_op, glu_limit, gate_scale);
- // NVFP4 weight scale of a fused MUL (per tensor, or of the expert of this tile), 1 without.
+ // NVFP4 weight scale (per tensor, or of the expert of this tile), 1 without.
const float scale = type == GGML_TYPE_NVFP4 && x_scale ? *x_scale : 1.0f;
constexpr int warp_size = ggml_cuda_get_physical_warp_size();
@@ -528,7 +528,7 @@ static __device__ __forceinline__ void ggml_cuda_mmq_write_back_mma(
const bool y_scale_used = type == GGML_TYPE_NVFP4 && y_scale != nullptr;
- // NVFP4: load the weight scales once, 1 without scales. Without fusion x_scale is the scale of a fused MUL.
+ // NVFP4: load the weight scales once, 1 without scales. Without fusion x_scale is the weight scale attached to the MUL_MAT.
constexpr bool fused_nvfp4 = has_fusion && type == GGML_TYPE_NVFP4;
const float scale_up = type == GGML_TYPE_NVFP4 && x_scale ? *x_scale : 1.0f;
const float scale_gate = fused_nvfp4 && gate_scale ? *gate_scale : 1.0f;
diff --git a/ggml/src/ggml-et/ggml-et.cpp b/ggml/src/ggml-et/ggml-et.cpp
index 57bcb6187..7abf7641a 100644
--- a/ggml/src/ggml-et/ggml-et.cpp
+++ b/ggml/src/ggml-et/ggml-et.cpp
@@ -868,6 +868,10 @@ static bool et_ggml_is_row_contiguous(const ggml_tensor * t) {
static bool ggml_backend_et_device_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
GGML_UNUSED(dev);
+ if (ggml_get_src_scale(op, 0)) {
+ return false;
+ }
+
bool supported = false;
switch (op->op) {
case GGML_OP_CUMSUM:
diff --git a/ggml/src/ggml-hexagon/ggml-hexagon.cpp b/ggml/src/ggml-hexagon/ggml-hexagon.cpp
index b61e1f57d..b5f05b8f1 100644
--- a/ggml/src/ggml-hexagon/ggml-hexagon.cpp
+++ b/ggml/src/ggml-hexagon/ggml-hexagon.cpp
@@ -3233,6 +3233,10 @@ static bool ggml_backend_hexagon_device_supports_op(ggml_backend_dev_t dev, cons
return false;
}
+ if (ggml_get_src_scale(op, 0)) {
+ return false;
+ }
+
bool supp = false;
if (ggml_op_is_empty(op->op) || ggml_is_empty(op)) {
supp = true;
diff --git a/ggml/src/ggml-impl.h b/ggml/src/ggml-impl.h
index 8288e0a35..0499b7660 100644
--- a/ggml/src/ggml-impl.h
+++ b/ggml/src/ggml-impl.h
@@ -104,6 +104,16 @@ static inline bool ggml_impl_is_view(const struct ggml_tensor * t) {
return t->view_src != NULL;
}
+// scale of src[i] set by ggml_mul_mat_ext / ggml_mul_mat_id_ext
+static inline const struct ggml_tensor * ggml_get_src_scale(const struct ggml_tensor * op, int i) {
+ GGML_ASSERT(i == 0 || i == 1);
+ switch (op->op) {
+ case GGML_OP_MUL_MAT: return op->src[2 + i];
+ case GGML_OP_MUL_MAT_ID: return op->src[3 + i];
+ default: return NULL;
+ }
+}
+
static inline float ggml_compute_softplus_f32(float input) {
return (input > 20.0f) ? input : log1pf(expf(input));
}
diff --git a/ggml/src/ggml-metal/ggml-metal-device.cpp b/ggml/src/ggml-metal/ggml-metal-device.cpp
index 47a1b7c07..c3d4f8ef5 100644
--- a/ggml/src/ggml-metal/ggml-metal-device.cpp
+++ b/ggml/src/ggml-metal/ggml-metal-device.cpp
@@ -97,6 +97,17 @@ ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_cpy(ggml_metal_l
return res;
}
+ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mat_scale(ggml_metal_library_t lib) {
+ const char * name = "kernel_mul_mat_scale";
+
+ ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
+ if (!res.pipeline) {
+ res = ggml_metal_library_compile_pipeline(lib, name, name, nullptr);
+ }
+
+ return res;
+}
+
ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_pool_1d(ggml_metal_library_t lib, const ggml_tensor * op, ggml_op_pool op_pool) {
GGML_ASSERT(ggml_is_contiguous(op->src[0]));
GGML_ASSERT(op->src[0]->type == GGML_TYPE_F32 && op->src[0]->type == op->type);
diff --git a/ggml/src/ggml-metal/ggml-metal-device.h b/ggml/src/ggml-metal/ggml-metal-device.h
index c0466d62f..c370ce9a7 100644
--- a/ggml/src/ggml-metal/ggml-metal-device.h
+++ b/ggml/src/ggml-metal/ggml-metal-device.h
@@ -109,6 +109,7 @@ struct ggml_metal_pipeline_with_params ggml_metal_library_compile_pipeline(ggml_
struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_base (ggml_metal_library_t lib, enum ggml_op op);
struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_cpy (ggml_metal_library_t lib, enum ggml_type tsrc, enum ggml_type tdst);
+struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mat_scale (ggml_metal_library_t lib);
struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_pool_1d (ggml_metal_library_t lib, const struct ggml_tensor * op, enum ggml_op_pool op_pool);
struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_pool_2d (ggml_metal_library_t lib, const struct ggml_tensor * op, enum ggml_op_pool op_pool);
struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_get_rows (ggml_metal_library_t lib, enum ggml_type tsrc);
diff --git a/ggml/src/ggml-metal/ggml-metal-fusion.cpp b/ggml/src/ggml-metal/ggml-metal-fusion.cpp
index 7b55a301e..397d8ce21 100644
--- a/ggml/src/ggml-metal/ggml-metal-fusion.cpp
+++ b/ggml/src/ggml-metal/ggml-metal-fusion.cpp
@@ -4,6 +4,7 @@
#include "ggml-metal-device.h"
#include "ggml-backend-impl.h"
+#include "ggml-impl.h"
#include <algorithm>
#include <cstddef>
@@ -792,6 +793,10 @@ static bool ggml_metal_fusion_check_mul_mat_add(
const ggml_tensor * mm = nodes[0];
const ggml_tensor * add = nodes[1];
+ if (ggml_get_src_scale(mm, 0) != nullptr) {
+ return false;
+ }
+
if (ggml_metal_mul_mat_add_operand(mm, add) == nullptr ||
!ggml_metal_op_mul_mat_may_use_mma(mm)) {
return false;
diff --git a/ggml/src/ggml-metal/ggml-metal-impl.h b/ggml/src/ggml-metal/ggml-metal-impl.h
index c0042a8c1..a8de8f7f6 100644
--- a/ggml/src/ggml-metal/ggml-metal-impl.h
+++ b/ggml/src/ggml-metal/ggml-metal-impl.h
@@ -290,6 +290,19 @@ typedef struct {
size_t nb21;
} ggml_metal_kargs_add_id;
+typedef struct {
+ int64_t ne0;
+ int64_t ne2;
+ uint64_t nb1;
+ uint64_t nb2;
+ uint64_t nb3;
+ uint64_t s_nb0;
+ uint64_t s_nb1;
+ uint64_t ids_nb0;
+ uint64_t ids_nb1;
+ int32_t use_ids;
+} ggml_metal_kargs_mul_mat_scale;
+
typedef struct {
int32_t ne00;
int32_t ne01;
diff --git a/ggml/src/ggml-metal/ggml-metal-ops.cpp b/ggml/src/ggml-metal/ggml-metal-ops.cpp
index f2439bdb2..81b6237ac 100644
--- a/ggml/src/ggml-metal/ggml-metal-ops.cpp
+++ b/ggml/src/ggml-metal/ggml-metal-ops.cpp
@@ -388,10 +388,12 @@ static int ggml_metal_op_encode_impl(ggml_metal_op_t ctx, int idx) {
case GGML_OP_MUL_MAT:
{
n_fuse = ggml_metal_op_mul_mat(ctx, idx);
+ ggml_metal_op_mul_mat_scale(ctx, idx);
} break;
case GGML_OP_MUL_MAT_ID:
{
n_fuse = ggml_metal_op_mul_mat_id(ctx, idx);
+ ggml_metal_op_mul_mat_scale(ctx, idx);
} break;
case GGML_OP_GET_ROWS:
{
@@ -3223,6 +3225,58 @@ int ggml_metal_op_add_id(ggml_metal_op_t ctx, int idx) {
return 1;
}
+int ggml_metal_op_mul_mat_scale(ggml_metal_op_t ctx, int idx) {
+ ggml_tensor * op = ctx->node(idx);
+
+ const ggml_tensor * scale = ggml_get_src_scale(op, 0);
+ if (scale == nullptr) {
+ return 1;
+ }
+
+ ggml_metal_library_t lib = ctx->lib;
+ ggml_metal_encoder_t enc = ctx->enc;
+
+ GGML_ASSERT(scale->type == GGML_TYPE_F32);
+ GGML_ASSERT(op->type == GGML_TYPE_F32);
+ GGML_ASSERT(ggml_is_contiguous_rows(op));
+
+ const ggml_tensor * src0 = op->src[0];
+ const ggml_tensor * ids = op->op == GGML_OP_MUL_MAT_ID ? op->src[2] : nullptr;
+
+ const bool per_channel_expert = ids && scale->ne[0] == op->ne[0] && scale->ne[1] == src0->ne[2];
+ const bool per_expert = ids && (ggml_nelements(scale) == src0->ne[2] || per_channel_expert);
+ const bool per_channel = per_expert ? per_channel_expert : scale->ne[0] == op->ne[0] && ggml_nelements(scale) > 1;
+
+ ggml_metal_kargs_mul_mat_scale args = {
+ /*.ne0 =*/ op->ne[0],
+ /*.ne2 =*/ op->ne[2],
+ /*.nb1 =*/ op->nb[1],
+ /*.nb2 =*/ op->nb[2],
+ /*.nb3 =*/ op->nb[3],
+ /*.s_nb0 =*/ per_channel ? scale->nb[0] : 0,
+ /*.s_nb1 =*/ per_expert ? (per_channel_expert ? scale->nb[1] : scale->nb[0]) : 0,
+ /*.ids_nb0 =*/ per_expert ? ids->nb[0] : 0,
+ /*.ids_nb1 =*/ per_expert ? ids->nb[1] : 0,
+ /*.use_ids =*/ per_expert,
+ };
+
+ ggml_metal_op_concurrency_reset(ctx);
+
+ auto pipeline = ggml_metal_library_get_pipeline_mul_mat_scale(lib);
+
+ ggml_metal_encoder_set_pipeline(enc, pipeline);
+ ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
+ ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(scale), 1);
+ ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(per_expert ? ids : scale), 2);
+ ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 3);
+
+ const int nth = std::min<int64_t>(ggml_metal_pipeline_max_theads_per_threadgroup(pipeline), op->ne[0]);
+
+ ggml_metal_encoder_dispatch_threadgroups(enc, op->ne[1], op->ne[2]*op->ne[3], 1, nth, 1, 1);
+
+ return 1;
+}
+
bool ggml_metal_op_flash_attn_ext_use_vec(const ggml_tensor * op) {
assert(op->op == GGML_OP_FLASH_ATTN_EXT);
diff --git a/ggml/src/ggml-metal/ggml-metal-ops.h b/ggml/src/ggml-metal/ggml-metal-ops.h
index 2330bff1e..a775e88ec 100644
--- a/ggml/src/ggml-metal/ggml-metal-ops.h
+++ b/ggml/src/ggml-metal/ggml-metal-ops.h
@@ -83,6 +83,7 @@ int ggml_metal_op_pool_2d (ggml_metal_op_t ctx, int idx);
int ggml_metal_op_fwht (ggml_metal_op_t ctx, int idx);
int ggml_metal_op_mul_mat (ggml_metal_op_t ctx, int idx);
int ggml_metal_op_mul_mat_id (ggml_metal_op_t ctx, int idx);
+int ggml_metal_op_mul_mat_scale (ggml_metal_op_t ctx, int idx);
int ggml_metal_op_add_id (ggml_metal_op_t ctx, int idx);
int ggml_metal_op_flash_attn_ext (ggml_metal_op_t ctx, int idx);
int ggml_metal_op_bin (ggml_metal_op_t ctx, int idx);
diff --git a/ggml/src/ggml-metal/kernels/binbcast.metal b/ggml/src/ggml-metal/kernels/binbcast.metal
index e125b7a03..bfc0f1cc8 100644
--- a/ggml/src/ggml-metal/kernels/binbcast.metal
+++ b/ggml/src/ggml-metal/kernels/binbcast.metal
@@ -205,6 +205,35 @@ kernel void kernel_add_id(
}
}
+kernel void kernel_mul_mat_scale(
+ constant ggml_metal_kargs_mul_mat_scale & args,
+ device const char * scale,
+ device const char * ids,
+ device char * dst,
+ uint3 tgpig[[threadgroup_position_in_grid]],
+ ushort3 tpitg[[thread_position_in_threadgroup]],
+ ushort3 ntg[[threads_per_threadgroup]]) {
+ const int i1 = tgpig.x;
+ const int i2 = tgpig.y % args.ne2;
+ const int i3 = tgpig.y / args.ne2;
+
+ device const char * s = scale;
+
+ if (args.use_ids) {
+ const int32_t expert = *((device const int32_t *) (ids + i1*args.ids_nb0 + i2*args.ids_nb1));
+ if (expert < 0) {
+ return;
+ }
+ s += expert*args.s_nb1;
+ }
+
+ device float * dst_row = (device float *) (dst + i1*args.nb1 + i2*args.nb2 + i3*args.nb3);
+
+ for (int i0 = tpitg.x; i0 < args.ne0; i0 += ntg.x) {
+ dst_row[i0] *= *((device const float *) (s + i0*args.s_nb0));
+ }
+}
+
template<typename T>
kernel void kernel_repeat(
constant ggml_metal_kargs_repeat & args,
diff --git a/ggml/src/ggml-opencl/ggml-opencl.cpp b/ggml/src/ggml-opencl/ggml-opencl.cpp
index a3f42d946..40d2934f1 100644
--- a/ggml/src/ggml-opencl/ggml-opencl.cpp
+++ b/ggml/src/ggml-opencl/ggml-opencl.cpp
@@ -12746,6 +12746,10 @@ static bool ggml_opencl_supports_op(ggml_backend_dev_t dev, const struct ggml_te
return false;
}
+ if (ggml_get_src_scale(op, 0)) {
+ return false;
+ }
+
switch (op->op) {
case GGML_OP_NONE:
return true;
diff --git a/ggml/src/ggml-openvino/ggml-openvino.cpp b/ggml/src/ggml-openvino/ggml-openvino.cpp
index f34a4b77c..f018c271e 100644
--- a/ggml/src/ggml-openvino/ggml-openvino.cpp
+++ b/ggml/src/ggml-openvino/ggml-openvino.cpp
@@ -2409,6 +2409,10 @@ struct supports_op_tally {
} // namespace
static bool ggml_backend_openvino_device_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
+ if (ggml_get_src_scale(op, 0)) {
+ return false;
+ }
+
auto res = ggml_backend_openvino_device_supports_op_impl(dev, op);
const bool supported = res.is_supported;
if (!supported) {
diff --git a/ggml/src/ggml-sycl/ggml-sycl.cpp b/ggml/src/ggml-sycl/ggml-sycl.cpp
index 6214085ad..359b9ccda 100644
--- a/ggml/src/ggml-sycl/ggml-sycl.cpp
+++ b/ggml/src/ggml-sycl/ggml-sycl.cpp
@@ -7344,6 +7344,10 @@ static ggml_backend_buffer_t ggml_backend_sycl_device_buffer_from_host_ptr(ggml_
}
static bool do_ggml_backend_sycl_device_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
+ if (ggml_get_src_scale(op, 0)) {
+ return false;
+ }
+
ggml_backend_sycl_device_context *sycl_ctx =
(ggml_backend_sycl_device_context *)dev->context;
int device = sycl_ctx->device;
diff --git a/ggml/src/ggml-vulkan/ggml-vulkan.cpp b/ggml/src/ggml-vulkan/ggml-vulkan.cpp
index ba1a648a8..bd61086d7 100644
--- a/ggml/src/ggml-vulkan/ggml-vulkan.cpp
+++ b/ggml/src/ggml-vulkan/ggml-vulkan.cpp
@@ -16728,6 +16728,10 @@ static bool ggml_backend_vk_device_supports_op(ggml_backend_dev_t dev, const ggm
ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
const vk_device& device = ggml_vk_get_device(ctx->device);
+ if (ggml_get_src_scale(op, 0)) {
+ return false;
+ }
+
const bool uses_bda = (op->op == GGML_OP_IM2COL || op->op == GGML_OP_IM2COL_3D) &&
device->shader_int64 && device->buffer_device_address;
diff --git a/ggml/src/ggml-webgpu/ggml-webgpu.cpp b/ggml/src/ggml-webgpu/ggml-webgpu.cpp
index 56447c4e5..c7bc2d62c 100644
--- a/ggml/src/ggml-webgpu/ggml-webgpu.cpp
+++ b/ggml/src/ggml-webgpu/ggml-webgpu.cpp
@@ -5012,6 +5012,10 @@ static bool ggml_backend_webgpu_device_supports_op(ggml_backend_dev_t dev, const
ggml_tensor * src1 = op->src[1];
ggml_tensor * src2 = op->src[2];
+ if (ggml_get_src_scale(op, 0)) {
+ return false;
+ }
+
// on smaller devices (or CI), tensors may be larger than the max storage buffer size
if (ggml_nbytes(op) > ctx->webgpu_global_ctx->capabilities.limits.maxStorageBufferBindingSize ||
(src0 != nullptr &&
diff --git a/ggml/src/ggml-zdnn/ggml-zdnn.cpp b/ggml/src/ggml-zdnn/ggml-zdnn.cpp
index c4dc23cde..59e105807 100644
--- a/ggml/src/ggml-zdnn/ggml-zdnn.cpp
+++ b/ggml/src/ggml-zdnn/ggml-zdnn.cpp
@@ -100,6 +100,10 @@ static bool ggml_zdnn_is_tensor_own(const ggml_tensor * tensor) {
}
static bool ggml_zdnn_supports_op(const ggml_backend_zdnn_device_context * ctx_dev, const ggml_tensor * op) {
+ if (ggml_get_src_scale(op, 0)) {
+ return false;
+ }
+
switch (op->op) {
case GGML_OP_NONE:
case GGML_OP_RESHAPE:
diff --git a/ggml/src/ggml-zendnn/ggml-zendnn.cpp b/ggml/src/ggml-zendnn/ggml-zendnn.cpp
index c25aab552..05bf41d99 100644
--- a/ggml/src/ggml-zendnn/ggml-zendnn.cpp
+++ b/ggml/src/ggml-zendnn/ggml-zendnn.cpp
@@ -698,6 +698,10 @@ static bool ggml_zendnn_adaptive_fallback_enabled() {
}
static bool ggml_backend_zendnn_device_supports_op(ggml_backend_dev_t dev, const struct ggml_tensor * op) {
+ if (ggml_get_src_scale(op, 0)) {
+ return false;
+ }
+
switch (op->op) {
case GGML_OP_NONE:
case GGML_OP_RESHAPE:
diff --git a/ggml/src/ggml.c b/ggml/src/ggml.c
index e3d5819af..912e8653a 100644
--- a/ggml/src/ggml.c
+++ b/ggml/src/ggml.c
@@ -1376,6 +1376,18 @@ bool ggml_is_quantized(enum ggml_type type) {
return type_traits[type].is_quantized;
}
+bool ggml_needs_scale(enum ggml_type type) {
+ assert(type >= 0);
+ assert(type < GGML_TYPE_COUNT);
+ switch (type) {
+ case GGML_TYPE_NVFP4:
+ case GGML_TYPE_F8_E4M3:
+ return true;
+ default:
+ return false;
+ }
+}
+
const char * ggml_op_name(enum ggml_op op) {
return GGML_OP_NAME[op];
}
@@ -3386,8 +3398,31 @@ struct ggml_tensor * ggml_mul_mat(
struct ggml_context * ctx,
struct ggml_tensor * a,
struct ggml_tensor * b) {
+ if (ggml_needs_scale(a->type)) {
+ GGML_LOG_ERROR("%s: tensor type %s requires a weight scale; use ggml_mul_mat_ext instead\n",
+ __func__, ggml_type_name(a->type));
+ GGML_ABORT("fatal error");
+ }
+
+ return ggml_mul_mat_ext(ctx, a, b, NULL, NULL);
+}
+
+struct ggml_tensor * ggml_mul_mat_ext(
+ struct ggml_context * ctx,
+ struct ggml_tensor * a,
+ struct ggml_tensor * b,
+ struct ggml_tensor * scale_weight,
+ struct ggml_tensor * scale_activations) {
GGML_ASSERT(ggml_can_mul_mat(a, b));
GGML_ASSERT(!ggml_is_transposed(a));
+ GGML_ASSERT(!ggml_needs_scale(a->type) || scale_weight != NULL);
+ if (ggml_needs_scale(b->type)) {
+ GGML_LOG_ERROR("%s: scaled tensor type %s currently cannot be used as the activation tensor\n",
+ __func__, ggml_type_name(b->type));
+ GGML_ABORT("fatal error");
+ }
+ GGML_ASSERT(scale_weight == NULL || scale_weight->type == GGML_TYPE_F32);
+ GGML_ASSERT(scale_activations == NULL || scale_activations->type == GGML_TYPE_F32);
const int64_t ne[4] = { a->ne[1], b->ne[1], b->ne[2], b->ne[3] };
struct ggml_tensor * result = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne);
@@ -3396,6 +3431,16 @@ struct ggml_tensor * ggml_mul_mat(
result->src[0] = a;
result->src[1] = b;
+ if (scale_weight) {
+ GGML_ASSERT(ggml_is_scalar(scale_weight) || (ggml_is_vector(scale_weight) && scale_weight->ne[0] == a->ne[1]));
+ result->src[2] = scale_weight;
+ }
+
+ if (scale_activations) {
+ GGML_ASSERT(ggml_is_scalar(scale_activations));
+ result->src[3] = scale_activations;
+ }
+
return result;
}
@@ -3430,8 +3475,32 @@ struct ggml_tensor * ggml_mul_mat_id(
struct ggml_tensor * as,
struct ggml_tensor * b,
struct ggml_tensor * ids) {
+ if (ggml_needs_scale(as->type)) {
+ GGML_LOG_ERROR("%s: tensor type %s requires a weight scale; use ggml_mul_mat_id_ext instead\n",
+ __func__, ggml_type_name(as->type));
+ GGML_ABORT("fatal error");
+ }
+
+ return ggml_mul_mat_id_ext(ctx, as, b, ids, NULL, NULL);
+}
+
+struct ggml_tensor * ggml_mul_mat_id_ext(
+ struct ggml_context * ctx,
+ struct ggml_tensor * as,
+ struct ggml_tensor * b,
+ struct ggml_tensor * ids,
+ struct ggml_tensor * scale_weight,
+ struct ggml_tensor * scale_activations) {
GGML_ASSERT(!ggml_is_transposed(as));
GGML_ASSERT(ids->type == GGML_TYPE_I32);
+ if (ggml_needs_scale(b->type)) {
+ GGML_LOG_ERROR("%s: scaled tensor type %s currently cannot be used as the activation tensor\n",
+ __func__, ggml_type_name(b->type));
+ GGML_ABORT("fatal error");
+ }
+ GGML_ASSERT(!ggml_needs_scale(as->type) || scale_weight != NULL);
+ GGML_ASSERT(scale_weight == NULL || scale_weight->type == GGML_TYPE_F32);
+ GGML_ASSERT(scale_activations == NULL || scale_activations->type == GGML_TYPE_F32);
GGML_ASSERT(as->ne[3] == 1); // as is 3d (one matrix per expert)
GGML_ASSERT(b->ne[3] == 1); // b is 3d
@@ -3448,6 +3517,23 @@ struct ggml_tensor * ggml_mul_mat_id(
result->src[1] = b;
result->src[2] = ids;
+ if (scale_weight) {
+ struct ggml_tensor * s = scale_weight;
+ const bool per_tensor = ggml_is_scalar(s);
+ const bool per_expert = ggml_is_vector(s) && s->ne[0] == as->ne[2];
+ const bool per_ch_exp = s->ne[0] == as->ne[1] && s->ne[1] == as->ne[2] && s->ne[2] == 1 && s->ne[3] == 1;
+ GGML_ASSERT(per_tensor || per_expert || per_ch_exp);
+ result->src[3] = s;
+ }
+
+ if (scale_activations) {
+ const struct ggml_tensor * s = scale_activations;
+ const bool per_tensor = ggml_is_scalar(s);
+ const bool per_expert = ggml_is_vector(s) && s->ne[0] == as->ne[2];
+ GGML_ASSERT(per_tensor || per_expert);
+ result->src[4] = scale_activations;
+ }
+
return result;
}
diff --git a/src/llama-arch.cpp b/src/llama-arch.cpp
index 327e62571..fe0b808b3 100644
--- a/src/llama-arch.cpp
+++ b/src/llama-arch.cpp
@@ -1583,6 +1583,9 @@ bool llm_arch_supports_sm_tensor(const llm_arch & arch) {
case LLM_ARCH_ZAYA1:
case LLM_ARCH_GROK:
case LLM_ARCH_MPT:
+ case LLM_ARCH_BLOOM: // ALiBi
+ case LLM_ARCH_JAIS: // ALiBi
+ case LLM_ARCH_REFACT: // ALiBi
case LLM_ARCH_PLAMO2:
case LLM_ARCH_MINICPM3:
case LLM_ARCH_GEMMA3N:
diff --git a/src/llama-context.cpp b/src/llama-context.cpp
index aa0b93045..35308bf3e 100644
--- a/src/llama-context.cpp
+++ b/src/llama-context.cpp
@@ -3076,6 +3076,7 @@ llm_graph_params llama_context::graph_params(
/*.layer_inp_src =*/ supports_embd_row() ? cparams.ctx_other->get_layer_inp_dev() : nullptr,
/*.h_nextn_dev =*/ cparams.embeddings_nextn_dev ? h_nextn_dev : nullptr,
/*.h_nextn_src =*/ supports_state_row() ? cparams.ctx_state_src->get_h_nextn_dev() : nullptr,
+ /*.w_scales =*/ &model.w_scales,
/*.samplers =*/ sampling.samplers,
/*.n_outputs =*/ n_outputs,
/*.cb =*/ graph_get_cb(),
diff --git a/src/llama-graph.cpp b/src/llama-graph.cpp
index a7cae138a..31200d0d1 100644
--- a/src/llama-graph.cpp
+++ b/src/llama-graph.cpp
@@ -1761,6 +1761,7 @@ llm_graph_context::llm_graph_context(const llm_graph_params & params) :
layer_inp_src (params.layer_inp_src),
h_nextn_dev (params.h_nextn_dev),
h_nextn_src (params.h_nextn_src),
+ w_scales (params.w_scales),
samplers (params.samplers),
cb_func (params.cb),
res (params.res),
@@ -1845,29 +1846,37 @@ ggml_tensor * llm_graph_context::build_rope_indexer(ggml_tensor * cur, ggml_tens
return rp.n_rot > 0 && rp.n_offs > 0 ? ggml_rope_set_offset(res, rp.n_offs) : res;
}
-ggml_tensor * llm_graph_context::build_lora_mm(
- ggml_tensor * w,
- ggml_tensor * cur,
- ggml_tensor * w_s) const {
- if (!w_s) {
- w_s = model.get_w_scale(w);
+ggml_tensor * llm_graph_context::get_weight_scale(const ggml_tensor * w) const {
+ ggml_tensor * s = w_scales ? w_scales->get(w) : nullptr;
+ // weights shared from the target model
+ if (s == nullptr && cparams.ctx_other) {
+ s = llama_get_model(cparams.ctx_other)->w_scales.get(w);
}
+ return s;
+}
+ggml_tensor * llm_graph_context::get_input_scale(const ggml_tensor * w) const {
+ ggml_tensor * s = w_scales ? w_scales->get_input(w) : nullptr;
+ // weights shared from the target model
+ if (s == nullptr && cparams.ctx_other) {
+ s = llama_get_model(cparams.ctx_other)->w_scales.get_input(w);
+ }
+ return s;
+}
+
+ggml_tensor * llm_graph_context::build_lora_mm(
+ ggml_tensor * w,
+ ggml_tensor * cur) const {
// a Hadamard-folded weight reads the transformed input; a LoRA delta lives in the primal basis and reads `cur`
ggml_tensor * cur_mm = build_hadamard_input(w, cur);
- ggml_tensor * res = ggml_mul_mat(ctx0, w, cur_mm);
+ ggml_tensor * res = ggml_mul_mat_ext(ctx0, w, cur_mm, get_weight_scale(w), get_input_scale(w));
if (w->type == GGML_TYPE_NVFP4) {
ggml_prec_set_acc(res, GGML_PREC_BF16);
}
-
prec_policy.apply(res, w);
- if (w_s) {
- res = ggml_mul(ctx0, res, w_s);
- }
-
for (const auto & lora : *loras) {
llama_adapter_lora_weight * lw = lora.first->get_weight(w);
if (lw == nullptr) {
@@ -1932,13 +1941,16 @@ ggml_tensor * llm_graph_context::build_lora_mm_id(
ggml_tensor * ids,
ggml_tensor * slots,
bool cached) const {
- ggml_tensor * w_s = model.get_w_scale(w);
+ ggml_tensor * w_s = get_weight_scale(w);
+ ggml_tensor * x_s = get_input_scale(w);
// as in build_lora_mm: the folded weight reads the transformed input, the LoRA delta reads `cur`
ggml_tensor * cur_mm = build_hadamard_input(w, cur);
// the experts in the MoE cache are selected by their slots
ggml_tensor * res = slots == nullptr ?
- ggml_mul_mat_id(ctx0, w, cur_mm, ids) :
- ggml_mul_mat_id(ctx0, cached ? moe_cache->get_experts(w) : w, cur_mm, slots);
+ ggml_mul_mat_id_ext(ctx0, w, cur_mm, ids, w_s, x_s) :
+ ggml_mul_mat_id_ext(ctx0, cached ? moe_cache->get_experts(w) : w, cur_mm, slots,
+ w_s ? (cached ? moe_cache->get_scales(w, LLAMA_MOE_CACHE_SCALE_WEIGHT) : w_s) : nullptr,
+ x_s ? (cached ? moe_cache->get_scales(w, LLAMA_MOE_CACHE_SCALE_INPUT) : x_s) : nullptr);
if (w->type == GGML_TYPE_NVFP4) {
ggml_prec_set_acc(res, GGML_PREC_BF16);
@@ -1947,14 +1959,6 @@ ggml_tensor * llm_graph_context::build_lora_mm_id(
// after the NVFP4 default, so that the policy can request a higher precision
prec_policy.apply(res, w);
- if (w_s) {
- const int64_t n_expert = w_s->ne[0];
- const int64_t n_tokens = cur->ne[2];
- ggml_tensor * s = ggml_reshape_3d(ctx0, w_s, 1, n_expert, 1);
- s = ggml_repeat_4d(ctx0, s, 1, n_expert, n_tokens, 1);
- s = ggml_get_rows(ctx0, s, ids);
- res = ggml_mul(ctx0, res, s);
- }
for (const auto & lora : *loras) {
llama_adapter_lora_weight * lw = lora.first->get_weight(w);
if (lw == nullptr) {
@@ -2046,7 +2050,7 @@ llm_graph_qkv llm_graph_context::build_qkv(
if (layer.wqkv) {
// fused QKV path
- ggml_tensor * qkv = build_lora_mm(layer.wqkv, cur, layer.wqkv_s);
+ ggml_tensor * qkv = build_lora_mm(layer.wqkv, cur);
cb(qkv, "wqkv", il);
if (layer.wqkv_b) {
qkv = ggml_add(ctx0, qkv, layer.wqkv_b);
@@ -2084,7 +2088,7 @@ llm_graph_qkv llm_graph_context::build_qkv(
}
} else {
// separate Q/K/V path
- Qcur = build_lora_mm(layer.wq, cur, layer.wq_s);
+ Qcur = build_lora_mm(layer.wq, cur);
if (reshape) {
cb(Qcur, "Qcur", il);
}
@@ -2098,7 +2102,7 @@ llm_graph_qkv llm_graph_context::build_qkv(
Qcur = ggml_clamp(ctx0, Qcur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
cb(Qcur, "Qcur_clamped", il);
}
- Kcur = build_lora_mm(layer.wk, cur, layer.wk_s);
+ Kcur = build_lora_mm(layer.wk, cur);
if (reshape) {
cb(Kcur, "Kcur", il);
}
@@ -2112,7 +2116,7 @@ llm_graph_qkv llm_graph_context::build_qkv(
Kcur = ggml_clamp(ctx0, Kcur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
cb(Kcur, "Kcur_clamped", il);
}
- Vcur = build_lora_mm(layer.wv, cur, layer.wv_s);
+ Vcur = build_lora_mm(layer.wv, cur);
if (reshape) {
cb(Vcur, "Vcur", il);
}
@@ -2147,20 +2151,17 @@ ggml_tensor * llm_graph_context::build_ffn(
ggml_tensor * cur,
ggml_tensor * up,
ggml_tensor * up_b,
- ggml_tensor * up_s,
ggml_tensor * gate,
ggml_tensor * gate_b,
- ggml_tensor * gate_s,
ggml_tensor * down,
ggml_tensor * down_b,
- ggml_tensor * down_s,
ggml_tensor * act_scales,
llm_ffn_op_type type_op,
llm_ffn_gate_type type_gate,
int il) const {
const llm_arch arch = model.arch;
- ggml_tensor * tmp = up ? build_lora_mm(up, cur, up_s) : cur;
+ ggml_tensor * tmp = up ? build_lora_mm(up, cur) : cur;
cb(tmp, "ffn_up", il);
if (up_b) {
@@ -2172,12 +2173,12 @@ ggml_tensor * llm_graph_context::build_ffn(
switch (type_gate) {
case LLM_FFN_SEQ:
{
- cur = build_lora_mm(gate, tmp, gate_s);
+ cur = build_lora_mm(gate, tmp);
cb(cur, "ffn_gate", il);
} break;
case LLM_FFN_PAR:
{
- cur = build_lora_mm(gate, cur, gate_s);
+ cur = build_lora_mm(gate, cur);
cb(cur, "ffn_gate", il);
} break;
}
@@ -2308,7 +2309,7 @@ ggml_tensor * llm_graph_context::build_ffn(
}
if (down) {
- cur = build_lora_mm(down, cur, down_s);
+ cur = build_lora_mm(down, cur);
}
if (down_b) {
@@ -3155,7 +3156,9 @@ ggml_tensor * llm_graph_context::build_attn_mha(
float kq_scale,
int il,
ggml_tensor * bias_table,
- ggml_tensor * bias_indices) const {
+ ggml_tensor * bias_indices,
+ ggml_tensor * k_scale,
+ ggml_tensor * v_scale) const {
const bool v_trans = v->nb[1] > v->nb[2];
// split the batch into streams if needed
@@ -3193,6 +3196,10 @@ ggml_tensor * llm_graph_context::build_attn_mha(
if (use_flash_attn) {
GGML_ASSERT(kq_b == nullptr && "Flash attention does not support dense KQ bias");
+ if (k_scale) {
+ q = ggml_mul(ctx0, q, k_scale);
+ }
+
if (v_trans) {
v = ggml_transpose(ctx0, v);
}
@@ -3235,12 +3242,12 @@ ggml_tensor * llm_graph_context::build_attn_mha(
// v_mla can be applied as a matrix-vector multiplication with broadcasting across dimension 3 == n_tokens.
// However, the code is optimized for dimensions 0 and 1 being large, so this is inefficient.
cur = ggml_reshape_4d(ctx0, cur, v_mla->ne[0], 1, n_head, n_tokens);
- cur = ggml_mul_mat(ctx0, v_mla, cur);
+ cur = build_lora_mm(v_mla, cur);
#else
// It's preferable to do the calculation as a matrix-matrix multiplication with n_tokens in dimension 1.
// The permutations are noops and only change how the tensor data is interpreted.
cur = ggml_permute(ctx0, cur, 0, 2, 1, 3);
- cur = ggml_mul_mat(ctx0, v_mla, cur);
+ cur = build_lora_mm(v_mla, cur);
cb(cur, "fattn_mla", il);
cur = ggml_permute(ctx0, cur, 0, 2, 1, 3);
cur = ggml_cont(ctx0, cur); // Needed because ggml_reshape_2d expects contiguous inputs.
@@ -3248,11 +3255,24 @@ ggml_tensor * llm_graph_context::build_attn_mha(
}
cur = ggml_reshape_2d(ctx0, cur, cur->ne[0]*cur->ne[1], cur->ne[2]*cur->ne[3]);
+ if (v_scale) {
+ cur = ggml_mul(ctx0, cur, v_scale);
+ }
} else {
if (bias_table) {
kq_mask = dense_bias_mask();
}
- ggml_tensor * kq = ggml_mul_mat(ctx0, k, q);
+ // A cache without an exported scale uses a unit scale.
+ if ((ggml_needs_scale(k->type) && !k_scale) || (ggml_needs_scale(v->type) && !v_scale)) {
+ ggml_tensor * unit_scale = ggml_fill(ctx0, ggml_new_tensor_1d(ctx0, GGML_TYPE_F32, 1), 1.0f);
+ if (ggml_needs_scale(k->type) && !k_scale) {
+ k_scale = unit_scale;
+ }
+ if (ggml_needs_scale(v->type) && !v_scale) {
+ v_scale = unit_scale;
+ }
+ }
+ ggml_tensor * kq = ggml_mul_mat_ext(ctx0, k, q, k_scale, nullptr);
cb(kq, "kq", il);
ggml_prec_set_acc(kq, GGML_PREC_BF16);
@@ -3294,12 +3314,12 @@ ggml_tensor * llm_graph_context::build_attn_mha(
cb(v, "v_cont", il);
}
- ggml_tensor * kqv = ggml_mul_mat(ctx0, v, kq);
+ ggml_tensor * kqv = ggml_mul_mat_ext(ctx0, v, kq, v_scale, nullptr);
cb(kqv, "kqv", il);
// for MLA with the absorption optimization, we need to "decompress" from MQA back to MHA
if (v_mla) {
- kqv = ggml_mul_mat(ctx0, v_mla, kqv);
+ kqv = build_lora_mm(v_mla, kqv);
cb(kqv, "kqv_mla", il);
}
@@ -3346,7 +3366,6 @@ ggml_tensor * llm_graph_context::build_attn(
llm_graph_input_attn_no_cache * inp,
ggml_tensor * wo,
ggml_tensor * wo_b,
- ggml_tensor * wo_s,
ggml_tensor * q_cur,
ggml_tensor * k_cur,
ggml_tensor * v_cur,
@@ -3391,7 +3410,7 @@ ggml_tensor * llm_graph_context::build_attn(
cb(cur, "kqv_out", il);
if (wo) {
- cur = build_lora_mm(wo, cur, wo_s);
+ cur = build_lora_mm(wo, cur);
}
if (wo_b) {
@@ -3440,45 +3459,26 @@ llm_graph_input_attn_kv * llm_graph_context::build_attn_inp_kv() const {
static void build_attn_scale_kv_inputs(
ggml_context * ctx,
const llama_kv_cache_context * mctx,
- ggml_tensor * & q,
ggml_tensor * & k,
ggml_tensor * & v,
int32_t il) {
- if (ggml_tensor * scale = mctx->get_k_scale(il)) {
- // Store K/s and move its dequant scale to Q: (Q*s) dot (K/s) = Q dot K.
- q = ggml_mul(ctx, q, scale);
- if (k) {
+ if (k) {
+ if (ggml_tensor * scale = mctx->get_k_scale(il)) {
k = ggml_div(ctx, k, scale);
}
}
if (v) {
- // Store V/s here and restore its dequant scale after the weighted sum.
- ggml_tensor * scale = mctx->get_v_scale(il);
- if (scale) {
+ if (ggml_tensor * scale = mctx->get_v_scale(il)) {
v = ggml_div(ctx, v, scale);
}
}
}
-static ggml_tensor * build_attn_scale_kv_output(
- ggml_context * ctx,
- const llama_kv_cache_context * mctx,
- ggml_tensor * cur,
- int32_t il) {
- if (ggml_tensor * scale = mctx->get_v_scale(il)) {
- // P*(V/s)*s = P*V.
- cur = ggml_mul(ctx, cur, scale);
- }
-
- return cur;
-}
-
ggml_tensor * llm_graph_context::build_attn(
llm_graph_input_attn_kv * inp,
ggml_tensor * wo,
ggml_tensor * wo_b,
- ggml_tensor * wo_s,
ggml_tensor * q_cur,
ggml_tensor * k_cur,
ggml_tensor * v_cur,
@@ -3500,7 +3500,7 @@ ggml_tensor * llm_graph_context::build_attn(
const auto * mctx_cur = inp->mctx;
- build_attn_scale_kv_inputs(ctx0, mctx_cur, q_cur, k_cur, v_cur, il);
+ build_attn_scale_kv_inputs(ctx0, mctx_cur, k_cur, v_cur, il);
// these nodes are added to the graph together so that they are not reordered
// by doing so, the number of splits in the graph is reduced
@@ -3534,8 +3534,8 @@ ggml_tensor * llm_graph_context::build_attn(
const auto & kq_mask = inp->self_attn_mask.raw;
const auto & kv_rows = inp->self_attn_mask.rows;
- ggml_tensor * cur = build_attn_mha(q, k, v, kq_b, kq_mask, kv_rows, sinks, v_mla, 0, kq_scale, il);
- cur = build_attn_scale_kv_output(ctx0, mctx_cur, cur, il);
+ ggml_tensor * cur = build_attn_mha(q, k, v, kq_b, kq_mask, kv_rows, sinks, v_mla, 0, kq_scale, il, nullptr, nullptr,
+ mctx_cur->get_k_scale(il), mctx_cur->get_v_scale(il));
cb(cur, "kqv_out", il);
if (inp->self_v_rot[il]) {
@@ -3543,7 +3543,7 @@ ggml_tensor * llm_graph_context::build_attn(
}
if (wo) {
- cur = build_lora_mm(wo, cur, wo_s);
+ cur = build_lora_mm(wo, cur);
}
if (wo_b) {
@@ -3585,7 +3585,6 @@ ggml_tensor * llm_graph_context::build_attn(
llm_graph_input_attn_k * inp,
ggml_tensor * wo,
ggml_tensor * wo_b,
- ggml_tensor * wo_s,
ggml_tensor * q_cur,
ggml_tensor * k_cur,
ggml_tensor * v_cur,
@@ -3621,7 +3620,7 @@ ggml_tensor * llm_graph_context::build_attn(
cb(cur, "kqv_out", il);
if (wo) {
- cur = build_lora_mm(wo, cur, wo_s);
+ cur = build_lora_mm(wo, cur);
}
if (wo_b) {
@@ -3682,7 +3681,6 @@ ggml_tensor * llm_graph_context::build_attn(
llm_graph_input_attn_k_dsa * inp,
ggml_tensor * wo,
ggml_tensor * wo_b,
- ggml_tensor * wo_s,
ggml_tensor * q_cur,
ggml_tensor * k_cur,
ggml_tensor * v_cur,
@@ -3735,7 +3733,7 @@ ggml_tensor * llm_graph_context::build_attn(
cb(cur, "kqv_out", il);
if (wo) {
- cur = build_lora_mm(wo, cur, wo_s);
+ cur = build_lora_mm(wo, cur);
}
if (wo_b) {
@@ -3751,7 +3749,6 @@ static ggml_tensor * build_attn_iswa_impl(
llm_graph_input_attn_kv_iswa * inp,
ggml_tensor * wo,
ggml_tensor * wo_b,
- ggml_tensor * wo_s,
ggml_tensor * q_cur,
ggml_tensor * k_cur,
ggml_tensor * v_cur,
@@ -3790,7 +3787,7 @@ static ggml_tensor * build_attn_iswa_impl(
const auto * mctx_iswa = inp->mctx;
const auto * mctx_cur = is_swa ? mctx_iswa->get_swa() : mctx_iswa->get_base();
- build_attn_scale_kv_inputs(ctx0, mctx_cur, q_cur, k_cur, v_cur, il);
+ build_attn_scale_kv_inputs(ctx0, mctx_cur, k_cur, v_cur, il);
// these nodes are added to the graph together so that they are not reordered
// by doing so, the number of splits in the graph is reduced
@@ -3829,8 +3826,8 @@ static ggml_tensor * build_attn_iswa_impl(
ggml_tensor * k = use_kv_cur ? k_cur : mctx_cur->get_k(ctx0, il);
ggml_tensor * v = use_kv_cur ? v_cur : mctx_cur->get_v(ctx0, il);
- ggml_tensor * cur = gctx.build_attn_mha(q, k, v, kq_b, kq_mask, kv_rows, sinks, v_mla, 0, kq_scale, il, bias_table, bias_indices);
- cur = build_attn_scale_kv_output(ctx0, mctx_cur, cur, il);
+ ggml_tensor * cur = gctx.build_attn_mha(q, k, v, kq_b, kq_mask, kv_rows, sinks, v_mla, 0, kq_scale, il, bias_table, bias_indices,
+ mctx_cur->get_k_scale(il), mctx_cur->get_v_scale(il));
gctx.cb(cur, "kqv_out", il);
if (v_rot) {
@@ -3838,7 +3835,7 @@ static ggml_tensor * build_attn_iswa_impl(
}
if (wo) {
- cur = gctx.build_lora_mm(wo, cur, wo_s);
+ cur = gctx.build_lora_mm(wo, cur);
}
if (wo_b) {
@@ -3856,7 +3853,6 @@ ggml_tensor * llm_graph_context::build_attn(
llm_graph_input_attn_kv_iswa * inp,
ggml_tensor * wo,
ggml_tensor * wo_b,
- ggml_tensor * wo_s,
ggml_tensor * q_cur,
ggml_tensor * k_cur,
ggml_tensor * v_cur,
@@ -3870,14 +3866,13 @@ ggml_tensor * llm_graph_context::build_attn(
const auto & kq_mask = is_swa ? inp->self_attn_mask_swa.raw : inp->self_attn_mask.raw;
const auto & kv_rows = is_swa ? inp->self_attn_mask_swa.rows : inp->self_attn_mask.rows;
- return build_attn_iswa_impl(*this, inp, wo, wo_b, wo_s, q_cur, k_cur, v_cur, kq_b, kq_mask, kv_rows, sinks, v_mla, kq_scale, il);
+ return build_attn_iswa_impl(*this, inp, wo, wo_b, q_cur, k_cur, v_cur, kq_b, kq_mask, kv_rows, sinks, v_mla, kq_scale, il);
}
ggml_tensor * llm_graph_context::build_attn(
llm_graph_input_attn_k_iswa * inp,
ggml_tensor * wo,
ggml_tensor * wo_b,
- ggml_tensor * wo_s,
ggml_tensor * q_cur,
ggml_tensor * k_cur,
ggml_tensor * v_cur,
@@ -3931,7 +3926,7 @@ ggml_tensor * llm_graph_context::build_attn(
}
if (wo) {
- cur = build_lora_mm(wo, cur, wo_s);
+ cur = build_lora_mm(wo, cur);
}
if (wo_b) {
@@ -3959,7 +3954,6 @@ ggml_tensor * llm_graph_context::build_attn(
llm_graph_input_attn_cross * inp,
ggml_tensor * wo,
ggml_tensor * wo_b,
- ggml_tensor * wo_s,
ggml_tensor * q_cur,
ggml_tensor * k_cur,
ggml_tensor * v_cur,
@@ -3985,7 +3979,7 @@ ggml_tensor * llm_graph_context::build_attn(
cb(cur, "kqv_out", il);
if (wo) {
- cur = build_lora_mm(wo, cur, wo_s);
+ cur = build_lora_mm(wo, cur);
}
if (wo_b) {
@@ -4513,7 +4507,6 @@ ggml_tensor * llm_graph_context::build_attn(
llm_graph_input_mem_hybrid_iswa_rel * inp,
ggml_tensor * wo,
ggml_tensor * wo_b,
- ggml_tensor * wo_s,
ggml_tensor * q_cur,
ggml_tensor * k_cur,
ggml_tensor * v_cur,
@@ -4530,7 +4523,7 @@ ggml_tensor * llm_graph_context::build_attn(
GGML_ASSERT(attn_mask.rows == nullptr);
ggml_tensor * indices = is_swa ? inp->self_rel_pos_swa : inp->self_rel_pos;
- return build_attn_iswa_impl(*this, inp_attn, wo, wo_b, wo_s, q_cur, k_cur, v_cur, nullptr, attn_mask.raw, nullptr,
+ return build_attn_iswa_impl(*this, inp_attn, wo, wo_b, q_cur, k_cur, v_cur, nullptr, attn_mask.raw, nullptr,
sinks, nullptr, kq_scale, il, bias_table, indices);
}
@@ -4545,13 +4538,13 @@ void llm_graph_context::build_dense_out(
GGML_ASSERT(cur != nullptr && "missing t_embd_pooled/t_embd");
if (dense_2) {
- cur = ggml_mul_mat(ctx0, dense_2, cur);
+ cur = build_lora_mm(dense_2, cur);
}
if (dense_2_b) {
cur = ggml_add(ctx0, cur, dense_2_b);
}
if (dense_3) {
- cur = ggml_mul_mat(ctx0, dense_3, cur);
+ cur = build_lora_mm(dense_3, cur);
}
cb(cur, "result_embd_pooled", -1);
res->t_embd_pooled = cur;
@@ -4622,7 +4615,7 @@ void llm_graph_context::build_pooling(
// classification head
// https://github.com/huggingface/transformers/blob/5af7d41e49bbfc8319f462eb45253dcb3863dfb7/src/transformers/models/roberta/modeling_roberta.py#L1566
if (cls) {
- cur = ggml_mul_mat(ctx0, cls, cur);
+ cur = build_lora_mm(cls, cur);
if (cls_b) {
cur = ggml_add(ctx0, cur, cls_b);
}
@@ -4638,7 +4631,7 @@ void llm_graph_context::build_pooling(
// Single layer classification head (direct projection)
// https://github.com/huggingface/transformers/blob/f4fc42216cd56ab6b68270bf80d811614d8d59e4/src/transformers/models/bert/modeling_bert.py#L1476
if (cls_out) {
- cur = ggml_mul_mat(ctx0, cls_out, cur);
+ cur = build_lora_mm(cls_out, cur);
if (cls_out_b) {
cur = ggml_add(ctx0, cur, cls_out_b);
}
diff --git a/src/llama-graph.h b/src/llama-graph.h
index 33b1df4ce..4a1d497b7 100644
--- a/src/llama-graph.h
+++ b/src/llama-graph.h
@@ -24,6 +24,7 @@ struct llama_prec_policy;
struct llama_hadamard;
class llama_moe_cache;
+struct llama_weight_scales;
struct llama_memory_context_i;
@@ -883,6 +884,7 @@ struct llm_graph_params {
ggml_tensor * layer_inp_src = nullptr; // ctx_other's layer input device buffer (read)
ggml_tensor * h_nextn_dev = nullptr; // own h_nextn device buffer (write)
ggml_tensor * h_nextn_src = nullptr; // ctx_state_src's h_nextn device buffer (read)
+ const llama_weight_scales * w_scales = nullptr;
std::map<llama_seq_id, llama_sampler *> samplers;
@@ -1159,6 +1161,7 @@ struct llm_graph_context {
ggml_tensor * layer_inp_src;
ggml_tensor * h_nextn_dev;
ggml_tensor * h_nextn_src;
+ const llama_weight_scales * w_scales;
std::map<llama_seq_id, llama_sampler *> samplers;
@@ -1197,14 +1200,16 @@ struct llm_graph_context {
ggml_tensor * w,
ggml_tensor * cur) const;
- // do mat_mul, while optionally apply lora and per-tensor scale
- // w_s defaults to the weight scale of w
+ // scale tensor of a weight, nullptr if it has none
+ ggml_tensor * get_weight_scale(const ggml_tensor * w) const;
+ ggml_tensor * get_input_scale(const ggml_tensor * w) const;
+
+ // do mat_mul, while optionally apply lora and the weight scale
ggml_tensor * build_lora_mm(
ggml_tensor * w,
- ggml_tensor * cur,
- ggml_tensor * w_s = nullptr) const;
+ ggml_tensor * cur) const;
- // do mat_mul_id, while optionally apply lora and per-expert scale
+ // do mat_mul_id, while optionally apply lora and the weight scale
// if slots is set, the experts are selected by slots instead of ids, from the MoE cache if cached (see llama_moe_cache::build_lookup)
ggml_tensor * build_lora_mm_id(
ggml_tensor * w, // ggml_tensor * as
@@ -1253,13 +1258,10 @@ struct llm_graph_context {
ggml_tensor * cur,
ggml_tensor * up,
ggml_tensor * up_b,
- ggml_tensor * up_s,
ggml_tensor * gate,
ggml_tensor * gate_b,
- ggml_tensor * gate_s,
ggml_tensor * down,
ggml_tensor * down_b,
- ggml_tensor * down_s,
ggml_tensor * act_scales,
llm_ffn_op_type type_op,
llm_ffn_gate_type type_gate,
@@ -1357,7 +1359,9 @@ struct llm_graph_context {
float kq_scale,
int il,
ggml_tensor * bias_table = nullptr, // [n_bins, n_head_q, n_tokens]
- ggml_tensor * bias_indices = nullptr) const;
+ ggml_tensor * bias_indices = nullptr,
+ ggml_tensor * k_scale = nullptr,
+ ggml_tensor * v_scale = nullptr) const;
llm_graph_input_attn_no_cache * build_attn_inp_no_cache() const;
@@ -1365,7 +1369,6 @@ struct llm_graph_context {
llm_graph_input_attn_no_cache * inp,
ggml_tensor * wo,
ggml_tensor * wo_b,
- ggml_tensor * wo_s,
ggml_tensor * q_cur, // [n_embd_head_q, n_head_q, n_tokens]
ggml_tensor * k_cur, // [n_embd_head_k, n_head_k, n_tokens]
ggml_tensor * v_cur, // [n_embd_head_v, n_head_v, n_tokens]
@@ -1381,7 +1384,6 @@ struct llm_graph_context {
llm_graph_input_attn_kv * inp,
ggml_tensor * wo,
ggml_tensor * wo_b,
- ggml_tensor * wo_s,
ggml_tensor * q_cur, // [n_embd_head_q, n_head_q, n_tokens]
ggml_tensor * k_cur, // [n_embd_head_k, n_head_k, n_tokens]
ggml_tensor * v_cur, // [n_embd_head_v, n_head_v, n_tokens]
@@ -1397,7 +1399,6 @@ struct llm_graph_context {
llm_graph_input_attn_k * inp,
ggml_tensor * wo,
ggml_tensor * wo_b,
- ggml_tensor * wo_s,
ggml_tensor * q_cur, // [n_embd_head_q, n_head_q, n_tokens]
ggml_tensor * k_cur, // [n_embd_head_k, n_head_k, n_tokens]
ggml_tensor * v_cur, // [n_embd_head_v, n_head_v, n_tokens]
@@ -1417,7 +1418,6 @@ struct llm_graph_context {
llm_graph_input_attn_k_dsa * inp,
ggml_tensor * wo,
ggml_tensor * wo_b,
- ggml_tensor * wo_s,
ggml_tensor * q_cur, // [n_embd_head_q, n_head_q, n_tokens]
ggml_tensor * k_cur, // [n_embd_head_k, n_head_k, n_tokens]
ggml_tensor * v_cur, // [n_embd_head_v, n_head_v, n_tokens]
@@ -1449,7 +1449,6 @@ struct llm_graph_context {
llm_graph_input_attn_kv_iswa * inp,
ggml_tensor * wo,
ggml_tensor * wo_b,
- ggml_tensor * wo_s,
ggml_tensor * q_cur, // [n_embd_head_q, n_head_q, n_tokens]
ggml_tensor * k_cur, // [n_embd_head_k, n_head_k, n_tokens] optional
ggml_tensor * v_cur, // [n_embd_head_v, n_head_v, n_tokens] optional
@@ -1467,7 +1466,6 @@ struct llm_graph_context {
llm_graph_input_attn_k_iswa * inp,
ggml_tensor * wo,
ggml_tensor * wo_b,
- ggml_tensor * wo_s,
ggml_tensor * q_cur, // [n_embd_head_q, n_head_q, n_tokens]
ggml_tensor * k_cur, // [n_embd_head_k, n_head_k, n_tokens] optional
ggml_tensor * v_cur, // [n_embd_head_v, n_head_v, n_tokens] optional
@@ -1483,7 +1481,6 @@ struct llm_graph_context {
llm_graph_input_attn_cross * inp,
ggml_tensor * wo,
ggml_tensor * wo_b,
- ggml_tensor * wo_s,
ggml_tensor * q_cur, // [n_embd_head_q, n_head_q, n_tokens]
ggml_tensor * k_cur, // [n_embd_head_k, n_head_k, n_tokens]
ggml_tensor * v_cur, // [n_embd_head_v, n_head_v, n_tokens]
@@ -1566,7 +1563,6 @@ struct llm_graph_context {
llm_graph_input_mem_hybrid_iswa_rel * inp,
ggml_tensor * wo,
ggml_tensor * wo_b,
- ggml_tensor * wo_s,
ggml_tensor * q_cur, // [n_embd_head_q, n_head_q, n_tokens]
ggml_tensor * k_cur, // [n_embd_head_k, n_head_k, n_tokens]
ggml_tensor * v_cur, // [n_embd_head_v, n_head_v, n_tokens]
diff --git a/src/llama-kv-cache.cpp b/src/llama-kv-cache.cpp
index c7444319b..c0166d30b 100644
--- a/src/llama-kv-cache.cpp
+++ b/src/llama-kv-cache.cpp
@@ -1455,12 +1455,8 @@ ggml_tensor * llama_kv_cache::get_v(ggml_context * ctx, int32_t il, uint32_t n_k
ggml_row_size(v->type, kv_size*n_embd_v_gqa)*sinfo.s0);
}
-static bool is_type_scaled(ggml_type type) {
- return type == GGML_TYPE_F8_E4M3;
-}
-
ggml_tensor * llama_kv_cache::get_k_scale(int32_t il) const {
- if (!is_type_scaled(type_k())) {
+ if (!ggml_needs_scale(type_k())) {
return nullptr;
}
@@ -1470,7 +1466,7 @@ ggml_tensor * llama_kv_cache::get_k_scale(int32_t il) const {
}
ggml_tensor * llama_kv_cache::get_v_scale(int32_t il) const {
- if (!is_type_scaled(type_v())) {
+ if (!ggml_needs_scale(type_v())) {
return nullptr;
}
@@ -2252,7 +2248,7 @@ ggml_tensor * llama_kv_cache::build_rope_shift(
const int64_t n_embd_head_k = hparams.n_embd_head_k(il);
const int64_t n_cells = ggml_nelements(k)/n_embd_k_gqa;
- if (!ggml_is_quantized(k->type) && k->type != GGML_TYPE_F8_E4M3 && !rot && !rp.k_norm) {
+ if (!ggml_is_quantized(k->type) && !ggml_needs_scale(k->type) && !rot && !rp.k_norm) {
// we rotate only the n_rot dimensions
ggml_tensor * k_rope =
ggml_view_3d(ctx, k,
diff --git a/src/llama-model-loader.cpp b/src/llama-model-loader.cpp
index 808fb44d8..83c8c042b 100644
--- a/src/llama-model-loader.cpp
+++ b/src/llama-model-loader.cpp
@@ -1012,7 +1012,7 @@ const struct ggml_tensor * llama_model_loader::check_tensor_dims(
}
// checks if the weight tensor can be used with the specified buffer type and device
-static bool weight_buft_supported(const llama_hparams & hparams, ggml_tensor * w, ggml_op op, ggml_backend_buffer_type_t buft, ggml_backend_dev_t dev) {
+static bool weight_buft_supported(const llama_hparams & hparams, ggml_tensor * w, const ggml_tensor * scale_meta, ggml_op op, ggml_backend_buffer_type_t buft, ggml_backend_dev_t dev) {
GGML_ASSERT(w != nullptr);
if (op == GGML_OP_NONE) {
@@ -1048,7 +1048,8 @@ static bool weight_buft_supported(const llama_hparams & hparams, ggml_tensor * w
case GGML_OP_MUL_MAT:
{
ggml_tensor * b = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, w->ne[0], 512, w->ne[2], w->ne[3]);
- op_tensor = ggml_mul_mat(ctx, w, b);
+ ggml_tensor * s = scale_meta ? ggml_new_tensor(ctx, GGML_TYPE_F32, GGML_MAX_DIMS, scale_meta->ne) : nullptr;
+ op_tensor = ggml_mul_mat_ext(ctx, w, b, s, nullptr);
} break;
case GGML_OP_MUL_MAT_ID:
{
@@ -1057,7 +1058,8 @@ static bool weight_buft_supported(const llama_hparams & hparams, ggml_tensor * w
GGML_ASSERT(n_ids_used > 0);
ggml_tensor * b = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, w->ne[0], n_ids_used, 512);
ggml_tensor * ids = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, n_ids_used, 512);
- op_tensor = ggml_mul_mat_id(ctx, w, b, ids);
+ ggml_tensor * s = scale_meta ? ggml_new_tensor(ctx, GGML_TYPE_F32, GGML_MAX_DIMS, scale_meta->ne) : nullptr;
+ op_tensor = ggml_mul_mat_id_ext(ctx, w, b, ids, s, nullptr);
} break;
case GGML_OP_ADD:
{
@@ -1159,12 +1161,12 @@ static bool weight_buft_supported(const llama_hparams & hparams, ggml_tensor * w
}
// find the first buffer type in the list that can use the tensor
-static ggml_backend_buffer_type_t select_weight_buft(const llama_hparams & hparams, ggml_tensor * tensor, ggml_op op, const buft_list_t * buft_list) {
+static ggml_backend_buffer_type_t select_weight_buft(const llama_hparams & hparams, ggml_tensor * tensor, const ggml_tensor * scale_meta, ggml_op op, const buft_list_t * buft_list) {
GGML_ASSERT(!buft_list->empty());
for (const auto & cur : *buft_list) {
ggml_backend_dev_t cur_dev = cur.first;
ggml_backend_buffer_type_t cur_buft = cur.second;
- if (weight_buft_supported(hparams, tensor, op, cur_buft, cur_dev)) {
+ if (weight_buft_supported(hparams, tensor, scale_meta, op, cur_buft, cur_dev)) {
return cur_buft;
}
}
@@ -1280,11 +1282,37 @@ struct ggml_tensor * llama_model_loader::create_tensor(
return nullptr;
}
+ const bool is_matmul = info.op == GGML_OP_MUL_MAT || info.op == GGML_OP_MUL_MAT_ID;
+ const bool is_matmul_weight = is_matmul && tn.suffix != nullptr && strcmp(tn.suffix, "weight") == 0 && !(flags & TENSOR_NO_SCALE);
+ const bool is_matmul_scale = is_matmul && tn.suffix != nullptr &&
+ (strcmp(tn.suffix, "scale") == 0 || strcmp(tn.suffix, "input_scale") == 0);
+
+ if ((flags & TENSOR_NO_SCALE) && ggml_needs_scale(t_meta->type)) {
+ throw std::runtime_error(format("tensor '%s' takes no weight scale, so type %s is not supported; re-convert the model",
+ tn.str().c_str(), ggml_type_name(t_meta->type)));
+ }
+
+ // the scale is a source of the matmul, so it goes where the weight is
+ if (is_matmul_scale) {
+ const auto it = matmul_weight_bufts.find(LLM_TN_IMPL(tn.arch, tn.tensor, "weight", tn.bid, tn.xid).str());
+ if (it != matmul_weight_bufts.end()) {
+ ggml_backend_dev_t dev = ggml_backend_buft_get_device(it->second);
+ // CPU extra buffers (repack, AMX, KleidiAI) only hold the layouts of their weights
+ if (dev && ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_CPU && !ggml_backend_buft_is_host(it->second)) {
+ return ggml_backend_dev_buffer_type(dev);
+ }
+ return it->second;
+ }
+ }
+
// tensors with "bias" suffix are always used with GGML_OP_ADD or GGML_OP_ADD_ID;
// embedded-adapter ".lora_a"/".lora_b" tensors are always used with GGML_OP_MUL_MAT_ID
ggml_op op;
if (tn.suffix != nullptr && strcmp(tn.suffix, "bias") == 0) {
op = info.op == GGML_OP_MUL_MAT_ID ? GGML_OP_ADD_ID : GGML_OP_ADD;
+ } else if (is_matmul_scale) {
+ // scale without a weight
+ op = GGML_OP_MUL;
} else if (hparams.router_layer >= 0 && tn.suffix != nullptr &&
(strcmp(tn.suffix, "lora_a") == 0 || strcmp(tn.suffix, "lora_b") == 0)) {
op = GGML_OP_MUL_MAT_ID;
@@ -1303,7 +1331,38 @@ struct ggml_tensor * llama_model_loader::create_tensor(
}
}
+ const ggml_tensor * scale_meta = nullptr;
+ ggml_tensor user_scale_meta = {};
+ if (is_matmul_weight && (op == GGML_OP_MUL_MAT || op == GGML_OP_MUL_MAT_ID)) {
+ const std::string scale_name = LLM_TN_IMPL(tn.arch, tn.tensor, "scale", tn.bid, tn.xid).str();
+ scale_meta = get_tensor_meta(scale_name.c_str());
+ if (!scale_meta && files.empty()) {
+ const int64_t tid = gguf_find_tensor(metadata, scale_name.c_str());
+ if (tid != -1) {
+ user_scale_meta.type = gguf_get_tensor_type(metadata, tid);
+ memcpy(user_scale_meta.ne, gguf_get_tensor_ne(metadata, tid), sizeof(user_scale_meta.ne));
+ scale_meta = &user_scale_meta;
+ }
+ }
+ if (!scale_meta && ggml_needs_scale(t_meta->type)) {
+ throw std::runtime_error(format("tensor '%s' of type %s requires a weight scale, but '%s' is missing; re-convert the model",
+ tn.str().c_str(), ggml_type_name(t_meta->type), scale_name.c_str()));
+ }
+ if (scale_meta && op == GGML_OP_MUL_MAT && !ggml_is_scalar(scale_meta) &&
+ !(ggml_is_vector(scale_meta) && scale_meta->ne[0] == t_meta->ne[1])) {
+ throw std::runtime_error(format("weight scale tensor '%s' must be scalar or have one value per output channel", scale_name.c_str()));
+ }
+ if (scale_meta && op == GGML_OP_MUL_MAT_ID && !ggml_is_scalar(scale_meta) &&
+ !(ggml_is_vector(scale_meta) && scale_meta->ne[0] == t_meta->ne[2]) &&
+ !(scale_meta->ne[0] == t_meta->ne[1] && scale_meta->ne[1] == t_meta->ne[2] && scale_meta->ne[2] == 1 && scale_meta->ne[3] == 1)) {
+ throw std::runtime_error(format("weight scale tensor '%s' must be scalar, per expert or per output channel and expert", scale_name.c_str()));
+ }
+ }
+
if (is_lazy) {
+ if (is_matmul_weight) {
+ matmul_weight_bufts[tn.str()] = lazy_read::buft();
+ }
return lazy_read::buft();
}
@@ -1334,7 +1393,7 @@ struct ggml_tensor * llama_model_loader::create_tensor(
if (std::regex_search(tensor_name, pattern)) {
if (overrides->buft == ggml_backend_cpu_buffer_type()) {
// when overriding to a CPU buffer, consider the extra buffer types
- buft = select_weight_buft(hparams, t_meta, op, buft_list_cpu);
+ buft = select_weight_buft(hparams, t_meta, scale_meta, op, buft_list_cpu);
if (use_mmap) {
static std::once_flag once;
std::call_once(once, [] {
@@ -1355,7 +1414,7 @@ struct ggml_tensor * llama_model_loader::create_tensor(
}
if (!buft) {
- buft = select_weight_buft(hparams, t_meta, op, buft_list);
+ buft = select_weight_buft(hparams, t_meta, scale_meta, op, buft_list);
if (!buft) {
throw std::runtime_error(format("failed to find a compatible buffer type for tensor %s", tn.str().c_str()));
}
@@ -1381,6 +1440,10 @@ struct ggml_tensor * llama_model_loader::create_tensor(
n_tensors_moved++;
}
+ if (is_matmul_weight) {
+ matmul_weight_bufts[tn.str()] = buft;
+ }
+
return buft;
};
@@ -1485,6 +1548,29 @@ struct ggml_tensor * llama_model_loader::create_tensor(
}
void llama_model_loader::done_getting_tensors(bool partial) const {
+ std::unordered_set<std::string> created;
+ for (const auto & it : ctx_map) {
+ for (ggml_tensor * t = ggml_get_first_tensor(it.second.get()); t; t = ggml_get_next_tensor(it.second.get(), t)) {
+ created.insert(ggml_get_name(t));
+ }
+ }
+ for (const auto & it : ctx_map) {
+ for (ggml_tensor * t = ggml_get_first_tensor(it.second.get()); t; t = ggml_get_next_tensor(it.second.get(), t)) {
+ if (!ggml_needs_scale(t->type)) {
+ continue;
+ }
+ std::string scale_name = ggml_get_name(t);
+ if (scale_name.size() > 7 && scale_name.compare(scale_name.size() - 7, 7, ".weight") == 0) {
+ scale_name.resize(scale_name.size() - 7);
+ }
+ scale_name += ".scale";
+ if (!created.count(scale_name)) {
+ throw std::runtime_error(format("tensor '%s' of type %s requires a weight scale, but '%s' is missing or not used by this architecture; re-convert the model",
+ ggml_get_name(t), ggml_type_name(t->type), scale_name.c_str()));
+ }
+ }
+ }
+
if (n_created > n_tensors) {
throw std::runtime_error(format("%s: too many tensors created; expected %d, got %d", __func__, n_tensors, n_created));
}
diff --git a/src/llama-model-loader.h b/src/llama-model-loader.h
index ed59079a4..e2db7395b 100644
--- a/src/llama-model-loader.h
+++ b/src/llama-model-loader.h
@@ -68,6 +68,7 @@ struct llama_model_loader {
static const int TENSOR_SKIP_IF_VIRTUAL = 1 << 3;
static const int TENSOR_ALLOW_RESHAPE = 1 << 4;
static const int TENSOR_READ_LAZY = 1 << 5; // read rows on demand instead of loading whole tensor; requires mmap for now
+ static const int TENSOR_NO_SCALE = 1 << 6; // this weight takes no dequantization scale
int n_kv = 0;
int n_tensors = 0;
@@ -162,6 +163,9 @@ struct llama_model_loader {
std::map<ctx_key, ggml_context_ptr, ctx_key_comparator> ctx_map;
+ // buffer types of the matmul weights, their scales are placed with them
+ std::unordered_map<std::string, ggml_backend_buffer_type_t> matmul_weight_bufts;
+
// track tensors that had to be moved for debugging:
size_t n_tensors_moved = 0;
std::string first_tensor_moved_name;
diff --git a/src/llama-model-saver.cpp b/src/llama-model-saver.cpp
index 00b169ef6..7a02ef1a9 100644
--- a/src/llama-model-saver.cpp
+++ b/src/llama-model-saver.cpp
@@ -10,9 +10,12 @@
#include "llama-model.h"
#include "llama-vocab.h"
+#include <algorithm>
#include <cstdint>
+#include <cstring>
#include <string>
#include <unordered_set>
+#include <vector>
llama_model_saver::llama_model_saver(const struct llama_model * model) :
gguf_ctx(gguf_init_empty()), model(model), llm_kv(model->arch) {
@@ -528,8 +531,6 @@ void llama_model_saver::add_tensors_from_model() {
add_tensor(model->output);
add_tensor(model->output_b);
add_tensor(model->output_norm_enc);
- add_tensor(model->output_s);
- add_tensor(model->output_in_s);
add_tensor(model->output_res_score);
add_tensor(model->output_res_query);
add_tensor(model->nextn_proj_pre);
@@ -593,6 +594,21 @@ void llama_model_saver::add_tensors_from_model() {
add_tensor(tensor);
}
}
+
+ std::vector<const struct ggml_tensor *> scales;
+ for (const auto * map : { &model->w_scales.scale, &model->w_scales.input_scale }) {
+ for (const auto & [_, s] : *map) {
+ if (seen.insert(s).second) {
+ scales.push_back(s);
+ }
+ }
+ }
+ std::sort(scales.begin(), scales.end(), [](const ggml_tensor * a, const ggml_tensor * b) {
+ return strcmp(a->name, b->name) < 0;
+ });
+ for (const auto * s : scales) {
+ add_tensor(s);
+ }
}
void llama_model_saver::save(const std::string & path_model) {
diff --git a/src/llama-model.cpp b/src/llama-model.cpp
index b83636631..f06ea2ec0 100644
--- a/src/llama-model.cpp
+++ b/src/llama-model.cpp
@@ -441,6 +441,20 @@ struct ggml_backend_meta_split_state llama_meta_device_get_split_state(const str
(ud->model->arch == LLM_ARCH_DFLASH && hparams.dsv4_hc_mult > 0);
const bool is_glm5_next = ud->model->arch == LLM_ARCH_GLM5_NEXT;
+ // a per-output-channel scale indexes the weight's ne[1], so it splits on ne[0] to match
+ if (tensor_name.size() >= 6 && tensor_name.compare(tensor_name.size() - 6, 6, ".scale") == 0) {
+ const std::string weight_name = tensor_name.substr(0, tensor_name.size() - 6) + ".weight";
+ const ggml_tensor * w = ud->model->get_tensor(weight_name.c_str());
+ if (w != nullptr) {
+ ggml_backend_meta_split_state ws = llama_meta_device_get_split_state(w, userdata);
+ if (ws.axis == GGML_BACKEND_SPLIT_AXIS_1 && tensor->ne[0] == w->ne[1]) {
+ ws.axis = GGML_BACKEND_SPLIT_AXIS_0;
+ return ws;
+ }
+ }
+ return {GGML_BACKEND_SPLIT_AXIS_MIRRORED, {0}, {1}, 1};
+ }
+
static const std::regex pattern_q_weight ("blk\\.\\d*\\.attn_q.weight");
static const std::regex pattern_kv_weight ("blk\\.\\d*\\.attn_(k|v).weight");
static const std::regex pattern_v_exps_weight ("blk\\.\\d*\\.attn_v_exps.weight"); // K2 Horizon MoVA
@@ -1949,8 +1963,6 @@ bool llama_model_base::load_tensors(llama_model_loader & ml) {
// call the per-model loading function
load_arch_tensors(ml);
- // generic pass: load optional per-tensor/per-expert ".scale" tensors (e.g. NVFP4 scale2)
- // this avoids having to add scale loading to every architecture
for (int i = 0; i < n_layer_all; ++i) {
auto & layer = layers[i];
@@ -1963,161 +1975,6 @@ bool llama_model_base::load_tensors(llama_model_loader & ml) {
throw std::runtime_error(format("KV cache scales for layer %d must be F32", i));
}
}
-
- // attention weight scales (per-tensor, shape {1})
- if (!layer.wq_s && layer.wq) {
- layer.wq_s = create_tensor(tn(LLM_TENSOR_ATTN_Q, "scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.wk_s && layer.wk) {
- layer.wk_s = create_tensor(tn(LLM_TENSOR_ATTN_K, "scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.wv_s && layer.wv) {
- layer.wv_s = create_tensor(tn(LLM_TENSOR_ATTN_V, "scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.wo_s && layer.wo) {
- layer.wo_s = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.wqkv_s && layer.wqkv) {
- layer.wqkv_s = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.wqkv_gate_s && layer.wqkv_gate) {
- layer.wqkv_gate_s = create_tensor(tn(LLM_TENSOR_ATTN_GATE, "scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
-
- // dense FFN weight scales (per-tensor, shape {1})
- if (!layer.ffn_gate_s && layer.ffn_gate) {
- layer.ffn_gate_s = create_tensor(tn(LLM_TENSOR_FFN_GATE, "scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.ffn_down_s && layer.ffn_down) {
- layer.ffn_down_s = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.ffn_up_s && layer.ffn_up) {
- layer.ffn_up_s = create_tensor(tn(LLM_TENSOR_FFN_UP, "scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.ffn_gate_shexp_s && layer.ffn_gate_shexp) {
- layer.ffn_gate_shexp_s = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.ffn_down_shexp_s && layer.ffn_down_shexp) {
- layer.ffn_down_shexp_s = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.ffn_up_shexp_s && layer.ffn_up_shexp) {
- layer.ffn_up_shexp_s = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
-
- // MoE expert weight scales (per-expert, shape {n_expert})
- if (!layer.ffn_gate_exps_s && layer.ffn_gate_exps) {
- layer.ffn_gate_exps_s = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "scale", i), {n_expert}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.ffn_down_exps_s && layer.ffn_down_exps) {
- layer.ffn_down_exps_s = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "scale", i), {n_expert}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.ffn_up_exps_s && layer.ffn_up_exps) {
- layer.ffn_up_exps_s = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "scale", i), {n_expert}, TENSOR_NOT_REQUIRED);
- if (layer.ffn_up_exps_s && layer.ffn_gate_up_exps) {
- throw std::runtime_error(format("ffn_up_exps.scale and ffn_gate_up_exps.weight both exist for layer %d", i));
- }
- }
- if (!layer.ffn_up_exps_s && layer.ffn_gate_up_exps) {
- layer.ffn_up_exps_s = create_tensor(tn(LLM_TENSOR_FFN_GATE_UP_EXPS, "scale", i), {n_expert}, TENSOR_NOT_REQUIRED);
- }
-
- // recurrent / linear-attention weight scales (per-tensor, shape {1})
- if (!layer.ssm_in_s && layer.ssm_in) {
- layer.ssm_in_s = create_tensor(tn(LLM_TENSOR_SSM_IN, "scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.ssm_out_s && layer.ssm_out) {
- layer.ssm_out_s = create_tensor(tn(LLM_TENSOR_SSM_OUT, "scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.ssm_alpha_s && layer.ssm_alpha) {
- layer.ssm_alpha_s = create_tensor(tn(LLM_TENSOR_SSM_ALPHA, "scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.ssm_beta_s && layer.ssm_beta) {
- layer.ssm_beta_s = create_tensor(tn(LLM_TENSOR_SSM_BETA, "scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.nextn.eh_proj_s && layer.nextn.eh_proj) {
- layer.nextn.eh_proj_s = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ, "scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.nextn.shared_head_head_s && layer.nextn.shared_head_head) {
- layer.nextn.shared_head_head_s = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD, "scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
-
- // input scales
- if (!layer.wq_in_s && layer.wq) {
- layer.wq_in_s = create_tensor(tn(LLM_TENSOR_ATTN_Q, "input_scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.wk_in_s && layer.wk) {
- layer.wk_in_s = create_tensor(tn(LLM_TENSOR_ATTN_K, "input_scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.wv_in_s && layer.wv) {
- layer.wv_in_s = create_tensor(tn(LLM_TENSOR_ATTN_V, "input_scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.wo_in_s && layer.wo) {
- layer.wo_in_s = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "input_scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.wqkv_in_s && layer.wqkv) {
- layer.wqkv_in_s = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "input_scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.wqkv_gate_in_s && layer.wqkv_gate) {
- layer.wqkv_gate_in_s = create_tensor(tn(LLM_TENSOR_ATTN_GATE, "input_scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.ffn_gate_in_s && layer.ffn_gate) {
- layer.ffn_gate_in_s = create_tensor(tn(LLM_TENSOR_FFN_GATE, "input_scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.ffn_down_in_s && layer.ffn_down) {
- layer.ffn_down_in_s = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "input_scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.ffn_up_in_s && layer.ffn_up) {
- layer.ffn_up_in_s = create_tensor(tn(LLM_TENSOR_FFN_UP, "input_scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.ffn_gate_exps_in_s && layer.ffn_gate_exps) {
- layer.ffn_gate_exps_in_s = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "input_scale", i), {n_expert}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.ffn_down_exps_in_s && layer.ffn_down_exps) {
- layer.ffn_down_exps_in_s = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "input_scale", i), {n_expert}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.ffn_up_exps_in_s && layer.ffn_up_exps) {
- layer.ffn_up_exps_in_s = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "input_scale", i), {n_expert}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.ffn_gate_shexp_in_s && layer.ffn_gate_shexp) {
- layer.ffn_gate_shexp_in_s = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "input_scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.ffn_down_shexp_in_s && layer.ffn_down_shexp) {
- layer.ffn_down_shexp_in_s = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "input_scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.ffn_up_shexp_in_s && layer.ffn_up_shexp) {
- layer.ffn_up_shexp_in_s = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "input_scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.ssm_in_in_s && layer.ssm_in) {
- layer.ssm_in_in_s = create_tensor(tn(LLM_TENSOR_SSM_IN, "input_scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.ssm_out_in_s && layer.ssm_out) {
- layer.ssm_out_in_s = create_tensor(tn(LLM_TENSOR_SSM_OUT, "input_scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.ssm_alpha_in_s && layer.ssm_alpha) {
- layer.ssm_alpha_in_s = create_tensor(tn(LLM_TENSOR_SSM_ALPHA, "input_scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.ssm_beta_in_s && layer.ssm_beta) {
- layer.ssm_beta_in_s = create_tensor(tn(LLM_TENSOR_SSM_BETA, "input_scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.nextn.eh_proj_in_s && layer.nextn.eh_proj) {
- layer.nextn.eh_proj_in_s = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ, "input_scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- if (!layer.nextn.shared_head_head_in_s && layer.nextn.shared_head_head) {
- layer.nextn.shared_head_head_in_s = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD, "input_scale", i), {1}, TENSOR_NOT_REQUIRED);
- }
- }
- // output scales
- if (output) {
- // weight scale
- if (!output_s && (ml.get_weight(tn(LLM_TENSOR_OUTPUT, "scale").str().c_str()) ||
- gguf_find_tensor(ml.metadata, tn(LLM_TENSOR_OUTPUT, "scale").str().c_str()) >= 0)) {
- output_s = create_tensor(tn(LLM_TENSOR_OUTPUT, "scale"), {1}, TENSOR_NOT_REQUIRED);
- }
- // input scale
- if (!output_in_s && (ml.get_weight(tn(LLM_TENSOR_OUTPUT, "input_scale").str().c_str()) ||
- gguf_find_tensor(ml.metadata, tn(LLM_TENSOR_OUTPUT, "input_scale").str().c_str()) >= 0)) {
- output_in_s = create_tensor(tn(LLM_TENSOR_OUTPUT, "input_scale"), {1}, TENSOR_NOT_REQUIRED);
- }
}
post_arch_tensors(ml);
}
@@ -2130,11 +1987,6 @@ bool llama_model_base::load_tensors(llama_model_loader & ml) {
can_prefetch.insert(per_layer_tok_embd);
}
- if (output && tok_embd &&
- strcmp(output->name, tok_embd->name) == 0 &&
- (output_s || output_in_s)) {
- throw std::runtime_error("output scales require a separate output weight");
- }
// populate tensors_by_name
for (auto & [_, ctx_ptr] : ml.ctx_map) {
for (auto * cur = ggml_get_first_tensor(ctx_ptr.get()); cur != NULL; cur = ggml_get_next_tensor(ctx_ptr.get(), cur)) {
@@ -2142,35 +1994,6 @@ bool llama_model_base::load_tensors(llama_model_loader & ml) {
}
}
- // map each weight to its "<name>.scale" tensor: shape {1} for a 2D weight, {n_expert} for a 3D weight
- {
- auto ends_with = [](const std::string & str, const std::string & suffix) {
- return str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
- };
- std::unordered_map<std::string, ggml_tensor *> weights;
- for (const auto & [name, cur] : tensors_by_name) {
- if (ends_with(name, ".weight")) {
- weights.emplace(name.substr(0, name.size() - 7), cur);
- }
- }
- for (const auto & [name, s] : tensors_by_name) {
- if (!ends_with(name, ".scale")) {
- continue;
- }
- const auto it = weights.find(name.substr(0, name.size() - 6));
- if (it == weights.end()) {
- continue;
- }
- ggml_tensor * w = it->second;
- const bool ok = w->ne[3] == 1 && ggml_nelements(s) == w->ne[2] && s->ne[0] == w->ne[2];
- if (ok) {
- w_scales.emplace(w, s);
- } else {
- LLAMA_LOG_DEBUG("%s: '%s' is not a weight scale for '%s', skipping\n", __func__, name.c_str(), w->name);
- }
- }
- }
-
load_prec_policy(ml);
ml.init_mappings(true, use_mlock ? &pimpl->mlock_mmaps : nullptr);
@@ -2682,9 +2505,56 @@ ggml_tensor * llama_model_base::create_tensor(llama_model_loader & ml, const LLM
// blocks that are not model layers (e.g. the blocks of a head) go with the output
buft_list_layer = (size_t) tn.bid < pimpl->dev_layer.size() ? pimpl->dev_layer.at(tn.bid).buft_list : pimpl->dev_output.buft_list;
}
- return ml.create_tensor(
+ ggml_tensor * t = ml.create_tensor(
hparams, &pimpl->cpu_buft_list, pimpl->dev_input.buft_list, pimpl->dev_output.buft_list, buft_list_layer,
tn, ne, flags);
+
+ if (tn.suffix == nullptr || strcmp(tn.suffix, "weight") != 0 || (flags & llama_model_loader::TENSOR_NO_SCALE)) {
+ return t;
+ }
+ const ggml_op op = llm_tensor_info_for(tn.tensor).op;
+ if (op != GGML_OP_MUL_MAT && op != GGML_OP_MUL_MAT_ID) {
+ return t;
+ }
+ if (t == nullptr && !(flags & llama_model_loader::TENSOR_SKIP)) {
+ return t;
+ }
+
+ const int scale_flags = TENSOR_NOT_REQUIRED | (flags & (TENSOR_SKIP | TENSOR_DUPLICATED));
+ const std::pair<const char *, std::unordered_map<const ggml_tensor *, ggml_tensor *> *> scale_suffixes[] = {
+ { "scale", &w_scales.scale },
+ { "input_scale", &w_scales.input_scale },
+ };
+ for (const auto & [suffix, map] : scale_suffixes) {
+ const LLM_TN_IMPL tn_s(tn.arch, tn.tensor, suffix, tn.bid, tn.xid);
+ const std::string name = tn_s.str();
+ const ggml_tensor * meta = ml.get_tensor_meta(name.c_str());
+ const int64_t * ne = nullptr;
+ if (meta != nullptr) {
+ ne = meta->ne;
+ } else {
+ const int64_t tid = gguf_find_tensor(ml.metadata, name.c_str());
+ if (tid == -1) {
+ continue;
+ }
+ ne = gguf_get_tensor_ne(ml.metadata, tid);
+ }
+ ggml_tensor * s = ml.create_tensor(
+ hparams, &pimpl->cpu_buft_list, pimpl->dev_input.buft_list, pimpl->dev_output.buft_list, buft_list_layer,
+ tn_s, {ne[0], ne[1], ne[2], ne[3]}, scale_flags);
+ if (t != nullptr && s != nullptr) {
+ if (s->type != GGML_TYPE_F32) {
+ throw std::runtime_error(format("scale tensor %s must be F32", s->name));
+ }
+ if (strcmp(suffix, "input_scale") == 0 && !ggml_is_scalar(s) &&
+ !(op == GGML_OP_MUL_MAT_ID && ggml_is_vector(s) && s->ne[0] == t->ne[2])) {
+ throw std::runtime_error(format("input scale tensor %s must be scalar or have one value per expert", s->name));
+ }
+ (*map)[t] = s;
+ }
+ }
+
+ return t;
}
std::string llama_model::arch_name() const {
@@ -2707,11 +2577,6 @@ size_t llama_model::size() const {
return pimpl->n_bytes;
}
-ggml_tensor * llama_model::get_w_scale(const ggml_tensor * w) const {
- const auto it = w_scales.find(w);
- return it == w_scales.end() ? nullptr : it->second;
-}
-
size_t llama_model::n_tensors() const {
return tensors_by_name.size();
}
@@ -4274,7 +4139,8 @@ llama_model_base::llama_model_base(const struct llama_model_params & params) : l
TENSOR_SKIP (llama_model_loader::TENSOR_SKIP),
TENSOR_SKIP_IF_VIRTUAL(llama_model_loader::TENSOR_SKIP_IF_VIRTUAL),
TENSOR_ALLOW_RESHAPE (llama_model_loader::TENSOR_ALLOW_RESHAPE),
- TENSOR_READ_LAZY (llama_model_loader::TENSOR_READ_LAZY) {}
+ TENSOR_READ_LAZY (llama_model_loader::TENSOR_READ_LAZY),
+ TENSOR_NO_SCALE (llama_model_loader::TENSOR_NO_SCALE) {}
ggml_tensor * llama_model_base::create_tensor(const LLM_TN_IMPL & tn, const std::initializer_list<int64_t> & ne, int flags) {
GGML_ASSERT(ml != nullptr);
diff --git a/src/llama-model.h b/src/llama-model.h
index 62fbda68a..c3334469d 100644
--- a/src/llama-model.h
+++ b/src/llama-model.h
@@ -246,14 +246,10 @@ struct llama_layer_zaya1 {
struct llama_layer_nextn {
struct ggml_tensor * eh_proj = nullptr;
- struct ggml_tensor * eh_proj_s = nullptr;
- struct ggml_tensor * eh_proj_in_s = nullptr;
struct ggml_tensor * embed_tokens = nullptr;
struct ggml_tensor * enorm = nullptr;
struct ggml_tensor * hnorm = nullptr;
struct ggml_tensor * shared_head_head = nullptr;
- struct ggml_tensor * shared_head_head_s = nullptr;
- struct ggml_tensor * shared_head_head_in_s = nullptr;
struct ggml_tensor * shared_head_norm = nullptr;
// qwen4exp: the MTP block collapses its hyper-connection streams with its own mixer
@@ -378,11 +374,6 @@ struct llama_layer {
struct ggml_tensor * ffn_up_exps_b = nullptr;
struct ggml_tensor * ffn_gate_up_exps_b = nullptr;
- // ff MoE per-expert scales (NVFP4 per-tensor scale2)
- struct ggml_tensor * ffn_gate_exps_s = nullptr;
- struct ggml_tensor * ffn_down_exps_s = nullptr;
- struct ggml_tensor * ffn_up_exps_s = nullptr;
-
// ff MoE latent proj
struct ggml_tensor * ffn_latent_down = nullptr;
struct ggml_tensor * ffn_latent_up = nullptr;
@@ -438,13 +429,10 @@ struct llama_layer {
struct ggml_tensor * lilicorr_attn_in_proj_b = nullptr;
struct ggml_tensor * lilicorr_attn_out_proj = nullptr;
struct ggml_tensor * lilicorr_attn_out_proj_b = nullptr;
- struct ggml_tensor * lilicorr_attn_out_proj_s = nullptr;
struct ggml_tensor * lilicorr_mlp_0 = nullptr;
struct ggml_tensor * lilicorr_mlp_0_b = nullptr;
- struct ggml_tensor * lilicorr_mlp_0_s = nullptr;
struct ggml_tensor * lilicorr_mlp_2 = nullptr;
struct ggml_tensor * lilicorr_mlp_2_b = nullptr;
- struct ggml_tensor * lilicorr_mlp_2_s = nullptr;
// mamba proj
struct ggml_tensor * ssm_in = nullptr;
@@ -520,45 +508,6 @@ struct llama_layer {
struct ggml_tensor * rope_short = nullptr;
struct ggml_tensor * rope_freqs = nullptr;
- // bitnet scale
- struct ggml_tensor * wq_s = nullptr;
- struct ggml_tensor * wk_s = nullptr;
- struct ggml_tensor * wv_s = nullptr;
- struct ggml_tensor * wo_s = nullptr;
- struct ggml_tensor * wqkv_s = nullptr;
- struct ggml_tensor * wqkv_gate_s = nullptr;
- struct ggml_tensor * ffn_gate_s = nullptr;
- struct ggml_tensor * ffn_up_s = nullptr;
- struct ggml_tensor * ffn_down_s = nullptr;
- struct ggml_tensor * ffn_gate_shexp_s = nullptr;
- struct ggml_tensor * ffn_up_shexp_s = nullptr;
- struct ggml_tensor * ffn_down_shexp_s = nullptr;
- struct ggml_tensor * ssm_in_s = nullptr;
- struct ggml_tensor * ssm_out_s = nullptr;
- struct ggml_tensor * ssm_alpha_s = nullptr;
- struct ggml_tensor * ssm_beta_s = nullptr;
-
- // input scales
- struct ggml_tensor * wq_in_s = nullptr;
- struct ggml_tensor * wk_in_s = nullptr;
- struct ggml_tensor * wv_in_s = nullptr;
- struct ggml_tensor * wo_in_s = nullptr;
- struct ggml_tensor * wqkv_in_s = nullptr;
- struct ggml_tensor * wqkv_gate_in_s = nullptr;
- struct ggml_tensor * ffn_gate_in_s = nullptr;
- struct ggml_tensor * ffn_up_in_s = nullptr;
- struct ggml_tensor * ffn_down_in_s = nullptr;
- struct ggml_tensor * ffn_gate_exps_in_s = nullptr;
- struct ggml_tensor * ffn_down_exps_in_s = nullptr;
- struct ggml_tensor * ffn_up_exps_in_s = nullptr;
- struct ggml_tensor * ffn_gate_shexp_in_s= nullptr;
- struct ggml_tensor * ffn_up_shexp_in_s = nullptr;
- struct ggml_tensor * ffn_down_shexp_in_s= nullptr;
- struct ggml_tensor * ssm_in_in_s = nullptr;
- struct ggml_tensor * ssm_out_in_s = nullptr;
- struct ggml_tensor * ssm_alpha_in_s = nullptr;
- struct ggml_tensor * ssm_beta_in_s = nullptr;
-
// altup & laurel
struct ggml_tensor * per_layer_inp_gate = nullptr;
struct ggml_tensor * per_layer_proj = nullptr;
@@ -805,6 +754,32 @@ struct llama_hadamard_transform {
};
using llama_hadamard_rotations = std::unordered_map<const ggml_tensor *, llama_hadamard_transform>;
+// ".scale" / ".input_scale" tensors of the matmul weights, keyed by weight
+struct llama_weight_scales {
+ std::unordered_map<const ggml_tensor *, ggml_tensor *> scale;
+ std::unordered_map<const ggml_tensor *, ggml_tensor *> input_scale;
+
+ ggml_tensor * get(const ggml_tensor * w) const {
+ auto it = scale.find(w);
+ if (it == scale.end() && w->view_src) {
+ it = scale.find(w->view_src);
+ // only per-tensor scales are supported for views of a weight
+ GGML_ASSERT(it == scale.end() || ggml_nelements(it->second) == 1);
+ }
+ return it == scale.end() ? nullptr : it->second;
+ }
+
+ ggml_tensor * get_input(const ggml_tensor * w) const {
+ auto it = input_scale.find(w);
+ if (it == input_scale.end() && w->view_src) {
+ it = input_scale.find(w->view_src);
+ // only per-tensor scales are supported for views of a weight
+ GGML_ASSERT(it == input_scale.end() || ggml_nelements(it->second) == 1);
+ }
+ return it == input_scale.end() ? nullptr : it->second;
+ }
+};
+
struct llama_hadamard {
// names and sign data come from the GGUF metadata in load_hparams, the transforms are made in load_tensors
std::unordered_map<std::string, uint32_t> weight_blocks;
@@ -850,10 +825,7 @@ struct llama_model {
llama_hadamard hdmd;
- // weight -> "<name>.scale" tensor (e.g. NVFP4 scale2), applied by build_lora_mm and build_lora_mm_id
- std::unordered_map<const ggml_tensor *, ggml_tensor *> w_scales;
-
- ggml_tensor * get_w_scale(const ggml_tensor * w) const;
+ llama_weight_scales w_scales;
// see llama_model_set_attn_rot
bool attn_rot = true;
@@ -881,11 +853,6 @@ struct llama_model {
struct ggml_tensor * output_b = nullptr;
struct ggml_tensor * output_norm_enc = nullptr;
-
- // NVFP4 per-tensor scale2, input_scale for LM head
- struct ggml_tensor * output_s = nullptr;
- struct ggml_tensor * output_in_s = nullptr;
-
// NextN/MTP model-level projections
struct ggml_tensor * nextn_proj_pre = nullptr;
struct ggml_tensor * nextn_proj_post = nullptr;
@@ -954,15 +921,12 @@ struct llama_model {
struct ggml_tensor * per_layer_proj_norm = nullptr;
// eagle3 / dflash feature fusion layer
- struct ggml_tensor * fc = nullptr;
- struct ggml_tensor * fc_s = nullptr;
- struct ggml_tensor * fc_in_s = nullptr;
+ struct ggml_tensor * fc = nullptr;
struct ggml_tensor * d2t = nullptr; // draft to target vocabulary mapping
// dspark
struct ggml_tensor * dspark_markov_w1 = nullptr;
struct ggml_tensor * dspark_markov_w2 = nullptr;
- struct ggml_tensor * dspark_markov_w2_s = nullptr;
struct ggml_tensor * dspark_conf_proj = nullptr;
struct ggml_tensor * dspark_conf_proj_b = nullptr;
struct ggml_tensor * dspark_joint_proj = nullptr; // rnn markov head
@@ -982,10 +946,8 @@ struct llama_model {
struct ggml_tensor * lilicorr_token_proj = nullptr;
struct ggml_tensor * lilicorr_token_proj_b = nullptr;
- struct ggml_tensor * lilicorr_token_proj_s = nullptr;
struct ggml_tensor * lilicorr_pass_hidden_proj = nullptr;
struct ggml_tensor * lilicorr_pass_hidden_proj_b = nullptr;
- struct ggml_tensor * lilicorr_pass_hidden_proj_s = nullptr;
struct ggml_tensor * lilicorr_feature_norm = nullptr;
struct ggml_tensor * lilicorr_feature_norm_b = nullptr;
struct ggml_tensor * lilicorr_feature_mlp_1 = nullptr;
@@ -997,7 +959,6 @@ struct llama_model {
struct ggml_tensor * lilicorr_attention_bias = nullptr;
struct ggml_tensor * lilicorr_context_proj = nullptr;
struct ggml_tensor * lilicorr_context_proj_b = nullptr;
- struct ggml_tensor * lilicorr_context_proj_s = nullptr;
struct ggml_tensor * lilicorr_output_norm = nullptr;
struct ggml_tensor * lilicorr_anchor_norm = nullptr;
struct ggml_tensor * lilicorr_factor_input_proj = nullptr;
@@ -1009,7 +970,6 @@ struct llama_model {
struct ggml_tensor * lilicorr_anchor_out_head = nullptr;
struct ggml_tensor * lilicorr_anchor_out_head_b = nullptr;
-
// unified vector to store target-model extracted layer ids in eagle3, dflash, etc.
std::vector<int32_t> target_layer_ids;
@@ -1140,6 +1100,7 @@ struct llama_model_base : public llama_model {
const int TENSOR_SKIP_IF_VIRTUAL;
const int TENSOR_ALLOW_RESHAPE;
const int TENSOR_READ_LAZY;
+ const int TENSOR_NO_SCALE;
explicit llama_model_base(const llama_model_params & params);
virtual ~llama_model_base() = default;
diff --git a/src/llama-moe-cache.cpp b/src/llama-moe-cache.cpp
index f97c213e4..db30860ca 100644
--- a/src/llama-moe-cache.cpp
+++ b/src/llama-moe-cache.cpp
@@ -8,6 +8,7 @@
#include "ggml-cpp.h"
#include <algorithm>
+#include <array>
#include <cmath>
#include <cstdio>
#include <cstdlib>
@@ -287,12 +288,17 @@ struct moe_cache_lru {
}
};
+using llama_moe_cache_scales = std::array<ggml_tensor *, LLAMA_MOE_CACHE_SCALE_COUNT>;
+
// gate, up, down or gate_up, down
-static std::vector<ggml_tensor *> llama_moe_cache_layer_experts(const llama_layer & layer) {
+static std::vector<ggml_tensor *> llama_moe_cache_layer_experts(const llama_layer & layer, const llama_weight_scales * ws = nullptr, std::vector<llama_moe_cache_scales> * scales = nullptr) {
std::vector<ggml_tensor *> res;
for (ggml_tensor * t : { layer.ffn_gate_up_exps, layer.ffn_gate_exps, layer.ffn_up_exps, layer.ffn_down_exps }) {
if (t != nullptr) {
res.push_back(t);
+ if (scales != nullptr) {
+ scales->push_back({ ws->get(t), ws->get_input(t) });
+ }
}
}
return res;
@@ -338,6 +344,9 @@ struct llama_moe_cache::impl {
std::vector<ggml_tensor *> ref; // expert tensors of the first layer
std::vector<int32_t> layers;
std::vector<ggml_tensor *> banks; // device storage of all slots, one per expert tensor
+ std::vector<std::array<int64_t, LLAMA_MOE_CACHE_SCALE_COUNT>> scale_rows;
+ std::vector<llama_moe_cache_scales> scale_banks;
+ size_t n_scalar = 0; // per-tensor scales of all layers
size_t host_bytes = 0;
int32_t n_slots = 0;
moe_cache_lru lru;
@@ -352,12 +361,14 @@ struct llama_moe_cache::impl {
uint32_t cpu_acc = 0; // fraction of a miss carried over to the next plan, in 1/65536
int32_t n_used = 0; // experts selected per token by the graph, set by build_lookup
std::vector<ggml_tensor *> experts; // host expert tensors, in the order of the banks
+ std::vector<std::array<std::vector<float>, LLAMA_MOE_CACHE_SCALE_COUNT>> scales;
};
struct binding {
int32_t il;
int32_t ip; // index of the bank
ggml_tensor * cached; // view of the bank used in place of the host experts
+ llama_moe_cache_scales scales;
};
struct stats {
@@ -422,26 +433,47 @@ struct llama_moe_cache::impl {
// only cache layers that keep all of their experts in host memory, on the device the layer is assigned to
for (size_t il = 0; il < model.layers.size(); ++il) {
- auto experts = llama_moe_cache_layer_experts(model.layers[il]);
+ std::vector<llama_moe_cache_scales> scales;
+ auto experts = llama_moe_cache_layer_experts(model.layers[il], &model.w_scales, &scales);
if (experts.empty() || !std::all_of(experts.begin(), experts.end(), llama_moe_cache_is_host_weight)) {
continue;
}
+ std::vector<std::array<int64_t, LLAMA_MOE_CACHE_SCALE_COUNT>> scale_rows(experts.size(), { 0, 0 });
+ size_t n_scalar = 0;
+ for (size_t ip = 0; ip < experts.size(); ++ip) {
+ for (int k = 0; k < LLAMA_MOE_CACHE_SCALE_COUNT; ++k) {
+ const ggml_tensor * s = scales[ip][k];
+ if (s != nullptr) {
+ GGML_ASSERT(s->type == GGML_TYPE_F32 && ggml_is_contiguous(s));
+ if (ggml_nelements(s) > 1) {
+ GGML_ASSERT((s->ne[0] == experts[ip]->ne[2] && s->ne[1] == 1) ||
+ (s->ne[0] == experts[ip]->ne[1] && s->ne[1] == experts[ip]->ne[2]));
+ scale_rows[ip][k] = ggml_nelements(s)/experts[ip]->ne[2];
+ } else {
+ n_scalar++;
+ }
+ }
+ }
+ }
const auto it_dev = std::find_if(devices.begin(), devices.end(), [&](const device & d) { return ggml_backend_get_device(d.backend) == model.dev_layer(il); });
if (it_dev == devices.end()) {
continue;
}
const int32_t id = (int32_t) (it_dev - devices.begin());
- auto it = std::find_if(groups.begin(), groups.end(), [&](const group & g) { return g.id == id && llama_moe_cache_same_layout(g.ref, experts); });
+ auto it = std::find_if(groups.begin(), groups.end(), [&](const group & g) { return g.id == id && llama_moe_cache_same_layout(g.ref, experts) && g.scale_rows == scale_rows; });
if (it == groups.end()) {
groups.emplace_back();
it = groups.end() - 1;
it->id = id;
it->ref = experts;
+ it->scale_rows = scale_rows;
}
it->layers.push_back(il);
- for (const ggml_tensor * t : experts) {
- it->host_bytes += ggml_nbytes(t);
- devices[id].host_bytes += ggml_nbytes(t);
+ it->n_scalar += n_scalar;
+ for (size_t ip = 0; ip < experts.size(); ++ip) {
+ const size_t bytes = ggml_nbytes(experts[ip]) + (scale_rows[ip][0] + scale_rows[ip][1])*experts[ip]->ne[2]*sizeof(float);
+ it->host_bytes += bytes;
+ devices[id].host_bytes += bytes;
}
}
if (groups.empty()) {
@@ -455,9 +487,15 @@ struct llama_moe_cache::impl {
auto alloc_size = [&](const group & g, int32_t n_slots) {
const size_t alignment = ggml_backend_buft_get_alignment(devices[g.id].buft);
size_t res = 0;
- for (const ggml_tensor * t : g.ref) {
- res += GGML_PAD(t->nb[2]*(n_slots + n_zero + 1), alignment);
+ for (size_t ip = 0; ip < g.ref.size(); ++ip) {
+ res += GGML_PAD(g.ref[ip]->nb[2]*(n_slots + n_zero + 1), alignment);
+ for (int64_t rows : g.scale_rows[ip]) {
+ if (rows > 0) {
+ res += GGML_PAD(rows*sizeof(float)*(n_slots + n_zero + 1), alignment);
+ }
+ }
}
+ res += g.n_scalar*GGML_PAD(sizeof(float), alignment);
return res;
};
@@ -516,7 +554,11 @@ struct llama_moe_cache::impl {
continue;
}
g.lru.init(model.layers.size(), n_expert, g.n_slots);
- n_tensors[g.id] += g.ref.size()*(1 + g.layers.size());
+ size_t n_scales = 0;
+ for (const auto & rows : g.scale_rows) {
+ n_scales += std::count_if(rows.begin(), rows.end(), [](int64_t r) { return r > 0; });
+ }
+ n_tensors[g.id] += (g.ref.size() + n_scales)*(1 + g.layers.size()) + g.n_scalar;
n_tensors_host += g.layers.size()*(n_zero > 0 ? 3 : 1); // slot maps and CPU map
}
if (n_tensors_host == 0) {
@@ -543,6 +585,8 @@ struct llama_moe_cache::impl {
ctx_host = init_ctx(n_tensors_host);
ctx_views = init_ctx(2);
+ std::vector<std::pair<ggml_tensor *, const ggml_tensor *>> scalar_scales; // device copy, model tensor
+
ggml_backend_buffer_type_t buft_host = ggml_backend_cpu_buffer_type();
const size_t alignment_host = ggml_backend_buft_get_alignment(buft_host);
@@ -552,21 +596,53 @@ struct llama_moe_cache::impl {
continue;
}
ggml_context * ctx = devices[g.id].ctx.get();
- for (const ggml_tensor * t : g.ref) {
+ for (size_t ip = 0; ip < g.ref.size(); ++ip) {
+ const ggml_tensor * t = g.ref[ip];
ggml_tensor * bank = ggml_new_tensor_3d(ctx, t->type, t->ne[0], t->ne[1], g.n_slots + n_zero + 1);
GGML_ASSERT(bank->nb[2] == t->nb[2]);
ggml_format_name(bank, "moe_cache.%zu.%s", ig, t->name);
g.banks.push_back(bank);
+ llama_moe_cache_scales scale_banks = {};
+ for (int k = 0; k < LLAMA_MOE_CACHE_SCALE_COUNT; ++k) {
+ if (g.scale_rows[ip][k] > 0) {
+ scale_banks[k] = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, g.scale_rows[ip][k], g.n_slots + n_zero + 1);
+ ggml_format_name(scale_banks[k], "moe_cache.%zu.%s.%s", ig, t->name, k == LLAMA_MOE_CACHE_SCALE_WEIGHT ? "scale" : "input_scale");
+ }
+ }
+ g.scale_banks.push_back(scale_banks);
}
for (int32_t il : g.layers) {
layer & l = layers[il];
l.ig = (int32_t) ig;
- l.experts = llama_moe_cache_layer_experts(model.layers[il]);
+ std::vector<llama_moe_cache_scales> scales;
+ l.experts = llama_moe_cache_layer_experts(model.layers[il], &model.w_scales, &scales);
+ l.scales.resize(l.experts.size());
for (size_t ip = 0; ip < l.experts.size(); ++ip) {
ggml_tensor * bank = g.banks[ip];
ggml_tensor * cached = ggml_view_3d(ctx, bank, bank->ne[0], bank->ne[1], g.n_slots + n_zero, bank->nb[1], bank->nb[2], 0);
ggml_format_name(cached, "moe_cache.%s", l.experts[ip]->name);
- bindings[l.experts[ip]] = { il, (int32_t) ip, cached };
+ llama_moe_cache_scales bound = scales[ip];
+ for (int k = 0; k < LLAMA_MOE_CACHE_SCALE_COUNT; ++k) {
+ ggml_tensor * scale = scales[ip][k];
+ ggml_tensor * scale_bank = g.scale_banks[ip][k];
+ if (scale_bank == nullptr) {
+ if (scale != nullptr) {
+ bound[k] = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1);
+ ggml_format_name(bound[k], "moe_cache.%s", scale->name);
+ scalar_scales.emplace_back(bound[k], scale);
+ }
+ continue;
+ }
+ if (!no_alloc) {
+ l.scales[ip][k].resize(ggml_nelements(scale));
+ ggml_backend_tensor_get(scale, l.scales[ip][k].data(), 0, ggml_nbytes(scale));
+ }
+ const int64_t rows = g.scale_rows[ip][k];
+ bound[k] = rows == 1 ? ggml_view_1d(ctx, scale_bank, g.n_slots + n_zero, 0) :
+ ggml_view_2d(ctx, scale_bank, rows, g.n_slots + n_zero, scale_bank->nb[1], 0);
+ ggml_format_name(bound[k], "moe_cache.%s", scale->name);
+ }
+ bindings[l.experts[ip]] = { il, (int32_t) ip, cached, bound };
}
l.slot_map = ggml_new_tensor_2d(ctx_host.get(), GGML_TYPE_I32, 1, g.ref[0]->ne[2]);
ggml_format_name(l.slot_map, "moe_cache.slot_map-%d", il);
@@ -601,6 +677,22 @@ struct llama_moe_cache::impl {
ggml_backend_buffer_clear(buf_host.get(), 0xff); // the CPU maps are -1
buf_host_size = ggml_backend_buffer_get_size(buf_host.get());
+ // zero and unfilled slots keep a unit input scale: activations are divided by it
+ for (group & g : groups) {
+ for (const auto & banks : g.scale_banks) {
+ if (ggml_tensor * bank = banks[LLAMA_MOE_CACHE_SCALE_INPUT]) {
+ const std::vector<float> ones(ggml_nelements(bank), 1.0f);
+ ggml_backend_tensor_set(bank, ones.data(), 0, ggml_nbytes(bank));
+ }
+ }
+ }
+
+ for (const auto & [dst, src] : scalar_scales) {
+ float v;
+ ggml_backend_tensor_get(src, &v, 0, sizeof(v));
+ ggml_backend_tensor_set(dst, &v, 0, sizeof(v));
+ }
+
for (group & g : groups) {
for (int32_t il : g.layers) {
if (layers[il].slot_map != nullptr) {
@@ -730,6 +822,11 @@ struct llama_moe_cache::impl {
return it != bindings.end() ? it->second.cached : nullptr;
}
+ ggml_tensor * get_scales(const ggml_tensor * w, llama_moe_cache_scale kind) const {
+ const auto it = bindings.find(w);
+ return it != bindings.end() ? it->second.scales[kind] : nullptr;
+ }
+
int64_t copy_experts(ggml_backend_t backend, const ggml_tensor * w, ggml_tensor * dst, int64_t e, int64_t last) {
const auto it = bindings.find(w);
if (it == bindings.end()) {
@@ -849,6 +946,14 @@ struct llama_moe_cache::impl {
}
ggml_backend_tensor_set_async(backend, bank, (const uint8_t *) w->data + fills[i].expert*expert_size, fills[i].slot*expert_size, n*expert_size);
bytes += n*expert_size;
+ for (int k = 0; k < LLAMA_MOE_CACHE_SCALE_COUNT; ++k) {
+ if (ggml_tensor * scale_bank = g.scale_banks[ip][k]) {
+ const int64_t rows = g.scale_rows[ip][k];
+ const size_t scale_size = rows*sizeof(float);
+ ggml_backend_tensor_set_async(backend, scale_bank, l.scales[ip][k].data() + fills[i].expert*rows, fills[i].slot*scale_size, n*scale_size);
+ bytes += n*scale_size;
+ }
+ }
i += n;
}
}
@@ -916,8 +1021,10 @@ llama_moe_cache_lookup llama_moe_cache::build_lookup(const llm_graph_context & g
return {};
}
// the device of the cache must compute the matmul, else the scheduler copies the cache to another backend
- ggml_tensor * b = ggml_new_tensor_3d(ctx0, GGML_TYPE_F32, experts->ne[0], w == layer.down_exps ? selected_experts->ne[0] : 1, selected_experts->ne[1]);
- if (!ggml_backend_supports_op(pimpl->backend(il), ggml_mul_mat_id(ctx0, experts, b, selected_experts))) {
+ ggml_tensor * w_s = g.get_weight_scale(w) ? pimpl->get_scales(w, LLAMA_MOE_CACHE_SCALE_WEIGHT) : nullptr;
+ ggml_tensor * x_s = g.get_input_scale(w) ? pimpl->get_scales(w, LLAMA_MOE_CACHE_SCALE_INPUT) : nullptr;
+ ggml_tensor * b = ggml_new_tensor_3d(ctx0, GGML_TYPE_F32, experts->ne[0], w == layer.down_exps ? selected_experts->ne[0] : 1, selected_experts->ne[1]);
+ if (!ggml_backend_supports_op(pimpl->backend(il), ggml_mul_mat_id_ext(ctx0, experts, b, selected_experts, w_s, x_s))) {
return {};
}
}
@@ -968,6 +1075,10 @@ ggml_tensor * llama_moe_cache::build_experts(const llm_graph_context & g, const
return experts;
}
+ggml_tensor * llama_moe_cache::get_scales(const ggml_tensor * w, llama_moe_cache_scale kind) const {
+ return pimpl->get_scales(w, kind);
+}
+
bool llama_moe_cache::copy(ggml_backend_t backend, const ggml_tensor * src, ggml_tensor * dst, ggml_cgraph * graph) {
return pimpl->copy(backend, src, dst, graph);
}
diff --git a/src/llama-moe-cache.h b/src/llama-moe-cache.h
index 833eac360..e2e9adad5 100644
--- a/src/llama-moe-cache.h
+++ b/src/llama-moe-cache.h
@@ -30,6 +30,12 @@ struct llama_moe_cache_lookup {
// cur is the expert input and ids are the selected experts, for the biases and scales (see llm_graph_context::build_moe_ffn)
using llama_moe_cache_build_experts_t = ggml_tensor * (*)(ggml_tensor * cur, ggml_tensor * ids, ggml_tensor * mm_ids, bool cached, void * user_data);
+enum llama_moe_cache_scale {
+ LLAMA_MOE_CACHE_SCALE_WEIGHT,
+ LLAMA_MOE_CACHE_SCALE_INPUT,
+ LLAMA_MOE_CACHE_SCALE_COUNT,
+};
+
// keeps recently and repeatedly used experts of host-resident MoE layers in a device buffer
// each layer has a slot map in host memory: when the scheduler copies it to the device, the copy callback uploads the missing experts
class llama_moe_cache {
@@ -53,6 +59,9 @@ public:
ggml_tensor * build_experts(const llm_graph_context & g, const llama_moe_cache_lookup & lookup,
ggml_tensor * cur, ggml_tensor * selected_experts, llama_moe_cache_build_experts_t build, void * user_data) const;
+ // weight or input scales of w on the cache device: in cache-slot order, or a copy of the per-tensor scale
+ ggml_tensor * get_scales(const ggml_tensor * w, llama_moe_cache_scale kind) const;
+
// ggml_backend_sched copy callback, returns false if src is not a slot map
bool copy(ggml_backend_t backend, const ggml_tensor * src, ggml_tensor * dst, ggml_cgraph * graph);
diff --git a/src/llama-quant.cpp b/src/llama-quant.cpp
index c1fab892f..38eb0d588 100644
--- a/src/llama-quant.cpp
+++ b/src/llama-quant.cpp
@@ -235,7 +235,7 @@ static void llama_tensor_dequantize_impl(ggml_type type,
const int nthread
) {
const ggml_type_traits * qtype = ggml_get_type_traits(type);
- if (ggml_is_quantized(type) || type == GGML_TYPE_F8_E4M3) {
+ if (ggml_is_quantized(type) || ggml_needs_scale(type)) {
if (qtype->to_float == nullptr) {
throw std::runtime_error(format("type %s unsupported for integer quantization: no dequantization available", ggml_type_name(type)));
}
@@ -246,7 +246,7 @@ static void llama_tensor_dequantize_impl(ggml_type type,
if (nthread < 2) {
if (type == GGML_TYPE_F16) { ggml_fp16_to_fp32_row((const ggml_fp16_t *)data, f32_output, nelements); }
else if (type == GGML_TYPE_BF16) { ggml_bf16_to_fp32_row((const ggml_bf16_t *)data, f32_output, nelements); }
- else if (ggml_is_quantized(type) || type == GGML_TYPE_F8_E4M3) { qtype->to_float(data, f32_output, nelements); }
+ else if (ggml_is_quantized(type) || ggml_needs_scale(type)) { qtype->to_float(data, f32_output, nelements); }
else { GGML_ABORT("fatal error"); }
return;
@@ -3416,6 +3416,11 @@ static void llama_model_quantize_impl(const std::string & fname_inp, const std::
__func__, metadata[i].name.c_str(), ggml_type_name(metadata[i].target_type));
}
+ if (metadata[i].target_type != tensor->type && ggml_needs_scale(metadata[i].target_type)) {
+ throw std::runtime_error(format("cannot quantize %s to %s: this type needs a weight scale that llama-quantize does not compute",
+ metadata[i].name.c_str(), ggml_type_name(metadata[i].target_type)));
+ }
+
metadata[i].requires_imatrix = tensor_requires_imatrix(tensor->name, metadata[i].target_type, ftype);
if (params->imatrix) { metadata[i].remapped_imatrix_name = remap_imatrix(tensor->name, mapped); }
diff --git a/src/models/afmoe.cpp b/src/models/afmoe.cpp
index 0eb208f63..edd616035 100644
--- a/src/models/afmoe.cpp
+++ b/src/models/afmoe.cpp
@@ -163,7 +163,7 @@ llama_model_afmoe::graph::graph(const llm_graph_params & params) : llm_graph_con
cb(Kcur, "Kcur_rope", il);
cur = build_attn(inp_attn,
- NULL, NULL, NULL, // wo will be applied after gating
+ NULL, NULL, // wo will be applied after gating
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "attn_out", il);
@@ -174,7 +174,7 @@ llama_model_afmoe::graph::graph(const llm_graph_params & params) : llm_graph_con
cb(cur, "attn_gated", il);
// now apply output projection
- cur = build_lora_mm(model.layers[il].wo, cur, model.layers[il].wo_s);
+ cur = build_lora_mm(model.layers[il].wo, cur);
cb(cur, "attn_o_proj", il);
}
@@ -218,9 +218,9 @@ llama_model_afmoe::graph::graph(const llm_graph_params & params) : llm_graph_con
// shared expert
if (hparams.n_expert_shared > 0) {
ggml_tensor * ffn_shexp = build_ffn(cur,
- model.layers[il].ffn_up_shexp, NULL, NULL,
- model.layers[il].ffn_gate_shexp, NULL, NULL,
- model.layers[il].ffn_down_shexp, NULL, NULL,
+ model.layers[il].ffn_up_shexp, NULL,
+ model.layers[il].ffn_gate_shexp, NULL,
+ model.layers[il].ffn_down_shexp, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "ffn_shexp", il);
@@ -233,9 +233,9 @@ llama_model_afmoe::graph::graph(const llm_graph_params & params) : llm_graph_con
} else {
// dense layer
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -265,7 +265,7 @@ llama_model_afmoe::graph::graph(const llm_graph_params & params) : llm_graph_con
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
}
diff --git a/src/models/apertus.cpp b/src/models/apertus.cpp
index c0bb23659..ed42d1c6f 100644
--- a/src/models/apertus.cpp
+++ b/src/models/apertus.cpp
@@ -102,7 +102,7 @@ llama_model_apertus::graph::graph(const llm_graph_params & params) : llm_graph_c
cb(Vcur, "Vcur_pos", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "attn_out", il);
}
@@ -156,7 +156,7 @@ llama_model_apertus::graph::graph(const llm_graph_params & params) : llm_graph_c
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/arcee.cpp b/src/models/arcee.cpp
index 58607d82e..13d29e921 100644
--- a/src/models/arcee.cpp
+++ b/src/models/arcee.cpp
@@ -88,7 +88,7 @@ llama_model_arcee::graph::graph(const llm_graph_params & params) : llm_graph_con
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "attn_out", il);
}
@@ -109,9 +109,9 @@ llama_model_arcee::graph::graph(const llm_graph_params & params) : llm_graph_con
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- NULL, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ NULL, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_RELU_SQR, LLM_FFN_SEQ, il);
cb(cur, "ffn_out", il);
@@ -136,7 +136,7 @@ llama_model_arcee::graph::graph(const llm_graph_params & params) : llm_graph_con
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/arctic.cpp b/src/models/arctic.cpp
index a72f2043b..f69ac4f18 100644
--- a/src/models/arctic.cpp
+++ b/src/models/arctic.cpp
@@ -94,7 +94,7 @@ llama_model_arctic::graph::graph(const llm_graph_params & params) : llm_graph_co
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
@@ -113,9 +113,9 @@ llama_model_arctic::graph::graph(const llm_graph_params & params) : llm_graph_co
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -162,7 +162,7 @@ llama_model_arctic::graph::graph(const llm_graph_params & params) : llm_graph_co
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/arwkv7.cpp b/src/models/arwkv7.cpp
index 022a6738f..a2a47fd65 100644
--- a/src/models/arwkv7.cpp
+++ b/src/models/arwkv7.cpp
@@ -172,9 +172,9 @@ llama_model_arwkv7::graph::graph(const llm_graph_params & params) : llm_build_rw
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -193,7 +193,7 @@ llama_model_arwkv7::graph::graph(const llm_graph_params & params) : llm_build_rw
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/baichuan.cpp b/src/models/baichuan.cpp
index 3bccc0bff..8cb5e46c7 100644
--- a/src/models/baichuan.cpp
+++ b/src/models/baichuan.cpp
@@ -87,7 +87,7 @@ llama_model_baichuan::graph::graph(const llm_graph_params & params) : llm_graph_
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
@@ -107,9 +107,9 @@ llama_model_baichuan::graph::graph(const llm_graph_params & params) : llm_graph_
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -134,7 +134,7 @@ llama_model_baichuan::graph::graph(const llm_graph_params & params) : llm_graph_
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/bailingmoe.cpp b/src/models/bailingmoe.cpp
index 890dcaa7b..dfa3add92 100644
--- a/src/models/bailingmoe.cpp
+++ b/src/models/bailingmoe.cpp
@@ -95,7 +95,7 @@ llama_model_bailingmoe::graph::graph(const llm_graph_params & params) : llm_grap
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_rot)), il);
}
@@ -129,9 +129,9 @@ llama_model_bailingmoe::graph::graph(const llm_graph_params & params) : llm_grap
// FFN shared expert
{
ggml_tensor * ffn_shexp = build_ffn(cur,
- model.layers[il].ffn_up_shexp, NULL, NULL,
- model.layers[il].ffn_gate_shexp, NULL, NULL,
- model.layers[il].ffn_down_shexp, NULL, NULL,
+ model.layers[il].ffn_up_shexp, NULL,
+ model.layers[il].ffn_gate_shexp, NULL,
+ model.layers[il].ffn_down_shexp, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "ffn_shexp", il);
@@ -159,7 +159,7 @@ llama_model_bailingmoe::graph::graph(const llm_graph_params & params) : llm_grap
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/bailingmoe2.cpp b/src/models/bailingmoe2.cpp
index 651623e0e..6bdcded01 100644
--- a/src/models/bailingmoe2.cpp
+++ b/src/models/bailingmoe2.cpp
@@ -132,7 +132,7 @@ llama_model_bailingmoe2::graph::graph(const llm_graph_params & params) :
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
}
@@ -150,9 +150,9 @@ llama_model_bailingmoe2::graph::graph(const llm_graph_params & params) :
if (static_cast<uint32_t>(il) < hparams.n_layer_dense_lead) {
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
} else {
@@ -172,9 +172,9 @@ llama_model_bailingmoe2::graph::graph(const llm_graph_params & params) :
{
ggml_tensor * ffn_shexp =
build_ffn(cur,
- model.layers[il].ffn_up_shexp, NULL, NULL,
- model.layers[il].ffn_gate_shexp, NULL, NULL,
- model.layers[il].ffn_down_shexp, NULL, NULL,
+ model.layers[il].ffn_up_shexp, NULL,
+ model.layers[il].ffn_gate_shexp, NULL,
+ model.layers[il].ffn_down_shexp, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "ffn_shexp", il);
@@ -200,7 +200,7 @@ llama_model_bailingmoe2::graph::graph(const llm_graph_params & params) :
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/bailingmoe3.cpp b/src/models/bailingmoe3.cpp
index 199d9ecd7..112ad9276 100644
--- a/src/models/bailingmoe3.cpp
+++ b/src/models/bailingmoe3.cpp
@@ -168,7 +168,7 @@ ggml_tensor * llama_model_bailingmoe3::graph::build_causal_conv1d(
const int64_t conv_state_size = (d_conv - 1) * d_inner;
const int64_t total_state_size = 3 * conv_state_size;
- ggml_tensor * x_proj = ggml_mul_mat(ctx0, proj_w, x);
+ ggml_tensor * x_proj = build_lora_mm(proj_w, x);
x_proj = ggml_reshape_3d(ctx0, x_proj, d_inner, n_seq_tokens, n_seqs);
ggml_tensor * conv_x = nullptr;
@@ -247,7 +247,7 @@ llama_model_bailingmoe3::graph::graph(const llm_graph_params & params) :
inp_rs, conv_states_all, conv_state_all, 2, cur, layer.wv, layer.ssm_v_conv,
d_conv, head_dim, n_head, n_seq_tokens, n_seqs, il);
- ggml_tensor * gate = ggml_mul_mat(ctx0, layer.ssm_f_a, cur);
+ ggml_tensor * gate = build_lora_mm(layer.ssm_f_a, cur);
gate = ggml_add(ctx0, gate, layer.ssm_dt_b);
gate = ggml_reshape_3d(ctx0, gate, head_dim, n_head, n_tokens);
ggml_tensor * a = ggml_reshape_3d(ctx0, layer.ssm_a, 1, n_head, 1);
@@ -255,7 +255,7 @@ llama_model_bailingmoe3::graph::graph(const llm_graph_params & params) :
gate = ggml_reshape_4d(ctx0, gate, head_dim, n_head, n_seq_tokens, n_seqs);
cb(gate, "kda_gate", il);
- ggml_tensor * beta = ggml_mul_mat(ctx0, layer.ssm_beta, cur);
+ ggml_tensor * beta = build_lora_mm(layer.ssm_beta, cur);
beta = ggml_sigmoid(ctx0, ggml_reshape_4d(ctx0, beta, 1, n_head, n_seq_tokens, n_seqs));
q = build_gdn_l2_norm(ctx0, q, hparams.f_norm_rms_eps);
@@ -268,25 +268,25 @@ llama_model_bailingmoe3::graph::graph(const llm_graph_params & params) :
ggml_tensor * out = build_recurrent_attn(
inp_rs, states_all, q, k, v, gate, beta, state, il);
- ggml_tensor * out_gate = ggml_mul_mat(ctx0, layer.ssm_g_a, cur);
+ ggml_tensor * out_gate = build_lora_mm(layer.ssm_g_a, cur);
out_gate = ggml_reshape_3d(ctx0, out_gate, head_dim, n_head, n_tokens);
out = ggml_reshape_3d(ctx0, out, head_dim, n_head, n_tokens);
out = build_norm(out, layer.ssm_o_norm, nullptr, LLM_NORM_RMS, il);
out = ggml_mul(ctx0, out, ggml_sigmoid(ctx0, out_gate));
- cur = ggml_mul_mat(ctx0, layer.wo, ggml_reshape_2d(ctx0, out, d_inner, n_tokens));
+ cur = build_lora_mm(layer.wo, ggml_reshape_2d(ctx0, out, d_inner, n_tokens));
cb(cur, "kda_out", il);
} else {
ggml_tensor * attn_input = cur;
ggml_tensor * q_all;
if (layer.wq_a) {
- q_all = ggml_mul_mat(ctx0, layer.wq_a, cur);
+ q_all = build_lora_mm(layer.wq_a, cur);
cb(q_all, "q_a", il);
q_all = build_norm(q_all, layer.attn_q_a_norm, nullptr, LLM_NORM_RMS, il);
cb(q_all, "q_a_norm", il);
- q_all = ggml_mul_mat(ctx0, layer.wq_b, q_all);
+ q_all = build_lora_mm(layer.wq_b, q_all);
cb(q_all, "q_b", il);
} else {
- q_all = ggml_mul_mat(ctx0, layer.wq, cur);
+ q_all = build_lora_mm(layer.wq, cur);
}
ggml_tensor * q_nope = ggml_view_3d(ctx0, q_all, qk_nope_head_dim, n_head, n_tokens,
ggml_row_size(q_all->type, qk_head_dim),
@@ -296,7 +296,7 @@ llama_model_bailingmoe3::graph::graph(const llm_graph_params & params) :
ggml_row_size(q_all->type, qk_head_dim) * n_head,
ggml_row_size(q_all->type, qk_nope_head_dim));
- ggml_tensor * kv_all = ggml_mul_mat(ctx0, layer.wkv_a_mqa, cur);
+ ggml_tensor * kv_all = build_lora_mm(layer.wkv_a_mqa, cur);
ggml_tensor * kv = ggml_view_2d(ctx0, kv_all, kv_lora_rank, n_tokens,
ggml_row_size(kv_all->type, kv_lora_rank + qk_rope_head_dim), 0);
ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_all, qk_rope_head_dim, 1, n_tokens,
@@ -309,21 +309,21 @@ llama_model_bailingmoe3::graph::graph(const llm_graph_params & params) :
kv = build_norm(kv, layer.attn_kv_a_norm, nullptr, LLM_NORM_RMS, il);
q_nope = ggml_permute(ctx0, q_nope, 0, 2, 1, 3);
- q_nope = ggml_mul_mat(ctx0, layer.wk_b, q_nope);
+ q_nope = build_lora_mm(layer.wk_b, q_nope);
q_nope = ggml_permute(ctx0, q_nope, 0, 2, 1, 3);
ggml_tensor * q = ggml_concat(ctx0, q_nope, q_pe, 0);
kv = ggml_reshape_3d(ctx0, kv, kv_lora_rank, 1, n_tokens);
ggml_tensor * k = ggml_concat(ctx0, kv, k_pe, 0);
- cur = build_attn(inp_attn, nullptr, nullptr, nullptr,
+ cur = build_attn(inp_attn, nullptr, nullptr,
q, k, kv, nullptr, nullptr, layer.wv_b, kq_scale, il);
- ggml_tensor * attn_gate = ggml_mul_mat(ctx0, layer.wqkv_gate, attn_input);
+ ggml_tensor * attn_gate = build_lora_mm(layer.wqkv_gate, attn_input);
attn_gate = ggml_sigmoid(ctx0, ggml_reshape_3d(ctx0, attn_gate, 1, n_head, n_tokens));
cur = ggml_reshape_3d(ctx0, cur, v_head_dim, n_head, n_tokens);
cur = ggml_mul(ctx0, cur, attn_gate);
- cur = ggml_mul_mat(ctx0, layer.wo, ggml_reshape_2d(ctx0, cur, v_head_dim * n_head, n_tokens));
+ cur = build_lora_mm(layer.wo, ggml_reshape_2d(ctx0, cur, v_head_dim * n_head, n_tokens));
cb(cur, "mla_out", il);
}
@@ -337,9 +337,9 @@ llama_model_bailingmoe3::graph::graph(const llm_graph_params & params) :
if ((uint32_t) il < hparams.n_layer_dense_lead) {
cur = build_ffn(cur,
- layer.ffn_up, nullptr, nullptr,
- layer.ffn_gate, nullptr, nullptr,
- layer.ffn_down, nullptr, nullptr,
+ layer.ffn_up, nullptr,
+ layer.ffn_gate, nullptr,
+ layer.ffn_down, nullptr,
nullptr, LLM_FFN_SILU, LLM_FFN_PAR, il);
} else {
ggml_tensor * moe = build_moe_ffn(cur,
@@ -355,9 +355,9 @@ llama_model_bailingmoe3::graph::graph(const llm_graph_params & params) :
(llama_expert_gating_func_type) hparams.expert_gating_func,
il);
ggml_tensor * shared = build_ffn(cur,
- layer.ffn_up_shexp, nullptr, nullptr,
- layer.ffn_gate_shexp, nullptr, nullptr,
- layer.ffn_down_shexp, nullptr, nullptr,
+ layer.ffn_up_shexp, nullptr,
+ layer.ffn_gate_shexp, nullptr,
+ layer.ffn_down_shexp, nullptr,
nullptr, LLM_FFN_SILU, LLM_FFN_PAR, il);
cur = ggml_add(ctx0, moe, shared);
}
@@ -379,7 +379,7 @@ llama_model_bailingmoe3::graph::graph(const llm_graph_params & params) :
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = ggml_mul_mat(ctx0, model.output, cur);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
}
@@ -417,7 +417,7 @@ llama_model_bailingmoe3::graph_mtp::graph_mtp(const llm_graph_params & params) :
ggml_tensor * tok_embd = ubatch.token ? ggml_get_rows(ctx0, model.tok_embd, inp->tokens) : inp->embd;
ggml_tensor * h_norm = build_norm(inp->h, layer.nextn.hnorm, nullptr, LLM_NORM_RMS, il);
ggml_tensor * e_norm = build_norm(tok_embd, layer.nextn.enorm, nullptr, LLM_NORM_RMS, il);
- ggml_tensor * cur = ggml_mul_mat(ctx0, layer.nextn.eh_proj, ggml_concat(ctx0, e_norm, h_norm, 0));
+ ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, ggml_concat(ctx0, e_norm, h_norm, 0));
cb(cur, "mtp_eh_proj", il);
res->add_input(std::move(inp));
@@ -432,14 +432,14 @@ llama_model_bailingmoe3::graph_mtp::graph_mtp(const llm_graph_params & params) :
ggml_tensor * q_all;
if (layer.wq_a) {
- q_all = ggml_mul_mat(ctx0, layer.wq_a, cur);
+ q_all = build_lora_mm(layer.wq_a, cur);
cb(q_all, "q_a", il);
q_all = build_norm(q_all, layer.attn_q_a_norm, nullptr, LLM_NORM_RMS, il);
cb(q_all, "q_a_norm", il);
- q_all = ggml_mul_mat(ctx0, layer.wq_b, q_all);
+ q_all = build_lora_mm(layer.wq_b, q_all);
cb(q_all, "q_b", il);
} else {
- q_all = ggml_mul_mat(ctx0, layer.wq, cur);
+ q_all = build_lora_mm(layer.wq, cur);
}
ggml_tensor * q_nope = ggml_view_3d(ctx0, q_all, qk_nope_head_dim, n_head, n_tokens,
ggml_row_size(q_all->type, qk_head_dim),
@@ -449,7 +449,7 @@ llama_model_bailingmoe3::graph_mtp::graph_mtp(const llm_graph_params & params) :
ggml_row_size(q_all->type, qk_head_dim) * n_head,
ggml_row_size(q_all->type, qk_nope_head_dim));
- ggml_tensor * kv_all = ggml_mul_mat(ctx0, layer.wkv_a_mqa, cur);
+ ggml_tensor * kv_all = build_lora_mm(layer.wkv_a_mqa, cur);
ggml_tensor * kv = ggml_view_2d(ctx0, kv_all, kv_lora_rank, n_tokens,
ggml_row_size(kv_all->type, kv_lora_rank + qk_rope_head_dim), 0);
ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_all, qk_rope_head_dim, 1, n_tokens,
@@ -462,21 +462,21 @@ llama_model_bailingmoe3::graph_mtp::graph_mtp(const llm_graph_params & params) :
kv = build_norm(kv, layer.attn_kv_a_norm, nullptr, LLM_NORM_RMS, il);
q_nope = ggml_permute(ctx0, q_nope, 0, 2, 1, 3);
- q_nope = ggml_mul_mat(ctx0, layer.wk_b, q_nope);
+ q_nope = build_lora_mm(layer.wk_b, q_nope);
q_nope = ggml_permute(ctx0, q_nope, 0, 2, 1, 3);
ggml_tensor * q = ggml_concat(ctx0, q_nope, q_pe, 0);
kv = ggml_reshape_3d(ctx0, kv, kv_lora_rank, 1, n_tokens);
ggml_tensor * k = ggml_concat(ctx0, kv, k_pe, 0);
- cur = build_attn(inp_attn, nullptr, nullptr, nullptr,
+ cur = build_attn(inp_attn, nullptr, nullptr,
q, k, kv, nullptr, nullptr, layer.wv_b, kq_scale, il);
- ggml_tensor * attn_gate = ggml_mul_mat(ctx0, layer.wqkv_gate, attn_input);
+ ggml_tensor * attn_gate = build_lora_mm(layer.wqkv_gate, attn_input);
attn_gate = ggml_sigmoid(ctx0, ggml_reshape_3d(ctx0, attn_gate, 1, n_head, n_tokens));
cur = ggml_reshape_3d(ctx0, cur, v_head_dim, n_head, n_tokens);
cur = ggml_mul(ctx0, cur, attn_gate);
- cur = ggml_mul_mat(ctx0, layer.wo, ggml_cont_2d(ctx0, cur, v_head_dim * n_head, n_tokens));
+ cur = build_lora_mm(layer.wo, ggml_cont_2d(ctx0, cur, v_head_dim * n_head, n_tokens));
ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
cur = build_norm(ffn_inp, layer.ffn_norm, nullptr, LLM_NORM_RMS, il);
@@ -494,9 +494,9 @@ llama_model_bailingmoe3::graph_mtp::graph_mtp(const llm_graph_params & params) :
(llama_expert_gating_func_type) hparams.expert_gating_func,
il);
ggml_tensor * shared = build_ffn(cur,
- layer.ffn_up_shexp, nullptr, nullptr,
- layer.ffn_gate_shexp, nullptr, nullptr,
- layer.ffn_down_shexp, nullptr, nullptr,
+ layer.ffn_up_shexp, nullptr,
+ layer.ffn_gate_shexp, nullptr,
+ layer.ffn_down_shexp, nullptr,
nullptr, LLM_FFN_SILU, LLM_FFN_PAR, il);
cur = ggml_add(ctx0, moe, shared);
cur = ggml_add(ctx0, cur, ffn_inp);
@@ -506,7 +506,7 @@ llama_model_bailingmoe3::graph_mtp::graph_mtp(const llm_graph_params & params) :
res->t_h_nextn = cur;
cur = ggml_get_rows(ctx0, cur, inp_out_ids);
- cur = ggml_mul_mat(ctx0, model.output, cur);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
}
diff --git a/src/models/beam.cpp b/src/models/beam.cpp
index 8654e0c95..f54166ae2 100644
--- a/src/models/beam.cpp
+++ b/src/models/beam.cpp
@@ -167,7 +167,7 @@ llama_model_beam::graph::graph(const llm_graph_params & params) : llm_graph_cont
}
cur = build_attn(inp_attn,
- NULL, NULL, NULL, // o_proj is applied after the gate
+ NULL, NULL, // o_proj is applied after the gate
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "attn_out", il);
@@ -182,7 +182,7 @@ llama_model_beam::graph::graph(const llm_graph_params & params) : llm_graph_cont
cur = ggml_mul(ctx0, cur, gate);
cb(cur, "attn_gated", il);
- cur = build_lora_mm(layer.wo, cur, layer.wo_s);
+ cur = build_lora_mm(layer.wo, cur);
cb(cur, "attn_o_proj", il);
}
@@ -203,9 +203,9 @@ llama_model_beam::graph::graph(const llm_graph_params & params) : llm_graph_cont
if ((uint32_t) il < hparams.n_layer_dense_lead) {
cur = build_ffn(cur,
- layer.ffn_up, NULL, NULL,
- layer.ffn_gate, NULL, NULL,
- layer.ffn_down, NULL, NULL,
+ layer.ffn_up, NULL,
+ layer.ffn_gate, NULL,
+ layer.ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -242,7 +242,7 @@ llama_model_beam::graph::graph(const llm_graph_params & params) : llm_graph_cont
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
if (hparams.f_final_logit_softcapping != 0.0f) {
cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_final_logit_softcapping);
diff --git a/src/models/bert.cpp b/src/models/bert.cpp
index b98faffc9..b4978636e 100644
--- a/src/models/bert.cpp
+++ b/src/models/bert.cpp
@@ -140,7 +140,7 @@ llama_model_bert::graph::graph(const llm_graph_params & params) : llm_graph_cont
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
cb(cur, "kqv_out", il);
}
@@ -182,25 +182,25 @@ llama_model_bert::graph::graph(const llm_graph_params & params) : llm_graph_cont
} else if (model.arch == LLM_ARCH_BERT || model.arch == LLM_ARCH_NOMIC_BERT_MOE ||
model.arch == LLM_ARCH_JINA_BERT_V3) {
cur = build_ffn(cur,
- model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
- NULL, NULL, NULL,
- model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL, NULL,
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b,
+ NULL, NULL,
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
LLM_FFN_GELU, LLM_FFN_SEQ, il);
cb(cur, "ffn_out", il);
} else if (model.arch == LLM_ARCH_JINA_BERT_V2) {
const bool up_contains_gate = !model.layers[il].ffn_gate && model.layers[il].ffn_up->ne[1] != hparams.n_ff();
auto type_op = up_contains_gate ? LLM_FFN_GEGLU : LLM_FFN_GELU;
cur = build_ffn(cur,
- model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL, NULL,
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
type_op, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
} else {
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
}
diff --git a/src/models/bitnet.cpp b/src/models/bitnet.cpp
index caee27a3d..5c2145a4f 100644
--- a/src/models/bitnet.cpp
+++ b/src/models/bitnet.cpp
@@ -24,23 +24,16 @@ void llama_model_bitnet::load_arch_tensors(llama_model_loader &) {
layer.attn_sub_norm = create_tensor(tn(LLM_TENSOR_ATTN_SUB_NORM, "weight", i), {n_embd}, 0);
layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
- layer.wq_s = create_tensor(tn(LLM_TENSOR_ATTN_Q, "scale", i), {1}, TENSOR_NOT_REQUIRED);
layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
- layer.wk_s = create_tensor(tn(LLM_TENSOR_ATTN_K, "scale", i), {1}, TENSOR_NOT_REQUIRED);
layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
- layer.wv_s = create_tensor(tn(LLM_TENSOR_ATTN_V, "scale", i), {1}, TENSOR_NOT_REQUIRED);
layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
- layer.wo_s = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "scale", i), {1}, TENSOR_NOT_REQUIRED);
layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
layer.ffn_sub_norm = create_tensor(tn(LLM_TENSOR_FFN_SUB_NORM, "weight", i), {n_ff}, 0);
layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
- layer.ffn_gate_s = create_tensor(tn(LLM_TENSOR_FFN_GATE, "scale", i), {1}, TENSOR_NOT_REQUIRED);
layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
- layer.ffn_down_s = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "scale", i), {1}, TENSOR_NOT_REQUIRED);
layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
- layer.ffn_up_s = create_tensor(tn(LLM_TENSOR_FFN_UP, "scale", i), {1}, TENSOR_NOT_REQUIRED);
}
}
@@ -86,7 +79,7 @@ llama_model_bitnet::graph::graph(const llm_graph_params & params) : llm_graph_co
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- NULL, NULL, NULL,
+ NULL, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
cur = build_norm(cur,
@@ -94,7 +87,7 @@ llama_model_bitnet::graph::graph(const llm_graph_params & params) : llm_graph_co
LLM_NORM_RMS, il);
cb(cur, "attn_sub_norm", il);
- cur = build_lora_mm(model.layers[il].wo, cur, model.layers[il].wo_s);
+ cur = build_lora_mm(model.layers[il].wo, cur);
if (model.layers[il].wo_b) {
cur = ggml_add(ctx0, cur, model.layers[il].wo_b);
}
@@ -116,9 +109,9 @@ llama_model_bitnet::graph::graph(const llm_graph_params & params) : llm_graph_co
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, model.layers[il].ffn_up_s,
- model.layers[il].ffn_gate, NULL, model.layers[il].ffn_gate_s,
- NULL, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ NULL, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_sub_out", il);
@@ -128,7 +121,7 @@ llama_model_bitnet::graph::graph(const llm_graph_params & params) : llm_graph_co
LLM_NORM_RMS, il);
cb(cur, "ffn_sub_norm", il);
- cur = build_lora_mm(model.layers[il].ffn_down, cur, model.layers[il].ffn_down_s);
+ cur = build_lora_mm(model.layers[il].ffn_down, cur);
cb(cur, "ffn_down", il);
cur = ggml_add(ctx0, cur, ffn_inp);
diff --git a/src/models/bloom.cpp b/src/models/bloom.cpp
index 076927e0d..cc4e7ce8a 100644
--- a/src/models/bloom.cpp
+++ b/src/models/bloom.cpp
@@ -95,7 +95,7 @@ llama_model_bloom::graph::graph(const llm_graph_params & params) : llm_graph_con
n_embd_head, n_head, n_head_kv, il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
@@ -117,9 +117,9 @@ llama_model_bloom::graph::graph(const llm_graph_params & params) : llm_graph_con
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
- NULL, NULL, NULL,
- model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b,
+ NULL, NULL,
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b,
NULL,
LLM_FFN_GELU, LLM_FFN_SEQ, il);
cb(cur, "ffn_out", il);
@@ -142,7 +142,7 @@ llama_model_bloom::graph::graph(const llm_graph_params & params) : llm_graph_con
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/chameleon.cpp b/src/models/chameleon.cpp
index ab16927c7..599890705 100644
--- a/src/models/chameleon.cpp
+++ b/src/models/chameleon.cpp
@@ -111,7 +111,7 @@ llama_model_chameleon::graph::graph(const llm_graph_params & params) : llm_graph
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, nullptr, model.layers[il].wo_s,
+ model.layers[il].wo, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
@@ -138,9 +138,9 @@ llama_model_chameleon::graph::graph(const llm_graph_params & params) : llm_graph
}
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -172,7 +172,7 @@ llama_model_chameleon::graph::graph(const llm_graph_params & params) : llm_graph
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output_with_img_logits", -1);
// TODO: this suppresses the output of image tokens, which is required to enable text-only outputs.
diff --git a/src/models/chatglm.cpp b/src/models/chatglm.cpp
index e7442b75f..917afd802 100644
--- a/src/models/chatglm.cpp
+++ b/src/models/chatglm.cpp
@@ -95,7 +95,7 @@ llama_model_chatglm::graph::graph(const llm_graph_params & params) : llm_graph_c
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
@@ -117,9 +117,9 @@ llama_model_chatglm::graph::graph(const llm_graph_params & params) : llm_graph_c
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- NULL, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ NULL, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
cb(cur, "ffn_out", il);
@@ -143,7 +143,7 @@ llama_model_chatglm::graph::graph(const llm_graph_params & params) : llm_graph_c
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/clef.cpp b/src/models/clef.cpp
index c41c93cb1..0add0655b 100644
--- a/src/models/clef.cpp
+++ b/src/models/clef.cpp
@@ -298,9 +298,9 @@ llama_model_clef::graph::graph(const llm_graph_params & params) :
cb(cur, "attn_post_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, model.layers[il].ffn_up_s,
- model.layers[il].ffn_gate, NULL, model.layers[il].ffn_gate_s,
- model.layers[il].ffn_down, NULL, model.layers[il].ffn_down_s,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -406,13 +406,13 @@ ggml_tensor * llama_model_clef::graph::build_layer_attn(
const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f / sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
cur = build_attn(inp,
- nullptr, nullptr, nullptr,
+ nullptr, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "attn_pregate", il);
cur = ggml_mul(ctx0, cur, ggml_sigmoid(ctx0, gate));
- cur = build_lora_mm(model.layers[il].wo, cur, model.layers[il].wo_s);
+ cur = build_lora_mm(model.layers[il].wo, cur);
cb(cur, "attn_output", il);
return cur;
@@ -431,16 +431,16 @@ ggml_tensor * llama_model_clef::graph::build_layer_attn_linear(
const int64_t head_v_dim = d_inner / num_v_heads;
const int64_t n_seqs = 1;
- ggml_tensor * qkv_mixed = build_lora_mm(layer.wqkv, cur, layer.wqkv_s);
+ ggml_tensor * qkv_mixed = build_lora_mm(layer.wqkv, cur);
qkv_mixed = ggml_reshape_3d(ctx0, qkv_mixed, qkv_mixed->ne[0], n_tokens, n_seqs);
- ggml_tensor * z = build_lora_mm(layer.wqkv_gate, cur, layer.wqkv_gate_s);
+ ggml_tensor * z = build_lora_mm(layer.wqkv_gate, cur);
- ggml_tensor * beta = build_lora_mm(layer.ssm_beta, cur, layer.ssm_beta_s);
+ ggml_tensor * beta = build_lora_mm(layer.ssm_beta, cur);
beta = ggml_reshape_4d(ctx0, beta, 1, num_v_heads, n_tokens, n_seqs);
beta = ggml_sigmoid(ctx0, beta);
- ggml_tensor * alpha = build_lora_mm(layer.ssm_alpha, cur, layer.ssm_alpha_s);
+ ggml_tensor * alpha = build_lora_mm(layer.ssm_alpha, cur);
alpha = ggml_reshape_3d(ctx0, alpha, num_v_heads, n_tokens, n_seqs);
ggml_tensor * gate = ggml_softplus(ctx0, ggml_add(ctx0, alpha, layer.ssm_dt));
@@ -484,7 +484,7 @@ ggml_tensor * llama_model_clef::graph::build_layer_attn_linear(
output = ggml_reshape_3d(ctx0, output, head_v_dim * num_v_heads, n_tokens, n_seqs);
- cur = build_lora_mm(layer.ssm_out, output, layer.ssm_out_s);
+ cur = build_lora_mm(layer.ssm_out, output);
cb(cur, "linear_attn_out", il);
return ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
@@ -502,9 +502,9 @@ ggml_tensor * llama_model_clef::graph::build_head_attn(ggml_tensor * q, ggml_ten
const int64_t n_q = q->ne[1];
const int64_t n_kv = kv->ne[1];
- ggml_tensor * Qcur = ggml_add(ctx0, ggml_mul_mat(ctx0, a.wq, q), a.bq);
- ggml_tensor * Kcur = ggml_add(ctx0, ggml_mul_mat(ctx0, a.wk, kv), a.bk);
- ggml_tensor * Vcur = ggml_add(ctx0, ggml_mul_mat(ctx0, a.wv, kv), a.bv);
+ ggml_tensor * Qcur = ggml_add(ctx0, build_lora_mm(a.wq, q), a.bq);
+ ggml_tensor * Kcur = ggml_add(ctx0, build_lora_mm(a.wk, kv), a.bk);
+ ggml_tensor * Vcur = ggml_add(ctx0, build_lora_mm(a.wv, kv), a.bv);
Qcur = ggml_permute(ctx0, ggml_reshape_3d(ctx0, Qcur, d_head, n_head_h, n_q), 0, 2, 1, 3); // [d_head, n_q, n_head]
Kcur = ggml_permute(ctx0, ggml_reshape_3d(ctx0, Kcur, d_head, n_head_h, n_kv), 0, 2, 1, 3); // [d_head, n_kv, n_head]
@@ -517,14 +517,14 @@ ggml_tensor * llama_model_clef::graph::build_head_attn(ggml_tensor * q, ggml_ten
ggml_tensor * cur = ggml_mul_mat(ctx0, Vcur, kq); // [d_head, n_q, n_head]
cur = ggml_cont_2d(ctx0, ggml_permute(ctx0, cur, 0, 2, 1, 3), n_embd_h, n_q);
- return ggml_add(ctx0, ggml_mul_mat(ctx0, a.wo, cur), a.bo);
+ return ggml_add(ctx0, build_lora_mm(a.wo, cur), a.bo);
}
ggml_tensor * llama_model_clef::graph::build_head_ffn(ggml_tensor * cur, const head_layer & layer) {
cur = build_head_norm(cur, layer.ffn_norm);
- cur = ggml_add(ctx0, ggml_mul_mat(ctx0, layer.ffn_up, cur), layer.ffn_up_b);
+ cur = ggml_add(ctx0, build_lora_mm(layer.ffn_up, cur), layer.ffn_up_b);
cur = ggml_gelu_erf(ctx0, cur);
- return ggml_add(ctx0, ggml_mul_mat(ctx0, layer.ffn_down, cur), layer.ffn_down_b);
+ return ggml_add(ctx0, build_lora_mm(layer.ffn_down, cur), layer.ffn_down_b);
}
// ref: JointSchemaHead in joint_schema_model.py of the model repo
@@ -546,7 +546,7 @@ ggml_tensor * llama_model_clef::graph::build_head(ggml_tensor * hidden, input_de
hidden = build_head_norm(hidden, model.hidden_norm);
- ggml_tensor * memory = ggml_mul_mat(ctx0, model.proj_memory, hidden); // [n_embd_h, n_tokens]
+ ggml_tensor * memory = build_lora_mm(model.proj_memory, hidden); // [n_embd_h, n_tokens]
const int64_t n_embd_h = memory->ne[0];
@@ -563,9 +563,9 @@ ggml_tensor * llama_model_clef::graph::build_head(ggml_tensor * hidden, input_de
lexical = ggml_mul_mat(ctx0, ggml_cont(ctx0, ggml_transpose(ctx0, lexical)), inp->pool_o); // [n_embd, n_options]
// one query per option
- ggml_tensor * options = ggml_mul_mat(ctx0, model.proj_option_context, opt_ctx);
- options = ggml_add(ctx0, options, ggml_mul_mat(ctx0, model.proj_option_lexical, lexical));
- options = ggml_add(ctx0, options, ggml_get_rows(ctx0, ggml_mul_mat(ctx0, model.proj_option_question, question), inp->option_question));
+ ggml_tensor * options = build_lora_mm(model.proj_option_context, opt_ctx);
+ options = ggml_add(ctx0, options, build_lora_mm(model.proj_option_lexical, lexical));
+ options = ggml_add(ctx0, options, ggml_get_rows(ctx0, build_lora_mm(model.proj_option_question, question), inp->option_question));
// the options read the prompt
for (uint32_t il = 0; il < model.n_layer_routing; ++il) {
@@ -581,7 +581,7 @@ ggml_tensor * llama_model_clef::graph::build_head(ggml_tensor * hidden, input_de
cb(options, "decision_options", -1);
// one vector per question: its text, a summary of its options, the end of the prompt and its type
- ggml_tensor * fields = ggml_mul_mat(ctx0, model.proj_question, question); // [n_embd_h, n_questions]
+ ggml_tensor * fields = build_lora_mm(model.proj_question, question); // [n_embd_h, n_questions]
{
// each question weights its own options
ggml_tensor * weights = ggml_mul_mat(ctx0, options, fields); // [n_options, n_questions]
@@ -591,7 +591,7 @@ ggml_tensor * llama_model_clef::graph::build_head(ggml_tensor * hidden, input_de
ggml_tensor * summary = ggml_mul_mat(ctx0, ggml_cont(ctx0, ggml_transpose(ctx0, options)), weights);
fields = ggml_add(ctx0, fields, build_head_norm(summary, model.option_summary_norm));
- fields = ggml_add(ctx0, fields, ggml_mul_mat(ctx0, model.proj_global, global));
+ fields = ggml_add(ctx0, fields, build_lora_mm(model.proj_global, global));
fields = ggml_add(ctx0, fields, ggml_get_rows(ctx0, model.type_embd, inp->question_type));
}
@@ -627,9 +627,9 @@ ggml_tensor * llama_model_clef::graph::build_head(ggml_tensor * hidden, input_de
features = ggml_concat(ctx0, features, ggml_mul(ctx0, field, options), 0);
features = ggml_concat(ctx0, features, ggml_abs(ctx0, ggml_sub(ctx0, field, options)), 0);
- ggml_tensor * residual = ggml_add(ctx0, ggml_mul_mat(ctx0, model.scorer, features), model.scorer_b);
+ ggml_tensor * residual = ggml_add(ctx0, build_lora_mm(model.scorer, features), model.scorer_b);
residual = ggml_gelu_erf(ctx0, residual);
- residual = ggml_add(ctx0, ggml_mul_mat(ctx0, model.scorer_out, residual), model.scorer_out_b); // [1, n_options]
+ residual = ggml_add(ctx0, build_lora_mm(model.scorer_out, residual), model.scorer_out_b); // [1, n_options]
auto scale = [&](int i) {
return ggml_view_1d(ctx0, model.scales, 1, i * ggml_element_size(model.scales));
diff --git a/src/models/clover-moe.cpp b/src/models/clover-moe.cpp
index 837997261..bc1519d65 100644
--- a/src/models/clover-moe.cpp
+++ b/src/models/clover-moe.cpp
@@ -181,7 +181,7 @@ llama_model_clover_moe::graph::graph(const llm_graph_params & params) :
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
@@ -193,11 +193,11 @@ std::pair<ggml_tensor *, ggml_tensor *> llama_model_clover_moe::graph::build_qkv
const int64_t n_seqs = ubatch.n_seqs;
const int64_t n_seq_tokens = ubatch.n_seq_tokens;
- ggml_tensor * qkv_mixed = build_lora_mm(model.layers[il].wqkv, input, model.layers[il].wqkv_s);
+ ggml_tensor * qkv_mixed = build_lora_mm(model.layers[il].wqkv, input);
qkv_mixed = ggml_reshape_3d(ctx0, qkv_mixed, qkv_mixed->ne[0], n_seq_tokens, n_seqs);
cb(qkv_mixed, "linear_attn_qkv_mixed", il);
- ggml_tensor * z = build_lora_mm(model.layers[il].wqkv_gate, input, model.layers[il].wqkv_gate_s);
+ ggml_tensor * z = build_lora_mm(model.layers[il].wqkv_gate, input);
cb(z, "z", il);
return { qkv_mixed, z };
@@ -262,7 +262,7 @@ ggml_tensor * llama_model_clover_moe::graph::build_layer_attn(
const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f / sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
cur = build_attn(inp,
- nullptr, nullptr, nullptr,
+ nullptr, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "attn_pregate", il);
@@ -272,7 +272,7 @@ ggml_tensor * llama_model_clover_moe::graph::build_layer_attn(
cur = ggml_mul(ctx0, cur, gate_sigmoid);
cb(cur, "attn_gated", il);
- cur = build_lora_mm(model.layers[il].wo, cur, model.layers[il].wo_s);
+ cur = build_lora_mm(model.layers[il].wo, cur);
cb(cur, "attn_output", il);
return cur;
@@ -300,8 +300,8 @@ ggml_tensor * llama_model_clover_moe::graph::build_layer_attn_linear(
ggml_tensor * qkv_mixed = qkvz.first;
ggml_tensor * z = qkvz.second;
- ggml_tensor * beta = build_lora_mm(model.layers[il].ssm_beta, cur, model.layers[il].ssm_beta_s);
- ggml_tensor * alpha = build_lora_mm(model.layers[il].ssm_alpha, cur, model.layers[il].ssm_alpha_s);
+ ggml_tensor * beta = build_lora_mm(model.layers[il].ssm_beta, cur);
+ ggml_tensor * alpha = build_lora_mm(model.layers[il].ssm_alpha, cur);
beta = ggml_reshape_4d(ctx0, beta, 1, num_v_heads, n_seq_tokens, n_seqs);
cb(beta, "beta", il);
@@ -378,7 +378,7 @@ ggml_tensor * llama_model_clover_moe::graph::build_layer_attn_linear(
ggml_tensor * final_output = ggml_reshape_2d(ctx0, attn_out_norm, head_v_dim * num_v_heads, n_seq_tokens * n_seqs);
cb(final_output, "final_output", il);
- cur = build_lora_mm(model.layers[il].ssm_out, final_output, model.layers[il].ssm_out_s);
+ cur = build_lora_mm(model.layers[il].ssm_out, final_output);
cb(cur, "linear_attn_out", il);
return cur;
@@ -403,9 +403,9 @@ ggml_tensor * llama_model_clover_moe::graph::build_layer_ffn(ggml_tensor * cur,
if (model.layers[il].ffn_up_shexp != nullptr) {
ggml_tensor * ffn_shexp = build_ffn(cur,
- model.layers[il].ffn_up_shexp, NULL, model.layers[il].ffn_up_shexp_s,
- model.layers[il].ffn_gate_shexp, NULL, model.layers[il].ffn_gate_shexp_s,
- model.layers[il].ffn_down_shexp, NULL, model.layers[il].ffn_down_shexp_s,
+ model.layers[il].ffn_up_shexp, NULL,
+ model.layers[il].ffn_gate_shexp, NULL,
+ model.layers[il].ffn_down_shexp, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "ffn_shexp", il);
diff --git a/src/models/codeshell.cpp b/src/models/codeshell.cpp
index e316602f5..b37af8c12 100644
--- a/src/models/codeshell.cpp
+++ b/src/models/codeshell.cpp
@@ -88,7 +88,7 @@ llama_model_codeshell::graph::graph(const llm_graph_params & params) : llm_graph
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
@@ -110,9 +110,9 @@ llama_model_codeshell::graph::graph(const llm_graph_params & params) : llm_graph
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
- NULL, NULL, NULL,
- model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b,
+ NULL, NULL,
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b,
NULL,
LLM_FFN_GELU, LLM_FFN_SEQ, il);
cb(cur, "ffn_out", il);
@@ -135,7 +135,7 @@ llama_model_codeshell::graph::graph(const llm_graph_params & params) : llm_graph
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/cogvlm.cpp b/src/models/cogvlm.cpp
index 657b7d89d..36c8d1093 100644
--- a/src/models/cogvlm.cpp
+++ b/src/models/cogvlm.cpp
@@ -95,20 +95,18 @@ llama_model_cogvlm::graph::graph(const llm_graph_params & params) :
for (int il = 0; il < n_layer; ++il) {
// get either the text or image weight tensors
- ggml_tensor *wqkv, *wo, *wo_s;
+ ggml_tensor *wqkv, *wo;
ggml_tensor *ffn_gate, *ffn_down, *ffn_up;
if (is_text) {
wqkv = model.layers[il].wqkv;
wo = model.layers[il].wo;
- wo_s = model.layers[il].wo_s;
ffn_gate = model.layers[il].ffn_gate;
ffn_down = model.layers[il].ffn_down;
ffn_up = model.layers[il].ffn_up;
} else {
wqkv = model.layers[il].visexp_attn_wqkv;
wo = model.layers[il].visexp_attn_wo;
- wo_s = nullptr;
ffn_gate = model.layers[il].visexp_ffn_gate;
ffn_down = model.layers[il].visexp_ffn_down;
ffn_up = model.layers[il].visexp_ffn_up;
@@ -133,7 +131,7 @@ llama_model_cogvlm::graph::graph(const llm_graph_params & params) :
Kcur = build_rope(Kcur, inp_pos, il);
cur = build_attn(inp_attn,
- wo, nullptr, wo_s,
+ wo, nullptr,
Qcur, Kcur, Vcur,
nullptr, nullptr, nullptr,
kq_scale, il);
@@ -147,9 +145,9 @@ llama_model_cogvlm::graph::graph(const llm_graph_params & params) :
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- ffn_up, NULL, NULL,
- ffn_gate, NULL, NULL,
- ffn_down, NULL, NULL,
+ ffn_up, NULL,
+ ffn_gate, NULL,
+ ffn_down, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cur = ggml_add(ctx0, cur, ffn_inp);
@@ -168,7 +166,7 @@ llama_model_cogvlm::graph::graph(const llm_graph_params & params) :
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
}
diff --git a/src/models/cohere2.cpp b/src/models/cohere2.cpp
index 251b2da62..6acbaef73 100644
--- a/src/models/cohere2.cpp
+++ b/src/models/cohere2.cpp
@@ -99,7 +99,7 @@ llama_model_cohere2::graph::graph(const llm_graph_params & params) : llm_graph_c
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
@@ -114,9 +114,9 @@ llama_model_cohere2::graph::graph(const llm_graph_params & params) : llm_graph_c
// feed-forward network
{
cur = build_ffn(ffn_inp,
- model.layers[il].ffn_up, NULL, model.layers[il].ffn_up_s,
- model.layers[il].ffn_gate, NULL, model.layers[il].ffn_gate_s,
- model.layers[il].ffn_down, NULL, model.layers[il].ffn_down_s,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
}
@@ -140,7 +140,7 @@ llama_model_cohere2::graph::graph(const llm_graph_params & params) : llm_graph_c
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
if (f_logit_scale) {
cur = ggml_scale(ctx0, cur, f_logit_scale);
diff --git a/src/models/cohere2moe.cpp b/src/models/cohere2moe.cpp
index c4d778e50..70a1418cb 100644
--- a/src/models/cohere2moe.cpp
+++ b/src/models/cohere2moe.cpp
@@ -192,7 +192,7 @@ llama_model_cohere2moe::graph::graph(const llm_graph_params & params) : llm_grap
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- layer.wo, layer.wo_b, layer.wo_s,
+ layer.wo, layer.wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr,
1.0f / sqrtf(float(n_embd_head)), il);
}
@@ -209,9 +209,9 @@ llama_model_cohere2moe::graph::graph(const llm_graph_params & params) : llm_grap
if (layer.ffn_gate_inp == nullptr) {
cur = build_ffn(ffn_inp,
- layer.ffn_up, nullptr, layer.ffn_up_s,
- layer.ffn_gate, nullptr, layer.ffn_gate_s,
- layer.ffn_down, nullptr, layer.ffn_down_s,
+ layer.ffn_up, nullptr,
+ layer.ffn_gate, nullptr,
+ layer.ffn_down, nullptr,
nullptr, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
} else {
@@ -231,9 +231,9 @@ llama_model_cohere2moe::graph::graph(const llm_graph_params & params) : llm_grap
if (layer.ffn_up_shexp) {
ggml_tensor * ffn_shexp = build_ffn(ffn_inp,
- layer.ffn_up_shexp, nullptr, layer.ffn_up_shexp_s,
- layer.ffn_gate_shexp, nullptr, layer.ffn_gate_shexp_s,
- layer.ffn_down_shexp, nullptr, layer.ffn_down_shexp_s,
+ layer.ffn_up_shexp, nullptr,
+ layer.ffn_gate_shexp, nullptr,
+ layer.ffn_down_shexp, nullptr,
nullptr, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "ffn_shexp", il);
@@ -332,7 +332,7 @@ llama_model_cohere2moe::graph_mtp::graph_mtp(const llm_graph_params & params) :
ggml_tensor * concat = ggml_concat(ctx0, e_norm, h_norm, /*dim=*/ 0);
cb(concat, "mtp_concat", il);
- ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat, layer.nextn.eh_proj_s);
+ ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat);
cb(cur, "mtp_eh_proj", il);
ggml_tensor * inpL = cur;
@@ -350,7 +350,7 @@ llama_model_cohere2moe::graph_mtp::graph_mtp(const llm_graph_params & params) :
cb(Vcur, "mtp_Vcur", il);
cur = build_attn(inp_attn,
- layer.wo, layer.wo_b, layer.wo_s,
+ layer.wo, layer.wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr,
1.0f / sqrtf(float(n_embd_head)), il);
cb(cur, "mtp_attn_out", il);
@@ -373,9 +373,9 @@ llama_model_cohere2moe::graph_mtp::graph_mtp(const llm_graph_params & params) :
if (layer.ffn_up_shexp) {
ggml_tensor * ffn_shexp = build_ffn(ffn_inp,
- layer.ffn_up_shexp, nullptr, layer.ffn_up_shexp_s,
- layer.ffn_gate_shexp, nullptr, layer.ffn_gate_shexp_s,
- layer.ffn_down_shexp, nullptr, layer.ffn_down_shexp_s,
+ layer.ffn_up_shexp, nullptr,
+ layer.ffn_gate_shexp, nullptr,
+ layer.ffn_down_shexp, nullptr,
nullptr, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "mtp_ffn_shexp", il);
@@ -402,7 +402,7 @@ llama_model_cohere2moe::graph_mtp::graph_mtp(const llm_graph_params & params) :
ggml_tensor * head_w = layer.nextn.shared_head_head ? layer.nextn.shared_head_head : model.output;
GGML_ASSERT(head_w && "COHERE2MOE MTP: missing LM head (nextn.shared_head_head or model.output)");
- cur = build_lora_mm(head_w, cur, layer.nextn.shared_head_head ? layer.nextn.shared_head_head_s : nullptr);
+ cur = build_lora_mm(head_w, cur);
if (hparams.f_logit_scale) {
cur = ggml_scale(ctx0, cur, hparams.f_logit_scale);
diff --git a/src/models/command-r.cpp b/src/models/command-r.cpp
index bea97e6d8..056cb04f8 100644
--- a/src/models/command-r.cpp
+++ b/src/models/command-r.cpp
@@ -93,7 +93,7 @@ llama_model_command_r::graph::graph(const llm_graph_params & params) :
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -106,9 +106,9 @@ llama_model_command_r::graph::graph(const llm_graph_params & params) :
// feed-forward network
{
cur = build_ffn(ffn_inp,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
}
@@ -130,7 +130,7 @@ llama_model_command_r::graph::graph(const llm_graph_params & params) :
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
if (f_logit_scale) {
cur = ggml_scale(ctx0, cur, f_logit_scale);
diff --git a/src/models/dbrx.cpp b/src/models/dbrx.cpp
index 343040e0b..10910c229 100644
--- a/src/models/dbrx.cpp
+++ b/src/models/dbrx.cpp
@@ -84,7 +84,7 @@ llama_model_dbrx::graph::graph(const llm_graph_params & params) : llm_graph_cont
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
@@ -136,7 +136,7 @@ llama_model_dbrx::graph::graph(const llm_graph_params & params) : llm_graph_cont
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/deci.cpp b/src/models/deci.cpp
index 114b12640..f1e593c69 100644
--- a/src/models/deci.cpp
+++ b/src/models/deci.cpp
@@ -131,7 +131,7 @@ llama_model_deci::graph::graph(const llm_graph_params & params) : llm_graph_cont
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -154,9 +154,9 @@ llama_model_deci::graph::graph(const llm_graph_params & params) : llm_graph_cont
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
- model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
- model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b,
+ model.layers[il].ffn_gate, model.layers[il].ffn_gate_b,
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
}
@@ -177,7 +177,7 @@ llama_model_deci::graph::graph(const llm_graph_params & params) : llm_graph_cont
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/deepseek.cpp b/src/models/deepseek.cpp
index c6a6d6a97..8b7865f24 100644
--- a/src/models/deepseek.cpp
+++ b/src/models/deepseek.cpp
@@ -114,7 +114,7 @@ llama_model_deepseek::graph::graph(const llm_graph_params & params) :
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -129,9 +129,9 @@ llama_model_deepseek::graph::graph(const llm_graph_params & params) :
if ((uint32_t) il < hparams.n_layer_dense_lead) {
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
} else {
@@ -153,9 +153,9 @@ llama_model_deepseek::graph::graph(const llm_graph_params & params) :
{
ggml_tensor * ffn_shexp =
build_ffn(cur,
- model.layers[il].ffn_up_shexp, NULL, NULL,
- model.layers[il].ffn_gate_shexp, NULL, NULL,
- model.layers[il].ffn_down_shexp, NULL, NULL,
+ model.layers[il].ffn_up_shexp, NULL,
+ model.layers[il].ffn_gate_shexp, NULL,
+ model.layers[il].ffn_down_shexp, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "ffn_shexp", il);
@@ -179,7 +179,7 @@ llama_model_deepseek::graph::graph(const llm_graph_params & params) :
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/deepseek2.cpp b/src/models/deepseek2.cpp
index 96c628ff7..b8f6073ec 100644
--- a/src/models/deepseek2.cpp
+++ b/src/models/deepseek2.cpp
@@ -247,7 +247,7 @@ llama_model_deepseek2::graph_mtp::graph_mtp(const llm_graph_params & params) :
ggml_tensor * concat = ggml_concat(ctx0, e_norm, h_norm, 0);
cb(concat, "mtp_concat", il);
- ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat, layer.nextn.eh_proj_s);
+ ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat);
cb(cur, "mtp_eh_proj", il);
ggml_tensor * inpSA = cur;
@@ -255,13 +255,13 @@ llama_model_deepseek2::graph_mtp::graph_mtp(const llm_graph_params & params) :
cur = build_norm(cur, layer.attn_norm, nullptr, LLM_NORM_RMS, il);
cb(cur, "mtp_attn_norm", il);
- ggml_tensor * q = ggml_mul_mat(ctx0, layer.wq_a, cur);
+ ggml_tensor * q = build_lora_mm(layer.wq_a, cur);
cb(q, "mtp_q_a", il);
q = build_norm(q, layer.attn_q_a_norm, nullptr, LLM_NORM_RMS, il);
cb(q, "mtp_q_a_norm", il);
- q = ggml_mul_mat(ctx0, layer.wq_b, q);
+ q = build_lora_mm(layer.wq_b, q);
cb(q, "mtp_q_b", il);
ggml_tensor * q_nope =
@@ -277,7 +277,7 @@ llama_model_deepseek2::graph_mtp::graph_mtp(const llm_graph_params & params) :
ggml_row_size(q->type, n_embd_head_qk_nope));
cb(q_pe, "mtp_q_pe", il);
- ggml_tensor * kv_cmpr_pe = ggml_mul_mat(ctx0, layer.wkv_a_mqa, cur);
+ ggml_tensor * kv_cmpr_pe = build_lora_mm(layer.wkv_a_mqa, cur);
cb(kv_cmpr_pe, "mtp_kv_cmpr_pe", il);
ggml_tensor * kv_cmpr =
@@ -315,7 +315,7 @@ llama_model_deepseek2::graph_mtp::graph_mtp(const llm_graph_params & params) :
q_nope = ggml_permute(ctx0, q_nope, 0, 2, 1, 3);
cb(q_nope, "mtp_q_nope_perm", il);
- ggml_tensor * q_nope_absorbed = ggml_mul_mat(ctx0, layer.wk_b, q_nope);
+ ggml_tensor * q_nope_absorbed = build_lora_mm(layer.wk_b, q_nope);
cb(q_nope_absorbed, "mtp_q_nope_absorbed", il);
q_nope_absorbed = ggml_permute(ctx0, q_nope_absorbed, 0, 2, 1, 3);
@@ -334,7 +334,7 @@ llama_model_deepseek2::graph_mtp::graph_mtp(const llm_graph_params & params) :
cb(Vcur, "mtp_Vcur", il);
cur = build_attn(inp_attn_k,
- layer.wo, nullptr, layer.wo_s,
+ layer.wo, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, layer.wv_b, kq_scale, il);
cb(cur, "mtp_attn_out", il);
@@ -360,9 +360,9 @@ llama_model_deepseek2::graph_mtp::graph_mtp(const llm_graph_params & params) :
cb(moe_out, "mtp_ffn_moe_out", il);
ggml_tensor * ffn_shexp = build_ffn(cur,
- layer.ffn_up_shexp, nullptr, nullptr,
- layer.ffn_gate_shexp, nullptr, nullptr,
- layer.ffn_down_shexp, nullptr, nullptr,
+ layer.ffn_up_shexp, nullptr,
+ layer.ffn_gate_shexp, nullptr,
+ layer.ffn_down_shexp, nullptr,
nullptr, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "mtp_ffn_shexp", il);
@@ -390,13 +390,9 @@ llama_model_deepseek2::graph_mtp::graph_mtp(const llm_graph_params & params) :
? layer.nextn.shared_head_head
: model.output;
- ggml_tensor * head_s = layer.nextn.shared_head_head
- ? layer.nextn.shared_head_head_s
- : model.output_s;
-
GGML_ASSERT(head_w && "GLM4 MTP: missing LM head (nextn.shared_head_head or model.output)");
- cur = build_lora_mm(head_w, cur, head_s);
+ cur = build_lora_mm(head_w, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
@@ -475,7 +471,7 @@ llama_model_deepseek2::graph::graph(const llm_graph_params & params) :
cb(Kcur, "k_pe", il);
cur = build_attn(inp_attn_kv,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "attn_out", il);
}
@@ -485,23 +481,23 @@ llama_model_deepseek2::graph::graph(const llm_graph_params & params) :
const bool is_lite = model.layers[il].wq;
if (!is_lite) {
- q = ggml_mul_mat(ctx0, model.layers[il].wq_a, cur);
+ q = build_lora_mm(model.layers[il].wq_a, cur);
cb(q, "q", il);
q = build_norm(q, model.layers[il].attn_q_a_norm, nullptr, LLM_NORM_RMS, il);
cb(q, "q", il);
- q = ggml_mul_mat(ctx0, model.layers[il].wq_b, q);
+ q = build_lora_mm(model.layers[il].wq_b, q);
cb(q, "q", il);
} else {
- q = ggml_mul_mat(ctx0, model.layers[il].wq, cur);
+ q = build_lora_mm(model.layers[il].wq, cur);
cb(q, "q", il);
}
// {n_embd_head_k, n_head, n_tokens}
q = ggml_reshape_3d(ctx0, q, n_embd_head_k, n_head, n_tokens);
cb(q, "q", il);
- ggml_tensor * kv_cmpr_pe = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);
+ ggml_tensor * kv_cmpr_pe = build_lora_mm(model.layers[il].wkv_a_mqa, cur);
cb(kv_cmpr_pe, "kv_cmpr_pe", il);
// split into {kv_lora_rank, n_tokens}
@@ -542,7 +538,7 @@ llama_model_deepseek2::graph::graph(const llm_graph_params & params) :
cb(q_nope, "q_nope_perm", il);
// {n_embd_head_qk_nope, kv_lora_rank, n_head} x {n_embd_head_qk_nope, n_tokens, n_head}
- ggml_tensor * q_nope_absorbed = ggml_mul_mat(ctx0, model.layers[il].wk_b, q_nope);
+ ggml_tensor * q_nope_absorbed = build_lora_mm(model.layers[il].wk_b, q_nope);
cb(q_nope_absorbed, "q_nope_absorbed", il);
// {kv_lora_rank, n_head, n_tokens}
@@ -573,10 +569,10 @@ llama_model_deepseek2::graph::graph(const llm_graph_params & params) :
// note: MLA with the absorption optimization converts into MQA (ie: GQA with 1 group)
cur = build_attn(inp_attn_k,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, model.layers[il].wv_b, kq_scale, il);
} else {
- ggml_tensor * kv = ggml_mul_mat(ctx0, model.layers[il].wkv_b, kv_cmpr);
+ ggml_tensor * kv = build_lora_mm(model.layers[il].wkv_b, kv_cmpr);
cb(kv, "kv", il);
// split into {n_embd_head_qk_nope, n_head, n_tokens}
@@ -613,7 +609,7 @@ llama_model_deepseek2::graph::graph(const llm_graph_params & params) :
// note: MLA without the absorption optimization converts into MHA (ie: GQA with full n_head groups)
cur = build_attn(inp_attn_kv,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
}
}
@@ -629,9 +625,9 @@ llama_model_deepseek2::graph::graph(const llm_graph_params & params) :
if ((uint32_t) il < hparams.n_layer_dense_lead) {
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
} else {
@@ -655,9 +651,9 @@ llama_model_deepseek2::graph::graph(const llm_graph_params & params) :
{
ggml_tensor * ffn_shexp =
build_ffn(cur,
- model.layers[il].ffn_up_shexp, NULL, NULL,
- model.layers[il].ffn_gate_shexp, NULL, NULL,
- model.layers[il].ffn_down_shexp, NULL, NULL,
+ model.layers[il].ffn_up_shexp, NULL,
+ model.layers[il].ffn_gate_shexp, NULL,
+ model.layers[il].ffn_down_shexp, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "ffn_shexp", il);
@@ -688,7 +684,7 @@ llama_model_deepseek2::graph::graph(const llm_graph_params & params) :
res->t_embd = cur;
// lm_head
- cur = ggml_mul_mat(ctx0, model.output, cur);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/deepseek32.cpp b/src/models/deepseek32.cpp
index 8f6f34790..0f22161ec 100644
--- a/src/models/deepseek32.cpp
+++ b/src/models/deepseek32.cpp
@@ -209,7 +209,7 @@ llama_model_deepseek32::graph::graph(const llm_graph_params & params) :
// self_attention
{
- ggml_tensor * qr = ggml_mul_mat(ctx0, model.layers[il].wq_a, cur);
+ ggml_tensor * qr = build_lora_mm(model.layers[il].wq_a, cur);
cb(qr, "qr", il);
qr = build_norm(qr, model.layers[il].attn_q_a_norm, nullptr, LLM_NORM_RMS, il);
@@ -219,7 +219,7 @@ llama_model_deepseek32::graph::graph(const llm_graph_params & params) :
// lightning indexer
{
- ggml_tensor * indexer_q = ggml_mul_mat(ctx0, model.layers[il].indexer_attn_q_b, qr);
+ ggml_tensor * indexer_q = build_lora_mm(model.layers[il].indexer_attn_q_b, qr);
cb(indexer_q, "indexer_q", il);
// {n_embd_indexer_head, n_indexer_head, n_tokens}
@@ -227,7 +227,7 @@ llama_model_deepseek32::graph::graph(const llm_graph_params & params) :
indexer_q = build_rope_indexer(indexer_q, inp_pos, il);
cb(indexer_q, "indexer_q", il);
- ggml_tensor * indexer_k = ggml_mul_mat(ctx0, model.layers[il].indexer_attn_k, cur);
+ ggml_tensor * indexer_k = build_lora_mm(model.layers[il].indexer_attn_k, cur);
cb(indexer_k, "indexer_k", il);
indexer_k = build_norm(indexer_k, model.layers[il].indexer_k_norm, model.layers[il].indexer_k_norm_b, LLM_NORM, il);
@@ -250,7 +250,7 @@ llama_model_deepseek32::graph::graph(const llm_graph_params & params) :
ggml_build_forward_expand(gf, mctx_lid->cpy_k(ctx0, indexer_k, k_idxs_lid, il));
// prepare indexer weights
- ggml_tensor * indexer_weights = ggml_mul_mat(ctx0, model.layers[il].indexer_proj, cur);
+ ggml_tensor * indexer_weights = build_lora_mm(model.layers[il].indexer_proj, cur);
cb(indexer_weights, "indexer_weights", il);
// get cached indexer keys
@@ -274,7 +274,7 @@ llama_model_deepseek32::graph::graph(const llm_graph_params & params) :
cb(top_k, "top_k", il);
}
- ggml_tensor * q = ggml_mul_mat(ctx0, model.layers[il].wq_b, qr);
+ ggml_tensor * q = build_lora_mm(model.layers[il].wq_b, qr);
cb(q, "q", il);
// split into {n_embd_head_qk_nope, n_head, n_tokens}
@@ -289,7 +289,7 @@ llama_model_deepseek32::graph::graph(const llm_graph_params & params) :
ggml_row_size(q->type, n_embd_head_k) * n_head, ggml_row_size(q->type, n_embd_head_qk_nope));
cb(q_pe, "q_pe", il);
- ggml_tensor * kv_cmpr_pe = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);
+ ggml_tensor * kv_cmpr_pe = build_lora_mm(model.layers[il].wkv_a_mqa, cur);
cb(kv_cmpr_pe, "kv_cmpr_pe", il);
// split into {kv_lora_rank, n_tokens}
@@ -321,7 +321,7 @@ llama_model_deepseek32::graph::graph(const llm_graph_params & params) :
cb(q_nope, "q_nope_perm", il);
// {n_embd_head_qk_nope, kv_lora_rank, n_head} x {n_embd_head_qk_nope, n_tokens, n_head}
- ggml_tensor * q_nope_absorbed = ggml_mul_mat(ctx0, model.layers[il].wk_b, q_nope);
+ ggml_tensor * q_nope_absorbed = build_lora_mm(model.layers[il].wk_b, q_nope);
cb(q_nope_absorbed, "q_nope_absorbed", il);
// {kv_lora_rank, n_head, n_tokens}
@@ -346,7 +346,7 @@ llama_model_deepseek32::graph::graph(const llm_graph_params & params) :
// note: MLA with the absorption optimization converts into MQA (ie: GQA with 1 group)
cur = build_attn(inp_attn_dsa,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, model.layers[il].wv_b, top_k, kq_scale, il);
}
}
@@ -364,9 +364,9 @@ llama_model_deepseek32::graph::graph(const llm_graph_params & params) :
if ((uint32_t) il < hparams.n_layer_dense_lead) {
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, model.layers[il].ffn_up_s,
- model.layers[il].ffn_gate, NULL, model.layers[il].ffn_gate_s,
- model.layers[il].ffn_down, NULL, model.layers[il].ffn_down_s,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
} else {
@@ -390,9 +390,9 @@ llama_model_deepseek32::graph::graph(const llm_graph_params & params) :
{
ggml_tensor * ffn_shexp =
build_ffn(cur,
- model.layers[il].ffn_up_shexp, NULL, model.layers[il].ffn_up_shexp_s,
- model.layers[il].ffn_gate_shexp, NULL, model.layers[il].ffn_gate_shexp_s,
- model.layers[il].ffn_down_shexp, NULL, model.layers[il].ffn_down_shexp_s,
+ model.layers[il].ffn_up_shexp, NULL,
+ model.layers[il].ffn_gate_shexp, NULL,
+ model.layers[il].ffn_down_shexp, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "ffn_shexp", il);
@@ -424,7 +424,7 @@ llama_model_deepseek32::graph::graph(const llm_graph_params & params) :
res->t_embd = cur;
// lm_head
- cur = ggml_mul_mat(ctx0, model.output, cur);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
@@ -511,7 +511,7 @@ llama_model_deepseek32::graph_mtp::graph_mtp(const llm_graph_params & params)
ggml_tensor * concat = ggml_concat(ctx0, e_norm, h_norm, /*dim=*/ 0);
cb(concat, "mtp_concat", il);
- ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat, layer.nextn.eh_proj_s);
+ ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat);
cb(cur, "mtp_eh_proj", il);
ggml_tensor * inpSA = cur;
@@ -521,13 +521,13 @@ llama_model_deepseek32::graph_mtp::graph_mtp(const llm_graph_params & params)
// self-attention: dense MLA, same construction as the deepseek2 trunk graph
{
- ggml_tensor * q = ggml_mul_mat(ctx0, layer.wq_a, cur);
+ ggml_tensor * q = build_lora_mm(layer.wq_a, cur);
cb(q, "mtp_q", il);
q = build_norm(q, layer.attn_q_a_norm, nullptr, LLM_NORM_RMS, il);
cb(q, "mtp_q", il);
- q = ggml_mul_mat(ctx0, layer.wq_b, q);
+ q = build_lora_mm(layer.wq_b, q);
cb(q, "mtp_q", il);
// split into {n_embd_head_qk_nope, n_head, n_tokens}
@@ -542,7 +542,7 @@ llama_model_deepseek32::graph_mtp::graph_mtp(const llm_graph_params & params)
ggml_row_size(q->type, n_embd_head_k) * n_head, ggml_row_size(q->type, n_embd_head_qk_nope));
cb(q_pe, "mtp_q_pe", il);
- ggml_tensor * kv_cmpr_pe = ggml_mul_mat(ctx0, layer.wkv_a_mqa, cur);
+ ggml_tensor * kv_cmpr_pe = build_lora_mm(layer.wkv_a_mqa, cur);
cb(kv_cmpr_pe, "mtp_kv_cmpr_pe", il);
// split into {kv_lora_rank, n_tokens}
@@ -572,7 +572,7 @@ llama_model_deepseek32::graph_mtp::graph_mtp(const llm_graph_params & params)
cb(q_nope, "mtp_q_nope_perm", il);
// {n_embd_head_qk_nope, kv_lora_rank, n_head} x {n_embd_head_qk_nope, n_tokens, n_head}
- ggml_tensor * q_nope_absorbed = ggml_mul_mat(ctx0, layer.wk_b, q_nope);
+ ggml_tensor * q_nope_absorbed = build_lora_mm(layer.wk_b, q_nope);
cb(q_nope_absorbed, "mtp_q_nope_absorbed", il);
// {kv_lora_rank, n_head, n_tokens}
@@ -597,7 +597,7 @@ llama_model_deepseek32::graph_mtp::graph_mtp(const llm_graph_params & params)
// note: MLA with the absorption optimization converts into MQA (ie: GQA with 1 group)
cur = build_attn(inp_attn,
- layer.wo, NULL, layer.wo_s,
+ layer.wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, layer.wv_b, kq_scale, il);
cb(cur, "mtp_attn_out", il);
}
@@ -627,9 +627,9 @@ llama_model_deepseek32::graph_mtp::graph_mtp(const llm_graph_params & params)
// FFN shared expert
ggml_tensor * ffn_shexp =
build_ffn(cur,
- layer.ffn_up_shexp, NULL, layer.ffn_up_shexp_s,
- layer.ffn_gate_shexp, NULL, layer.ffn_gate_shexp_s,
- layer.ffn_down_shexp, NULL, layer.ffn_down_shexp_s,
+ layer.ffn_up_shexp, NULL,
+ layer.ffn_gate_shexp, NULL,
+ layer.ffn_down_shexp, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "mtp_ffn_shexp", il);
@@ -654,9 +654,8 @@ llama_model_deepseek32::graph_mtp::graph_mtp(const llm_graph_params & params)
cb(cur, "mtp_shared_head_norm", -1);
ggml_tensor * head_w = layer.nextn.shared_head_head ? layer.nextn.shared_head_head : model.output;
- ggml_tensor * head_s = layer.nextn.shared_head_head ? layer.nextn.shared_head_head_s : model.output_s;
GGML_ASSERT(head_w && "DEEPSEEK32 MTP: missing LM head (nextn.shared_head_head or model.output)");
- cur = build_lora_mm(head_w, cur, head_s);
+ cur = build_lora_mm(head_w, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/deepseek4.cpp b/src/models/deepseek4.cpp
index f4269e043..fb7fb3048 100644
--- a/src/models/deepseek4.cpp
+++ b/src/models/deepseek4.cpp
@@ -370,7 +370,7 @@ llama_model_deepseek4::graph::build_hc_mixes(
ggml_tensor * flat = ggml_reshape_2d(ctx0, x, hc_dim, nt);
ggml_tensor * flat_norm = ggml_rms_norm(ctx0, flat, norm_rms_eps);
- ggml_tensor * mixes = ggml_mul_mat(ctx0, hc_fn, flat_norm);
+ ggml_tensor * mixes = build_lora_mm(hc_fn, flat_norm);
cb(mixes, "hc_mixes", il);
ggml_tensor * scale_pre = dsv4_view_1d(ctx0, hc_scale, 1, 0);
@@ -458,7 +458,7 @@ ggml_tensor * llama_model_deepseek4::graph::build_hc_head(
ggml_tensor * flat = ggml_reshape_2d(ctx0, x, hc_dim, nt);
ggml_tensor * flat_norm = ggml_rms_norm(ctx0, flat, norm_rms_eps);
- ggml_tensor * mixes = ggml_mul_mat(ctx0, hc_fn, flat_norm);
+ ggml_tensor * mixes = build_lora_mm(hc_fn, flat_norm);
cb(mixes, "hc_head_mixes", -1);
ggml_tensor * pre = dsv4_hc_affine(ctx0, mixes, hc_scale, hc_base);
@@ -1160,7 +1160,7 @@ ggml_tensor * llama_model_deepseek4::graph::build_attention_impl(
ggml_tensor * out = nullptr;
if (inp_mtp) {
out = build_attn(inp_mtp,
- nullptr, nullptr, nullptr,
+ nullptr, nullptr,
q, kv, kv,
nullptr, layer.attn_sinks, nullptr,
1.0f/sqrtf(float(n_embd_head)), il);
@@ -1188,7 +1188,7 @@ ggml_tensor * llama_model_deepseek4::graph::build_attention_impl(
out = ggml_reshape_3d(ctx0, out, o_group_dim, n_groups, nt);
out = ggml_permute(ctx0, out, 0, 2, 1, 3);
- ggml_tensor * oa = ggml_mul_mat(ctx0, layer.wo_a, out);
+ ggml_tensor * oa = build_lora_mm(layer.wo_a, out);
cb(oa, "attn_wo_a", il);
oa = ggml_permute(ctx0, oa, 0, 2, 1, 3);
oa = ggml_cont_2d(ctx0, oa, o_lora_rank*n_groups, nt);
@@ -1288,9 +1288,9 @@ llama_model_deepseek4::graph::graph(const llm_graph_params & params) :
cb(moe_out, "ffn_moe_out", il);
ggml_tensor * ffn_shexp = build_ffn(cur,
- layer.ffn_up_shexp, nullptr, nullptr,
- layer.ffn_gate_shexp, nullptr, nullptr,
- layer.ffn_down_shexp, nullptr, nullptr,
+ layer.ffn_up_shexp, nullptr,
+ layer.ffn_gate_shexp, nullptr,
+ layer.ffn_down_shexp, nullptr,
nullptr, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "ffn_shexp", il);
@@ -1326,7 +1326,7 @@ llama_model_deepseek4::graph::graph(const llm_graph_params & params) :
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = ggml_mul_mat(ctx0, model.output, cur);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
}
@@ -1391,7 +1391,7 @@ llama_model_deepseek4::graph_mtp::graph_mtp(const llm_graph_params & params) :
ggml_tensor * concat = ggml_concat(ctx0, e_norm, h_norm, 0);
cb(concat, "mtp_concat", il);
- ggml_tensor * inpL = build_lora_mm(layer.nextn.eh_proj, concat, layer.nextn.eh_proj_s);
+ ggml_tensor * inpL = build_lora_mm(layer.nextn.eh_proj, concat);
cb(inpL, "mtp_eh_proj", il);
ggml_tensor * residual = inpL;
@@ -1438,9 +1438,9 @@ llama_model_deepseek4::graph_mtp::graph_mtp(const llm_graph_params & params) :
cb(moe_out, "mtp_ffn_moe_out", il);
ggml_tensor * ffn_shexp = build_ffn(cur,
- layer.ffn_up_shexp, nullptr, nullptr,
- layer.ffn_gate_shexp, nullptr, nullptr,
- layer.ffn_down_shexp, nullptr, nullptr,
+ layer.ffn_up_shexp, nullptr,
+ layer.ffn_gate_shexp, nullptr,
+ layer.ffn_down_shexp, nullptr,
nullptr, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "mtp_ffn_shexp", il);
@@ -1469,7 +1469,7 @@ llama_model_deepseek4::graph_mtp::graph_mtp(const llm_graph_params & params) :
ggml_tensor * head_w = layer.nextn.shared_head_head ? layer.nextn.shared_head_head : model.output;
GGML_ASSERT(head_w && "DEEPSEEK4 MTP missing LM head");
- cur = ggml_mul_mat(ctx0, head_w, cur);
+ cur = build_lora_mm(head_w, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/deepseek41.cpp b/src/models/deepseek41.cpp
index c9c00b885..9cc21d429 100644
--- a/src/models/deepseek41.cpp
+++ b/src/models/deepseek41.cpp
@@ -756,7 +756,7 @@ ggml_tensor * llama_model_deepseek41::graph::build_attention41(
cb(out, "attn_derope", il);
out = ggml_permute(ctx0, ggml_reshape_3d(ctx0, out, n_head/n_groups*n_embd_head, n_groups, nt), 0, 2, 1, 3);
- ggml_tensor * oa = ggml_mul_mat(ctx0, layer.wo_a, out);
+ ggml_tensor * oa = build_lora_mm(layer.wo_a, out);
cb(oa, "attn_wo_a", il);
oa = ggml_cont_2d(ctx0, ggml_permute(ctx0, oa, 0, 2, 1, 3), o_lora_rank*n_groups, nt);
@@ -900,9 +900,9 @@ llama_model_deepseek41::graph::graph(const llm_graph_params & params) :
cb(moe_out, "ffn_moe_out", il);
ggml_tensor * ffn_shexp = build_ffn(cur,
- layer.ffn_up_shexp, nullptr, nullptr,
- layer.ffn_gate_shexp, nullptr, nullptr,
- layer.ffn_down_shexp, nullptr, nullptr,
+ layer.ffn_up_shexp, nullptr,
+ layer.ffn_gate_shexp, nullptr,
+ layer.ffn_down_shexp, nullptr,
nullptr, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "ffn_shexp", il);
@@ -926,7 +926,7 @@ llama_model_deepseek41::graph::graph(const llm_graph_params & params) :
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = ggml_mul_mat(ctx0, model.output, cur);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
}
diff --git a/src/models/dflash.cpp b/src/models/dflash.cpp
index d6b8d8713..04749dfad 100644
--- a/src/models/dflash.cpp
+++ b/src/models/dflash.cpp
@@ -116,8 +116,6 @@ void llama_model_dflash::load_arch_tensors(llama_model_loader &) {
const int fc_flags = ml->get_tensor_meta("blk.0.ctx_mix.weight") ? TENSOR_NOT_REQUIRED : 0;
fc = create_tensor(tn(LLM_TENSOR_FC, "weight"), { n_embd_inp, n_embd }, fc_flags);
- fc_s = create_tensor(tn(LLM_TENSOR_FC, "scale"), { 1 }, TENSOR_NOT_REQUIRED);
- fc_in_s = create_tensor(tn(LLM_TENSOR_FC, "input_scale"), { 1 }, TENSOR_NOT_REQUIRED);
output_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_OUTPUT_NORM, "weight"), { n_embd }, fc_flags); // encoder hidden_norm (after fc)
output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0); // decoder final norm
@@ -211,7 +209,7 @@ template <>
llama_model_dflash::graph<true>::graph(const llm_graph_params & params) : llm_graph_context(params) {
ggml_tensor * cur = build_inp_embd_enc();
- cur = build_lora_mm(model.fc, cur, model.fc_s);
+ cur = build_lora_mm(model.fc, cur);
cb(cur, "fc_out", -1);
cur = build_norm(cur, model.output_norm_enc, NULL, LLM_NORM_RMS, -1);
@@ -251,7 +249,7 @@ ggml_tensor * llama_model_dflash::build_inject_inp(llm_graph_context & g, const
g.res->add_input(std::move(inp));
- ggml_tensor * inp_g = g.build_lora_mm(model.fc, inp_target, model.fc_s);
+ ggml_tensor * inp_g = g.build_lora_mm(model.fc, inp_target);
return g.build_norm(inp_g, model.output_norm_enc, nullptr, LLM_NORM_RMS, -1);
}
@@ -267,16 +265,16 @@ ggml_tensor * llama_model_dflash::get_tok_embd(const llm_graph_context & g, cons
return model_other->tok_embd;
}
-// lm_head and its scale from the target model (shared via ctx_other) when the draft has none
-std::pair<ggml_tensor *, ggml_tensor *> llama_model_dflash::get_lm_head(const llm_graph_context & g, const llama_model & model) {
+// lm_head from the target model (shared via ctx_other) when the draft has none
+ggml_tensor * llama_model_dflash::get_lm_head(const llm_graph_context & g, const llama_model & model) {
if (model.output) {
- return { model.output, model.output_s };
+ return model.output;
}
GGML_ASSERT(g.cparams.ctx_other != nullptr);
const auto * model_other = llama_get_model(g.cparams.ctx_other);
GGML_ASSERT(model_other->output != nullptr && "DFlash draft requires the target model's output projection");
- return { model_other->output, model_other->output_s };
+ return model_other->output;
}
// DFlash decoder, dual-mode by batch type:
@@ -361,9 +359,9 @@ ggml_tensor * llama_model_dflash::graph_base::build_inject() {
for (int il = 0; il < n_layer; ++il) {
const auto & layer = model.layers[il];
- ggml_tensor * Kcur = build_lora_mm(layer.wk, inp_g, layer.wk_s);
+ ggml_tensor * Kcur = build_lora_mm(layer.wk, inp_g);
const bool shared_kv = layer.wv == nullptr;
- ggml_tensor * Vcur = shared_kv ? Kcur : build_lora_mm(layer.wv, inp_g, layer.wv_s);
+ ggml_tensor * Vcur = shared_kv ? Kcur : build_lora_mm(layer.wv, inp_g);
Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
@@ -401,7 +399,7 @@ void llama_model_dflash::graph_base::build_inject_ctx_mix() {
cb(feats, "inp_target_features", -1);
const auto build_mix = [&](ggml_tensor * mix, ggml_tensor * norm, int il) {
- ggml_tensor * cur = ggml_reshape_2d(ctx0, ggml_mul_mat(ctx0, mix, feats), n_embd, n_tokens);
+ ggml_tensor * cur = ggml_reshape_2d(ctx0, build_lora_mm(mix, feats), n_embd, n_tokens);
return build_norm(cur, norm, nullptr, LLM_NORM_RMS, il);
};
@@ -469,10 +467,10 @@ ggml_tensor * llama_model_dflash::graph_base::build_layers(ggml_tensor * inp_tok
cb(noise_norm, "attn_conv_in", il);
}
- ggml_tensor * Qcur = build_lora_mm(layer.wq, noise_norm, layer.wq_s);
- ggml_tensor * Kcur = build_lora_mm(layer.wk, noise_norm, layer.wk_s);
+ ggml_tensor * Qcur = build_lora_mm(layer.wq, noise_norm);
+ ggml_tensor * Kcur = build_lora_mm(layer.wk, noise_norm);
const bool shared_kv = layer.wv == nullptr;
- ggml_tensor * Vcur = shared_kv ? Kcur : build_lora_mm(layer.wv, noise_norm, layer.wv_s);
+ ggml_tensor * Vcur = shared_kv ? Kcur : build_lora_mm(layer.wv, noise_norm);
Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
@@ -492,8 +490,8 @@ ggml_tensor * llama_model_dflash::graph_base::build_layers(ggml_tensor * inp_tok
// cache-aware, non-causal attention
ggml_tensor * cur = use_iswa
- ? build_attn(inp_attn_iswa, layer.wo, NULL, layer.wo_s, Qcur, Kcur, Vcur, nullptr, layer.attn_sinks, nullptr, kq_scale, il)
- : build_attn(inp_attn, layer.wo, NULL, layer.wo_s, Qcur, Kcur, Vcur, nullptr, layer.attn_sinks, nullptr, kq_scale, il);
+ ? build_attn(inp_attn_iswa, layer.wo, NULL, Qcur, Kcur, Vcur, nullptr, layer.attn_sinks, nullptr, kq_scale, il)
+ : build_attn(inp_attn, layer.wo, NULL, Qcur, Kcur, Vcur, nullptr, layer.attn_sinks, nullptr, kq_scale, il);
if (hparams.f_attn_value_scale != 0.0f) {
cur = ggml_scale(ctx0, cur, hparams.f_attn_value_scale);
@@ -524,9 +522,9 @@ ggml_tensor * llama_model_dflash::graph_base::build_layers(ggml_tensor * inp_tok
}
cur = build_ffn(cur,
- layer.ffn_up, NULL, layer.ffn_up_s,
- layer.ffn_gate, NULL, layer.ffn_gate_s,
- layer.ffn_down, NULL, layer.ffn_down_s,
+ layer.ffn_up, NULL,
+ layer.ffn_gate, NULL,
+ layer.ffn_down, NULL,
NULL,
hparams.llm_ffn_op, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -559,8 +557,8 @@ ggml_tensor * llama_model_dflash::graph_base::build_layers(ggml_tensor * inp_tok
}
ggml_tensor * llama_model_dflash::graph_base::build_logits(ggml_tensor * cur) {
- const auto [output, output_s] = get_lm_head(*this, model);
- cur = build_lora_mm(output, cur, output_s);
+ auto * output = get_lm_head(*this, model);
+ cur = build_lora_mm(output, cur);
// a padded lm head (e.g. shared from the target): drop the rows past the vocabulary
if (!model.d2t && cur->ne[0] > (int64_t) model.vocab.n_tokens()) {
diff --git a/src/models/domyn-edge.cpp b/src/models/domyn-edge.cpp
index 6452894d3..27282045d 100644
--- a/src/models/domyn-edge.cpp
+++ b/src/models/domyn-edge.cpp
@@ -113,7 +113,7 @@ llama_model_domyn_edge::graph::graph(const llm_graph_params & params)
cb(Kcur, "Kcur_rope", il);
cur = build_attn(inp_attn,
- layer.wo, NULL, layer.wo_s,
+ layer.wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "attn_out", il);
@@ -135,9 +135,9 @@ llama_model_domyn_edge::graph::graph(const llm_graph_params & params)
// SwiGLU FFN
cur = build_ffn(cur,
- layer.ffn_up, NULL, NULL,
- layer.ffn_gate, NULL, NULL,
- layer.ffn_down, NULL, NULL, NULL,
+ layer.ffn_up, NULL,
+ layer.ffn_gate, NULL,
+ layer.ffn_down, NULL, NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -154,7 +154,7 @@ llama_model_domyn_edge::graph::graph(const llm_graph_params & params)
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
}
diff --git a/src/models/dots1.cpp b/src/models/dots1.cpp
index c2fa96560..3f4b9e513 100644
--- a/src/models/dots1.cpp
+++ b/src/models/dots1.cpp
@@ -118,7 +118,7 @@ llama_model_dots1::graph::graph(const llm_graph_params & params) :
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -134,9 +134,9 @@ llama_model_dots1::graph::graph(const llm_graph_params & params) :
if ((uint32_t) il < hparams.n_layer_dense_lead) {
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
} else {
@@ -156,9 +156,9 @@ llama_model_dots1::graph::graph(const llm_graph_params & params) :
{
ggml_tensor * ffn_shexp =
build_ffn(cur,
- model.layers[il].ffn_up_shexp, NULL, NULL,
- model.layers[il].ffn_gate_shexp, NULL, NULL,
- model.layers[il].ffn_down_shexp, NULL, NULL,
+ model.layers[il].ffn_up_shexp, NULL,
+ model.layers[il].ffn_gate_shexp, NULL,
+ model.layers[il].ffn_down_shexp, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "ffn_shexp", il);
@@ -182,7 +182,7 @@ llama_model_dots1::graph::graph(const llm_graph_params & params) :
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/dots3note.cpp b/src/models/dots3note.cpp
index 2a2d5500b..206557b12 100644
--- a/src/models/dots3note.cpp
+++ b/src/models/dots3note.cpp
@@ -203,7 +203,7 @@ llama_model_dots3note::graph::graph(const llm_graph_params & params) :
{
ggml_tensor * attn_inp = cur;
- ggml_tensor * qr = ggml_mul_mat(ctx0, model.layers[il].wq_a, cur);
+ ggml_tensor * qr = build_lora_mm(model.layers[il].wq_a, cur);
cb(qr, "qr", il);
qr = build_norm(qr, model.layers[il].attn_q_a_norm, nullptr, LLM_NORM_RMS, il);
@@ -213,7 +213,7 @@ llama_model_dots3note::graph::graph(const llm_graph_params & params) :
// lightning indexer (full-attention layers only)
if (!is_swa) {
- ggml_tensor * indexer_q = ggml_mul_mat(ctx0, model.layers[il].indexer_attn_q_b, qr);
+ ggml_tensor * indexer_q = build_lora_mm(model.layers[il].indexer_attn_q_b, qr);
cb(indexer_q, "indexer_q", il);
// {n_embd_indexer_head, n_indexer_head, n_tokens}
@@ -221,7 +221,7 @@ llama_model_dots3note::graph::graph(const llm_graph_params & params) :
indexer_q = build_rope_indexer(indexer_q, inp_pos, il);
cb(indexer_q, "indexer_q", il);
- ggml_tensor * indexer_k = ggml_mul_mat(ctx0, model.layers[il].indexer_attn_k, cur);
+ ggml_tensor * indexer_k = build_lora_mm(model.layers[il].indexer_attn_k, cur);
cb(indexer_k, "indexer_k", il);
indexer_k = build_norm(indexer_k, model.layers[il].indexer_k_norm, model.layers[il].indexer_k_norm_b, LLM_NORM, il);
@@ -243,7 +243,7 @@ llama_model_dots3note::graph::graph(const llm_graph_params & params) :
const auto & k_idxs_lid = inp_attn->get_dsa()->get_k_idxs_lid();
ggml_build_forward_expand(gf, mctx_lid->cpy_k(ctx0, indexer_k, k_idxs_lid, il));
- ggml_tensor * indexer_weights = ggml_mul_mat(ctx0, model.layers[il].indexer_proj, cur);
+ ggml_tensor * indexer_weights = build_lora_mm(model.layers[il].indexer_proj, cur);
cb(indexer_weights, "indexer_weights", il);
indexer_k = mctx_lid->get_k(ctx0, il);
@@ -266,7 +266,7 @@ llama_model_dots3note::graph::graph(const llm_graph_params & params) :
cb(top_k, "top_k", il);
}
- ggml_tensor * q = ggml_mul_mat(ctx0, model.layers[il].wq_b, qr);
+ ggml_tensor * q = build_lora_mm(model.layers[il].wq_b, qr);
cb(q, "q", il);
// split into {n_embd_head_qk_nope, n_head_l, n_tokens}
@@ -281,7 +281,7 @@ llama_model_dots3note::graph::graph(const llm_graph_params & params) :
ggml_row_size(q->type, n_embd_head_k_mla) * n_head_l, ggml_row_size(q->type, n_embd_head_qk_nope));
cb(q_pe, "q_pe", il);
- ggml_tensor * kv_cmpr_pe = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);
+ ggml_tensor * kv_cmpr_pe = build_lora_mm(model.layers[il].wkv_a_mqa, cur);
cb(kv_cmpr_pe, "kv_cmpr_pe", il);
// split into {kv_lora_rank, n_tokens}
@@ -317,7 +317,7 @@ llama_model_dots3note::graph::graph(const llm_graph_params & params) :
cb(q_nope, "q_nope_perm", il);
// {n_embd_head_qk_nope, kv_lora_rank, n_head_l} x {n_embd_head_qk_nope, n_tokens, n_head_l}
- ggml_tensor * q_nope_absorbed = ggml_mul_mat(ctx0, model.layers[il].wk_b, q_nope);
+ ggml_tensor * q_nope_absorbed = build_lora_mm(model.layers[il].wk_b, q_nope);
cb(q_nope_absorbed, "q_nope_absorbed", il);
// {kv_lora_rank, n_head_l, n_tokens}
@@ -342,11 +342,11 @@ llama_model_dots3note::graph::graph(const llm_graph_params & params) :
// apply the head-wise output gate before o_proj, so wo stays out of build_attn
if (is_swa) {
cur = build_attn(inp_attn->get_swa(),
- nullptr, nullptr, nullptr,
+ nullptr, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, model.layers[il].wv_b, kq_scale, il);
} else {
cur = build_attn(inp_attn->get_dsa(),
- nullptr, nullptr, nullptr,
+ nullptr, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, model.layers[il].wv_b, top_k, kq_scale, il);
}
cb(cur, "attn_out", il);
@@ -365,7 +365,7 @@ llama_model_dots3note::graph::graph(const llm_graph_params & params) :
cur = ggml_reshape_2d(ctx0, attn_3d, n_embd_head_v_mla * n_head_l, n_tokens);
- cur = build_lora_mm(model.layers[il].wo, cur, model.layers[il].wo_s);
+ cur = build_lora_mm(model.layers[il].wo, cur);
cb(cur, "attn_output", il);
}
}
@@ -383,9 +383,9 @@ llama_model_dots3note::graph::graph(const llm_graph_params & params) :
if ((uint32_t) il < hparams.n_layer_dense_lead) {
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, model.layers[il].ffn_up_s,
- model.layers[il].ffn_gate, NULL, model.layers[il].ffn_gate_s,
- model.layers[il].ffn_down, NULL, model.layers[il].ffn_down_s,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
} else {
@@ -406,9 +406,9 @@ llama_model_dots3note::graph::graph(const llm_graph_params & params) :
ggml_tensor * ffn_shexp =
build_ffn(cur,
- model.layers[il].ffn_up_shexp, NULL, model.layers[il].ffn_up_shexp_s,
- model.layers[il].ffn_gate_shexp, NULL, model.layers[il].ffn_gate_shexp_s,
- model.layers[il].ffn_down_shexp, NULL, model.layers[il].ffn_down_shexp_s,
+ model.layers[il].ffn_up_shexp, NULL,
+ model.layers[il].ffn_gate_shexp, NULL,
+ model.layers[il].ffn_down_shexp, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "ffn_shexp", il);
@@ -431,7 +431,7 @@ llama_model_dots3note::graph::graph(const llm_graph_params & params) :
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = ggml_mul_mat(ctx0, model.output, cur);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/dream.cpp b/src/models/dream.cpp
index 76c0b03c8..9b0a0fbe4 100644
--- a/src/models/dream.cpp
+++ b/src/models/dream.cpp
@@ -89,7 +89,7 @@ llama_model_dream::graph::graph(const llm_graph_params & params) :
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -104,9 +104,9 @@ llama_model_dream::graph::graph(const llm_graph_params & params) :
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -126,7 +126,7 @@ llama_model_dream::graph::graph(const llm_graph_params & params) :
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/dspark.cpp b/src/models/dspark.cpp
index 81ededc79..012d42bf3 100644
--- a/src/models/dspark.cpp
+++ b/src/models/dspark.cpp
@@ -83,7 +83,6 @@ void llama_model_dflash::load_tensors_dspark(int64_t n_vocab_draft) {
dspark_markov_w1 = create_tensor(tn(LLM_TENSOR_DSPARK_MARKOV_W1, "weight"), { dspark_markov_rank, n_vocab }, 0);
dspark_markov_w2 = create_tensor(tn(LLM_TENSOR_DSPARK_MARKOV_W2, "weight"), { dspark_markov_rank, n_vocab_draft }, 0);
- dspark_markov_w2_s = create_tensor(tn(LLM_TENSOR_DSPARK_MARKOV_W2, "scale"), { 1 }, TENSOR_NOT_REQUIRED);
dspark_markov_w2_b = create_tensor(tn(LLM_TENSOR_DSPARK_MARKOV_W2, "bias"), { n_vocab_draft }, TENSOR_NOT_REQUIRED);
dspark_conf_proj = create_tensor(tn(LLM_TENSOR_DSPARK_CONF_PROJ, "weight"), { n_embd + dspark_markov_rank, 1 }, TENSOR_NOT_REQUIRED);
@@ -366,7 +365,7 @@ static void build_dspark_markov_head(llm_graph_context & g, const llama_model &
if (f_rnn) {
joint = ggml_concat(ctx0, joint, f_rnn, 0);
}
- joint = ggml_add(ctx0, ggml_mul_mat(ctx0, model.dspark_joint_proj, joint), model.dspark_joint_proj_b); // [3R, n_blocks]
+ joint = ggml_add(ctx0, g.build_lora_mm(model.dspark_joint_proj, joint), model.dspark_joint_proj_b); // [3R, n_blocks]
const auto chunk = [&](int64_t k) {
return ggml_cont(ctx0, ggml_view_2d(ctx0, joint, rank, n_blocks, joint->nb[1], k*rank*ggml_element_size(joint)));
@@ -379,7 +378,7 @@ static void build_dspark_markov_head(llm_graph_context & g, const llama_model &
markov = ggml_tanh(ctx0, chunk(2));
}
- ggml_tensor * bias = g.build_lora_mm(w2, markov, model.dspark_markov_w2_s); // [n_vocab_draft, n_blocks]
+ ggml_tensor * bias = g.build_lora_mm(w2, markov); // [n_vocab_draft, n_blocks]
if (model.dspark_markov_w2_b) {
bias = ggml_add(ctx0, bias, model.dspark_markov_w2_b);
}
@@ -409,7 +408,7 @@ static void build_dspark_markov_head(llm_graph_context & g, const llama_model &
ggml_tensor * conf_inp_i = ggml_view_2d(ctx0, conf_inp, conf_inp->ne[0], n_blocks,
(size_t) block_drafts * conf_inp->nb[1], i*conf_inp->nb[1]);
ggml_tensor * feat = ggml_concat(ctx0, ggml_cont(ctx0, conf_inp_i), markov, 0);
- ggml_tensor * conf = ggml_mul_mat(ctx0, model.dspark_conf_proj, feat);
+ ggml_tensor * conf = g.build_lora_mm(model.dspark_conf_proj, feat);
if (model.dspark_conf_proj_b) {
conf = ggml_add(ctx0, conf, model.dspark_conf_proj_b);
}
@@ -606,9 +605,9 @@ llama_model_dflash::graph_dsv4::graph_dsv4(const llm_graph_params & params) :
cb(moe_out, "ffn_moe_out", il);
ggml_tensor * ffn_shexp = build_ffn(cur,
- layer.ffn_up_shexp, nullptr, nullptr,
- layer.ffn_gate_shexp, nullptr, nullptr,
- layer.ffn_down_shexp, nullptr, nullptr,
+ layer.ffn_up_shexp, nullptr,
+ layer.ffn_gate_shexp, nullptr,
+ layer.ffn_down_shexp, nullptr,
nullptr, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "ffn_shexp", il);
@@ -628,8 +627,8 @@ llama_model_dflash::graph_dsv4::graph_dsv4(const llm_graph_params & params) :
cur = build_norm(cur, model.output_norm, nullptr, LLM_NORM_RMS, -1);
cb(cur, "result_norm", -1);
- const auto [output, output_s] = get_lm_head(*this, model);
- cur = build_lora_mm(output, cur, output_s);
+ auto * output = get_lm_head(*this, model);
+ cur = build_lora_mm(output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
@@ -757,7 +756,7 @@ llama_model_dflash::graph_dsv41::graph_dsv41(const llm_graph_params & params) :
cb(kv, "kv", il);
ggml_tensor * out = build_attn(inp_attn,
- nullptr, nullptr, nullptr,
+ nullptr, nullptr,
q, kv, kv,
nullptr, layer.attn_sinks, nullptr,
kq_scale, il);
@@ -771,7 +770,7 @@ llama_model_dflash::graph_dsv41::graph_dsv41(const llm_graph_params & params) :
out = ggml_reshape_3d(ctx0, out, o_group_dim, n_groups, n_tokens);
out = ggml_permute(ctx0, out, 0, 2, 1, 3);
- ggml_tensor * oa = ggml_mul_mat(ctx0, layer.wo_a, out);
+ ggml_tensor * oa = build_lora_mm(layer.wo_a, out);
cb(oa, "attn_wo_a", il);
oa = ggml_permute(ctx0, oa, 0, 2, 1, 3);
oa = ggml_cont_2d(ctx0, oa, o_lora_rank*n_groups, n_tokens);
@@ -807,9 +806,9 @@ llama_model_dflash::graph_dsv41::graph_dsv41(const llm_graph_params & params) :
cb(moe_out, "ffn_moe_out", il);
ggml_tensor * ffn_shexp = build_ffn(cur,
- layer.ffn_up_shexp, nullptr, nullptr,
- layer.ffn_gate_shexp, nullptr, nullptr,
- layer.ffn_down_shexp, nullptr, nullptr,
+ layer.ffn_up_shexp, nullptr,
+ layer.ffn_gate_shexp, nullptr,
+ layer.ffn_down_shexp, nullptr,
nullptr, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "ffn_shexp", il);
@@ -833,8 +832,8 @@ llama_model_dflash::graph_dsv41::graph_dsv41(const llm_graph_params & params) :
cur = build_norm(cur, model.output_norm, nullptr, LLM_NORM_RMS, -1);
cb(cur, "result_norm", -1);
- const auto [output, output_s] = get_lm_head(*this, model);
- cur = build_lora_mm(output, cur, output_s);
+ auto * output = get_lm_head(*this, model);
+ cur = build_lora_mm(output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
@@ -867,7 +866,7 @@ llama_model_dflash::graph_mistral_large4::graph_mistral_large4(const llm_graph_p
const auto build_kv = [&](ggml_tensor * x, int il) -> std::pair<ggml_tensor *, ggml_tensor *> {
const auto & layer = model.layers[il];
- ggml_tensor * kv_cmpr_pe = ggml_mul_mat(ctx0, layer.wkv_a_mqa, x);
+ ggml_tensor * kv_cmpr_pe = build_lora_mm(layer.wkv_a_mqa, x);
cb(kv_cmpr_pe, "kv_cmpr_pe", il);
ggml_tensor * kv_cmpr = ggml_view_2d(ctx0, kv_cmpr_pe, kv_lora_rank, n_tokens,
@@ -929,7 +928,7 @@ llama_model_dflash::graph_mistral_large4::graph_mistral_large4(const llm_graph_p
cb(cur, "attn_norm", il);
{
- ggml_tensor * q = ggml_mul_mat(ctx0, layer.wq, cur);
+ ggml_tensor * q = build_lora_mm(layer.wq, cur);
cb(q, "q", il);
// {n_embd_head_nope, n_tokens, n_head}
@@ -939,7 +938,7 @@ llama_model_dflash::graph_mistral_large4::graph_mistral_large4(const llm_graph_p
q_nope = ggml_permute(ctx0, q_nope, 0, 2, 1, 3);
// {kv_lora_rank, n_head, n_tokens}
- ggml_tensor * q_nope_absorbed = ggml_mul_mat(ctx0, layer.wk_b, q_nope);
+ ggml_tensor * q_nope_absorbed = build_lora_mm(layer.wk_b, q_nope);
q_nope_absorbed = ggml_permute(ctx0, q_nope_absorbed, 0, 2, 1, 3);
cb(q_nope_absorbed, "q_nope_absorbed", il);
@@ -958,7 +957,7 @@ llama_model_dflash::graph_mistral_large4::graph_mistral_large4(const llm_graph_p
// apply wv_b after attn, so the K rotation is already undone
ggml_tensor * out = build_attn(inp_attn,
- nullptr, nullptr, nullptr,
+ nullptr, nullptr,
Qcur, Kcur, Vcur,
nullptr, nullptr, nullptr,
kq_scale, il);
@@ -966,10 +965,10 @@ llama_model_dflash::graph_mistral_large4::graph_mistral_large4(const llm_graph_p
// {kv_lora_rank, n_tokens, n_head} -> {n_embd_head_v_mla, n_tokens, n_head}
out = ggml_reshape_3d(ctx0, out, kv_lora_rank, n_head, n_tokens);
- out = ggml_mul_mat(ctx0, layer.wv_b, ggml_permute(ctx0, out, 0, 2, 1, 3));
+ out = build_lora_mm(layer.wv_b, ggml_permute(ctx0, out, 0, 2, 1, 3));
out = ggml_cont_2d(ctx0, ggml_permute(ctx0, out, 0, 2, 1, 3), n_embd_head_v_mla*n_head, n_tokens);
- cur = build_lora_mm(layer.wo, out, layer.wo_s);
+ cur = build_lora_mm(layer.wo, out);
cb(cur, "attn_out", il);
}
@@ -980,9 +979,9 @@ llama_model_dflash::graph_mistral_large4::graph_mistral_large4(const llm_graph_p
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- layer.ffn_up, nullptr, nullptr,
- layer.ffn_gate, nullptr, nullptr,
- layer.ffn_down, nullptr, nullptr,
+ layer.ffn_up, nullptr,
+ layer.ffn_gate, nullptr,
+ layer.ffn_down, nullptr,
nullptr, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -994,8 +993,8 @@ llama_model_dflash::graph_mistral_large4::graph_mistral_large4(const llm_graph_p
cb(cur, "result_norm", -1);
res->t_embd = cur;
- const auto [output, output_s] = get_lm_head(*this, model);
- ggml_tensor * logits = build_lora_mm(output, cur, output_s);
+ auto * output = get_lm_head(*this, model);
+ ggml_tensor * logits = build_lora_mm(output, cur);
// softcap does not apply to the markov bias
if (hparams.f_final_logit_softcapping) {
diff --git a/src/models/eagle3.cpp b/src/models/eagle3.cpp
index 990b9fe5d..cecc04ee0 100644
--- a/src/models/eagle3.cpp
+++ b/src/models/eagle3.cpp
@@ -242,7 +242,7 @@ llama_model_eagle3::graph<false>::graph(const llm_graph_params & params) : llm_g
cb(Kcur, "Kcur_rope", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, nullptr,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
// Add residual and update it
@@ -256,9 +256,9 @@ llama_model_eagle3::graph<false>::graph(const llm_graph_params & params) : llm_g
cb(cur, "post_attn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
diff --git a/src/models/ernie4-5-moe.cpp b/src/models/ernie4-5-moe.cpp
index 7edf9f508..c1af284a0 100644
--- a/src/models/ernie4-5-moe.cpp
+++ b/src/models/ernie4-5-moe.cpp
@@ -45,7 +45,7 @@ llama_model_ernie4_5_moe::graph::graph(const llm_graph_params & params) :
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
cb(cur, "attn_out", il);
}
@@ -65,9 +65,9 @@ llama_model_ernie4_5_moe::graph::graph(const llm_graph_params & params) :
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
} else {
@@ -92,9 +92,9 @@ llama_model_ernie4_5_moe::graph::graph(const llm_graph_params & params) :
if (hparams.n_ff_shexp > 0) {
ggml_tensor * ffn_shexp =
build_ffn(cur,
- model.layers[il].ffn_up_shexp, NULL, NULL,
- model.layers[il].ffn_gate_shexp, NULL, NULL,
- model.layers[il].ffn_down_shexp, NULL, NULL,
+ model.layers[il].ffn_up_shexp, NULL,
+ model.layers[il].ffn_gate_shexp, NULL,
+ model.layers[il].ffn_down_shexp, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "ffn_shexp", il);
@@ -121,7 +121,7 @@ llama_model_ernie4_5_moe::graph::graph(const llm_graph_params & params) :
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/ernie4-5.cpp b/src/models/ernie4-5.cpp
index 932cc74ce..928f91272 100644
--- a/src/models/ernie4-5.cpp
+++ b/src/models/ernie4-5.cpp
@@ -113,7 +113,7 @@ llama_model_ernie4_5::graph::graph(const llm_graph_params & params) :
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1) {
@@ -130,9 +130,9 @@ llama_model_ernie4_5::graph::graph(const llm_graph_params & params) :
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
}
@@ -152,7 +152,7 @@ llama_model_ernie4_5::graph::graph(const llm_graph_params & params) :
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/eurobert.cpp b/src/models/eurobert.cpp
index 7a42f0dbf..891d262f0 100644
--- a/src/models/eurobert.cpp
+++ b/src/models/eurobert.cpp
@@ -70,7 +70,7 @@ llama_model_eurobert::graph::graph(const llm_graph_params & params) : llm_graph_
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, nullptr, model.layers[il].wo_s,
+ model.layers[il].wo, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
cb(cur, "kqv_out", il);
}
@@ -91,9 +91,9 @@ llama_model_eurobert::graph::graph(const llm_graph_params & params) : llm_graph_
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
diff --git a/src/models/exaone-moe.cpp b/src/models/exaone-moe.cpp
index 15bbd5a3f..c85ca8ce6 100644
--- a/src/models/exaone-moe.cpp
+++ b/src/models/exaone-moe.cpp
@@ -160,7 +160,7 @@ llama_model_exaone_moe::graph::graph(const llm_graph_params & params) :
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn_iswa,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
cb(cur, "attn_out", il);
}
@@ -179,9 +179,9 @@ llama_model_exaone_moe::graph::graph(const llm_graph_params & params) :
if (model.layers[il].ffn_gate_inp == nullptr) {
// dense branch
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL, NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
} else {
@@ -203,9 +203,9 @@ llama_model_exaone_moe::graph::graph(const llm_graph_params & params) :
{
ggml_tensor * ffn_shexp =
build_ffn(cur,
- model.layers[il].ffn_up_shexp, NULL, NULL,
- model.layers[il].ffn_gate_shexp, NULL, NULL,
- model.layers[il].ffn_down_shexp, NULL, NULL,
+ model.layers[il].ffn_up_shexp, NULL,
+ model.layers[il].ffn_gate_shexp, NULL,
+ model.layers[il].ffn_down_shexp, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "ffn_shexp", il);
@@ -231,7 +231,7 @@ llama_model_exaone_moe::graph::graph(const llm_graph_params & params) :
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/exaone.cpp b/src/models/exaone.cpp
index 780f34f04..9f30e3421 100644
--- a/src/models/exaone.cpp
+++ b/src/models/exaone.cpp
@@ -83,7 +83,7 @@ llama_model_exaone::graph::graph(const llm_graph_params & params) :
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -98,9 +98,9 @@ llama_model_exaone::graph::graph(const llm_graph_params & params) :
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -121,7 +121,7 @@ llama_model_exaone::graph::graph(const llm_graph_params & params) :
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/exaone4.cpp b/src/models/exaone4.cpp
index bca08e061..d405b076c 100644
--- a/src/models/exaone4.cpp
+++ b/src/models/exaone4.cpp
@@ -140,7 +140,7 @@ llama_model_exaone4::graph<iswa>::graph(const llm_graph_params & params) :
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
cb(cur, "attn_out", il);
}
@@ -156,9 +156,9 @@ llama_model_exaone4::graph<iswa>::graph(const llm_graph_params & params) :
// feed-forward network
cur = build_ffn(ffn_inp,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL, NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -181,7 +181,7 @@ llama_model_exaone4::graph<iswa>::graph(const llm_graph_params & params) :
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/falcon-h1.cpp b/src/models/falcon-h1.cpp
index 22c677b47..ffdf708bb 100644
--- a/src/models/falcon-h1.cpp
+++ b/src/models/falcon-h1.cpp
@@ -155,7 +155,7 @@ llama_model_falcon_h1::graph::graph(const llm_graph_params & params) :
cb(Vcur, "Vcur-post-rope", il);
ggml_tensor * attn_out = build_attn(inp->get_attn(),
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(attn_out, "attn_out", il);
@@ -183,9 +183,9 @@ llama_model_falcon_h1::graph::graph(const llm_graph_params & params) :
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
- model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
- model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b,
+ model.layers[il].ffn_gate, model.layers[il].ffn_gate_b,
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -205,7 +205,7 @@ llama_model_falcon_h1::graph::graph(const llm_graph_params & params) :
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/falcon.cpp b/src/models/falcon.cpp
index 6269e633c..82ad1f420 100644
--- a/src/models/falcon.cpp
+++ b/src/models/falcon.cpp
@@ -99,7 +99,7 @@ llama_model_falcon::graph::graph(const llm_graph_params & params) : llm_graph_co
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
@@ -114,9 +114,9 @@ llama_model_falcon::graph::graph(const llm_graph_params & params) : llm_graph_co
// feed forward
{
cur = build_ffn(attn_norm, // !! use the attn norm, not the result
- model.layers[il].ffn_up, NULL, NULL,
- NULL, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ NULL, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_GELU, LLM_FFN_SEQ, il);
cb(cur, "ffn_out", il);
@@ -143,7 +143,7 @@ llama_model_falcon::graph::graph(const llm_graph_params & params) : llm_graph_co
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/flint.cpp b/src/models/flint.cpp
index 173278b6f..8f78ea2b8 100644
--- a/src/models/flint.cpp
+++ b/src/models/flint.cpp
@@ -126,7 +126,7 @@ llama_model_flint::graph::graph(const llm_graph_params & params) :
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, model.layers[il].attn_sinks, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -142,9 +142,9 @@ llama_model_flint::graph::graph(const llm_graph_params & params) :
if ((uint32_t) il < hparams.n_layer_dense_lead) {
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, model.layers[il].ffn_up_s,
- model.layers[il].ffn_gate, NULL, model.layers[il].ffn_gate_s,
- model.layers[il].ffn_down, NULL, model.layers[il].ffn_down_s,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
} else {
@@ -164,9 +164,9 @@ llama_model_flint::graph::graph(const llm_graph_params & params) :
{
ggml_tensor * ffn_shexp =
build_ffn(cur,
- model.layers[il].ffn_up_shexp, NULL, model.layers[il].ffn_up_shexp_s,
- model.layers[il].ffn_gate_shexp, NULL, model.layers[il].ffn_gate_shexp_s,
- model.layers[il].ffn_down_shexp, NULL, model.layers[il].ffn_down_shexp_s,
+ model.layers[il].ffn_up_shexp, NULL,
+ model.layers[il].ffn_gate_shexp, NULL,
+ model.layers[il].ffn_down_shexp, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "ffn_shexp", il);
@@ -190,7 +190,7 @@ llama_model_flint::graph::graph(const llm_graph_params & params) :
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/gemma-embedding.cpp b/src/models/gemma-embedding.cpp
index aa43910be..a07a73c9e 100644
--- a/src/models/gemma-embedding.cpp
+++ b/src/models/gemma-embedding.cpp
@@ -119,7 +119,7 @@ llama_model_gemma_embedding::graph::graph(const llm_graph_params & params) :
cur =
build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
}
@@ -140,9 +140,9 @@ llama_model_gemma_embedding::graph::graph(const llm_graph_params & params) :
// feed-forward network
{
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL, LLM_FFN_GELU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
}
diff --git a/src/models/gemma-embedding2.cpp b/src/models/gemma-embedding2.cpp
index 920827dc5..3dcc4d9c9 100644
--- a/src/models/gemma-embedding2.cpp
+++ b/src/models/gemma-embedding2.cpp
@@ -106,9 +106,9 @@ llama_model_gemma_embedding2::graph::graph(const llm_graph_params & params) :
// self-attention
{
- ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur, model.layers[il].wq_s);
- ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur, model.layers[il].wk_s);
- ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur, model.layers[il].wv_s);
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
cb(Qcur, "Qcur", il);
cb(Kcur, "Kcur", il);
cb(Vcur, "Vcur", il);
@@ -130,7 +130,7 @@ llama_model_gemma_embedding2::graph::graph(const llm_graph_params & params) :
cb(Kcur, "Kcur_pos", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, nullptr, model.layers[il].wo_s,
+ model.layers[il].wo, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, hparams.f_attention_scale, il);
}
@@ -150,9 +150,9 @@ llama_model_gemma_embedding2::graph::graph(const llm_graph_params & params) :
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, nullptr, model.layers[il].ffn_up_s,
- model.layers[il].ffn_gate, nullptr, model.layers[il].ffn_gate_s,
- model.layers[il].ffn_down, nullptr, model.layers[il].ffn_down_s,
+ model.layers[il].ffn_up, nullptr,
+ model.layers[il].ffn_gate, nullptr,
+ model.layers[il].ffn_down, nullptr,
nullptr,
LLM_FFN_GELU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -200,7 +200,7 @@ llama_model_gemma_embedding2::graph::graph(const llm_graph_params & params) :
cb(cur, "result_norm", -1);
// projecting per token is equivalent to projecting after mean pooling
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_embd", -1);
res->t_embd = cur;
}
@@ -212,7 +212,7 @@ llama_model_gemma_embedding2::graph::graph(const llm_graph_params & params) :
ggml_tensor * llama_model_gemma_embedding2::graph::build_inp_per_layer(ggml_tensor * inpL) {
const int64_t n_embd_per_layer = hparams.n_embd_per_layer;
- ggml_tensor * cur = ggml_mul_mat(ctx0, model.per_layer_model_proj, inpL); // [n_embd_per_layer * n_layer, n_tokens]
+ ggml_tensor * cur = build_lora_mm(model.per_layer_model_proj, inpL); // [n_embd_per_layer * n_layer, n_tokens]
cur = ggml_scale(ctx0, cur, 1.0f / sqrtf((float) n_embd));
cur = ggml_reshape_3d(ctx0, cur, n_embd_per_layer, n_layer, n_tokens);
diff --git a/src/models/gemma.cpp b/src/models/gemma.cpp
index 1d42acd01..7439f8ebe 100644
--- a/src/models/gemma.cpp
+++ b/src/models/gemma.cpp
@@ -80,7 +80,7 @@ llama_model_gemma::graph::graph(const llm_graph_params & params) : llm_graph_con
cb(Qcur, "Qcur_scaled", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -98,9 +98,9 @@ llama_model_gemma::graph::graph(const llm_graph_params & params) : llm_graph_con
// feed-forward network
{
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_GELU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -123,7 +123,7 @@ llama_model_gemma::graph::graph(const llm_graph_params & params) : llm_graph_con
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/gemma2.cpp b/src/models/gemma2.cpp
index 826396ce1..92ce6d9a6 100644
--- a/src/models/gemma2.cpp
+++ b/src/models/gemma2.cpp
@@ -99,7 +99,7 @@ llama_model_gemma2::graph::graph(const llm_graph_params & params) : llm_graph_co
Qcur = ggml_scale(ctx0, Qcur, hparams.f_attention_scale);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -122,9 +122,9 @@ llama_model_gemma2::graph::graph(const llm_graph_params & params) : llm_graph_co
// feed-forward network
{
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_GELU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -152,7 +152,7 @@ llama_model_gemma2::graph::graph(const llm_graph_params & params) : llm_graph_co
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
// final logit soft-capping
cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_final_logit_softcapping);
diff --git a/src/models/gemma3.cpp b/src/models/gemma3.cpp
index 9520c3fcf..8c4f235a6 100644
--- a/src/models/gemma3.cpp
+++ b/src/models/gemma3.cpp
@@ -133,7 +133,7 @@ llama_model_gemma3::graph<iswa>::graph(const llm_graph_params & params) : llm_gr
Qcur = ggml_scale(ctx0, Qcur, hparams.f_attention_scale);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -156,9 +156,9 @@ llama_model_gemma3::graph<iswa>::graph(const llm_graph_params & params) : llm_gr
// feed-forward network
{
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_GELU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -186,7 +186,7 @@ llama_model_gemma3::graph<iswa>::graph(const llm_graph_params & params) : llm_gr
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
if (hparams.f_final_logit_softcapping) {
cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_final_logit_softcapping);
diff --git a/src/models/gemma3n.cpp b/src/models/gemma3n.cpp
index 64809def8..91c1c9006 100644
--- a/src/models/gemma3n.cpp
+++ b/src/models/gemma3n.cpp
@@ -122,7 +122,7 @@ llama_model_gemma3n::graph::graph(const llm_graph_params & params) :
ggml_tensor * target_magnitude = calc_magnitude(inpL);
ggml_tensor * inp_repeated = ggml_repeat_4d(ctx0, inpL, n_embd, n_tokens, n_altup - 1, 1);
ggml_tensor * altup_added =
- ggml_mul_mat(ctx0, model.altup_proj, inp_repeated); // shape: [n_embd, n_tokens, n_altup - 1]
+ build_lora_mm(model.altup_proj, inp_repeated); // shape: [n_embd, n_tokens, n_altup - 1]
ggml_tensor * new_magnitude = calc_magnitude(altup_added);
altup_added = ggml_div(ctx0, ggml_mul(ctx0, altup_added, target_magnitude), new_magnitude);
inpL = ggml_concat(ctx0, inpL, altup_added, 2); // shape: [n_embd, n_tokens, n_altup]
@@ -167,7 +167,7 @@ llama_model_gemma3n::graph::graph(const llm_graph_params & params) :
cb(Kcur, "Kcur_pos", il);
cur = build_attn(inp_attn, model.layers[il].wo,
- NULL, model.layers[il].wo_s, Qcur, Kcur, Vcur, nullptr, nullptr, nullptr,
+ NULL, Qcur, Kcur, Vcur, nullptr, nullptr, nullptr,
hparams.f_attention_scale, il);
} else {
// reuse KV cache of earlier layers
@@ -189,7 +189,7 @@ llama_model_gemma3n::graph::graph(const llm_graph_params & params) :
cb(Qcur, "Qcur_pos", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, nullptr, nullptr, nullptr, nullptr, nullptr, hparams.f_attention_scale, il);
}
cur = build_norm(cur, model.layers[il].attn_post_norm, NULL, LLM_NORM_RMS, il);
@@ -271,7 +271,7 @@ llama_model_gemma3n::graph::graph(const llm_graph_params & params) :
ggml_view_3d(ctx0, cur, n_embd, n_tokens, n_altup - 1, ggml_row_size(cur->type, n_embd),
ggml_row_size(cur->type, n_embd * n_tokens), n_embd * n_tokens * ggml_element_size(cur));
ggml_tensor * altup_unembd =
- ggml_mul_mat(ctx0, model.altup_unembd_proj, alt_slice); // shape: [n_embd, n_tokens, n_altup - 1]
+ build_lora_mm(model.altup_unembd_proj, alt_slice); // shape: [n_embd, n_tokens, n_altup - 1]
ggml_tensor * new_magnitude = calc_magnitude(altup_unembd);
altup_unembd = ggml_div(ctx0, ggml_mul(ctx0, altup_unembd, target_magnitude), new_magnitude);
cb(altup_unembd, "altup_unembd", -1);
@@ -297,7 +297,7 @@ llama_model_gemma3n::graph::graph(const llm_graph_params & params) :
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
{
// final logit soft-capping
@@ -354,7 +354,7 @@ ggml_tensor * llama_model_gemma3n::graph::project_per_layer_inputs(ggml_tensor *
const float per_layer_input_scale = 1.0f / sqrtf(2.0f);
ggml_tensor * per_layer_proj;
- per_layer_proj = ggml_mul_mat (ctx0, model.per_layer_model_proj, inp_batch);
+ per_layer_proj = build_lora_mm(model.per_layer_model_proj, inp_batch);
per_layer_proj = ggml_scale (ctx0, per_layer_proj, per_layer_projection_scale);
per_layer_proj = ggml_reshape_3d(ctx0, per_layer_proj, n_embd_altup, n_layer, n_tokens);
@@ -405,7 +405,7 @@ ggml_tensor * llama_model_gemma3n::graph::altup_compute_router_modalities(ggml_t
// router_input_scale
router_inputs = ggml_scale(ctx0, router_inputs, 1.0f / (float) n_embd);
- ggml_tensor * output = ggml_mul_mat(ctx0, model.layers[il].altup_router, router_inputs);
+ ggml_tensor * output = build_lora_mm(model.layers[il].altup_router, router_inputs);
return ggml_tanh(ctx0, output); // [n_altup, n_tokens]
}
diff --git a/src/models/gemma4-assistant.cpp b/src/models/gemma4-assistant.cpp
index 48b7aa864..89b1c1ea5 100644
--- a/src/models/gemma4-assistant.cpp
+++ b/src/models/gemma4-assistant.cpp
@@ -120,7 +120,7 @@ llama_model_gemma4_assistant::graph::graph(const llm_graph_params & params) :
ggml_tensor * xh = ggml_concat(ctx0, x, inp_h, 0);
cb(xh, "inp_xh", -1);
- ggml_tensor * cur = ggml_mul_mat(ctx0, model.nextn_proj_pre, xh);
+ ggml_tensor * cur = build_lora_mm(model.nextn_proj_pre, xh);
cb(cur, "pre_proj", -1);
auto * inp_attn = build_attn_inp_kv_iswa();
@@ -145,7 +145,7 @@ llama_model_gemma4_assistant::graph::graph(const llm_graph_params & params) :
Qcur = build_rope(Qcur, inp_pos, il);
cb(Qcur, "Qcur_pos", il);
- cur = build_attn(inp_attn, model.layers[il].wo, nullptr, nullptr,
+ cur = build_attn(inp_attn, model.layers[il].wo, nullptr,
Qcur, nullptr, nullptr, nullptr, nullptr, nullptr, hparams.f_attention_scale, il);
if (il == n_layer_nextn - 1 && inp_out_ids) {
@@ -163,9 +163,9 @@ llama_model_gemma4_assistant::graph::graph(const llm_graph_params & params) :
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, nullptr, nullptr,
- model.layers[il].ffn_gate, nullptr, nullptr,
- model.layers[il].ffn_down, nullptr, nullptr,
+ model.layers[il].ffn_up, nullptr,
+ model.layers[il].ffn_gate, nullptr,
+ model.layers[il].ffn_down, nullptr,
nullptr,
LLM_FFN_GELU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -189,7 +189,7 @@ llama_model_gemma4_assistant::graph::graph(const llm_graph_params & params) :
cb(logits, "result_output", -1);
res->t_logits = logits;
- ggml_tensor * h_next = ggml_mul_mat(ctx0, model.nextn_proj_post, cur);
+ ggml_tensor * h_next = build_lora_mm(model.nextn_proj_post, cur);
cb(h_next, "h_nextn", -1);
res->t_h_nextn = h_next;
diff --git a/src/models/gemma4.cpp b/src/models/gemma4.cpp
index 491ef1283..2af261dd0 100644
--- a/src/models/gemma4.cpp
+++ b/src/models/gemma4.cpp
@@ -114,7 +114,7 @@ void llama_model_gemma4::load_arch_tensors(llama_model_loader &) {
layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
// MoE router
- layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, TENSOR_NOT_REQUIRED);
+ layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, TENSOR_NOT_REQUIRED | TENSOR_NO_SCALE);
bool has_expert = layer.ffn_gate_inp != nullptr;
// norm
@@ -287,12 +287,12 @@ llama_model_gemma4::graph::graph(const llm_graph_params & params) :
ggml_tensor * qkv_fused = nullptr;
ggml_tensor * Qcur;
if (model.layers[il].wqkv) {
- qkv_fused = build_lora_mm(model.layers[il].wqkv, cur, model.layers[il].wqkv_s);
+ qkv_fused = build_lora_mm(model.layers[il].wqkv, cur);
cb(qkv_fused, "wqkv", il);
const int64_t q_dim = n_embd_head * n_head;
Qcur = ggml_cont(ctx0, ggml_view_2d(ctx0, qkv_fused, q_dim, n_rows, qkv_fused->nb[1], 0));
} else {
- Qcur = build_lora_mm(model.layers[il].wq, cur, model.layers[il].wq_s);
+ Qcur = build_lora_mm(model.layers[il].wq, cur);
}
{
cb(Qcur, "Qcur", il);
@@ -318,9 +318,9 @@ llama_model_gemma4::graph::graph(const llm_graph_params & params) :
Kcur = ggml_cont(ctx0, ggml_view_2d(ctx0, qkv_fused, k_dim, n_tokens, qkv_fused->nb[1], q_dim * esize));
Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, qkv_fused, v_dim, n_tokens, qkv_fused->nb[1], (q_dim + k_dim) * esize));
} else {
- Kcur = build_lora_mm(model.layers[il].wk, cur, model.layers[il].wk_s);
+ Kcur = build_lora_mm(model.layers[il].wk, cur);
Vcur = model.layers[il].wv
- ? build_lora_mm(model.layers[il].wv, cur, model.layers[il].wv_s)
+ ? build_lora_mm(model.layers[il].wv, cur)
: Kcur; // if v_proj is not present, use Kcur as Vcur
}
cb(Kcur, "Kcur", il);
@@ -340,12 +340,12 @@ llama_model_gemma4::graph::graph(const llm_graph_params & params) :
cb(Kcur, "Kcur_pos", il);
cur = build_attn(inp_attn, model.layers[il].wo,
- nullptr, model.layers[il].wo_s, Qcur, Kcur, Vcur, nullptr, nullptr, nullptr,
+ nullptr, Qcur, Kcur, Vcur, nullptr, nullptr, nullptr,
hparams.f_attention_scale, il);
} else {
// reuse KV cache of earlier layers
cur = build_attn(inp_attn,
- model.layers[il].wo, nullptr, model.layers[il].wo_s,
+ model.layers[il].wo, nullptr,
Qcur, nullptr, nullptr, nullptr, nullptr, nullptr, hparams.f_attention_scale, il);
}
@@ -372,9 +372,9 @@ llama_model_gemma4::graph::graph(const llm_graph_params & params) :
cb(cur_mlp, "ffn_norm_1", il);
cur_mlp = build_ffn(cur_mlp,
- model.layers[il].ffn_up, nullptr, model.layers[il].ffn_up_s,
- model.layers[il].ffn_gate, nullptr, model.layers[il].ffn_gate_s,
- model.layers[il].ffn_down, nullptr, model.layers[il].ffn_down_s,
+ model.layers[il].ffn_up, nullptr,
+ model.layers[il].ffn_gate, nullptr,
+ model.layers[il].ffn_down, nullptr,
nullptr,
LLM_FFN_GELU, LLM_FFN_PAR, il);
cur_mlp = build_norm(cur_mlp,
@@ -421,9 +421,9 @@ llama_model_gemma4::graph::graph(const llm_graph_params & params) :
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, nullptr, model.layers[il].ffn_up_s,
- model.layers[il].ffn_gate, nullptr, model.layers[il].ffn_gate_s,
- model.layers[il].ffn_down, nullptr, model.layers[il].ffn_down_s,
+ model.layers[il].ffn_up, nullptr,
+ model.layers[il].ffn_gate, nullptr,
+ model.layers[il].ffn_down, nullptr,
nullptr,
LLM_FFN_GELU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -497,7 +497,7 @@ llama_model_gemma4::graph::graph(const llm_graph_params & params) :
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
if (hparams.f_final_logit_softcapping) {
cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_final_logit_softcapping);
@@ -589,7 +589,7 @@ ggml_tensor * llama_model_gemma4::graph::project_per_layer_inputs(ggml_tensor *
ggml_build_forward_expand(gf, proj_norm_w);
ggml_tensor * per_layer_proj;
- per_layer_proj = ggml_mul_mat (ctx0, model.per_layer_model_proj, inp_batch);
+ per_layer_proj = build_lora_mm(model.per_layer_model_proj, inp_batch);
per_layer_proj = ggml_reshape_3d(ctx0, per_layer_proj, n_embd_per_layer, n_layer, n_tokens);
const float proj_eps = hparams.f_norm_rms_eps / (per_layer_projection_scale * per_layer_projection_scale);
diff --git a/src/models/glm-dsa.cpp b/src/models/glm-dsa.cpp
index c0ff62e4a..5881933b2 100644
--- a/src/models/glm-dsa.cpp
+++ b/src/models/glm-dsa.cpp
@@ -250,7 +250,7 @@ llama_model_glm_dsa::graph::graph(const llm_graph_params & params) :
// self_attention
{
- ggml_tensor * qr = ggml_mul_mat(ctx0, model.layers[il].wq_a, cur);
+ ggml_tensor * qr = build_lora_mm(model.layers[il].wq_a, cur);
cb(qr, "qr", il);
qr = build_norm(qr, model.layers[il].attn_q_a_norm, nullptr, LLM_NORM_RMS, il);
@@ -261,7 +261,7 @@ llama_model_glm_dsa::graph::graph(const llm_graph_params & params) :
// lightning indexer
if (hparams.is_indexer_full(il)) {
// "full" layer
- ggml_tensor * indexer_q = ggml_mul_mat(ctx0, model.layers[il].indexer_attn_q_b, qr);
+ ggml_tensor * indexer_q = build_lora_mm(model.layers[il].indexer_attn_q_b, qr);
cb(indexer_q, "indexer_q", il);
// {n_embd_indexer_head, n_indexer_head, n_tokens}
@@ -269,7 +269,7 @@ llama_model_glm_dsa::graph::graph(const llm_graph_params & params) :
indexer_q = build_rope_indexer(indexer_q, inp_pos, il);
cb(indexer_q, "indexer_q", il);
- ggml_tensor * indexer_k = ggml_mul_mat(ctx0, model.layers[il].indexer_attn_k, cur);
+ ggml_tensor * indexer_k = build_lora_mm(model.layers[il].indexer_attn_k, cur);
cb(indexer_k, "indexer_k", il);
indexer_k = build_norm(indexer_k, model.layers[il].indexer_k_norm, model.layers[il].indexer_k_norm_b, LLM_NORM, il);
@@ -292,7 +292,7 @@ llama_model_glm_dsa::graph::graph(const llm_graph_params & params) :
ggml_build_forward_expand(gf, mctx_lid->cpy_k(ctx0, indexer_k, k_idxs_lid, il));
// prepare indexer weights
- ggml_tensor * indexer_weights = ggml_mul_mat(ctx0, model.layers[il].indexer_proj, cur);
+ ggml_tensor * indexer_weights = build_lora_mm(model.layers[il].indexer_proj, cur);
cb(indexer_weights, "indexer_weights", il);
// get cached indexer keys
@@ -322,7 +322,7 @@ llama_model_glm_dsa::graph::graph(const llm_graph_params & params) :
cb(top_k, "top_k", il);
}
- ggml_tensor * q = ggml_mul_mat(ctx0, model.layers[il].wq_b, qr);
+ ggml_tensor * q = build_lora_mm(model.layers[il].wq_b, qr);
cb(q, "q", il);
// split into {n_embd_head_qk_nope, n_head, n_tokens}
@@ -337,7 +337,7 @@ llama_model_glm_dsa::graph::graph(const llm_graph_params & params) :
ggml_row_size(q->type, n_embd_head_k) * n_head, ggml_row_size(q->type, n_embd_head_qk_nope));
cb(q_pe, "q_pe", il);
- ggml_tensor * kv_cmpr_pe = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);
+ ggml_tensor * kv_cmpr_pe = build_lora_mm(model.layers[il].wkv_a_mqa, cur);
cb(kv_cmpr_pe, "kv_cmpr_pe", il);
// split into {kv_lora_rank, n_tokens}
@@ -369,7 +369,7 @@ llama_model_glm_dsa::graph::graph(const llm_graph_params & params) :
cb(q_nope, "q_nope_perm", il);
// {n_embd_head_qk_nope, kv_lora_rank, n_head} x {n_embd_head_qk_nope, n_tokens, n_head}
- ggml_tensor * q_nope_absorbed = ggml_mul_mat(ctx0, model.layers[il].wk_b, q_nope);
+ ggml_tensor * q_nope_absorbed = build_lora_mm(model.layers[il].wk_b, q_nope);
cb(q_nope_absorbed, "q_nope_absorbed", il);
// {kv_lora_rank, n_head, n_tokens}
@@ -394,7 +394,7 @@ llama_model_glm_dsa::graph::graph(const llm_graph_params & params) :
// note: MLA with the absorption optimization converts into MQA (ie: GQA with 1 group)
cur = build_attn(inp_attn_dsa,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, model.layers[il].wv_b, top_k, kq_scale, il);
}
}
@@ -412,9 +412,9 @@ llama_model_glm_dsa::graph::graph(const llm_graph_params & params) :
if ((uint32_t) il < hparams.n_layer_dense_lead) {
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, model.layers[il].ffn_up_s,
- model.layers[il].ffn_gate, NULL, model.layers[il].ffn_gate_s,
- model.layers[il].ffn_down, NULL, model.layers[il].ffn_down_s,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
} else {
@@ -438,9 +438,9 @@ llama_model_glm_dsa::graph::graph(const llm_graph_params & params) :
{
ggml_tensor * ffn_shexp =
build_ffn(cur,
- model.layers[il].ffn_up_shexp, NULL, model.layers[il].ffn_up_shexp_s,
- model.layers[il].ffn_gate_shexp, NULL, model.layers[il].ffn_gate_shexp_s,
- model.layers[il].ffn_down_shexp, NULL, model.layers[il].ffn_down_shexp_s,
+ model.layers[il].ffn_up_shexp, NULL,
+ model.layers[il].ffn_gate_shexp, NULL,
+ model.layers[il].ffn_down_shexp, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "ffn_shexp", il);
@@ -472,7 +472,7 @@ llama_model_glm_dsa::graph::graph(const llm_graph_params & params) :
res->t_embd = cur;
// lm_head
- cur = ggml_mul_mat(ctx0, model.output, cur);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
@@ -559,7 +559,7 @@ llama_model_glm_dsa::graph_mtp::graph_mtp(const llm_graph_params & params)
ggml_tensor * concat = ggml_concat(ctx0, e_norm, h_norm, /*dim=*/ 0);
cb(concat, "mtp_concat", il);
- ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat, layer.nextn.eh_proj_s);
+ ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat);
cb(cur, "mtp_eh_proj", il);
ggml_tensor * inpSA = cur;
@@ -569,13 +569,13 @@ llama_model_glm_dsa::graph_mtp::graph_mtp(const llm_graph_params & params)
// self-attention: dense MLA, same construction as the deepseek2 trunk graph
{
- ggml_tensor * q = ggml_mul_mat(ctx0, layer.wq_a, cur);
+ ggml_tensor * q = build_lora_mm(layer.wq_a, cur);
cb(q, "mtp_q", il);
q = build_norm(q, layer.attn_q_a_norm, nullptr, LLM_NORM_RMS, il);
cb(q, "mtp_q", il);
- q = ggml_mul_mat(ctx0, layer.wq_b, q);
+ q = build_lora_mm(layer.wq_b, q);
cb(q, "mtp_q", il);
// split into {n_embd_head_qk_nope, n_head, n_tokens}
@@ -590,7 +590,7 @@ llama_model_glm_dsa::graph_mtp::graph_mtp(const llm_graph_params & params)
ggml_row_size(q->type, n_embd_head_k) * n_head, ggml_row_size(q->type, n_embd_head_qk_nope));
cb(q_pe, "mtp_q_pe", il);
- ggml_tensor * kv_cmpr_pe = ggml_mul_mat(ctx0, layer.wkv_a_mqa, cur);
+ ggml_tensor * kv_cmpr_pe = build_lora_mm(layer.wkv_a_mqa, cur);
cb(kv_cmpr_pe, "mtp_kv_cmpr_pe", il);
// split into {kv_lora_rank, n_tokens}
@@ -620,7 +620,7 @@ llama_model_glm_dsa::graph_mtp::graph_mtp(const llm_graph_params & params)
cb(q_nope, "mtp_q_nope_perm", il);
// {n_embd_head_qk_nope, kv_lora_rank, n_head} x {n_embd_head_qk_nope, n_tokens, n_head}
- ggml_tensor * q_nope_absorbed = ggml_mul_mat(ctx0, layer.wk_b, q_nope);
+ ggml_tensor * q_nope_absorbed = build_lora_mm(layer.wk_b, q_nope);
cb(q_nope_absorbed, "mtp_q_nope_absorbed", il);
// {kv_lora_rank, n_head, n_tokens}
@@ -645,7 +645,7 @@ llama_model_glm_dsa::graph_mtp::graph_mtp(const llm_graph_params & params)
// note: MLA with the absorption optimization converts into MQA (ie: GQA with 1 group)
cur = build_attn(inp_attn,
- layer.wo, NULL, layer.wo_s,
+ layer.wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, layer.wv_b, kq_scale, il);
cb(cur, "mtp_attn_out", il);
}
@@ -675,9 +675,9 @@ llama_model_glm_dsa::graph_mtp::graph_mtp(const llm_graph_params & params)
// FFN shared expert
ggml_tensor * ffn_shexp =
build_ffn(cur,
- layer.ffn_up_shexp, NULL, layer.ffn_up_shexp_s,
- layer.ffn_gate_shexp, NULL, layer.ffn_gate_shexp_s,
- layer.ffn_down_shexp, NULL, layer.ffn_down_shexp_s,
+ layer.ffn_up_shexp, NULL,
+ layer.ffn_gate_shexp, NULL,
+ layer.ffn_down_shexp, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "mtp_ffn_shexp", il);
@@ -702,9 +702,8 @@ llama_model_glm_dsa::graph_mtp::graph_mtp(const llm_graph_params & params)
cb(cur, "mtp_shared_head_norm", -1);
ggml_tensor * head_w = layer.nextn.shared_head_head ? layer.nextn.shared_head_head : model.output;
- ggml_tensor * head_s = layer.nextn.shared_head_head ? layer.nextn.shared_head_head_s : model.output_s;
GGML_ASSERT(head_w && "GLM_DSA MTP: missing LM head (nextn.shared_head_head or model.output)");
- cur = build_lora_mm(head_w, cur, head_s);
+ cur = build_lora_mm(head_w, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/glm4-moe.cpp b/src/models/glm4-moe.cpp
index a3e2fa46e..73de9cbea 100644
--- a/src/models/glm4-moe.cpp
+++ b/src/models/glm4-moe.cpp
@@ -186,7 +186,7 @@ llama_model_glm4_moe::graph_mtp::graph_mtp(const llm_graph_params & params)
ggml_tensor * concat = ggml_concat(ctx0, e_norm, h_norm, 0);
cb(concat, "mtp_concat", il);
- ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat, layer.nextn.eh_proj_s);
+ ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat);
cb(cur, "mtp_eh_proj", il);
ggml_tensor * inpSA = cur;
@@ -214,7 +214,7 @@ llama_model_glm4_moe::graph_mtp::graph_mtp(const llm_graph_params & params)
cb(Vcur, "mtp_Vcur", il);
cur = build_attn(inp_attn,
- layer.wo, nullptr, layer.wo_s,
+ layer.wo, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr,
1.0f / sqrtf(float(n_embd_head)), il);
cb(cur, "mtp_attn_out", il);
@@ -239,9 +239,9 @@ llama_model_glm4_moe::graph_mtp::graph_mtp(const llm_graph_params & params)
cb(routed_out, "mtp_ffn_moe_out", il);
ggml_tensor * shared_out = build_ffn(cur,
- layer.ffn_up_shexp, nullptr, nullptr,
- layer.ffn_gate_shexp, nullptr, nullptr,
- layer.ffn_down_shexp, nullptr, nullptr,
+ layer.ffn_up_shexp, nullptr,
+ layer.ffn_gate_shexp, nullptr,
+ layer.ffn_down_shexp, nullptr,
nullptr,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(shared_out, "mtp_ffn_shexp_out", il);
@@ -269,12 +269,9 @@ llama_model_glm4_moe::graph_mtp::graph_mtp(const llm_graph_params & params)
ggml_tensor * head_w = layer.nextn.shared_head_head
? layer.nextn.shared_head_head
: model.output;
- ggml_tensor * head_s = layer.nextn.shared_head_head
- ? layer.nextn.shared_head_head_s
- : model.output_s;
GGML_ASSERT(head_w && "GLM4_MOE MTP: missing LM head (nextn.shared_head_head or model.output)");
- cur = build_lora_mm(head_w, cur, head_s);
+ cur = build_lora_mm(head_w, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
@@ -335,7 +332,7 @@ llama_model_glm4_moe::graph::graph(const llm_graph_params & params) : llm_graph_
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && crop_before_nextn(inp_out_ids)) {
@@ -353,9 +350,9 @@ llama_model_glm4_moe::graph::graph(const llm_graph_params & params) : llm_graph_
if (static_cast<uint32_t>(il) < hparams.n_layer_dense_lead) {
// Dense FFN layer
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -376,9 +373,9 @@ llama_model_glm4_moe::graph::graph(const llm_graph_params & params) : llm_graph_
// Process shared expert on original input
ggml_tensor * shared_out = build_ffn(cur,
- model.layers[il].ffn_up_shexp, NULL, NULL,
- model.layers[il].ffn_gate_shexp, NULL, NULL,
- model.layers[il].ffn_down_shexp, NULL, NULL,
+ model.layers[il].ffn_up_shexp, NULL,
+ model.layers[il].ffn_gate_shexp, NULL,
+ model.layers[il].ffn_down_shexp, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(shared_out, "ffn_shexp_out", il);
@@ -409,7 +406,7 @@ llama_model_glm4_moe::graph::graph(const llm_graph_params & params) : llm_graph_
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/glm4.cpp b/src/models/glm4.cpp
index 5c50b2f05..f2f9c2f57 100644
--- a/src/models/glm4.cpp
+++ b/src/models/glm4.cpp
@@ -115,7 +115,7 @@ llama_model_glm4::graph::graph(const llm_graph_params & params) : llm_graph_cont
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -138,9 +138,9 @@ llama_model_glm4::graph::graph(const llm_graph_params & params) : llm_graph_cont
// MLP
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- NULL, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ NULL, NULL,
+ model.layers[il].ffn_down, NULL,
NULL, LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
cb(cur, "ffn_out", il);
@@ -163,7 +163,7 @@ llama_model_glm4::graph::graph(const llm_graph_params & params) : llm_graph_cont
res->t_embd = cur;
// Output projection
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/glm5-next.cpp b/src/models/glm5-next.cpp
index 5d3944af2..a2d67b164 100644
--- a/src/models/glm5-next.cpp
+++ b/src/models/glm5-next.cpp
@@ -199,7 +199,7 @@ ggml_tensor * llama_model_glm5_next::graph::build_kda_conv1d(
const int64_t conv_state_size = (d_conv - 1) * d_inner;
const int64_t n_embd_r_total = 3 * conv_state_size;
- ggml_tensor * x_proj = ggml_mul_mat(ctx0, proj_w, x);
+ ggml_tensor * x_proj = build_lora_mm(proj_w, x);
ggml_tensor * x_3d = ggml_reshape_3d(ctx0, x_proj, d_inner, n_seq_tokens, n_seqs);
ggml_tensor * conv_x = nullptr;
@@ -450,7 +450,7 @@ llama_model_glm5_next::graph::build_hc_pre(
ggml_tensor * flat = ggml_reshape_2d(ctx0, x, hc_dim, nt);
ggml_tensor * flat_norm = ggml_rms_norm(ctx0, flat, norm_rms_eps);
- ggml_tensor * mixes = ggml_mul_mat(ctx0, hc_fn, flat_norm);
+ ggml_tensor * mixes = build_lora_mm(hc_fn, flat_norm);
cb(mixes, "hc_mixes", il);
ggml_tensor * scale_pre = dsv4_view_1d(ctx0, hc_scale, 1, 0);
@@ -589,7 +589,7 @@ llama_model_glm5_next::graph_mtp::graph_mtp(const llm_graph_params & params) :
ggml_tensor * h_norm = build_norm(h, layer.nextn.hnorm, nullptr, LLM_NORM_RMS, il);
cb(h_norm, "mtp_hnorm", il);
- ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, ggml_concat(ctx0, e_norm, h_norm, 0), layer.nextn.eh_proj_s);
+ ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, ggml_concat(ctx0, e_norm, h_norm, 0));
cb(cur, "mtp_eh_proj", il);
ggml_tensor * inpSA = cur;
@@ -628,9 +628,9 @@ llama_model_glm5_next::graph_mtp::graph_mtp(const llm_graph_params & params) :
cb(moe_out, "mtp_ffn_moe_out", il);
ggml_tensor * ffn_shexp = build_ffn(cur,
- layer.ffn_up_shexp, nullptr, nullptr,
- layer.ffn_gate_shexp, nullptr, nullptr,
- layer.ffn_down_shexp, nullptr, nullptr,
+ layer.ffn_up_shexp, nullptr,
+ layer.ffn_gate_shexp, nullptr,
+ layer.ffn_down_shexp, nullptr,
nullptr, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "mtp_ffn_shexp", il);
@@ -652,9 +652,8 @@ llama_model_glm5_next::graph_mtp::graph_mtp(const llm_graph_params & params) :
cb(cur, "mtp_shared_head_norm", -1);
ggml_tensor * head_w = layer.nextn.shared_head_head ? layer.nextn.shared_head_head : model.output;
- ggml_tensor * head_s = layer.nextn.shared_head_head ? layer.nextn.shared_head_head_s : model.output_s;
GGML_ASSERT(head_w && "GLM5-Next MTP: missing both nextn.shared_head_head and output");
- cur = build_lora_mm(head_w, cur, head_s);
+ cur = build_lora_mm(head_w, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
@@ -730,9 +729,9 @@ llama_model_glm5_next::graph::graph(const llm_graph_params & params) :
if ((uint32_t) il < hparams.n_layer_dense_lead) {
cur = build_ffn(cur,
- layer.ffn_up, nullptr, nullptr,
- layer.ffn_gate, nullptr, nullptr,
- layer.ffn_down, nullptr, nullptr,
+ layer.ffn_up, nullptr,
+ layer.ffn_gate, nullptr,
+ layer.ffn_down, nullptr,
nullptr, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
} else {
@@ -750,9 +749,9 @@ llama_model_glm5_next::graph::graph(const llm_graph_params & params) :
cb(moe_out, "ffn_moe_out", il);
ggml_tensor * ffn_shexp = build_ffn(cur,
- layer.ffn_up_shexp, nullptr, nullptr,
- layer.ffn_gate_shexp, nullptr, nullptr,
- layer.ffn_down_shexp, nullptr, nullptr,
+ layer.ffn_up_shexp, nullptr,
+ layer.ffn_gate_shexp, nullptr,
+ layer.ffn_down_shexp, nullptr,
nullptr, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "ffn_shexp", il);
@@ -788,7 +787,7 @@ llama_model_glm5_next::graph::graph(const llm_graph_params & params) :
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = ggml_mul_mat(ctx0, model.output, cur);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
}
@@ -813,8 +812,8 @@ ggml_tensor * llama_model_glm5_next::graph::build_kda_layer(
cb(Vcur, "kda_v_conv", il);
// Decay gate, ssm_a holds -exp(A_log)
- ggml_tensor * f_a = ggml_mul_mat(ctx0, layer.ssm_f_a, cur);
- ggml_tensor * g1 = ggml_mul_mat(ctx0, layer.ssm_f_b, f_a);
+ ggml_tensor * f_a = build_lora_mm(layer.ssm_f_a, cur);
+ ggml_tensor * g1 = build_lora_mm(layer.ssm_f_b, f_a);
g1 = ggml_add(ctx0, g1, layer.ssm_dt_b);
ggml_tensor * A = ggml_reshape_3d(ctx0, layer.ssm_a, 1, n_head_kda, 1);
@@ -833,7 +832,7 @@ ggml_tensor * llama_model_glm5_next::graph::build_kda_layer(
g1 = ggml_reshape_4d(ctx0, g1, head_dim, n_head_kda, n_seq_tokens, n_seqs);
- ggml_tensor * beta = ggml_mul_mat(ctx0, layer.ssm_beta, cur);
+ ggml_tensor * beta = build_lora_mm(layer.ssm_beta, cur);
beta = ggml_reshape_4d(ctx0, beta, 1, n_head_kda, n_seq_tokens, n_seqs);
beta = ggml_sigmoid(ctx0, beta);
cb(beta, "kda_beta", il);
@@ -852,8 +851,8 @@ ggml_tensor * llama_model_glm5_next::graph::build_kda_layer(
cb(output, "kda_scan_out", il);
// output gate, then RMSNorm(o) * Sigmoid(g2)
- ggml_tensor * g_a = ggml_mul_mat(ctx0, layer.ssm_g_a, cur);
- ggml_tensor * g2 = ggml_mul_mat(ctx0, layer.ssm_g_b, g_a);
+ ggml_tensor * g_a = build_lora_mm(layer.ssm_g_a, cur);
+ ggml_tensor * g2 = build_lora_mm(layer.ssm_g_b, g_a);
g2 = ggml_reshape_3d(ctx0, g2, head_dim, n_head_kda, n_tokens);
ggml_tensor * o = ggml_reshape_3d(ctx0, output, head_dim, n_head_kda, n_tokens);
@@ -863,7 +862,7 @@ ggml_tensor * llama_model_glm5_next::graph::build_kda_layer(
ggml_tensor * gated = ggml_mul(ctx0, normed, ggml_sigmoid(ctx0, g2));
gated = ggml_cont_2d(ctx0, gated, d_inner, n_tokens);
- cur = ggml_mul_mat(ctx0, layer.wo, gated);
+ cur = build_lora_mm(layer.wo, gated);
cb(cur, "kda_out", il);
return cur;
@@ -883,16 +882,16 @@ ggml_tensor * llama_model_glm5_next::graph::build_kpool_select(
const int64_t n_pool = inp_kpool->pool_cells->ne[0];
const int64_t n_new = inp_kpool->n_new;
- ggml_tensor * iq = ggml_mul_mat(ctx0, layer.indexer_attn_q_b, qr);
+ ggml_tensor * iq = build_lora_mm(layer.indexer_attn_q_b, qr);
iq = ggml_reshape_3d(ctx0, iq, n_embd_indexer, n_indexer_head, n_tokens);
cb(iq, "indexer_q", il);
// Per-token key and pool gate scores, cached together
- ggml_tensor * ik = ggml_mul_mat(ctx0, layer.indexer_attn_k, cur);
+ ggml_tensor * ik = build_lora_mm(layer.indexer_attn_k, cur);
ik = build_norm(ik, layer.indexer_k_norm, layer.indexer_k_norm_b, LLM_NORM, il);
cb(ik, "indexer_k", il);
- ggml_tensor * ig = ggml_mul_mat(ctx0, layer.indexer_kpool_gate, cur);
+ ggml_tensor * ig = build_lora_mm(layer.indexer_kpool_gate, cur);
cb(ig, "indexer_gate", il);
// Cache rows store key | gate | pooled
@@ -934,7 +933,7 @@ ggml_tensor * llama_model_glm5_next::graph::build_kpool_select(
ggml_tensor * sel_idx = nullptr;
{
- ggml_tensor * weights = ggml_mul_mat(ctx0, layer.indexer_proj, cur);
+ ggml_tensor * weights = build_lora_mm(layer.indexer_proj, cur);
weights = ggml_scale(ctx0, weights, 1.0f / sqrtf(float(n_embd_indexer * n_indexer_head)));
cb(weights, "indexer_weights", il);
@@ -1027,21 +1026,21 @@ llama_model_glm5_next::graph::build_dsa_layer(
GGML_ASSERT(hparams.n_rot() == 0 && "GLM5-Next MLA is nope-only");
- ggml_tensor * qr = ggml_mul_mat(ctx0, layer.wq_a, cur);
+ ggml_tensor * qr = build_lora_mm(layer.wq_a, cur);
qr = build_norm(qr, layer.attn_q_a_norm, nullptr, LLM_NORM_RMS, il);
cb(qr, "q_resid", il);
- ggml_tensor * q = ggml_mul_mat(ctx0, layer.wq_b, qr);
+ ggml_tensor * q = build_lora_mm(layer.wq_b, qr);
q = ggml_reshape_3d(ctx0, q, n_embd_head_qk_nope, n_head, n_tokens);
- ggml_tensor * kv_cmpr = ggml_mul_mat(ctx0, layer.wkv_a_mqa, cur);
+ ggml_tensor * kv_cmpr = build_lora_mm(layer.wkv_a_mqa, cur);
kv_cmpr = build_norm(kv_cmpr, layer.attn_kv_a_norm, nullptr, LLM_NORM_RMS, il);
kv_cmpr = ggml_reshape_3d(ctx0, kv_cmpr, kv_lora_rank, 1, n_tokens);
cb(kv_cmpr, "kv_cmpr", il);
// absorb wk_b so the cache holds only the latent
ggml_tensor * q_absorbed = ggml_permute(ctx0, q, 0, 2, 1, 3);
- q_absorbed = ggml_mul_mat(ctx0, layer.wk_b, q_absorbed);
+ q_absorbed = build_lora_mm(layer.wk_b, q_absorbed);
q_absorbed = ggml_permute(ctx0, q_absorbed, 0, 2, 1, 3);
cb(q_absorbed, "q_absorbed", il);
@@ -1069,7 +1068,7 @@ llama_model_glm5_next::graph::build_dsa_layer(
ggml_tensor * out = build_attn_mha(q_absorbed, k, v, nullptr, sel, nullptr, nullptr, layer.wv_b, inp_kpool->n_sel, kq_scale, il);
cb(out, "kqv_out", il);
- out = ggml_mul_mat(ctx0, layer.wo, out);
+ out = build_lora_mm(layer.wo, out);
cb(out, "attn_out", il);
return { out, sel };
diff --git a/src/models/gpt2.cpp b/src/models/gpt2.cpp
index 711a148a2..b34d3688e 100644
--- a/src/models/gpt2.cpp
+++ b/src/models/gpt2.cpp
@@ -93,7 +93,7 @@ llama_model_gpt2::graph::graph(const llm_graph_params & params) : llm_graph_cont
n_embd_head, n_head, n_head_kv, il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
@@ -115,9 +115,9 @@ llama_model_gpt2::graph::graph(const llm_graph_params & params) : llm_graph_cont
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
- NULL, NULL, NULL,
- model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b,
+ NULL, NULL,
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b,
NULL,
LLM_FFN_GELU, LLM_FFN_SEQ, il);
cb(cur, "ffn_out", il);
@@ -140,7 +140,7 @@ llama_model_gpt2::graph::graph(const llm_graph_params & params) : llm_graph_cont
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
if (model.output_b) {
cur = ggml_add(ctx0, cur, model.output_b);
}
diff --git a/src/models/gptneox.cpp b/src/models/gptneox.cpp
index 8217e5a8a..bd56a7736 100644
--- a/src/models/gptneox.cpp
+++ b/src/models/gptneox.cpp
@@ -121,7 +121,7 @@ llama_model_gptneox::graph::graph(const llm_graph_params & params) : llm_graph_c
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
@@ -144,9 +144,9 @@ llama_model_gptneox::graph::graph(const llm_graph_params & params) : llm_graph_c
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
- NULL, NULL, NULL,
- model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b,
+ NULL, NULL,
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b,
NULL,
LLM_FFN_GELU, LLM_FFN_SEQ, il);
cb(cur, "ffn_out", il);
@@ -176,9 +176,9 @@ llama_model_gptneox::graph::graph(const llm_graph_params & params) : llm_graph_c
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
- NULL, NULL, NULL,
- model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b,
+ NULL, NULL,
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b,
NULL,
LLM_FFN_GELU, LLM_FFN_SEQ, il);
cb(cur, "ffn_out", il);
@@ -201,7 +201,7 @@ llama_model_gptneox::graph::graph(const llm_graph_params & params) : llm_graph_c
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/granite-hybrid.cpp b/src/models/granite-hybrid.cpp
index d638663af..8c2ce7da8 100644
--- a/src/models/granite-hybrid.cpp
+++ b/src/models/granite-hybrid.cpp
@@ -189,7 +189,7 @@ llama_model_granite_hybrid::graph::graph(const llm_graph_params & params) :
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
// For Granite architectures - scale logits
if (hparams.f_logit_scale) {
@@ -216,7 +216,7 @@ ggml_tensor * llama_model_granite_hybrid::graph::build_attention_layer(ggml_tens
const float kq_scale =
hparams.f_attention_scale == 0.0f ? 1.0f / sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "attn_out", il);
return cur;
@@ -238,9 +238,9 @@ ggml_tensor * llama_model_granite_hybrid::graph::build_layer_ffn(ggml_tensor *
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, model.layers[il].ffn_up_b, model.layers[il].ffn_up_s,
- model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, model.layers[il].ffn_gate_s,
- model.layers[il].ffn_down, model.layers[il].ffn_down_b, model.layers[il].ffn_down_s,
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b,
+ model.layers[il].ffn_gate, model.layers[il].ffn_gate_b,
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -267,9 +267,9 @@ ggml_tensor * llama_model_granite_hybrid::graph::build_layer_ffn(ggml_tensor *
if (hparams.n_ff_shexp > 0) {
ggml_tensor * ffn_shexp =
build_ffn(cur,
- model.layers[il].ffn_up_shexp, NULL, model.layers[il].ffn_up_shexp_s,
- model.layers[il].ffn_gate_shexp, NULL, model.layers[il].ffn_gate_shexp_s,
- model.layers[il].ffn_down_shexp, NULL, model.layers[il].ffn_down_shexp_s,
+ model.layers[il].ffn_up_shexp, NULL,
+ model.layers[il].ffn_gate_shexp, NULL,
+ model.layers[il].ffn_down_shexp, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "ffn_shexp", il);
diff --git a/src/models/granite-swa.cpp b/src/models/granite-swa.cpp
index 2e2609f52..021de4537 100644
--- a/src/models/granite-swa.cpp
+++ b/src/models/granite-swa.cpp
@@ -196,7 +196,7 @@ llama_model_granite_swa::graph::graph(const llm_graph_params & params) : llm_gra
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
// For Granite architectures - scale logits
cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_logit_scale);
@@ -225,7 +225,7 @@ ggml_tensor * llama_model_granite_swa::graph::build_attention_layer(
// Pass layer.attn_sinks to build_attn for sink-based attention modulation
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, model.layers[il].attn_sinks, nullptr, kq_scale, il);
cb(cur, "attn_out", il);
return cur;
@@ -252,9 +252,9 @@ ggml_tensor * llama_model_granite_swa::graph::build_layer_ffn(
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, model.layers[il].ffn_up_b, model.layers[il].ffn_up_s,
- model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, model.layers[il].ffn_gate_s,
- model.layers[il].ffn_down, model.layers[il].ffn_down_b, model.layers[il].ffn_down_s,
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b,
+ model.layers[il].ffn_gate, model.layers[il].ffn_gate_b,
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -283,9 +283,9 @@ ggml_tensor * llama_model_granite_swa::graph::build_layer_ffn(
// For Granite MoE Shared - gate+up kept fused in ffn_up_shexp
if (hparams.n_ff_shexp > 0) {
ggml_tensor * ffn_shexp = build_ffn(cur,
- model.layers[il].ffn_up_shexp, NULL, model.layers[il].ffn_up_shexp_s,
- NULL, NULL, NULL,
- model.layers[il].ffn_down_shexp, NULL, model.layers[il].ffn_down_shexp_s,
+ model.layers[il].ffn_up_shexp, NULL,
+ NULL, NULL,
+ model.layers[il].ffn_down_shexp, NULL,
NULL,
LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
cb(ffn_shexp, "ffn_shexp", il);
diff --git a/src/models/granite-switch.cpp b/src/models/granite-switch.cpp
index 53195c97c..f6f1906c6 100644
--- a/src/models/granite-switch.cpp
+++ b/src/models/granite-switch.cpp
@@ -202,8 +202,8 @@ ggml_tensor * llama_model_granite_switch::graph::build_switched_lora_delta(
ggml_tensor * x = ggml_reshape_3d(ctx0, cur, n_in, 1, n_tokens);
ggml_tensor * ids2 = ggml_reshape_2d(ctx0, ids, 1, n_tokens);
- ggml_tensor * a = ggml_mul_mat_id(ctx0, lora_a, x, ids2); // {max_rank, 1, n_tokens}
- ggml_tensor * d = ggml_mul_mat_id(ctx0, lora_b, a, ids2); // {n_out, 1, n_tokens}
+ ggml_tensor * a = build_lora_mm_id(lora_a, x, ids2); // {max_rank, 1, n_tokens}
+ ggml_tensor * d = build_lora_mm_id(lora_b, a, ids2); // {n_out, 1, n_tokens}
return ggml_reshape_2d(ctx0, d, d->ne[0], n_tokens);
}
@@ -214,7 +214,7 @@ ggml_tensor * llama_model_granite_switch::graph::build_switched_lora_mm(
ggml_tensor * lora_b,
ggml_tensor * cur,
ggml_tensor * ids) {
- ggml_tensor * base = ggml_mul_mat(ctx0, w, cur);
+ ggml_tensor * base = build_lora_mm(w, cur);
ggml_tensor * delta = build_switched_lora_delta(lora_a, lora_b, cur, ids);
return ggml_add(ctx0, base, delta);
}
@@ -272,7 +272,7 @@ llama_model_granite_switch::graph::graph(const llm_graph_params & params) : llm_
ggml_tensor * Vr = router_lane(router_vval);
ggml_tensor * router_out = build_attn(inp_attn,
- nullptr, nullptr, nullptr,
+ nullptr, nullptr,
Qr, Kr, Vr, nullptr, nullptr, nullptr, /*kq_scale=*/1.0f, /*il=*/R);
cb(router_out, "router_out", R);
@@ -320,7 +320,7 @@ llama_model_granite_switch::graph::graph(const llm_graph_params & params) : llm_
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_logit_scale);
cb(cur, "result_output", -1);
@@ -341,7 +341,7 @@ ggml_tensor * llama_model_granite_switch::graph::build_attention_layer(
const int64_t n_head = hparams.n_head(il);
const int64_t n_head_kv = hparams.n_head_kv(il);
- ggml_tensor * qkv = ggml_mul_mat(ctx0, layer.wqkv, cur);
+ ggml_tensor * qkv = build_lora_mm(layer.wqkv, cur);
cb(qkv, "wqkv", il);
const int64_t n_embd_q = n_embd_head * n_head;
@@ -371,7 +371,7 @@ ggml_tensor * llama_model_granite_switch::graph::build_attention_layer(
// wo = nullptr so build_attn returns concatenated heads; o-proj is switched below
ggml_tensor * attn = build_attn(inp_attn,
- nullptr, nullptr, nullptr,
+ nullptr, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(attn, "attn_pre_o", il);
diff --git a/src/models/granite.cpp b/src/models/granite.cpp
index 0a5dd78ea..b43fb1540 100644
--- a/src/models/granite.cpp
+++ b/src/models/granite.cpp
@@ -191,7 +191,7 @@ llama_model_granite::graph::graph(const llm_graph_params & params) : llm_graph_c
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
// For Granite architectures - scale logits
cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_logit_scale);
@@ -218,7 +218,7 @@ ggml_tensor * llama_model_granite::graph::build_attention_layer(
const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "attn_out", il);
return cur;
@@ -245,9 +245,9 @@ ggml_tensor * llama_model_granite::graph::build_layer_ffn(
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, model.layers[il].ffn_up_b, model.layers[il].ffn_up_s,
- model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, model.layers[il].ffn_gate_s,
- model.layers[il].ffn_down, model.layers[il].ffn_down_b, model.layers[il].ffn_down_s,
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b,
+ model.layers[il].ffn_gate, model.layers[il].ffn_gate_b,
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -275,9 +275,9 @@ ggml_tensor * llama_model_granite::graph::build_layer_ffn(
// For Granite MoE Shared
if (hparams.n_ff_shexp > 0) {
ggml_tensor * ffn_shexp = build_ffn(cur,
- model.layers[il].ffn_up_shexp, NULL, model.layers[il].ffn_up_shexp_s,
- model.layers[il].ffn_gate_shexp, NULL, model.layers[il].ffn_gate_shexp_s,
- model.layers[il].ffn_down_shexp, NULL, model.layers[il].ffn_down_shexp_s,
+ model.layers[il].ffn_up_shexp, NULL,
+ model.layers[il].ffn_gate_shexp, NULL,
+ model.layers[il].ffn_down_shexp, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "ffn_shexp", il);
diff --git a/src/models/grok.cpp b/src/models/grok.cpp
index 36debf2ad..e2598e65c 100644
--- a/src/models/grok.cpp
+++ b/src/models/grok.cpp
@@ -121,7 +121,7 @@ llama_model_grok::graph::graph(const llm_graph_params & params) : llm_graph_cont
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -158,9 +158,9 @@ llama_model_grok::graph::graph(const llm_graph_params & params) : llm_graph_cont
if (model.layers[il].ffn_up) {
ggml_tensor * ffn_out = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_GELU, LLM_FFN_PAR, il);
cb(ffn_out, "ffn_out", il);
@@ -194,7 +194,7 @@ llama_model_grok::graph::graph(const llm_graph_params & params) : llm_graph_cont
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cur = ggml_scale(ctx0, cur, hparams.f_logit_scale);
diff --git a/src/models/grovemoe.cpp b/src/models/grovemoe.cpp
index c86ddbbf5..e55a8ad99 100644
--- a/src/models/grovemoe.cpp
+++ b/src/models/grovemoe.cpp
@@ -112,7 +112,7 @@ llama_model_grovemoe::graph::graph(const llm_graph_params & params) :
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
}
@@ -182,7 +182,7 @@ llama_model_grovemoe::graph::graph(const llm_graph_params & params) :
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/hrm-text.cpp b/src/models/hrm-text.cpp
index 1a800ee5b..06e59bc2d 100644
--- a/src/models/hrm-text.cpp
+++ b/src/models/hrm-text.cpp
@@ -130,7 +130,7 @@ ggml_tensor * llama_model_hrm_text::graph::build_stack(llm_graph_input_attn_kv *
cb(Kcur, "Kcur", s);
cur = build_attn(inp_attn,
- nullptr, nullptr, nullptr,
+ nullptr, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, s);
cb(cur, "attn_pregate", s);
@@ -140,7 +140,7 @@ ggml_tensor * llama_model_hrm_text::graph::build_stack(llm_graph_input_attn_kv *
cur = ggml_mul(ctx0, cur, gate);
cb(cur, "attn_gated", s);
- cur = build_lora_mm(layer.wo, cur, layer.wo_s);
+ cur = build_lora_mm(layer.wo, cur);
cb(cur, "attn_out", s);
}
@@ -152,9 +152,9 @@ ggml_tensor * llama_model_hrm_text::graph::build_stack(llm_graph_input_attn_kv *
cb(cur, "ffn_norm", s);
cur = build_ffn(cur,
- layer.ffn_up, nullptr, nullptr,
- layer.ffn_gate, nullptr, nullptr,
- layer.ffn_down, nullptr, nullptr,
+ layer.ffn_up, nullptr,
+ layer.ffn_gate, nullptr,
+ layer.ffn_down, nullptr,
nullptr,
LLM_FFN_SILU, LLM_FFN_PAR, s);
cb(cur, "ffn_out", s);
@@ -210,7 +210,7 @@ llama_model_hrm_text::graph::graph(const llm_graph_params & params) :
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/hunyuan-moe.cpp b/src/models/hunyuan-moe.cpp
index 00659c88e..0bcf00810 100644
--- a/src/models/hunyuan-moe.cpp
+++ b/src/models/hunyuan-moe.cpp
@@ -115,7 +115,7 @@ llama_model_hunyuan_moe::graph::graph(const llm_graph_params & params) : llm_gra
cb(Qcur, "Qcur_norm", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "attn_out", il);
}
@@ -133,9 +133,9 @@ llama_model_hunyuan_moe::graph::graph(const llm_graph_params & params) : llm_gra
// feed-forward network (non-MoE)
ggml_tensor * cur_mlp = build_ffn(cur,
- model.layers[il].ffn_up_shexp, NULL, NULL,
- model.layers[il].ffn_gate_shexp, NULL, NULL,
- model.layers[il].ffn_down_shexp, NULL, NULL,
+ model.layers[il].ffn_up_shexp, NULL,
+ model.layers[il].ffn_gate_shexp, NULL,
+ model.layers[il].ffn_down_shexp, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur_mlp, "ffn_mlp", il);
@@ -176,7 +176,7 @@ llama_model_hunyuan_moe::graph::graph(const llm_graph_params & params) : llm_gra
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
}
diff --git a/src/models/hunyuan-vl.cpp b/src/models/hunyuan-vl.cpp
index 219ba39bd..cf4020499 100644
--- a/src/models/hunyuan-vl.cpp
+++ b/src/models/hunyuan-vl.cpp
@@ -111,7 +111,7 @@ llama_model_hunyuan_vl::graph::graph(const llm_graph_params & params) : llm_grap
cb(Qcur, "Qcur_norm", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "attn_out", il);
}
@@ -128,9 +128,9 @@ llama_model_hunyuan_vl::graph::graph(const llm_graph_params & params) : llm_grap
cb(cur, "ffn_norm", il);
// feed-forward network (non-MoE)
ggml_tensor * cur_mlp = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur_mlp, "ffn_out", il);
@@ -152,7 +152,7 @@ llama_model_hunyuan_vl::graph::graph(const llm_graph_params & params) : llm_grap
cb(cur, "result_norm", -1);
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
}
diff --git a/src/models/hy-v3.cpp b/src/models/hy-v3.cpp
index f53d94827..482567746 100644
--- a/src/models/hy-v3.cpp
+++ b/src/models/hy-v3.cpp
@@ -129,7 +129,7 @@ llama_model_hy_v3::graph::graph(const llm_graph_params & params) : llm_graph_con
Kcur = build_rope(Kcur, inp_pos, il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "attn_out", il);
}
@@ -148,9 +148,9 @@ llama_model_hy_v3::graph::graph(const llm_graph_params & params) : llm_graph_con
if (model.layers[il].ffn_gate_inp == nullptr) {
// dense FFN (leading dense blocks)
cur = build_ffn(cur,
- model.layers[il].ffn_up, model.layers[il].ffn_up_b, model.layers[il].ffn_up_s,
- model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, model.layers[il].ffn_gate_s,
- model.layers[il].ffn_down, model.layers[il].ffn_down_b, model.layers[il].ffn_down_s,
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b,
+ model.layers[il].ffn_gate, model.layers[il].ffn_gate_b,
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b,
nullptr,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_dense_out", il);
@@ -173,9 +173,9 @@ llama_model_hy_v3::graph::graph(const llm_graph_params & params) : llm_graph_con
// shared expert (always active, no gate)
ggml_tensor * sh_out = build_ffn(cur,
- model.layers[il].ffn_up_shexp, nullptr, model.layers[il].ffn_up_shexp_s,
- model.layers[il].ffn_gate_shexp, nullptr, model.layers[il].ffn_gate_shexp_s,
- model.layers[il].ffn_down_shexp, nullptr, model.layers[il].ffn_down_shexp_s,
+ model.layers[il].ffn_up_shexp, nullptr,
+ model.layers[il].ffn_gate_shexp, nullptr,
+ model.layers[il].ffn_down_shexp, nullptr,
nullptr,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(sh_out, "ffn_shared_out", il);
@@ -206,7 +206,7 @@ llama_model_hy_v3::graph::graph(const llm_graph_params & params) : llm_graph_con
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
}
@@ -288,7 +288,7 @@ llama_model_hy_v3::graph_mtp::graph_mtp(const llm_graph_params & params)
const float kq_scale = 1.0f / sqrtf(float(n_embd_head));
cur = build_attn(inp_attn,
- layer.wo, layer.wo_b, layer.wo_s,
+ layer.wo, layer.wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "mtp_attn_out", il);
}
@@ -301,9 +301,9 @@ llama_model_hy_v3::graph_mtp::graph_mtp(const llm_graph_params & params)
if (layer.ffn_gate_inp == nullptr) {
cur = build_ffn(cur,
- layer.ffn_up, layer.ffn_up_b, layer.ffn_up_s,
- layer.ffn_gate, layer.ffn_gate_b, layer.ffn_gate_s,
- layer.ffn_down, layer.ffn_down_b, layer.ffn_down_s,
+ layer.ffn_up, layer.ffn_up_b,
+ layer.ffn_gate, layer.ffn_gate_b,
+ layer.ffn_down, layer.ffn_down_b,
nullptr,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "mtp_ffn_dense_out", il);
@@ -324,9 +324,9 @@ llama_model_hy_v3::graph_mtp::graph_mtp(const llm_graph_params & params)
cb(moe_out, "mtp_ffn_moe_out", il);
ggml_tensor * sh_out = build_ffn(cur,
- layer.ffn_up_shexp, nullptr, layer.ffn_up_shexp_s,
- layer.ffn_gate_shexp, nullptr, layer.ffn_gate_shexp_s,
- layer.ffn_down_shexp, nullptr, layer.ffn_down_shexp_s,
+ layer.ffn_up_shexp, nullptr,
+ layer.ffn_gate_shexp, nullptr,
+ layer.ffn_down_shexp, nullptr,
nullptr,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(sh_out, "mtp_ffn_shared_out", il);
@@ -354,9 +354,8 @@ llama_model_hy_v3::graph_mtp::graph_mtp(const llm_graph_params & params)
cb(cur, "mtp_shared_head_norm", -1);
ggml_tensor * head_w = layer.nextn.shared_head_head ? layer.nextn.shared_head_head : model.output;
- ggml_tensor * head_s = layer.nextn.shared_head_head ? layer.nextn.shared_head_head_s : model.output_s;
GGML_ASSERT(head_w && "HY_V3 MTP: missing LM head (nextn.shared_head_head or model.output)");
- cur = build_lora_mm(head_w, cur, head_s);
+ cur = build_lora_mm(head_w, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/hy-v4-flash.cpp b/src/models/hy-v4-flash.cpp
index 7f5c39232..063b7aea4 100644
--- a/src/models/hy-v4-flash.cpp
+++ b/src/models/hy-v4-flash.cpp
@@ -552,7 +552,7 @@ llama_model_hy_v4_flash::graph::build_hc_pre(
ggml_tensor * flat = ggml_reshape_2d(ctx0, x, hc * n_embd, nt);
ggml_tensor * flat_norm = ggml_rms_norm(ctx0, flat, hparams.f_norm_rms_eps);
- ggml_tensor * mixes = ggml_mul_mat(ctx0, hc_fn, flat_norm);
+ ggml_tensor * mixes = build_lora_mm(hc_fn, flat_norm);
cb(mixes, "hc_mixes", il);
ggml_tensor * scale_pre = hy_v4_flash_view_1d(ctx0, hc_scale, 1, 0);
@@ -617,7 +617,7 @@ ggml_tensor * llama_model_hy_v4_flash::graph::build_hc_head(
ggml_tensor * flat = ggml_reshape_2d(ctx0, x, hc * n_embd, nt);
ggml_tensor * flat_norm = ggml_rms_norm(ctx0, flat, hparams.f_norm_rms_eps);
- ggml_tensor * mixes = ggml_mul_mat(ctx0, hc_fn, flat_norm);
+ ggml_tensor * mixes = build_lora_mm(hc_fn, flat_norm);
cb(mixes, "hc_head_mixes", -1);
ggml_tensor * pre = ggml_mul(ctx0, mixes, hc_scale);
@@ -861,7 +861,7 @@ ggml_tensor * llama_model_hy_v4_flash::graph::build_attention(
cb(kv_cmpr, "kv_cmpr", il);
q_nope = ggml_permute(ctx0, q_nope, 0, 2, 1, 3);
- ggml_tensor * q_nope_absorbed = ggml_mul_mat(ctx0, layer.wk_b, q_nope);
+ ggml_tensor * q_nope_absorbed = build_lora_mm(layer.wk_b, q_nope);
q_nope_absorbed = ggml_permute(ctx0, q_nope_absorbed, 0, 2, 1, 3);
// rope goes last so build_rope_shift() can shift the cache in place
@@ -875,7 +875,7 @@ ggml_tensor * llama_model_hy_v4_flash::graph::build_attention(
if (hparams.is_swa(il)) {
attn = build_attn(inp_attn->get_swa(),
- nullptr, nullptr, nullptr,
+ nullptr, nullptr,
Qcur, Kcur, Vcur, nullptr, layer.attn_sinks, layer.wv_b, kq_scale, il);
} else {
// as build_attn of the DSA input, with the cells of the picked pools instead of a top-k of cells
@@ -945,9 +945,9 @@ ggml_tensor * llama_model_hy_v4_flash::graph::build_latent_moe(
}
ggml_tensor * shexp = build_ffn(cur,
- layer.ffn_up_shexp, NULL, NULL,
- layer.ffn_gate_shexp, NULL, NULL,
- layer.ffn_down_shexp, NULL, NULL,
+ layer.ffn_up_shexp, NULL,
+ layer.ffn_gate_shexp, NULL,
+ layer.ffn_down_shexp, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(shexp, "ffn_shexp", il);
@@ -1037,9 +1037,9 @@ llama_model_hy_v4_flash::graph::graph(const llama_model & model, const llm_graph
if ((uint32_t) il < hparams.n_layer_dense_lead) {
cur = build_ffn(cur,
- layer.ffn_up, NULL, NULL,
- layer.ffn_gate, NULL, NULL,
- layer.ffn_down, NULL, NULL,
+ layer.ffn_up, NULL,
+ layer.ffn_gate, NULL,
+ layer.ffn_down, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
} else {
cur = build_latent_moe(model, cur, il);
diff --git a/src/models/hy-v4.cpp b/src/models/hy-v4.cpp
index de7d87cd0..88465f70f 100644
--- a/src/models/hy-v4.cpp
+++ b/src/models/hy-v4.cpp
@@ -195,7 +195,7 @@ llama_model_hy_v4::graph::build_hc_pre(
ggml_tensor * flat = ggml_reshape_2d(ctx0, x, hc * n_embd, nt);
ggml_tensor * flat_norm = ggml_rms_norm(ctx0, flat, hparams.f_norm_rms_eps);
- ggml_tensor * mixes = ggml_mul_mat(ctx0, hc_fn, flat_norm); // [2*hc, nt]
+ ggml_tensor * mixes = build_lora_mm(hc_fn, flat_norm); // [2*hc, nt]
cb(mixes, "hc_mixes", il);
ggml_tensor * scale_pre = hy_v4_view_1d(ctx0, hc_scale, 1, 0);
@@ -264,7 +264,7 @@ ggml_tensor * llama_model_hy_v4::graph::build_hc_head(
ggml_tensor * flat = ggml_reshape_2d(ctx0, x, hc * n_embd, nt);
ggml_tensor * flat_norm = ggml_rms_norm(ctx0, flat, hparams.f_norm_rms_eps);
- ggml_tensor * mixes = ggml_mul_mat(ctx0, hc_fn, flat_norm); // [hc, nt]
+ ggml_tensor * mixes = build_lora_mm(hc_fn, flat_norm); // [hc, nt]
cb(mixes, "hc_head_mixes", -1);
ggml_tensor * pre = ggml_mul(ctx0, mixes, hc_scale);
@@ -289,9 +289,9 @@ ggml_tensor * llama_model_hy_v4::graph::build_attention(
const int64_t n_embd_head_qk_nope = n_embd_head_k - n_embd_head_qk_rope;
const uint32_t kv_lora_rank = hparams.n_lora_kv;
- ggml_tensor * q = ggml_mul_mat(ctx0, layer.wq_a, cur);
+ ggml_tensor * q = build_lora_mm(layer.wq_a, cur);
q = build_norm(q, layer.attn_q_a_norm, nullptr, LLM_NORM_RMS, il);
- q = ggml_mul_mat(ctx0, layer.wq_b, q);
+ q = build_lora_mm(layer.wq_b, q);
ggml_tensor * q_nope = ggml_view_3d(ctx0, q, n_embd_head_qk_nope, n_head, n_tokens,
ggml_row_size(q->type, n_embd_head_k), ggml_row_size(q->type, n_embd_head_k) * n_head, 0);
@@ -299,7 +299,7 @@ ggml_tensor * llama_model_hy_v4::graph::build_attention(
ggml_row_size(q->type, n_embd_head_k), ggml_row_size(q->type, n_embd_head_k) * n_head,
ggml_row_size(q->type, n_embd_head_qk_nope));
- ggml_tensor * kv_cmpr_pe = ggml_mul_mat(ctx0, layer.wkv_a_mqa, cur);
+ ggml_tensor * kv_cmpr_pe = build_lora_mm(layer.wkv_a_mqa, cur);
ggml_tensor * kv_cmpr = ggml_view_2d(ctx0, kv_cmpr_pe, kv_lora_rank, n_tokens,
ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope), 0);
ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_cmpr_pe, n_embd_head_qk_rope, 1, n_tokens,
@@ -317,7 +317,7 @@ ggml_tensor * llama_model_hy_v4::graph::build_attention(
// MLA absorption: q_nope @ wk_b -> compressed space
q_nope = ggml_permute(ctx0, q_nope, 0, 2, 1, 3);
- ggml_tensor * q_nope_absorbed = ggml_mul_mat(ctx0, layer.wk_b, q_nope);
+ ggml_tensor * q_nope_absorbed = build_lora_mm(layer.wk_b, q_nope);
q_nope_absorbed = ggml_permute(ctx0, q_nope_absorbed, 0, 2, 1, 3);
// note: rope must go first for in-place context shifting in build_rope_shift()
@@ -329,12 +329,12 @@ ggml_tensor * llama_model_hy_v4::graph::build_attention(
// MLA-as-MQA; wo applied manually below so the gated-MLA gate can sit before o_proj
ggml_tensor * attn = build_attn(inp_attn,
- nullptr, nullptr, nullptr,
+ nullptr, nullptr,
Qcur, Kcur, Vcur, nullptr, layer.attn_sinks, layer.wv_b, kq_scale, il);
cb(attn, "attn_kqv", il); // [n_head * n_embd_head_v, n_tokens]
// gated MLA: elementwise sigmoid gate on the decompressed attention output
- ggml_tensor * gate = ggml_mul_mat(ctx0, layer.wqkv_gate, cur);
+ ggml_tensor * gate = build_lora_mm(layer.wqkv_gate, cur);
gate = ggml_sigmoid(ctx0, gate);
attn = ggml_mul(ctx0, attn, gate);
cb(attn, "attn_gated", il);
@@ -357,14 +357,14 @@ ggml_tensor * llama_model_hy_v4::graph::build_indexer_top_k(
const int64_t n_embd_indexer = hparams.indexer_head_size;
// nope rows come first, so rope only the last n_embd_indexer_rope rows, same as the MLA path
- ggml_tensor * iq = ggml_mul_mat(ctx0, layer.indexer_attn_q_b, qr);
+ ggml_tensor * iq = build_lora_mm(layer.indexer_attn_q_b, qr);
iq = ggml_reshape_3d(ctx0, iq, n_embd_indexer, n_indexer_head, n_tokens);
iq = build_rope_indexer(iq, inp_pos, il);
cb(iq, "indexer_q", il);
- ggml_tensor * ik = ggml_mul_mat(ctx0, layer.indexer_attn_k, cur);
+ ggml_tensor * ik = build_lora_mm(layer.indexer_attn_k, cur);
ik = build_norm(ik, layer.indexer_k_norm, layer.indexer_k_norm_b, LLM_NORM, il);
@@ -380,7 +380,7 @@ ggml_tensor * llama_model_hy_v4::graph::build_indexer_top_k(
const auto & k_idxs_lid = inp_attn_dsa->get_k_idxs_lid();
ggml_build_forward_expand(gf, mctx_lid->cpy_k(ctx0, ik, k_idxs_lid, il));
- ggml_tensor * iw = ggml_mul_mat(ctx0, layer.indexer_proj, cur);
+ ggml_tensor * iw = build_lora_mm(layer.indexer_proj, cur);
ik = mctx_lid->get_k(ctx0, il);
@@ -416,7 +416,7 @@ llama_model_hy_v4::graph::build_attention_dsa(
const int64_t n_embd_head_qk_nope = n_embd_head_k - n_embd_head_qk_rope;
const uint32_t kv_lora_rank = hparams.n_lora_kv;
- ggml_tensor * qr = ggml_mul_mat(ctx0, layer.wq_a, cur);
+ ggml_tensor * qr = build_lora_mm(layer.wq_a, cur);
qr = build_norm(qr, layer.attn_q_a_norm, nullptr, LLM_NORM_RMS, il);
ggml_tensor * top_k = last_top_k;
@@ -426,7 +426,7 @@ llama_model_hy_v4::graph::build_attention_dsa(
}
GGML_ASSERT(top_k != nullptr);
- ggml_tensor * q = ggml_mul_mat(ctx0, layer.wq_b, qr);
+ ggml_tensor * q = build_lora_mm(layer.wq_b, qr);
ggml_tensor * q_nope = ggml_view_3d(ctx0, q, n_embd_head_qk_nope, n_head, n_tokens,
ggml_row_size(q->type, n_embd_head_k), ggml_row_size(q->type, n_embd_head_k) * n_head, 0);
@@ -434,7 +434,7 @@ llama_model_hy_v4::graph::build_attention_dsa(
ggml_row_size(q->type, n_embd_head_k), ggml_row_size(q->type, n_embd_head_k) * n_head,
ggml_row_size(q->type, n_embd_head_qk_nope));
- ggml_tensor * kv_cmpr_pe = ggml_mul_mat(ctx0, layer.wkv_a_mqa, cur);
+ ggml_tensor * kv_cmpr_pe = build_lora_mm(layer.wkv_a_mqa, cur);
ggml_tensor * kv_cmpr = ggml_view_2d(ctx0, kv_cmpr_pe, kv_lora_rank, n_tokens,
ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope), 0);
ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_cmpr_pe, n_embd_head_qk_rope, 1, n_tokens,
@@ -451,7 +451,7 @@ llama_model_hy_v4::graph::build_attention_dsa(
cb(kv_cmpr, "kv_cmpr", il);
q_nope = ggml_permute(ctx0, q_nope, 0, 2, 1, 3);
- ggml_tensor * q_nope_absorbed = ggml_mul_mat(ctx0, layer.wk_b, q_nope);
+ ggml_tensor * q_nope_absorbed = build_lora_mm(layer.wk_b, q_nope);
q_nope_absorbed = ggml_permute(ctx0, q_nope_absorbed, 0, 2, 1, 3);
ggml_tensor * Qcur = ggml_concat(ctx0, q_nope_absorbed, q_pe, 0);
@@ -461,11 +461,11 @@ llama_model_hy_v4::graph::build_attention_dsa(
ggml_tensor * Vcur = kv_cmpr;
ggml_tensor * attn = build_attn(inp_attn_dsa,
- nullptr, nullptr, nullptr,
+ nullptr, nullptr,
Qcur, Kcur, Vcur, nullptr, layer.attn_sinks, layer.wv_b, top_k, kq_scale, il);
cb(attn, "attn_kqv", il);
- ggml_tensor * gate = ggml_mul_mat(ctx0, layer.wqkv_gate, cur);
+ ggml_tensor * gate = build_lora_mm(layer.wqkv_gate, cur);
gate = ggml_sigmoid(ctx0, gate);
attn = ggml_mul(ctx0, attn, gate);
cb(attn, "attn_gated", il);
@@ -529,9 +529,9 @@ llama_model_hy_v4::graph::graph(const llm_graph_params & params) :
const auto & layer = model.layers[il];
if ((uint32_t) il < hparams.n_layer_dense_lead) {
cur = build_ffn(cur,
- layer.ffn_up, NULL, NULL,
- layer.ffn_gate, NULL, NULL,
- layer.ffn_down, NULL, NULL,
+ layer.ffn_up, NULL,
+ layer.ffn_gate, NULL,
+ layer.ffn_down, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
} else {
@@ -551,9 +551,9 @@ llama_model_hy_v4::graph::graph(const llm_graph_params & params) :
cb(moe_out, "ffn_moe_out", il);
ggml_tensor * ffn_shexp = build_ffn(cur,
- layer.ffn_up_shexp, NULL, NULL,
- layer.ffn_gate_shexp, NULL, NULL,
- layer.ffn_down_shexp, NULL, NULL,
+ layer.ffn_up_shexp, NULL,
+ layer.ffn_gate_shexp, NULL,
+ layer.ffn_down_shexp, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "ffn_shexp", il);
@@ -579,7 +579,7 @@ llama_model_hy_v4::graph::graph(const llm_graph_params & params) :
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = ggml_mul_mat(ctx0, model.output, cur);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
}
diff --git a/src/models/inkling2-loop.cpp b/src/models/inkling2-loop.cpp
index 1b9455b52..6d6732f8d 100644
--- a/src/models/inkling2-loop.cpp
+++ b/src/models/inkling2-loop.cpp
@@ -53,7 +53,7 @@ void llama_model_inkling2_loop::load_arch_tensors(llama_model_loader & ml) {
for (uint32_t il = core_start; il < core_end; ++il) {
auto & l = loop_layers[il - core_start];
// the q, k, v and ffn message queries
- l.msg_query = create_tensor(tn(LLM_TENSOR_LOOP_MSG_QUERY, "weight", il), {n_embd, LOOP_CTRL_COUNT}, 0);
+ l.msg_query = create_tensor(tn(LLM_TENSOR_LOOP_MSG_QUERY, "weight", il), {n_embd, LOOP_CTRL_COUNT}, TENSOR_NO_SCALE);
for (int c = 0; c < LOOP_CTRL_COUNT; ++c) {
l.ctrl_down[c] = create_tensor(tn(down[c], "weight", il), {2*n_embd, rank}, 0);
l.ctrl_gain[c] = create_tensor(tn(gain[c], "weight", il), {rank, n_embd}, 0);
diff --git a/src/models/inkling2.cpp b/src/models/inkling2.cpp
index f55e4ac56..25d4df7c0 100644
--- a/src/models/inkling2.cpp
+++ b/src/models/inkling2.cpp
@@ -99,7 +99,7 @@ void llama_model_inkling2::load_arch_tensors(llama_model_loader &) {
output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab_padded}, 0);
prec_policy.set_acc(output, GGML_PREC_F32);
output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
- output_res_query = create_tensor(tn(LLM_TENSOR_OUTPUT_RES_QUERY, "weight"), {n_embd}, 0);
+ output_res_query = create_tensor(tn(LLM_TENSOR_OUTPUT_RES_QUERY, "weight"), {n_embd}, TENSOR_NO_SCALE);
for (int i = 0; i < n_layer_phys(); ++i) {
auto & layer = layers[i];
@@ -122,7 +122,7 @@ void llama_model_inkling2::load_arch_tensors(llama_model_loader &) {
// relative-position bias
layer.attn_rel = create_tensor(tn(LLM_TENSOR_ATTN_REL, "weight", i), {n_embd, n_rel*n_head}, 0);
- layer.attn_rel_bias = create_tensor(tn(LLM_TENSOR_ATTN_REL_BIAS, "weight", i), {n_rel, n_extent}, 0);
+ layer.attn_rel_bias = create_tensor(tn(LLM_TENSOR_ATTN_REL_BIAS, "weight", i), {n_rel, n_extent}, TENSOR_NO_SCALE);
layer.attn_rel_clip = create_tensor(tn(LLM_TENSOR_ATTN_REL_CLIP, "weight", i), {n_rel, n_head}, 0);
// short convolutions
@@ -144,9 +144,9 @@ void llama_model_inkling2::load_arch_tensors(llama_model_loader &) {
// block attention residuals
if (i > 0) {
- layer.attn_res_query = create_tensor(tn(LLM_TENSOR_ATTN_RES_QUERY, "weight", i), {n_embd, hparams.is_swa(i) ? 1 : 3}, 0);
+ layer.attn_res_query = create_tensor(tn(LLM_TENSOR_ATTN_RES_QUERY, "weight", i), {n_embd, hparams.is_swa(i) ? 1 : 3}, TENSOR_NO_SCALE);
}
- layer.ffn_res_query = create_tensor(tn(LLM_TENSOR_FFN_RES_QUERY, "weight", i), {n_embd}, 0);
+ layer.ffn_res_query = create_tensor(tn(LLM_TENSOR_FFN_RES_QUERY, "weight", i), {n_embd}, TENSOR_NO_SCALE);
// MoE
layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
@@ -386,7 +386,7 @@ ggml_tensor * llama_model_inkling2::graph::build_layer(int il, int ip, ggml_tens
sinks = zero;
cur = build_attn(inp,
- nullptr, nullptr, nullptr,
+ nullptr, nullptr,
Qcur, Kcur, Vcur, table, sinks, 1.0f/sqrtf(n_embd_head), il);
cur = ggml_mul(ctx0, cur, ggml_sigmoid(ctx0, build_lora_mm(layer.wqkv_gate, inputs[0])));
diff --git a/src/models/internlm2.cpp b/src/models/internlm2.cpp
index 39ce67af6..09b084bf4 100644
--- a/src/models/internlm2.cpp
+++ b/src/models/internlm2.cpp
@@ -79,7 +79,7 @@ llama_model_internlm2::graph::graph(const llm_graph_params & params) : llm_graph
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -96,9 +96,9 @@ llama_model_internlm2::graph::graph(const llm_graph_params & params) : llm_graph
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -121,7 +121,7 @@ llama_model_internlm2::graph::graph(const llm_graph_params & params) : llm_graph
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/jais.cpp b/src/models/jais.cpp
index daa3315bf..6dbbee8a9 100644
--- a/src/models/jais.cpp
+++ b/src/models/jais.cpp
@@ -79,7 +79,7 @@ llama_model_jais::graph::graph(const llm_graph_params & params) : llm_graph_cont
n_embd_head, n_head, n_head_kv, il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/float(n_embd_head), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -99,9 +99,9 @@ llama_model_jais::graph::graph(const llm_graph_params & params) : llm_graph_cont
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
- model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
- model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b,
+ model.layers[il].ffn_gate, model.layers[il].ffn_gate_b,
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -123,7 +123,7 @@ llama_model_jais::graph::graph(const llm_graph_params & params) : llm_graph_cont
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/jais2.cpp b/src/models/jais2.cpp
index 1f077fd86..207523fd5 100644
--- a/src/models/jais2.cpp
+++ b/src/models/jais2.cpp
@@ -95,7 +95,7 @@ llama_model_jais2::graph::graph(const llm_graph_params & params) : llm_graph_con
cb(Kcur, "Kcur_rope", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
@@ -118,9 +118,9 @@ llama_model_jais2::graph::graph(const llm_graph_params & params) : llm_graph_con
// FFN with relu2 activation (ReLU squared) - no gate projection
// up -> relu2 -> down
cur = build_ffn(cur,
- model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
- NULL, NULL, NULL, // no gate
- model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b,
+ NULL, NULL, // no gate
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b,
NULL,
LLM_FFN_RELU_SQR, LLM_FFN_SEQ, il);
cb(cur, "ffn_out", il);
@@ -141,7 +141,7 @@ llama_model_jais2::graph::graph(const llm_graph_params & params) : llm_graph_con
res->t_embd = cur;
// Output projection
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/jamba.cpp b/src/models/jamba.cpp
index 842df63b6..60a9a1ea6 100644
--- a/src/models/jamba.cpp
+++ b/src/models/jamba.cpp
@@ -134,7 +134,7 @@ llama_model_jamba::graph::graph(const llm_graph_params & params) : llm_build_mam
// No RoPE :)
cur = build_attn(inp_hybrid->get_attn(),
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, NULL, NULL, NULL, 1.0f/sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -152,9 +152,9 @@ llama_model_jamba::graph::graph(const llm_graph_params & params) : llm_build_mam
if (model.layers[il].ffn_gate_inp == nullptr) {
// FFN
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -189,7 +189,7 @@ llama_model_jamba::graph::graph(const llm_graph_params & params) : llm_build_mam
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/k2-horizon.cpp b/src/models/k2-horizon.cpp
index cf82347b7..5654d5759 100644
--- a/src/models/k2-horizon.cpp
+++ b/src/models/k2-horizon.cpp
@@ -243,9 +243,9 @@ llama_model_k2_horizon::graph::graph(const llm_graph_params & params) : llm_grap
{
ggml_tensor * attn_inp = cur; // saved for the output gate
- ggml_tensor * Qcur = build_lora_mm(layer.wq, cur, layer.wq_s);
- ggml_tensor * Kcur = build_lora_mm(layer.wk, cur, layer.wk_s);
- ggml_tensor * Vcur = is_mova_layer ? build_routed_value(layer, cur, il) : build_lora_mm(layer.wv, cur, layer.wv_s);
+ ggml_tensor * Qcur = build_lora_mm(layer.wq, cur);
+ ggml_tensor * Kcur = build_lora_mm(layer.wk, cur);
+ ggml_tensor * Vcur = is_mova_layer ? build_routed_value(layer, cur, il) : build_lora_mm(layer.wv, cur);
Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
@@ -270,19 +270,19 @@ llama_model_k2_horizon::graph::graph(const llm_graph_params & params) : llm_grap
const bool gated = layer.wqkv_gate != nullptr;
cur = build_attn(inp_attn,
- gated ? nullptr : layer.wo, gated ? nullptr : layer.wo_b, gated ? nullptr : layer.wo_s,
+ gated ? nullptr : layer.wo, gated ? nullptr : layer.wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
if (gated) {
// softplus with beta = ln(2): log2(1 + 2^x)
constexpr float ln2 = 0.6931471805599453f;
- ggml_tensor * gate = build_lora_mm(layer.wqkv_gate, attn_inp, layer.wqkv_gate_s);
+ ggml_tensor * gate = build_lora_mm(layer.wqkv_gate, attn_inp);
gate = ggml_scale(ctx0, gate, ln2);
gate = ggml_softplus(ctx0, gate);
gate = ggml_scale(ctx0, gate, 1.4426950408889634f); // 1 / ln(2)
cur = ggml_mul(ctx0, cur, gate);
- cur = build_lora_mm(layer.wo, cur, layer.wo_s);
+ cur = build_lora_mm(layer.wo, cur);
if (layer.wo_b) {
cur = ggml_add(ctx0, cur, layer.wo_b);
}
@@ -316,9 +316,9 @@ llama_model_k2_horizon::graph::graph(const llm_graph_params & params) : llm_grap
if (layer.ffn_gate_shexp) {
ggml_tensor * ffn_shexp = build_ffn(cur,
- layer.ffn_up_shexp, NULL, NULL,
- layer.ffn_gate_shexp, NULL, NULL,
- layer.ffn_down_shexp, NULL, NULL,
+ layer.ffn_up_shexp, NULL,
+ layer.ffn_gate_shexp, NULL,
+ layer.ffn_down_shexp, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cur = ggml_add(ctx0, moe_out, ffn_shexp);
@@ -327,9 +327,9 @@ llama_model_k2_horizon::graph::graph(const llm_graph_params & params) : llm_grap
}
} else {
cur = build_ffn(cur,
- layer.ffn_up, NULL, NULL,
- layer.ffn_gate, NULL, NULL,
- layer.ffn_down, NULL, NULL,
+ layer.ffn_up, NULL,
+ layer.ffn_gate, NULL,
+ layer.ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
}
@@ -348,7 +348,7 @@ llama_model_k2_horizon::graph::graph(const llm_graph_params & params) : llm_grap
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
}
diff --git a/src/models/kimi-k3.cpp b/src/models/kimi-k3.cpp
index 846a48866..882c45811 100644
--- a/src/models/kimi-k3.cpp
+++ b/src/models/kimi-k3.cpp
@@ -312,10 +312,10 @@ llama_model_kimi_k3::graph::graph(const llm_graph_params & params) :
cb(cur, "ffn_norm", il);
if ((uint32_t) il < hparams.n_layer_dense_lead) {
- ggml_tensor * g = ggml_mul_mat(ctx0, layer.ffn_gate, cur);
- ggml_tensor * u = ggml_mul_mat(ctx0, layer.ffn_up, cur);
+ ggml_tensor * g = build_lora_mm(layer.ffn_gate, cur);
+ ggml_tensor * u = build_lora_mm(layer.ffn_up, cur);
cur = kimi_k3_situ(ctx0, g, u, hparams.situ_beta, hparams.situ_linear_beta);
- cur = ggml_mul_mat(ctx0, layer.ffn_down, cur);
+ cur = build_lora_mm(layer.ffn_down, cur);
cb(cur, "ffn_out", il);
} else {
cur = build_latent_moe(cur, layer, n_embd_latent, il);
@@ -342,7 +342,7 @@ llama_model_kimi_k3::graph::graph(const llm_graph_params & params) :
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = ggml_mul_mat(ctx0, model.output, cur);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
}
@@ -361,11 +361,11 @@ ggml_tensor * llama_model_kimi_k3::graph::build_kda_layer(
ggml_tensor * conv_states_all = mctx_cur->get_r_l(il);
ggml_tensor * conv_state_all = build_rs(inp_rs, conv_states_all, conv_states_all->ne[0], n_seqs);
- ggml_tensor * Qcur = build_kda_conv1d(inp_rs, conv_states_all, conv_state_all, 0, ggml_mul_mat(ctx0, layer.wq, cur),
+ ggml_tensor * Qcur = build_kda_conv1d(inp_rs, conv_states_all, conv_state_all, 0, build_lora_mm(layer.wq, cur),
layer.ssm_q_conv, d_conv, head_dim, n_head_kda, n_seq_tokens, n_seqs, il);
- ggml_tensor * Kcur = build_kda_conv1d(inp_rs, conv_states_all, conv_state_all, 1, ggml_mul_mat(ctx0, layer.wk, cur),
+ ggml_tensor * Kcur = build_kda_conv1d(inp_rs, conv_states_all, conv_state_all, 1, build_lora_mm(layer.wk, cur),
layer.ssm_k_conv, d_conv, head_dim, n_head_kda, n_seq_tokens, n_seqs, il);
- ggml_tensor * Vcur = build_kda_conv1d(inp_rs, conv_states_all, conv_state_all, 2, ggml_mul_mat(ctx0, layer.wv, cur),
+ ggml_tensor * Vcur = build_kda_conv1d(inp_rs, conv_states_all, conv_state_all, 2, build_lora_mm(layer.wv, cur),
layer.ssm_v_conv, d_conv, head_dim, n_head_kda, n_seq_tokens, n_seqs, il);
cb(Qcur, "kda_q_conv", il);
cb(Kcur, "kda_k_conv", il);
@@ -375,8 +375,8 @@ ggml_tensor * llama_model_kimi_k3::graph::build_kda_layer(
// unset (kimi-linear): g = -exp(A_log) * softplus(f_b(f_a(x)) + dt_bias)
// set (K3, -5.0): g = lower_bound * sigmoid(exp(A_log) * (f_b(f_a(x)) + dt_bias))
// ssm_a holds -exp(A_log) (folded at conversion time), so exp(A_log) == -ssm_a
- ggml_tensor * f_a = ggml_mul_mat(ctx0, layer.ssm_f_a, cur);
- ggml_tensor * g1 = ggml_mul_mat(ctx0, layer.ssm_f_b, f_a);
+ ggml_tensor * f_a = build_lora_mm(layer.ssm_f_a, cur);
+ ggml_tensor * g1 = build_lora_mm(layer.ssm_f_b, f_a);
g1 = ggml_add(ctx0, g1, layer.ssm_dt_b);
ggml_tensor * A = ggml_reshape_3d(ctx0, layer.ssm_a, 1, n_head_kda, 1);
@@ -395,7 +395,7 @@ ggml_tensor * llama_model_kimi_k3::graph::build_kda_layer(
g1 = ggml_reshape_4d(ctx0, g1, head_dim, n_head_kda, n_seq_tokens, n_seqs);
- ggml_tensor * beta = ggml_mul_mat(ctx0, layer.ssm_beta, cur);
+ ggml_tensor * beta = build_lora_mm(layer.ssm_beta, cur);
beta = ggml_reshape_4d(ctx0, beta, 1, n_head_kda, n_seq_tokens, n_seqs);
beta = ggml_sigmoid(ctx0, beta);
cb(beta, "kda_beta", il);
@@ -416,7 +416,7 @@ ggml_tensor * llama_model_kimi_k3::graph::build_kda_layer(
// K3: single full-rank gate (kimi-linear factors this as g_b(g_a(x)))
ggml_tensor * cur_2d = ggml_reshape_2d(ctx0, cur_3d, cur_3d->ne[0], n_seq_tokens * n_seqs);
- ggml_tensor * g2 = ggml_mul_mat(ctx0, layer.ssm_g, cur_2d);
+ ggml_tensor * g2 = build_lora_mm(layer.ssm_g, cur_2d);
g2 = ggml_reshape_3d(ctx0, g2, head_dim, n_head_kda, n_seq_tokens * n_seqs);
ggml_tensor * o = ggml_reshape_3d(ctx0, output, head_dim, n_head_kda, n_seq_tokens * n_seqs);
@@ -426,7 +426,7 @@ ggml_tensor * llama_model_kimi_k3::graph::build_kda_layer(
ggml_tensor * gated = ggml_mul(ctx0, normed, ggml_sigmoid(ctx0, g2));
gated = ggml_cont_2d(ctx0, gated, d_inner, n_tokens);
- cur = ggml_mul_mat(ctx0, layer.wo, gated);
+ cur = build_lora_mm(layer.wo, gated);
cb(cur, "kda_out", il);
return cur;
@@ -446,14 +446,14 @@ ggml_tensor * llama_model_kimi_k3::graph::build_mla_layer(
ggml_tensor * Qcur;
if (layer.wq_a) {
- Qcur = ggml_mul_mat(ctx0, layer.wq_a, cur);
+ Qcur = build_lora_mm(layer.wq_a, cur);
Qcur = build_norm(Qcur, layer.attn_q_a_norm, nullptr, LLM_NORM_RMS, il);
- Qcur = ggml_mul_mat(ctx0, layer.wq_b, Qcur);
+ Qcur = build_lora_mm(layer.wq_b, Qcur);
} else {
- Qcur = ggml_mul_mat(ctx0, layer.wq, cur);
+ Qcur = build_lora_mm(layer.wq, cur);
}
- ggml_tensor * kv_cmpr_pe = ggml_mul_mat(ctx0, layer.wkv_a_mqa, cur);
+ ggml_tensor * kv_cmpr_pe = build_lora_mm(layer.wkv_a_mqa, cur);
ggml_tensor * kv_cmpr = ggml_view_2d(ctx0, kv_cmpr_pe, kv_lora_rank, n_tokens,
ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope), 0);
@@ -476,7 +476,7 @@ ggml_tensor * llama_model_kimi_k3::graph::build_mla_layer(
ggml_row_size(Qcur->type, n_embd_head_qk_nope));
q_nope = ggml_permute(ctx0, q_nope, 0, 2, 1, 3);
- ggml_tensor * q_nope_absorbed = ggml_mul_mat(ctx0, layer.wk_b, q_nope);
+ ggml_tensor * q_nope_absorbed = build_lora_mm(layer.wk_b, q_nope);
q_nope_absorbed = ggml_permute(ctx0, q_nope_absorbed, 0, 2, 1, 3);
ggml_tensor * Q = ggml_concat(ctx0, q_nope_absorbed, q_pe, 0);
@@ -485,10 +485,10 @@ ggml_tensor * llama_model_kimi_k3::graph::build_mla_layer(
ggml_tensor * V = kv_cmpr_3d;
// wo == NULL: the output projection is applied after the gate below
- out = build_attn(inp_attn_k, nullptr, NULL, nullptr, Q, K, V, nullptr, nullptr, layer.wv_b, kq_scale, il);
+ out = build_attn(inp_attn_k, nullptr, NULL, Q, K, V, nullptr, nullptr, layer.wv_b, kq_scale, il);
} else {
ggml_tensor * Q = ggml_reshape_3d(ctx0, Qcur, n_embd_head_k_mla, n_head, n_tokens);
- ggml_tensor * kv = ggml_mul_mat(ctx0, layer.wkv_b, kv_cmpr);
+ ggml_tensor * kv = build_lora_mm(layer.wkv_b, kv_cmpr);
const int64_t kv_per_head = n_embd_head_qk_nope + n_embd_head_v_mla;
ggml_tensor * k_nope = ggml_view_3d(ctx0, kv, n_embd_head_qk_nope, n_head, n_tokens,
@@ -500,17 +500,17 @@ ggml_tensor * llama_model_kimi_k3::graph::build_mla_layer(
ggml_tensor * k_pe_t = ggml_new_tensor_3d(ctx0, k_pe->type, n_embd_head_qk_rope, n_head, n_tokens);
ggml_tensor * K = ggml_concat(ctx0, ggml_repeat(ctx0, k_pe, k_pe_t), k_nope, 0);
- out = build_attn(inp_attn_kv, nullptr, NULL, nullptr, Q, K, V, nullptr, nullptr, nullptr, kq_scale, il);
+ out = build_attn(inp_attn_kv, nullptr, NULL, Q, K, V, nullptr, nullptr, nullptr, kq_scale, il);
}
// K3: attn_output *= sigmoid(g_proj(x)), then o_proj
if (layer.wqkv_gate) {
- ggml_tensor * g = ggml_sigmoid(ctx0, ggml_mul_mat(ctx0, layer.wqkv_gate, inp_gate));
+ ggml_tensor * g = ggml_sigmoid(ctx0, build_lora_mm(layer.wqkv_gate, inp_gate));
out = ggml_mul(ctx0, out, g);
cb(out, "mla_gated", il);
}
- out = ggml_mul_mat(ctx0, layer.wo, out);
+ out = build_lora_mm(layer.wo, out);
cb(out, "mla_out", il);
return out;
@@ -527,12 +527,12 @@ ggml_tensor * llama_model_kimi_k3::graph::build_latent_moe(
ggml_tensor * identity = cur;
ggml_tensor * routed_in = layer.ffn_routed_down
- ? ggml_mul_mat(ctx0, layer.ffn_routed_down, cur)
+ ? build_lora_mm(layer.ffn_routed_down, cur)
: cur;
// the router scores the full-width input while the experts take the latent one,
// so the logits are computed here and passed to build_moe_ffn
- ggml_tensor * logits = ggml_mul_mat(ctx0, layer.ffn_gate_inp, identity);
+ ggml_tensor * logits = build_lora_mm(layer.ffn_gate_inp, identity);
cb(logits, "ffn_moe_logits", il);
ggml_tensor * moe_out = build_moe_ffn(routed_in,
@@ -554,15 +554,15 @@ ggml_tensor * llama_model_kimi_k3::graph::build_latent_moe(
moe_out = build_norm(moe_out, layer.ffn_routed_norm, NULL, LLM_NORM_RMS, il);
}
if (layer.ffn_routed_up) {
- moe_out = ggml_mul_mat(ctx0, layer.ffn_routed_up, moe_out);
+ moe_out = build_lora_mm(layer.ffn_routed_up, moe_out);
}
GGML_UNUSED(n_embd_latent);
if (layer.ffn_gate_shexp) {
- ggml_tensor * g = ggml_mul_mat(ctx0, layer.ffn_gate_shexp, identity);
- ggml_tensor * u = ggml_mul_mat(ctx0, layer.ffn_up_shexp, identity);
+ ggml_tensor * g = build_lora_mm(layer.ffn_gate_shexp, identity);
+ ggml_tensor * u = build_lora_mm(layer.ffn_up_shexp, identity);
ggml_tensor * sh = kimi_k3_situ(ctx0, g, u, hparams.situ_beta, hparams.situ_linear_beta);
- sh = ggml_mul_mat(ctx0, layer.ffn_down_shexp, sh);
+ sh = build_lora_mm(layer.ffn_down_shexp, sh);
cb(sh, "ffn_shexp", il);
moe_out = ggml_add(ctx0, moe_out, sh);
}
diff --git a/src/models/kimi-k31.cpp b/src/models/kimi-k31.cpp
index 049f667de..62719ba15 100644
--- a/src/models/kimi-k31.cpp
+++ b/src/models/kimi-k31.cpp
@@ -348,10 +348,10 @@ llama_model_kimi_k31::graph::graph(const llm_graph_params & params) :
cb(cur, "ffn_norm", il);
if ((uint32_t) il < hparams.n_layer_dense_lead) {
- ggml_tensor * g = ggml_mul_mat(ctx0, layer.ffn_gate, cur);
- ggml_tensor * u = ggml_mul_mat(ctx0, layer.ffn_up, cur);
+ ggml_tensor * g = build_lora_mm(layer.ffn_gate, cur);
+ ggml_tensor * u = build_lora_mm(layer.ffn_up, cur);
cur = kimi_k31_situ(ctx0, g, u, hparams.situ_beta, hparams.situ_linear_beta);
- cur = ggml_mul_mat(ctx0, layer.ffn_down, cur);
+ cur = build_lora_mm(layer.ffn_down, cur);
cb(cur, "ffn_out", il);
} else {
cur = build_latent_moe(cur, layer, n_embd_latent, il);
@@ -378,7 +378,7 @@ llama_model_kimi_k31::graph::graph(const llm_graph_params & params) :
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = ggml_mul_mat(ctx0, model.output, cur);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
}
@@ -396,7 +396,7 @@ ggml_tensor * llama_model_kimi_k31::graph::build_kda_conv1d(
const int64_t conv_state_size = (d_conv - 1) * d_inner;
const int64_t n_embd_r_total = 3 * conv_state_size;
- ggml_tensor * x_proj = ggml_mul_mat(ctx0, proj_w, x);
+ ggml_tensor * x_proj = build_lora_mm(proj_w, x);
ggml_tensor * x_3d = ggml_reshape_3d(ctx0, x_proj, d_inner, n_seq_tokens, n_seqs);
ggml_tensor * conv_x = nullptr;
@@ -444,8 +444,8 @@ ggml_tensor * llama_model_kimi_k31::graph::build_kda_layer(
// unset (kimi-linear): g = -exp(A_log) * softplus(f_b(f_a(x)) + dt_bias)
// set (K3, -5.0): g = lower_bound * sigmoid(exp(A_log) * (f_b(f_a(x)) + dt_bias))
// ssm_a holds -exp(A_log) (folded at conversion time), so exp(A_log) == -ssm_a
- ggml_tensor * f_a = ggml_mul_mat(ctx0, layer.ssm_f_a, cur);
- ggml_tensor * g1 = ggml_mul_mat(ctx0, layer.ssm_f_b, f_a);
+ ggml_tensor * f_a = build_lora_mm(layer.ssm_f_a, cur);
+ ggml_tensor * g1 = build_lora_mm(layer.ssm_f_b, f_a);
g1 = ggml_add(ctx0, g1, layer.ssm_dt_b);
ggml_tensor * A = ggml_reshape_3d(ctx0, layer.ssm_a, 1, n_head_kda, 1);
@@ -464,7 +464,7 @@ ggml_tensor * llama_model_kimi_k31::graph::build_kda_layer(
g1 = ggml_reshape_4d(ctx0, g1, head_dim, n_head_kda, n_seq_tokens, n_seqs);
- ggml_tensor * beta = ggml_mul_mat(ctx0, layer.ssm_beta, cur);
+ ggml_tensor * beta = build_lora_mm(layer.ssm_beta, cur);
beta = ggml_reshape_4d(ctx0, beta, 1, n_head_kda, n_seq_tokens, n_seqs);
beta = ggml_sigmoid(ctx0, beta);
cb(beta, "kda_beta", il);
@@ -485,7 +485,7 @@ ggml_tensor * llama_model_kimi_k31::graph::build_kda_layer(
// K3: single full-rank gate (kimi-linear factors this as g_b(g_a(x)))
ggml_tensor * cur_2d = ggml_reshape_2d(ctx0, cur_3d, cur_3d->ne[0], n_seq_tokens * n_seqs);
- ggml_tensor * g2 = ggml_mul_mat(ctx0, layer.ssm_g, cur_2d);
+ ggml_tensor * g2 = build_lora_mm(layer.ssm_g, cur_2d);
g2 = ggml_reshape_3d(ctx0, g2, head_dim, n_head_kda, n_seq_tokens * n_seqs);
ggml_tensor * o = ggml_reshape_3d(ctx0, output, head_dim, n_head_kda, n_seq_tokens * n_seqs);
@@ -495,7 +495,7 @@ ggml_tensor * llama_model_kimi_k31::graph::build_kda_layer(
ggml_tensor * gated = ggml_mul(ctx0, normed, ggml_sigmoid(ctx0, g2));
gated = ggml_cont_2d(ctx0, gated, d_inner, n_tokens);
- cur = ggml_mul_mat(ctx0, layer.wo, gated);
+ cur = build_lora_mm(layer.wo, gated);
cb(cur, "kda_out", il);
return cur;
@@ -523,11 +523,11 @@ llama_model_kimi_k31::graph::build_dsa_layer(
// --- MLA query; the indexer reads the same q latent ---
GGML_ASSERT(layer.wq_a && layer.wq_b && "K3.1 MLA is served from the q latent");
- ggml_tensor * qr = ggml_mul_mat(ctx0, layer.wq_a, cur);
+ ggml_tensor * qr = build_lora_mm(layer.wq_a, cur);
qr = build_norm(qr, layer.attn_q_a_norm, nullptr, LLM_NORM_RMS, il);
- ggml_tensor * Qcur = ggml_mul_mat(ctx0, layer.wq_b, qr);
+ ggml_tensor * Qcur = build_lora_mm(layer.wq_b, qr);
- ggml_tensor * kv_cmpr_pe = ggml_mul_mat(ctx0, layer.wkv_a_mqa, cur);
+ ggml_tensor * kv_cmpr_pe = build_lora_mm(layer.wkv_a_mqa, cur);
ggml_tensor * kv_cmpr = ggml_view_2d(ctx0, kv_cmpr_pe, kv_lora_rank, n_tokens,
ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope), 0);
@@ -551,7 +551,7 @@ llama_model_kimi_k31::graph::build_dsa_layer(
ggml_row_size(Qcur->type, n_embd_head_qk_nope));
q_nope = ggml_permute(ctx0, q_nope, 0, 2, 1, 3);
- ggml_tensor * q_nope_absorbed = ggml_mul_mat(ctx0, layer.wk_b, q_nope);
+ ggml_tensor * q_nope_absorbed = build_lora_mm(layer.wk_b, q_nope);
q_nope_absorbed = ggml_permute(ctx0, q_nope_absorbed, 0, 2, 1, 3);
ggml_tensor * Q = ggml_concat(ctx0, q_nope_absorbed, q_pe, 0);
@@ -563,10 +563,10 @@ llama_model_kimi_k31::graph::build_dsa_layer(
if (hparams.is_indexer_full(il) && layer.indexer_attn_q_b) {
GGML_ASSERT(mctx_idx != nullptr);
- ggml_tensor * iq = ggml_mul_mat(ctx0, layer.indexer_attn_q_b, qr);
+ ggml_tensor * iq = build_lora_mm(layer.indexer_attn_q_b, qr);
iq = ggml_reshape_3d(ctx0, iq, n_idx_dim, n_idx_head, n_tokens);
- ggml_tensor * ik = ggml_mul_mat(ctx0, layer.indexer_attn_k, cur);
+ ggml_tensor * ik = build_lora_mm(layer.indexer_attn_k, cur);
ik = build_norm(ik, layer.indexer_k_norm, layer.indexer_k_norm_b, LLM_NORM, il);
ik = ggml_reshape_3d(ctx0, ik, n_idx_dim, 1, n_tokens);
cb(ik, "indexer_k", il);
@@ -574,7 +574,7 @@ llama_model_kimi_k31::graph::build_dsa_layer(
// the indexer keys go to the cells of the matching attention layer
ggml_build_forward_expand(gf, mctx_idx->cpy_k(ctx0, ik, inp_attn->get_k_idxs(), il));
- ggml_tensor * iw = ggml_mul_mat(ctx0, layer.indexer_proj, cur);
+ ggml_tensor * iw = build_lora_mm(layer.indexer_proj, cur);
// pre-scale so the huge score tensor is not scaled afterwards
iw = ggml_scale(ctx0, iw, 1.0f / sqrtf(float(n_idx_dim * n_idx_head)));
@@ -624,12 +624,12 @@ llama_model_kimi_k31::graph::build_dsa_layer(
// K3.1: attn_output *= sigmoid(g_proj(x)), then o_proj
if (layer.wqkv_gate) {
- ggml_tensor * g = ggml_sigmoid(ctx0, ggml_mul_mat(ctx0, layer.wqkv_gate, inp_gate));
+ ggml_tensor * g = ggml_sigmoid(ctx0, build_lora_mm(layer.wqkv_gate, inp_gate));
out = ggml_mul(ctx0, out, g);
cb(out, "mla_gated", il);
}
- out = ggml_mul_mat(ctx0, layer.wo, out);
+ out = build_lora_mm(layer.wo, out);
cb(out, "mla_out", il);
return { out, sel };
@@ -646,12 +646,12 @@ ggml_tensor * llama_model_kimi_k31::graph::build_latent_moe(
ggml_tensor * identity = cur;
ggml_tensor * routed_in = layer.ffn_routed_down
- ? ggml_mul_mat(ctx0, layer.ffn_routed_down, cur)
+ ? build_lora_mm(layer.ffn_routed_down, cur)
: cur;
// the router scores the full-width input while the experts take the latent one,
// so the logits are computed here and passed to build_moe_ffn
- ggml_tensor * logits = ggml_mul_mat(ctx0, layer.ffn_gate_inp, identity);
+ ggml_tensor * logits = build_lora_mm(layer.ffn_gate_inp, identity);
cb(logits, "ffn_moe_logits", il);
ggml_tensor * moe_out = build_moe_ffn(routed_in,
@@ -673,15 +673,15 @@ ggml_tensor * llama_model_kimi_k31::graph::build_latent_moe(
moe_out = build_norm(moe_out, layer.ffn_routed_norm, NULL, LLM_NORM_RMS, il);
}
if (layer.ffn_routed_up) {
- moe_out = ggml_mul_mat(ctx0, layer.ffn_routed_up, moe_out);
+ moe_out = build_lora_mm(layer.ffn_routed_up, moe_out);
}
GGML_UNUSED(n_embd_latent);
if (layer.ffn_gate_shexp) {
- ggml_tensor * g = ggml_mul_mat(ctx0, layer.ffn_gate_shexp, identity);
- ggml_tensor * u = ggml_mul_mat(ctx0, layer.ffn_up_shexp, identity);
+ ggml_tensor * g = build_lora_mm(layer.ffn_gate_shexp, identity);
+ ggml_tensor * u = build_lora_mm(layer.ffn_up_shexp, identity);
ggml_tensor * sh = kimi_k31_situ(ctx0, g, u, hparams.situ_beta, hparams.situ_linear_beta);
- sh = ggml_mul_mat(ctx0, layer.ffn_down_shexp, sh);
+ sh = build_lora_mm(layer.ffn_down_shexp, sh);
cb(sh, "ffn_shexp", il);
moe_out = ggml_add(ctx0, moe_out, sh);
}
diff --git a/src/models/kimi-linear.cpp b/src/models/kimi-linear.cpp
index 2190df8c9..81b147152 100644
--- a/src/models/kimi-linear.cpp
+++ b/src/models/kimi-linear.cpp
@@ -172,7 +172,7 @@ std::unique_ptr<llm_graph_context> llama_model_kimi_linear::build_arch_graph(con
// Causal Conv1d function for Q,K,V
// When qkv is 0, it is Q, 1 is K, 2 is V
-static ggml_tensor * causal_conv1d(ggml_cgraph * gf, ggml_context * ctx0, ggml_tensor * conv_states_all, ggml_tensor * conv_state_all, int64_t qkv, ggml_tensor * x, ggml_tensor * proj_w, ggml_tensor * conv_w, int64_t d_conv, int64_t head_dim, int64_t n_head, int64_t n_seq_tokens, int64_t n_seqs, int64_t n_tokens, int64_t kv_head) {
+static ggml_tensor * causal_conv1d(ggml_cgraph * gf, ggml_context * ctx0, ggml_tensor * conv_states_all, ggml_tensor * conv_state_all, int64_t qkv, ggml_tensor * x_proj, ggml_tensor * conv_w, int64_t d_conv, int64_t head_dim, int64_t n_head, int64_t n_seq_tokens, int64_t n_seqs, int64_t n_tokens, int64_t kv_head) {
const int64_t d_inner = head_dim * n_head;
const int64_t conv_state_size = (d_conv - 1) * d_inner;
const int64_t n_embd_r_total = 3 * conv_state_size; // Q + K + V
@@ -194,9 +194,6 @@ static ggml_tensor * causal_conv1d(ggml_cgraph * gf, ggml_context * ctx0, ggml_t
// Causal Conv1d function for Q,K,V
// When qkv is 0, it is Q, 1 is K, 2 is V
- // Step 1: Q, K, V projections -> [d_inner, n_tokens]
- ggml_tensor * x_proj = proj_w ? ggml_mul_mat(ctx0, proj_w, x) : x;
-
// Reshape input: {d_inner, n_tokens} -> {d_inner, n_seq_tokens, n_seqs}
ggml_tensor * x_3d = ggml_reshape_3d(ctx0, x_proj, d_inner, n_seq_tokens, n_seqs);
@@ -295,24 +292,26 @@ llama_model_kimi_linear::graph::graph(const llm_graph_params & params) :
ggml_tensor * conv_states_all = mctx_cur->get_r_l(il);
cb(conv_states_all, "conv_states_all", il);
ggml_tensor * conv_state_all = build_rs(inp_rs, conv_states_all, hparams.n_embd_r(), n_seqs);
- ggml_tensor * q_in = cur, * k_in = cur, * v_in = cur;
- ggml_tensor * q_w = layer.wq, * k_w = layer.wk, * v_w = layer.wv;
+ ggml_tensor * q_in, * k_in, * v_in;
if (layer.wqkv) {
- ggml_tensor * qkv = ggml_mul_mat(ctx0, layer.wqkv, cur);
+ ggml_tensor * qkv = build_lora_mm(layer.wqkv, cur);
const int64_t d_inner = head_dim * n_head;
const size_t esize = ggml_element_size(qkv);
q_in = ggml_cont(ctx0, ggml_view_2d(ctx0, qkv, d_inner, n_tokens, qkv->nb[1], 0));
k_in = ggml_cont(ctx0, ggml_view_2d(ctx0, qkv, d_inner, n_tokens, qkv->nb[1], d_inner * esize));
v_in = ggml_cont(ctx0, ggml_view_2d(ctx0, qkv, d_inner, n_tokens, qkv->nb[1], 2 * d_inner * esize));
- q_w = nullptr; k_w = nullptr; v_w = nullptr;
+ } else {
+ q_in = build_lora_mm(layer.wq, cur);
+ k_in = build_lora_mm(layer.wk, cur);
+ v_in = build_lora_mm(layer.wv, cur);
}
- ggml_tensor * Qcur = causal_conv1d(gf, ctx0, conv_states_all, conv_state_all, 0, q_in, q_w, layer.ssm_q_conv, d_conv, head_dim, n_head, n_seq_tokens, n_seqs, n_tokens, kv_head);
- ggml_tensor * Kcur = causal_conv1d(gf, ctx0, conv_states_all, conv_state_all, 1, k_in, k_w, layer.ssm_k_conv, d_conv, head_dim, n_head, n_seq_tokens, n_seqs, n_tokens, kv_head);
- ggml_tensor * Vcur = causal_conv1d(gf, ctx0, conv_states_all, conv_state_all, 2, v_in, v_w, layer.ssm_v_conv, d_conv, head_dim, n_head, n_seq_tokens, n_seqs, n_tokens, kv_head);
+ ggml_tensor * Qcur = causal_conv1d(gf, ctx0, conv_states_all, conv_state_all, 0, q_in, layer.ssm_q_conv, d_conv, head_dim, n_head, n_seq_tokens, n_seqs, n_tokens, kv_head);
+ ggml_tensor * Kcur = causal_conv1d(gf, ctx0, conv_states_all, conv_state_all, 1, k_in, layer.ssm_k_conv, d_conv, head_dim, n_head, n_seq_tokens, n_seqs, n_tokens, kv_head);
+ ggml_tensor * Vcur = causal_conv1d(gf, ctx0, conv_states_all, conv_state_all, 2, v_in, layer.ssm_v_conv, d_conv, head_dim, n_head, n_seq_tokens, n_seqs, n_tokens, kv_head);
// g1 = -exp(A_log) * softplus(f_b(f_a(x)) + dt_bias)
- ggml_tensor * f_a = ggml_mul_mat(ctx0, layer.ssm_f_a, cur);
- ggml_tensor * g1 = ggml_mul_mat(ctx0, layer.ssm_f_b, f_a);
+ ggml_tensor * f_a = build_lora_mm(layer.ssm_f_a, cur);
+ ggml_tensor * g1 = build_lora_mm(layer.ssm_f_b, f_a);
cb(g1, "g1 f_b(f_a(cur))", il);
g1 = ggml_add(ctx0, g1, layer.ssm_dt_b);
g1 = ggml_softplus(ctx0, g1);
@@ -327,7 +326,7 @@ llama_model_kimi_linear::graph::graph(const llm_graph_params & params) :
g1 = ggml_reshape_4d(ctx0, g1, head_dim, n_head, n_seq_tokens, n_seqs);
// Compute beta (mixing coefficient)
- ggml_tensor * beta = ggml_mul_mat(ctx0, layer.ssm_beta, cur);
+ ggml_tensor * beta = build_lora_mm(layer.ssm_beta, cur);
beta = ggml_reshape_4d(ctx0, beta, 1, n_head, n_seq_tokens, n_seqs);
cb(beta, "kda_beta", il);
@@ -363,8 +362,8 @@ llama_model_kimi_linear::graph::graph(const llm_graph_params & params) :
// Output gating g2 = g_b(g_a(x))
ggml_tensor * cur_2d = ggml_reshape_2d(ctx0, cur, cur->ne[0], n_seq_tokens * n_seqs);
- ggml_tensor * g_a = ggml_mul_mat(ctx0, layer.ssm_g_a, cur_2d);
- ggml_tensor * g2 = ggml_mul_mat(ctx0, layer.ssm_g_b, g_a);
+ ggml_tensor * g_a = build_lora_mm(layer.ssm_g_a, cur_2d);
+ ggml_tensor * g2 = build_lora_mm(layer.ssm_g_b, g_a);
cb(g2, "g2 g_b(g_a(cur_2d))", il);
g2 = ggml_reshape_3d(ctx0, g2, head_dim, n_head, n_seq_tokens * n_seqs);
@@ -379,7 +378,7 @@ llama_model_kimi_linear::graph::graph(const llm_graph_params & params) :
// Output projection
gated = ggml_cont_2d(ctx0, gated, d_inner, n_tokens);
- cur = ggml_mul_mat(ctx0, layer.wo, gated);
+ cur = build_lora_mm(layer.wo, gated);
cb(cur, "kda_out", il);
} else {
@@ -388,11 +387,11 @@ llama_model_kimi_linear::graph::graph(const llm_graph_params & params) :
// Step 1: Q projection and reshape
// vLLM Kimi: q = q_proj(hidden_states), then view as [n_tokens, n_head, qk_head_dim]
// Note: Kimi MLA does NOT use RoPE (rotary_emb=None in vLLM)
- ggml_tensor * Qcur = ggml_mul_mat(ctx0, layer.wq, cur);
+ ggml_tensor * Qcur = build_lora_mm(layer.wq, cur);
// Step 2: KV compression
// kv_cmpr_pe = kv_a_proj_with_mqa(hidden_states) -> [kv_lora_rank + qk_rope_head_dim, n_tokens]
- ggml_tensor * kv_cmpr_pe = ggml_mul_mat(ctx0, layer.wkv_a_mqa, cur);
+ ggml_tensor * kv_cmpr_pe = build_lora_mm(layer.wkv_a_mqa, cur);
// Split: kv_cmpr = kv_lora[:kv_lora_rank], k_pe = kv_lora[kv_lora_rank:]
ggml_tensor * kv_cmpr = ggml_view_2d(ctx0, kv_cmpr_pe, kv_lora_rank, n_tokens,
@@ -424,7 +423,7 @@ llama_model_kimi_linear::graph::graph(const llm_graph_params & params) :
cb(q_nope, "q_nope_perm", il);
// {n_embd_head_qk_nope, kv_lora_rank, n_head} x {n_embd_head_qk_nope, n_tokens, n_head}
- ggml_tensor * q_nope_absorbed = ggml_mul_mat(ctx0, layer.wk_b, q_nope);
+ ggml_tensor * q_nope_absorbed = build_lora_mm(layer.wk_b, q_nope);
cb(q_nope_absorbed, "q_nope_absorbed", il);
// {kv_lora_rank, n_head, n_tokens}
@@ -447,13 +446,13 @@ llama_model_kimi_linear::graph::graph(const llm_graph_params & params) :
ggml_tensor * Vcur = kv_cmpr;
cb(Vcur, "Vcur", il);
- cur = build_attn(inp_attn_k, layer.wo, NULL, layer.wo_s, Qcur, Kcur, Vcur, nullptr, nullptr, layer.wv_b, kq_scale_mla, il);
+ cur = build_attn(inp_attn_k, layer.wo, NULL, Qcur, Kcur, Vcur, nullptr, nullptr, layer.wv_b, kq_scale_mla, il);
cb(cur, "mla_out", il);
} else { // MLA KV cache disabled. Fall back to MHA KV cache.
Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head_k_mla, n_head, n_tokens);
cb(Qcur, "mla_Q", il);
// KV decompression: kv = kv_b_proj(kv_c_normed)
- ggml_tensor * kv = ggml_mul_mat(ctx0, layer.wkv_b, kv_cmpr);
+ ggml_tensor * kv = build_lora_mm(layer.wkv_b, kv_cmpr);
const int64_t kv_per_head = n_embd_head_qk_nope + n_embd_head_v_mla;
// Split kv into k_nope and v
@@ -478,7 +477,7 @@ llama_model_kimi_linear::graph::graph(const llm_graph_params & params) :
// Direct softmax attention (with MHA KV cache)
// Use build_attn with inp_attn for proper mask handling
- cur = build_attn(inp_attn_kv, layer.wo, NULL, layer.wo_s, Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale_mla, il);
+ cur = build_attn(inp_attn_kv, layer.wo, NULL, Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale_mla, il);
cb(cur, "mla_out", il);
}
}
@@ -500,9 +499,9 @@ llama_model_kimi_linear::graph::graph(const llm_graph_params & params) :
if ((uint32_t) il < hparams.n_layer_dense_lead) {
// Dense FFN layer
cur = build_ffn(cur,
- layer.ffn_up, NULL, NULL,
- layer.ffn_gate, NULL, NULL,
- layer.ffn_down, NULL, NULL,
+ layer.ffn_up, NULL,
+ layer.ffn_gate, NULL,
+ layer.ffn_down, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
} else {
@@ -525,9 +524,9 @@ llama_model_kimi_linear::graph::graph(const llm_graph_params & params) :
// Shared expert
{
ggml_tensor * ffn_shexp = build_ffn(cur,
- layer.ffn_up_shexp, NULL, NULL,
- layer.ffn_gate_shexp, NULL, NULL,
- layer.ffn_down_shexp, NULL, NULL,
+ layer.ffn_up_shexp, NULL,
+ layer.ffn_gate_shexp, NULL,
+ layer.ffn_down_shexp, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "ffn_shexp", il);
@@ -553,7 +552,7 @@ llama_model_kimi_linear::graph::graph(const llm_graph_params & params) :
res->t_embd = cur;
// Output
- cur = ggml_mul_mat(ctx0, model.output, cur);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
}
diff --git a/src/models/kokoro.cpp b/src/models/kokoro.cpp
index c236134af..77f230a83 100644
--- a/src/models/kokoro.cpp
+++ b/src/models/kokoro.cpp
@@ -83,7 +83,7 @@ llama_model_kokoro::graph::graph(const llm_graph_params & params) : llm_graph_co
inpL = build_norm(inpL, model.tok_norm, model.tok_norm_b, LLM_NORM, 0);
cb(inpL, "inp_norm", 0);
- inpL = ggml_add(ctx0, ggml_mul_mat(ctx0, model.tok_embd_proj, inpL), model.tok_embd_proj_b);
+ inpL = ggml_add(ctx0, build_lora_mm(model.tok_embd_proj, inpL), model.tok_embd_proj_b);
cb(inpL, "inp_proj", 0);
auto * inp_attn = build_attn_inp_no_cache();
@@ -104,7 +104,7 @@ llama_model_kokoro::graph::graph(const llm_graph_params & params) : llm_graph_co
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- layer.wo, layer.wo_b, layer.wo_s,
+ layer.wo, layer.wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
cb(cur, "kqv_out", il);
}
@@ -121,9 +121,9 @@ llama_model_kokoro::graph::graph(const llm_graph_params & params) : llm_graph_co
cb(ffn_inp, "ffn_inp", il);
cur = build_ffn(cur,
- layer.ffn_up, layer.ffn_up_b, NULL,
- NULL, NULL, NULL,
- layer.ffn_down, layer.ffn_down_b, NULL, NULL,
+ layer.ffn_up, layer.ffn_up_b,
+ NULL, NULL,
+ layer.ffn_down, layer.ffn_down_b, NULL,
LLM_FFN_GELU, LLM_FFN_SEQ, il);
cb(cur, "ffn_out", il);
diff --git a/src/models/kolibri1.cpp b/src/models/kolibri1.cpp
index 64182d186..ebaf039bc 100644
--- a/src/models/kolibri1.cpp
+++ b/src/models/kolibri1.cpp
@@ -103,7 +103,7 @@ llama_model_kolibri1::graph::graph(const llm_graph_params & params)
cb(Kcur, "Kcur_rope", il);
cur = build_attn(inp_attn,
- layer.wo, NULL, layer.wo_s,
+ layer.wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "attn_out", il);
@@ -146,9 +146,9 @@ llama_model_kolibri1::graph::graph(const llm_graph_params & params)
cb(moe_out, "ffn_moe_out", il);
ggml_tensor * shared = build_ffn(cur,
- layer.ffn_up_shexp, nullptr, nullptr,
- layer.ffn_gate_shexp, nullptr, nullptr,
- layer.ffn_down_shexp, nullptr, nullptr, nullptr,
+ layer.ffn_up_shexp, nullptr,
+ layer.ffn_gate_shexp, nullptr,
+ layer.ffn_down_shexp, nullptr, nullptr,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(shared, "ffn_shexp_out", il);
@@ -164,7 +164,7 @@ llama_model_kolibri1::graph::graph(const llm_graph_params & params)
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
}
diff --git a/src/models/laguna.cpp b/src/models/laguna.cpp
index 2b179e8c3..177b31451 100644
--- a/src/models/laguna.cpp
+++ b/src/models/laguna.cpp
@@ -222,10 +222,10 @@ llama_model_laguna::graph::graph(const llm_graph_params & params) : llm_graph_co
cur = has_swa
? build_attn(inp_attn_iswa,
- NULL, NULL, NULL, // o_proj deferred until after gating
+ NULL, NULL, // o_proj deferred until after gating
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il)
: build_attn(inp_attn_kv,
- NULL, NULL, NULL,
+ NULL, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "attn_out", il);
@@ -251,7 +251,7 @@ llama_model_laguna::graph::graph(const llm_graph_params & params) : llm_graph_co
}
cb(cur, "attn_gated", il);
- cur = build_lora_mm(model.layers[il].wo, cur, model.layers[il].wo_s);
+ cur = build_lora_mm(model.layers[il].wo, cur);
cb(cur, "attn_o_proj", il);
}
@@ -286,9 +286,9 @@ llama_model_laguna::graph::graph(const llm_graph_params & params) : llm_graph_co
// Always-on shared expert, summed in parallel.
ggml_tensor * ffn_shexp = build_ffn(cur,
- model.layers[il].ffn_up_shexp, NULL, NULL,
- model.layers[il].ffn_gate_shexp, NULL, NULL,
- model.layers[il].ffn_down_shexp, NULL, NULL,
+ model.layers[il].ffn_up_shexp, NULL,
+ model.layers[il].ffn_gate_shexp, NULL,
+ model.layers[il].ffn_down_shexp, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "ffn_shexp", il);
@@ -298,9 +298,9 @@ llama_model_laguna::graph::graph(const llm_graph_params & params) : llm_graph_co
} else {
// Dense FFN for the leading n_layer_dense_lead layers (XS.2: 1, M.1: 3)
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
diff --git a/src/models/lfm-base.cpp b/src/models/lfm-base.cpp
index 03e59bd09..03bf1c063 100644
--- a/src/models/lfm-base.cpp
+++ b/src/models/lfm-base.cpp
@@ -98,9 +98,9 @@ ggml_tensor * llm_build_lfm_base::build_lfm_dense_ffn(ggml_tensor * cur,
GGML_ASSERT(!model.layers[il].ffn_gate_b);
GGML_ASSERT(!model.layers[il].ffn_down_b);
return build_ffn(cur,
- model.layers[il].ffn_up, NULL, model.layers[il].ffn_up_s,
- model.layers[il].ffn_gate, NULL, model.layers[il].ffn_gate_s,
- model.layers[il].ffn_down, NULL, model.layers[il].ffn_down_s,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
}
diff --git a/src/models/lfm2.cpp b/src/models/lfm2.cpp
index 86b948cf3..3e6e8c1a0 100644
--- a/src/models/lfm2.cpp
+++ b/src/models/lfm2.cpp
@@ -184,7 +184,7 @@ llama_model_lfm2::graph<iswa>::graph(const llm_graph_params & params) :
k = build_rope(k, inp_pos, il);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
q, k, v, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
cb(cur, "model.layers.{}.self_attn.out_proj", il);
@@ -248,7 +248,7 @@ llama_model_lfm2::graph<iswa>::graph(const llm_graph_params & params) :
res->t_embd = cur;
if (!cparams.embeddings) {
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
@@ -396,7 +396,7 @@ llama_model_lfm2::graph_decision::graph_decision(const llm_graph_params & params
k = build_rope(k, inp_pos, il);
cur = build_attn(inp_attn[0],
- layer.wo, NULL, layer.wo_s,
+ layer.wo, NULL,
q, k, v, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
cb(cur, "model.layers.{}.self_attn.out_proj", il);
}
@@ -405,9 +405,9 @@ llama_model_lfm2::graph_decision::graph_decision(const llm_graph_params & params
ggml_tensor * ffn_out = build_norm(cur, layer.ffn_norm, NULL, LLM_NORM_RMS, il);
ffn_out = build_ffn(ffn_out,
- layer.ffn_up, NULL, layer.ffn_up_s,
- layer.ffn_gate, NULL, layer.ffn_gate_s,
- layer.ffn_down, NULL, layer.ffn_down_s,
+ layer.ffn_up, NULL,
+ layer.ffn_gate, NULL,
+ layer.ffn_down, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cur = ggml_add(ctx0, cur, ffn_out);
@@ -448,7 +448,7 @@ ggml_tensor * llama_model_lfm2::graph_decision::build_decision_head(
auto [Qcur, Kcur, Vcur] = build_qkv(layer, cur, n_embd_head, hparams.n_head(il), hparams.n_head_kv(il), il);
cur = build_attn(inp_attn,
- layer.wo, layer.wo_b, layer.wo_s,
+ layer.wo, layer.wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
cb(cur, "kqv_out", il);
@@ -464,9 +464,9 @@ ggml_tensor * llama_model_lfm2::graph_decision::build_decision_head(
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- layer.ffn_up, layer.ffn_up_b, NULL,
- NULL, NULL, NULL,
- layer.ffn_down, layer.ffn_down_b, NULL,
+ layer.ffn_up, layer.ffn_up_b,
+ NULL, NULL,
+ layer.ffn_down, layer.ffn_down_b,
NULL,
LLM_FFN_RELU,
LLM_FFN_SEQ, il);
diff --git a/src/models/lfm3.cpp b/src/models/lfm3.cpp
index a820d9b97..273e16094 100644
--- a/src/models/lfm3.cpp
+++ b/src/models/lfm3.cpp
@@ -117,11 +117,11 @@ llama_model_lfm3::graph<decision>::graph(const llm_graph_params & params) :
k = build_rope(k, inp_pos, il);
ggml_tensor * attn_out = build_attn(inp_attn,
- NULL, NULL, NULL,
+ NULL, NULL,
q, k, v, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
cb(attn_out, "attn_out", il);
- ggml_tensor * gate = build_lora_mm(model.layers[il].wqkv_gate, cur_in, model.layers[il].wqkv_gate_s);
+ ggml_tensor * gate = build_lora_mm(model.layers[il].wqkv_gate, cur_in);
gate = ggml_sigmoid(ctx0, gate);
cb(gate, "model.layers.{}.self_attn.gate", il);
@@ -130,7 +130,7 @@ llama_model_lfm3::graph<decision>::graph(const llm_graph_params & params) :
attn_3d = ggml_mul(ctx0, attn_3d, gate_3d);
attn_out = ggml_reshape_2d(ctx0, attn_3d, n_embd_head * n_head_il, n_tokens);
- ggml_tensor * out = build_lora_mm(model.layers[il].wo, attn_out, model.layers[il].wo_s);
+ ggml_tensor * out = build_lora_mm(model.layers[il].wo, attn_out);
cb(out, "model.layers.{}.self_attn.out_proj", il);
return out;
@@ -201,7 +201,7 @@ llama_model_lfm3::graph<decision>::graph(const llm_graph_params & params) :
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/lilicorr.cpp b/src/models/lilicorr.cpp
index 0c5527a65..a60780d6a 100644
--- a/src/models/lilicorr.cpp
+++ b/src/models/lilicorr.cpp
@@ -32,11 +32,9 @@ void llama_model_dflash::load_tensors_lilicorr() {
if (hidden != n_embd) {
lilicorr_token_proj = create_tensor(tn(LLM_TENSOR_LILICORR_TOKEN_PROJ, "weight"), { n_embd, hidden }, 0);
lilicorr_token_proj_b = create_tensor(tn(LLM_TENSOR_LILICORR_TOKEN_PROJ, "bias"), { hidden }, 0);
- lilicorr_token_proj_s = create_tensor(tn(LLM_TENSOR_LILICORR_TOKEN_PROJ, "scale"), { 1 }, TENSOR_NOT_REQUIRED);
}
lilicorr_pass_hidden_proj = create_tensor(tn(LLM_TENSOR_LILICORR_PASS_HIDDEN_PROJ, "weight"), { hparams.n_embd, hidden }, 0);
lilicorr_pass_hidden_proj_b = create_tensor(tn(LLM_TENSOR_LILICORR_PASS_HIDDEN_PROJ, "bias"), { hidden }, 0);
- lilicorr_pass_hidden_proj_s = create_tensor(tn(LLM_TENSOR_LILICORR_PASS_HIDDEN_PROJ, "scale"), { 1 }, TENSOR_NOT_REQUIRED);
lilicorr_feature_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", feature_bid), { 5 }, 0);
lilicorr_feature_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", feature_bid), { 5 }, 0);
lilicorr_feature_mlp_1 = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", feature_bid), { 5, hidden }, 0);
@@ -48,7 +46,6 @@ void llama_model_dflash::load_tensors_lilicorr() {
lilicorr_attention_bias = create_tensor(tn(LLM_TENSOR_LILICORR_ATTN_BIAS, "bias"), { slots * top_k, slots * top_k, heads }, 0);
lilicorr_context_proj = create_tensor(tn(LLM_TENSOR_LILICORR_CONTEXT_PROJ, "weight"), { n_embd, hidden }, 0);
lilicorr_context_proj_b = create_tensor(tn(LLM_TENSOR_LILICORR_CONTEXT_PROJ, "bias"), { hidden }, 0);
- lilicorr_context_proj_s = create_tensor(tn(LLM_TENSOR_LILICORR_CONTEXT_PROJ, "scale"), { 1 }, TENSOR_NOT_REQUIRED);
for (uint32_t i = 0; i < hparams.lilicorr_layers; ++i) {
const int bid = n_layer_all + i;
layers[i].lilicorr_attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", bid), { hidden }, 0);
@@ -57,13 +54,10 @@ void llama_model_dflash::load_tensors_lilicorr() {
layers[i].lilicorr_attn_in_proj_b = create_tensor(tn(LLM_TENSOR_LILICORR_ATTN_IN_PROJ, "bias", i), { 3 * hidden }, 0);
layers[i].lilicorr_attn_out_proj = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", bid), { hidden, hidden }, 0);
layers[i].lilicorr_attn_out_proj_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", bid), { hidden }, 0);
- layers[i].lilicorr_attn_out_proj_s = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "scale", bid), { 1 }, TENSOR_NOT_REQUIRED);
layers[i].lilicorr_mlp_0 = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", bid), { hidden, mlp }, 0);
layers[i].lilicorr_mlp_0_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", bid), { mlp }, 0);
- layers[i].lilicorr_mlp_0_s = create_tensor(tn(LLM_TENSOR_FFN_UP, "scale", bid), { 1 }, TENSOR_NOT_REQUIRED);
layers[i].lilicorr_mlp_2 = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", bid), { mlp, hidden }, 0);
layers[i].lilicorr_mlp_2_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", bid), { hidden }, 0);
- layers[i].lilicorr_mlp_2_s = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "scale", bid), { 1 }, TENSOR_NOT_REQUIRED);
}
lilicorr_output_norm = create_tensor(tn(LLM_TENSOR_LILICORR_OUTPUT_NORM, "weight"), { hidden }, 0);
lilicorr_anchor_norm = create_tensor(tn(LLM_TENSOR_LILICORR_ANCHOR_NORM, "weight"), { hidden }, 0);
@@ -141,14 +135,14 @@ static void build_lilicorr_scores(llm_graph_context & g, const llama_model & mod
feature = ggml_reshape_2d(ctx0, feature, 5, nodes * n_blocks);
ggml_tensor * embeds = ggml_get_rows(ctx0, tok_embd, ggml_reshape_1d(ctx0, cand_run, nodes * n_blocks));
- ggml_tensor * token_states = n_embd == hidden ? embeds : ggml_add(ctx0, g.build_lora_mm(model.lilicorr_token_proj, embeds, model.lilicorr_token_proj_s), model.lilicorr_token_proj_b);
+ ggml_tensor * token_states = n_embd == hidden ? embeds : ggml_add(ctx0, g.build_lora_mm(model.lilicorr_token_proj, embeds), model.lilicorr_token_proj_b);
ggml_tensor * pass_blk = ggml_reshape_3d(ctx0, res->t_embd, n_embd, block_size, n_blocks);
ggml_tensor * passes = ggml_reshape_2d(ctx0, ggml_cont(ctx0, ggml_view_3d(ctx0, pass_blk, n_embd, slots, n_blocks,
pass_blk->nb[1], pass_blk->nb[2], pass_blk->nb[1])),
n_embd, slots * n_blocks);
- ggml_tensor * pass_states = ggml_add(ctx0, g.build_lora_mm(model.lilicorr_pass_hidden_proj, passes, model.lilicorr_pass_hidden_proj_s), model.lilicorr_pass_hidden_proj_b);
+ ggml_tensor * pass_states = ggml_add(ctx0, g.build_lora_mm(model.lilicorr_pass_hidden_proj, passes), model.lilicorr_pass_hidden_proj_b);
pass_states = ggml_reshape_4d(ctx0, pass_states, hidden, 1, slots, n_blocks);
pass_states = ggml_reshape_2d(ctx0, ggml_repeat_4d(ctx0, pass_states, hidden, top_k, slots, n_blocks), hidden, nodes * n_blocks);
@@ -169,7 +163,7 @@ static void build_lilicorr_scores(llm_graph_context & g, const llama_model & mod
ggml_tensor * anchor = ggml_reshape_2d(ctx0,
ggml_cont(ctx0, ggml_view_3d(ctx0, anchor_blk, n_embd, 1, n_blocks,
anchor_blk->nb[1], anchor_blk->nb[2], 0)), n_embd, n_blocks);
- ggml_tensor * anchor_state = ggml_add(ctx0, g.build_lora_mm(model.lilicorr_context_proj, anchor, model.lilicorr_context_proj_s), model.lilicorr_context_proj_b);
+ ggml_tensor * anchor_state = ggml_add(ctx0, g.build_lora_mm(model.lilicorr_context_proj, anchor), model.lilicorr_context_proj_b);
const int64_t head_dim = hidden / heads;
for (uint32_t layer = 0; layer < hparams.lilicorr_layers; ++layer) {
@@ -188,11 +182,11 @@ static void build_lilicorr_scores(llm_graph_context & g, const llama_model & mod
ggml_tensor * joined = ggml_out_prod(ctx0, v, ggml_transpose(ctx0, attn));
joined = ggml_permute(ctx0, joined, 0, 2, 1, 3);
ggml_tensor * attn_output = ggml_cont_2d(ctx0, joined, hidden, nodes * n_blocks);
- attn_output = ggml_add(ctx0, g.build_lora_mm(weights.lilicorr_attn_out_proj, attn_output, weights.lilicorr_attn_out_proj_s), weights.lilicorr_attn_out_proj_b);
+ attn_output = ggml_add(ctx0, g.build_lora_mm(weights.lilicorr_attn_out_proj, attn_output), weights.lilicorr_attn_out_proj_b);
x = ggml_add(ctx0, x, attn_output);
ggml_tensor * mlp = ggml_mul(ctx0, ggml_rms_norm(ctx0, x, hparams.f_norm_rms_eps), weights.lilicorr_mlp_norm);
- mlp = ggml_silu(ctx0, ggml_add(ctx0, g.build_lora_mm(weights.lilicorr_mlp_0, mlp, weights.lilicorr_mlp_0_s), weights.lilicorr_mlp_0_b));
- mlp = ggml_add(ctx0, g.build_lora_mm(weights.lilicorr_mlp_2, mlp, weights.lilicorr_mlp_2_s), weights.lilicorr_mlp_2_b);
+ mlp = ggml_silu(ctx0, ggml_add(ctx0, g.build_lora_mm(weights.lilicorr_mlp_0, mlp), weights.lilicorr_mlp_0_b));
+ mlp = ggml_add(ctx0, g.build_lora_mm(weights.lilicorr_mlp_2, mlp), weights.lilicorr_mlp_2_b);
x = ggml_add(ctx0, x, mlp);
}
diff --git a/src/models/limite.cpp b/src/models/limite.cpp
index 887bfef63..38107cf52 100644
--- a/src/models/limite.cpp
+++ b/src/models/limite.cpp
@@ -183,7 +183,7 @@ llama_model_limite::graph::graph(const llm_graph_params & params) : llm_graph_co
cb(Kcur, "Kcur", il);
cur = build_attn(inp_attn,
- nullptr, nullptr, nullptr,
+ nullptr, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, hparams.f_attention_scale, il);
cb(cur, "attn_raw", il);
@@ -212,7 +212,7 @@ llama_model_limite::graph::graph(const llm_graph_params & params) : llm_graph_co
}
cur = ggml_reshape_2d(ctx0, cur, n_embd_head*n_head, n_tokens);
- cur = build_lora_mm(layer.wo, cur, layer.wo_s);
+ cur = build_lora_mm(layer.wo, cur);
cb(cur, "attn_out", il);
}
@@ -230,9 +230,9 @@ llama_model_limite::graph::graph(const llm_graph_params & params) : llm_graph_co
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- layer.ffn_up, nullptr, nullptr,
- layer.ffn_gate, nullptr, nullptr,
- layer.ffn_down, nullptr, nullptr,
+ layer.ffn_up, nullptr,
+ layer.ffn_gate, nullptr,
+ layer.ffn_down, nullptr,
nullptr,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -250,7 +250,7 @@ llama_model_limite::graph::graph(const llm_graph_params & params) : llm_graph_co
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
// scale * sigmoid((x + shift) / temperature)
const auto & cap = hparams.final_logit_sigmoid_capping;
diff --git a/src/models/llada-moe.cpp b/src/models/llada-moe.cpp
index 0086d52af..cc4e9b972 100644
--- a/src/models/llada-moe.cpp
+++ b/src/models/llada-moe.cpp
@@ -98,7 +98,7 @@ llama_model_llada_moe::graph::graph(const llm_graph_params & params) : llm_graph
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -145,7 +145,7 @@ llama_model_llada_moe::graph::graph(const llm_graph_params & params) : llm_graph
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/llada.cpp b/src/models/llada.cpp
index 8777cff64..ef540aaef 100644
--- a/src/models/llada.cpp
+++ b/src/models/llada.cpp
@@ -112,7 +112,7 @@ llama_model_llada::graph::graph(const llm_graph_params & params) : llm_graph_con
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -127,9 +127,9 @@ llama_model_llada::graph::graph(const llm_graph_params & params) : llm_graph_con
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -149,7 +149,7 @@ llama_model_llada::graph::graph(const llm_graph_params & params) : llm_graph_con
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/llama.cpp b/src/models/llama.cpp
index 3925699f4..8e1dbed90 100644
--- a/src/models/llama.cpp
+++ b/src/models/llama.cpp
@@ -155,7 +155,7 @@ llama_model_llama::graph<embed>::graph(const llm_graph_params & params) : llm_gr
cb(Kcur, "Kcur_normed", il);
}
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "attn_out", il);
}
@@ -175,9 +175,9 @@ llama_model_llama::graph<embed>::graph(const llm_graph_params & params) : llm_gr
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, model.layers[il].ffn_up_b, model.layers[il].ffn_up_s,
- model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, model.layers[il].ffn_gate_s,
- model.layers[il].ffn_down, model.layers[il].ffn_down_b, model.layers[il].ffn_down_s,
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b,
+ model.layers[il].ffn_gate, model.layers[il].ffn_gate_b,
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -221,7 +221,7 @@ llama_model_llama::graph<embed>::graph(const llm_graph_params & params) : llm_gr
if constexpr (!embed) {
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/llama4.cpp b/src/models/llama4.cpp
index 5fa2a623f..1ba9a6d89 100644
--- a/src/models/llama4.cpp
+++ b/src/models/llama4.cpp
@@ -175,7 +175,7 @@ llama_model_llama4::graph<iswa>::graph(const llm_graph_params & params) : llm_gr
cb(Kcur, "Kcur_normed", il);
}
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "attn_out", il);
}
@@ -194,9 +194,9 @@ llama_model_llama4::graph<iswa>::graph(const llm_graph_params & params) : llm_gr
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
- model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
- model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b,
+ model.layers[il].ffn_gate, model.layers[il].ffn_gate_b,
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -220,9 +220,9 @@ llama_model_llama4::graph<iswa>::graph(const llm_graph_params & params) : llm_gr
// Shared experts
ggml_tensor * shexp_out = build_ffn(ffn_inp_normed,
- model.layers[il].ffn_up_shexp, NULL, NULL,
- model.layers[il].ffn_gate_shexp, NULL, NULL,
- model.layers[il].ffn_down_shexp, NULL, NULL,
+ model.layers[il].ffn_up_shexp, NULL,
+ model.layers[il].ffn_gate_shexp, NULL,
+ model.layers[il].ffn_down_shexp, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(shexp_out, "ffn_moe_shexp", il);
@@ -249,7 +249,7 @@ llama_model_llama4::graph<iswa>::graph(const llm_graph_params & params) : llm_gr
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/maincoder.cpp b/src/models/maincoder.cpp
index 1a2d2c695..b88ecaa4c 100644
--- a/src/models/maincoder.cpp
+++ b/src/models/maincoder.cpp
@@ -99,7 +99,7 @@ llama_model_maincoder::graph::graph(const llm_graph_params & params) : llm_graph
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -116,9 +116,9 @@ llama_model_maincoder::graph::graph(const llm_graph_params & params) : llm_graph
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -141,7 +141,7 @@ llama_model_maincoder::graph::graph(const llm_graph_params & params) : llm_graph
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/maion-coder.cpp b/src/models/maion-coder.cpp
index 5c1e376c5..198600701 100644
--- a/src/models/maion-coder.cpp
+++ b/src/models/maion-coder.cpp
@@ -97,7 +97,7 @@ llama_model_maion_coder::graph::graph(const llm_graph_params & params) : llm_gra
Qcur = build_rope(Qcur, inp_pos, il);
Kcur = build_rope(Kcur, inp_pos, il);
- cur = build_attn(inp_attn, model.layers[il].wo, NULL, model.layers[il].wo_s, Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
+ cur = build_attn(inp_attn, model.layers[il].wo, NULL, Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
if (il == n_layer - 1 && inp_out_ids) {
cur = ggml_get_rows(ctx0, cur, inp_out_ids);
@@ -110,9 +110,9 @@ llama_model_maion_coder::graph::graph(const llm_graph_params & params) : llm_gra
if (model.layers[il].ffn_gate_inp == nullptr) {
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
} else {
ggml_tensor * router_logits = build_lora_mm(model.layers[il].ffn_gate_inp, cur);
@@ -139,7 +139,7 @@ llama_model_maion_coder::graph::graph(const llm_graph_params & params) : llm_gra
ggml_tensor * cur = build_norm(inpL, model.output_norm, NULL, LLM_NORM_RMS, -1);
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
}
diff --git a/src/models/mamba-base.cpp b/src/models/mamba-base.cpp
index 4e16f18aa..11ec79c76 100644
--- a/src/models/mamba-base.cpp
+++ b/src/models/mamba-base.cpp
@@ -43,7 +43,7 @@ ggml_tensor * llm_build_mamba_base::build_mamba_layer(llm_graph_input_rs * inp,
cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], n_seq_tokens, n_seqs);
// {n_embd, 2*d_inner} @ {n_embd, n_seq_tokens, n_seqs} => {2*d_inner, n_seq_tokens, n_seqs}
- ggml_tensor * xz = build_lora_mm(layer.ssm_in, cur, layer.ssm_in_s);
+ ggml_tensor * xz = build_lora_mm(layer.ssm_in, cur);
// split the above in two
// => {d_inner, n_seq_tokens, n_seqs}
ggml_tensor * x = ggml_view_3d(ctx0, xz, d_inner, xz->ne[1], xz->ne[2], xz->nb[1], xz->nb[2], 0);
@@ -140,7 +140,7 @@ ggml_tensor * llm_build_mamba_base::build_mamba_layer(llm_graph_input_rs * inp,
y = ggml_swiglu_split(ctx0, ggml_cont(ctx0, z), y);
// {d_inner, n_embd} @ {d_inner, n_seq_tokens, n_seqs} => {n_embd, n_seq_tokens, n_seqs}
- cur = build_lora_mm(layer.ssm_out, y, layer.ssm_out_s);
+ cur = build_lora_mm(layer.ssm_out, y);
}
// {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens}
@@ -187,7 +187,7 @@ ggml_tensor * llm_build_mamba_base::build_mamba2_layer(llm_graph_input_rs * inp,
// Keep the projection 2D: with a {n_embd, 1, n_seqs} batch the CUDA backend
// dispatches a column-batched GEMV for what is a large dense GEMM.
// {n_embd, d_in_proj} @ {n_embd, n_tokens} => {d_in_proj, n_tokens}
- ggml_tensor * zxBCdt = build_lora_mm(model.layers[il].ssm_in, cur, model.layers[il].ssm_in_s);
+ ggml_tensor * zxBCdt = build_lora_mm(model.layers[il].ssm_in, cur);
// {d_in_proj, n_tokens} => {d_in_proj, n_seq_tokens, n_seqs}
zxBCdt = ggml_reshape_3d(ctx0, zxBCdt, zxBCdt->ne[0], n_seq_tokens, n_seqs);
@@ -286,7 +286,7 @@ ggml_tensor * llm_build_mamba_base::build_mamba2_layer(llm_graph_input_rs * inp,
y = ggml_reshape_2d(ctx0, y, d_inner, n_seq_tokens * n_seqs);
// {d_inner, n_embd} @ {d_inner, n_tokens} => {n_embd, n_tokens}
- cur = build_lora_mm(model.layers[il].ssm_out, y, model.layers[il].ssm_out_s);
+ cur = build_lora_mm(model.layers[il].ssm_out, y);
}
cb(cur, "mamba_out", il);
diff --git a/src/models/mamba.cpp b/src/models/mamba.cpp
index 13a9de6b0..c3d724438 100644
--- a/src/models/mamba.cpp
+++ b/src/models/mamba.cpp
@@ -128,7 +128,7 @@ llama_model_mamba::graph::graph(const llm_graph_params & params) : llm_build_mam
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/maple.cpp b/src/models/maple.cpp
index eb0b3b4ac..202bb9d54 100644
--- a/src/models/maple.cpp
+++ b/src/models/maple.cpp
@@ -95,7 +95,7 @@ llama_model_maple::graph::graph(const llm_graph_params & params) :
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, nullptr, model.layers[il].wo_s,
+ model.layers[il].wo, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
cb(cur, "attn_out", il);
}
@@ -135,7 +135,7 @@ llama_model_maple::graph::graph(const llm_graph_params & params) :
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
}
diff --git a/src/models/mellum.cpp b/src/models/mellum.cpp
index d0b3609b1..a31dbfb04 100644
--- a/src/models/mellum.cpp
+++ b/src/models/mellum.cpp
@@ -172,7 +172,7 @@ llama_model_mellum::graph<iswa>::graph(const llm_graph_params & params) : llm_gr
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && crop_before_nextn(inp_out_ids)) {
@@ -230,7 +230,7 @@ llama_model_mellum::graph<iswa>::graph(const llm_graph_params & params) : llm_gr
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
@@ -300,7 +300,7 @@ llama_model_mellum::graph_mtp<iswa>::graph_mtp(const llm_graph_params & params)
ggml_tensor * concat = ggml_concat(ctx0, e_norm, h_norm, 0);
cb(concat, "mtp_concat", il);
- ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat, layer.nextn.eh_proj_s);
+ ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat);
cb(cur, "mtp_eh_proj", il);
ggml_tensor * inpSA = cur;
@@ -326,7 +326,7 @@ llama_model_mellum::graph_mtp<iswa>::graph_mtp(const llm_graph_params & params)
cb(Vcur, "mtp_Vcur", il);
cur = build_attn(inp_attn,
- layer.wo, layer.wo_b, layer.wo_s,
+ layer.wo, layer.wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
cb(cur, "mtp_attn_out", il);
}
@@ -361,7 +361,7 @@ llama_model_mellum::graph_mtp<iswa>::graph_mtp(const llm_graph_params & params)
cur = ggml_get_rows(ctx0, cur, inp_out_ids);
cb(cur, "mtp_shared_head_norm", -1);
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/metis16.cpp b/src/models/metis16.cpp
index 0a57a6ab0..443daf116 100644
--- a/src/models/metis16.cpp
+++ b/src/models/metis16.cpp
@@ -182,10 +182,10 @@ ggml_tensor * llama_model_metis16::graph::hc_read(ggml_tensor * x, ggml_tensor *
ggml_tensor * x2 = ggml_reshape_2d(ctx0, x, n_s, x->ne[1]*x->ne[2]);
ggml_tensor * m = ggml_view_2d(ctx0, mix, n_s, n_s, mix->nb[1], p*mix->nb[2]);
- *mixed = ggml_mul_mat(ctx0, m, x2);
+ *mixed = build_lora_mm(m, x2);
ggml_tensor * r = ggml_reshape_2d(ctx0, ggml_view_1d(ctx0, read, n_s, p*read->nb[1]), n_s, 1);
- ggml_tensor * src = ggml_reshape_2d(ctx0, ggml_mul_mat(ctx0, r, x2), x->ne[1], x->ne[2]);
+ ggml_tensor * src = ggml_reshape_2d(ctx0, build_lora_mm(r, x2), x->ne[1], x->ne[2]);
return build_norm(src, norm, NULL, LLM_NORM_RMS, il);
}
@@ -193,7 +193,7 @@ ggml_tensor * llama_model_metis16::graph::hc_write(ggml_tensor * mixed, ggml_ten
const int64_t n_s = mixed->ne[0];
ggml_tensor * w = ggml_reshape_2d(ctx0, ggml_view_1d(ctx0, write, n_s, p*write->nb[1]), 1, n_s);
ggml_tensor * u = ggml_reshape_2d(ctx0, update, 1, update->ne[0]*update->ne[1]);
- ggml_tensor * out = ggml_add(ctx0, mixed, ggml_mul_mat(ctx0, w, u));
+ ggml_tensor * out = ggml_add(ctx0, mixed, build_lora_mm(w, u));
return ggml_reshape_3d(ctx0, out, n_s, update->ne[0], update->ne[1]);
}
@@ -204,14 +204,14 @@ ggml_tensor * llama_model_metis16::graph::mem_read(const llama_model_metis16 & m
ggml_tensor * x_e = ggml_cont(ctx0, ggml_permute(ctx0, *x, 1, 0, 2, 3));
- ggml_tensor * q = ggml_mul_mat(ctx0, model.depth_mem_query, ggml_reshape_2d(ctx0, x_e, n_embd, n_s*n_tokens));
+ ggml_tensor * q = build_lora_mm(model.depth_mem_query, ggml_reshape_2d(ctx0, x_e, n_embd, n_s*n_tokens));
q = ggml_reshape_3d(ctx0, ggml_add(ctx0, q, model.depth_mem_query_b), n_m, n_s, n_tokens);
ggml_tensor * kq = ggml_mul_mat(ctx0, bank_k, q);
kq = ggml_soft_max_ext(ctx0, kq, nullptr, 1.0f/sqrtf(float(n_m)), 0.0f);
ggml_tensor * ret = ggml_mul_mat(ctx0, bank_vt, kq);
- ggml_tensor * proj = ggml_mul_mat(ctx0, model.depth_mem_out, ggml_reshape_2d(ctx0, ret, n_m, n_s*n_tokens));
+ ggml_tensor * proj = build_lora_mm(model.depth_mem_out, ggml_reshape_2d(ctx0, ret, n_m, n_s*n_tokens));
proj = ggml_reshape_3d(ctx0, ggml_add(ctx0, proj, model.depth_mem_out_b), n_embd, n_s, n_tokens);
ggml_tensor * gate = ggml_sum_rows(ctx0, ggml_mul(ctx0, x_e, model.depth_mem_stream_gate));
@@ -232,11 +232,11 @@ void llama_model_metis16::graph::mem_write(const llama_model_metis16 & model, gg
}
ggml_tensor * meta = ggml_concat(ctx0, ggml_concat(ctx0, summary, entropy, 0), ggml_concat(ctx0, confidence, cont, 0), 0);
- ggml_tensor * e = ggml_add(ctx0, ggml_mul_mat(ctx0, model.depth_mem_state_write, stream_mean(x)), ggml_mul_mat(ctx0, model.depth_mem_meta_write, meta));
+ ggml_tensor * e = ggml_add(ctx0, build_lora_mm(model.depth_mem_state_write, stream_mean(x)), build_lora_mm(model.depth_mem_meta_write, meta));
e = ggml_add(ctx0, e, ggml_view_1d(ctx0, model.depth_mem_entry_bias, n_m, entry*model.depth_mem_entry_bias->nb[1]));
- ggml_tensor * k = ggml_add(ctx0, ggml_mul_mat(ctx0, model.depth_mem_key, e), model.depth_mem_key_b);
- ggml_tensor * v = ggml_add(ctx0, ggml_mul_mat(ctx0, model.depth_mem_value, e), model.depth_mem_value_b);
+ ggml_tensor * k = ggml_add(ctx0, build_lora_mm(model.depth_mem_key, e), model.depth_mem_key_b);
+ ggml_tensor * v = ggml_add(ctx0, build_lora_mm(model.depth_mem_value, e), model.depth_mem_value_b);
k = ggml_reshape_3d(ctx0, k, n_m, 1, n_tokens);
v = ggml_reshape_3d(ctx0, v, 1, n_m, n_tokens);
bank_k = bank_k ? ggml_concat(ctx0, bank_k, k, 1) : k;
@@ -245,15 +245,15 @@ void llama_model_metis16::graph::mem_write(const llama_model_metis16 & model, gg
ggml_tensor * llama_model_metis16::graph::route_features(const llama_model_metis16 & model, ggml_tensor * state,
ggml_tensor * summary, ggml_tensor * diff, ggml_tensor * history) const {
- ggml_tensor * f = ggml_mul_mat(ctx0, model.depth_mem_route_state, state);
+ ggml_tensor * f = build_lora_mm(model.depth_mem_route_state, state);
if (summary) {
- f = ggml_add(ctx0, f, ggml_mul_mat(ctx0, model.depth_mem_route_retr, summary));
+ f = ggml_add(ctx0, f, build_lora_mm(model.depth_mem_route_retr, summary));
}
if (diff) {
- f = ggml_add(ctx0, f, ggml_mul_mat(ctx0, model.depth_mem_route_diff, diff));
+ f = ggml_add(ctx0, f, build_lora_mm(model.depth_mem_route_diff, diff));
}
if (history) {
- f = ggml_add(ctx0, f, ggml_mul_mat(ctx0, model.depth_mem_route_hist, history));
+ f = ggml_add(ctx0, f, build_lora_mm(model.depth_mem_route_hist, history));
}
return ggml_tanh(ctx0, ggml_add(ctx0, f, model.depth_mem_route_b));
}
@@ -301,7 +301,7 @@ llama_model_metis16::graph::graph(const llm_graph_params & params) :
ggml_tensor * cur = hc_read(x, layer.hc_attn_mix, layer.hc_attn_read, layer.attn_norm, p, &mixed, il);
if (is_attn) {
ggml_tensor * qkv = build_lora_mm(layer.wqkv, cur);
- ggml_tensor * lora = ggml_mul_mat(ctx0, layer.attn_pass_lora_up, ggml_mul_mat(ctx0, layer.attn_pass_lora_down, cur));
+ ggml_tensor * lora = build_lora_mm(layer.attn_pass_lora_up, build_lora_mm(layer.attn_pass_lora_down, cur));
qkv = ggml_add(ctx0, qkv, ggml_mul(ctx0, lora, ggml_view_1d(ctx0, layer.attn_pass_gate, 1, p*layer.attn_pass_gate->nb[0])));
const int64_t n_head = hparams.n_head(il);
@@ -311,7 +311,7 @@ llama_model_metis16::graph::graph(const llm_graph_params & params) :
ggml_tensor * K = ggml_cont(ctx0, ggml_view_3d(ctx0, qkv, n_hd, n_head_kv, n_tokens, n_hd*es, qkv->nb[1], n_hd*n_head*es));
ggml_tensor * V = ggml_cont(ctx0, ggml_view_3d(ctx0, qkv, n_hd, n_head_kv, n_tokens, n_hd*es, qkv->nb[1], n_hd*(n_head + n_head_kv)*es));
- cur = build_attn(inp->get_attn(), layer.wo, nullptr, nullptr, Q, K, V, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_hd)), il);
+ cur = build_attn(inp->get_attn(), layer.wo, nullptr, Q, K, V, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_hd)), il);
} else {
cur = build_mamba2_layer(inp->get_recr(), cur, ubatch, il);
}
@@ -320,8 +320,8 @@ llama_model_metis16::graph::graph(const llm_graph_params & params) :
cur = hc_read(x, layer.hc_ffn_mix, layer.hc_ffn_read, layer.ffn_norm, p, &mixed, il);
{
- ggml_tensor * latent = ggml_mul_mat(ctx0, layer.ffn_latent_down, cur);
- ggml_tensor * logits = ggml_mul_mat(ctx0, layer.ffn_gate_inp, ggml_concat(ctx0, latent, feats, 0));
+ ggml_tensor * latent = build_lora_mm(layer.ffn_latent_down, cur);
+ ggml_tensor * logits = build_lora_mm(layer.ffn_gate_inp, ggml_concat(ctx0, latent, feats, 0));
logits = ggml_add(ctx0, logits, layer.ffn_gate_inp_b);
// the balance bias picks the experts; the weights come from the unbiased logits
@@ -331,11 +331,11 @@ llama_model_metis16::graph::graph(const llm_graph_params & params) :
n_exp, n_used, LLM_FFN_SILU, false, 0.0f, LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX_WEIGHT, il_p,
logits, layer.ffn_gate_up_exps, selected);
ggml_tensor * shexp = build_ffn(latent,
- layer.ffn_up_shexp, NULL, NULL,
- layer.ffn_gate_shexp, NULL, NULL,
- layer.ffn_down_shexp, NULL, NULL,
+ layer.ffn_up_shexp, NULL,
+ layer.ffn_gate_shexp, NULL,
+ layer.ffn_down_shexp, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il_p);
- cur = ggml_mul_mat(ctx0, layer.ffn_latent_up, ggml_add(ctx0, moe_out, shexp));
+ cur = build_lora_mm(layer.ffn_latent_up, ggml_add(ctx0, moe_out, shexp));
cb(cur, "ffn_out", il);
// routing summary: the chosen experts' identity vectors blended by their weights
@@ -370,13 +370,13 @@ llama_model_metis16::graph::graph(const llm_graph_params & params) :
ggml_tensor * diff = ggml_sub(ctx0, state, prev_state);
ggml_tensor * feats = route_features(model, state, retrieved, diff, history);
- ggml_tensor * h = ggml_mul_mat(ctx0, model.cont_hidden_state, state);
- h = ggml_add(ctx0, h, ggml_mul_mat(ctx0, model.cont_hidden_retr, retrieved));
- h = ggml_add(ctx0, h, ggml_mul_mat(ctx0, model.cont_hidden_diff, diff));
- h = ggml_add(ctx0, h, ggml_mul_mat(ctx0, model.cont_hidden_route, feats));
+ ggml_tensor * h = build_lora_mm(model.cont_hidden_state, state);
+ h = ggml_add(ctx0, h, build_lora_mm(model.cont_hidden_retr, retrieved));
+ h = ggml_add(ctx0, h, build_lora_mm(model.cont_hidden_diff, diff));
+ h = ggml_add(ctx0, h, build_lora_mm(model.cont_hidden_route, feats));
h = ggml_silu(ctx0, ggml_add(ctx0, h, model.cont_hidden_b));
- ggml_tensor * logit = ggml_add(ctx0, ggml_mul_mat(ctx0, model.cont_output, h), model.cont_output_b);
+ ggml_tensor * logit = ggml_add(ctx0, build_lora_mm(model.cont_output, h), model.cont_output_b);
ggml_tensor * scale = ggml_clamp(ctx0, ggml_sqrt(ctx0, ggml_mean(ctx0, ggml_sqr(ctx0, state))), 1.0e-6f, INFINITY);
ggml_tensor * innov = ggml_sqrt(ctx0, ggml_mean(ctx0, ggml_sqr(ctx0, diff)));
logit = ggml_add(ctx0, logit, ggml_scale_bias(ctx0, ggml_tanh(ctx0, ggml_div(ctx0, innov, scale)), 1.0f, -0.5f));
diff --git a/src/models/mimo2.cpp b/src/models/mimo2.cpp
index 4fa97ff5b..06f0adf00 100644
--- a/src/models/mimo2.cpp
+++ b/src/models/mimo2.cpp
@@ -156,7 +156,7 @@ llama_model_mimo2::graph::graph(const llm_graph_params & params) : llm_graph_con
ggml_tensor * sinks = model.layers[il].attn_sinks;
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, sinks, nullptr, 1.0f/sqrtf(float(n_embd_head_k)), il);
cb(cur, "attn_out", il);
@@ -183,9 +183,9 @@ llama_model_mimo2::graph::graph(const llm_graph_params & params) : llm_graph_con
if (model.layers[il].ffn_gate_inp == nullptr) {
// dense branch
cur = build_ffn(cur,
- model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
- model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
- model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b,
+ model.layers[il].ffn_gate, model.layers[il].ffn_gate_b,
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -238,7 +238,7 @@ llama_model_mimo2::graph::graph(const llm_graph_params & params) : llm_graph_con
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
@@ -297,7 +297,7 @@ llama_model_mimo2::graph_mtp::graph_mtp(const llm_graph_params & params)
ggml_tensor * concat = ggml_concat(ctx0, e_norm, h_norm, /*dim=*/ 0);
cb(concat, "mtp_concat", il);
- ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat, layer.nextn.eh_proj_s);
+ ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat);
cb(cur, "mtp_eh_proj", il);
ggml_tensor * inpSA = cur;
@@ -305,7 +305,7 @@ llama_model_mimo2::graph_mtp::graph_mtp(const llm_graph_params & params)
cur = build_norm(cur, layer.attn_norm, nullptr, LLM_NORM_RMS, il);
cb(cur, "mtp_attn_norm", il);
- ggml_tensor * qkv = build_lora_mm(layer.wqkv, cur, layer.wqkv_s);
+ ggml_tensor * qkv = build_lora_mm(layer.wqkv, cur);
cb(qkv, "mtp_wqkv", il);
const size_t row_k = ggml_row_size(qkv->type, n_embd_head_k);
@@ -326,7 +326,7 @@ llama_model_mimo2::graph_mtp::graph_mtp(const llm_graph_params & params)
cb(Vcur, "mtp_Vcur", il);
cur = build_attn(inp_attn,
- layer.wo, nullptr, layer.wo_s,
+ layer.wo, nullptr,
Qcur, Kcur, Vcur, nullptr, layer.attn_sinks, nullptr,
1.0f / sqrtf(float(n_embd_head_k)), il);
cb(cur, "mtp_attn_out", il);
@@ -344,9 +344,9 @@ llama_model_mimo2::graph_mtp::graph_mtp(const llm_graph_params & params)
GGML_ASSERT(layer.ffn_gate && layer.ffn_down && layer.ffn_up && "MIMO2 MTP requires dense FFN tensors");
cur = build_ffn(cur,
- layer.ffn_up, layer.ffn_up_b, nullptr,
- layer.ffn_gate, layer.ffn_gate_b, nullptr,
- layer.ffn_down, layer.ffn_down_b, nullptr,
+ layer.ffn_up, layer.ffn_up_b,
+ layer.ffn_gate, layer.ffn_gate_b,
+ layer.ffn_down, layer.ffn_down_b,
nullptr,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "mtp_ffn_out", il);
@@ -367,9 +367,8 @@ llama_model_mimo2::graph_mtp::graph_mtp(const llm_graph_params & params)
cb(cur, "mtp_shared_head_norm", -1);
ggml_tensor * head_w = layer.nextn.shared_head_head ? layer.nextn.shared_head_head : model.output;
- ggml_tensor * head_s = layer.nextn.shared_head_head ? layer.nextn.shared_head_head_s : model.output_s;
GGML_ASSERT(head_w && "MIMO2 MTP missing LM head fallback");
- cur = build_lora_mm(head_w, cur, head_s);
+ cur = build_lora_mm(head_w, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/minicpm3.cpp b/src/models/minicpm3.cpp
index 67c8d6708..5c182d91d 100644
--- a/src/models/minicpm3.cpp
+++ b/src/models/minicpm3.cpp
@@ -101,7 +101,7 @@ llama_model_minicpm3::graph::graph(const llm_graph_params & params) : llm_graph_
{
ggml_tensor * q = NULL;
// {n_embd, q_lora_rank} * {n_embd, n_tokens} -> {q_lora_rank, n_tokens}
- q = ggml_mul_mat(ctx0, model.layers[il].wq_a, cur);
+ q = build_lora_mm(model.layers[il].wq_a, cur);
cb(q, "q", il);
q = build_norm(q,
@@ -110,7 +110,7 @@ llama_model_minicpm3::graph::graph(const llm_graph_params & params) : llm_graph_
cb(q, "q", il);
// {q_lora_rank, n_head * hparams.n_embd_head_k()} * {q_lora_rank, n_tokens} -> {n_head * hparams.n_embd_head_k(), n_tokens}
- q = ggml_mul_mat(ctx0, model.layers[il].wq_b, q);
+ q = build_lora_mm(model.layers[il].wq_b, q);
cb(q, "q", il);
// {n_embd_head_k, n_head, n_tokens}, RoPE is applied to the trailing dims only
@@ -118,7 +118,7 @@ llama_model_minicpm3::graph::graph(const llm_graph_params & params) : llm_graph_
cb(q, "q", il);
// {n_embd, kv_lora_rank + n_embd_head_qk_rope} * {n_embd, n_tokens} -> {kv_lora_rank + n_embd_head_qk_rope, n_tokens}
- ggml_tensor * kv_pe_compresseed = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);
+ ggml_tensor * kv_pe_compresseed = build_lora_mm(model.layers[il].wkv_a_mqa, cur);
cb(kv_pe_compresseed, "kv_pe_compresseed", il);
// split into {kv_lora_rank, n_tokens}
@@ -140,7 +140,7 @@ llama_model_minicpm3::graph::graph(const llm_graph_params & params) : llm_graph_
cb(kv_compressed, "kv_compressed", il);
// {kv_lora_rank, n_head * (n_embd_head_qk_nope + n_embd_head_v)} * {kv_lora_rank, n_tokens} -> {n_head * (n_embd_head_qk_nope + n_embd_head_v), n_tokens}
- ggml_tensor * kv = ggml_mul_mat(ctx0, model.layers[il].wkv_b, kv_compressed);
+ ggml_tensor * kv = build_lora_mm(model.layers[il].wkv_b, kv_compressed);
cb(kv, "kv", il);
// split into {n_head * n_embd_head_qk_nope, n_tokens}
@@ -176,7 +176,7 @@ llama_model_minicpm3::graph::graph(const llm_graph_params & params) : llm_graph_
cb(k_states, "k_states", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
q_states, k_states, v_states, nullptr, nullptr, nullptr, kq_scale, il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -199,9 +199,9 @@ llama_model_minicpm3::graph::graph(const llm_graph_params & params) : llm_graph_
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -233,7 +233,7 @@ llama_model_minicpm3::graph::graph(const llm_graph_params & params) : llm_graph_
cb(cur, "lmhead_scaling", -1);
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/minimax-01.cpp b/src/models/minimax-01.cpp
index 08268e0f3..14df1bfb4 100644
--- a/src/models/minimax-01.cpp
+++ b/src/models/minimax-01.cpp
@@ -263,7 +263,7 @@ llama_model_minimax_01::graph::graph(const llm_graph_params & params) : llm_grap
cb(Vcur, "Vcur", il);
cur = build_attn(inp_hybrid->get_attn(),
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
} else {
// lightning attention layer
@@ -466,7 +466,7 @@ llama_model_minimax_01::graph::graph(const llm_graph_params & params) : llm_grap
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/minimax-m2.cpp b/src/models/minimax-m2.cpp
index 5b8258255..cced2eb51 100644
--- a/src/models/minimax-m2.cpp
+++ b/src/models/minimax-m2.cpp
@@ -100,7 +100,7 @@ llama_model_minimax_m2::graph::graph(const llm_graph_params & params) : llm_grap
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
@@ -150,7 +150,7 @@ llama_model_minimax_m2::graph::graph(const llm_graph_params & params) : llm_grap
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/minimax-m3.cpp b/src/models/minimax-m3.cpp
index 8560c710a..92c7ca757 100644
--- a/src/models/minimax-m3.cpp
+++ b/src/models/minimax-m3.cpp
@@ -339,7 +339,7 @@ llama_model_minimax_m3::graph::graph(const llm_graph_params & params) : llm_grap
const bool is_sparse = msa_enabled && hparams.is_indexer_full(il);
if (!is_sparse) {
- cur = build_attn(inp_attn, model.layers[il].wo, NULL, model.layers[il].wo_s,
+ cur = build_attn(inp_attn, model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr,
1.0f/sqrtf(float(n_embd_head)), il);
} else {
@@ -535,7 +535,7 @@ llama_model_minimax_m3::graph::graph(const llm_graph_params & params) : llm_grap
}
cb(cur, "kqv_out", il);
if (model.layers[il].wo) {
- cur = build_lora_mm(model.layers[il].wo, cur, model.layers[il].wo_s);
+ cur = build_lora_mm(model.layers[il].wo, cur);
}
}
}
@@ -554,9 +554,9 @@ llama_model_minimax_m3::graph::graph(const llm_graph_params & params) : llm_grap
if ((uint32_t) il < hparams.n_layer_dense_lead) {
// leading dense FFN (swigluoai)
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, model.layers[il].ffn_up_s,
- model.layers[il].ffn_gate, NULL, model.layers[il].ffn_gate_s,
- model.layers[il].ffn_down, NULL, model.layers[il].ffn_down_s,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SWIGLU_OAI_MOE, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -577,9 +577,9 @@ llama_model_minimax_m3::graph::graph(const llm_graph_params & params) : llm_grap
// shared expert (swigluoai)
ggml_tensor * ffn_shexp = build_ffn(cur,
- model.layers[il].ffn_up_shexp, NULL, model.layers[il].ffn_up_shexp_s,
- model.layers[il].ffn_gate_shexp, NULL, model.layers[il].ffn_gate_shexp_s,
- model.layers[il].ffn_down_shexp, NULL, model.layers[il].ffn_down_shexp_s,
+ model.layers[il].ffn_up_shexp, NULL,
+ model.layers[il].ffn_gate_shexp, NULL,
+ model.layers[il].ffn_down_shexp, NULL,
NULL,
LLM_FFN_SWIGLU_OAI_MOE, LLM_FFN_PAR, il);
cb(ffn_shexp, "ffn_shexp", il);
@@ -611,7 +611,7 @@ llama_model_minimax_m3::graph::graph(const llm_graph_params & params) : llm_grap
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
}
diff --git a/src/models/mistral-large4.cpp b/src/models/mistral-large4.cpp
index aab49df9d..952313804 100644
--- a/src/models/mistral-large4.cpp
+++ b/src/models/mistral-large4.cpp
@@ -239,7 +239,7 @@ llama_model_mistral_large4::graph::graph(const llm_graph_params & params) :
ggml_tensor * indexer_q = build_norm(cur, layer.indexer_q_norm, layer.indexer_q_norm_b, LLM_NORM, il);
cb(indexer_q, "indexer_q_norm", il);
- indexer_q = ggml_mul_mat(ctx0, layer.indexer_attn_q_b, indexer_q);
+ indexer_q = build_lora_mm(layer.indexer_attn_q_b, indexer_q);
cb(indexer_q, "indexer_q", il);
// {n_embd_indexer_head, n_indexer_head, n_tokens}
@@ -247,7 +247,7 @@ llama_model_mistral_large4::graph::graph(const llm_graph_params & params) :
indexer_q = ggml_mul(ctx0, indexer_q, build_ssmax(layer.indexer_ssmax_s, layer.indexer_ssmax_b, n_indexer_head));
cb(indexer_q, "indexer_q_ssmax", il);
- ggml_tensor * indexer_k = ggml_mul_mat(ctx0, layer.indexer_attn_k, cur);
+ ggml_tensor * indexer_k = build_lora_mm(layer.indexer_attn_k, cur);
cb(indexer_k, "indexer_k", il);
indexer_k = build_norm(indexer_k, layer.indexer_k_norm, layer.indexer_k_norm_b, LLM_NORM, il);
@@ -275,7 +275,7 @@ llama_model_mistral_large4::graph::graph(const llm_graph_params & params) :
const auto & k_idxs_lid = inp_attn_dsa->get_k_idxs_lid();
ggml_build_forward_expand(gf, mctx_lid->cpy_k(ctx0, indexer_k, k_idxs_lid, il));
- ggml_tensor * indexer_weights = ggml_mul_mat(ctx0, layer.indexer_proj, cur);
+ ggml_tensor * indexer_weights = build_lora_mm(layer.indexer_proj, cur);
cb(indexer_weights, "indexer_weights", il);
// get cached indexer keys
@@ -304,7 +304,7 @@ llama_model_mistral_large4::graph::graph(const llm_graph_params & params) :
cb(top_k, "top_k", il);
}
- ggml_tensor * q = ggml_mul_mat(ctx0, layer.wq, cur);
+ ggml_tensor * q = build_lora_mm(layer.wq, cur);
cb(q, "q", il);
// {n_embd_head_qk_nope, n_head, n_tokens}
@@ -318,7 +318,7 @@ llama_model_mistral_large4::graph::graph(const llm_graph_params & params) :
cb(q_nope, "q_nope_perm", il);
// {n_embd_head_qk_nope, kv_lora_rank, n_head} x {n_embd_head_qk_nope, n_tokens, n_head}
- ggml_tensor * q_nope_absorbed = ggml_mul_mat(ctx0, layer.wk_b, q_nope);
+ ggml_tensor * q_nope_absorbed = build_lora_mm(layer.wk_b, q_nope);
cb(q_nope_absorbed, "q_nope_absorbed", il);
// {kv_lora_rank, n_head, n_tokens}
@@ -350,7 +350,7 @@ llama_model_mistral_large4::graph::graph(const llm_graph_params & params) :
ggml_tensor * Vcur = nullptr;
if (hparams.has_kv(il)) {
- ggml_tensor * kv_cmpr_pe = ggml_mul_mat(ctx0, layer.wkv_a_mqa, cur);
+ ggml_tensor * kv_cmpr_pe = build_lora_mm(layer.wkv_a_mqa, cur);
cb(kv_cmpr_pe, "kv_cmpr_pe", il);
// {kv_lora_rank, n_tokens}
@@ -385,7 +385,7 @@ llama_model_mistral_large4::graph::graph(const llm_graph_params & params) :
// note: MLA with the absorption optimization converts into MQA (ie: GQA with 1 group)
cur = build_attn(inp_attn_dsa,
- layer.wo, NULL, layer.wo_s,
+ layer.wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, layer.wv_b, top_k, kq_scale, il);
}
@@ -402,9 +402,9 @@ llama_model_mistral_large4::graph::graph(const llm_graph_params & params) :
if ((uint32_t) il < hparams.n_layer_dense_lead) {
cur = build_ffn(cur,
- layer.ffn_up, NULL, layer.ffn_up_s,
- layer.ffn_gate, NULL, layer.ffn_gate_s,
- layer.ffn_down, NULL, layer.ffn_down_s,
+ layer.ffn_up, NULL,
+ layer.ffn_gate, NULL,
+ layer.ffn_down, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
} else {
@@ -424,9 +424,9 @@ llama_model_mistral_large4::graph::graph(const llm_graph_params & params) :
cb(moe_out, "ffn_moe_out", il);
ggml_tensor * ffn_shexp = build_ffn(cur,
- layer.ffn_up_shexp, NULL, layer.ffn_up_shexp_s,
- layer.ffn_gate_shexp, NULL, layer.ffn_gate_shexp_s,
- layer.ffn_down_shexp, NULL, layer.ffn_down_shexp_s,
+ layer.ffn_up_shexp, NULL,
+ layer.ffn_gate_shexp, NULL,
+ layer.ffn_down_shexp, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "ffn_shexp", il);
@@ -454,7 +454,7 @@ llama_model_mistral_large4::graph::graph(const llm_graph_params & params) :
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = ggml_mul_mat(ctx0, model.output, cur);
+ cur = build_lora_mm(model.output, cur);
if (hparams.f_final_logit_softcapping) {
cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_final_logit_softcapping);
diff --git a/src/models/mistral3.cpp b/src/models/mistral3.cpp
index b883dfa58..83ac7581f 100644
--- a/src/models/mistral3.cpp
+++ b/src/models/mistral3.cpp
@@ -191,7 +191,7 @@ llama_model_mistral3::graph::graph(const llm_graph_params & params) : llm_graph_
}
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "attn_out", il);
}
@@ -213,9 +213,9 @@ llama_model_mistral3::graph::graph(const llm_graph_params & params) : llm_graph_
if (t_cond) {
// ada rms norm: scale the normalized input by (1 + mlp(t_cond))
ggml_tensor * ada = build_ffn(t_cond,
- model.layers[il].ffn_norm_ada_down, NULL, NULL,
- NULL, NULL, NULL,
- model.layers[il].ffn_norm_ada_up, NULL, NULL,
+ model.layers[il].ffn_norm_ada_down, NULL,
+ NULL, NULL,
+ model.layers[il].ffn_norm_ada_up, NULL,
NULL,
LLM_FFN_GELU_ERF, LLM_FFN_SEQ, il);
cur = ggml_add(ctx0, cur, ggml_mul(ctx0, cur, ada));
@@ -223,9 +223,9 @@ llama_model_mistral3::graph::graph(const llm_graph_params & params) : llm_graph_
}
cur = build_ffn(cur,
- model.layers[il].ffn_up, model.layers[il].ffn_up_b, model.layers[il].ffn_up_s,
- model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, model.layers[il].ffn_gate_s,
- model.layers[il].ffn_down, model.layers[il].ffn_down_b, model.layers[il].ffn_down_s,
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b,
+ model.layers[il].ffn_gate, model.layers[il].ffn_gate_b,
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -268,7 +268,7 @@ llama_model_mistral3::graph::graph(const llm_graph_params & params) : llm_graph_
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/models.h b/src/models/models.h
index 85b97dda0..128722e69 100644
--- a/src/models/models.h
+++ b/src/models/models.h
@@ -1723,7 +1723,7 @@ struct llama_model_dflash : public llama_model_base {
// graph helpers shared by the backbones
static ggml_tensor * build_inject_inp(llm_graph_context & g, const llama_model & model);
static ggml_tensor * get_tok_embd(const llm_graph_context & g, const llama_model & model);
- static std::pair<ggml_tensor *, ggml_tensor *> get_lm_head(const llm_graph_context & g, const llama_model & model);
+ static ggml_tensor * get_lm_head(const llm_graph_context & g, const llama_model & model);
// DFlash2 local conv, run by the base layers of DFlash2 and LiliCorr drafts
static ggml_tensor * build_dflash2_conv(llm_graph_context & g, ggml_tensor * hidden, ggml_tensor * dynamic, ggml_tensor * base, int side);
diff --git a/src/models/modern-bert.cpp b/src/models/modern-bert.cpp
index 17c9c5ab6..9b3aa0374 100644
--- a/src/models/modern-bert.cpp
+++ b/src/models/modern-bert.cpp
@@ -184,7 +184,7 @@ llama_model_modern_bert::graph::graph(const llm_graph_params & params) : llm_gra
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, nullptr, model.layers[il].wo_s,
+ model.layers[il].wo, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
cb(cur, "kqv_out", il);
@@ -204,9 +204,9 @@ llama_model_modern_bert::graph::graph(const llm_graph_params & params) : llm_gra
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- NULL, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ NULL, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
hparams.llm_ffn_op,
LLM_FFN_SEQ, il);
@@ -258,7 +258,7 @@ ggml_tensor * llama_model_modern_bert::graph::build_decision_head(
auto [Qcur, Kcur, Vcur] = build_qkv(layer, cur, n_embd_head, n_head, n_head_kv, il);
cur = build_attn(inp_attn,
- layer.wo, layer.wo_b, layer.wo_s,
+ layer.wo, layer.wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
cb(cur, "kqv_out", il);
@@ -274,9 +274,9 @@ ggml_tensor * llama_model_modern_bert::graph::build_decision_head(
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- layer.ffn_up, layer.ffn_up_b, NULL,
- NULL, NULL, NULL,
- layer.ffn_down, layer.ffn_down_b, NULL,
+ layer.ffn_up, layer.ffn_up_b,
+ NULL, NULL,
+ layer.ffn_down, layer.ffn_down_b,
NULL,
LLM_FFN_RELU,
LLM_FFN_SEQ, il);
diff --git a/src/models/moshi.cpp b/src/models/moshi.cpp
index 5cb63cd7e..724dafa71 100644
--- a/src/models/moshi.cpp
+++ b/src/models/moshi.cpp
@@ -81,7 +81,7 @@ llama_model_moshi::graph::graph(const llm_graph_params & params) : llm_graph_con
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
@@ -97,9 +97,9 @@ llama_model_moshi::graph::graph(const llm_graph_params & params) : llm_graph_con
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -117,7 +117,7 @@ llama_model_moshi::graph::graph(const llm_graph_params & params) : llm_graph_con
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
// pad the logits of the input-only tokens with -inf
const int64_t pad = model.tok_embd->ne[1] - model.output->ne[1];
diff --git a/src/models/mpt.cpp b/src/models/mpt.cpp
index 774907b4d..dd13e976a 100644
--- a/src/models/mpt.cpp
+++ b/src/models/mpt.cpp
@@ -120,7 +120,7 @@ llama_model_mpt::graph::graph(const llm_graph_params & params) : llm_graph_conte
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
}
@@ -138,9 +138,9 @@ llama_model_mpt::graph::graph(const llm_graph_params & params) : llm_graph_conte
cur = build_norm(ffn_inp, model.layers[il].ffn_norm, model.layers[il].ffn_norm_b, LLM_NORM, il);
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
- NULL, NULL, NULL,
- model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b,
+ NULL, NULL,
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b,
model.layers[il].ffn_act, LLM_FFN_GELU, LLM_FFN_SEQ, il);
cb(cur, "ffn_out", il);
}
@@ -161,7 +161,7 @@ llama_model_mpt::graph::graph(const llm_graph_params & params) : llm_graph_conte
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/muse-glimmer.cpp b/src/models/muse-glimmer.cpp
index e3f478fe3..128776b6b 100644
--- a/src/models/muse-glimmer.cpp
+++ b/src/models/muse-glimmer.cpp
@@ -112,7 +112,7 @@ llama_model_muse_glimmer::graph::graph(const llm_graph_params & params)
// SDPA. wo is deferred; the gate goes between attn_out and o_proj.
cur = build_attn(inp_attn,
- NULL, NULL, NULL,
+ NULL, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "attn_out", il);
@@ -121,7 +121,7 @@ llama_model_muse_glimmer::graph::graph(const llm_graph_params & params)
cur = ggml_mul(ctx0, cur, gate);
cb(cur, "attn_gated", il);
- cur = build_lora_mm(model.layers[il].wo, cur, model.layers[il].wo_s);
+ cur = build_lora_mm(model.layers[il].wo, cur);
cb(cur, "attn_o_proj", il);
}
@@ -143,9 +143,9 @@ llama_model_muse_glimmer::graph::graph(const llm_graph_params & params)
// SwiGLU dense FFN
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -169,7 +169,7 @@ llama_model_muse_glimmer::graph::graph(const llm_graph_params & params)
res->t_embd = cur;
// lm_head, followed by output multiplier
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cur = ggml_scale(ctx0, cur, hparams.f_logit_scale);
// Final logit tanh softcap (from gemma3.cpp).
diff --git a/src/models/muse-spark.cpp b/src/models/muse-spark.cpp
index 10de4676e..6f8442481 100644
--- a/src/models/muse-spark.cpp
+++ b/src/models/muse-spark.cpp
@@ -122,7 +122,7 @@ llama_model_muse_spark::graph::graph(const llm_graph_params & params)
cb(Kcur, "Kcur_rope", il);
cur = build_attn(inp_attn,
- NULL, NULL, NULL,
+ NULL, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "attn_out", il);
@@ -137,7 +137,7 @@ llama_model_muse_spark::graph::graph(const llm_graph_params & params)
cur = ggml_mul(ctx0, cur, gate);
cb(cur, "attn_gated", il);
- cur = build_lora_mm(model.layers[il].wo, cur, model.layers[il].wo_s);
+ cur = build_lora_mm(model.layers[il].wo, cur);
cb(cur, "attn_o_proj", il);
}
@@ -207,7 +207,7 @@ llama_model_muse_spark::graph::graph(const llm_graph_params & params)
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cur = ggml_scale(ctx0, cur, hparams.f_logit_scale);
if (hparams.f_final_logit_softcapping) {
diff --git a/src/models/nanbeige.cpp b/src/models/nanbeige.cpp
index 2f3e0ff1c..0c70e32ad 100644
--- a/src/models/nanbeige.cpp
+++ b/src/models/nanbeige.cpp
@@ -122,7 +122,7 @@ llama_model_nanbeige::graph::graph(const llm_graph_params & params) :
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "attn_out", il);
}
@@ -139,9 +139,9 @@ llama_model_nanbeige::graph::graph(const llm_graph_params & params) :
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, model.layers[il].ffn_up_b, model.layers[il].ffn_up_s,
- model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, model.layers[il].ffn_gate_s,
- model.layers[il].ffn_down, model.layers[il].ffn_down_b, model.layers[il].ffn_down_s,
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b,
+ model.layers[il].ffn_gate, model.layers[il].ffn_gate_b,
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -169,7 +169,7 @@ llama_model_nanbeige::graph::graph(const llm_graph_params & params) :
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
}
diff --git a/src/models/nemotron-h-moe.cpp b/src/models/nemotron-h-moe.cpp
index 3aa385379..05a023223 100644
--- a/src/models/nemotron-h-moe.cpp
+++ b/src/models/nemotron-h-moe.cpp
@@ -60,7 +60,7 @@ llama_model_nemotron_h_moe::graph_mtp::graph_mtp(const llm_graph_params & params
ggml_tensor * concat = ggml_concat(ctx0, e_norm, h_norm, /*dim=*/ 0);
cb(concat, "mtp_concat", il);
- ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat, layer.nextn.eh_proj_s);
+ ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat);
cb(cur, "mtp_eh_proj", il);
// dense NoPE attention sub-layer (mtp.layers.0)
@@ -72,7 +72,7 @@ llama_model_nemotron_h_moe::graph_mtp::graph_mtp(const llm_graph_params & params
auto [Qcur, Kcur, Vcur] = build_qkv(layer, cur, n_embd_head, hparams.n_head(il), hparams.n_head_kv(il), il);
const float kq_scale = hparams.f_attention_scale == 0.0f
? 1.0f / sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
- cur = build_attn(inp_attn, layer.wo, layer.wo_b, layer.wo_s,
+ cur = build_attn(inp_attn, layer.wo, layer.wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "mtp_attn_out", il);
}
@@ -95,15 +95,15 @@ llama_model_nemotron_h_moe::graph_mtp::graph_mtp(const llm_graph_params & params
cb(router_logits, "mtp_ffn_moe_logits", il);
ggml_tensor * ffn_shexp = build_ffn(cur,
- layer.ffn_up_shexp, NULL, layer.ffn_up_shexp_s,
- NULL, NULL, NULL,
- layer.ffn_down_shexp, NULL, layer.ffn_down_shexp_s,
+ layer.ffn_up_shexp, NULL,
+ NULL, NULL,
+ layer.ffn_down_shexp, NULL,
NULL,
LLM_FFN_RELU_SQR, LLM_FFN_PAR, il);
cb(ffn_shexp, "mtp_ffn_shexp", il);
if (layer.ffn_latent_down) {
- cur = ggml_mul_mat(ctx0, layer.ffn_latent_down, cur);
+ cur = build_lora_mm(layer.ffn_latent_down, cur);
}
ggml_tensor * moe_out =
@@ -122,7 +122,7 @@ llama_model_nemotron_h_moe::graph_mtp::graph_mtp(const llm_graph_params & params
cb(moe_out, "mtp_ffn_moe_out", il);
if (layer.ffn_latent_up) {
- moe_out = ggml_mul_mat(ctx0, layer.ffn_latent_up, moe_out);
+ moe_out = build_lora_mm(layer.ffn_latent_up, moe_out);
}
cur = ggml_add(ctx0, moe_out, ffn_shexp);
@@ -145,9 +145,8 @@ llama_model_nemotron_h_moe::graph_mtp::graph_mtp(const llm_graph_params & params
// LM head
ggml_tensor * head_w = layer.nextn.shared_head_head ? layer.nextn.shared_head_head : model.output;
- ggml_tensor * head_s = layer.nextn.shared_head_head ? layer.nextn.shared_head_head_s : model.output_s;
GGML_ASSERT(head_w != nullptr && "NEMOTRON_H_MOE MTP requires an output projection");
- cur = build_lora_mm(head_w, cur, head_s);
+ cur = build_lora_mm(head_w, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/nemotron-h.cpp b/src/models/nemotron-h.cpp
index 18e45320a..3b8760343 100644
--- a/src/models/nemotron-h.cpp
+++ b/src/models/nemotron-h.cpp
@@ -183,15 +183,6 @@ void llama_model_nemotron_h::load_arch_tensors(llama_model_loader & ml) {
layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {moe_n_embd, n_ff_exp, n_expert}, mtp_flags);
layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd}, mtp_flags);
layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_shexp}, mtp_flags);
-
- layer.ffn_down_exps_s = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "scale", i), {n_expert}, mtp_flags | TENSOR_NOT_REQUIRED);
- layer.ffn_up_exps_s = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "scale", i), {n_expert}, mtp_flags | TENSOR_NOT_REQUIRED);
- layer.ffn_down_exps_in_s = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "input_scale", i), {n_expert}, mtp_flags | TENSOR_NOT_REQUIRED);
- layer.ffn_up_exps_in_s = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "input_scale", i), {n_expert}, mtp_flags | TENSOR_NOT_REQUIRED);
- layer.ffn_down_shexp_s = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "scale", i), {1}, mtp_flags | TENSOR_NOT_REQUIRED);
- layer.ffn_up_shexp_s = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "scale", i), {1}, mtp_flags | TENSOR_NOT_REQUIRED);
- layer.ffn_down_shexp_in_s = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "input_scale", i), {1}, mtp_flags | TENSOR_NOT_REQUIRED);
- layer.ffn_up_shexp_in_s = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "input_scale", i), {1}, mtp_flags | TENSOR_NOT_REQUIRED);
}
}
@@ -270,7 +261,7 @@ llama_model_nemotron_h::graph::graph(const llm_graph_params & params) :
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
}
@@ -284,7 +275,7 @@ ggml_tensor * llama_model_nemotron_h::graph::build_attention_layer(ggml_tensor *
const float kq_scale =
hparams.f_attention_scale == 0.0f ? 1.0f / sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "attn_out", il);
return cur;
@@ -293,9 +284,9 @@ ggml_tensor * llama_model_nemotron_h::graph::build_attention_layer(ggml_tensor *
ggml_tensor * llama_model_nemotron_h::graph::build_ffn_layer(ggml_tensor * cur, int il) {
if (model.layers[il].ffn_gate_inp == nullptr) {
cur = build_ffn(cur,
- model.layers[il].ffn_up, model.layers[il].ffn_up_b, model.layers[il].ffn_up_s,
- NULL, NULL, NULL,
- model.layers[il].ffn_down, model.layers[il].ffn_down_b, model.layers[il].ffn_down_s,
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b,
+ NULL, NULL,
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b,
NULL,
LLM_FFN_RELU_SQR, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -304,7 +295,7 @@ ggml_tensor * llama_model_nemotron_h::graph::build_ffn_layer(ggml_tensor * cur,
ggml_tensor * inp_latent = cur;
if (model.layers[il].ffn_latent_down) {
- inp_latent = ggml_mul_mat(ctx0, model.layers[il].ffn_latent_down, cur);
+ inp_latent = build_lora_mm(model.layers[il].ffn_latent_down, cur);
}
ggml_tensor * router_logits = build_lora_mm(model.layers[il].ffn_gate_inp, cur);
@@ -326,13 +317,13 @@ ggml_tensor * llama_model_nemotron_h::graph::build_ffn_layer(ggml_tensor * cur,
cb(moe_out, "ffn_moe_out", il);
if (model.layers[il].ffn_latent_up) {
- moe_out = ggml_mul_mat(ctx0, model.layers[il].ffn_latent_up, moe_out);
+ moe_out = build_lora_mm(model.layers[il].ffn_latent_up, moe_out);
}
ggml_tensor * ffn_shexp = build_ffn(inp_emb,
- model.layers[il].ffn_up_shexp, NULL, model.layers[il].ffn_up_shexp_s,
- NULL /* no gate */ , NULL, NULL,
- model.layers[il].ffn_down_shexp, NULL, model.layers[il].ffn_down_shexp_s,
+ model.layers[il].ffn_up_shexp, NULL,
+ NULL /* no gate */ , NULL,
+ model.layers[il].ffn_down_shexp, NULL,
NULL,
LLM_FFN_RELU_SQR, LLM_FFN_PAR, il);
cb(ffn_shexp, "ffn_shexp", il);
diff --git a/src/models/nemotron.cpp b/src/models/nemotron.cpp
index f769b2537..139491a05 100644
--- a/src/models/nemotron.cpp
+++ b/src/models/nemotron.cpp
@@ -92,7 +92,7 @@ llama_model_nemotron::graph::graph(const llm_graph_params & params) : llm_graph_
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -110,9 +110,9 @@ llama_model_nemotron::graph::graph(const llm_graph_params & params) : llm_graph_
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
- NULL, NULL, NULL,
- model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b,
+ NULL, NULL,
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b,
NULL,
LLM_FFN_RELU_SQR, LLM_FFN_SEQ, il);
@@ -135,7 +135,7 @@ llama_model_nemotron::graph::graph(const llm_graph_params & params) : llm_graph_
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/neo-bert.cpp b/src/models/neo-bert.cpp
index 58a012a68..3b9a0f85d 100644
--- a/src/models/neo-bert.cpp
+++ b/src/models/neo-bert.cpp
@@ -78,7 +78,7 @@ llama_model_neo_bert::graph::graph(const llm_graph_params & params) : llm_graph_
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, nullptr, model.layers[il].wo_s,
+ model.layers[il].wo, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
cb(cur, "kqv_out", il);
}
@@ -101,9 +101,9 @@ llama_model_neo_bert::graph::graph(const llm_graph_params & params) : llm_graph_
// feed-forward network
cur = build_ffn(cur,
model.layers[il].ffn_up,
- NULL, NULL, NULL, NULL, NULL,
- model.layers[il].ffn_down,
NULL, NULL, NULL,
+ model.layers[il].ffn_down,
+ NULL, NULL,
LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
// attentions bypass the intermediate layer
diff --git a/src/models/olmo.cpp b/src/models/olmo.cpp
index 2afead631..844b6dfe4 100644
--- a/src/models/olmo.cpp
+++ b/src/models/olmo.cpp
@@ -81,7 +81,7 @@ llama_model_olmo::graph::graph(const llm_graph_params & params) : llm_graph_cont
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, nullptr, model.layers[il].wo_s,
+ model.layers[il].wo, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -98,9 +98,9 @@ llama_model_olmo::graph::graph(const llm_graph_params & params) : llm_graph_cont
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -124,7 +124,7 @@ llama_model_olmo::graph::graph(const llm_graph_params & params) : llm_graph_cont
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/olmo2.cpp b/src/models/olmo2.cpp
index c0a3e8295..2b35b171e 100644
--- a/src/models/olmo2.cpp
+++ b/src/models/olmo2.cpp
@@ -134,7 +134,7 @@ llama_model_olmo2::graph<iswa>::graph(const llm_graph_params & params) : llm_gra
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -151,9 +151,9 @@ llama_model_olmo2::graph<iswa>::graph(const llm_graph_params & params) : llm_gra
// feed-forward network
cur = build_ffn(ffn_inp,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -182,7 +182,7 @@ llama_model_olmo2::graph<iswa>::graph(const llm_graph_params & params) : llm_gra
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/olmoe.cpp b/src/models/olmoe.cpp
index 9ebdd9c94..3f3952ac1 100644
--- a/src/models/olmoe.cpp
+++ b/src/models/olmoe.cpp
@@ -108,7 +108,7 @@ llama_model_olmoe::graph::graph(const llm_graph_params & params) : llm_graph_con
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -155,7 +155,7 @@ llama_model_olmoe::graph::graph(const llm_graph_params & params) : llm_graph_con
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/openai-moe.cpp b/src/models/openai-moe.cpp
index 920acbb64..3a1b3c94e 100644
--- a/src/models/openai-moe.cpp
+++ b/src/models/openai-moe.cpp
@@ -97,7 +97,7 @@ llama_model_openai_moe::graph::graph(const llm_graph_params & params) : llm_grap
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, model.layers[il].attn_sinks, nullptr, 1.0f/sqrtf(float(n_rot)), il);
cb(cur, "attn_out", il);
@@ -154,7 +154,7 @@ llama_model_openai_moe::graph::graph(const llm_graph_params & params) : llm_grap
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/openelm.cpp b/src/models/openelm.cpp
index 3328bd54c..6db688a0e 100644
--- a/src/models/openelm.cpp
+++ b/src/models/openelm.cpp
@@ -111,7 +111,7 @@ llama_model_openelm::graph::graph(const llm_graph_params & params) : llm_graph_c
cb(Qcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -129,9 +129,9 @@ llama_model_openelm::graph::graph(const llm_graph_params & params) : llm_graph_c
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -153,7 +153,7 @@ llama_model_openelm::graph::graph(const llm_graph_params & params) : llm_graph_c
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/orion.cpp b/src/models/orion.cpp
index 4fe921189..e1dbe6bda 100644
--- a/src/models/orion.cpp
+++ b/src/models/orion.cpp
@@ -81,7 +81,7 @@ llama_model_orion::graph::graph(const llm_graph_params & params) : llm_graph_con
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -98,9 +98,9 @@ llama_model_orion::graph::graph(const llm_graph_params & params) : llm_graph_con
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -123,7 +123,7 @@ llama_model_orion::graph::graph(const llm_graph_params & params) : llm_graph_con
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/paddleocr.cpp b/src/models/paddleocr.cpp
index 4dbd9e215..684c2d480 100644
--- a/src/models/paddleocr.cpp
+++ b/src/models/paddleocr.cpp
@@ -47,7 +47,7 @@ llama_model_paddleocr::graph::graph(const llm_graph_params & params) :
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1) {
@@ -64,9 +64,9 @@ llama_model_paddleocr::graph::graph(const llm_graph_params & params) :
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
}
@@ -86,7 +86,7 @@ llama_model_paddleocr::graph::graph(const llm_graph_params & params) :
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/pangu-embed.cpp b/src/models/pangu-embed.cpp
index 336c71a41..92cc43fbf 100644
--- a/src/models/pangu-embed.cpp
+++ b/src/models/pangu-embed.cpp
@@ -104,7 +104,7 @@ llama_model_pangu_embed::graph::graph(const llm_graph_params & params) : llm_gra
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
@@ -123,9 +123,9 @@ llama_model_pangu_embed::graph::graph(const llm_graph_params & params) : llm_gra
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
- model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
- model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b,
+ model.layers[il].ffn_gate, model.layers[il].ffn_gate_b,
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
@@ -149,7 +149,7 @@ llama_model_pangu_embed::graph::graph(const llm_graph_params & params) : llm_gra
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
if (model.output_b != nullptr) {
cur = ggml_add(ctx0, cur, model.output_b);
diff --git a/src/models/phi2.cpp b/src/models/phi2.cpp
index 90c502cfd..3ebe6801a 100644
--- a/src/models/phi2.cpp
+++ b/src/models/phi2.cpp
@@ -86,7 +86,7 @@ llama_model_phi2::graph::graph(const llm_graph_params & params) : llm_graph_cont
Qcur = ggml_scale(ctx0, Qcur, 1.0f/sqrtf(float(n_embd_head)));
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -97,9 +97,9 @@ llama_model_phi2::graph::graph(const llm_graph_params & params) : llm_graph_cont
// FF
{
ffn_output = build_ffn(attn_norm_output,
- model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
- NULL, NULL, NULL,
- model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b,
+ NULL, NULL,
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b,
NULL,
LLM_FFN_GELU, LLM_FFN_SEQ, il);
cb(ffn_output, "ffn_out", il);
@@ -121,7 +121,7 @@ llama_model_phi2::graph::graph(const llm_graph_params & params) : llm_graph_cont
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output_no_bias", -1);
cur = ggml_add(ctx0, cur, model.output_b);
diff --git a/src/models/phi3.cpp b/src/models/phi3.cpp
index 815ab0465..4a856865d 100644
--- a/src/models/phi3.cpp
+++ b/src/models/phi3.cpp
@@ -113,7 +113,7 @@ llama_model_phi3::graph<iswa>::graph(const llm_graph_params & params) : llm_grap
cb(Qcur, "Qcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -131,9 +131,9 @@ llama_model_phi3::graph<iswa>::graph(const llm_graph_params & params) : llm_grap
// feed-forward network
if (model.layers[il].ffn_gate_inp == nullptr) {
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- NULL, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ NULL, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
cb(cur, "ffn_out", il);
@@ -168,7 +168,7 @@ llama_model_phi3::graph<iswa>::graph(const llm_graph_params & params) : llm_grap
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
if (model.output_b != nullptr) {
cb(cur, "result_output_no_bias", -1);
diff --git a/src/models/plamo.cpp b/src/models/plamo.cpp
index 10348f291..cc695ea10 100644
--- a/src/models/plamo.cpp
+++ b/src/models/plamo.cpp
@@ -77,7 +77,7 @@ llama_model_plamo::graph::graph(const llm_graph_params & params) : llm_graph_con
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -92,9 +92,9 @@ llama_model_plamo::graph::graph(const llm_graph_params & params) : llm_graph_con
// feed-forward network
{
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -118,7 +118,7 @@ llama_model_plamo::graph::graph(const llm_graph_params & params) : llm_graph_con
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/plamo2.cpp b/src/models/plamo2.cpp
index f019bd0b6..c04d0fab3 100644
--- a/src/models/plamo2.cpp
+++ b/src/models/plamo2.cpp
@@ -162,9 +162,9 @@ llama_model_plamo2::graph::graph(const llm_graph_params & params) :
// feed-forward network
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- NULL, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ NULL, NULL,
+ model.layers[il].ffn_down, NULL,
NULL, LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
cb(cur, "ffn_out", il);
@@ -189,7 +189,7 @@ llama_model_plamo2::graph::graph(const llm_graph_params & params) :
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
// Explicitly mark as output tensor to ensure proper backend assignment
@@ -241,7 +241,7 @@ ggml_tensor * llama_model_plamo2::graph::build_plamo2_attn_layer(llm_graph_input
Kcur = build_rope(Kcur, inp_pos, il);
cur = build_attn(inp,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, NULL, NULL, NULL, 1.0f / sqrtf(float(n_embd_head_v)), il);
}
diff --git a/src/models/plamo3.cpp b/src/models/plamo3.cpp
index d6de1a356..5efb13dea 100644
--- a/src/models/plamo3.cpp
+++ b/src/models/plamo3.cpp
@@ -124,7 +124,7 @@ llama_model_plamo3::graph<iswa>::graph(const llm_graph_params & params) :
const float attn_scale = 1.0f / sqrtf(float(head_dim_q));
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, attn_scale, il);
cb(cur, "attn_out", il);
@@ -145,9 +145,9 @@ llama_model_plamo3::graph<iswa>::graph(const llm_graph_params & params) :
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- NULL, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ NULL, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
cb(cur, "ffn_out", il);
@@ -170,7 +170,7 @@ llama_model_plamo3::graph<iswa>::graph(const llm_graph_params & params) :
cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
res->t_logits = cur;
}
diff --git a/src/models/plm.cpp b/src/models/plm.cpp
index b0069e49b..40a487d85 100644
--- a/src/models/plm.cpp
+++ b/src/models/plm.cpp
@@ -78,7 +78,7 @@ llama_model_plm::graph::graph(const llm_graph_params & params) : llm_graph_conte
// self_attention
{
ggml_tensor * q = NULL;
- q = ggml_mul_mat(ctx0, model.layers[il].wq, cur);
+ q = build_lora_mm(model.layers[il].wq, cur);
cb(q, "q", il);
// {n_embd_head_k, n_head, n_tokens}, RoPE is applied to the trailing dims only
@@ -86,7 +86,7 @@ llama_model_plm::graph::graph(const llm_graph_params & params) : llm_graph_conte
cb(q, "q", il);
// {n_embd, kv_lora_rank + n_embd_head_qk_rope} * {n_embd, n_tokens} -> {kv_lora_rank + n_embd_head_qk_rope, n_tokens}
- ggml_tensor * kv_pe_compresseed = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);
+ ggml_tensor * kv_pe_compresseed = build_lora_mm(model.layers[il].wkv_a_mqa, cur);
cb(kv_pe_compresseed, "kv_pe_compresseed", il);
// split into {kv_lora_rank, n_tokens}
@@ -108,7 +108,7 @@ llama_model_plm::graph::graph(const llm_graph_params & params) : llm_graph_conte
cb(kv_compressed, "kv_compressed", il);
// {kv_lora_rank, n_head * (n_embd_head_qk_nope + n_embd_head_v)} * {kv_lora_rank, n_tokens} -> {n_head * (n_embd_head_qk_nope + n_embd_head_v), n_tokens}
- ggml_tensor * kv = ggml_mul_mat(ctx0, model.layers[il].wkv_b, kv_compressed);
+ ggml_tensor * kv = build_lora_mm(model.layers[il].wkv_b, kv_compressed);
cb(kv, "kv", il);
// split into {n_head * n_embd_head_qk_nope, n_tokens}
@@ -149,7 +149,7 @@ llama_model_plm::graph::graph(const llm_graph_params & params) : llm_graph_conte
cb(k_states, "k_states", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
q_states, k_states, v_states, nullptr, nullptr, nullptr, kq_scale, il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -165,9 +165,9 @@ llama_model_plm::graph::graph(const llm_graph_params & params) : llm_graph_conte
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- NULL, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ NULL, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_RELU_SQR, LLM_FFN_SEQ, il);
cb(cur, "ffn_out", il);
@@ -189,7 +189,7 @@ llama_model_plm::graph::graph(const llm_graph_params & params) : llm_graph_conte
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/pockettts.cpp b/src/models/pockettts.cpp
index a7dcb5366..9b7a1669f 100644
--- a/src/models/pockettts.cpp
+++ b/src/models/pockettts.cpp
@@ -82,7 +82,7 @@ llama_model_pockettts::graph::graph(const llm_graph_params & params) : llm_graph
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
@@ -103,9 +103,9 @@ llama_model_pockettts::graph::graph(const llm_graph_params & params) : llm_graph
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- NULL, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ NULL, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_GELU, LLM_FFN_SEQ, il);
cb(cur, "ffn_out", il);
@@ -128,7 +128,7 @@ llama_model_pockettts::graph::graph(const llm_graph_params & params) : llm_graph
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/qwen.cpp b/src/models/qwen.cpp
index 4e0b1986d..0e6325bb9 100644
--- a/src/models/qwen.cpp
+++ b/src/models/qwen.cpp
@@ -79,7 +79,7 @@ llama_model_qwen::graph::graph(const llm_graph_params & params) : llm_graph_cont
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -97,9 +97,9 @@ llama_model_qwen::graph::graph(const llm_graph_params & params) : llm_graph_cont
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -122,7 +122,7 @@ llama_model_qwen::graph::graph(const llm_graph_params & params) : llm_graph_cont
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/qwen2.cpp b/src/models/qwen2.cpp
index bb93ca9b1..7846a5960 100644
--- a/src/models/qwen2.cpp
+++ b/src/models/qwen2.cpp
@@ -91,7 +91,7 @@ llama_model_qwen2::graph::graph(const llm_graph_params & params) : llm_graph_con
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -108,9 +108,9 @@ llama_model_qwen2::graph::graph(const llm_graph_params & params) : llm_graph_con
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, model.layers[il].ffn_up_s,
- model.layers[il].ffn_gate, NULL, model.layers[il].ffn_gate_s,
- model.layers[il].ffn_down, NULL, model.layers[il].ffn_down_s,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -133,7 +133,7 @@ llama_model_qwen2::graph::graph(const llm_graph_params & params) : llm_graph_con
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
if (model.output_b != nullptr) {
cur = ggml_add(ctx0, cur, model.output_b);
diff --git a/src/models/qwen2moe.cpp b/src/models/qwen2moe.cpp
index e24919bc6..abb2f5a8c 100644
--- a/src/models/qwen2moe.cpp
+++ b/src/models/qwen2moe.cpp
@@ -103,7 +103,7 @@ llama_model_qwen2moe::graph::graph(const llm_graph_params & params) : llm_graph_
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -143,9 +143,9 @@ llama_model_qwen2moe::graph::graph(const llm_graph_params & params) : llm_graph_
cb(cur_gate, "ffn_shexp_gate", il);
ggml_tensor * cur_ffn = build_ffn(cur,
- model.layers[il].ffn_up_shexp, NULL, NULL,
- model.layers[il].ffn_gate_shexp, NULL, NULL,
- model.layers[il].ffn_down_shexp, NULL, NULL,
+ model.layers[il].ffn_up_shexp, NULL,
+ model.layers[il].ffn_gate_shexp, NULL,
+ model.layers[il].ffn_down_shexp, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur_ffn, "ffn_shexp", il);
@@ -176,7 +176,7 @@ llama_model_qwen2moe::graph::graph(const llm_graph_params & params) : llm_graph_
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/qwen2vl.cpp b/src/models/qwen2vl.cpp
index ba72876c2..a70089fb1 100644
--- a/src/models/qwen2vl.cpp
+++ b/src/models/qwen2vl.cpp
@@ -80,7 +80,7 @@ llama_model_qwen2vl::graph::graph(const llm_graph_params & params) : llm_graph_c
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -97,9 +97,9 @@ llama_model_qwen2vl::graph::graph(const llm_graph_params & params) : llm_graph_c
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, model.layers[il].ffn_up_s,
- model.layers[il].ffn_gate, NULL, model.layers[il].ffn_gate_s,
- model.layers[il].ffn_down, NULL, model.layers[il].ffn_down_s,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -122,7 +122,7 @@ llama_model_qwen2vl::graph::graph(const llm_graph_params & params) : llm_graph_c
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/qwen3.cpp b/src/models/qwen3.cpp
index 7a6052970..5059166e8 100644
--- a/src/models/qwen3.cpp
+++ b/src/models/qwen3.cpp
@@ -58,7 +58,7 @@ llama_model_qwen3::graph::graph(const llm_graph_params & params) : llm_graph_con
ggml_tensor * cur = build_backbone();
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
@@ -114,7 +114,7 @@ ggml_tensor * llama_model_qwen3::graph::build_backbone() {
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -131,9 +131,9 @@ ggml_tensor * llama_model_qwen3::graph::build_backbone() {
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, model.layers[il].ffn_up_s,
- model.layers[il].ffn_gate, NULL, model.layers[il].ffn_gate_s,
- model.layers[il].ffn_down, NULL, model.layers[il].ffn_down_s,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
diff --git a/src/models/qwen35.cpp b/src/models/qwen35.cpp
index 5b05fc63f..542e150cb 100644
--- a/src/models/qwen35.cpp
+++ b/src/models/qwen35.cpp
@@ -234,7 +234,7 @@ llama_model_qwen35::graph::graph(const llm_graph_params & params) :
}
// LM head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
@@ -262,11 +262,11 @@ std::pair<ggml_tensor *, ggml_tensor *> llama_model_qwen35::graph::build_qkvz(
return { qkv_mixed, z };
}
- ggml_tensor * qkv_mixed = build_lora_mm(model.layers[il].wqkv, input, model.layers[il].wqkv_s);
+ ggml_tensor * qkv_mixed = build_lora_mm(model.layers[il].wqkv, input);
qkv_mixed = ggml_reshape_3d(ctx0, qkv_mixed, qkv_mixed->ne[0], n_seq_tokens, n_seqs);
cb(qkv_mixed, "linear_attn_qkv_mixed", il);
- ggml_tensor * z = build_lora_mm(model.layers[il].wqkv_gate, input, model.layers[il].wqkv_gate_s);
+ ggml_tensor * z = build_lora_mm(model.layers[il].wqkv_gate, input);
cb(z, "z", il);
return { qkv_mixed, z };
@@ -338,7 +338,7 @@ ggml_tensor * llama_model_qwen35::graph::build_layer_attn(
const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f / sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
cur = build_attn(inp,
- nullptr, nullptr, nullptr,
+ nullptr, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "attn_pregate", il);
@@ -348,7 +348,7 @@ ggml_tensor * llama_model_qwen35::graph::build_layer_attn(
cur = ggml_mul(ctx0, cur, gate_sigmoid);
cb(cur, "attn_gated", il);
- cur = build_lora_mm(model.layers[il].wo, cur, model.layers[il].wo_s);
+ cur = build_lora_mm(model.layers[il].wo, cur);
cb(cur, "attn_output", il);
return cur;
@@ -388,10 +388,10 @@ ggml_tensor * llama_model_qwen35::graph::build_layer_attn_linear(
alpha = ggml_view_3d(ctx0, ba, num_v_heads, n_seq_tokens, n_seqs,
ba->nb[1], ba->nb[1] * n_seq_tokens, ggml_row_size(ba->type, num_v_heads));
} else {
- beta = build_lora_mm(model.layers[il].ssm_beta, cur, model.layers[il].ssm_beta_s);
+ beta = build_lora_mm(model.layers[il].ssm_beta, cur);
beta = ggml_reshape_4d(ctx0, beta, 1, num_v_heads, n_seq_tokens, n_seqs);
- alpha = build_lora_mm(model.layers[il].ssm_alpha, cur, model.layers[il].ssm_alpha_s);
+ alpha = build_lora_mm(model.layers[il].ssm_alpha, cur);
alpha = ggml_reshape_3d(ctx0, alpha, num_v_heads, n_seq_tokens, n_seqs);
}
cb(beta, "beta", il);
@@ -485,7 +485,7 @@ ggml_tensor * llama_model_qwen35::graph::build_layer_attn_linear(
cb(final_output, "final_output", il);
// Output projection
- cur = build_lora_mm(model.layers[il].ssm_out, final_output, model.layers[il].ssm_out_s);
+ cur = build_lora_mm(model.layers[il].ssm_out, final_output);
cb(cur, "linear_attn_out", il);
return cur;
@@ -496,9 +496,9 @@ ggml_tensor * llama_model_qwen35::graph::build_layer_ffn(ggml_tensor * cur, cons
GGML_ASSERT(model.layers[il].ffn_gate_inp == nullptr);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, model.layers[il].ffn_up_s,
- model.layers[il].ffn_gate, NULL, model.layers[il].ffn_gate_s,
- model.layers[il].ffn_down, NULL, model.layers[il].ffn_down_s,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -576,7 +576,7 @@ llama_model_qwen35::graph_mtp::graph_mtp(const llm_graph_params & params)
ggml_tensor * concat = ggml_concat(ctx0, e_norm, h_norm, /*dim=*/ 0);
cb(concat, "mtp_concat", il);
- ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat, layer.nextn.eh_proj_s);
+ ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat);
cb(cur, "mtp_eh_proj", il);
ggml_tensor * inpSA = cur;
@@ -621,12 +621,12 @@ llama_model_qwen35::graph_mtp::graph_mtp(const llm_graph_params & params)
? 1.0f / sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
cur = build_attn(inp_attn,
- nullptr, nullptr, nullptr,
+ nullptr, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "mtp_attn_pregate", il);
cur = ggml_mul(ctx0, cur, ggml_sigmoid(ctx0, gate));
- cur = build_lora_mm(layer.wo, cur, layer.wo_s);
+ cur = build_lora_mm(layer.wo, cur);
cb(cur, "mtp_attn_out", il);
cur = ggml_add(ctx0, cur, inpSA);
@@ -637,9 +637,9 @@ llama_model_qwen35::graph_mtp::graph_mtp(const llm_graph_params & params)
cb(cur, "mtp_attn_post_norm", il);
cur = build_ffn(cur,
- layer.ffn_up, nullptr, layer.ffn_up_s,
- layer.ffn_gate, nullptr, layer.ffn_gate_s,
- layer.ffn_down, nullptr, layer.ffn_down_s,
+ layer.ffn_up, nullptr,
+ layer.ffn_gate, nullptr,
+ layer.ffn_down, nullptr,
nullptr,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "mtp_ffn_out", il);
@@ -660,9 +660,8 @@ llama_model_qwen35::graph_mtp::graph_mtp(const llm_graph_params & params)
cb(cur, "mtp_shared_head_norm", -1);
ggml_tensor * head_w = layer.nextn.shared_head_head ? layer.nextn.shared_head_head : model.output;
- ggml_tensor * head_s = layer.nextn.shared_head_head ? layer.nextn.shared_head_head_s : model.output_s;
GGML_ASSERT(head_w && "QWEN35 MTP: missing LM head (nextn.shared_head_head or model.output)");
- cur = build_lora_mm(head_w, cur, head_s);
+ cur = build_lora_mm(head_w, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/qwen35moe.cpp b/src/models/qwen35moe.cpp
index c92b84d9d..cf6dd9e74 100644
--- a/src/models/qwen35moe.cpp
+++ b/src/models/qwen35moe.cpp
@@ -237,7 +237,7 @@ llama_model_qwen35moe::graph::graph(const llm_graph_params & params) :
res->t_embd = cur;
// LM head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
@@ -249,11 +249,11 @@ std::pair<ggml_tensor *, ggml_tensor *> llama_model_qwen35moe::graph::build_qkvz
const int64_t n_seqs = ubatch.n_seqs;
const int64_t n_seq_tokens = ubatch.n_seq_tokens;
- ggml_tensor * qkv_mixed = build_lora_mm(model.layers[il].wqkv, input, model.layers[il].wqkv_s);
+ ggml_tensor * qkv_mixed = build_lora_mm(model.layers[il].wqkv, input);
qkv_mixed = ggml_reshape_3d(ctx0, qkv_mixed, qkv_mixed->ne[0], n_seq_tokens, n_seqs);
cb(qkv_mixed, "linear_attn_qkv_mixed", il);
- ggml_tensor * z = build_lora_mm(model.layers[il].wqkv_gate, input, model.layers[il].wqkv_gate_s);
+ ggml_tensor * z = build_lora_mm(model.layers[il].wqkv_gate, input);
cb(z, "z", il);
return { qkv_mixed, z };
@@ -325,7 +325,7 @@ ggml_tensor * llama_model_qwen35moe::graph::build_layer_attn(
const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f / sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
cur = build_attn(inp,
- nullptr, nullptr, nullptr,
+ nullptr, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "attn_pregate", il);
@@ -335,7 +335,7 @@ ggml_tensor * llama_model_qwen35moe::graph::build_layer_attn(
cur = ggml_mul(ctx0, cur, gate_sigmoid);
cb(cur, "attn_gated", il);
- cur = build_lora_mm(model.layers[il].wo, cur, model.layers[il].wo_s);
+ cur = build_lora_mm(model.layers[il].wo, cur);
cb(cur, "attn_output", il);
return cur;
@@ -364,14 +364,14 @@ ggml_tensor * llama_model_qwen35moe::graph::build_layer_attn_linear(
ggml_tensor * qkv_mixed = qkvz.first;
ggml_tensor * z = qkvz.second;
- ggml_tensor * beta = build_lora_mm(model.layers[il].ssm_beta, cur, model.layers[il].ssm_beta_s);
+ ggml_tensor * beta = build_lora_mm(model.layers[il].ssm_beta, cur);
beta = ggml_reshape_4d(ctx0, beta, 1, num_v_heads, n_seq_tokens, n_seqs);
cb(beta, "beta", il);
beta = ggml_sigmoid(ctx0, beta);
cb(beta, "beta_sigmoid", il);
- ggml_tensor * alpha = build_lora_mm(model.layers[il].ssm_alpha, cur, model.layers[il].ssm_alpha_s);
+ ggml_tensor * alpha = build_lora_mm(model.layers[il].ssm_alpha, cur);
alpha = ggml_reshape_3d(ctx0, alpha, num_v_heads, n_seq_tokens, n_seqs);
cb(alpha, "alpha", il);
@@ -458,7 +458,7 @@ ggml_tensor * llama_model_qwen35moe::graph::build_layer_attn_linear(
ggml_tensor * final_output = ggml_reshape_2d(ctx0, attn_out_norm, head_v_dim * num_v_heads, n_seq_tokens * n_seqs);
// Output projection
- cur = build_lora_mm(model.layers[il].ssm_out, final_output, model.layers[il].ssm_out_s);
+ cur = build_lora_mm(model.layers[il].ssm_out, final_output);
cb(cur, "linear_attn_out", il);
return cur;
@@ -486,9 +486,9 @@ ggml_tensor * llama_model_qwen35moe::graph::build_layer_ffn(ggml_tensor * cur, c
if (model.layers[il].ffn_up_shexp != nullptr) {
ggml_tensor * ffn_shexp =
build_ffn(cur,
- model.layers[il].ffn_up_shexp, NULL, model.layers[il].ffn_up_shexp_s,
- model.layers[il].ffn_gate_shexp, NULL, model.layers[il].ffn_gate_shexp_s,
- model.layers[il].ffn_down_shexp, NULL, model.layers[il].ffn_down_shexp_s,
+ model.layers[il].ffn_up_shexp, NULL,
+ model.layers[il].ffn_gate_shexp, NULL,
+ model.layers[il].ffn_down_shexp, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "ffn_shexp", il);
@@ -576,7 +576,7 @@ llama_model_qwen35moe::graph_mtp::graph_mtp(const llm_graph_params & params)
ggml_tensor * concat = ggml_concat(ctx0, e_norm, h_norm, /*dim=*/ 0);
cb(concat, "mtp_concat", il);
- ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat, layer.nextn.eh_proj_s);
+ ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat);
cb(cur, "mtp_eh_proj", il);
ggml_tensor * inpSA = cur;
@@ -621,12 +621,12 @@ llama_model_qwen35moe::graph_mtp::graph_mtp(const llm_graph_params & params)
? 1.0f / sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
cur = build_attn(inp_attn,
- nullptr, nullptr, nullptr,
+ nullptr, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "mtp_attn_pregate", il);
cur = ggml_mul(ctx0, cur, ggml_sigmoid(ctx0, gate));
- cur = build_lora_mm(layer.wo, cur, layer.wo_s);
+ cur = build_lora_mm(layer.wo, cur);
cb(cur, "mtp_attn_out", il);
cur = ggml_add(ctx0, cur, inpSA);
@@ -654,9 +654,9 @@ llama_model_qwen35moe::graph_mtp::graph_mtp(const llm_graph_params & params)
if (layer.ffn_up_shexp != nullptr) {
ggml_tensor * ffn_shexp =
build_ffn(cur,
- layer.ffn_up_shexp, nullptr, layer.ffn_up_shexp_s,
- layer.ffn_gate_shexp, nullptr, layer.ffn_gate_shexp_s,
- layer.ffn_down_shexp, nullptr, layer.ffn_down_shexp_s,
+ layer.ffn_up_shexp, nullptr,
+ layer.ffn_gate_shexp, nullptr,
+ layer.ffn_down_shexp, nullptr,
nullptr,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "mtp_ffn_shexp", il);
@@ -690,9 +690,8 @@ llama_model_qwen35moe::graph_mtp::graph_mtp(const llm_graph_params & params)
cb(cur, "mtp_shared_head_norm", -1);
ggml_tensor * head_w = layer.nextn.shared_head_head ? layer.nextn.shared_head_head : model.output;
- ggml_tensor * head_s = layer.nextn.shared_head_head ? layer.nextn.shared_head_head_s : model.output_s;
GGML_ASSERT(head_w && "QWEN35MOE MTP: missing LM head (nextn.shared_head_head or model.output)");
- cur = build_lora_mm(head_w, cur, head_s);
+ cur = build_lora_mm(head_w, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/qwen3moe.cpp b/src/models/qwen3moe.cpp
index fc57928f5..53e230477 100644
--- a/src/models/qwen3moe.cpp
+++ b/src/models/qwen3moe.cpp
@@ -109,7 +109,7 @@ llama_model_qwen3moe::graph::graph(const llm_graph_params & params) : llm_graph_
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -158,7 +158,7 @@ llama_model_qwen3moe::graph::graph(const llm_graph_params & params) : llm_graph_
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/qwen3next.cpp b/src/models/qwen3next.cpp
index 208aa49d2..2c093cd09 100644
--- a/src/models/qwen3next.cpp
+++ b/src/models/qwen3next.cpp
@@ -214,7 +214,7 @@ llama_model_qwen3next::graph::graph(const llm_graph_params & params) :
res->t_embd = cur;
// LM head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
@@ -283,7 +283,7 @@ ggml_tensor * llama_model_qwen3next::graph::build_layer_attn(
const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f / sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
cur = build_attn(inp,
- nullptr, nullptr, nullptr,
+ nullptr, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "attn_pregate", il);
@@ -295,7 +295,7 @@ ggml_tensor * llama_model_qwen3next::graph::build_layer_attn(
cur = ggml_mul(ctx0, cur, gate);
cb(cur, "attn_gated", il);
- cur = build_lora_mm(model.layers[il].wo, cur, model.layers[il].wo_s);
+ cur = build_lora_mm(model.layers[il].wo, cur);
cb(cur, "attn_output", il);
return cur;
@@ -314,11 +314,11 @@ std::pair<ggml_tensor *, ggml_tensor *> llama_model_qwen3next::graph::build_qkvz
if (model.layers[il].wqkv) {
// optimized path
- ggml_tensor * qkv_mixed = build_lora_mm(model.layers[il].wqkv, input, model.layers[il].wqkv_s);
+ ggml_tensor * qkv_mixed = build_lora_mm(model.layers[il].wqkv, input);
qkv_mixed = ggml_reshape_3d(ctx0, qkv_mixed, qkv_mixed->ne[0], n_seq_tokens, n_seqs);
cb(qkv_mixed, "linear_attn_qkv_mixed", il);
- ggml_tensor * z = build_lora_mm(model.layers[il].wqkv_gate, input, model.layers[il].wqkv_gate_s);
+ ggml_tensor * z = build_lora_mm(model.layers[il].wqkv_gate, input);
cb(z, "z", il);
return { qkv_mixed, z };
@@ -540,7 +540,7 @@ ggml_tensor * llama_model_qwen3next::graph::build_layer_attn_linear(
cb(final_output, "final_output", il);
// Output projection
- cur = build_lora_mm(model.layers[il].ssm_out, final_output, model.layers[il].ssm_out_s);
+ cur = build_lora_mm(model.layers[il].ssm_out, final_output);
cb(cur, "linear_attn_out", il);
return cur;
@@ -568,9 +568,9 @@ ggml_tensor * llama_model_qwen3next::graph::build_layer_ffn(ggml_tensor * cur, c
if (model.layers[il].ffn_up_shexp != nullptr) {
ggml_tensor * ffn_shexp =
build_ffn(cur,
- model.layers[il].ffn_up_shexp, NULL, model.layers[il].ffn_up_shexp_s,
- model.layers[il].ffn_gate_shexp, NULL, model.layers[il].ffn_gate_shexp_s,
- model.layers[il].ffn_down_shexp, NULL, model.layers[il].ffn_down_shexp_s,
+ model.layers[il].ffn_up_shexp, NULL,
+ model.layers[il].ffn_gate_shexp, NULL,
+ model.layers[il].ffn_down_shexp, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "ffn_shexp", il);
@@ -595,9 +595,9 @@ ggml_tensor * llama_model_qwen3next::graph::build_layer_ffn(ggml_tensor * cur, c
} else {
// Dense FFN branch (not currently used I believe)
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -664,7 +664,7 @@ llama_model_qwen3next::graph_mtp::graph_mtp(const llm_graph_params & params)
ggml_tensor * concat = ggml_concat(ctx0, e_norm, h_norm, /*dim=*/ 0);
cb(concat, "mtp_concat", il);
- ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat, layer.nextn.eh_proj_s);
+ ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat);
cb(cur, "mtp_eh_proj", il);
ggml_tensor * inpSA = cur;
@@ -704,7 +704,7 @@ llama_model_qwen3next::graph_mtp::graph_mtp(const llm_graph_params & params)
? 1.0f / sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
cur = build_attn(inp_attn,
- nullptr, nullptr, nullptr,
+ nullptr, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "mtp_attn_pregate", il);
@@ -718,7 +718,7 @@ llama_model_qwen3next::graph_mtp::graph_mtp(const llm_graph_params & params)
cb(gate, "mtp_gate", il);
cur = ggml_mul(ctx0, cur, ggml_sigmoid(ctx0, gate));
- cur = build_lora_mm(layer.wo, cur, layer.wo_s);
+ cur = build_lora_mm(layer.wo, cur);
cb(cur, "mtp_attn_out", il);
if (inp_out_ids) {
@@ -751,9 +751,9 @@ llama_model_qwen3next::graph_mtp::graph_mtp(const llm_graph_params & params)
if (layer.ffn_up_shexp != nullptr) {
ggml_tensor * ffn_shexp =
build_ffn(cur,
- layer.ffn_up_shexp, nullptr, layer.ffn_up_shexp_s,
- layer.ffn_gate_shexp, nullptr, layer.ffn_gate_shexp_s,
- layer.ffn_down_shexp, nullptr, layer.ffn_down_shexp_s,
+ layer.ffn_up_shexp, nullptr,
+ layer.ffn_gate_shexp, nullptr,
+ layer.ffn_down_shexp, nullptr,
nullptr,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "mtp_ffn_shexp", il);
@@ -784,9 +784,8 @@ llama_model_qwen3next::graph_mtp::graph_mtp(const llm_graph_params & params)
res->t_h_nextn = cur;
ggml_tensor * head_w = layer.nextn.shared_head_head ? layer.nextn.shared_head_head : model.output;
- ggml_tensor * head_s = layer.nextn.shared_head_head ? layer.nextn.shared_head_head_s : model.output_s;
GGML_ASSERT(head_w && "QWEN3NEXT MTP: missing LM head (nextn.shared_head_head or model.output)");
- cur = build_lora_mm(head_w, cur, head_s);
+ cur = build_lora_mm(head_w, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/qwen3vl.cpp b/src/models/qwen3vl.cpp
index 1bc56e7f4..3873cb5ea 100644
--- a/src/models/qwen3vl.cpp
+++ b/src/models/qwen3vl.cpp
@@ -116,7 +116,7 @@ llama_model_qwen3vl::graph::graph(const llm_graph_params & params) : llm_graph_c
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
@@ -135,9 +135,9 @@ llama_model_qwen3vl::graph::graph(const llm_graph_params & params) : llm_graph_c
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -167,7 +167,7 @@ llama_model_qwen3vl::graph::graph(const llm_graph_params & params) : llm_graph_c
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
int64_t n_vocab_in = model.tok_embd->ne[1];
int64_t n_vocab_out = model.output->ne[1];
diff --git a/src/models/qwen3vlmoe.cpp b/src/models/qwen3vlmoe.cpp
index c13f61990..0d13a1dc5 100644
--- a/src/models/qwen3vlmoe.cpp
+++ b/src/models/qwen3vlmoe.cpp
@@ -112,7 +112,7 @@ llama_model_qwen3vlmoe::graph::graph(const llm_graph_params & params) : llm_grap
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
@@ -170,7 +170,7 @@ llama_model_qwen3vlmoe::graph::graph(const llm_graph_params & params) : llm_grap
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/qwen4exp.cpp b/src/models/qwen4exp.cpp
index f77e6f509..6f672e9e2 100644
--- a/src/models/qwen4exp.cpp
+++ b/src/models/qwen4exp.cpp
@@ -574,7 +574,7 @@ llama_model_qwen4exp::graph::graph(const llm_graph_params & params) :
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
}
@@ -665,7 +665,7 @@ llama_model_qwen4exp::graph_mtp::graph_mtp(const llm_graph_params & params) :
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
}
@@ -693,11 +693,11 @@ std::pair<ggml_tensor *, ggml_tensor *> llama_model_qwen4exp::graph::build_qkvz(
return { qkv_mixed, z };
}
- ggml_tensor * qkv_mixed = build_lora_mm(model.layers[il].wqkv, input, model.layers[il].wqkv_s);
+ ggml_tensor * qkv_mixed = build_lora_mm(model.layers[il].wqkv, input);
qkv_mixed = ggml_reshape_3d(ctx0, qkv_mixed, qkv_mixed->ne[0], n_seq_tokens, n_seqs);
cb(qkv_mixed, "linear_attn_qkv_mixed", il);
- ggml_tensor * z = build_lora_mm(model.layers[il].wqkv_gate, input, model.layers[il].wqkv_gate_s);
+ ggml_tensor * z = build_lora_mm(model.layers[il].wqkv_gate, input);
cb(z, "z", il);
return { qkv_mixed, z };
@@ -1023,7 +1023,7 @@ ggml_tensor * llama_model_qwen4exp::graph::build_layer_attn(
ggml_tensor * sel = qsa ? build_qsa_sel(mctx_hyb, inp_kpool, cur, inp_pos, il) : nullptr;
// Qwen3Next uses a single Q projection that outputs query + gate
- ggml_tensor * Qcur_full = build_lora_mm(model.layers[il].wq, cur, model.layers[il].wq_s); // [ (n_embd_head * 2) * n_head, n_tokens ]
+ ggml_tensor * Qcur_full = build_lora_mm(model.layers[il].wq, cur); // [ (n_embd_head * 2) * n_head, n_tokens ]
cb(Qcur_full, "Qcur_full", il);
ggml_tensor * Qcur = ggml_view_3d(ctx0, Qcur_full, n_embd_head, n_head, n_tokens,
@@ -1034,10 +1034,10 @@ ggml_tensor * llama_model_qwen4exp::graph::build_layer_attn(
Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, nullptr, LLM_NORM_RMS, il);
cb(Qcur, "Qcur_normed", il);
- ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur, model.layers[il].wk_s);
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
cb(Kcur, "Kcur", il);
- ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur, model.layers[il].wv_s);
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
cb(Vcur, "Vcur", il);
Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
@@ -1067,7 +1067,7 @@ ggml_tensor * llama_model_qwen4exp::graph::build_layer_attn(
cur = build_attn_qsa(inp, Qcur, Kcur, Vcur, sel, inp_kpool->n_sel, kq_scale, il);
} else {
cur = build_attn(inp,
- nullptr, nullptr, nullptr,
+ nullptr, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
}
cb(cur, "attn_pregate", il);
@@ -1078,7 +1078,7 @@ ggml_tensor * llama_model_qwen4exp::graph::build_layer_attn(
cur = ggml_mul(ctx0, cur, gate_sigmoid);
cb(cur, "attn_gated", il);
- cur = build_lora_mm(model.layers[il].wo, cur, model.layers[il].wo_s);
+ cur = build_lora_mm(model.layers[il].wo, cur);
cb(cur, "attn_output", il);
return cur;
@@ -1119,10 +1119,10 @@ ggml_tensor * llama_model_qwen4exp::graph::build_layer_attn_linear(
alpha = ggml_view_3d(ctx0, ba, num_v_heads, n_seq_tokens, n_seqs,
ba->nb[1], ba->nb[1] * n_seq_tokens, ggml_row_size(ba->type, num_v_heads));
} else {
- beta = build_lora_mm(model.layers[il].ssm_beta, cur, model.layers[il].ssm_beta_s);
+ beta = build_lora_mm(model.layers[il].ssm_beta, cur);
beta = ggml_reshape_4d(ctx0, beta, 1, num_v_heads, n_seq_tokens, n_seqs);
- alpha = build_lora_mm(model.layers[il].ssm_alpha, cur, model.layers[il].ssm_alpha_s);
+ alpha = build_lora_mm(model.layers[il].ssm_alpha, cur);
alpha = ggml_reshape_3d(ctx0, alpha, num_v_heads, n_seq_tokens, n_seqs);
}
cb(beta, "beta", il);
@@ -1210,7 +1210,7 @@ ggml_tensor * llama_model_qwen4exp::graph::build_layer_attn_linear(
ggml_tensor * final_output = ggml_reshape_2d(ctx0, attn_out_norm, head_v_dim * num_v_heads, n_seq_tokens * n_seqs);
cb(final_output, "final_output", il);
- cur = build_lora_mm(model.layers[il].ssm_out, final_output, model.layers[il].ssm_out_s);
+ cur = build_lora_mm(model.layers[il].ssm_out, final_output);
cb(cur, "linear_attn_out", il);
return cur;
@@ -1237,9 +1237,9 @@ ggml_tensor * llama_model_qwen4exp::graph::build_layer_ffn(ggml_tensor * cur, co
if (model.layers[il].ffn_up_shexp != nullptr) {
ggml_tensor * ffn_shexp =
build_ffn(cur,
- model.layers[il].ffn_up_shexp, NULL, model.layers[il].ffn_up_shexp_s,
- model.layers[il].ffn_gate_shexp, NULL, model.layers[il].ffn_gate_shexp_s,
- model.layers[il].ffn_down_shexp, NULL, model.layers[il].ffn_down_shexp_s,
+ model.layers[il].ffn_up_shexp, NULL,
+ model.layers[il].ffn_gate_shexp, NULL,
+ model.layers[il].ffn_down_shexp, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "ffn_shexp", il);
diff --git a/src/models/raon1-mini.cpp b/src/models/raon1-mini.cpp
index e5b1671ec..70fbfa351 100644
--- a/src/models/raon1-mini.cpp
+++ b/src/models/raon1-mini.cpp
@@ -366,7 +366,7 @@ ggml_tensor * llama_model_raon1_mini::graph::build_mla_layer(
q = ggml_concat(ctx0, q_nope, q_extra, 0);
latent = ggml_reshape_3d(ctx0, latent, rank, 1, n_tokens);
ggml_tensor * k = ggml_concat(ctx0, latent, extra, 0);
- ggml_tensor * out = build_attn(inp_attn_k, nullptr, nullptr, nullptr, q, k, latent,
+ ggml_tensor * out = build_attn(inp_attn_k, nullptr, nullptr, q, k, latent,
nullptr, nullptr, layer.wv_b, 1.0f/sqrtf(nk), il);
out = ggml_mul(ctx0, out, ggml_sigmoid(ctx0, build_lora_mm(layer.wqkv_gate, cur)));
cb(out, "mla_gated", il);
diff --git a/src/models/refact.cpp b/src/models/refact.cpp
index 51bb0bad6..de379f7d7 100644
--- a/src/models/refact.cpp
+++ b/src/models/refact.cpp
@@ -108,7 +108,7 @@ llama_model_refact::graph::graph(const llm_graph_params & params) : llm_graph_co
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -126,9 +126,9 @@ llama_model_refact::graph::graph(const llm_graph_params & params) : llm_graph_co
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -151,7 +151,7 @@ llama_model_refact::graph::graph(const llm_graph_params & params) : llm_graph_co
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/ricursive.cpp b/src/models/ricursive.cpp
index 306cdd4dc..07ef4420e 100644
--- a/src/models/ricursive.cpp
+++ b/src/models/ricursive.cpp
@@ -142,8 +142,8 @@ llama_model_ricursive::graph::graph(const llm_graph_params & params) : llm_graph
ggml_tensor * attn_in = cur;
{
- ggml_tensor * Qcur = build_lora_mm(layer.wq, attn_in, layer.wq_s);
- ggml_tensor * Kcur = build_lora_mm(layer.wk, attn_in, layer.wk_s);
+ ggml_tensor * Qcur = build_lora_mm(layer.wq, attn_in);
+ ggml_tensor * Kcur = build_lora_mm(layer.wk, attn_in);
// v_in = lerp(x0, attn_norm(x), sigmoid(lambda)), lambda is learned per layer (modeling_ricursive.py:182-185)
ggml_tensor * v_in = attn_in;
@@ -152,7 +152,7 @@ llama_model_ricursive::graph::graph(const llm_graph_params & params) : llm_graph
v_in = ggml_add(ctx0, inp_vres, ggml_mul(ctx0, ggml_sub(ctx0, attn_in, inp_vres), mix));
cb(v_in, "vres_in", il);
}
- ggml_tensor * Vcur = build_lora_mm(layer.wv, v_in, layer.wv_s);
+ ggml_tensor * Vcur = build_lora_mm(layer.wv, v_in);
Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
@@ -170,18 +170,18 @@ llama_model_ricursive::graph::graph(const llm_graph_params & params) : llm_graph
cb(Kcur, "Kcur", il);
cb(Vcur, "Vcur", il);
- cur = build_attn(inp_attn, NULL, NULL, NULL, Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
+ cur = build_attn(inp_attn, NULL, NULL, Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "attn_out", il);
if (layer.wqkv_gate) {
- ggml_tensor * gate = build_lora_mm(layer.wqkv_gate, attn_in, layer.wqkv_gate_s);
+ ggml_tensor * gate = build_lora_mm(layer.wqkv_gate, attn_in);
gate = ggml_sigmoid(ctx0, gate);
cb(gate, "attn_gate", il);
cur = ggml_mul(ctx0, cur, gate);
cb(cur, "attn_gated", il);
}
- cur = build_lora_mm(layer.wo, cur, layer.wo_s);
+ cur = build_lora_mm(layer.wo, cur);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -199,13 +199,13 @@ llama_model_ricursive::graph::graph(const llm_graph_params & params) : llm_graph
cb(cur, "ffn_norm", il);
{
- ggml_tensor * gate = build_lora_mm(layer.ffn_gate, cur, layer.ffn_gate_s);
- ggml_tensor * up = build_lora_mm(layer.ffn_up, cur, layer.ffn_up_s);
+ ggml_tensor * gate = build_lora_mm(layer.ffn_gate, cur);
+ ggml_tensor * up = build_lora_mm(layer.ffn_up, cur);
cur = hparams.swiglu_clamp[il] > 0.0f
? ggml_swiglu_clamp(ctx0, gate, up, hparams.swiglu_clamp[il])
: ggml_swiglu_split(ctx0, gate, up);
cb(cur, "ffn_swiglu", il);
- cur = build_lora_mm(layer.ffn_down, cur, layer.ffn_down_s);
+ cur = build_lora_mm(layer.ffn_down, cur);
cb(cur, "ffn_out", il);
}
@@ -223,7 +223,7 @@ llama_model_ricursive::graph::graph(const llm_graph_params & params) : llm_graph
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
}
diff --git a/src/models/rnd1.cpp b/src/models/rnd1.cpp
index 8ea8d2364..cf16a5071 100644
--- a/src/models/rnd1.cpp
+++ b/src/models/rnd1.cpp
@@ -111,7 +111,7 @@ llama_model_rnd1::graph::graph(const llm_graph_params & params) : llm_graph_cont
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -160,7 +160,7 @@ llama_model_rnd1::graph::graph(const llm_graph_params & params) : llm_graph_cont
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/rwkv6-base.cpp b/src/models/rwkv6-base.cpp
index 68efec666..7eb917d1d 100644
--- a/src/models/rwkv6-base.cpp
+++ b/src/models/rwkv6-base.cpp
@@ -55,13 +55,13 @@ ggml_tensor * llm_build_rwkv6_base::build_rwkv6_time_mix(llm_graph_input_rs * in
ggml_tensor * xxx = ggml_add(ctx0, ggml_mul(ctx0, sx, layer.time_mix_lerp_x), cur);
- xxx = ggml_reshape_4d(ctx0, ggml_tanh(ctx0, ggml_mul_mat(ctx0, layer.time_mix_w1, xxx)),
+ xxx = ggml_reshape_4d(ctx0, ggml_tanh(ctx0, build_lora_mm(layer.time_mix_w1, xxx)),
layer.time_mix_w1->ne[1] / 5, 1, 5, n_tokens);
xxx = ggml_cont(ctx0, ggml_permute(ctx0, xxx, 0, 1, 3, 2));
- xxx = ggml_mul_mat(
- ctx0, ggml_reshape_4d(ctx0, layer.time_mix_w2, layer.time_mix_w2->ne[0], layer.time_mix_w2->ne[1], 1, 5), xxx);
+ xxx = build_lora_mm(
+ ggml_reshape_4d(ctx0, layer.time_mix_w2, layer.time_mix_w2->ne[0], layer.time_mix_w2->ne[1], 1, 5), xxx);
ggml_tensor *xw, *xk, *xv, *xr, *xg;
if (layer.time_mix_lerp_fused) {
@@ -119,7 +119,7 @@ ggml_tensor * llm_build_rwkv6_base::build_rwkv6_time_mix(llm_graph_input_rs * in
r = ggml_reshape_3d(ctx0, r, head_size, n_head, n_tokens);
ggml_tensor * w =
- ggml_mul_mat(ctx0, layer.time_mix_decay_w2, ggml_tanh(ctx0, ggml_mul_mat(ctx0, layer.time_mix_decay_w1, xw)));
+ build_lora_mm(layer.time_mix_decay_w2, ggml_tanh(ctx0, build_lora_mm(layer.time_mix_decay_w1, xw)));
w = ggml_add(ctx0, w, layer.time_mix_decay);
w = ggml_exp(ctx0, ggml_neg(ctx0, ggml_exp(ctx0, w)));
diff --git a/src/models/rwkv6.cpp b/src/models/rwkv6.cpp
index 0b17ae40a..2a6ac3769 100644
--- a/src/models/rwkv6.cpp
+++ b/src/models/rwkv6.cpp
@@ -178,7 +178,7 @@ llama_model_rwkv6::graph::graph(const llm_graph_params & params) :
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/rwkv6qwen2.cpp b/src/models/rwkv6qwen2.cpp
index e81c0132f..9139eda56 100644
--- a/src/models/rwkv6qwen2.cpp
+++ b/src/models/rwkv6qwen2.cpp
@@ -136,9 +136,9 @@ llama_model_rwkv6qwen2::graph::graph(const llm_graph_params & params) : llm_buil
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -158,7 +158,7 @@ llama_model_rwkv6qwen2::graph::graph(const llm_graph_params & params) : llm_buil
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/rwkv7-base.cpp b/src/models/rwkv7-base.cpp
index 3300822d2..fdcead7c0 100644
--- a/src/models/rwkv7-base.cpp
+++ b/src/models/rwkv7-base.cpp
@@ -64,7 +64,7 @@ ggml_tensor * llm_build_rwkv7_base::build_rwkv7_time_mix(llm_graph_input_rs * in
ggml_tensor * r = build_lora_mm(layer.time_mix_receptance, xr);
ggml_tensor * w = ggml_add(
- ctx0, ggml_mul_mat(ctx0, layer.time_mix_w2, ggml_tanh(ctx0, ggml_mul_mat(ctx0, layer.time_mix_w1, xw))),
+ ctx0, build_lora_mm(layer.time_mix_w2, ggml_tanh(ctx0, build_lora_mm(layer.time_mix_w1, xw))),
layer.time_mix_w0);
w = ggml_exp(ctx0, ggml_scale(ctx0, ggml_sigmoid(ctx0, w), -0.606531));
@@ -77,16 +77,16 @@ ggml_tensor * llm_build_rwkv7_base::build_rwkv7_time_mix(llm_graph_input_rs * in
v = ggml_add(ctx0, v,
ggml_mul(ctx0, ggml_sub(ctx0, first_layer_value, v),
ggml_sigmoid(ctx0, ggml_add(ctx0,
- ggml_mul_mat(ctx0, layer.time_mix_v2,
- ggml_mul_mat(ctx0, layer.time_mix_v1, xv)),
+ build_lora_mm(layer.time_mix_v2,
+ build_lora_mm(layer.time_mix_v1, xv)),
layer.time_mix_v0))));
}
ggml_tensor * g = nullptr;
if (layer.time_mix_g1 && layer.time_mix_g2) {
- g = ggml_mul_mat(ctx0, layer.time_mix_g2, ggml_sigmoid(ctx0, ggml_mul_mat(ctx0, layer.time_mix_g1, xg)));
+ g = build_lora_mm(layer.time_mix_g2, ggml_sigmoid(ctx0, build_lora_mm(layer.time_mix_g1, xg)));
}
ggml_tensor * a = ggml_sigmoid(
- ctx0, ggml_add(ctx0, ggml_mul_mat(ctx0, layer.time_mix_a2, ggml_mul_mat(ctx0, layer.time_mix_a1, xa)),
+ ctx0, ggml_add(ctx0, build_lora_mm(layer.time_mix_a2, build_lora_mm(layer.time_mix_a1, xa)),
layer.time_mix_a0));
ggml_tensor * kk = ggml_reshape_3d(ctx0, ggml_mul(ctx0, k, layer.time_mix_k_k), head_size, head_count, n_tokens);
diff --git a/src/models/rwkv7.cpp b/src/models/rwkv7.cpp
index b1d525940..e630a694d 100644
--- a/src/models/rwkv7.cpp
+++ b/src/models/rwkv7.cpp
@@ -204,7 +204,7 @@ llama_model_rwkv7::graph::graph(const llm_graph_params & params) :
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/seed-oss.cpp b/src/models/seed-oss.cpp
index c7940dc80..85cdbbf06 100644
--- a/src/models/seed-oss.cpp
+++ b/src/models/seed-oss.cpp
@@ -89,7 +89,7 @@ llama_model_seed_oss::graph::graph(const llm_graph_params & params) : llm_graph_
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "attn_out", il);
}
@@ -107,9 +107,9 @@ llama_model_seed_oss::graph::graph(const llm_graph_params & params) : llm_graph_
cb(cur, "attn_post_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -133,7 +133,7 @@ llama_model_seed_oss::graph::graph(const llm_graph_params & params) : llm_graph_
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/sensenova-u1.cpp b/src/models/sensenova-u1.cpp
index 7c8025703..cdd974d72 100644
--- a/src/models/sensenova-u1.cpp
+++ b/src/models/sensenova-u1.cpp
@@ -147,7 +147,7 @@ llama_model_sensenova_u1::graph::graph(const llm_graph_params & params) : llm_gr
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -162,9 +162,9 @@ llama_model_sensenova_u1::graph::graph(const llm_graph_params & params) : llm_gr
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, model.layers[il].ffn_up_s,
- model.layers[il].ffn_gate, NULL, model.layers[il].ffn_gate_s,
- model.layers[il].ffn_down, NULL, model.layers[il].ffn_down_s,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -185,7 +185,7 @@ llama_model_sensenova_u1::graph::graph(const llm_graph_params & params) : llm_gr
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/smallthinker.cpp b/src/models/smallthinker.cpp
index 2c183e33c..76f61f021 100644
--- a/src/models/smallthinker.cpp
+++ b/src/models/smallthinker.cpp
@@ -123,7 +123,7 @@ llama_model_smallthinker::graph<iswa>::graph(const llm_graph_params & params) :
cb(Kcur, "Kcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -169,7 +169,7 @@ llama_model_smallthinker::graph<iswa>::graph(const llm_graph_params & params) :
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
}
diff --git a/src/models/smollm3.cpp b/src/models/smollm3.cpp
index fa9e9c539..dcb52b0a3 100644
--- a/src/models/smollm3.cpp
+++ b/src/models/smollm3.cpp
@@ -88,7 +88,7 @@ llama_model_smollm3::graph::graph(const llm_graph_params & params) : llm_graph_c
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "attn_out", il);
}
@@ -107,9 +107,9 @@ llama_model_smollm3::graph::graph(const llm_graph_params & params) : llm_graph_c
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
- model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
- model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b,
+ model.layers[il].ffn_gate, model.layers[il].ffn_gate_b,
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -133,7 +133,7 @@ llama_model_smollm3::graph::graph(const llm_graph_params & params) : llm_graph_c
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/spark2-5.cpp b/src/models/spark2-5.cpp
index e90184d8e..c9076402a 100644
--- a/src/models/spark2-5.cpp
+++ b/src/models/spark2-5.cpp
@@ -83,7 +83,7 @@ llama_model_spark2_5::graph::graph(const llm_graph_params & params) : llm_graph_
cb(Kcur, "Kcur_rope", il);
cur = build_attn(inp_attn,
- nullptr, nullptr, nullptr,
+ nullptr, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "attn_out", il);
@@ -98,7 +98,7 @@ llama_model_spark2_5::graph::graph(const llm_graph_params & params) : llm_graph_
cur = ggml_reshape_2d(ctx0, cur, n_embd_head * n_head_i, n_tokens_i);
cb(cur, "attn_gated", il);
- cur = build_lora_mm(model.layers[il].wo, cur, model.layers[il].wo_s);
+ cur = build_lora_mm(model.layers[il].wo, cur);
cb(cur, "attn_out_proj", il);
if (il == n_layer - 1 && inp_out_ids) {
@@ -113,9 +113,9 @@ llama_model_spark2_5::graph::graph(const llm_graph_params & params) : llm_graph_
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, nullptr, nullptr,
- model.layers[il].ffn_gate, nullptr, nullptr,
- model.layers[il].ffn_down, nullptr, nullptr,
+ model.layers[il].ffn_up, nullptr,
+ model.layers[il].ffn_gate, nullptr,
+ model.layers[il].ffn_down, nullptr,
nullptr,
LLM_FFN_GELU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
diff --git a/src/models/stablelm.cpp b/src/models/stablelm.cpp
index 5aec7beee..fe2b87493 100644
--- a/src/models/stablelm.cpp
+++ b/src/models/stablelm.cpp
@@ -104,7 +104,7 @@ llama_model_stablelm::graph::graph(const llm_graph_params & params) : llm_graph_
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -128,9 +128,9 @@ llama_model_stablelm::graph::graph(const llm_graph_params & params) : llm_graph_
cur = inpSA;
}
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -154,7 +154,7 @@ llama_model_stablelm::graph::graph(const llm_graph_params & params) : llm_graph_
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/starcoder.cpp b/src/models/starcoder.cpp
index 7d73aa01b..700ab0ce1 100644
--- a/src/models/starcoder.cpp
+++ b/src/models/starcoder.cpp
@@ -93,7 +93,7 @@ llama_model_starcoder::graph::graph(const llm_graph_params & params) : llm_graph
n_embd_head, n_head, n_head_kv, il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -113,9 +113,9 @@ llama_model_starcoder::graph::graph(const llm_graph_params & params) : llm_graph
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
- NULL, NULL, NULL,
- model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b,
+ NULL, NULL,
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b,
NULL,
LLM_FFN_GELU, LLM_FFN_SEQ, il);
cb(cur, "ffn_out", il);
@@ -136,7 +136,7 @@ llama_model_starcoder::graph::graph(const llm_graph_params & params) : llm_graph
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/starcoder2.cpp b/src/models/starcoder2.cpp
index 2b670d562..9b3a14868 100644
--- a/src/models/starcoder2.cpp
+++ b/src/models/starcoder2.cpp
@@ -97,7 +97,7 @@ llama_model_starcoder2::graph::graph(const llm_graph_params & params) : llm_grap
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -115,9 +115,9 @@ llama_model_starcoder2::graph::graph(const llm_graph_params & params) : llm_grap
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
- NULL, NULL, NULL,
- model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b,
+ NULL, NULL,
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b,
NULL,
LLM_FFN_GELU, LLM_FFN_SEQ, il);
cb(cur, "ffn_out", il);
@@ -140,7 +140,7 @@ llama_model_starcoder2::graph::graph(const llm_graph_params & params) : llm_grap
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/step35.cpp b/src/models/step35.cpp
index 5bed70163..b2692d6a7 100644
--- a/src/models/step35.cpp
+++ b/src/models/step35.cpp
@@ -246,7 +246,7 @@ llama_model_step35::graph::graph(const llm_graph_params & params) : llm_graph_co
const float kq_scale = 1.0f / sqrtf(float(n_embd_head_k));
ggml_tensor * attn_out = build_attn(inp_attn,
- nullptr, nullptr, nullptr,
+ nullptr, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(attn_out, "attn_out", il);
// head-wise attention gate: sigmoid(g_proj(x)) in torch
@@ -270,7 +270,7 @@ llama_model_step35::graph::graph(const llm_graph_params & params) : llm_graph_co
}
// output projection
- cur = build_lora_mm(model.layers[il].wo, attn_out, model.layers[il].wo_s);
+ cur = build_lora_mm(model.layers[il].wo, attn_out);
cb(cur, "attn_proj", il);
}
@@ -289,9 +289,9 @@ llama_model_step35::graph::graph(const llm_graph_params & params) : llm_graph_co
if (model.layers[il].ffn_gate_inp == nullptr) {
// dense MLP
cur = build_ffn(cur,
- model.layers[il].ffn_up, model.layers[il].ffn_up_b, nullptr,
- model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, nullptr,
- model.layers[il].ffn_down, model.layers[il].ffn_down_b, nullptr,
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b,
+ model.layers[il].ffn_gate, model.layers[il].ffn_gate_b,
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b,
nullptr,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -312,9 +312,9 @@ llama_model_step35::graph::graph(const llm_graph_params & params) : llm_graph_co
// shared expert MLP (always added on MoE layers in Step35)
ggml_tensor * sh_out = build_ffn(cur,
- model.layers[il].ffn_up_shexp, nullptr, nullptr,
- model.layers[il].ffn_gate_shexp, nullptr, nullptr,
- model.layers[il].ffn_down_shexp, nullptr, nullptr,
+ model.layers[il].ffn_up_shexp, nullptr,
+ model.layers[il].ffn_gate_shexp, nullptr,
+ model.layers[il].ffn_down_shexp, nullptr,
nullptr,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(sh_out, "ffn_shared_out", il);
@@ -344,7 +344,7 @@ llama_model_step35::graph::graph(const llm_graph_params & params) : llm_graph_co
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
}
@@ -439,7 +439,7 @@ llama_model_step35::graph_mtp::graph_mtp(const llm_graph_params & params)
const float kq_scale = 1.0f / sqrtf(float(n_embd_head_k));
ggml_tensor * attn_out = build_attn(inp_attn,
- nullptr, nullptr, nullptr,
+ nullptr, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(attn_out, "mtp_attn_out", il);
@@ -462,7 +462,7 @@ llama_model_step35::graph_mtp::graph_mtp(const llm_graph_params & params)
cb(attn_out, "mtp_attn_gated", il);
}
- cur = build_lora_mm(layer.wo, attn_out, layer.wo_s);
+ cur = build_lora_mm(layer.wo, attn_out);
cb(cur, "mtp_attn_proj", il);
cur = ggml_add(ctx0, cur, inpSA);
@@ -475,9 +475,9 @@ llama_model_step35::graph_mtp::graph_mtp(const llm_graph_params & params)
// FFN: dense MLP or MoE (mirrors trunk path)
if (layer.ffn_gate_inp == nullptr) {
cur = build_ffn(cur,
- layer.ffn_up, layer.ffn_up_b, nullptr,
- layer.ffn_gate, layer.ffn_gate_b, nullptr,
- layer.ffn_down, layer.ffn_down_b, nullptr,
+ layer.ffn_up, layer.ffn_up_b,
+ layer.ffn_gate, layer.ffn_gate_b,
+ layer.ffn_down, layer.ffn_down_b,
nullptr,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "mtp_ffn_out", il);
@@ -496,9 +496,9 @@ llama_model_step35::graph_mtp::graph_mtp(const llm_graph_params & params)
cb(moe_out, "mtp_ffn_moe_out", il);
ggml_tensor * sh_out = build_ffn(cur,
- layer.ffn_up_shexp, nullptr, nullptr,
- layer.ffn_gate_shexp, nullptr, nullptr,
- layer.ffn_down_shexp, nullptr, nullptr,
+ layer.ffn_up_shexp, nullptr,
+ layer.ffn_gate_shexp, nullptr,
+ layer.ffn_down_shexp, nullptr,
nullptr,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(sh_out, "mtp_ffn_shared_out", il);
diff --git a/src/models/t5.cpp b/src/models/t5.cpp
index 8a0aac1c8..86b14a1d9 100644
--- a/src/models/t5.cpp
+++ b/src/models/t5.cpp
@@ -147,7 +147,7 @@ llama_model_t5::graph<false>::graph(const llm_graph_params & params) : llm_graph
ggml_tensor * kq_b = build_pos_bias(pos_bucket_dec, attn_rel_b);
cur = build_attn(inp_attn_self,
- model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
+ model.layers[il].wo, model.layers[il].wo_b,
Qcur, Kcur, Vcur, kq_b, nullptr, nullptr, 1.0f, il);
cb(cur, "kqv_out", il);
}
@@ -178,7 +178,7 @@ llama_model_t5::graph<false>::graph(const llm_graph_params & params) : llm_graph
Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_outputs_enc);
cur = build_attn(inp_attn_cross,
- model.layers[il].wo_cross, nullptr, nullptr,
+ model.layers[il].wo_cross, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
cb(cur, "kqv_out", il);
@@ -224,9 +224,9 @@ llama_model_t5::graph<false>::graph(const llm_graph_params & params) : llm_graph
// T5 uses relu, flan-T5 uses gelu-gated
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
model.layers[il].ffn_gate ? LLM_FFN_GELU : LLM_FFN_RELU,
model.layers[il].ffn_gate ? LLM_FFN_PAR : LLM_FFN_SEQ,
@@ -253,7 +253,7 @@ llama_model_t5::graph<false>::graph(const llm_graph_params & params) : llm_graph
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
@@ -304,7 +304,7 @@ llama_model_t5::graph<true>::graph(const llm_graph_params & params) : llm_graph_
ggml_tensor * kq_b = build_pos_bias(pos_bucket_enc, attn_rel_b);
cur = build_attn(inp_attn,
- model.layers[il].wo_enc, nullptr, nullptr,
+ model.layers[il].wo_enc, nullptr,
Qcur, Kcur, Vcur, kq_b, nullptr, nullptr, 1.0f, il);
cb(cur, "kqv_out", il);
}
@@ -324,9 +324,9 @@ llama_model_t5::graph<true>::graph(const llm_graph_params & params) : llm_graph_
// T5 uses relu, flan-T5 uses gelu-gated
cur = build_ffn(cur,
- model.layers[il].ffn_up_enc, NULL, NULL,
- model.layers[il].ffn_gate_enc, NULL, NULL,
- model.layers[il].ffn_down_enc, NULL, NULL,
+ model.layers[il].ffn_up_enc, NULL,
+ model.layers[il].ffn_gate_enc, NULL,
+ model.layers[il].ffn_down_enc, NULL,
NULL,
model.layers[il].ffn_gate_enc ? LLM_FFN_GELU : LLM_FFN_RELU,
model.layers[il].ffn_gate_enc ? LLM_FFN_PAR : LLM_FFN_SEQ,
diff --git a/src/models/talkie.cpp b/src/models/talkie.cpp
index 5bf18f066..afb6e6772 100644
--- a/src/models/talkie.cpp
+++ b/src/models/talkie.cpp
@@ -86,7 +86,7 @@ llama_model_talkie::graph::graph(const llm_graph_params & params) : llm_graph_co
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, nullptr, model.layers[il].wo_s,
+ model.layers[il].wo, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "attn_out", il);
}
@@ -104,9 +104,9 @@ llama_model_talkie::graph::graph(const llm_graph_params & params) : llm_graph_co
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, nullptr, nullptr,
- model.layers[il].ffn_gate, nullptr, nullptr,
- model.layers[il].ffn_down, nullptr, model.layers[il].ffn_down_s,
+ model.layers[il].ffn_up, nullptr,
+ model.layers[il].ffn_gate, nullptr,
+ model.layers[il].ffn_down, nullptr,
nullptr,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
diff --git a/src/models/tiny-voca.cpp b/src/models/tiny-voca.cpp
index c7541f5e9..b4969f3c6 100644
--- a/src/models/tiny-voca.cpp
+++ b/src/models/tiny-voca.cpp
@@ -120,7 +120,7 @@ llama_model_tiny_voca::graph::graph(const llm_graph_params & params) : llm_graph
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- layer.wo, layer.wo_b, layer.wo_s,
+ layer.wo, layer.wo_b,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
cb(cur, "kqv_out", il);
}
@@ -144,9 +144,9 @@ llama_model_tiny_voca::graph::graph(const llm_graph_params & params) : llm_graph
}
cur = build_ffn(cur,
- layer.ffn_up, layer.ffn_up_b, NULL,
- NULL, NULL, NULL,
- layer.ffn_down, layer.ffn_down_b, NULL, NULL,
+ layer.ffn_up, layer.ffn_up_b,
+ NULL, NULL,
+ layer.ffn_down, layer.ffn_down_b, NULL,
LLM_FFN_GELU_ERF, LLM_FFN_SEQ, il);
cb(cur, "ffn_out", il);
diff --git a/src/models/vllm-decision2.cpp b/src/models/vllm-decision2.cpp
index 692534425..a140a5297 100644
--- a/src/models/vllm-decision2.cpp
+++ b/src/models/vllm-decision2.cpp
@@ -169,15 +169,15 @@ ggml_tensor * llama_model_vllm_decision2::graph::build_head(ggml_tensor * hidden
query = build_norm(query, model.query_norm, model.query_norm_b, LLM_NORM, -1);
// scaled dot product of the option key and the query
- ggml_tensor * k = ggml_mul_mat(ctx0, model.key, options); // [n_embd_h, n_options]
- ggml_tensor * q = ggml_mul_mat(ctx0, model.query, query); // [n_embd_h, 1]
+ ggml_tensor * k = build_lora_mm(model.key, options); // [n_embd_h, n_options]
+ ggml_tensor * q = build_lora_mm(model.query, query); // [n_embd_h, 1]
ggml_tensor * bilinear = ggml_sum_rows(ctx0, ggml_mul(ctx0, k, q));
bilinear = ggml_scale(ctx0, bilinear, 1.0f / sqrtf((float) k->ne[0])); // [1, n_options]
- ggml_tensor * mlp = ggml_add(ctx0, ggml_mul_mat(ctx0, model.option_mlp, options), model.option_mlp_b);
- mlp = ggml_add(ctx0, mlp, ggml_mul_mat(ctx0, model.query_mlp, query));
+ ggml_tensor * mlp = ggml_add(ctx0, build_lora_mm(model.option_mlp, options), model.option_mlp_b);
+ mlp = ggml_add(ctx0, mlp, build_lora_mm(model.query_mlp, query));
mlp = ggml_gelu_erf(ctx0, mlp);
- ggml_tensor * nonlinear = ggml_mul_mat(ctx0, model.scorer_out, mlp); // [1, n_options]
+ ggml_tensor * nonlinear = build_lora_mm(model.scorer_out, mlp); // [1, n_options]
ggml_tensor * scores = ggml_add(ctx0, bilinear, nonlinear);
cb(scores, "decision_scores", -1);
diff --git a/src/models/wavtokenizer-dec.cpp b/src/models/wavtokenizer-dec.cpp
index ac3094f9c..e543af367 100644
--- a/src/models/wavtokenizer-dec.cpp
+++ b/src/models/wavtokenizer-dec.cpp
@@ -231,9 +231,9 @@ llama_model_wavtokenizer_dec::graph::graph(const llm_graph_params & params) : ll
LLM_NORM, -1);
cur = build_ffn(cur,
- layer.pw1, layer.pw1_b, NULL,
- NULL, NULL, NULL,
- layer.pw2, layer.pw2_b, NULL,
+ layer.pw1, layer.pw1_b,
+ NULL, NULL,
+ layer.pw2, layer.pw2_b,
NULL,
LLM_FFN_GELU, LLM_FFN_SEQ, il);
@@ -253,7 +253,7 @@ llama_model_wavtokenizer_dec::graph::graph(const llm_graph_params & params) : ll
LLM_NORM, -1);
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cur = ggml_add(ctx0, cur, model.output_b);
diff --git a/src/models/xing4-0.cpp b/src/models/xing4-0.cpp
index f405fcc9c..e19c35272 100644
--- a/src/models/xing4-0.cpp
+++ b/src/models/xing4-0.cpp
@@ -257,7 +257,7 @@ llama_model_xing4_0::graph_mtp::graph_mtp(const llama_model & model, const llm_g
ggml_tensor * concat = ggml_concat(ctx0, e_norm, h_norm, 0);
cb(concat, "mtp_concat", il);
- ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat, layer.nextn.eh_proj_s);
+ ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat);
cb(cur, "mtp_eh_proj", il);
ggml_tensor * inpSA = cur;
@@ -265,13 +265,13 @@ llama_model_xing4_0::graph_mtp::graph_mtp(const llama_model & model, const llm_g
cur = build_norm(cur, layer.attn_norm, nullptr, LLM_NORM_RMS, il);
cb(cur, "mtp_attn_norm", il);
- ggml_tensor * q = ggml_mul_mat(ctx0, layer.wq_a, cur);
+ ggml_tensor * q = build_lora_mm(layer.wq_a, cur);
cb(q, "mtp_q_a", il);
q = build_norm(q, layer.attn_q_a_norm, nullptr, LLM_NORM_RMS, il);
cb(q, "mtp_q_a_norm", il);
- q = ggml_mul_mat(ctx0, layer.wq_b, q);
+ q = build_lora_mm(layer.wq_b, q);
cb(q, "mtp_q_b", il);
ggml_tensor * q_nope =
@@ -287,7 +287,7 @@ llama_model_xing4_0::graph_mtp::graph_mtp(const llama_model & model, const llm_g
ggml_row_size(q->type, n_embd_head_qk_nope));
cb(q_pe, "mtp_q_pe", il);
- ggml_tensor * kv_cmpr_pe = ggml_mul_mat(ctx0, layer.wkv_a_mqa, cur);
+ ggml_tensor * kv_cmpr_pe = build_lora_mm(layer.wkv_a_mqa, cur);
cb(kv_cmpr_pe, "mtp_kv_cmpr_pe", il);
ggml_tensor * kv_cmpr =
@@ -325,7 +325,7 @@ llama_model_xing4_0::graph_mtp::graph_mtp(const llama_model & model, const llm_g
q_nope = ggml_permute(ctx0, q_nope, 0, 2, 1, 3);
cb(q_nope, "mtp_q_nope_perm", il);
- ggml_tensor * q_nope_absorbed = ggml_mul_mat(ctx0, layer.wk_b, q_nope);
+ ggml_tensor * q_nope_absorbed = build_lora_mm(layer.wk_b, q_nope);
cb(q_nope_absorbed, "mtp_q_nope_absorbed", il);
q_nope_absorbed = ggml_permute(ctx0, q_nope_absorbed, 0, 2, 1, 3);
@@ -344,7 +344,7 @@ llama_model_xing4_0::graph_mtp::graph_mtp(const llama_model & model, const llm_g
cb(Vcur, "mtp_Vcur", il);
cur = build_attn(inp_attn_k,
- layer.wo, nullptr, layer.wo_s,
+ layer.wo, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, layer.wv_b, kq_scale, il);
cb(cur, "mtp_attn_out", il);
@@ -370,9 +370,9 @@ llama_model_xing4_0::graph_mtp::graph_mtp(const llama_model & model, const llm_g
cb(moe_out, "mtp_ffn_moe_out", il);
ggml_tensor * ffn_shexp = build_ffn(cur,
- layer.ffn_up_shexp, nullptr, nullptr,
- layer.ffn_gate_shexp, nullptr, nullptr,
- layer.ffn_down_shexp, nullptr, nullptr,
+ layer.ffn_up_shexp, nullptr,
+ layer.ffn_gate_shexp, nullptr,
+ layer.ffn_down_shexp, nullptr,
nullptr, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "mtp_ffn_shexp", il);
@@ -400,13 +400,9 @@ llama_model_xing4_0::graph_mtp::graph_mtp(const llama_model & model, const llm_g
? layer.nextn.shared_head_head
: model.output;
- ggml_tensor * head_s = layer.nextn.shared_head_head
- ? layer.nextn.shared_head_head_s
- : model.output_s;
-
GGML_ASSERT(head_w && "xing4_0 MTP: missing LM head (nextn.shared_head_head or model.output)");
- cur = build_lora_mm(head_w, cur, head_s);
+ cur = build_lora_mm(head_w, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
@@ -496,7 +492,7 @@ ggml_tensor * llama_model_xing4_0::graph::build_hc_pre(
ggml_tensor * flat = ggml_reshape_2d(ctx0, x, hc_dim, nt);
ggml_tensor * flat_norm = ggml_rms_norm(ctx0, flat, norm_rms_eps);
- ggml_tensor * mixes = ggml_mul_mat(ctx0, hc_fn, flat_norm);
+ ggml_tensor * mixes = build_lora_mm(hc_fn, flat_norm);
cb(mixes, "hc_mixes", il);
ggml_tensor * scale_pre = xing4_0_view_1d(ctx0, hc_scale, 1, 0);
@@ -643,13 +639,13 @@ llama_model_xing4_0::graph::graph(const llama_model & model, const llm_graph_par
cb(cur, "attn_norm", il);
// MLA self-attention (identical to deepseek2)
- ggml_tensor * q = ggml_mul_mat(ctx0, layer.wq_a, cur);
+ ggml_tensor * q = build_lora_mm(layer.wq_a, cur);
cb(q, "q_a", il);
q = build_norm(q, layer.attn_q_a_norm, nullptr, LLM_NORM_RMS, il);
cb(q, "q_anorm", il);
- q = ggml_mul_mat(ctx0, layer.wq_b, q);
+ q = build_lora_mm(layer.wq_b, q);
cb(q, "q_b", il);
// split into {n_embd_head_qk_nope, n_head, n_tokens}
@@ -664,7 +660,7 @@ llama_model_xing4_0::graph::graph(const llama_model & model, const llm_graph_par
ggml_row_size(q->type, n_embd_head_k) * n_head, ggml_row_size(q->type, n_embd_head_qk_nope));
cb(q_pe, "q_pe_in", il);
- ggml_tensor * kv_cmpr_pe = ggml_mul_mat(ctx0, layer.wkv_a_mqa, cur);
+ ggml_tensor * kv_cmpr_pe = build_lora_mm(layer.wkv_a_mqa, cur);
cb(kv_cmpr_pe, "kv_cmpr_pe", il);
// split into {kv_lora_rank, n_tokens}
@@ -694,7 +690,7 @@ llama_model_xing4_0::graph::graph(const llama_model & model, const llm_graph_par
cb(q_nope, "q_nope_perm", il);
// {n_embd_head_qk_nope, kv_lora_rank, n_head} x {n_embd_head_qk_nope, n_tokens, n_head}
- ggml_tensor * q_nope_absorbed = ggml_mul_mat(ctx0, layer.wk_b, q_nope);
+ ggml_tensor * q_nope_absorbed = build_lora_mm(layer.wk_b, q_nope);
cb(q_nope_absorbed, "q_nope_absorbed", il);
// {kv_lora_rank, n_head, n_tokens}
@@ -719,7 +715,7 @@ llama_model_xing4_0::graph::graph(const llama_model & model, const llm_graph_par
// note: MLA with the absorption optimization converts into MQA (ie: GQA with 1 group)
cur = build_attn(inp_attn_k,
- layer.wo, nullptr, layer.wo_s,
+ layer.wo, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, layer.wv_b, kq_scale, il);
cb(cur, "attn_out", il);
@@ -754,9 +750,9 @@ llama_model_xing4_0::graph::graph(const llama_model & model, const llm_graph_par
if ((uint32_t) il < hparams.n_layer_dense_lead) {
cur = build_ffn(cur,
- layer.ffn_up, nullptr, nullptr,
- layer.ffn_gate, nullptr, nullptr,
- layer.ffn_down, nullptr, nullptr,
+ layer.ffn_up, nullptr,
+ layer.ffn_gate, nullptr,
+ layer.ffn_down, nullptr,
nullptr, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
} else {
@@ -780,9 +776,9 @@ llama_model_xing4_0::graph::graph(const llama_model & model, const llm_graph_par
{
ggml_tensor * ffn_shexp =
build_ffn(cur,
- layer.ffn_up_shexp, nullptr, nullptr,
- layer.ffn_gate_shexp, nullptr, nullptr,
- layer.ffn_down_shexp, nullptr, nullptr,
+ layer.ffn_up_shexp, nullptr,
+ layer.ffn_gate_shexp, nullptr,
+ layer.ffn_down_shexp, nullptr,
nullptr, LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(ffn_shexp, "ffn_shexp", il);
@@ -813,7 +809,7 @@ llama_model_xing4_0::graph::graph(const llama_model & model, const llm_graph_par
res->t_embd = cur;
// lm_head
- cur = ggml_mul_mat(ctx0, model.output, cur);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/xverse.cpp b/src/models/xverse.cpp
index e0710bd9c..7d83a55da 100644
--- a/src/models/xverse.cpp
+++ b/src/models/xverse.cpp
@@ -77,7 +77,7 @@ llama_model_xverse::graph::graph(const llm_graph_params & params) : llm_graph_co
cb(Vcur, "Vcur", il);
cur = build_attn(inp_attn,
- model.layers[il].wo, NULL, model.layers[il].wo_s,
+ model.layers[il].wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
}
if (il == n_layer - 1 && inp_out_ids) {
@@ -95,9 +95,9 @@ llama_model_xverse::graph::graph(const llm_graph_params & params) : llm_graph_co
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- model.layers[il].ffn_up, NULL, NULL,
- model.layers[il].ffn_gate, NULL, NULL,
- model.layers[il].ffn_down, NULL, NULL,
+ model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_gate, NULL,
+ model.layers[il].ffn_down, NULL,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
@@ -118,7 +118,7 @@ llama_model_xverse::graph::graph(const llm_graph_params & params) : llm_graph_co
res->t_embd = cur;
// lm_head
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
diff --git a/src/models/zaya1.cpp b/src/models/zaya1.cpp
index 013658c73..dd9082d0e 100644
--- a/src/models/zaya1.cpp
+++ b/src/models/zaya1.cpp
@@ -60,7 +60,7 @@ void llama_model_zaya1::load_arch_tensors(llama_model_loader &) {
layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, n_qk}, 0);
layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {n_qk}, 0);
- z.cca_conv_grp = create_tensor(tn(LLM_TENSOR_CCA_CONV_GRP, "weight", i), {n_embd_head, n_embd_head, n_group, d_conv}, 0);
+ z.cca_conv_grp = create_tensor(tn(LLM_TENSOR_CCA_CONV_GRP, "weight", i), {n_embd_head, n_embd_head, n_group, d_conv}, TENSOR_NO_SCALE);
z.cca_conv_grp_b = create_tensor(tn(LLM_TENSOR_CCA_CONV_GRP, "bias", i), {n_qk}, 0);
z.cca_temp = create_tensor(tn(LLM_TENSOR_CCA_TEMP, "weight", i), {n_head_kv}, 0);
@@ -233,7 +233,7 @@ ggml_tensor * llama_model_zaya1::graph::build_cca_block(const llama_model & mode
cb(Vcur, "cca_v", il);
return build_attn(inp->get_attn(),
- layer.wo, NULL, layer.wo_s,
+ layer.wo, NULL,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf((float) n_embd_head), il);
}
diff --git a/src/models/zonos3.cpp b/src/models/zonos3.cpp
index c5f62fe29..dd0a88f32 100644
--- a/src/models/zonos3.cpp
+++ b/src/models/zonos3.cpp
@@ -153,7 +153,7 @@ llama_model_zonos3::graph::graph(const llm_graph_params & params) :
cb(cur, "result_norm", -1);
res->t_embd = cur;
- cur = build_lora_mm(model.output, cur, model.output_s);
+ cur = build_lora_mm(model.output, cur);
cb(cur, "result_output", -1);
res->t_logits = cur;
}
@@ -210,7 +210,7 @@ ggml_tensor * llama_model_zonos3::graph::build_layer_attn(
cb(Vcur, "Vcur", il);
cur = build_attn(inp,
- nullptr, nullptr, nullptr,
+ nullptr, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
cb(cur, "attn_pregate", il);
@@ -219,7 +219,7 @@ ggml_tensor * llama_model_zonos3::graph::build_layer_attn(
cur = ggml_reshape_2d(ctx0, cur, n_embd_head * n_head, n_tokens);
cb(cur, "attn_gated", il);
- return build_lora_mm(layer.wo, cur, layer.wo_s);
+ return build_lora_mm(layer.wo, cur);
}
// copied from qwen35 build_layer_attn_linear, with the reference L2 norm eps
@@ -243,19 +243,19 @@ ggml_tensor * llama_model_zonos3::graph::build_layer_gdn(
GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs);
GGML_ASSERT(num_k_heads == num_v_heads);
- ggml_tensor * qkv_mixed = build_lora_mm(layer.wqkv, cur, layer.wqkv_s);
+ ggml_tensor * qkv_mixed = build_lora_mm(layer.wqkv, cur);
qkv_mixed = ggml_reshape_3d(ctx0, qkv_mixed, qkv_mixed->ne[0], n_seq_tokens, n_seqs);
cb(qkv_mixed, "linear_attn_qkv_mixed", il);
- ggml_tensor * z = build_lora_mm(layer.wqkv_gate, cur, layer.wqkv_gate_s);
+ ggml_tensor * z = build_lora_mm(layer.wqkv_gate, cur);
cb(z, "z", il);
- ggml_tensor * beta = build_lora_mm(layer.ssm_beta, cur, layer.ssm_beta_s);
+ ggml_tensor * beta = build_lora_mm(layer.ssm_beta, cur);
beta = ggml_reshape_4d(ctx0, beta, 1, num_v_heads, n_seq_tokens, n_seqs);
beta = ggml_sigmoid(ctx0, beta);
cb(beta, "beta_sigmoid", il);
- ggml_tensor * alpha = build_lora_mm(layer.ssm_alpha, cur, layer.ssm_alpha_s);
+ ggml_tensor * alpha = build_lora_mm(layer.ssm_alpha, cur);
alpha = ggml_reshape_3d(ctx0, alpha, num_v_heads, n_seq_tokens, n_seqs);
ggml_tensor * alpha_softplus = ggml_softplus(ctx0, ggml_add(ctx0, alpha, layer.ssm_dt));
@@ -307,7 +307,7 @@ ggml_tensor * llama_model_zonos3::graph::build_layer_gdn(
output = ggml_reshape_2d(ctx0, output, head_v_dim * num_v_heads, n_seq_tokens * n_seqs);
cb(output, "linear_attn_gated", il);
- return build_lora_mm(layer.ssm_out, output, layer.ssm_out_s);
+ return build_lora_mm(layer.ssm_out, output);
}
ggml_tensor * llama_model_zonos3::graph::build_layer_ffn(ggml_tensor * cur, int il) {
@@ -315,9 +315,9 @@ ggml_tensor * llama_model_zonos3::graph::build_layer_ffn(ggml_tensor * cur, int
if (layer.ffn_gate_inp == nullptr) {
cur = build_ffn(cur,
- layer.ffn_up, nullptr, layer.ffn_up_s,
- layer.ffn_gate, nullptr, layer.ffn_gate_s,
- layer.ffn_down, nullptr, layer.ffn_down_s,
+ layer.ffn_up, nullptr,
+ layer.ffn_gate, nullptr,
+ layer.ffn_down, nullptr,
nullptr,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 2e0edcb49..46fd38d5f 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -239,9 +239,15 @@ if (GGML_CPU AND (NOT WIN32 OR NOT BUILD_SHARED_LIBS))
set_tests_properties(test-generate-fp8-models PROPERTIES FIXTURES_SETUP fp8-models)
llama_test(test-fp8 NAME test-fp8-requantize LABEL "main behavior backend"
ARGS --models "${FP8_MODEL_DIR}")
+ set(FP8_KV_MODEL_DIR "${CMAKE_CURRENT_BINARY_DIR}/test-models-fp8-kv")
+ file(MAKE_DIRECTORY "${FP8_KV_MODEL_DIR}")
+ llama_test(test-llama-archs NAME test-generate-fp8-kv-models LABEL "main behavior backend"
+ ARGS -a "^(llama|gemma3)$" -s 1 -o "${FP8_KV_MODEL_DIR}")
+ set_tests_properties(test-generate-fp8-kv-models PROPERTIES FIXTURES_SETUP fp8-kv-models)
llama_test(test-fp8 NAME test-fp8-kv LABEL "main behavior backend"
- ARGS --models "${FP8_MODEL_DIR}" --kv)
- set_tests_properties(test-fp8-requantize test-fp8-kv PROPERTIES FIXTURES_REQUIRED fp8-models)
+ ARGS --models "${FP8_KV_MODEL_DIR}" --kv)
+ set_tests_properties(test-fp8-requantize PROPERTIES FIXTURES_REQUIRED fp8-models)
+ set_tests_properties(test-fp8-kv PROPERTIES FIXTURES_REQUIRED fp8-kv-models)
endif()
set(MODEL_DIR "${CMAKE_CURRENT_BINARY_DIR}/test-models/")
diff --git a/tests/fusion/MTL.csv b/tests/fusion/MTL.csv
index defaa0743..57bf77b5e 100644
--- a/tests/fusion/MTL.csv
+++ b/tests/fusion/MTL.csv
@@ -37,12 +37,12 @@ clover_moe ,1 ,any ,SOFT_MAX+ARGSORT+GET_ROWS+SUM_ROWS+CLAMP+DIV,
clover_moe ,1 ,any ,SSM_CONV+UNARY , 1
codeshell ,0 ,any ,NORM+MUL+ADD , 5
cogvlm ,0 ,any ,RMS_NORM+MUL , 5
-cohere2 ,0 ,any ,ADD+ADD , 2
+cohere2 ,0 ,any ,ADD+ADD , 1
cohere2 ,0 ,any ,NORM+MUL , 3
-cohere2moe ,1 ,any ,ADD+ADD , 2
+cohere2moe ,1 ,any ,ADD+ADD , 1
cohere2moe ,1 ,any ,MUL+ADD , 2
cohere2moe ,1 ,any ,RMS_NORM+MUL , 3
-command-r ,0 ,any ,ADD+ADD , 2
+command-r ,0 ,any ,ADD+ADD , 1
command-r ,0 ,any ,NORM+MUL , 3
dbrx ,1 ,any ,MUL+ADD , 4
dbrx ,1 ,any ,NORM+MUL , 5
@@ -105,6 +105,11 @@ gemma3 ,0 ,any ,RMS_NORM+MUL , 9
gemma3 ,0 ,any ,RMS_NORM+MUL+ADD , 4
gemma4 ,0 ,any ,RMS_NORM+MUL , 37
gemma4 ,0 ,any ,RMS_NORM+MUL+ADD , 30
+gemma4 ,1 ,any ,MUL+ADD , 20
+gemma4 ,1 ,any ,RMS_NORM+MUL , 57
+gemma4 ,1 ,any ,RMS_NORM+MUL+ADD , 40
+gemma4 ,1 ,any ,RMS_NORM+SCALE , 10
+gemma4 ,1 ,any ,SOFT_MAX+ARGSORT+GET_ROWS+SUM_ROWS+CLAMP+DIV, 10
glm-dsa ,1 ,any ,ADD+ADD , 1
glm-dsa ,1 ,any ,MUL+ADD , 2
glm-dsa ,1 ,any ,NORM+MUL+ADD , 2
@@ -212,7 +217,6 @@ llama ,1 ,any ,MUL+ADD , 4
llama ,1 ,any ,RMS_NORM+MUL , 5
llama ,1 ,any ,SOFT_MAX+ARGSORT+GET_ROWS+SUM_ROWS+CLAMP+DIV, 2
llama4 ,1 ,any ,ADD+ADD , 2
-llama4 ,1 ,any ,MUL+ADD , 4
llama4 ,1 ,any ,RMS_NORM+MUL , 9
maincoder ,0 ,any ,RMS_NORM+MUL , 9
maion-coder ,1 ,any ,MUL+ADD , 4
@@ -263,7 +267,8 @@ moshi ,0 ,any ,RMS_NORM+MUL , 5
mpt ,0 ,any ,NORM+MUL+ADD , 5
muse-glimmer ,0 ,any ,RMS_NORM+MUL , 10
muse-glimmer ,0 ,any ,RMS_NORM+MUL+ADD , 3
-muse-spark ,1 ,any ,MUL+ADD , 4
+muse-spark ,1 ,prefill ,MUL+ADD , 2
+muse-spark ,1 ,decode ,MUL+ADD , 4
muse-spark ,1 ,any ,RMS_NORM+MUL , 11
muse-spark ,1 ,any ,RMS_NORM+MUL+ADD , 3
muse-spark ,1 ,any ,UNARY+ADD+ARGSORT+GET_ROWS+SUM_ROWS+CLAMP+DIV, 2
diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index 3e49f40ed..c6ce63cf1 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -112,7 +112,7 @@ static void init_tensor_uniform(ggml_tensor * tensor, float min = -1.0f, float m
if (tensor->type == GGML_TYPE_F32 || tensor->type == GGML_TYPE_I32) {
ggml_backend_tensor_set(tensor, data.data(), 0, nels * sizeof(float));
- } else if (ggml_is_quantized(tensor->type) || tensor->type == GGML_TYPE_F8_E4M3 || tensor->type == GGML_TYPE_F16 || tensor->type == GGML_TYPE_BF16) {
+ } else if (ggml_is_quantized(tensor->type) || ggml_needs_scale(tensor->type) || tensor->type == GGML_TYPE_F16 || tensor->type == GGML_TYPE_BF16) {
GGML_ASSERT(nels % ggml_blck_size(tensor->type) == 0);
// dummy importance matrix
@@ -286,7 +286,7 @@ static std::vector<float> tensor_to_float(const ggml_tensor * t) {
const auto * tt = ggml_get_type_traits(t->type);
size_t bs = ggml_blck_size(t->type);
std::vector<float> vq(ggml_blck_size(t->type));
- bool uses_type_conversion = ggml_is_quantized(t->type) || t->type == GGML_TYPE_F8_E4M3;
+ bool uses_type_conversion = ggml_is_quantized(t->type) || ggml_needs_scale(t->type);
// access elements by index to avoid gaps in views
for (int64_t i3 = 0; i3 < t->ne[3]; i3++) {
@@ -6459,9 +6459,11 @@ struct test_mul_mat : public test_case {
const int64_t pad; // bytes after the m_v rows of each batch of a, so nb[2] of a is not a multiple of nb[1]
const size_t offset_a; // byte offset of the a view when k_v > k or m_v > m
const bool weights; // a in a weights buffer (CUDA with PDL reads weights early)
+ const int scale_gran; // weight scale granularity: 0=default, 1=per-tensor, 2=per-output-channel
+ const bool scale_view;
std::string vars() override {
- std::string s = VARS_TO_STR14(type_a, type_b, m, n, k, bs, nr, per, k_v, o, src_overlap, m_v, pad, offset_a);
+ std::string s = VARS_TO_STR16(type_a, type_b, m, n, k, bs, nr, per, k_v, o, src_overlap, m_v, pad, offset_a, scale_gran, scale_view);
if (weights) {
s += ",weights=1";
}
@@ -6499,8 +6501,8 @@ struct test_mul_mat : public test_case {
std::array<int64_t, 2> bs = {10, 10},
std::array<int64_t, 2> nr = {2, 2},
std::array<int64_t, 4> per = {0, 1, 2, 3},
- int64_t k_v = 0, uint32_t o = 1, bool src_overlap = false, int64_t m_v = 0, int64_t pad = 0, size_t offset_a = 0, bool weights = false)
- : type_a(type_a), type_b(type_b), m(m), n(n), k(k), bs(bs), nr(nr), per(per), k_v(k_v), o(o), src_overlap(src_overlap), m_v(m_v), pad(pad), offset_a(offset_a), weights(weights) {}
+ int64_t k_v = 0, uint32_t o = 1, bool src_overlap = false, int64_t m_v = 0, int64_t pad = 0, size_t offset_a = 0, bool weights = false, int scale_gran = 0, bool scale_view = false)
+ : type_a(type_a), type_b(type_b), m(m), n(n), k(k), bs(bs), nr(nr), per(per), k_v(k_v), o(o), src_overlap(src_overlap), m_v(m_v), pad(pad), offset_a(offset_a), weights(weights), scale_gran(scale_gran), scale_view(scale_view) {}
bool use_weight_context() override {
return weights;
@@ -6588,10 +6590,25 @@ struct test_mul_mat : public test_case {
ggml_set_name(b, "b");
}
- ggml_tensor * out = ggml_mul_mat(ctx, a, b);
+ ggml_tensor * s = nullptr;
+ if (scale_gran == 2) {
+ s = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, m);
+ } else if (scale_gran == 1 || ggml_needs_scale(type_a)) {
+ s = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1);
+ }
+ if (s && scale_view) {
+ s = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, 2, s->ne[0], s->ne[1]);
+ s = ggml_view_3d(ctx, s, 1, s->ne[1], s->ne[2], s->nb[1], s->nb[2], sizeof(float));
+ s = ggml_permute(ctx, s, 2, 0, 1, 3);
+ }
+ if (s) {
+ ggml_set_name(s, "s");
+ }
+
+ ggml_tensor * out = ggml_mul_mat_ext(ctx, a, b, s, nullptr);
ggml_set_name(out, "out");
for (uint32_t i = 1; i < o; ++i) {
- ggml_tensor * out2 = ggml_mul_mat(ctx, a, b);
+ ggml_tensor * out2 = ggml_mul_mat_ext(ctx, a, b, s, nullptr);
ggml_set_name(out2, "out2");
out = ggml_add(ctx, out, out2);
}
@@ -6824,9 +6841,11 @@ struct test_mul_mat_id : public test_case {
const int64_t m_v; // rows of as in memory, the experts of as are strided for m_v > m, no view for m_v == 0
const bool ids_offset;
const bool skip; // set some ids to a negative value
+ const int scale_gran; // weight scale granularity: 0=default, 1=per-expert scalar, 2=per-channel-per-expert
+ const bool scale_view;
std::string vars() override {
- return VARS_TO_STR11(type_a, type_b, n_mats, n_used, b, m, n, k, m_v, ids_offset, skip);
+ return VARS_TO_STR13(type_a, type_b, n_mats, n_used, b, m, n, k, m_v, ids_offset, skip, scale_gran, scale_view);
}
double max_nmse_err() override {
@@ -6854,9 +6873,9 @@ struct test_mul_mat_id : public test_case {
test_mul_mat_id(ggml_type type_a = GGML_TYPE_F32, ggml_type type_b = GGML_TYPE_F32,
int n_mats = 8, int n_used = 2, bool b = false,
int64_t m = 32, int64_t n = 32, int64_t k = 32,
- int64_t m_v = 0, bool ids_offset = false, bool skip = false)
+ int64_t m_v = 0, bool ids_offset = false, bool skip = false, int scale_gran = 0, bool scale_view = false)
: type_a(type_a), type_b(type_b), n_mats(n_mats), n_used(n_used), b(b),
- m(m), n(n), k(k), m_v(m_v), ids_offset(ids_offset), skip(skip) {
+ m(m), n(n), k(k), m_v(m_v), ids_offset(ids_offset), skip(skip), scale_gran(scale_gran), scale_view(scale_view) {
GGML_ASSERT(n_used <= n_mats);
GGML_ASSERT(m_v == 0 || m_v > m);
}
@@ -6879,7 +6898,29 @@ struct test_mul_mat_id : public test_case {
ggml_tensor * b = ggml_new_tensor_3d(ctx, type_b, k, this->b ? 1 : n_used, n);
ggml_set_name(b, "b");
- ggml_tensor * out = ggml_mul_mat_id(ctx, as, b, ids);
+ ggml_tensor * s = nullptr;
+ if (scale_gran == 2) {
+ s = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, m, n_mats);
+ } else if (scale_gran == 1 || ggml_needs_scale(type_a)) {
+ s = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, n_mats);
+ }
+ if (s && skip && !scale_view) {
+ const int64_t n_channels = scale_gran == 2 ? m : 1;
+ ggml_tensor * storage = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_channels, n_mats + 1);
+ ggml_set_name(storage, "scale_storage");
+ s = scale_gran == 2 ? ggml_view_2d(ctx, storage, m, n_mats, storage->nb[1], storage->nb[1]) :
+ ggml_view_1d(ctx, storage, n_mats, sizeof(float));
+ }
+ if (s && scale_view) {
+ s = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, 2, s->ne[0], s->ne[1]);
+ s = ggml_view_3d(ctx, s, 1, s->ne[1], s->ne[2], s->nb[1], s->nb[2], sizeof(float));
+ s = ggml_permute(ctx, s, 2, 0, 1, 3);
+ }
+ if (s) {
+ ggml_set_name(s, "s");
+ }
+
+ ggml_tensor * out = ggml_mul_mat_id_ext(ctx, as, b, ids, s, nullptr);
ggml_set_name(out, "out");
return out;
@@ -6887,6 +6928,11 @@ struct test_mul_mat_id : public test_case {
void initialize_tensors(ggml_context * ctx) override {
init_mul_mat_id_tensors(ctx, n_mats, skip);
+ if (ggml_tensor * storage = ggml_get_tensor(ctx, "scale_storage")) {
+ // A skipped expert must not read the scale row before the view.
+ std::vector<float> prefix(storage->ne[0], NAN);
+ ggml_backend_tensor_set(storage, prefix.data(), 0, prefix.size()*sizeof(float));
+ }
}
void reinit_perf_iter(ggml_context * ctx) override {
@@ -9334,7 +9380,7 @@ struct test_mul_mat_fusion : public test_case {
}
bool run_whole_graph() override { return true; }
- bool use_weight_context() override { return use_id && with_lane_scale; }
+ bool use_weight_context() override { return use_id && (with_lane_scale || ggml_needs_scale(type)); }
ggml_tensor * build_gate(ggml_context * ctx, ggml_tensor * ffn_gate, ggml_tensor * ffn_up) {
ggml_tensor * out = nullptr;
@@ -9353,20 +9399,6 @@ struct test_mul_mat_fusion : public test_case {
return out;
}
- ggml_tensor * build_lane_scale_dense(ggml_context * ctx, ggml_tensor * out) {
- ggml_tensor * scale = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1);
- return ggml_mul(ctx, out, scale);
- }
-
- ggml_tensor * build_lane_scale_id(ggml_context * ctx, ggml_context * ctx_weights, ggml_tensor * out, ggml_tensor * ids) {
- GGML_ASSERT(ctx_weights);
- ggml_tensor * scale = ggml_new_tensor_1d(ctx_weights, GGML_TYPE_F32, n_mats);
- ggml_tensor * s = ggml_reshape_3d(ctx, scale, 1, n_mats, 1);
- s = ggml_repeat_4d(ctx, s, 1, n_mats, m, 1);
- s = ggml_get_rows(ctx, s, ids);
- return ggml_mul(ctx, out, s);
- }
-
ggml_tensor * build_graph(ggml_context * ctx) override {
GGML_ASSERT(!use_weight_context());
return build_graph(ctx, nullptr);
@@ -9384,10 +9416,9 @@ struct test_mul_mat_fusion : public test_case {
ggml_tensor * up = ggml_new_tensor(ctx, type, 4, ne0.data());
auto build_lane_up = [&]() {
- ggml_tensor * ffn_up = ggml_mul_mat(ctx, up, cur);
- if (with_lane_scale) {
- ffn_up = build_lane_scale_dense(ctx, ffn_up);
- }
+ ggml_tensor * scale = (with_lane_scale || ggml_needs_scale(up->type))
+ ? ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1) : nullptr;
+ ggml_tensor * ffn_up = ggml_mul_mat_ext(ctx, up, cur, scale, nullptr);
if (with_bias) {
std::array<int64_t, 4> bias_ne = { ffn_up->ne[0], 1, channels, samples };
ggml_tensor * up_bias = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, bias_ne.data());
@@ -9397,10 +9428,9 @@ struct test_mul_mat_fusion : public test_case {
};
auto build_lane_gate = [&]() {
- ggml_tensor * ffn_gate = ggml_mul_mat(ctx, gate, cur);
- if (with_lane_scale) {
- ffn_gate = build_lane_scale_dense(ctx, ffn_gate);
- }
+ ggml_tensor * scale = (with_lane_scale || ggml_needs_scale(gate->type))
+ ? ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1) : nullptr;
+ ggml_tensor * ffn_gate = ggml_mul_mat_ext(ctx, gate, cur, scale, nullptr);
if (with_bias) {
std::array<int64_t, 4> bias_ne = { ffn_gate->ne[0], 1, channels, samples };
ggml_tensor * gate_bias = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, bias_ne.data());
@@ -9433,10 +9463,9 @@ struct test_mul_mat_fusion : public test_case {
ggml_set_name(cur, "cur");
auto build_lane_up = [&]() {
- ggml_tensor * ffn_up = ggml_mul_mat_id(ctx, ups, cur, ids);
- if (with_lane_scale) {
- ffn_up = build_lane_scale_id(ctx, ctx_weights, ffn_up, ids);
- }
+ ggml_tensor * scale = (with_lane_scale || ggml_needs_scale(ups->type))
+ ? ggml_new_tensor_1d(ctx_weights, GGML_TYPE_F32, n_mats) : nullptr;
+ ggml_tensor * ffn_up = ggml_mul_mat_id_ext(ctx, ups, cur, ids, scale, nullptr);
if (with_bias) {
ggml_tensor * up_bias_param = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, ffn_up->ne[0], n_mats);
ffn_up = ggml_add_id(ctx, ffn_up, up_bias_param, ids);
@@ -9445,10 +9474,9 @@ struct test_mul_mat_fusion : public test_case {
};
auto build_lane_gate = [&]() {
- ggml_tensor * ffn_gate = ggml_mul_mat_id(ctx, gates, cur, ids);
- if (with_lane_scale) {
- ffn_gate = build_lane_scale_id(ctx, ctx_weights, ffn_gate, ids);
- }
+ ggml_tensor * scale = (with_lane_scale || ggml_needs_scale(gates->type))
+ ? ggml_new_tensor_1d(ctx_weights, GGML_TYPE_F32, n_mats) : nullptr;
+ ggml_tensor * ffn_gate = ggml_mul_mat_id_ext(ctx, gates, cur, ids, scale, nullptr);
if (with_bias) {
ggml_tensor * gate_bias_param = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, ffn_gate->ne[0], n_mats);
ffn_gate = ggml_add_id(ctx, ffn_gate, gate_bias_param, ids);
@@ -13468,6 +13496,24 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
}
}
+ for (int scale_gran : {1, 2}) {
+ for (ggml_type type : {GGML_TYPE_F32, GGML_TYPE_F8_E4M3}) {
+ test_cases.emplace_back(new test_mul_mat_id(type, GGML_TYPE_F32, 1, 1, false, 32, 4, 32, 0, false, false, scale_gran));
+ for (int64_t n : {1, 4}) {
+ test_cases.emplace_back(new test_mul_mat_id(type, GGML_TYPE_F32, 4, 4, false, 64, n, 256, 0, false, true, scale_gran));
+ }
+ test_cases.emplace_back(new test_mul_mat(type, GGML_TYPE_F32, 32, 4, 32, {1, 1}, {1, 1}, {0, 1, 2, 3}, 0, 1, false, 0, 0, 0, false, scale_gran, true));
+ test_cases.emplace_back(new test_mul_mat_id(type, GGML_TYPE_F32, 4, 2, false, 32, 4, 32, 0, false, false, scale_gran, true));
+ }
+ for (int64_t n : {1, 8}) {
+ test_cases.emplace_back(new test_mul_mat(GGML_TYPE_Q4_K, GGML_TYPE_F32, 32, n, 256, {1, 1}, {1, 1}, {0, 1, 2, 3}, 0, 1, false, 0, 0, 0, false, scale_gran));
+ }
+ }
+
+ for (int64_t n : {1, 16}) {
+ test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_NVFP4, GGML_TYPE_F32, 4, 2, false, 32, n, 256, 0, false, false, 0, true));
+ }
+
#if 0
// > 2^31-element dst (8.9 GB), about 45 GB peak memory. Too large to be enabled by default.
// kernel_mul_mm dst offset of the last batch is past INT32_MAX: rows 0-31 use full tiles, rows 32-39 use edge tiles.
@@ -13552,6 +13598,16 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F8_E4M3, GGML_TYPE_F32, 16, 16, 64, {1, 1}, {1, 1}, {0, 1, 2, 3}, 0, 1, false, 17, 0, offset_a));
}
test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F8_E4M3, GGML_TYPE_F32, 16, 16, 64, {2, 1}, {1, 1}, {0, 1, 2, 3}, 0, 1, false, 17, 4));
+ // scaled weights sliced out of a larger tensor
+ for (ggml_type type : {GGML_TYPE_F8_E4M3, GGML_TYPE_NVFP4}) {
+ for (int scale_gran : {1, 2}) {
+ for (int64_t n : {1, 16}) {
+ for (int64_t bs : {1, 2}) {
+ test_cases.emplace_back(new test_mul_mat(type, GGML_TYPE_F32, 16, n, 64, {bs, 1}, {1, 1}, {0, 1, 2, 3}, 0, 1, false, 20, 0, 3*ggml_row_size(type, 64), false, scale_gran));
+ }
+ }
+ }
+ }
// testing very large N
test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 16, 2105344, 32, {1, 1}, {1, 1}));
@@ -15067,7 +15123,7 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
continue;
}
for (bool with_lane_scale : {false, true}) {
- if (with_lane_scale && type != GGML_TYPE_NVFP4 && type != GGML_TYPE_F8_E4M3) {
+ if (with_lane_scale && !ggml_needs_scale(type)) {
continue;
}
test_cases.emplace_back(new test_mul_mat_fusion(type, glu_op, 1, 32, 256,
diff --git a/tests/test-fp8.cpp b/tests/test-fp8.cpp
index dfd8ac541..c478f3e0c 100644
--- a/tests/test-fp8.cpp
+++ b/tests/test-fp8.cpp
@@ -316,7 +316,7 @@ static void check_fp8_attention(llama_model * model, bool offload_kqv = true) {
LOG("FP8 attention: tensor split requires flash attention, skipping disabled cases\n");
continue;
}
- for (int scaled : { 1, 2, 3 }) {
+ for (int scaled : { 0, 1, 2, 3 }) {
const float sk = scaled & 1 ? 0.03125f : 1.0f;
const float sv = scaled & 2 ? 0.125f : 1.0f;
ggml_backend_tensor_set(k_scale, &sk, 0, sizeof(sk));
@@ -324,15 +324,15 @@ static void check_fp8_attention(llama_model * model, bool offload_kqv = true) {
model->layers[0].k_cache_s = scaled & 1 ? k_scale : nullptr;
model->layers[0].v_cache_s = scaled & 2 ? v_scale : nullptr;
- std::vector<float> tensors[4];
+ std::vector<float> tensors[6];
auto params = llama_context_default_params();
params.n_ctx = 256;
params.n_batch = n_tokens;
params.n_ubatch = n_tokens;
params.n_threads = 1;
params.n_threads_batch = 1;
- params.type_k = scaled & 1 ? GGML_TYPE_F8_E4M3 : GGML_TYPE_F16;
- params.type_v = scaled & 2 ? GGML_TYPE_F8_E4M3 : GGML_TYPE_F16;
+ params.type_k = scaled == 0 || (scaled & 1) ? GGML_TYPE_F8_E4M3 : GGML_TYPE_F16;
+ params.type_v = scaled == 0 || (scaled & 2) ? GGML_TYPE_F8_E4M3 : GGML_TYPE_F16;
params.flash_attn_type = flash;
params.offload_kqv = offload_kqv;
if (model->split_mode() == LLAMA_SPLIT_MODE_TENSOR) {
@@ -378,16 +378,23 @@ static void check_fp8_attention(llama_model * model, bool offload_kqv = true) {
}
params.cb_eval_user_data = tensors;
params.cb_eval = [](ggml_tensor * t, bool ask, void * user_data) {
- const char * names[] = { "Qcur-0", "Kcur-0", "Vcur-0", "kqv_out-0" };
- for (int i = 0; i < 4; ++i) {
+ const char * names[] = { "Qcur-0", "Kcur-0", "Vcur-0", "kqv_out-0", "kq-0", "kqv-0" };
+ for (int i = 0; i < 6; ++i) {
if (strcmp(t->name, names[i]) != 0) {
continue;
}
if (!ask) {
- GGML_ASSERT(t->type == GGML_TYPE_F32 && ggml_is_contiguous(t));
auto & data = static_cast<std::vector<float> *>(user_data)[i];
- data.resize(ggml_nelements(t));
- ggml_backend_tensor_get(t, data.data(), 0, data.size()*sizeof(float));
+ ggml_tensor * value = t;
+ if (i >= 4) {
+ GGML_ASSERT(t->op == GGML_OP_MUL_MAT);
+ value = t->src[2];
+ }
+ if (value) {
+ GGML_ASSERT(value->type == GGML_TYPE_F32 && ggml_is_contiguous(value));
+ data.resize(ggml_nelements(value));
+ ggml_backend_tensor_get(value, data.data(), 0, data.size()*sizeof(float));
+ }
}
return true;
}
@@ -398,8 +405,12 @@ static void check_fp8_attention(llama_model * model, bool offload_kqv = true) {
GGML_ASSERT(lctx->get_cparams().flash_attn == (flash == LLAMA_FLASH_ATTN_TYPE_ENABLED));
get_logits(model, lctx.get(), { 1, 2, 3, 4 });
llama_synchronize(lctx.get());
- for (const auto & data : tensors) {
- GGML_ASSERT(data.size() == (size_t) (head_size*n_head*n_tokens));
+ for (int i = 0; i < 4; ++i) {
+ GGML_ASSERT(tensors[i].size() == (size_t) (head_size*n_head*n_tokens));
+ }
+ if (flash == LLAMA_FLASH_ATTN_TYPE_DISABLED) {
+ GGML_ASSERT(tensors[4] == (params.type_k == GGML_TYPE_F8_E4M3 ? std::vector<float>{ sk } : std::vector<float>{}));
+ GGML_ASSERT(tensors[5] == (params.type_v == GGML_TYPE_F8_E4M3 ? std::vector<float>{ sv } : std::vector<float>{}));
}
// Round the original K/V independently of the cache write graph.
@@ -547,15 +558,19 @@ static test_status test_fp8_kv(const std::string & path, const char * target_bac
auto params = llama_model_default_params();
params.devices = cpu_devices;
params.progress_callback = silent_model_load_progress;
+ bool supports_tensor_split;
{
llama_model_ptr model = load_model(path, params);
- if (model->arch != LLM_ARCH_LLAMA || model->hparams.n_expert > 0) {
- LOG("FP8 KV: only dense Llama fixtures are supported, skipping %s\n", path.c_str());
+ if ((model->arch != LLM_ARCH_LLAMA && model->arch != LLM_ARCH_GEMMA3) || model->hparams.n_expert > 0) {
+ LOG("FP8 KV: only dense Llama and Gemma3 fixtures are supported, skipping %s\n", path.c_str());
return test_status::SKIP;
}
- LOG("FP8 KV: CPU attention and context shift\n");
+ supports_tensor_split = llama_arch_supports_sm_tensor(model->arch);
+ LOG("FP8 KV: CPU attention\n");
check_fp8_attention(model.get());
- check_fp8_context_shift(model.get());
+ if (model->arch == LLM_ARCH_LLAMA) {
+ check_fp8_context_shift(model.get());
+ }
}
std::vector<ggml_backend_dev_t> devices_meta;
@@ -577,7 +592,7 @@ static test_status test_fp8_kv(const std::string & path, const char * target_bac
check_fp8_attention(model.get());
check_fp8_attention(model.get(), false);
}
- if (!target_backend && !devices_meta.empty()) {
+ if (!target_backend && supports_tensor_split && !devices_meta.empty()) {
devices_meta.push_back(nullptr);
params.devices = devices_meta.data();
params.split_mode = LLAMA_SPLIT_MODE_TENSOR;
@@ -616,6 +631,7 @@ static void test_fp8(const std::string & model_path) {
auto model_params = llama_model_default_params();
model_params.devices = devices;
model_params.tensor_buft_overrides = overrides;
+ model_params.use_extra_bufts = false;
model_params.progress_callback = silent_model_load_progress;
const int64_t n_embd = prepare_models(model_path, input, reference);
diff --git a/tests/test-llama-archs.cpp b/tests/test-llama-archs.cpp
index 3b22dc892..d7ecf29bf 100644
--- a/tests/test-llama-archs.cpp
+++ b/tests/test-llama-archs.cpp
@@ -1248,6 +1248,7 @@ static bool moe_implemented(const llm_arch arch) {
}
switch (arch) {
case LLM_ARCH_LLAMA:
+ case LLM_ARCH_GEMMA4:
case LLM_ARCH_REFACT:
case LLM_ARCH_MINICPM:
case LLM_ARCH_GRANITE:
@@ -1554,7 +1555,9 @@ static int test_backends(const std::string & arch_filter, const size_t seed, con
const double nmse_val = nmse(logits_cpu, logits_dev);
snprintf(nmse_str, sizeof(nmse_str), "(%.2e)", nmse_val);
status_nmse = "\033[1;32mOK\033[0m";
- if (nmse_val > 1e-4) {
+ // hrm_text repeats its stacks, which accumulates rounding error
+ const double nmse_max = arch == LLM_ARCH_HRM_TEXT ? 5e-4 : 1e-4;
+ if (nmse_val > nmse_max) {
test_ok = false;
status_nmse = "\033[1;31mFAIL\033[0m";
}
@@ -1866,8 +1869,11 @@ static int test_kv_shift(const std::string & arch_filter, const size_t seed, con
}
const char * config_name = moe ? "MoE" : "Dense";
- // the attention scale depends on the absolute position, so the hidden states of the prefix do too
- const double thold = arch == LLM_ARCH_MISTRAL_LARGE4 ? 1e-4 : 1e-6;
+ // the hidden states of the prefix depend on the absolute position:
+ // mistral-large4 scales attention by position, hunyuan/maincoder apply q/k norm weights after RoPE
+ const bool pos_dependent = arch == LLM_ARCH_MISTRAL_LARGE4 || arch == LLM_ARCH_HUNYUAN_MOE ||
+ arch == LLM_ARCH_HUNYUAN_DENSE || arch == LLM_ARCH_MAINCODER;
+ const double thold = pos_dependent ? 1e-4 : 1e-6;
// at least 4 layers, so that "every 4th layer" patterns (NoPE, full attention) are covered
// exaone4 is SWA only with 64 layers, smallthinker always ropes with 4
diff --git a/tests/test_conversion_fp8.py b/tests/test_conversion_fp8.py
index e191c8a55..0345c75b1 100644
--- a/tests/test_conversion_fp8.py
+++ b/tests/test_conversion_fp8.py
@@ -12,6 +12,7 @@ from __future__ import annotations
import json
from pathlib import Path
import tempfile
+import traceback
import unittest
import numpy as np
@@ -46,10 +47,24 @@ class OffsetNormModel(Qwen2MoeModel):
yield from super().modify_tensors(data_torch, name, bid)
+class ReorderedRowsModel(Qwen2MoeModel):
+ model_arch = gguf.MODEL_ARCH.QWEN2MOE
+
+ def modify_tensors(self, data_torch, name, bid):
+ if name.endswith("q_proj.weight"):
+ data_torch = data_torch.flip(0)
+ yield from super().modify_tensors(data_torch, name, bid)
+
+
class TestFP8Conversion(unittest.TestCase):
def convert(self, model_class, tensors, eager=False, check_lazy=False, scan_nvfp4=False, ftype=gguf.LlamaFileType.GUESSED, hparams_override=None, **kwargs):
with tempfile.TemporaryDirectory() as directory:
- return self._convert(Path(directory), model_class, tensors, eager, check_lazy, scan_nvfp4, ftype, hparams_override, **kwargs)
+ try:
+ return self._convert(Path(directory), model_class, tensors, eager, check_lazy, scan_nvfp4, ftype, hparams_override, **kwargs)
+ except Exception as e:
+ # traceback locals keep the safetensors mmap open, which blocks cleanup on Windows
+ traceback.clear_frames(e.__traceback__)
+ raise
def _convert(self, path, model_class, tensors, eager, check_lazy, scan_nvfp4, ftype, hparams_override, **kwargs):
hparams = {
@@ -90,7 +105,7 @@ class TestFP8Conversion(unittest.TestCase):
self.assertEqual(reads, [])
model.prepare_tensors()
if check_lazy:
- deferred = {name for name, tensor in tensors.items() if tensor.numel() != 1 or name.endswith((".weight", ".bias"))}
+ deferred = {name for name, tensor in tensors.items() if name in model.model_tensors and (tensor.numel() != 1 or name.endswith((".weight", ".bias")))}
self.assertFalse(deferred.intersection(reads), f"Materialized during preparation: {reads}")
for name, info in model.gguf_writer.tensors[0].items():
if name.endswith((".weight", ".bias")):
@@ -115,13 +130,9 @@ class TestFP8Conversion(unittest.TestCase):
}
result = self.convert(Qwen2MoeModel, tensors, check_lazy=True, scan_nvfp4=scan_nvfp4)
dtype, data = result["blk.0.attn_q.weight"]
- if channel_scale:
- self.assertEqual(dtype, gguf.GGMLQuantizationType.F16)
- np.testing.assert_array_equal(data, weight.float().numpy() * 0.25)
- else:
- self.assertEqual(dtype, gguf.GGMLQuantizationType.F8_E4M3)
- np.testing.assert_array_equal(gguf.dequantize(data, dtype), weight.float().numpy())
- np.testing.assert_array_equal(result["blk.0.attn_q.scale"][1], [0.25])
+ self.assertEqual(dtype, gguf.GGMLQuantizationType.F8_E4M3)
+ np.testing.assert_array_equal(gguf.dequantize(data, dtype), weight.float().numpy())
+ np.testing.assert_array_equal(result["blk.0.attn_q.scale"][1], [0.25, 0.25] if channel_scale else [0.25])
def test_mixed_compressed_fp8_block_scale(self):
name = "model.layers.0.self_attn.q_proj.weight"
@@ -167,6 +178,63 @@ class TestFP8Conversion(unittest.TestCase):
with self.assertRaisesRegex(ValueError, message):
self.convert(Qwen2MoeModel, tensors, hparams_override={"quantization_config": config})
+ def test_channel_scales_follow_rope_permutation(self):
+ for n_head, n_kv_head in ((1, 1), (2, 1)):
+ for proj in ("q", "k"):
+ n_rows = 8 if proj == "q" else 8 * n_kv_head // n_head
+ rows = [0, 4, 1, 5, 2, 6, 3, 7] if n_head == 1 else ([0, 2, 1, 3, 4, 6, 5, 7] if proj == "q" else [0, 2, 1, 3])
+ weight = torch.arange(n_rows * 8, dtype=torch.float32).reshape(n_rows, 8).to(torch.float8_e4m3fn)
+ for channel_scale in (False, True):
+ scales = (torch.arange(1, n_rows + 1, dtype=torch.float32).reshape(n_rows, 1) / 8) if channel_scale else torch.tensor([0.25])
+ for eager in (False, True):
+ with self.subTest(proj=proj, n_head=n_head, channel_scale=channel_scale, eager=eager):
+ name = f"model.layers.0.self_attn.{proj}_proj.weight"
+ hparams = {"hidden_size": 8, "num_attention_heads": n_head, "num_key_value_heads": n_kv_head}
+ result = self.convert(LlamaModel, {name: weight, name + "_scale": scales}, eager=eager,
+ check_lazy=not eager, hparams_override=hparams)
+ target = f"blk.0.attn_{proj}"
+ dtype, data = result[target + ".weight"]
+ self.assertEqual(dtype, gguf.GGMLQuantizationType.F8_E4M3)
+ actual_scale = result[target + ".scale"][1]
+ np.testing.assert_array_equal(actual_scale, scales.numpy().flatten()[rows] if channel_scale else [0.25])
+ np.testing.assert_array_equal(gguf.dequantize(data, dtype) * actual_scale.reshape(-1, 1), (weight.float() * scales).numpy()[rows])
+
+ def test_channel_scales_follow_projection_split(self):
+ name = "model.layers.0.linear_attn.in_proj_qkvz.weight"
+ for n_v_heads, qkv_rows, z_rows in (
+ (2, [0, 1, 8, 9, 2, 3, 10, 11, 4, 5, 12, 13], [6, 7, 14, 15]),
+ (4, [0, 1, 12, 13, 2, 3, 14, 15, 4, 5, 6, 7, 16, 17, 18, 19], [8, 9, 10, 11, 20, 21, 22, 23]),
+ ):
+ n_rows = len(qkv_rows) + len(z_rows)
+ weight = torch.arange(n_rows * 4, dtype=torch.float32).reshape(n_rows, 4).to(torch.float8_e4m3fn)
+ hparams = {"mtp_num_hidden_layers": 1, "linear_key_head_dim": 2, "linear_value_head_dim": 2,
+ "linear_num_key_heads": 2, "linear_num_value_heads": n_v_heads}
+ for channel_scale in (False, True):
+ scales = torch.arange(1, n_rows + 1, dtype=torch.float32).reshape(n_rows, 1) / 32 if channel_scale else torch.tensor([0.25])
+ expected = (weight.float() * scales).numpy()
+ for eager in (False, True):
+ with self.subTest(n_v_heads=n_v_heads, channel_scale=channel_scale, eager=eager):
+ result = self.convert(Qwen3NextModel, {name: weight, name + "_scale": scales}, eager=eager,
+ check_lazy=not eager, hparams_override=hparams)
+ for target, rows in (("blk.0.attn_qkv", qkv_rows), ("blk.0.attn_gate", z_rows)):
+ dtype, data = result[target + ".weight"]
+ self.assertEqual(dtype, gguf.GGMLQuantizationType.F8_E4M3)
+ actual_scale = result[target + ".scale"][1]
+ np.testing.assert_array_equal(actual_scale, scales.numpy().flatten()[rows] if channel_scale else [0.25])
+ np.testing.assert_array_equal(gguf.dequantize(data, dtype) * actual_scale.reshape(-1, 1), expected[rows])
+
+ def test_channel_scales_unknown_transform_fallback(self):
+ name = "model.layers.0.self_attn.q_proj.weight"
+ weight = torch.arange(16, dtype=torch.float32).reshape(4, 4).to(torch.float8_e4m3fn)
+ scales = torch.tensor([0.125, 0.25, 0.5, 1.]).reshape(4, 1)
+ for eager in (False, True):
+ with self.subTest(eager=eager):
+ result = self.convert(ReorderedRowsModel, {name: weight, name + "_scale": scales}, eager=eager, check_lazy=not eager)
+ dtype, data = result["blk.0.attn_q.weight"]
+ self.assertEqual(dtype, gguf.GGMLQuantizationType.F16)
+ np.testing.assert_array_equal(data, (weight.float() * scales).flip(0).numpy())
+ self.assertNotIn("blk.0.attn_q.scale", result)
+
def test_mla_scale_fallback(self):
weight = torch.arange(16, dtype=torch.float32).reshape(4, 4).to(torch.float8_e4m3fn)
prefix = "model.layers.0.self_attn."
@@ -181,6 +249,71 @@ class TestFP8Conversion(unittest.TestCase):
np.testing.assert_array_equal(result["blk.0.attn_k_b.weight"][1], (weight.float()[:2].T * 0.25).numpy()[None])
np.testing.assert_array_equal(result["blk.0.attn_v_b.weight"][1], (weight.float()[2:] * 0.25).numpy()[None])
+ def test_block_scales_rejected(self):
+ name = "model.layers.0.mlp.down_proj.weight"
+ weight = torch.ones((4, 16)).to(torch.float8_e4m3fn)
+ for block, shape in (((4, 4), (1, 4)), ((2, 8), (2, 2)), ((1, 16), (4, 1)), ((4, 16), (1, 1))):
+ scales = torch.arange(1, int(np.prod(shape)) + 1, dtype=torch.float32).reshape(shape) / 8
+ for method in ("compressed-tensors", "fp8", "modelopt"):
+ config = {"quant_method": method}
+ suffix = "_scale"
+ if method == "compressed-tensors":
+ config.update(format="float-quantized", config_groups={"group_0": {"weights": {"type": "float", "num_bits": 8, "strategy": "block", "block_structure": list(block)}}})
+ elif method == "fp8":
+ config["weight_block_size"] = list(block)
+ suffix = "_scale_inv"
+ elif shape in ((4, 1), (1, 1)):
+ continue
+ tensors = {name: weight, name + suffix: scales}
+ for eager in (False, True):
+ with self.subTest(block=block, method=method, eager=eager):
+ kwargs = {"eager": eager, "hparams_override": {"quantization_config": config}}
+ with self.assertRaisesRegex(ValueError, "unsupported block/group scale"):
+ self.convert(LlamaModel, tensors, **kwargs)
+ if method != "modelopt":
+ result = self.convert(LlamaModel, tensors, ftype=gguf.LlamaFileType.MOSTLY_F16, **kwargs)
+ expected = scales.repeat_interleave(block[0], 0).repeat_interleave(block[1], 1)
+ np.testing.assert_array_equal(result["blk.0.ffn_down.weight"][1], expected.numpy())
+
+ def test_channel_scale_layouts(self):
+ name = "model.layers.0.mlp.down_proj.weight"
+ weight = torch.ones((4, 16)).to(torch.float8_e4m3fn)
+ for shape in ((4,), (4, 1)):
+ scales = torch.arange(1, 5, dtype=torch.float32).reshape(shape) / 8
+ for eager in (False, True):
+ with self.subTest(shape=shape, eager=eager):
+ config = {"quant_method": "compressed-tensors", "format": "float-quantized", "config_groups": {"group_0": {"weights": {"type": "float", "num_bits": 8, "strategy": "channel"}}}}
+ result = self.convert(LlamaModel, {name: weight, name + "_scale": scales}, eager=eager, hparams_override={"quantization_config": config})
+ self.assertEqual(result["blk.0.ffn_down.weight"][0], gguf.GGMLQuantizationType.F8_E4M3)
+ np.testing.assert_array_equal(result["blk.0.ffn_down.scale"][1], scales.numpy().flatten())
+
+ def test_missing_weight_scale_rejected(self):
+ name = "model.layers.0.self_attn.q_proj.weight"
+ for nvfp4 in (False, True):
+ for eager in (False, True):
+ for scale in (None, 1.0, 0.25):
+ with self.subTest(nvfp4=nvfp4, eager=eager, scale=scale):
+ if nvfp4:
+ tensors = {
+ name: torch.zeros((4, 32), dtype=torch.uint8),
+ name + "_scale": torch.ones((4, 4)).to(torch.float8_e4m3fn),
+ }
+ scale_name = name + "_scale_2"
+ config = {"quant_method": "modelopt", "quant_algo": "NVFP4"}
+ else:
+ tensors = {name: torch.ones((4, 4)).to(torch.float8_e4m3fn)}
+ scale_name = name + "_scale"
+ config = {"quant_method": "modelopt"}
+ if scale is not None:
+ tensors[scale_name] = torch.tensor([scale])
+ kwargs = {"eager": eager, "hparams_override": {"quantization_config": config}}
+ if scale is None:
+ with self.assertRaisesRegex(ValueError, "source weight scale is missing"):
+ self.convert(Qwen2MoeModel, tensors, **kwargs)
+ else:
+ result = self.convert(Qwen2MoeModel, tensors, **kwargs)
+ np.testing.assert_array_equal(result["blk.0.attn_q.scale"][1], [scale])
+
def test_output_type_override(self):
name = "model.layers.0.self_attn.q_proj.weight"
weight = torch.tensor([[1., -1.], [2., -2.]]).to(torch.float8_e4m3fn)
@@ -190,6 +323,10 @@ class TestFP8Conversion(unittest.TestCase):
tensors = {name: weight}
if scaled:
tensors[name + "_scale"] = torch.tensor([0.25])
+ else:
+ with self.assertRaisesRegex(ValueError, "source weight scale is missing"):
+ self.convert(ForcedF16Model, tensors, eager=eager)
+ continue
result = self.convert(ForcedF16Model, tensors, eager=eager, check_lazy=not eager)
dtype, data = result["blk.0.attn_q.weight"]
self.assertEqual(dtype, gguf.GGMLQuantizationType.F16)
@@ -215,6 +352,10 @@ class TestFP8Conversion(unittest.TestCase):
tensors = {name: weight}
if scaled:
tensors[name + "_scale"] = torch.tensor([0.25])
+ else:
+ with self.assertRaisesRegex(ValueError, "source weight scale is missing"):
+ self.convert(Qwen3NextModel, tensors, eager=eager, hparams_override={"mtp_num_hidden_layers": 1})
+ continue
result = self.convert(Qwen3NextModel, tensors, eager=eager, check_lazy=not eager, hparams_override={"mtp_num_hidden_layers": 1})
dtype, data = result[target]
self.assertEqual(dtype, gguf.GGMLQuantizationType.F32)
@@ -225,7 +366,7 @@ class TestFP8Conversion(unittest.TestCase):
for shape in ((4,), (2, 2)):
with self.subTest(shape=shape):
weight = torch.ones(shape, dtype=torch.float32).to(torch.float8_e4m3fn)
- result = self.convert(OffsetNormModel, {"model.layers.0.input_layernorm.weight": weight}, check_lazy=True)
+ result = self.convert(OffsetNormModel, {"model.layers.0.input_layernorm.weight": weight, "model.layers.0.input_layernorm.weight_scale": torch.tensor([1.0])}, check_lazy=True)
dtype, data = result["blk.0.attn_norm.weight"]
self.assertEqual(dtype, gguf.GGMLQuantizationType.F32)
np.testing.assert_array_equal(data, np.full(shape, 2.))
@@ -248,6 +389,10 @@ class TestFP8Conversion(unittest.TestCase):
if scaled:
tensors[name + "_scale"] = torch.tensor([0.25])
tensors[name.removesuffix("weight") + "input_scale"] = torch.tensor([0.5])
+ else:
+ with self.assertRaisesRegex(ValueError, "source weight scale is missing"):
+ self.convert(Qwen2MoeModel, tensors, ftype=ftype, eager=eager)
+ continue
result = self.convert(Qwen2MoeModel, tensors, ftype=ftype, eager=eager, check_lazy=not eager)
self.assertEqual(set(result), {"blk.0.attn_q.weight"})
dtype, data = result["blk.0.attn_q.weight"]
@@ -326,6 +471,10 @@ class TestFP8Conversion(unittest.TestCase):
tensors[name + "_scale"] = torch.tensor([0.25])
elif other == "channel":
tensors[name + "_scale"] = torch.full((4, 1), 0.5)
+ if other == "unscaled":
+ with self.assertRaisesRegex(ValueError, "source weight scale is missing"):
+ self.convert(Qwen2MoeModel, tensors, eager=eager)
+ continue
result = self.convert(Qwen2MoeModel, tensors, eager=eager, check_lazy=not eager)
self.assertEqual(len(result), 3)
for proj in ("gate", "up", "down"):
@@ -349,11 +498,8 @@ class TestFP8Conversion(unittest.TestCase):
np.testing.assert_array_equal(result[f"blk.0.ffn_{proj}_exps.scale"][1], [0.25, 0.5])
unscaled = {name: tensor for name, tensor in tensors.items() if name.endswith(".weight")}
- result = self.convert(Qwen2MoeModel, unscaled, check_lazy=True)
- self.assertEqual(len(result), 3)
- for dtype, data in result.values():
- self.assertEqual(dtype, gguf.GGMLQuantizationType.F8_E4M3)
- np.testing.assert_array_equal(gguf.dequantize(data, dtype), np.ones((2, 4, 4)))
+ with self.assertRaisesRegex(ValueError, "source weight scale is missing"):
+ self.convert(Qwen2MoeModel, unscaled, check_lazy=True)
def test_fp8_as_q8(self):
name = "model.layers.0.self_attn.q_proj.weight"
@@ -388,6 +534,59 @@ class TestFP8Conversion(unittest.TestCase):
self.assertEqual(result["blk.0.attn_q.weight"][0], gguf.GGMLQuantizationType.F8_E4M3)
np.testing.assert_array_equal(result["blk.0.attn_q.scale"][1], [0.25])
+ def test_fp8_as_q8_requires_source_scale(self):
+ name = "model.layers.0.self_attn.q_proj.weight"
+ for dtype in (torch.float8_e4m3fn, torch.float8_e5m2):
+ for eager in (False, True):
+ with self.subTest(dtype=dtype, eager=eager):
+ tensors = {name: torch.ones((4, 32)).to(dtype)}
+ with self.assertRaisesRegex(ValueError, "source weight scale is missing"):
+ self.convert(Qwen2MoeModel, tensors, fp8_as_q8=True, eager=eager)
+
+ def test_nvfp4_writes_unit_scales(self):
+ for experts in (False, True):
+ with self.subTest(experts=experts):
+ names = ["model.layers.0.self_attn.q_proj.weight"]
+ targets = ["blk.0.attn_q"]
+ if experts:
+ names = [f"model.layers.0.mlp.experts.{expert}.{proj}_proj.weight"
+ for expert in range(2) for proj in ("gate", "up", "down")]
+ targets = [f"blk.0.ffn_{proj}_exps" for proj in ("gate", "up", "down")]
+ tensors = {}
+ for name in names:
+ tensors[name] = torch.zeros((2, 32), dtype=torch.uint8)
+ tensors[name + "_scale"] = torch.ones((2, 4)).to(torch.float8_e4m3fn)
+ with self.assertRaisesRegex(ValueError, "source weight scale is missing"):
+ self.convert(Qwen2MoeModel, tensors, eager=True, scan_nvfp4=True)
+ for name in names:
+ tensors[name + "_scale_2"] = torch.tensor([1.0])
+ tensors[name.removesuffix("weight") + "input_scale"] = torch.tensor([1.0])
+ result = self.convert(Qwen2MoeModel, tensors, eager=True, scan_nvfp4=True)
+ for target in targets:
+ np.testing.assert_array_equal(result[target + ".scale"][1], [1.0, 1.0] if experts else [1.0])
+ self.assertNotIn(target + ".input_scale", result)
+
+ def test_nvfp4_requires_block_scale(self):
+ name = "model.layers.0.self_attn.q_proj.weight"
+ tensors = {name: torch.zeros((2, 32), dtype=torch.uint8), name + "_scale_2": torch.tensor([1.0])}
+ with self.assertRaisesRegex(ValueError, "source block scale .* is missing"):
+ self.convert(Qwen2MoeModel, tensors, hparams_override={"quantization_config": {"quant_method": "modelopt", "quant_algo": "NVFP4"}})
+
+ def test_fp8_writes_expert_unit_scales(self):
+ tensors = {}
+ for expert in range(2):
+ for proj in ("gate", "up", "down"):
+ name = f"model.layers.0.mlp.experts.{expert}.{proj}_proj.weight"
+ tensors[name] = torch.ones((4, 4)).to(torch.float8_e4m3fn)
+ tensors[name + "_scale"] = torch.tensor([1.0])
+ tensors[name.removesuffix("weight") + "input_scale"] = torch.tensor([1.0])
+ for eager in (False, True):
+ with self.subTest(eager=eager):
+ result = self.convert(Qwen2MoeModel, tensors, eager=eager, check_lazy=not eager)
+ for proj in ("gate", "up", "down"):
+ np.testing.assert_array_equal(result[f"blk.0.ffn_{proj}_exps.scale"][1], [1.0, 1.0])
+ self.assertNotIn(f"blk.0.ffn_{proj}_exps.input_scale", result)
+
class TestInkling2PackedConversion(unittest.TestCase):
"""Conversion of the packed Inkling2 expert weights (see _dequant_expert_chunk in conversion/inkling2.py).
@@ -756,9 +955,8 @@ class TestQwen4ExpPleScale(unittest.TestCase):
self.assertEqual(dtype, gguf.GGMLQuantizationType.F32)
np.testing.assert_array_equal(data, [0.25])
- result = self.convert(scaled=False)
- self.assertEqual(set(result), {self.TABLE})
- np.testing.assert_array_equal(result[self.TABLE][1], raw)
+ with self.assertRaisesRegex(ValueError, "source weight scale is missing"):
+ self.convert(scaled=False)
def test_scale_is_folded_into_other_types(self):
values = torch.cat(self.SHARDS).numpy()
@@ -767,13 +965,14 @@ class TestQwen4ExpPleScale(unittest.TestCase):
({"ftype": gguf.LlamaFileType.MOSTLY_F16}, gguf.GGMLQuantizationType.F16),
({"fp8_as_q8": True}, gguf.GGMLQuantizationType.F16),
):
- for scaled in (False, True):
- with self.subTest(scaled=scaled, **kwargs):
- result = self.convert(scaled=scaled, **kwargs)
- self.assertEqual(set(result), {self.TABLE})
- dtype, data = result[self.TABLE]
- self.assertEqual(dtype, qtype)
- np.testing.assert_array_equal(data, values * (0.25 if scaled else 1.))
+ with self.subTest(**kwargs):
+ result = self.convert(**kwargs)
+ self.assertEqual(set(result), {self.TABLE})
+ dtype, data = result[self.TABLE]
+ self.assertEqual(dtype, qtype)
+ np.testing.assert_array_equal(data, values * 0.25)
+ with self.assertRaisesRegex(ValueError, "source weight scale is missing"):
+ self.convert(scaled=False, **kwargs)
if __name__ == "__main__":