Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for offloading when using TorchAO >= 0.7.0 #3332

Merged
merged 5 commits into from
Jan 13, 2025

Conversation

a-r-r-o-w
Copy link
Member

What does this PR do?

There was a breaking change made in TorchAO v0.7.0, where layout_tensor was made an internal private attribute and tensor_impl was added as another parameter.

This causes sequential offloading implementation to break in Diffusers: huggingface/diffusers#10470

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@SunMarc

@a-r-r-o-w a-r-r-o-w requested a review from SunMarc January 9, 2025 07:18
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@sayakpaul
Copy link
Member

Cc: @jerryzh168. Perhaps these things could be handled better?

Copy link
Member

@BenjaminBossan BenjaminBossan left a comment

Choose a reason for hiding this comment

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

Thanks for providing this PR to handle the new argument name. I have a small comment to simplify the code.

Perhaps these things could be handled better?

I don't know the reason for the renaming, but I agree that ideally this can be avoided (PEFT also had an error because of that).

src/accelerate/utils/modeling.py Outdated Show resolved Hide resolved
a-r-r-o-w and others added 2 commits January 9, 2025 12:47
Co-Authored-By: Benjamin Bossan <BenjaminBossan@users.noreply.github.com>

Co-Authored-By: Xuehai Pan <XuehaiPan@pku.edu.cn>
Copy link
Member

@SunMarc SunMarc left a comment

Choose a reason for hiding this comment

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

Thanks for the fix !

Copy link
Member

@BenjaminBossan BenjaminBossan left a comment

Choose a reason for hiding this comment

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

Thanks, LGTM.

@a-r-r-o-w
Copy link
Member Author

cc @muellerzr for merge :)

@jerryzh168
Copy link

jerryzh168 commented Jan 11, 2025

Cc: @jerryzh168. Perhaps these things could be handled better?

I remember discussing with @bdhirsh before, and seems like no better way, but let me confirm with him

problem is: "how to we change the device for a nn.Parameter(tensor_subclass) object" right?

@nitinmukesh
Copy link

Gentle reminder to merge this, if complete.

@SunMarc SunMarc merged commit f0b0305 into huggingface:main Jan 13, 2025
25 checks passed
@a-r-r-o-w a-r-r-o-w deleted the fix-torchao-v0.7.0 branch January 13, 2025 16:14
@jerryzh168
Copy link

@sayakpaul I looked a bit, according to #3085 the issue is that AQT does not support requires_grad, would adding requires_grad arg be a better fix here?

new_value.quant_max,
new_value.zero_point_domain,
)
new_value = torch.nn.Parameter(param_cls(*args), requires_grad=old_value.requires_grad).to(device)
Copy link

Choose a reason for hiding this comment

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

in the interest of making this easier to maintain - it seems like the most painful bit is that you are running one of AO's tensor subclass constructors, which does not have strong BC guarantees.

Is there a reason you need to call the constructor (to construct a new instance of this subclass) in the first place?

It sounds like the purpose of this logic is to take any parameters (which may be subclasses) that live on the wrong device, and in-place move them to the right device. So stepping back a bit:

(1) if you can, I would just call module.to(device) directly, which will save you from having to loop over and re-assign the params individually (hopefully this just works for you - if not, maybe we should discuss any problems with other core folks)

(2) if you need to reassign some params individually, can you do it without needing to reconstruct the subclass directly? I would probably do something like this:

# option 1:
new_value = torch.nn.Parameter(new_value.to(device=device), requires_grad=old_value.requires_grad)
# option 2:
new_value = torch.nn.Parameter(new_value, requires_grad=old_value.requires_grad).to(device=device)

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

We don't really need it as you said. This was done for better compatibility with bitsandbytes as you can see in this PR https://github.com/huggingface/accelerate/pull/539/files. Would you like to check @a-r-r-o-w if the changes proposed by @bdhirsh works ?

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.

9 participants