Skip to content

Add Gemma 4 E4B and 27B as downloadable model options #1094

Closed

Description

@malibio

Overview

Gemma 4 launched April 2, 2026 (Apache 2.0). The E4B variant was partially validated in the NLP experiment repo against llama-cpp-2 v0.1.143 — chat streaming works, but tool calling and remaining experiments are incomplete, and the production code paths (parser, chat template fallback) need updating before either variant can ship as a user-selectable model.

This issue covers completing validation of Gemma 4 E4B, adding both E4B and 27B as downloadable/selectable model options in the nlp-engine package, and making the parser and architecture changes required to support Gemma 4's tool-call schema alongside Mistral's.

Problem Statement

NodeSpace currently targets Ministral 3B (default) and 8B (upgrade path). Gemma 4 E4B shows significantly stronger reasoning (GPQA Diamond: 58.6% vs 33.8% / 39.9% for Ministral 3B / 8B) with competitive inference speed (20.1 t/s, 265ms TTFT on M2 Pro 16GB) and a much faster model load time (0.67s vs 12.9s for Ministral 8B). Gemma 4 27B is the larger quality-tier option for users with 24GB+ Apple Silicon.

Neither variant is wired into the production model registry, download flow, or chat pipeline. Two production code gaps must be closed:

  1. Chat template fallback: apply_chat_template returns ffi error -1 for Gemma 4 because its Jinja template applies an upper filter to an Array type that the bundled llama.cpp in v0.1.143 doesn't support. The fix (manual build_gemma4_prompt formatter using Gemma 4's native turn format) was validated in the experiment but is not in production code.

  2. Tool-call parser: Gemma 4 uses tool_name/tool_args fields instead of Mistral's name/arguments. The streaming parser must branch on model family to apply the correct schema.

Supporting multiple model families with incompatible prompt formats and tool-call schemas requires a deliberate architectural decision — see Technical Specifications below.

Proposed Solution

  • Complete the remaining NLP experiment validations (Experiments 2–5) for Gemma 4 E4B to confirm tool calling, dual-model loading, context stress, and download behavior.
  • Introduce a ModelFamily enum carried by each model registry entry; all per-family dispatch (prompt formatting, tool-call parsing, stop tokens) branches on this single value.
  • Implement the apply_chat_template fallback in packages/nlp-engine for Gemma 4 models.
  • Update the streaming tool-call parser to handle Gemma 4's tool_name/tool_args schema.
  • Register Gemma 4 E4B and 27B (Q4_K_M) in the model registry with HuggingFace download URLs, SHA256 hashes, and hardware requirement metadata.
  • Expose both models in the model selection UI as downloadable options.

Implementation Approach

Phase 1: Complete E4B Validation (NLP Experiment Repo)

  • Run Experiment 2 (tool calling) with Gemma 4 E4B and document parse rate and format
  • Run Experiment 3 (dual model) and record combined RSS, VRAM, and embedding latency
  • Run Experiment 4 (context stress) across 1k / 4k / 8k / 16k / 32k context sizes
  • Run Experiment 5 (model download) to confirm HuggingFace range request behavior
  • Update the NLP experiment README with all results

Phase 2: ModelFamily Enum and Per-Family Dispatch

  • Define ModelFamily enum with at minimum Mistral and Gemma4 variants
  • Each ModelEntry in the registry carries a family: ModelFamily field
  • All downstream code that needs per-family behavior matches on this enum — no string sniffing, no filename heuristics
  • This is the single place that knows "this loaded model is Gemma 4"; prompt formatting, tool-call parsing, and stop token handling all derive from it

Phase 3: Parser and Template Updates (nlp-engine)

  • Add build_gemma4_prompt to the production chat module: formats messages using Gemma 4's <start_of_turn>user/model...<end_of_turn> syntax, folds system prompt into the first user turn
  • Wrap apply_chat_template call with fallback: try template first, fall back to build_gemma4_prompt on error (or skip apply_chat_template entirely for Gemma 4 until the upstream Jinja bug is fixed)
  • Update the streaming tool-call parser to branch on ModelFamily: tool_name/tool_args for Gemma4, name/arguments for Mistral
  • Note: the Jinja upper-on-Array bug is a llama.cpp tooling limitation, not a permanent Gemma 4 property. Once upstream fixes it, the manual fallback can be removed and both families route through apply_chat_template. The ModelFamily enum and parser branching remain regardless — the tool-call schema difference is a model design choice.

Phase 4: Model Registry and Download Flow

  • Add Gemma 4 E4B entry: HuggingFace repo, filename, Q4_K_M GGUF, SHA256, ~5.0GB size, family: ModelFamily::Gemma4, minimum hardware recommendation
  • Add Gemma 4 27B entry: same fields, size and SHA256 confirmed from HuggingFace at implementation time, family: ModelFamily::Gemma4
  • Wire both entries into the existing model download flow (resumable HTTP range requests, SHA256 verification)

Phase 5: Model Selection UI

  • Surface Gemma 4 E4B and 27B in the model selection component alongside existing Ministral options
  • Display hardware requirements and GGUF size so users can assess compatibility before downloading

