unsloth/unsloth
Daniel Han 2bbb15994b gemma4 moe 4bit: pure-PyTorch NF4 dequant variant (negative result, kept for docs)
Adds UNSLOTH_GEMMA4_MOE_4BIT_GROUPED_PT_DEQUANT=1 as a 5th forward variant on
top of #5432 + the follow-up active-only path.

Implementation:
- Pre-compute the dequantized per-block absmax (bnb's nested-blockwise
  scheme) once at swap time and cache on each Linear4bit as
  _unsloth_pt_absmax_fp32. Removes bnb from the per-forward path entirely.
- Per-forward: nibble unpack + 16-entry NF4 codebook lookup + per-block
  absmax multiply + reshape to (out, in). All pure tensor ops.
- The dequant+stack helper is wrapped in torch.compile so Inductor can
  fuse with the surrounding stack and grouped_mm.

Numerical parity probe (temp/sim_5344_pt_nf4_probe.py) matches bnb
bit-exactly on a synthetic stub (cos=1.0, max_abs_diff=0). Real-model
swapped forward gives cos 0.996 vs BF16 baseline (vs active-only's 0.985)
because the FP32 intermediate multiply is closer to ideal BF16 weights.

Speed result on gemma-4-26B-A4B-it (B200): 2.17 tok/s vs active-only's
2.21 tok/s. Essentially break-even. Inductor's fusion across the
per-expert iteration in the torch.stack list comprehension is bounded;
the real bottleneck (per-expert dispatch + stack copy) survives the
compile pass.

Resident VRAM +1.33 GB for the cached FP32 absmax buffers.

Kept in the codebase as a negative result + foundation for a future
vectorised-across-experts dequant pass, which would stack packed uint8
and absmax into (E_active, ...) tensors BEFORE the dequant so Inductor
sees a single batched op.

Bit-exact loop-vs-pt_dequant equivalence test:
temp/sim_5344_pt_dequant_unit.py (cos=1.0, max_abs_diff=0 on synthetic
stub with 2 or 4 active experts).
2026-05-17 14:53:18 +00:00
..
dataprep Fix raw text paragraph break normalization (#4884) 2026-04-09 04:45:43 -07:00
kernels Fix: Add missing utf-8 encoding to text-mode file operations (#5356) 2026-05-14 18:15:27 +04:00
models gemma4 moe 4bit: pure-PyTorch NF4 dequant variant (negative result, kept for docs) 2026-05-17 14:53:18 +00:00
optimizers feat: Implement Q-GaLore optimizer and custom embedding learning rate… (#4511) 2026-03-25 01:03:10 -07:00
registry Revert "[pre-commit.ci] auto fixes from pre-commit.com hooks" 2025-12-01 07:24:58 -08:00
utils Add check to disable xformers on newer GPUs (#4342) 2026-03-16 22:42:38 -07:00
__init__.py MLX training support for Studio on Apple Silicon (#5340) 2026-05-14 05:24:20 -07:00
_auto_install.py Add PyTorch 2.10 and xformers 0.0.34 support (#3985) 2026-02-05 05:56:26 -08:00
_gpu_init.py revert: stop touching DEVICE_TYPE == "cuda" branches for CPU CI (#5473) 2026-05-15 19:41:09 -07:00
chat_templates.py Fix/issue 3667 vicuna template (#5357) 2026-05-14 18:49:47 +04:00
device_type.py revert: stop touching DEVICE_TYPE == "cuda" branches for CPU CI (#5473) 2026-05-15 19:41:09 -07:00
import_fixes.py import_fixes + drift detectors: cover transformers 5.x drift (#5423) 2026-05-14 05:14:21 -07:00
ollama_template_mappers.py Re-apply #4939: updated models template mappers (#4950) 2026-04-15 07:52:12 -07:00
save.py Fix: Add missing utf-8 encoding to text-mode file operations (#5356) 2026-05-14 18:15:27 +04:00
tokenizer_utils.py fix: 3 patch_* helpers — fast_lora import, sft_trainer Union, openenv OSError (#5319) 2026-05-07 00:12:09 -07:00
trainer.py Trim trainer.py import-fix comment to one line 2026-05-06 07:22:56 +00:00