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

modify clip grad #10441

Closed
wants to merge 2 commits into from
Closed
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
18 changes: 18 additions & 0 deletions python/oneflow/nn/utils/clip_grad.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ def clip_grad_norm_(
]
total_norm = norms[0] if len(norms) == 1 else flow.min(flow.stack(norms))
else:
"""
# data parallel
total_norm = flow.linalg.vector_norm(
flow.stack(
[
Expand All @@ -130,6 +132,22 @@ def clip_grad_norm_(
),
norm_type,
)
"""
# tensor parallel:
partial_grad_squre_sum = flow.sum(
flow.stack(
[
flow.sum(flow.pow(p.grad.detach(), norm_type)).to_local()
for p in parameters
]
)
)

flow.comm.all_reduce(partial_grad_squre_sum)
total_norm = flow.pow(partial_grad_squre_sum, 1 / norm_type)
total_norm = total_norm.to_global(
sbp=sbp_broadcast, placement=param0_placement
)
if error_if_nonfinite and flow.logical_or(
total_norm.isnan(), total_norm.isinf()
):
Expand Down