Skip to content

feat(lfm3): add LFM3, LFM3-VL and LFM3-Omni quantization support - #3496

Merged
Qubitium merged 1 commit into
mainfrom
zx_support_lfm3_final
Nov 3, 2026
Merged

Qubitium merged 1 commit into
mainfrom
zx_support_lfm3_final

Conversation

@ZX-ModelCloud

Copy link
Copy Markdown
Collaborator

Summary

Add quantization support for the LiquidAI LFM3 family:

  • LiquidAI/LFM3-1.7B — model type lfm3
  • LiquidAI/LFM3-VL-2B — model type lfm3_vl
  • LiquidAI/LFM3-Omni-2B — model type lfm3_omni

LFM3 is a tri-hybrid decoder that interleaves depthwise short-conv layers,
full-attention layers and sliding-window-attention layers. GPT-QModel already
handles hybrid stacks for other families, but LFM3 has two properties that need
new plumbing:

  1. Layer replay needs regenerated rotary state and masks. LFM3 builds one
    position_embeddings and one mask per layer_type in the outer
    Lfm3Model.forward, not inside each decoder layer. Layer-by-layer
    quantization replay therefore has to rebuild them.
  2. The checkpoints ship as remote-code (auto_map) models. Neither
    transformers 5.20 nor 5.22 has native LFM3 classes, so the definitions
    require trust_remote_code=True.

What Changed

  • gptqmodel/models/definitions/lfm3.py — LFM3QModel for LiquidAI/LFM3-1.7B.
    A forward pre-hook on the text model captures the raw
    attention_mask/position_ids; prepare_layer_replay_kwargs then
    regenerates the rotary state and the matching
    create_recurrent_attention_mask / create_causal_mask /
    create_sliding_window_causal_mask result, naming the first layer of each
    type the way the checkpoint's create_masks_by_layer_type does.
  • gptqmodel/models/definitions/lfm3_vl.py — LFM3VLQModel
    (LFM3 text decoder + vision tower + multimodal projector).
  • gptqmodel/models/definitions/lfm3_omni.py — LFM3OmniQModel
    (LFM3 text decoder + audio encoder + codebook embedding + depth decoder +
    vision tower/projectors).
  • gptqmodel/models/auto.py, gptqmodel/models/definitions/__init__.py —
    register lfm3, lfm3_vl, lfm3_omni.
  • README.md — add the LFM3 row to the supported-models table.
  • tests/ — see below.

Quantized surface: only the text decoder's linear projections. The
depthwise conv kernel, RMSNorm weights and the per-head sigmoid gate_proj
keep their checkpoint dtype, as do the vision/audio towers and projectors.

Behavior changes: none for existing models. LFM3* requires
trust_remote_code=True, matching how the upstream checkpoints are published.

Out of scope: no new quantization kernels or backends; no changes to
packer, config or eval framework.

Tests

  • Unit tests have been executed and passed.
  • Added new simple/fast unit tests (tests/test_lfm3_support.py, 17 tests).
# Unit tests (CPU, no weights)
conda activate gp_314t
PYTHONPATH=/tmp/lfm3shim pytest -q tests/test_lfm3_support.py
# => 17 passed

PYTHONPATH=/tmp/lfm3shim ruff check <changed files>
# => All checks passed!

# End-to-end quantization + eval (CUDA, RTX 4090, env gp_314t)
# runner: /tmp/lfm3run/run_eval.sh, workdir tests/, MODE=fast|slow
MODE=fast GPUS=1 ./run_eval.sh test_lfm3.py
MODE=slow GPUS=1 ./run_eval.sh test_lfm3.py
MODE=fast GPUS=1 ./run_eval.sh test_lfm3_vl.py
MODE=slow GPUS=1 ./run_eval.sh test_lfm3_vl.py
MODE=fast GPUS=1 ./run_eval.sh test_lfm3_omni.py
MODE=slow GPUS=1 ./run_eval.sh test_lfm3_omni.py

