Repository navigation
feat(lfm2): decode step for an NPU with block-quantized weights - #533
Merged
Merged
Conversation
Lfm2DecodeStep is the fixed-capacity single-token step of the cached network from an embedded token, with an output head of its own (a Q4_0 copy of the tied embedding folds; the tied tensor itself has two consumers). The layer loop and cache bookkeeping move into Lfm2CachedLayers, shared with Lfm2CachedTensorNetwork. The step's short convolution can be multiply + sum (ShortConvLowering.MULTIPLY_SUM) and its attention one batched product. DecodeStepExport.exportPorts traces a step over arbitrary named ports; export is built on it. exportLfm2DecodeStep and Lfm2DecodeStepExportCli use it with packed Q8_0 projections (loadLfm2Model(..., weightFormFor), lfm2ProjectionsAs) and write the module, port table, bf16 embedding rows and a manifest. LFM2.5-350M: 93 folded matmuls, top-1 equal to FP32; on an SL2610 NPU one dispatch, 533 ms per step, logits cosine 0.996. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
--parameters external-bf16 traces the step with every weight as a model-scope parameter (traceLfm2DecodeStepExternal), writes them as BF16 safetensors and types the parameter globals bf16 with a convert to f32 where they load (writeLfm2Bf16Parameters), for a GPU that serves weights from an archive. Writes io.txt, export.json and the embedding rows as the inline mode does. Compiled for Mali valhall4 with IREE 3.11: 152 ms per step, logits cosine 0.99991 to FP32. Co-Authored-By: Claude Opus 5.5 <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.
Against engine 0.63.0, transformers-only and additive (no API removed;
apiCheckpasses for lfm2 and klfm2).Changes
Lfm2DecodeStep(llm-inference:lfm2, common). The fixed-capacity single-token step ofLfm2CachedTensorNetwork, from an already embedded token and with an output head of its own. One graph serves every position up to the capacity:embedded,cos/sin, the additivemask[1, C+1]and the one-hotwrite_mask[1, C, 1];history_<l>andkey_<l>/value_<l>.The head can be a Q4_0 copy of the tied embedding, and then it folds. The tied tensor itself also serves the lookup, so with two consumers the fusion pass skips it. The layer loop and cache bookkeeping move into
Lfm2CachedLayers, whichLfm2CachedTensorNetworknow shares. That network's behaviour is unchanged and its tests pass.Lowerings for the step:
ShortConvLowering.MULTIPLY_SUMcomputes the single-token short convolution assum(window * taps)instead of a groupedconv1d. The result is numerically equal.AttentionLayoutPolicy.CURRENT_BATCHED). That's 12 attention matmuls instead of 192 for LFM2.5-350M, and 22 % faster on the NPU.DecodeStepExport.exportPorts(inputs, dtype) { ports, ctx -> results }(llm-core, jvm): the decode-step export over arbitrary named ports, for decoders whose state is more than K/V.exportis now built on it, with the same output.Export:
exportLfm2DecodeSteptraces the step with packed weights kept and folded.loadLfm2Model(..., weightFormFor)withlfm2ProjectionsAs(Q8_0)loads the projections requantized. The oldloadLfm2Modelsignature stays.Lfm2DecodeStepExportCli(GradleexportLfm2DecodeStep) writesdecode_step.mlir,io.txt(the port order),embed.bf16.bin(the host's embedding rows) andexport.jsonfrom a verified local checkpoint. It fails if any packed matmul is left unfolded.External bf16 parameters, for a GPU (
--parameters external-bf16, commit 844e0de):traceLfm2DecodeStepExternaltraces the dense step with every weight as amodel-scope parameter.writeLfm2Bf16Parameterswrites the weights as BF16 safetensors and types the parameter globals bf16, with a convert to f32 where they load.io.txt,export.jsonand the embedding rows.Results, LFM2.5-350M (Q8_0 projections, Q4_0 head, capacity 64)
With every projection as Q4_0, the Torq 2.3.0 compiler fails with "failed to allocate LRAM addresses". Q8_0 projections compile.
Tests
Lfm2DecodeStepTest:stablehlo.convolution;LFM2_MODEL_DIR): Q8_0/Q4_0 against FP32, a golden step for a device (LFM2_GOLDEN_DIR), and a greedy reference (LFM2_PROMPT_IDS).exportPortsNamesArbitraryStateAndRejectsAnAliasedResult; the existingDecoderKvModelTestexport tests are unchanged.🤖 Generated with Claude Code