Skip to content

Fixup mapper issues and resolve properly#4124

Open
Datta0 wants to merge 2 commits intounslothai:mainfrom
Datta0:name_resolution
Open

Fixup mapper issues and resolve properly#4124
Datta0 wants to merge 2 commits intounslothai:mainfrom
Datta0:name_resolution

Conversation

@Datta0
Copy link
Collaborator

@Datta0 Datta0 commented Feb 27, 2026

To run the test:
python -m unittest tests/test_get_model_name.py from the root directory.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly refactors and enhances the model name resolution system, particularly addressing how FP8 models are handled by redirecting them to their BF16 counterparts. By centralizing this complex logic into a single, robust get_model_name function and improving the underlying mapping mechanisms, the change aims to make model loading more consistent and reliable. The addition of comprehensive unit tests ensures the correctness of these critical resolution processes.

Highlights

  • Refactored Model Name Resolution: The logic for resolving model names, especially for FP8 models and their redirection to BF16 variants, has been centralized within the get_model_name function. The previously separate _redirect_fp8_to_bf16 utility function has been removed.
  • Enhanced Mapper Robustness: New helper functions (_add_with_lower, _add_lower_only) were introduced in mapper.py to standardize how model name mappings are added, ensuring consistent handling of both original and lowercased keys for improved robustness.
  • Comprehensive Unit Tests: A new test file tests/test_get_model_name.py was added, containing extensive test cases to validate the get_model_name function's behavior across various scenarios, including core mappings, fallback mechanisms, and backward compatibility.
  • Streamlined Model Loading: The get_model_name function now accepts token and trust_remote_code parameters, allowing it to handle remote lookups and configuration changes more effectively during model loading.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • tests/test_get_model_name.py
    • Added new unit tests for get_model_name function, covering various model mapping scenarios and quantization types.
  • unsloth/models/_utils.py
    • Removed _redirect_fp8_to_bf16 function and its export from __all__.
  • unsloth/models/llama.py
    • Removed import and call to _redirect_fp8_to_bf16 from from_pretrained method.
    • Added imports for is_bfloat16_supported and get_quant_type.
  • unsloth/models/loader.py
    • Updated calls to get_model_name to pass token and trust_remote_code parameters.
  • unsloth/models/loader_utils.py
    • Introduced _resolve_with_mappers helper function to encapsulate model name resolution logic.
    • Modified get_model_name to use _resolve_with_mappers and accept token and trust_remote_code.
    • Simplified the return logic for get_model_name to ensure a model name is always returned.
  • unsloth/models/mapper.py
    • Added unsloth/Kimi-K2-Instruct-BF16 to __INT_TO_FLOAT_MAPPER.
    • Introduced _add_with_lower and _add_lower_only helper functions for consistent mapper population.
    • Refactored mapper population logic to utilize new helper functions for improved robustness and case-insensitivity.
  • unsloth/models/vision.py
    • Removed import and call to _redirect_fp8_to_bf16 from from_pretrained method.
Activity
  • The pull request description provides instructions on how to run the newly added test file.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

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 refactors the model name resolution logic, centralizing it in get_model_name and removing the now-redundant _redirect_fp8_to_bf16 function. It also introduces a comprehensive test suite for the new mapping logic. The changes are generally well-structured and improve maintainability. However, I've identified a critical issue in the refactored get_model_name function that breaks the offline FP8 quantization path. Additionally, the new tests could be expanded to cover load_in_fp8 scenarios to ensure full coverage of the updated functionality.

I am having trouble creating individual review comments. Click here to see my feedback.

unsloth/models/loader_utils.py (265-268)

critical

This refactoring seems to have introduced a bug. The logic in unsloth/models/loader.py (line 376) relies on get_model_name returning None to trigger offline FP8 quantization when load_in_fp8 is true and a pre-quantized model is not found.

The new implementation of get_model_name always returns a model name string, which will prevent the offline quantization path from being taken.

To fix this, you should restore the previous behavior for the load_in_fp8 case, which is to return new_model_name directly, even if it's None.

    if load_in_fp8 != False:
        # Handle on the fly TorchAO FP8 quantization
        return new_model_name

    if new_model_name is None:
        new_model_name = model_name

    return new_model_name

tests/test_get_model_name.py (12-18)

medium

The tests for get_model_name don't seem to cover cases where load_in_fp8 is True. Since the signature of get_model_name has been updated to include load_in_fp8, it would be beneficial to expand the test matrix to include this parameter to ensure the new logic is fully tested.

You could update _assert_mapping to accept load_in_fp8 and then update the test cases in test_resolution_matrix accordingly. This would also require updating the loop in test_resolution_matrix to handle the new parameter.

    def _assert_mapping(self, model_name, load_in_4bit, load_in_fp8, expected, should_change):
        mapped = get_model_name(model_name, load_in_4bit = load_in_4bit, load_in_fp8 = load_in_fp8)
        self.assertEqual(mapped.lower(), expected.lower())
        if should_change:
            self.assertNotEqual(mapped.lower(), model_name.lower())
        else:
            self.assertEqual(mapped.lower(), model_name.lower())
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

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: dc7ddd8831

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

Comment on lines +271 to +272
if new_model_name is None:
new_model_name = model_name

Choose a reason for hiding this comment

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

P1 Badge Preserve None sentinel for unresolved FP8 mapping

This fallback changes get_model_name(..., load_in_fp8=True) from returning None (the sentinel used by the loaders to trigger _offline_quantize_to_fp8) to always returning the original model string. In both loader flows, offline FP8 quantization is gated on new_model_name is None (see unsloth/models/loader.py around the if new_model_name is None and load_in_fp8 != False branches), so this makes that path unreachable for environments without vllm>=0.12 and can leave load_in_fp8 requests unfulfilled or failing later during load.

Useful? React with 👍 / 👎.

Comment on lines +614 to +616
# Handle FP8 models: get_model_name has already redirected this to BF16 sibling if the model ships with
# FP8 weights. We just need to update it here for sanity.
auto_config.model_name = model_name

Choose a reason for hiding this comment

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

P1 Badge Restore runtime FP8-to-BF16 redirect for unmapped models

This now assumes get_model_name already handled FP8-weight repos, but get_model_name only uses static mappers and does not inspect quantization_config or probe a -BF16 sibling. As a result, FP8 models that are not explicitly listed in mapper.py no longer get redirected before load; they continue with an FP8 config in non-FP8 modes, which can trigger unsupported-load failures that the previous _redirect_fp8_to_bf16 path avoided.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant