Repository navigation
gemma3n, gemma4 : create token_embd before the tied output - #31394
Conversation
The duplicated output looks up token_embd in its buffer context, so token_embd must exist first. With the old order a load where input and output share a buffer type (CPU-only, -ngl 0) held the table twice: gemma-4-E2B-it Q4_K_M, no mmap, CPU model buffer 3163.73 -> 2947.73 MiB.
|
/bot review |
Automated code reviewI reviewed the diff (4-line reorder in Verification of the core claim In Findings No blocking issues. The change is minimal, correct, and aligns gemma3n/gemma4 with the convention every other tied-output architecture already follows. (will slow the review, minor) The PR description says "test-llama-archs currently skips GEMMA4, so there is no test included", but GEMMA3N is not in the skip list ( Nits (optional) (1) In both files the retained comment "if output is NULL, init from the input tok embed" sits directly under a create that now reuses Nice side effect worth knowing for the commit text: the old order also left two distinct tensors with the identical name This review was generated automatically by pi coding agent using |
Follow-up PR enabling Gemma 4 there: #31401 |
…31394) The duplicated output looks up token_embd in its buffer context, so token_embd must exist first. With the old order a load where input and output share a buffer type (CPU-only, -ngl 0) held the table twice: gemma-4-E2B-it Q4_K_M, no mmap, CPU model buffer 3163.73 -> 2947.73 MiB.
…31394) The duplicated output looks up token_embd in its buffer context, so token_embd must exist first. With the old order a load where input and output share a buffer type (CPU-only, -ngl 0) held the table twice: gemma-4-E2B-it Q4_K_M, no mmap, CPU model buffer 3163.73 -> 2947.73 MiB. (cherry picked from commit 344846b)
Overview
gemma3nandgemma4allocate a second copy oftoken_embdwhen loading without mmap (-lm none,-lm mlock) and both input and output layers share the same buffer type. This occurs on CPU-only runs,-ngl 0, or partial offload where the output layer stays on the CPU. With defaultmmap, both tensors simply point to the same mapped pages.Currently, both models create the tied output before
token_embd. The duplicated-tensor lookup inllama_model_loader::create_tensorruns at that point, finds notoken_embdyet, and creates a second one. The fix is to createtoken_embdfirst. This is the order the other 94 tied-output architectures already use;gemma3nandgemma4were the only exceptions.Additional information
Tested with
gemma-4-E2B-itQ4_K_M, CPU only, no mmap:The 216 MiB difference is the exact size of
token_embd. On the same model loaded CPU-only, greedy output remains identical to master both with and withoutmmap.test-llama-archscurrently skips GEMMA4, so there is no test included. With-ngl 99nothing changes: the output layer is on the GPU and still gets its own copy oftoken_embd, just like every other tied-output architecture.Requirements