Skip to content

Fix KeyError when loading ModelOpt FP8 checkpoint for Falcon-H1R-7B#19000

Open
Edwardf0t1 wants to merge 2 commits intosgl-project:mainfrom
Edwardf0t1:bugfix/falcon-h1-fp8-load-weights-keyerror
Open

Fix KeyError when loading ModelOpt FP8 checkpoint for Falcon-H1R-7B#19000
Edwardf0t1 wants to merge 2 commits intosgl-project:mainfrom
Edwardf0t1:bugfix/falcon-h1-fp8-load-weights-keyerror

Conversation

@Edwardf0t1
Copy link
Collaborator

@Edwardf0t1 Edwardf0t1 commented Feb 19, 2026

[FalconH1] Fix KeyError when loading FP8 checkpoint with mamba scale tensors

The load_weights method in FalconH1ForCausalLM raised a KeyError when loading
a ModelOpt FP8 checkpoint that contains quantization scale tensors (e.g.
mamba.in_proj.input_scale) with no corresponding registered model parameter.

Motivation

Modifications

  1. Only skip .output_scale, .k_scale, .v_scale (KV cache scales, no model params)
  2. Dequantize only when the target parameter is BF16 (param.dtype != float8_e4m3fn) — applies to mamba's BF16 in_proj/out_proj
  3. Load FP8 weights directly for attention/MLP layers (target params are FP8)
  4. Load weight_scale/input_scale through the normal path — for FP8 layers they land in the registered PerTensorScaleParameter; for mamba they fall through the name not in params_dict guard and get skipped naturally

Accuracy Tests

  #!/usr/bin/env python3                                                                                                                                                                                    
  """                                                                        
  Test script for FalconH1 FP8 model loading fix.                                                                                                                                                           
  Start the server first:                                                                                                                                                                                   
    python -m sglang.launch_server \
      --model /path/to/Falcon-H1R-7B-FP8 \
      --quantization modelopt \
      --tensor-parallel-size 1 \
      --reasoning-parser deepseek-r1 \
      --port 30000
  """

  import openai

  client = openai.Client(base_url="http://127.0.0.1:30000/v1", api_key="EMPTY")

  def test(prompt, expected_substr):
      response = client.chat.completions.create(
          model="default",
          messages=[{"role": "user", "content": prompt}],
          max_tokens=256,
          temperature=0,
      )
      content = response.choices[0].message.content
      print(f"Q: {prompt}")
      print(f"A: {content}")
      assert expected_substr.lower() in content.lower(), \
          f"Expected '{expected_substr}' in response, got: {content}"
      print("PASS\n")

  # Basic factual question
  test("What is the capital of France?", "Paris")

  # Arithmetic — exercises reasoning parser
  test("What is 2+2? Answer with just the number.", "4")

  print("All tests passed.")

Benchmarking and Profiling

Checklist

Review Process

  1. Ping Merge Oncalls to start the PR flow. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • /tag-run-ci-label, /rerun-failed-ci, /tag-and-rerun-ci
  4. After green CI and required approvals, ask Merge Oncalls to merge.

…tensors

Signed-off-by: Zhiyu Cheng <zhiyuc@nvidia.com>
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Edwardf0t1, 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 addresses a critical issue preventing the successful loading of ModelOpt FP8 checkpoints for Falcon-H1R-7B models. By introducing a robust check for parameter existence, it ensures that the loading process can gracefully handle quantization scale tensors without encountering a KeyError, thereby improving the stability and compatibility of the model loading mechanism.

Highlights

  • KeyError Fix: Resolved a KeyError that occurred when loading ModelOpt FP8 checkpoints for Falcon-H1R-7B models, specifically when quantization scale tensors lacked corresponding registered model parameters.

🧠 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
  • python/sglang/srt/models/falcon_h1.py
    • Added a missing-key guard to prevent KeyError during parameter loading.
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 addresses a KeyError encountered when loading ModelOpt FP8 checkpoints for Falcon-H1R-7B models. The issue arises from quantization scale tensors in the checkpoint that lack corresponding registered model parameters. The fix introduces a necessary guard to verify the existence of a tensor name within the model's parameter dictionary before access, effectively preventing the KeyError. This change is correct, minimal, and aligns with existing patterns in the codebase, making it a solid improvement.

Signed-off-by: Zhiyu Cheng <zhiyuc@nvidia.com>
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

Comments