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

Factor out loss_fn to share code with pipeline par #203

Merged
merged 4 commits into from
Apr 5, 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
25 changes: 15 additions & 10 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ def main(job_config: JobConfig):
dp_rank,
)

# loss_parallel enables dispatching to efficient loss operators
loss_parallel_ctx = (
loss_parallel()
if parallel_dims.loss_parallel_enabled
else contextlib.nullcontext()
)

# loss fn can be shared by pipeline-parallel or non-pp execution
def loss_fn(pred, labels):
Copy link
Contributor

Choose a reason for hiding this comment

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

is this loss_fn have any actual difference compare to the F.cross_entropy be directly used by PP?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

views

return F.cross_entropy(pred.flatten(0, 1), labels.flatten(0, 1))

# build model (using meta init)
model_cls = model_name_to_cls[model_name]
model_config = models_config[model_name][job_config.model.flavor]
Expand Down Expand Up @@ -268,16 +279,10 @@ def main(job_config: JobConfig):

optimizer.zero_grad()

# forward
pred = model(input_ids)

with (
loss_parallel()
if parallel_dims.loss_parallel_enabled
else contextlib.nullcontext()
):
loss = F.cross_entropy(pred.flatten(0, 1), labels.flatten(0, 1))
# backward
# forward / backward
with loss_parallel_ctx:
pred = model(input_ids)
loss = loss_fn(pred, labels)
loss.backward()

# clip gradients
Expand Down
Loading