Skip to content

Add Granite 4.1 20B support (GraniteSWA adapter) - #283

Open
lipikaworkemail-ctrl wants to merge 3 commits into
torch-spyre:mainfrom
lipikaworkemail-ctrl:enable-granite-4-1-20b-hf-adapters
Open

Add Granite 4.1 20B support (GraniteSWA adapter)#283
lipikaworkemail-ctrl wants to merge 3 commits into
torch-spyre:mainfrom
lipikaworkemail-ctrl:enable-granite-4-1-20b-hf-adapters

Conversation

@lipikaworkemail-ctrl

Copy link
Copy Markdown

Summary

Adds support for Granite 4.1 20B (ibm-research/granite-4.1-20b) via a new hf_granite_swa.py adapter for the GraniteSWAConfig model family.

Granite 4.1 introduces a Sliding Window Attention (SWA) architecture where decoder layers alternate between full attention and sliding-window attention (layer_type on each GraniteSWADecoderLayer). Sliding-window layers restrict attention to a local band of sliding_window tokens; full-attention layers use the standard causal mask. This requires a dedicated adapter rather than reusing hf_granite.py.

Changes

New file

  • hf_adapters/hf_granite_swa.py — Spyre adapter for GraniteSWAForCausalLM:
    • _make_compiled_block: per-layer compiled forward; builds a band mask for sliding-window layers, passes the normal causal mask for full-attention layers.
    • _run_forward: embedding × multiplier → blocks → RMSNorm → LM head with logits_scaling.
    • prepare_for_spyre: patches RMSNorm, pads LM head, compiles all blocks.

Modified files

  • hf_adapters/auto_spyre_model.py
    • Import GraniteSWAConfig and hf_granite_swa.
    • Register GraniteSWAConfig → hf_granite_swa in CONFIG_TO_ADAPTER_MODULE_MAPPING.
    • Register "ibm-research/granite-4.1-20b": torch.bfloat16 inMODEL_PATH_TO_TORCH_DTYPE.
  • tests/model_registry.py
    • Add granite41_20b (ibm-research/granite-4.1-20b, hf_granite_swa.py, 20b) — verified.
  • ARCHITECTURE.md — verified checkpoint row, adapter table row, coverage counts, model-specific features column for Granite 4.1 SWA.
  • hf_adapters.md — adapter summary row .
  • .github/cache_config/hf_models_and_datasets.yaml — add ibm-research/granite-4.1-20b.

Verified checkpoint

Model Repo dtype Prompt Output
Granite 4.1 20B ibm-research/granite-4.1-20b bfloat16 "The capital of France is" Paris.

Tested on Spyre with max_new_tokens=8.

Notes

  • ibm-research/granite-4.1-20b lives under the ibm-research org (notibm-granite). All other Granite 4.1/4.0/3.x models remain under ibm-granite.
  • The aten.embedding CPU fallback warning on Spyre is expected and does not affect correctness.
  • int64→int32 downcast warning is backend-level and expected.

Checklist

  • New adapter follows prepare_for_spyre / _run_forward / _make_compiled_block pattern
  • GraniteSWAConfig registered in CONFIG_TO_ADAPTER_MODULE_MAPPING
  • MODEL_PATH_TO_TORCH_DTYPE updated with correct org and dtype for 20B
  • Only verified checkpoint (20B) added to ARCHITECTURE.md and hf_adapters.md
  • Coverage counts updated (28 adapters · 45 verified checkpoints · 28 generative)
  • Model-specific features table updated in ARCHITECTURE.md
  • Cache config updated with ibm-research/granite-4.1-20b

Results:

image

@lipikaworkemail-ctrl

Copy link
Copy Markdown
Author

Hi @arielge, could you please review this PR?

cc: @saurabh-srivastava4 @goutham-binnadi-gopala @Mohini-Shrivastava

Signed-off-by: Lipika Sreedharan <lipikaworkemail@gmail.com>
@lipikaworkemail-ctrl
lipikaworkemail-ctrl force-pushed the enable-granite-4-1-20b-hf-adapters branch 4 times, most recently from f5bd456 to 876a6e2 Compare August 4, 2026 19:49
Signed-off-by: Lipika Sreedharan <lipikaworkemail@gmail.com>
@lipikaworkemail-ctrl
lipikaworkemail-ctrl force-pushed the enable-granite-4-1-20b-hf-adapters branch from 876a6e2 to 8ebb3c8 Compare August 4, 2026 19:52

@arielge arielge left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @lipikaworkemail-ctrl, I think that to merge this we would need to wait for a transformers release that includes the new GraniteSWAConfig class, and upgrade the dependencies accordingly

Comment thread tests/model_registry.py
"granite41_20b": {
"name": "Granite 4.1 20B",
"path": "ibm-research/granite-4.1-20b",
"adapter": "hf_granite_swa.py",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"adapter": "hf_granite_swa.py",
"adapter": "hf_granite_swa.py",
"is_gated": True,

until it is public, let's consider it gated

return torch.compile(block_forward, dynamic=False)


def _run_backbone_forward(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe better to do more reuse of the existing adapter, like in #281

Granite4VisionConfig,
GraniteConfig,
GraniteMoeHybridConfig,
GraniteSWAConfig,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The import currently fails, so to merge this change we would need to wait for a transformers release that includes this config

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