未验证 提交 97bbab47 编写于 作者: L Lv Mengsi 提交者: GitHub

[Cherry pick] instance_norm, gradients and batch_norm (#21301)

* Fix gradients (#20857)

* fix_gradients

* fix_gradients, test=develop

* fix instance norm (#21042)

* fix instance norm

* update unitest,test=develop

* fix_bn

* revert unittest,test=develop
上级 03dda317
......@@ -418,7 +418,7 @@ class BatchNormGradKernel<platform::CUDADeviceContext, T>
}
} else {
if (d_x) {
BNBackwardData<T, block, framework::DataLayout::kNCHW><<<
BNBackwardData<T, block, framework::DataLayout::kNHWC><<<
grid2, block, 0, dev_ctx.stream()>>>(
d_y->data<T>(), scale->data<BatchNormParamType<T>>(),
saved_mean_data, x->data<T>(), saved_var_data, C, N, H * W * D,
......
......@@ -328,7 +328,7 @@ class InstanceNormGradKernel<platform::CUDADeviceContext, T>
epsilon, saved_mean_data, saved_var_data));
} else {
if (d_x) {
GradComputeDX<T, block><<<grid, block, 0, dev_ctx.stream()>>>(
GradComputeDX<T, block><<<NxC, block, 0, dev_ctx.stream()>>>(
d_y->data<T>(), scale->data<BatchNormParamType<T>>(),
saved_mean_data, x->data<T>(), saved_var_data, C, H * W * D,
d_x->data<T>());
......
......@@ -1247,9 +1247,13 @@ def calc_gradient(targets, inputs, target_gradients=None, no_grad_set=None):
target = targets[i]
if grad is None:
grad_name = _append_grad_suffix_(target.name)
target_shape = paddle.fluid.layers.shape(target)
target_shape = target.name + '_shape'
block.desc.append_op().copy_from(
_create_op_desc_("shape", {'Input': [target.name]},
{"Out": [target_shape]}, {}))
input_grad_names_set.add(target_shape)
op_desc = _create_op_desc_("fill_constant",
{"ShapeTensor": [target_shape.name]},
{"ShapeTensor": [target_shape]},
{"Out": [grad_name]}, {
"shape": target.shape,
"value": 1.0,
......
......@@ -79,7 +79,7 @@ class TestInstanceNormOpTraining(unittest.TestCase):
self.init_test_case()
def init_test_case(self):
self.use_global_stats = False
self.shape = [2, 3, 4, 5]
self.no_grad_set = set()
self.fetch_list = [
'y', 'saved_mean', 'saved_variance', 'x@GRAD', 'scale@GRAD',
......@@ -181,12 +181,19 @@ class TestInstanceNormOpTraining(unittest.TestCase):
"instance_norm"):
places.append(core.CUDAPlace(0))
for place in places:
test_with_place(place, [2, 3, 4, 5])
test_with_place(place, self.shape)
class TestInstanceNormOpTrainingCase1(TestInstanceNormOpTraining):
def init_test_case(self):
self.use_global_stats = False
self.shape = [2, 3, 4, 5]
self.no_grad_set = set(['scale@GRAD', 'bias@GRAD'])
self.fetch_list = ['y', 'saved_mean', 'saved_variance', 'x@GRAD']
class TestInstanceNormOpTrainingCase2(TestInstanceNormOpTraining):
def init_test_case(self):
self.shape = [20, 50, 4, 5]
self.no_grad_set = set(['scale@GRAD', 'bias@GRAD'])
self.fetch_list = ['y', 'saved_mean', 'saved_variance', 'x@GRAD']
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册