Skip to content

Route dense NemotronH models to the transformers 5.10 tier - #6541

Merged
danielhanchen merged 11 commits into
mainfrom
fix/nemotron-h-mlp-transformers-tier
Jun 22, 2026
Merged

danielhanchen merged 11 commits into
mainfrom
fix/nemotron-h-mlp-transformers-tier

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

What

Fixes Failed to load checkpoint: '-' when loading or exporting a dense NemotronH
model (e.g. unsloth/NVIDIA-Nemotron-3-Nano-4B) in Studio.

Root cause

Dense NemotronH models describe their layer stack with a hybrid_override_pattern
string that mixes M (mamba), * (attention), E (moe) and - (mlp), for example
M-M-M-MM-M-M*-M-M*-.... transformers only learned to parse the - (MLP) layer in
5.10: pattern_mapping gained "-": "mlp", and valid_types / MIXER_TYPES gained
"mlp". On 5.3 / 5.5 the config raises KeyError: '-' in _pattern_to_list.

Studio's tier selector routed this model to the 5.3 tier (530) via the
TokenizersBackend tokenizer check. The model also ships auto_map remote code, so
training and inference (which run with trust_remote_code=True after consent) load
through the model's own code and work. But a native load with trust_remote_code=False
(such as export loading a LoRA's base model) hits the built-in 5.3 parser and fails.

Reproduced: AutoConfig.from_pretrained(<NemotronH>, trust_remote_code=False) raises
KeyError: '-' on transformers 5.3.0, and loads cleanly on 5.10.2 (42 layers,
block types {mamba, attention, mlp}).

Fix

Detect a dense NemotronH config (model_type == "nemotron_h" with a - in
hybrid_override_pattern, or "mlp" in an already-expanded layers_block_type) and
route it to the 5.10 tier, where it loads natively without remote code. Pure-MoE
NemotronH configs (no MLP layers) are unaffected and keep their existing tier.

Wired into _config_needs_510, so both the local-checkpoint and remote-HF-id paths
pick it up, and it takes precedence over the 530 tokenizer check.

Tests

tests/test_transformers_version.py: detector unit tests (dash pattern, expanded
mlp block list, MoE-only negative, non-NemotronH negative) and tier-selection tests
for a local dense-NemotronH checkpoint (510 beats the 530 tokenizer signal) and a
remote HF id. Full file: 63 passed.

Dense NemotronH models (e.g. unsloth/NVIDIA-Nemotron-3-Nano-4B) describe their
layer stack with a hybrid_override_pattern that includes '-' (MLP) layers.
transformers only learned to parse that ('-' -> 'mlp' in pattern_mapping, 'mlp'
in valid_types and MIXER_TYPES) in 5.10; on 5.3/5.5 the config raises
KeyError: '-'. The model also ships auto_map remote code, so training and
inference that approve trust_remote_code load fine, but a native (TRC=False)
load such as export hits the built-in parser and fails with
'Failed to load checkpoint: -'.

Detect dense NemotronH from config.json (a '-' in hybrid_override_pattern, or
'mlp' in an expanded layers_block_type) and route it to the 5.10 tier, where the
model loads natively without remote code. Pure-MoE NemotronH configs are
unaffected and keep their existing tier.

Covers both the local config.json and the remote HF-id paths, and adds tests for
the detector and the resulting tier selection.

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request adds support for routing dense NemotronH models (which use MLP layers requiring transformers>=5.10) to the 5.10 transformers sidecar. It introduces a helper function _nemotron_h_needs_mlp_support to detect these models based on their configuration pattern or block types, integrates this check into _config_needs_510, and adds comprehensive unit tests to verify the behavior for both local and remote configurations. There are no review comments, so I have no feedback to provide.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: daf90a3d26

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

)

with patch("urllib.request.urlopen") as mock_urlopen:
assert get_transformers_tier(str(tmp_path)) == "510"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Cover local checkpoints through activation

This local-checkpoint case calls get_transformers_tier() directly, but the workers actually enter through activate_transformers_for_subprocess(), which first resolves local adapters/checkpoints to base_model_name_or_path/model_name before tiering. In offline or private-base scenarios that discards the local config.json, the remote config fetch cannot see the NemotronH - pattern, and the same local dense NemotronH checkpoint can still route to 530/default and hit KeyError: '-'; please cover and fix the activation path, e.g. by consulting the local config before resolving away from it.

Useful? React with 👍 / 👎.

).encode()

