Skip to content

Fix duplicate gemma-2b-bnb-4bit key routing the base model to the instruct model - #6891

Merged
oobabooga merged 1 commit into
unslothai:mainfrom
vineethsaivs:fix/gemma-2b-mapper-duplicate-key
Jul 8, 2026
Merged

oobabooga merged 1 commit into
unslothai:mainfrom
vineethsaivs:fix/gemma-2b-mapper-duplicate-key

Conversation

@vineethsaivs

Copy link
Copy Markdown
Contributor

Summary

__INT_TO_FLOAT_MAPPER in unsloth/models/mapper.py has two entries keyed with the exact same string "unsloth/gemma-2b-bnb-4bit":

"unsloth/gemma-2b-bnb-4bit" : (          # line 129 (base)
    "unsloth/gemma-2b",
    "google/gemma-2b",
),
...
"unsloth/gemma-2b-bnb-4bit" : (          # line 137 (meant to be the instruct model)
    "unsloth/gemma-2b-it",
    "google/gemma-2b-it",
),

The line-137 entry was clearly meant to register the 4bit instruction-tuned model, but its key is missing the -it suffix (compare the adjacent gemma-7b pair, which correctly uses unsloth/gemma-7b-bnb-4bit and unsloth/gemma-7b-it-bnb-4bit). Because a Python dict literal keeps only the last value for a duplicate key, the base-model mapping is silently discarded.

Impact

After the module builds INT_TO_FLOAT_MAPPER / FLOAT_TO_INT_MAPPER / MAP_TO_UNSLOTH_16bit:

  • INT_TO_FLOAT_MAPPER["unsloth/gemma-2b-bnb-4bit"] resolves to "unsloth/gemma-2b-it", so loading the base 4bit repo with load_in_4bit=False returns the instruct model instead of the base model.
  • The base model loses its reverse mapping: FLOAT_TO_INT_MAPPER has no entry for "unsloth/gemma-2b" or "google/gemma-2b", so the base model does not get routed to its 4x-faster 4bit repo.
  • "google/gemma-2b-it" reverse-maps to the base 4bit repo.
  • "unsloth/gemma-2b-it-bnb-4bit" is never registered at all.

Fix

Give the instruct entry its correct key "unsloth/gemma-2b-it-bnb-4bit", mirroring the gemma-7b base/it pattern. This is a one-line change; the value tuple is unchanged. The Hugging Face repo unsloth/gemma-2b-it-bnb-4bit exists (the mapping now points at a real model), and the new key does not collide with any existing entry.

Out of scope: there is also a duplicate "unsloth/llama-2-7b-chat-bnb-4bit" key, but both of its entries have identical values, so it is harmless redundancy and left untouched.

Test

mapper.py has no imports, so the new tests/test_gemma_2b_mapper_key.py execs the module directly and asserts the base and instruct 4bit repos map to distinct models in both directions. It fails on the current code (the base repo resolves to the instruct model) and passes with this fix. It runs on CPU with no GPU, so it is wired into the Bucket-A CPU step in consolidated-tests-ci.yml.

@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.
Repo admins can enable using credits for code reviews in their settings.

@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 duplicate key issue in unsloth/models/mapper.py where unsloth/gemma-2b-bnb-4bit was used for both the base and instruct models, and adds a regression test to verify the fix. The feedback suggests explicitly specifying the UTF-8 encoding when opening files in Python to ensure cross-platform compatibility.

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 on lines +23 to +24
namespace = {}
exec(compile(source, MAPPER_PATH, "exec"), namespace)

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

When opening files in Python, it is best practice to explicitly specify the encoding (e.g., encoding="utf-8") to ensure cross-platform compatibility. On some platforms (like Windows), the default system encoding might not be UTF-8, which can lead to UnicodeDecodeError if non-ASCII characters are ever introduced to the file.

Suggested change
namespace = {}
exec(compile(source, MAPPER_PATH, "exec"), namespace)
with open(MAPPER_PATH, encoding="utf-8") as f:
source = f.read()

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 2a5a59b5bf

ℹ️ 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".

…bit repo to the instruct model

The INT_TO_FLOAT_MAPPER had two entries keyed the identical
"unsloth/gemma-2b-bnb-4bit"; Python keeps the last, so the base
gemma-2b 4bit repo resolved to the instruct model, the base lost its
reverse FLOAT_TO_INT mapping, and gemma-2b-it-bnb-4bit was unregistered.
Rename the second key to "unsloth/gemma-2b-it-bnb-4bit" to mirror the
gemma-7b base/it pair. Adds tests/test_gemma_2b_mapper_key.py, wired into
the Bucket-A CPU CI step.
@vineethsaivs
vineethsaivs force-pushed the fix/gemma-2b-mapper-duplicate-key branch from 2a5a59b to d7e2408 Compare July 7, 2026 20:54
@vineethsaivs

Copy link
Copy Markdown
Contributor Author

Rebased onto current main to clear the merge conflict (a new tests/test_prefetch_snapshot_scope.py line had landed in the same Bucket-A CI block). The fix and its test are unchanged; the branch is clean and mergeable again.

@oobabooga

Copy link
Copy Markdown
Member

Verified your fix by diffing the built mappers imported from main versus your branch. On main the duplicate literal key does exactly what you describe: INT_TO_FLOAT_MAPPER["unsloth/gemma-2b-bnb-4bit"] resolves to the instruct model, the base model loses its reverse (4bit) mapping, and unsloth/gemma-2b-it-bnb-4bit is never registered at all. Your one-line rekey restores all three. Confirmed both Hugging Face repos exist and their configs carry base versus instruct weights, so the corrected mappings are truthful. Also swept the whole file for other clobbered keys: only llama-2-7b-chat-bnb-4bit stays duplicated with identical values, which you correctly left alone.

Thanks @vineethsaivs. Merging.

@oobabooga
oobabooga merged commit dc4618c into unslothai:main Jul 8, 2026
45 of 46 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.

3 participants