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

support TP-only parallelism #137

Merged
merged 2 commits into from
Mar 14, 2024
Merged
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
15 changes: 10 additions & 5 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ def build_optimizer(model, job_config: JobConfig):

def build_grad_scaler(model):
# apply gradient scaling if mixed precision training is enabled with fp16 param dtype
if model.mixed_precision.param_dtype == torch.float16:
# NOTE: currently mixed precision training is supported only when FSDP is used
if isinstance(model, FSDP) and model.mixed_precision.param_dtype == torch.float16:
enable_grad_scaling = True
logger.info("Enabling gradient scaling for mixed precision training")
else:
Expand Down Expand Up @@ -160,13 +161,12 @@ def main(job_config: JobConfig):
model, world_mesh, parallel_dims, job_config
)

# to use FSDP-customized gradient scaler and gradient clipping solutions
assert isinstance(model, FSDP)

# build optimizer after applying parallelisms to the model
optimizer = build_optimizer(model, job_config)
scheduler = get_lr_scheduler(optimizer, job_config)

# build grad scaler which is effective only when mixed precision training
# is enabled with fp16 param dtype under FSDP
scaler = build_grad_scaler(model)

metric_logger = build_metric_logger(job_config)
Expand Down Expand Up @@ -240,7 +240,12 @@ def main(job_config: JobConfig):

# clip gradients (after unscaling gradients of the optimizer's params)
scaler.unscale_(optimizer)
model.clip_grad_norm_(job_config.training.max_norm)
if isinstance(model, FSDP):
model.clip_grad_norm_(job_config.training.max_norm)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this can be simplified once FSDP2 kicked in. cc @awgu

else:
torch.nn.utils.clip_grad_norm_(
model.parameters(), job_config.training.max_norm
)

# optimizer step
# If gradients don't contain infs/NaNs, optimizer.step() is then called;
Expand Down
Loading