fix: cast input to weight dtype in PatchEmbed and FluxTransformer2DModel to prevent dtype mismatch#13303
Open
s-zx wants to merge 1 commit intohuggingface:mainfrom
Open
fix: cast input to weight dtype in PatchEmbed and FluxTransformer2DModel to prevent dtype mismatch#13303s-zx wants to merge 1 commit intohuggingface:mainfrom
s-zx wants to merge 1 commit intohuggingface:mainfrom
Conversation
…ype mismatch When running SD3/FLUX models on CPU with torch_dtype=float16, the projection layers (Conv2d in PatchEmbed, Linear in FluxTransformer2DModel) may have float32 weights/bias while receiving half-precision inputs, causing 'Input type (c10::Half) and bias type (float) should be the same'. Cast inputs to match layer weight dtype before calling the projection. Fixes huggingface#13300
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fixes dtype mismatch errors when running SD3 or FLUX models on CPU with
torch_dtype=torch.float16.When the model's projection layers (Conv2d in
PatchEmbed, Linear inFluxTransformer2DModel) are in float32 but receive float16 inputs, PyTorch raises aRuntimeErroron CPU because autocast is not available there:The fix casts the input tensor to match the layer weight dtype before calling the projection:
PatchEmbed.forward:latent.to(self.proj.weight.dtype)before Conv2d callFluxTransformer2DModel.forward:hidden_states.to(self.x_embedder.weight.dtype)before Linear callFixes #13300