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
This commit is contained in:
Pascal 2026-11-26 16:24:45 +01:00
parent 017e34108d
commit e62ac03cd1
2 changed files with 12 additions and 2 deletions

View file

@ -1333,7 +1333,7 @@ struct ggml_tensor * llama_model_loader::create_tensor(
const ggml_tensor * scale_meta = nullptr;
ggml_tensor user_scale_meta = {};
if (!(flags & TENSOR_NO_SCALE) && (op == GGML_OP_MUL_MAT || op == GGML_OP_MUL_MAT_ID)) {
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()) {
@ -1352,6 +1352,11 @@ struct ggml_tensor * llama_model_loader::create_tensor(
!(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) {

View file

@ -236,7 +236,7 @@ static void llama_tensor_dequantize_impl(ggml_type type,
) {
const ggml_type_traits * qtype = ggml_get_type_traits(type);
if (ggml_is_quantized(type) || ggml_needs_scale(type)) {
if (qtype->to_float == NULL) {
if (qtype->to_float == nullptr) {
throw std::runtime_error(format("type %s unsupported for integer quantization: no dequantization available", ggml_type_name(type)));
}
} else if (type != GGML_TYPE_F16 && type != GGML_TYPE_BF16) {
@ -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); }