Skip to content
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
Collaborator

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