Acceptance Criteria

  • Gemma 4 E4B Experiment 2 (tool calling) completed and documented — parse rate and tool schema confirmed
  • Gemma 4 E4B Experiment 3 (dual model) completed — combined RSS and embedding latency recorded
  • Gemma 4 E4B Experiment 4 (context stress) completed across all context sizes
  • Gemma 4 E4B Experiment 5 (model download) completed — resume and integrity verification confirmed
  • ModelFamily enum defined; all model registry entries carry a family field
  • All per-family dispatch (prompt format, tool-call parsing, stop tokens) branches on ModelFamily — no string sniffing
  • build_gemma4_prompt implemented in production nlp-engine with correct turn format
  • apply_chat_template call wrapped with Gemma 4 fallback — no ffi errors at runtime
  • Streaming parser handles both Gemma 4 (tool_name/tool_args) and Mistral (name/arguments) schemas
  • Gemma 4 E4B registered in model registry with download URL, SHA256, and size
  • Gemma 4 27B registered in model registry with download URL, SHA256, and size
  • Both models appear in model selection UI with hardware requirements visible
  • Download, load, and chat streaming work end-to-end for Gemma 4 E4B on M2 Pro 16GB
  • Tests pass (bun run test:all)
  • Code passes bun run quality:fix

Technical Specifications

Reference Files

  • NLP experiment README: ~/nodespace/nodespace-nlp-experiment/README.md — validated workaround patterns and benchmark data
  • nlp-engine chat module: packages/nlp-engine/src/ — location for build_gemma4_prompt, template fallback, and ModelFamily dispatch
  • Streaming parser: tool-call parsing logic in packages/nlp-engine — needs schema branching on ModelFamily
  • Model registry: wherever model metadata (URLs, hashes, sizes) is defined in packages/nlp-engine or packages/core
  • Model selection UI: packages/desktop-app/src/ — model download/selection component

Key Implementation Details

ModelFamily enum (Rust sketch):

pub enum ModelFamily {
    Mistral,
    Gemma4,
}

pub struct ModelEntry {
    pub id: &'static str,
    pub family: ModelFamily,
    pub display_name: &'static str,
    pub gguf_size_bytes: u64,
    pub min_memory_gb: u8,
    pub download_url: &'static str,
    pub sha256: &'static str,
}

Per-family dispatch centralised in one match — adding a new model family means adding one enum variant and implementing the match arms, not scattering conditionals:

fn format_prompt(family: &ModelFamily, messages: &[Message]) -> String {
    match family {
        ModelFamily::Mistral => apply_chat_template_or_fallback(messages),
        ModelFamily::Gemma4  => build_gemma4_prompt(messages),
    }
}

fn parse_tool_call(family: &ModelFamily, output: &str) -> Option<ToolCall> {
    match family {
        ModelFamily::Mistral => parse_mistral_tool_call(output),
        ModelFamily::Gemma4  => parse_gemma4_tool_call(output),
    }
}

Gemma 4 prompt format (manual fallback):

<start_of_turn>user
[system prompt folded here if present]

[user message]<end_of_turn>
<start_of_turn>model
[assistant message]<end_of_turn>
<start_of_turn>user
[next user message]<end_of_turn>
<start_of_turn>model

Tool-call schema difference:

  • Mistral: {"name": "fn_name", "arguments": {...}}
  • Gemma 4: {"tool_name": "fn_name", "tool_args": {...}}

Validated performance (E4B, M2 Pro 16GB):

  • Tokens/sec: 20.1
  • TTFT: 265ms
  • Model load: 0.67s
  • GGUF size: ~5.0GB (Q4_K_M)

Gemma 4 27B notes:

  • Not yet benchmarked locally; recommended minimum hardware is 24GB+ Apple Silicon
  • GGUF download URL and SHA256 to be confirmed from HuggingFace at implementation time

Hardware Requirement Tags (suggested)

  • E4B: suitable for 16GB+ Apple Silicon
  • 27B: recommended for 24GB+ Apple Silicon (M3 Pro/Max, M4 Pro)

Non-Goals

  • Multimodal (vision/audio) input support — Gemma 4 supports it but NodeSpace has no vision pipeline yet; text-only for now
  • Automatic model upgrade prompts — users choose models manually
  • Benchmarking on non-Apple-Silicon hardware

Activity

  1. malibio commented on May 17, 2026

    @malibio
    CollaboratorAuthor

    Baseline test results (worktree at issue-1094-gemma-4-models, commit 82f14bf):

    Frontend (bun run test):

    • Test Files: 128 passed (128)
    • Tests: 3918 passed (3918)
    • Duration: 38.98s

    Starting work on this issue. Will use ../nodespace-nlp-experiment as reference for Gemma 4 validation (Experiments 2-5 already pass there per README — citing rather than re-running). Implementation diverges from issue body: using apply_chat_template_oaicompat with use_jinja: true (validated in experiment repo) instead of writing a manual build_gemma4_prompt formatter. Cleaner, less per-family code.

  2. self-assigned this
    on May 17, 2026
  3. added a commit that references this issue on May 17, 2026
    efe0b06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions