提交 3233b2b3 编写于 作者: K Kexin Zhao

update test

上级 5e36757c
...@@ -187,11 +187,13 @@ def set_output_grad(scope, outputs, place, feed_dict=None): ...@@ -187,11 +187,13 @@ def set_output_grad(scope, outputs, place, feed_dict=None):
class TestBatchNormOpInference(OpTest): class TestBatchNormOpInference(OpTest):
def setUp(self):
self.dtype = np.float32
def __assert_close(self, tensor, np_array, msg, atol=1e-4): def __assert_close(self, tensor, np_array, msg, atol=1e-4):
self.assertTrue(np.allclose(np.array(tensor), np_array, atol=atol), msg) self.assertTrue(np.allclose(np.array(tensor), np_array, atol=atol), msg)
def test_inference(self): def check_with_place(place, data_layout, dtype, shape):
def test_with_place(place, data_layout, dtype, shape):
epsilon = 0.00001 epsilon = 0.00001
if len(shape) == 2: if len(shape) == 2:
x_shape = shape x_shape = shape
...@@ -213,33 +215,32 @@ class TestBatchNormOpInference(OpTest): ...@@ -213,33 +215,32 @@ class TestBatchNormOpInference(OpTest):
mean = np.zeros(scale_shape).astype(dtype) mean = np.zeros(scale_shape).astype(dtype)
variance = np.ones(scale_shape).astype(dtype) variance = np.ones(scale_shape).astype(dtype)
y_out = _reference_testing(x_val, scale_val, bias_val, mean, y_out = _reference_testing(x_val, scale_val, bias_val, mean, variance,
variance, epsilon, epsilon, data_layout).astype(dtype)
data_layout).astype(dtype)
scope = core.Scope() scope = core.Scope()
# create input # create input
x_tensor = create_or_get_tensor( x_tensor = create_or_get_tensor(scope, "x_val",
scope, "x_val", OpTest.np_dtype_to_fluid_dtype(x_val), place) OpTest.np_dtype_to_fluid_dtype(x_val),
place)
scale_tensor = create_or_get_tensor( scale_tensor = create_or_get_tensor(
scope, "scale_val", scope, "scale_val",
OpTest.np_dtype_to_fluid_dtype(scale_val), place) OpTest.np_dtype_to_fluid_dtype(scale_val), place)
bias_tensor = create_or_get_tensor( bias_tensor = create_or_get_tensor(
scope, "bias_val", scope, "bias_val", OpTest.np_dtype_to_fluid_dtype(bias_val), place)
OpTest.np_dtype_to_fluid_dtype(bias_val), place) mean_tensor = create_or_get_tensor(scope, "mean",
mean_tensor = create_or_get_tensor( OpTest.np_dtype_to_fluid_dtype(mean),
scope, "mean", OpTest.np_dtype_to_fluid_dtype(mean), place) place)
variance_tensor = create_or_get_tensor( variance_tensor = create_or_get_tensor(
scope, "variance", scope, "variance", OpTest.np_dtype_to_fluid_dtype(variance), place)
OpTest.np_dtype_to_fluid_dtype(variance), place)
# create output # create output
y_tensor = create_or_get_tensor(scope, "y_out", None, place) y_tensor = create_or_get_tensor(scope, "y_out", None, place)
saved_mean_tensor = create_or_get_tensor(scope, "saved_mean", None, saved_mean_tensor = create_or_get_tensor(scope, "saved_mean", None,
place) place)
saved_variance_tensor = create_or_get_tensor( saved_variance_tensor = create_or_get_tensor(scope, "saved_variance",
scope, "saved_variance", None, place) None, place)
mean_out_tensor = mean_tensor mean_out_tensor = mean_tensor
variance_out_tensor = variance_tensor variance_out_tensor = variance_tensor
...@@ -265,21 +266,37 @@ class TestBatchNormOpInference(OpTest): ...@@ -265,21 +266,37 @@ class TestBatchNormOpInference(OpTest):
batch_norm_op.run(scope, place) batch_norm_op.run(scope, place)
# check inference result # check inference result
self.__assert_close( self.__assert_close(y_tensor, y_out,
y_tensor, y_out, "inference output are different at " + "inference output are different at " + str(place) +
str(place) + ", " + data_layout + ", " + str(np.dtype(dtype))) ", " + data_layout + ", " + str(np.dtype(dtype)))
def test_check_output(self):
places = [core.CPUPlace()] places = [core.CPUPlace()]
if core.is_compiled_with_cuda() and core.op_support_gpu("batch_norm"):
places.append(core.CUDAPlace(0))
for place in places:
for data_format in ["NCHW", "NHWC"]:
check_with_place(place, data_format, self.dtype, [2, 3, 4, 5])
check_with_place(place, data_format, self.dtype, [2, 3])
class TestFP16BatchNormOpInference(TestBatchNormOpInference):
def setUp(self):
self.dtype = np.float16
def test_check_output(self):
places = []
if core.is_compiled_with_cuda() and core.op_support_gpu("batch_norm"): if core.is_compiled_with_cuda() and core.op_support_gpu("batch_norm"):
place = core.CUDAPlace(0) place = core.CUDAPlace(0)
if self.dtype != np.float16 or core.is_float16_supported(place): if core.is_float16_supported(place):
places.append(place) places.append(place)
for place in places: for place in places:
for data_format in ["NCHW", "NHWC"]: for data_format in ["NCHW", "NHWC"]:
for dtype in [np.float32, np.float16]: check_output_with_place(place, data_format, self.dtype,
test_with_place(place, data_format, dtype, [2, 3, 4, 5]) [2, 3, 4, 5])
test_with_place(place, data_format, dtype, [2, 3]) check_output_with_place(place, data_format, self.dtype, [2, 3])
class TestBatchNormOpTraining(OpTest): class TestBatchNormOpTraining(OpTest):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册