CompressedGemma commited on
Commit
1f034b0
·
verified ·
1 Parent(s): 336c058

Upload 3 files

Browse files
Files changed (1) hide show
  1. gguf.py +48 -6
gguf.py CHANGED
@@ -436,6 +436,42 @@ def _is_embd_or_head(name: str) -> bool:
436
  return "token_embd" in name or name == "output.weight" or name.endswith(".output.weight")
437
 
438
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
439
  def is_q2k_eligible(name: str, dims: tuple[int, ...]) -> bool:
440
  if _is_skipped_metadata_tensor(name, dims):
441
  return False
@@ -500,11 +536,13 @@ def target_quant_type(
500
  quant_format: str = "mixed",
501
  source_type: int | None = None,
502
  ) -> str | None:
503
- """Determine target quantization format ('Q2_K', 'Q4_0', or None for Copy).
504
 
505
  Modes:
506
  * 'mixed' (default): all Q2_K-eligible tensors -> Q2_K; non-Q2_K 32-aligned
507
  tensors (and embeddings/head if quantize_embd_q4) -> Q4_0.
 
 
508
  * 'q4_0': all 32-aligned eligible weight tensors -> Q4_0.
509
  * 'q2_k': only Q2_K-eligible tensors -> Q2_K; everything else copied.
510
  * 'q1_0': all 128-aligned eligible weight tensors -> Q1_0.
@@ -514,15 +552,19 @@ def target_quant_type(
514
  """
515
  tgt: str | None = None
516
  if quant_format == "q1_0":
517
- if is_q1_0_eligible(name, dims, include_embd=quantize_embd_q4):
 
 
 
 
518
  tgt = "Q1_0"
519
  elif enable_q4_fallback and is_q4_0_eligible(
520
- name, dims, include_embd=quantize_embd_q4, promote_attn=False
521
  ):
522
  tgt = "Q4_0"
523
  elif quant_format == "mixed_q1":
524
- if promote_attn_q4 and _is_attention_projection(name) and is_q4_0_eligible(
525
- name, dims, include_embd=quantize_embd_q4, promote_attn=True
526
  ):
527
  tgt = "Q4_0"
528
  elif _is_embd_or_head(name):
@@ -531,7 +573,7 @@ def target_quant_type(
531
  elif is_q1_0_eligible(name, dims, include_embd=False):
532
  tgt = "Q1_0"
533
  elif enable_q4_fallback and is_q4_0_eligible(
534
- name, dims, include_embd=quantize_embd_q4, promote_attn=False
535
  ):
536
  tgt = "Q4_0"
537
  elif quant_format == "q4_0":
 
436
  return "token_embd" in name or name == "output.weight" or name.endswith(".output.weight")
437
 
438
 
439
+ def _is_attention_projection(name: str) -> bool:
440
+ """True for attention and recurrent/SSM projections (Q/K/V/O, QKV, gates, SSM state)."""
441
+ if "norm" in name or ".bias" in name:
442
+ return False
443
+ return any(
444
+ tok in name
445
+ for tok in (
446
+ ".attn_",
447
+ ".ssm_",
448
+ "self_attn.",
449
+ "linear_attn.",
450
+ "attention.w",
451
+ "attn.q",
452
+ "attn.k",
453
+ "attn.v",
454
+ "attn.o",
455
+ "attn.wq",
456
+ "attn.wk",
457
+ "attn.wv",
458
+ "attn.wo",
459
+ )
460
+ )
461
+
462
+
463
+ def _is_tiny_ssm_gate(name: str, dims: tuple[int, ...]) -> bool:
464
+ """True for low-rank recurrent step-size/decay gates (e.g. ssm_alpha, ssm_beta, ssm_f_a/b)."""
465
+ if any(
466
+ k in name
467
+ for k in ("ssm_alpha", "ssm_beta", "ssm_f_a", "ssm_f_b", "ssm_g_a", "ssm_g_b")
468
+ ):
469
+ return True
470
+ if ".ssm_" in name and len(dims) >= 2 and min(int(dims[0]), int(dims[1])) <= 128:
471
+ return True
472
+ return False
473
+
474
+
475
  def is_q2k_eligible(name: str, dims: tuple[int, ...]) -> bool:
476
  if _is_skipped_metadata_tensor(name, dims):
477
  return False
 
536
  quant_format: str = "mixed",
537
  source_type: int | None = None,
538
  ) -> str | None:
539
+ """Determine target quantization format ('Q1_0', 'Q2_K', 'Q4_0', or None for Copy).
540
 
541
  Modes:
542
  * 'mixed' (default): all Q2_K-eligible tensors -> Q2_K; non-Q2_K 32-aligned
543
  tensors (and embeddings/head if quantize_embd_q4) -> Q4_0.
544
+ * 'mixed_q1': FFN/expert 128-aligned tensors -> Q1_0; attention/SSM projections,
545
+ embeddings/head, and non-128-aligned 32-aligned tensors -> Q4_0.
546
  * 'q4_0': all 32-aligned eligible weight tensors -> Q4_0.
547
  * 'q2_k': only Q2_K-eligible tensors -> Q2_K; everything else copied.
548
  * 'q1_0': all 128-aligned eligible weight tensors -> Q1_0.
 
552
  """
553
  tgt: str | None = None
554
  if quant_format == "q1_0":
555
+ if promote_attn_q4 and _is_attention_projection(name) and is_q4_0_eligible(
556
+ name, dims, include_embd=quantize_embd_q4, promote_attn=True, allow_q2k_tensors=True
557
+ ):
558
+ tgt = "Q4_0"
559
+ elif is_q1_0_eligible(name, dims, include_embd=quantize_embd_q4):
560
  tgt = "Q1_0"
561
  elif enable_q4_fallback and is_q4_0_eligible(
562
+ name, dims, include_embd=quantize_embd_q4, promote_attn=True, allow_q2k_tensors=True
563
  ):
564
  tgt = "Q4_0"
565
  elif quant_format == "mixed_q1":
566
+ if (_is_attention_projection(name) or _is_tiny_ssm_gate(name, dims)) and is_q4_0_eligible(
567
+ name, dims, include_embd=quantize_embd_q4, promote_attn=True, allow_q2k_tensors=True
568
  ):
569
  tgt = "Q4_0"
570
  elif _is_embd_or_head(name):
 
573
  elif is_q1_0_eligible(name, dims, include_embd=False):
574
  tgt = "Q1_0"
575
  elif enable_q4_fallback and is_q4_0_eligible(
576
+ name, dims, include_embd=quantize_embd_q4, promote_attn=True, allow_q2k_tensors=True
577
  ):
578
  tgt = "Q4_0"
579
  elif quant_format == "q4_0":