Skip to content

Point the Apertus mapper rows at the Instruct repo, not the base one - #9311

Closed
vineethsaivs wants to merge 1 commit into
unslothai:mainfrom
vineethsaivs:fix-apertus-mapper-instruct-drift
Closed

vineethsaivs wants to merge 1 commit into
unslothai:mainfrom
vineethsaivs:fix-apertus-mapper-instruct-drift

Conversation

@vineethsaivs

Copy link
Copy Markdown
Contributor

Motivation

Slot 1 of an __INT_TO_FLOAT_MAPPER entry names the upstream repo the unsloth build mirrors, and mapper.py turns it into two lookups:

FLOAT_TO_INT_MAPPER[values[1]]  = key        # -> the 4bit build
MAP_TO_UNSLOTH_16bit[values[1]] = values[0]  # -> the 16bit build

Both Apertus rows put the pretrained base repo there:

"unsloth/Apertus-8B-Instruct-2509-unsloth-bnb-4bit" : (
    "unsloth/Apertus-8B-Instruct-2509",
    "swiss-ai/Apertus-8B-2509",              # <- the base model, not the Instruct one
    "unsloth/Apertus-8B-Instruct-2509-unsloth-bnb-4bit",
),

So __get_model_name resolves the base repo to the instruct build, and the instruct repo to nothing at all. Built straight from mapper.py on main:

model_name passed by the user            load_in_4bit=False resolves to                load_in_4bit=True resolves to
swiss-ai/Apertus-8B-2509                 unsloth/Apertus-8B-Instruct-2509              unsloth/apertus-8b-instruct-2509-unsloth-bnb-4bit
swiss-ai/Apertus-8B-Instruct-2509        None                                          None
swiss-ai/Apertus-70B-2509                unsloth/Apertus-70B-Instruct-2509             unsloth/apertus-70b-instruct-2509-unsloth-bnb-4bit
swiss-ai/Apertus-70B-Instruct-2509       None                                          None

Two consequences, both silent:

  • FastLanguageModel.from_pretrained("swiss-ai/Apertus-8B-2509") loads instruct weights, complete with a chat template, for someone who asked for the base model to continue-pretrain or fine-tune from scratch.
  • Someone who asks for swiss-ai/Apertus-8B-Instruct-2509 matches nothing and downloads the upstream repo, missing the unsloth build entirely.

Which repo is which

swiss-ai ships chat_template.jinja only in the -Instruct repos:

repo chat_template.jinja
swiss-ai/Apertus-8B-Instruct-2509 yes
swiss-ai/Apertus-8B-2509 no
swiss-ai/Apertus-70B-Instruct-2509 yes
swiss-ai/Apertus-70B-2509 no

and unsloth/Apertus-8B-Instruct-2509 carries that template inline in tokenizer_config.json, so the unsloth mirrors are of the Instruct models.

The base repo most likely landed in slot 1 because an instruct model's HuggingFace base_model card tag names the pretrained base: swiss-ai/Apertus-8B-Instruct-2509 itself declares base_model: ['swiss-ai/Apertus-8B-2509'], which reads like the upstream name when filling the row in.

Fix

Two lines: slot 1 becomes swiss-ai/Apertus-{8B,70B}-Instruct-2509. After it:

model_name passed by the user            load_in_4bit=False resolves to                load_in_4bit=True resolves to
swiss-ai/Apertus-8B-2509                 None                                          None
swiss-ai/Apertus-8B-Instruct-2509        unsloth/Apertus-8B-Instruct-2509              unsloth/apertus-8b-instruct-2509-unsloth-bnb-4bit
swiss-ai/Apertus-70B-2509                None                                          None
swiss-ai/Apertus-70B-Instruct-2509       unsloth/Apertus-70B-Instruct-2509             unsloth/apertus-70b-instruct-2509-unsloth-bnb-4bit

None for the base repos is correct: there is no unsloth Apertus base build, so those fall through to the ordinary HuggingFace path instead of being handed the wrong weights.

Tests

tests/test_mapper_instruct_drift.py, an ast check in the same shape as the existing test_mapper_no_duplicate_keys.py, so it needs no imports and no network. It asserts no entry crosses the instruct/base line in either direction.

