remove some fluid api (elementwise_div elementwise_mul sqrt reduce_sum). (#2859)

pull/2863/head
zxcd 2 years ago committed by GitHub
parent b5764e9f74
commit 64aeb6dccc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -44,7 +44,7 @@ class ClipGradByGlobalNormWithLog(paddle.nn.ClipGradByGlobalNorm):
merge_grad = layers.merge_selected_rows(g) merge_grad = layers.merge_selected_rows(g)
merge_grad = layers.get_tensor_from_selected_rows(merge_grad) merge_grad = layers.get_tensor_from_selected_rows(merge_grad)
square = paddle.square(merge_grad) square = paddle.square(merge_grad)
sum_square = layers.reduce_sum(square) sum_square = paddle.sum(square)
sum_square_list.append(sum_square) sum_square_list.append(sum_square)
# debug log, not dump all since slow down train process # debug log, not dump all since slow down train process
@ -57,14 +57,15 @@ class ClipGradByGlobalNormWithLog(paddle.nn.ClipGradByGlobalNorm):
return params_grads return params_grads
global_norm_var = layers.concat(sum_square_list) global_norm_var = layers.concat(sum_square_list)
global_norm_var = layers.reduce_sum(global_norm_var) global_norm_var = paddle.sum(global_norm_var)
global_norm_var = layers.sqrt(global_norm_var) global_norm_var = paddle.sqrt(global_norm_var)
# debug log # debug log
logger.debug(f"Grad Global Norm: {float(global_norm_var)}!!!!") logger.debug(f"Grad Global Norm: {float(global_norm_var)}!!!!")
max_global_norm = layers.fill_constant( max_global_norm = layers.fill_constant(
shape=[1], dtype=global_norm_var.dtype, value=self.clip_norm) shape=[1], dtype=global_norm_var.dtype, value=self.clip_norm)
clip_var = layers.elementwise_div( clip_var = paddle.divide(
x=max_global_norm, x=max_global_norm,
y=paddle.maximum(x=global_norm_var, y=max_global_norm)) y=paddle.maximum(x=global_norm_var, y=max_global_norm))
for i, (p, g) in enumerate(params_grads): for i, (p, g) in enumerate(params_grads):
@ -73,7 +74,7 @@ class ClipGradByGlobalNormWithLog(paddle.nn.ClipGradByGlobalNorm):
if getattr(p, 'need_clip', True) is False: if getattr(p, 'need_clip', True) is False:
params_and_grads.append((p, g)) params_and_grads.append((p, g))
continue continue
new_grad = layers.elementwise_mul(x=g, y=clip_var) new_grad = paddle.multiply(x=g, y=clip_var)
params_and_grads.append((p, new_grad)) params_and_grads.append((p, new_grad))
# debug log, not dump all since slow down train process # debug log, not dump all since slow down train process

Loading…
Cancel
Save