Skip to content

Fix BAD_MAPPINGS not redirecting the -unsloth-bnb-4bit dynamic quants - #6949

Merged
oobabooga merged 4 commits into
unslothai:mainfrom
vineethsaivs:fix/bad-mappings-unsloth-bnb-4bit
Jul 8, 2026
Merged

oobabooga merged 4 commits into
unslothai:mainfrom
vineethsaivs:fix/bad-mappings-unsloth-bnb-4bit

Conversation

@vineethsaivs

Copy link
Copy Markdown
Contributor

Summary

BAD_MAPPINGS redirects a few oversized dynamic quants to smaller models, e.g.:

BAD_MAPPINGS = {
    "unsloth/Qwen3-32B-unsloth-bnb-4bit".lower(): "unsloth/Qwen3-32B-bnb-4bit".lower(),  # 32B dynamic quant is way too big
    "unsloth/Qwen3-30B-A3B-unsloth-bnb-4bit".lower(): "unsloth/Qwen3-30B-A3B".lower(),   # HF loads MoEs too slowly
    ...
}

But the -unsloth-bnb-4bit entries never fire. get_model_name applies BAD_MAPPINGS only to the resolver's output:

new_model_name = _resolve_with_mappers(...)
if (new_model_name is not None and ... and new_model_name.lower() in BAD_MAPPINGS):
    new_model_name = BAD_MAPPINGS[new_model_name.lower()]

The -unsloth-bnb-4bit names are keys of INT_TO_FLOAT_MAPPER, not values of FLOAT_TO_INT_MAPPER, so with load_in_4bit=True _resolve_with_mappers returns None for them. The remap is guarded by new_model_name is not None, so it is skipped, and get_model_name falls through to new_model_name = model_name, returning the oversized name unchanged. So FastLanguageModel.from_pretrained("unsloth/Qwen3-32B-unsloth-bnb-4bit") loads the large dynamic quant (much higher VRAM / OOM) instead of unsloth/Qwen3-32B-bnb-4bit, which is exactly what BAD_MAPPINGS is meant to prevent. The -bnb-4bit entries work because the resolver does resolve those, so the existing block catches them.

Fix

When the resolver returns None, also remap the input name through BAD_MAPPINGS:

    elif new_model_name is None and model_name.lower() in BAD_MAPPINGS:
        new_model_name = BAD_MAPPINGS[model_name.lower()]

This activates the -unsloth-bnb-4bit entries. Names the resolver does resolve (including the working -bnb-4bit entries) and all other names are unchanged, since this branch only runs when the resolver returned None.

Test

tests/test_bad_mappings_redirect.py asserts that get_model_name(name, load_in_4bit=True) == BAD_MAPPINGS[name.lower()] for every BAD_MAPPINGS entry. It fails on the current code (the three -unsloth-bnb-4bit names come back unchanged) and passes with this fix. The mapper table and resolver have no heavy imports, so the test execs the import-free mapper.py and ast-extracts the resolver functions (no GPU, no import unsloth); it is wired into the Bucket-A CPU CI step.

get_model_name applied BAD_MAPPINGS only to the resolver's output, but the
-unsloth-bnb-4bit dynamic-quant names (e.g. unsloth/Qwen3-32B-unsloth-bnb-4bit)
are keys of the mappers rather than values, so _resolve_with_mappers returns
None for them. The BAD_MAPPINGS remap is guarded by new_model_name is not None,
so it was skipped and get_model_name returned the oversized name unchanged,
loading the large dynamic quant (high VRAM / OOM) instead of the intended
smaller model that BAD_MAPPINGS points to.

When the resolver returns None, also remap the input name through BAD_MAPPINGS.
The already-working -bnb-4bit entries (which the resolver does resolve) and
all other names are unchanged. Adds a CPU regression test that asserts every
BAD_MAPPINGS entry redirects, ast-extracting the resolver so it needs no GPU.

@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 fixes a bug in get_model_name where certain bad model names (such as -unsloth-bnb-4bit dynamic quants) were not redirected by BAD_MAPPINGS because the resolver returned None for them. The fix directly remaps the input model name when the resolved name is None. A regression test has also been added to verify this behavior. The review feedback recommends specifying encoding="utf-8" when opening files in the test to prevent potential UnicodeDecodeError on platforms like Windows.

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.

Comment thread tests/test_bad_mappings_redirect.py Outdated

def _load_get_model_name():
mapper_ns = {}
with open(os.path.join(_MODELS, "mapper.py")) as f:

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.

medium

Specify encoding="utf-8" when opening files to prevent potential UnicodeDecodeError on platforms where the default system encoding is not UTF-8 (such as Windows).

Suggested change
with open(os.path.join(_MODELS, "mapper.py")) as f:
with open(os.path.join(_MODELS, "mapper.py"), encoding="utf-8") as f:

Comment thread tests/test_bad_mappings_redirect.py Outdated
with open(os.path.join(_MODELS, "mapper.py")) as f:
exec(compile(f.read(), "mapper.py", "exec"), mapper_ns)

with open(os.path.join(_MODELS, "loader_utils.py")) as f:

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.

medium

Specify encoding="utf-8" when opening files to prevent potential UnicodeDecodeError on platforms where the default system encoding is not UTF-8 (such as Windows).

Suggested change
with open(os.path.join(_MODELS, "loader_utils.py")) as f:
with open(os.path.join(_MODELS, "loader_utils.py"), encoding="utf-8") as f:

@oobabooga oobabooga self-assigned this Jul 8, 2026
@oobabooga

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: d49928338f

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@oobabooga

Copy link
Copy Markdown
Member

Verified your fix by running get_model_name against main and this branch across every BAD_MAPPINGS entry with load_in_4bit=True. On main the redirect silently no-ops for the dynamic-quant names the resolver returns None for, so the oversized quant loads. Your input-side remap closes that. It fixes one more case than the writeup says: Qwen3-30B-A3B-Base-bnb-4bit was dead on main too, not just the -unsloth-bnb-4bit trio. It also skips the per-load raw-GitHub mapper fetch those names used to trigger. test_get_model_name.py stays green.

I pushed a small follow-up: utf-8 encoding on the test's two file reads, and a docstring fix (it had undercounted the dead entries). Your fix is unchanged. Merging.

@oobabooga
oobabooga merged commit 92c3e48 into unslothai:main Jul 8, 2026
46 of 47 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