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

Select the DeepSpeedCPUOptimizer based on the original optimizer class. #3255

Merged
merged 4 commits into from
Dec 2, 2024

Conversation

eljandoubi
Copy link
Contributor

What does this PR do?

For DeepSpeed optimizer offloading, select the appropriate optimizer based on the original optimizer class.

Who can review?

@BenjaminBossan
Copy link
Member

Thanks for this update. Just wondering aloud: If the optimizer is neither Adam, nor Adagrad, nor Lion, what would be the right thing to do? As is, Adam is used, is that correct?

@eljandoubi
Copy link
Contributor Author

eljandoubi commented Nov 25, 2024

@BenjaminBossan By default, I have kept the Adam as it was for everyone torch optimizer.

Copy link
Collaborator

@muellerzr muellerzr 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 doing this! Overall I'm a fan, however let's abstract this out to a util func to keep the Accelerator a bit more readable

Comment on lines 1852 to 1879
# For DeepSpeedCPUAdagrad
if compare_versions("deepspeed", ">=", "0.5.5"):
# Check if the optimizer is PyTorch's Adagrad.
is_ada = isinstance(optimizer, torch.optim.Adagrad)
# If not, and bitsandbytes is available,
# # check if the optimizer is the 32-bit bitsandbytes Adagrad.
if is_bnb_available() and not is_ada:
import bitsandbytes.optim as bnb_opt

is_ada = (
isinstance(optimizer, (bnb_opt.Adagrad, bnb_opt.Adagrad32bit))
and optimizer.optim_bits == 32
)
if is_ada:
from deepspeed.ops.adagrad import DeepSpeedCPUAdagrad

optimizer_class = DeepSpeedCPUAdagrad

# For DeepSpeedCPULion
if is_bnb_available(min_version="0.38.0") and compare_versions("deepspeed", ">=", "0.11.0"):
from bitsandbytes.optim import Lion, Lion32bit

if isinstance(optimizer, (Lion, Lion32bit)) and optimizer.optim_bits == 32:
from deepspeed.ops.lion import DeepSpeedCPULion

optimizer_class = DeepSpeedCPULion

optimizer = optimizer_class(optimizer.param_groups, **defaults)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems a bit bulky for the Accelerator, can we abstract this out to a deepspeed util of map_pytorch_optim_to_deepspeed?

@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.

@eljandoubi
Copy link
Contributor Author

@muellerzr What do you think of it like this?

Copy link
Collaborator

@muellerzr muellerzr left a comment

Choose a reason for hiding this comment

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

Nice! Looks great to me.

cc @XuehaiPan for viz :)

@muellerzr muellerzr merged commit b626ef5 into huggingface:main Dec 2, 2024
25 checks passed
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.

4 participants