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

Register backward hook for the whole optim_dict to enable working at multi schedule pp #780

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions torchtitan/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,31 @@ def __init__(
) -> None:
self.optimizers = []
self.model_parts = model_parts
optim_dict = {}
for model in self.model_parts:
if name == "Adam":
# TODO: make the optimizer options configurable by toml/cmd args
optim_dict = {
param: torch.optim.Adam([param], **optimizer_kwargs)
for param in model.parameters()
}
optim_dict.update(
{
param: torch.optim.Adam([param], **optimizer_kwargs)
for param in model.parameters()
}
)
elif name == "AdamW":
optim_dict = {
param: torch.optim.AdamW([param], **optimizer_kwargs)
for param in model.parameters()
}
optim_dict.update(
{
param: torch.optim.AdamW([param], **optimizer_kwargs)
for param in model.parameters()
}
)
else:
raise NotImplementedError(f"Optimizer {name} not added.")

def optim_hook(param) -> None:
optim_dict[param].step()
optim_dict[param].zero_grad()
def optim_hook(param) -> None:
optim_dict[param].step()
optim_dict[param].zero_grad()

for model in self.model_parts:
for param in model.parameters():
if param.requires_grad:
param.register_post_accumulate_grad_hook(optim_hook)
Expand Down
Loading