# on main
$ python3 -m pytest tests/test_mapper_instruct_drift.py -q
1 failed, 1 passed
FAILED tests/test_mapper_instruct_drift.py::test_instruct_entries_do_not_point_at_a_base_repo
E   AssertionError: ... ['line 1119: unsloth/Apertus-70B-Instruct-2509-unsloth-bnb-4bit -> swiss-ai/Apertus-70B-2509',
                         'line 1124: unsloth/Apertus-8B-Instruct-2509-unsloth-bnb-4bit -> swiss-ai/Apertus-8B-2509']

# this branch
$ python3 -m pytest tests/test_mapper_instruct_drift.py -q
2 passed

test_base_entries_do_not_point_at_an_instruct_repo is the other direction and passes on main too, so it is a control rather than a second bug report: the rest of the table has no crossing either way, which is why the guard can be strict.

The mapper suite around it, same environment:

$ python3 -m pytest tests/test_mapper_no_duplicate_keys.py tests/test_gemma_2b_mapper_key.py \
    tests/test_new_mapper_fetched_fp8.py tests/test_new_mapper_no_global_leak.py -q
4 failed, 4 passed    # main
4 failed, 6 passed    # this branch, plus tests/test_mapper_instruct_drift.py

The 4 are the same test_new_mapper_fetched_fp8.py failures on both sides, pre-existing in my environment and untouched here; the delta is exactly the 2 tests added.

ruff check passes on the new file, and scripts/run_ruff_format.py leaves it unchanged on a second pass. mapper.py is in ruff's extend-exclude and in the kwarg hook's exclude, so its two edited lines are not formatter-affected.

Slot 1 of an __INT_TO_FLOAT_MAPPER entry names the upstream repo the unsloth
build mirrors, and mapper.py turns it into two lookups:

    FLOAT_TO_INT_MAPPER[upstream]  -> the 4bit key
    MAP_TO_UNSLOTH_16bit[upstream] -> slot 0

Both Apertus rows put the BASE repo there:

    "unsloth/Apertus-8B-Instruct-2509-unsloth-bnb-4bit" : (
        "unsloth/Apertus-8B-Instruct-2509",
        "swiss-ai/Apertus-8B-2509",          # the pretrained base
        ...

so FastLanguageModel.from_pretrained("swiss-ai/Apertus-8B-2509") resolves to
unsloth/Apertus-8B-Instruct-2509 (or its 4bit build) and silently loads instruct
weights for someone who asked for the base model, while
swiss-ai/Apertus-8B-Instruct-2509 itself matches nothing and misses the unsloth
build entirely. Same for the 70B pair.

swiss-ai ships chat_template.jinja only in the -Instruct repos, and the unsloth
mirrors carry that template inline, so the mirrors are of the Instruct models.
The base repo landed in slot 1 because an instruct model's `base_model` card tag
names the pretrained base, which reads like the upstream name.

Add tests/test_mapper_instruct_drift.py, an ast check in the same shape as
test_mapper_no_duplicate_keys.py, asserting no entry crosses the instruct/base
line in either direction. The whole table has no other crossing.
@vineethsaivs

Copy link
Copy Markdown
Contributor Author

Closing this: it duplicates my own earlier PR #7339, which has been open on the same two mapper rows since 2026-07-22, and #7339 is the better change.

I found the drift by scanning mapper.py on main, confirmed it was still there, and did not check for an existing PR against that file. That was my mistake, and the cost is not just a duplicate.

#7339 handles the 70B row correctly and this one does not. @oobabooga pointed out on that PR that unsloth/Apertus-70B-Instruct-2509 is not published (the Hub returns 401; only the 70B GGUF and -unsloth-bnb-4bit repos exist), so pointing the upstream slot at the Instruct repo while keeping that name in slot 0 sends a 16bit load of swiss-ai/Apertus-70B-Instruct-2509 through MAP_TO_UNSLOTH_16bit to a repo that is not there, where main loads it from upstream. #7339 collapses that row to a 1-tuple naming the real upstream, matching the other 70B and 405B rows. This PR would have introduced exactly the regression that review already caught.

I did see that 401 while checking which repo was which today and read it as a gated repo rather than an absent one. It was the more important of the two facts.

The one thing here that #7339 does not have is tests/test_mapper_instruct_drift.py, a generic ast guard asserting no entry in the table crosses the instruct/base line in either direction, in the same shape as the existing test_mapper_no_duplicate_keys.py. #7339's test_apertus_instruct_mapping.py pins the Apertus rows specifically. Happy to add the generic guard to #7339 if that is wanted, or to leave it out, since it is a separate concern from the fix under review there. Say the word rather than my pushing it into a PR that is already being reviewed.

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.

1 participant