with patch("urllib.request.urlopen", return_value = _Response()):
assert get_transformers_tier("unsloth/NVIDIA-Nemotron-3-Nano-4B") == "510"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Add an offline path for known NemotronH IDs

This remote test only exercises the successful urlopen path; when HF_HUB_OFFLINE/TRANSFORMERS_OFFLINE is set (or the raw config fetch is blocked) but the HF model is already cached, _load_config_json() returns None before seeing hybrid_override_pattern and the name has no 510 substring fallback, so unsloth/NVIDIA-Nemotron-3-Nano-4B still selects default/530 and the cached load can hit the same KeyError: '-'. Please add a no-network detector for the known dense Nemotron IDs or use the HF cache, and cover that path.

Useful? React with 👍 / 👎.

5.10 tier. Detected via the raw pattern or an already-expanded
``layers_block_type`` list.
"""
if cfg.get("model_type") != "nemotron_h":

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Inspect nested NemotronH configs

This top-level model_type check misses wrapper configs whose dense NemotronH language model is under llm_config/text_config; for example nvidia/NVIDIA-Nemotron-Nano-12B-v2-VL-BF16 has top-level model_type NemotronH_Nano_VL_V2, but its nested llm_config is nemotron_h with a hybrid_override_pattern containing -. Those models still route to default/530 and can hit the same native config parser failure, so the detector should recurse into the nested language config before returning false.

Useful? React with 👍 / 👎.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

3 similar comments
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

Three gaps could still route a dense NemotronH (MLP '-' layers) to a tier
below 5.10 and hit KeyError: '-':

- VL wrappers (e.g. NemotronH_Nano_VL_V2) keep the dense language model under
  llm_config/text_config; the detector only checked the top-level model_type.
  Recurse into nested language configs.
- Offline or blocked config fetches returned None for an already-downloaded
  repo. Read config.json from the HF hub cache before any network.
- A local checkpoint resolves to its base before tiering, so an offline/private
  base discarded the local config that revealed the dense pattern. Prefer the
  higher tier of the resolved base and the original path.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 62738af0f9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

if model_name != resolved:
# Prefer the higher tier: a local checkpoint's own config may reveal a dense
# NemotronH the (offline/private) base does not. Avoids KeyError: '-'.
tier = _higher_tier(tier, get_transformers_tier(model_name))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Avoid using path-name heuristics for local adapters

When model_name is a local LoRA adapter that resolves through adapter_config.json, this added call runs the full tier detector on the adapter directory path itself. If the adapter has no config.json and lives under a path such as /runs/gemma-4-experiment/llama-lora, get_transformers_tier(model_name) hits the substring heuristics and _higher_tier upgrades a Llama adapter to the 5.5/5.x sidecar even though the resolved base model was default; the local re-check should be limited to actual local config/tokenizer metadata rather than arbitrary path names.

Useful? React with 👍 / 👎.

Comment on lines +385 to +388
cfg = _config_json_from_hf_cache(model_name)
if cfg is not None:
_config_json_cache[cache_key] = cfg
return cfg

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Fetch remote configs before trusting stale HF cache

For HF repo IDs that are already present in the hub cache, this returns the cached config.json without attempting the authenticated/raw fetch below. If the repo was updated after an earlier download, tier selection and the model-config/GPU-estimate callers keep using stale metadata even while online, which can misroute newly dense NemotronH/Gemma variants or show wrong config values; the cache fallback should be reserved for offline mode or after the network fetch fails.

Useful? React with 👍 / 👎.

Comment on lines +347 to +348
from huggingface_hub import try_to_load_from_cache
path = try_to_load_from_cache(model_name, "config.json")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve subprocess sidecar isolation

For remote IDs that reach the slow config path, this imports the default-environment huggingface_hub before activate_transformers_for_subprocess prepends the selected .venv_t5_* directory. That function only changes sys.path and does not purge sys.modules, so a dense NemotronH routed to transformers 5.10 can later reuse the already-loaded default hub module instead of the pinned sidecar huggingface_hub==1.8.0, defeating the version isolation and risking import/runtime mismatches.

Useful? React with 👍 / 👎.

Address review of the nested/cached/resolved-away detection:

- The local re-check ran the full tier detector on the original path, so a bare
  LoRA adapter under e.g. /runs/gemma-4-x/llama-lora could upgrade a default base
  via directory-name substrings. Gate the re-check on a real local config.json so
  it reads metadata, not path names.
- The HF hub cache was read before any network, so an online tier check could
  serve stale config.json after the repo changed upstream. Consult the cache only
  offline or after a failed fetch.
- Reading the cache imported huggingface_hub during tier detection, which runs
  before a sidecar venv is activated and could pin the default-env hub into
  sys.modules. Resolve the cache path with stdlib only.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 785f8b22e3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 785f8b22e3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 785f8b22e3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

return None
# Fall back to the hub cache if the fetch failed (rate limit, transient block).
cfg = _config_json_from_hf_cache(model_name)
_config_json_cache[cache_key] = cfg

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Avoid caching stale HF fallback configs

When an online fetch fails transiently and a cached Hub snapshot exists, this stores the fallback config under the normal (model_name, token) cache key; because _load_config_json returns that key before attempting the network, the same long-lived backend process will keep serving the stale snapshot even after connectivity recovers. This can leave tier detection stuck on old metadata for updated models, despite online mode being expected to prefer fresh config.json; consider not caching the HF-cache fallback (or caching it separately only for offline mode).

Useful? React with 👍 / 👎.

try:
if ref_main.is_file():
candidates.append(repo_dir / "snapshots" / ref_main.read_text().strip() / "config.json")
candidates += sorted(repo_dir.glob("snapshots/*/config.json"))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Prefer the newest cached snapshot without refs/main

When a cached repo has multiple snapshots but no refs/main file (for example after commit-pinned downloads), this lexicographic sort can pick an older SHA rather than the snapshot Hugging Face would actually load; the existing cache utilities use snapshot mtime for that reason. In offline or fetch-failure tier detection, choosing an old config.json can keep a newly downloaded Nemotron/Gemma config on the wrong transformers tier, so the fallback should select the newest snapshot instead of the first sorted path.

Useful? React with 👍 / 👎.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

…fetches

The HF cache fallback in tier detection picked the lexicographically-first
snapshot when refs/main was absent (commit-pinned downloads), which can be an
older SHA than the Hub would load. Sort snapshots by mtime instead.

A transient online fetch failure cached the hub-cache fallback under the normal
(model_name, token) key, so a long-lived worker kept serving stale metadata even
after connectivity recovered. Return the fallback without memoizing it so the
next call retries the network.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 0739c52572

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0739c52572

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

return None
# Transient failure (rate limit, block): serve the hub cache but do NOT cache it,
# so a later call retries the network once connectivity returns.
return _config_json_from_hf_cache(model_name)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Keep cache-fallback config results retryable

When this fallback is used after a transient fetch error, _load_config_json avoids _config_json_cache, but get_transformers_tier() still memoizes the derived result in _config_needs_510_cache/_config_needs_550_cache. If the local Hub config is stale or missing for a dense NemotronH repo during a rate limit or network blip, later tier checks in the same process skip urlopen entirely and keep selecting 530/default even after connectivity returns. Signal that this came from a non-definitive cache fallback, or skip/clear the tier caches for these reads.

Useful? React with 👍 / 👎.

Comment on lines 432 to +436
except Exception as exc:
logger.debug("Could not fetch config.json for '%s': %s", model_name, exc)
_config_json_cache[cache_key] = None
return None
# Transient failure (rate limit, block): serve the hub cache but do NOT cache it,
# so a later call retries the network once connectivity returns.
return _config_json_from_hf_cache(model_name)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Don't use Hub cache after authorization failures

Because this catches every fetch exception, a 401/403/404 from the raw Hub request is treated the same as a transient outage. On a shared Studio host where a private or gated repo was already cached by an authenticated user, a later request with no or wrong token can still read that cached config.json and proceed with metadata/tier detection, bypassing the failed access check and exposing private model metadata. Limit this fallback to offline/timeouts/5xx cases, or require an authenticated cache hit before serving it.

Useful? React with 👍 / 👎.

… blips

- _load_config_json: a 401/403/404 from the raw Hub request is a definitive access
  answer, not an outage. Return None instead of falling back to the HF hub cache, so
  an unauthenticated or wrong-token request can never read another caller's cached
  private metadata.
- _check_config_needs_510/550: only memoize the derived tier when the underlying
  config read was definitive (local file, offline cache, or a completed fetch).
  A transient fetch fallback is no longer pinned, so the tier is re-evaluated once
  connectivity returns instead of staying stuck on the lower tier.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 76b798bec5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

@danielhanchen
danielhanchen merged commit aeb5075 into main Jun 22, 2026
34 of 38 checks passed
@danielhanchen
danielhanchen deleted the fix/nemotron-h-mlp-transformers-tier branch June 22, 2026 11:47
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