MCPcopy Create free account
hub / github.com/antirez/llama.cpp-deepseek-v4-flash / tensor_allows_quantization

Function tensor_allows_quantization

src/llama-quant.cpp:288–355  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

286//
287
288static bool tensor_allows_quantization(const llama_model_quantize_params * params, llm_arch arch, const ggml_tensor * tensor) {
289 // trivial checks first -- no string ops needed
290 if (params->only_copy) return false;
291
292 // quantize only 2D and 3D tensors (experts)
293 if (ggml_n_dims(tensor) < 2) return false;
294
295 const std::string name = ggml_get_name(tensor);
296
297 // This used to be a regex, but <regex> has an extreme cost to compile times.
298 bool quantize = name.rfind("weight") == name.size() - 6; // ends with 'weight'?
299
300 // do not quantize norm tensors
301 quantize &= name.find("_norm.weight") == std::string::npos;
302
303 quantize &= params->quantize_output_tensor || name != "output.weight";
304
305 // do not quantize expert gating tensors
306 // NOTE: can't use LLM_TN here because the layer number is not known
307 quantize &= name.find("ffn_gate_inp.weight") == std::string::npos;
308
309 // these are very small (e.g. 4x4)
310 quantize &= name.find("altup") == std::string::npos;
311 quantize &= name.find("laurel") == std::string::npos;
312
313 // these are not too big so keep them as it is
314 quantize &= name.find("per_layer_model_proj") == std::string::npos;
315
316 // do not quantize positional embeddings and token types (BERT)
317 quantize &= name != LLM_TN(arch)(LLM_TENSOR_POS_EMBD, "weight");
318 quantize &= name != LLM_TN(arch)(LLM_TENSOR_TOKEN_TYPES, "weight");
319
320 // do not quantize Mamba/Kimi's small conv1d weights
321 // NOTE: can't use LLM_TN here because the layer number is not known
322 quantize &= name.find("ssm_conv1d") == std::string::npos;
323 quantize &= name.find("shortconv.conv.weight") == std::string::npos;
324
325 // do not quantize RWKV's small yet 2D weights
326 quantize &= name.find("time_mix_first.weight") == std::string::npos;
327 quantize &= name.find("time_mix_w0.weight") == std::string::npos;
328 quantize &= name.find("time_mix_w1.weight") == std::string::npos;
329 quantize &= name.find("time_mix_w2.weight") == std::string::npos;
330 quantize &= name.find("time_mix_v0.weight") == std::string::npos;
331 quantize &= name.find("time_mix_v1.weight") == std::string::npos;
332 quantize &= name.find("time_mix_v2.weight") == std::string::npos;
333 quantize &= name.find("time_mix_a0.weight") == std::string::npos;
334 quantize &= name.find("time_mix_a1.weight") == std::string::npos;
335 quantize &= name.find("time_mix_a2.weight") == std::string::npos;
336 quantize &= name.find("time_mix_g1.weight") == std::string::npos;
337 quantize &= name.find("time_mix_g2.weight") == std::string::npos;
338 quantize &= name.find("time_mix_decay_w1.weight") == std::string::npos;
339 quantize &= name.find("time_mix_decay_w2.weight") == std::string::npos;
340 quantize &= name.find("time_mix_lerp_fused.weight") == std::string::npos;
341
342 // do not quantize relative position bias (T5)
343 quantize &= name.find("attn_rel_b.weight") == std::string::npos;
344
345 // do not quantize specific multimodal tensors

Callers 3

llama_tensor_get_typeFunction · 0.85

Calls 5

ggml_n_dimsFunction · 0.85
ggml_get_nameFunction · 0.85
LLM_TNClass · 0.85
findMethod · 0.80
sizeMethod · 0.45

Tested by

no test coverage detected