Skip to content

feat: add Gemma4 Unsloth tensor-class recipes - #105

Merged
Brooooooklyn merged 4 commits into
mainfrom
agent/gemma4-unsloth-recipe
Jul 30, 2026
Merged

Brooooooklyn merged 4 commits into
mainfrom
agent/gemma4-unsloth-recipe

Conversation

@Brooooooklyn

@Brooooooklyn Brooooooklyn commented Jul 29, 2026 •

Copy link
Copy Markdown
Contributor

What changed

  • add a fail-closed fixed Unsloth selector for the exact SafeTensors Gemma 4 26B-A4B MoE shape
  • emit 180 MXFP4/NVFP4 dense and expert FFN modules plus 115 MXFP8/plain-E4M3 attention modules
  • keep routers, embeddings/head, norms, vision/audio, and unmatched tensors in BF16
  • add Gemma rank-2 plain E4M3 loading via one-time BF16 reconstruction while keeping stacked experts fail-closed
  • include plain-E4M3 BF16 reconstructions in cache-limit weight accounting (2,220,359,680 bytes for the published map)
  • preserve the calibrated legacy fallback for unsupported Gemma4 wrappers while rejecting uncalibrated fallback
  • update CLI messaging and docs to distinguish the existing Qwen final-eight policy from Gemma's all-low-FFN policy

Why

The CLI accepted --q-recipe unsloth --q-mxfp and --q-mode nvfp4 for Gemma, but the backend only selected the verified Qwen map. Gemma therefore fell back to the legacy Dynamic 2.0 predicate, and its loader rejected the plain E4M3 attention weights required by the DGX class map.

Validation

  • cargo check -p mlx-core --lib
  • yarn vp test packages/cli/__test__/convert-cmd.test.ts (25/25)
  • yarn build:ts
  • cargo fmt --all -- --check
  • git diff --check
  • GitHub Actions at 8c8b270e: Build, Rust Lint, full cargo test, TypeScript build/tests, and Publish all passed
  • regression coverage pins the exact reconstructed-BF16 byte delta and a dense zero-delta control
  • rebuilt the release native addon
  • converted the real google/gemma-4-26B-A4B-it checkpoint with matching Unsloth imatrix into both maps
  • audited exact 180 low / 115 high classes and SafeTensors index closure
  • loaded both artifacts and generated deterministic one-token OK

Note

Medium Risk
Touches quantization recipe selection, conversion validation, and Gemma4 weight loading/memory accounting; wrong gates could reject valid checkpoints or mis-classify tensors, but behavior is heavily gated and covered by new tests.

Overview
Extends the verified fixed Unsloth path beyond Qwen hybrids to the exact SafeTensors Gemma 4 26B-A4B MoE checkpoint (--q-mxfp / --q-mode nvfp4). OfficialUnslothRecipeKind is split into Qwen vs Gemma4 variants so Gemma never inherits Qwen’s final-eight FFN promotion.

Conversion (convert.rs): Strict gates on config, --model-type gemma4, MoE dims, and a 180-MLP / 115-attention tensor inventory; near-miss Gemma inputs fail closed instead of falling back to legacy Dynamic 2.0. Gemma’s map keeps all dense/expert FFNs low, attention q/k/v/o high, and everything else BF16. A pre-quant shape preflight enforces ranks, K%32, and the 180/115 class counts.

Runtime (Gemma4): Rank-2 plain E4M3 attention weights load via one-time BF16 reconstruction and A16 matmul; experts stay NVFP4-only. Reconstructed FP8 weight bytes are folded into cache-limit accounting.

CLI/docs/tests: Help text and docs describe Qwen vs Gemma policies; CLI tests updated for the new messaging.

Reviewed by Cursor Bugbot for commit 8c8b270. Bugbot is set up for automated code reviews on this repo. Configure here.

@coderabbitai

coderabbitai Bot commented Jul 29, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 740b728b-15c2-4c6e-abf9-fe8daf1936a5

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Brooooooklyn
Brooooooklyn marked this pull request as ready for review July 30, 2026 01:34
@cursor

cursor Bot commented Jul 30, 2026

Copy link
Copy Markdown

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: serverGenReqId_1e08332c-a266-4a93-8374-2baa6508f777)

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ff7f8a51aa

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@cursor

cursor Bot commented Jul 30, 2026

Copy link
Copy Markdown

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: serverGenReqId_76b6c216-fd3e-4789-b04d-882f439fc7e7)

@Brooooooklyn
Brooooooklyn merged commit 5e67047 into main Jul 30, 2026
7 checks passed
@Brooooooklyn
Brooooooklyn deleted the agent/gemma4-unsloth-recipe branch July 30, 2026 03:47
robert-johansson added a commit to robert-johansson/mlx-node that referenced this pull request Jul 30, 2026
… app, mlx-node#105 Gemma4 Unsloth tensor-class recipes

Merge-forward of mirror/upstream (400b12b) into our patched main.
Upstream delta since mlx-node#102:
  400b12b feat(desktop): mlx-node.app menubar app + supervised inference sidecar (mlx-node#103)
  5e67047 feat: add Gemma4 Unsloth tensor-class recipes (mlx-node#105)

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

# Conflicts:
#	packages/server/package.json
#	packages/server/src/server.ts
#	vite.config.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant