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

[Cherry-Pick][Performance]Remove CudaStreamSychornize in ClipGradByGlobalNorm and fix shape op #42170

Merged
merged 2 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion paddle/phi/kernels/shape_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,7 @@ PD_REGISTER_KERNEL(shape,
double,
phi::dtype::complex<float>,
phi::dtype::complex<double>,
phi::dtype::float16) {}
phi::dtype::float16) {
kernel->InputAt(0).SetBackend(phi::Backend::ALL_BACKEND);
}
#endif
20 changes: 15 additions & 5 deletions python/paddle/fluid/clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,10 +468,15 @@ class ClipGradByGlobalNorm(ClipGradBase):
sdg.step()
"""

def __init__(self, clip_norm, group_name="default_group"):
def __init__(self,
clip_norm,
group_name="default_group",
auto_skip_clip=False):
super(ClipGradByGlobalNorm, self).__init__()
self.clip_norm = float(clip_norm)
self.group_name = group_name
assert isinstance(auto_skip_clip, bool)
self.auto_skip_clip = auto_skip_clip

def __str__(self):
return "Gradient Clip By GlobalNorm, global_norm=%f" % (self.clip_norm)
Expand Down Expand Up @@ -524,14 +529,19 @@ def _dygraph_clip(self, params_grads):
max_global_norm = layers.fill_constant(
shape=[1], dtype=global_norm_var.dtype, value=self.clip_norm)

# only when global_norm_var > max_global_norm, grad need clip
need_clip = False
if global_norm_var > max_global_norm:
if not self.auto_skip_clip: # always apply clip
need_clip = True
clip_var = layers.elementwise_div(
x=max_global_norm,
y=layers.elementwise_max(
x=global_norm_var, y=max_global_norm))
elif global_norm_var > max_global_norm:
# only when global_norm_var > max_global_norm, grad need clip
need_clip = True

if need_clip:
clip_var = layers.elementwise_div(
x=max_global_norm, y=global_norm_var)

for p, g in params_grads:
if g is None:
continue
Expand Down