Skip to content

Conversation

@tenderness-git
Copy link
Member

No description provided.

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.

Summary of Changes

Hello @tenderness-git, 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 PR addresses a bug in the offloading mechanism within diffsynth_engine/utils/offload.py. Specifically, it ensures that parameters and buffers with the torch.float8_e4m3fn data type are correctly moved to CPU memory before being pinned, preventing potential issues during the offload process.

Highlights

  • Offload Fix for Float8 Tensors: Implemented a fix in diffsynth_engine/utils/offload.py to correctly handle torch.float8_e4m3fn tensors during the offloading process.
  • Conditional CPU Transfer: Added checks to move param.data and buffer.data to the CPU if their dtype is torch.float8_e4m3fn before pin_memory() is called, resolving an offload bug.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

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 issue 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 or fill out our survey to provide feedback.

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 aims to fix a bug in the CPU offloading logic, specifically for float8_e4m3fn tensors. However, the current implementation has a flaw where it still attempts to call pin_memory() on these unsupported tensor types, which would lead to a runtime error. My review provides a correction for this issue to ensure the offloading works as expected.

Comment on lines 30 to 37
for name, param in module.named_parameters(recurse=recurse):
if param.dtype == torch.float8_e4m3fn:
param.data = param.data.to(device='cpu')
param.data = param.data.pin_memory()
offload_param_dict[name] = param.data
param.data = param.data.to(device=device)
for name, buffer in module.named_buffers(recurse=recurse):
if buffer.dtype == torch.float8_e4m3fn:
buffer.data = buffer.data.to(device='cpu')
buffer.data = buffer.data.pin_memory()
offload_param_dict[name] = buffer.data
buffer.data = buffer.data.to(device=device)
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

The current logic for both parameters and buffers will attempt to call pin_memory() on float8_e4m3fn tensors. This is not supported by PyTorch and will raise a RuntimeError, causing a crash.

The if condition you've added only moves the tensor to CPU but does not prevent the subsequent pin_memory() call which is outside the conditional block.

To fix this, pin_memory() should only be called on tensors with supported data types. The logic for both parameters and buffers should be corrected.

Suggested change
for name, param in module.named_parameters(recurse=recurse):
if param.dtype == torch.float8_e4m3fn:
param.data = param.data.to(device='cpu')
param.data = param.data.pin_memory()
offload_param_dict[name] = param.data
param.data = param.data.to(device=device)
for name, buffer in module.named_buffers(recurse=recurse):
if buffer.dtype == torch.float8_e4m3fn:
buffer.data = buffer.data.to(device='cpu')
buffer.data = buffer.data.pin_memory()
offload_param_dict[name] = buffer.data
buffer.data = buffer.data.to(device=device)
for name, param in module.named_parameters(recurse=recurse):
if param.dtype != torch.float8_e4m3fn:
param.data = param.data.pin_memory()
offload_param_dict[name] = param.data
param.data = param.data.to(device=device)
for name, buffer in module.named_buffers(recurse=recurse):
if buffer.dtype != torch.float8_e4m3fn:
buffer.data = buffer.data.pin_memory()
offload_param_dict[name] = buffer.data
buffer.data = buffer.data.to(device=device)

@akaitsuki-ii akaitsuki-ii force-pushed the wangguan branch 2 times, most recently from 898ae75 to 0295065 Compare August 8, 2025 09:13
@akaitsuki-ii akaitsuki-ii changed the title fix offload bug fix cast fp8 Aug 8, 2025
@akaitsuki-ii akaitsuki-ii merged commit aeeeb5b into main Aug 8, 2025
@akaitsuki-ii akaitsuki-ii deleted the wangguan branch August 8, 2025 09:16
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.

3 participants