Skip to content

Fix float8_e4m3fnuz input scaling range#1528

Open
maskyuanzh wants to merge 1 commit into
modelscope:mainfrom
maskyuanzh:fix-fp8-e4m3fnuz-max
Open

Fix float8_e4m3fnuz input scaling range#1528
maskyuanzh wants to merge 1 commit into
modelscope:mainfrom
maskyuanzh:fix-fp8-e4m3fnuz-max

Conversation

@maskyuanzh

Copy link
Copy Markdown

Summary

In diffsynth/core/vram/layers.py, AutoWrappedLinear.fp8_linear() currently initializes the FP8 input scaling threshold to 448 and divides it by 2 for torch.float8_e4m3fnuz:

fp8_max = 448.0
if self.computation_dtype == torch.float8_e4m3fnuz:
    fp8_max = fp8_max / 2.0

This produces a threshold of 224, while the maximum finite value defined for torch.float8_e4m3fnuz is 240.

This PR replaces the manually derived threshold with the maximum finite value defined by the selected dtype:

fp8_max = torch.finfo(self.computation_dtype).max

As a result:

  • torch.float8_e4m3fn continues to use 448.
  • torch.float8_e4m3fnuz now uses 240 instead of 224.

Scaling behavior

Let (M) be the maximum absolute value of an input row. For torch.float8_e4m3fnuz, the old and new scales are:

$$ s_{\mathrm{old}}(M) = \max\left(\frac{M}{224}, 1\right) $$

$$ s_{\mathrm{new}}(M) = \max\left(\frac{M}{240}, 1\right) $$

Therefore:

  • For $M \leq 224$, the behavior is unchanged.
  • For $224 < M \leq 240$, the new implementation avoids unnecessary scaling.
  • For $M > 240$, the row is still scaled, but the full finite range of torch.float8_e4m3fnuz is used.

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

Copy link
Copy Markdown
Contributor

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 updates the FP8 linear layer implementation in diffsynth/core/vram/layers.py to dynamically determine the maximum representable value for the selected FP8 data type using torch.finfo(self.computation_dtype).max, replacing the previous hardcoded values and conditional logic. No review comments were provided, and there is no additional feedback.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

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