Skip to content

fix: work around transformers lazy_load_kernel offline regression - #2276

Merged
hallerite merged 4 commits into
mainfrom
fix/hub-kernels-offline-compat
Apr 17, 2026
Merged

fix: work around transformers lazy_load_kernel offline regression#2276
hallerite merged 4 commits into
mainfrom
fix/hub-kernels-offline-compat

Conversation

@hallerite

@hallerite hallerite commented Apr 14, 2026

Copy link
Copy Markdown
Member

Summary

  • Work around a regression in transformers >= 5.5 that breaks Mamba-based models (NemotronH, Zamba2, Jamba, etc.) when HF_HUB_OFFLINE=1 is set — as our multi-node SLURM template does
  • lazy_load_kernel() resolves kernel versions via HfApi().list_repo_refs(), which raises OfflineModeIsEnabled even when kernels are cached locally
  • The compat patch catches hub errors and loads kernels from the local HF Hub cache ($HF_HOME/hub/models--kernels-community--*/)

The regression was introduced across three transformers PRs:

  • PR #41577 (Oct 2025): lazy_load_kernel introduced
  • PR #43955 (Feb 2026): version=1 integer versioning bypasses snapshot_download cache fallback
  • PR #44176 (Feb 2026): all Mamba models switched from direct imports to lazy_load_kernel

No upstream issue exists yet — will file one on huggingface/transformers.

🤖 Generated with Claude Code


Note

Medium Risk
Monkeypatches transformers runtime behavior in a model-kernel loading path; if upstream behavior changes, this could mask real hub-kernel issues or alter which kernels get used in offline/online environments.

Overview
Extends prime_rl._compat from a single ring-flash-attn workaround into a small set of documented early-import compatibility shims.

Adds a new patch for transformers.integrations.hub_kernels.lazy_load_kernel on transformers >= 5.5 to gracefully handle HF_HUB_OFFLINE=1 by catching OfflineModeIsEnabled and returning None (so Mamba-based models can skip hub kernels and proceed with the project’s direct kernel path).

Reviewed by Cursor Bugbot for commit d3d91a6. Bugbot is set up for automated code reviews on this repo. Configure here.

@hallerite
hallerite force-pushed the fix/hub-kernels-offline-compat branch 2 times, most recently from 68b0710 to d480a02 Compare April 14, 2026 15:08
@hallerite
hallerite marked this pull request as ready for review April 14, 2026 15:14
Comment thread src/prime_rl/_compat.py Outdated
Comment thread src/prime_rl/_compat.py Outdated
@hallerite
hallerite force-pushed the fix/hub-kernels-offline-compat branch from d480a02 to bc73634 Compare April 14, 2026 15:28
Comment thread src/prime_rl/_compat.py Outdated
Comment thread src/prime_rl/_compat.py Outdated
@hallerite
hallerite force-pushed the fix/hub-kernels-offline-compat branch from bc73634 to 25f4dd7 Compare April 14, 2026 16:35
Comment thread src/prime_rl/_compat.py Outdated
Comment thread src/prime_rl/_compat.py Outdated
Comment thread src/prime_rl/_compat.py Outdated
@hallerite
hallerite force-pushed the fix/hub-kernels-offline-compat branch 2 times, most recently from 7297d9e to 4d75dd8 Compare April 14, 2026 16:48
Comment thread src/prime_rl/_compat.py Outdated
@hallerite
hallerite force-pushed the fix/hub-kernels-offline-compat branch 2 times, most recently from 22d39c9 to 4ffd8e7 Compare April 14, 2026 17:08
transformers >= 5.5 switched all Mamba-based models (NemotronH, Zamba2,
Jamba, etc.) from direct `import mamba_ssm` / `import causal_conv1d` to
`lazy_load_kernel()` which resolves kernel versions via HF Hub API. With
`HF_HUB_OFFLINE=1` (set in our SLURM template), this raises
`OfflineModeIsEnabled` — crashing model init even when the kernels are
already cached locally.

The compat patch wraps `lazy_load_kernel` to catch hub errors and load
kernels from the local HF Hub cache (`$HF_HOME/hub/models--kernels-community--*/`).

Regression chain in transformers:
- PR #41577 (Oct 2025): lazy_load_kernel introduced
- PR #43955 (Feb 2026): version=1 integer versioning bypasses snapshot_download cache
- PR #44176 (Feb 2026): all Mamba models switched to lazy_load_kernel

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@hallerite
hallerite force-pushed the fix/hub-kernels-offline-compat branch from 4ffd8e7 to a125615 Compare April 14, 2026 17:10

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

There are 2 total unresolved issues (including 1 from previous review).

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit a125615. Configure here.

Comment thread src/prime_rl/_compat.py Outdated
_mod.lazy_load_kernel = _patched_lazy_load_kernel

except ImportError:
pass

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Narrow except ImportError won't catch AttributeError from missing attributes

Medium Severity

The outer except ImportError on line 110 only catches ImportError, but lines 46–47 access _hub_kernels.lazy_load_kernel and _hub_kernels._KERNEL_MODULE_MAPPING, which raise AttributeError if those names don't exist. Since transformers.integrations.hub_kernels existed before lazy_load_kernel was added, there are valid transformers versions where the module imports successfully but the attribute access crashes the app at startup. Catching (ImportError, AttributeError) would match the graceful-degradation intent.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit a125615. Configure here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

that's the point, though

hallerite and others added 3 commits April 16, 2026 20:42
The original patch added ~70 lines of cache-walking fallback logic with
3 nested try/except blocks. This is unnecessary — when lazy_load_kernel
returns None, NemotronH falls back to torch_forward, and prime-rl's own
_patch_mamba2_use_triton_ssd() handles the rest via mamba_ssm directly.

The fix is just catching ConnectionError (parent of OfflineModeIsEnabled)
and returning None, same as upstream already does for FileNotFoundError.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@hallerite
hallerite merged commit 586e0f8 into main Apr 17, 2026
9 checks passed
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