ModelTest baselines on arc_challenge (INT4, marlin backend, 20 calibration
samples, slow seat floor_pct: 0.05):

Model fast acc / acc_norm slow acc / acc_norm
LFM3-1.7B 0.3328 / 0.3515 0.3268 / 0.3396
LFM3-VL-2B 0.3029 / 0.3404 0.3114 / 0.3430
LFM3-Omni-2B 0.4087 / 0.4241 0.3976 / 0.4113

Non-quantized LFM3-1.7B arc_challenge baseline (no chat template):
0.3404 / 0.3567 — the slow-mode INT4 score is within ~1.4 pt of that, i.e. no
quantization regression. The VL test additionally asserts the decoded image
description mentions "snow" (test image tests/models/ovis/10016.jpg).

Smoke runs on ROCm (AMD R9700, 4×, GPUs 2-3 only, env gp_314t_rocm):
quantize → save → reload → generate works for all three models
(LFM3-1.7B answers "Paris"; VL and Omni describe the snow mountains).

Tokenizer parity: chat-template render and resulting input IDs are identical to
AutoTokenizer for all three checkpoints; no Tokenicer change was needed.

Review Requirements

  • Reviewed this PR.
  • Checked that the code matches existing project structure, APIs, and conventions.
  • Avoided unnecessary monkeypatching and used the project's normal extension points where possible.
  • Minimized and compacted the impacted code surface.
  • Considered and eliminated potential regressions.

Kernel Accuracy Requirements

  • Not applicable: no quantization math, weight packer, or inference kernel
    change. This PR only adds model definitions (module tree, layer replay
    behavior, eval baselines). No dense/dequantized weight cache is added or
    retained; quantized inference storage is unchanged.

Notes

  • LFM3's depthwise conv has kernel width 7. causal_conv1d has no wide kernel
    for that shape, so the torch fallback path is used on Ampere/Ada
    (verified on RTX 4090).
  • generate for LFM3-Omni returns Lfm3OmniGenerateOutput; index [0] to
    reach the tensor. The tests do this.
  • A harmless The model weights are not tied warning appears at load time;
    embedding and lm_head are tied at the checkpoint level. This is pre-existing
    behavior, not introduced here.
  • auto.check_and_get_model_definition requires trust_remote_code=True to be
    passed explicitly for these checkpoints.

Add model definitions for the LiquidAI LFM3 family:

- `lfm3` (LFM3-1.7B): tri-hybrid decoder that alternates depthwise
  short-conv, full-attention and sliding-attention layers.
- `lfm3_vl` (LFM3-VL-2B): LFM3 text decoder plus vision tower and
  multimodal projector.
- `lfm3_omni` (LFM3-Omni-2B): LFM3 text decoder plus audio encoder,
  codebook embedding, depth decoder, vision tower and projectors.

Only the text decoder's linear projections are quantized. The depthwise
conv kernel, RMSNorm weights and the per-head sigmoid `gate_proj` keep
their checkpoint dtype, as do the vision/audio towers and projectors.

LFM3 builds one `position_embeddings` and one mask per `layer_type` in
the outer `Lfm3Model.forward`, so isolated decoder replay has to rebuild
them. A forward pre-hook on the text model captures the raw
`attention_mask`/`position_ids`; `prepare_layer_replay_kwargs` then
regenerates the rotary state and the matching
`create_recurrent_attention_mask` / `create_causal_mask` /
`create_sliding_window_causal_mask` result, naming the first layer of
each type the way the checkpoint's `create_masks_by_layer_type` does.

The checkpoints ship as remote-code (`auto_map`) models, so the
definitions require `trust_remote_code=True`.

Tests: `tests/test_lfm3_support.py` covers model-type dispatch, the
module tree, the multimodal base modules and the conv-vs-attention replay
kwargs. `tests/models/test_lfm3*.py` add `ModelTest` eval baselines
(arc_challenge) plus an image-description check for the VL model.

No quantization math, packer or inference kernel changes; no dense or
expanded weight cache is introduced.
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.

2 participants