Fix dtype mismatch for non-quantized layers when autocast is disabled#478
Conversation
When autocast is disabled (e.g. models on the FORCE_FLOAT32 list like Gemma3) and a non-quantized base layer has float32 weights (e.g. SiGLIP vision encoder), passing float16 activations to F.linear causes a dtype mismatch error. Add a dtype guard before the base_layer call in the compiled LoRA forward path: when autocast is not enabled, cast the input to match the base layer weight dtype. This only affects non-quantized layers since the 8-bit code path already replaces the base_layer call above this point.
Summary of ChangesHello @danielhanchen, 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 resolves a dtype mismatch issue that occurs in specific model configurations (e.g., Gemma3 with SiGLIP) when autocast is disabled. The core of the fix involves adding a dtype check and input casting to ensure compatibility between input tensors and layer weights, preventing Highlights
🧠 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
Activity
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request aims to fix a dtype mismatch error for non-quantized layers when autocast is disabled. The logic of the fix is sound, but the implementation introduces an indentation error in the generated code string, which would cause a syntax error at runtime. I've provided a critical comment with a suggested fix to correct the indentation.
| source = source.replace( | ||
| "result = self.base_layer(x, *args, **kwargs)", | ||
| "if not torch.is_autocast_enabled() and hasattr(self.base_layer, 'weight') " | ||
| "and self.base_layer.weight is not None " | ||
| "and x.dtype != self.base_layer.weight.dtype:\n" | ||
| " x = x.to(self.base_layer.weight.dtype)\n" | ||
| " result = self.base_layer(x, *args, **kwargs)", | ||
| ) |
There was a problem hiding this comment.
The indentation in the replacement string is incorrect and will cause an IndentationError when the generated code is executed. The result = ... line is indented less than the if block's body but is not aligned with the if statement itself.
To fix this, the result = self.base_layer(x, *args, **kwargs) line should be at the same indentation level as the if statement (i.e., unindented within the replacement string), and the body of the if statement should be indented relative to it (e.g., by 4 spaces).
| source = source.replace( | |
| "result = self.base_layer(x, *args, **kwargs)", | |
| "if not torch.is_autocast_enabled() and hasattr(self.base_layer, 'weight') " | |
| "and self.base_layer.weight is not None " | |
| "and x.dtype != self.base_layer.weight.dtype:\n" | |
| " x = x.to(self.base_layer.weight.dtype)\n" | |
| " result = self.base_layer(x, *args, **kwargs)", | |
| ) | |
| source = source.replace( | |
| "result = self.base_layer(x, *args, **kwargs)", | |
| "if not torch.is_autocast_enabled() and hasattr(self.base_layer, 'weight') " | |
| "and self.base_layer.weight is not None " | |
| "and x.dtype != self.base_layer.weight.dtype:\n" | |
| " x = x.to(self.base_layer.weight.dtype)\n" | |
| "result = self.base_layer(x, *args, **kwargs)", | |
| ) |
The replacement string previously hardcoded space counts (12, 8) that assumed the dedented source had `result = self.base_layer(...)` at exactly 8-space indent. Use re.search to detect the actual indentation so the fix adapts if peft changes its nesting depth.
Summary
Details
Models on the
FORCE_FLOAT32list (like Gemma3) have autocast disabled (fp16=False, bf16=False). When such models contain non-quantizednn.Linearlayers with float32 weights (e.g. SiGLIP vision encoder), passing float16 activations toF.linearcauses:The compiled LoRA forward calls
self.base_layer(x, *args, **kwargs)without any dtype guard. The fix adds a check before this call: when autocast is not enabled and the base layer has a weight tensor with a different dtype than the input, cast the input to match.This only affects non-quantized layers. For 8-bit quantized layers, the
base_layercall is already replaced by_call_8bit_base_layer(...)earlier in the compilation pipeline, so thesource.replace()is naturally a no-op for those layers.Companion PR: unslothai/unsloth#4005
Test plan