Add Gemma3 allHiddenStates for frozen-text-encoder use#387
Open
xocialize wants to merge 2 commits into
Open
Conversation
Encoder-style multi-layer hidden-state extraction on Gemma3Model / Gemma3TextModel: returns the embedding output plus every transformer layer's output (numHiddenLayers + 1 states), under a single caller- supplied uniform mask (combined causal+padding), intentionally bypassing the per-layer sliding/global mask selection of the generation path. Motivation: multimodal generators increasingly condition on ALL hidden states of a frozen LLM text encoder rather than on generated tokens — e.g. Lightricks LTX-2 conditions its video DiT on all 49 hidden states of Gemma-3-12B. Per-layer materialization keeps command buffers small so long-sequence encodes on large checkpoints stay under the macOS GPU watchdog. Unit tests run on a tiny randomly-initialized model (no downloads): state count/shapes, first-state == scaled embedding, layers transform the state, determinism. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Move @_spi(Testing) @testable import below the plain imports to satisfy the swift-format pre-commit hook. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Multimodal generators increasingly use a frozen LLM as a text encoder, conditioning on the hidden states of every layer rather than on generated tokens. A concrete example: Lightricks LTX-2 conditions its video DiT on all 49 hidden states of Gemma-3-12B-it. We've been carrying this as a small fork while building a Swift port of LTX-2 — upstreaming so the next consumer of Gemma-3-as-encoder doesn't have to.
API
Design notes:
evalkeeps each Metal command buffer small so long-sequence (1024-token) encodes on 12B-class checkpoints stay under the macOS GPU watchdog — without it all 48 layers fuse into one dispatch. Measured as load-bearing in production use.Testing
Gemma3AllHiddenStatesTests(tiny randomly-initialized model, no downloads, follows the existingChatSessionTestspattern): state count/shapes, first state == scaled embedding, layers transform the state, determinism. 3/3 pass viaxcodebuild test.🤖 Generated with Claude Code