def test_errors(self): with fluid.program_guard(fluid.Program(), fluid.Program()): # The input type of accuracy_op must be Variable. x1 = fluid.create_lod_tensor(np.array([[-1]]), [[1]], fluid.NPUPlace(0)) y1 = fluid.create_lod_tensor(np.array([[-1]]), [[1]], fluid.NPUPlace(0)) self.assertRaises(TypeError, fluid.layers.smooth_l1, x1, y1) # The input dtype of accuracy_op must be float32 or float64. x2 = fluid.layers.data(name='x2', shape=[4], dtype="int32") y2 = fluid.layers.data(name='x2', shape=[4], dtype="int32") self.assertRaises(TypeError, fluid.layers.smooth_l1, x2, y2)
def test_errors(self): with program_guard(Program(), Program()): # the input of elementwise_add must be Variable. x1 = fluid.create_lod_tensor( np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.NPUPlace(0)) y1 = fluid.create_lod_tensor( np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.NPUPlace(0)) self.assertRaises(TypeError, fluid.layers.elementwise_add, x1, y1) # the input dtype of elementwise_add must be float16 or float32 or float64 or int32 or int64 # float16 only can be set on GPU place x2 = fluid.layers.data(name='x2', shape=[3, 4, 5, 6], dtype="uint8") y2 = fluid.layers.data(name='y2', shape=[3, 4, 5, 6], dtype="uint8") self.assertRaises(TypeError, fluid.layers.elementwise_add, x2, y2)
def test_errors(self): with paddle.static.program_guard(paddle.static.Program()): # the input of elementwise_add must be Variable. x1 = fluid.create_lod_tensor( np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.NPUPlace(0)) y1 = fluid.create_lod_tensor( np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.NPUPlace(0)) self.assertRaises(TypeError, paddle.add, x1, y1) # the input dtype must be float16 or float32 or float64 or int32 or int64 x2 = paddle.static.data( name='x2', shape=[3, 4, 5, 6], dtype="uint8") y2 = paddle.static.data( name='y2', shape=[3, 4, 5, 6], dtype="uint8") self.assertRaises(TypeError, paddle.add, x2, y2)
def __test_vs(self, place=fluid.NPUPlace(0)): paddle.disable_static(place=place) linear_old = paddle.nn.Linear( 2, 2, weight_attr=paddle.nn.initializer.Constant(value=2.0), bias_attr=paddle.nn.initializer.Constant(value=2.0)) momentum_old = paddle.fluid.optimizer.Momentum( learning_rate=0.01, momentum=0.9, parameter_list=linear_old.parameters(), regularization=paddle.fluid.regularizer.L2Decay( regularization_coeff=0.1)) self.__update_params(momentum=momentum_old, linear=linear_old) linear_new = paddle.nn.Linear( 2, 2, weight_attr=paddle.nn.initializer.Constant(value=2.0), bias_attr=paddle.nn.initializer.Constant(value=2.0)) momentum_new = paddle.fluid.contrib.optimizer.Momentum( learning_rate=0.01, momentum=0.9, parameter_list=linear_new.parameters(), regularization=paddle.fluid.regularizer.L2Decay( regularization_coeff=0.1)) self.__update_params(momentum=momentum_new, linear=linear_new) self.assertEqual( (linear_old.weight.numpy() == linear_new.weight.numpy()).all(), True, 'the param weight updated by two Momentum optimizers should equal')
def run_static(self, use_npu=False): with fluid.program_guard(fluid.Program()): data_np = np.random.random((100, 100)).astype(np.float32) x = paddle.static.data('X', [100, 100]) y = paddle.cumsum(x) y2 = paddle.cumsum(x, axis=0) y3 = paddle.cumsum(x, axis=-1) y4 = paddle.cumsum(x, dtype='float32') y5 = paddle.cumsum(x, dtype=np.int32) y6 = paddle.cumsum(x, axis=-2) place = fluid.NPUPlace(0) if use_npu else fluid.CPUPlace() exe = fluid.Executor(place) exe.run(fluid.default_startup_program()) out = exe.run(feed={'X': data_np}, fetch_list=[ y.name, y2.name, y3.name, y4.name, y5.name, y6.name ]) z = np.cumsum(data_np) self.assertTrue(np.allclose(z, out[0])) z = np.cumsum(data_np, axis=0) self.assertTrue(np.allclose(z, out[1])) z = np.cumsum(data_np, axis=-1) self.assertTrue(np.allclose(z, out[2])) self.assertTrue(out[3].dtype == np.float32) self.assertTrue(out[4].dtype == np.int32) z = np.cumsum(data_np, axis=-2) self.assertTrue(np.allclose(z, out[5]))
def test_momentum_dygraph(self): paddle.disable_static(place=fluid.NPUPlace(0)) value = np.arange(26).reshape(2, 13).astype("float32") a = paddle.to_tensor(value) linear_1 = paddle.nn.Linear(13, 5) linear_2 = paddle.nn.Linear(5, 3) # This can be any optimizer supported by dygraph. adam = paddle.optimizer.Momentum(learning_rate=0.01, parameters=[{ 'params': linear_1.parameters() }, { 'params': linear_2.parameters(), 'weight_decay': 0.001, 'learning_rate': 0.1, 'momentum': 0.99 }], weight_decay=0.1, momentum=0.9) out = linear_1(a) out = linear_2(out) out.backward() adam.step() adam.clear_gradients()
def test_check_grad(self): if self.dtype == np.float16: return self.check_grad_with_place(fluid.NPUPlace(0), {'Input', 'Filter'}, 'Output', max_relative_error=0.03, numeric_place=paddle.CPUPlace())
def test_check_grad_no_input(self): self.check_grad_with_place( fluid.NPUPlace(0), ['Filter'], 'Output', max_relative_error=0.03, no_grad_set=set(['Input']), numeric_place=paddle.CPUPlace())
def run_trainer(self, args): device_id = int(os.getenv("FLAGS_selected_npus", "0")) place = fluid.NPUPlace(device_id) places = [place] # Test training for place in places: for layout in ["NCHW", "NHWC"]: self._compare(args, place, layout, False) # Test inference for place in places: for layout in ["NCHW", "NHWC"]: self._compare(args, place, layout, True) # Test FP16 - @TODO # self.dtype = np.float16 # self.atol = 1e-2 # Test training # for place in places: # for layout in ["NCHW", "NHWC"]: # self._compare(args, place, layout, False) # Test inference # for place in places: # for layout in ["NCHW", "NHWC"]: # self._compare(args, place, layout, True) sys.stdout.buffer.write( pickle.dumps( 'training, inference, fp32, fp16, NCHW, NHWC all passed'))
def test_BCELoss(self): input_np = np.random.uniform(0.1, 0.8, size=(20, 30)).astype(np.float32) label_np = np.random.randint(0, 2, size=(20, 30)).astype(np.float32) places = [fluid.CPUPlace()] if fluid.core.is_compiled_with_npu(): places.append(fluid.NPUPlace(0)) reductions = ['sum', 'mean', 'none'] for place in places: for reduction in reductions: static_result = test_static_layer(place, input_np, label_np, reduction) dy_result = test_dygraph_layer(place, input_np, label_np, reduction) expected = calc_bceloss(input_np, label_np, reduction) self.assertTrue(np.allclose(static_result, expected)) self.assertTrue(np.allclose(static_result, dy_result)) self.assertTrue(np.allclose(dy_result, expected)) static_functional = test_static_functional( place, input_np, label_np, reduction) dy_functional = test_dygraph_functional( place, input_np, label_np, reduction) self.assertTrue(np.allclose(static_functional, expected)) self.assertTrue(np.allclose(static_functional, dy_functional)) self.assertTrue(np.allclose(dy_functional, expected))
def test_api(self): x = fluid.data(shape=[100], dtype='int32', name='x') y = fluid.data(shape=[200], dtype='int32', name='y') input_1 = np.random.randint(0, 100, [ 100, ]).astype('int32') input_2 = np.random.randint(0, 100, [ 200, ]).astype('int32') out_1 = np.reshape(input_1, [100, 1]) out_1 = np.broadcast_to(out_1, [100, 200]) out_2 = np.reshape(input_2, [1, 200]) out_2 = np.broadcast_to(out_2, [100, 200]) exe = fluid.Executor(place=fluid.NPUPlace(0)) grid_x, grid_y = paddle.tensor.meshgrid(x, y) res_1, res_2 = exe.run(fluid.default_main_program(), feed={ 'x': input_1, 'y': input_2 }, fetch_list=[grid_x, grid_y]) self.assertTrue(np.allclose(res_1, out_1)) self.assertTrue(np.allclose(res_2, out_2))
def test_check_grad_no_filter(self): if self.dtype == np.float16: return self.check_grad_with_place(fluid.NPUPlace(0), ['Input'], 'Output', max_relative_error=0.03, no_grad_set=set(['Filter']), numeric_place=paddle.CPUPlace())
def test_out_name(self): with fluid.program_guard(fluid.Program()): np_x = np.array([0.1]) data = fluid.layers.data(name="X", shape=[1]) out = eval("paddle.%s(data, name='Y')" % self.op_type) place = fluid.NPUPlace(0) exe = fluid.Executor(place) result, = exe.run(feed={"X": np_x}, fetch_list=[out]) expected = eval("np.%s(np_x)" % self.op_type) self.assertEqual(result, expected)
def test_out(self): with fluid.program_guard(fluid.Program(), fluid.Program()): data = fluid.layers.data('data', shape=[-1, 10], dtype='float32') x0, x1 = paddle.split(data, num_or_sections=(3, 7), axis=1) place = fluid.NPUPlace(0) exe = fluid.Executor(place) input1 = np.random.random([1, 10]).astype('float32') r0, r1 = exe.run(feed={"data": input1}, fetch_list=[x0, x1]) ex_x0, ex_x1 = np.split(input1, (3, ), axis=1) self.assertTrue(np.allclose(ex_x0, r0)) self.assertTrue(np.allclose(ex_x1, r1))
def test_api_with_dygraph(self): paddle.disable_static(fluid.NPUPlace(0)) dtypes = ['float16', 'float32'] for dtype in dtypes: with fluid.dygraph.guard(): data = np.random.random([1, 9, 9, 4]).astype(dtype) x = fluid.dygraph.to_variable(data) tril_out, triu_out = tensor.tril(x).numpy(), tensor.triu( x).numpy() self.assertTrue(np.allclose(tril_out, np.tril(data))) self.assertTrue(np.allclose(triu_out, np.triu(data)))
def test_errors(self): with program_guard(Program(), Program()): my_sync_batch_norm = paddle.nn.SyncBatchNorm(10) x1 = fluid.create_lod_tensor(np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.NPUPlace(0)) self.assertRaises(TypeError, my_sync_batch_norm, x1) # the input dtype of SyncBatchNorm must be float16 or float32 # float16 only can be set on GPU place and NPU place x2 = fluid.layers.data(name='x2', shape=[3, 4, 5, 6], dtype="int32") self.assertRaises(TypeError, my_sync_batch_norm, x2)
def test_cpu_cpoy_npu(self): main_program, npu_var, cpu_var = self.get_prog() main_program.global_block().append_op(type='memcpy', inputs={'X': cpu_var}, outputs={'Out': npu_var}, attrs={'dst_place_type': 4}) place = fluid.NPUPlace(0) exe = fluid.Executor(place) npu_, cpu_ = exe.run(main_program, feed={}, fetch_list=[npu_var.name, cpu_var.name]) self.assertTrue(np.allclose(npu_, cpu_)) self.assertTrue(np.allclose(npu_, np.zeros((10, 10))))
def test_momentum_dygraph(self): paddle.disable_static(place=fluid.NPUPlace(0)) value = np.arange(26).reshape(2, 13).astype("float32") a = paddle.to_tensor(value) linear = paddle.nn.Linear(13, 5) # This can be any optimizer supported by dygraph. adam = paddle.optimizer.Momentum(learning_rate=0.01, momentum=0.9, parameters=linear.parameters()) out = linear(a) out.backward() adam.step() adam.clear_gradients()
def _run(self, depth): label = fluid.layers.data(name="label", shape=[1], dtype="int64") one_hot_label = fluid.one_hot(input=label, depth=depth) place = fluid.NPUPlace(0) label_data = np.array([np.random.randint(0, 10 - 1) for i in range(6)]).reshape([6, 1]) exe = fluid.Executor(place) exe.run(fluid.default_startup_program()) ret = exe.run(feed={'label': label_data, }, fetch_list=[one_hot_label], return_numpy=False)
def _test_momentum_dygraph_common(self, regularization): paddle.disable_static(fluid.NPUPlace(0)) inp = np.random.uniform(-0.1, 0.1, [10, 10]).astype("float32") linear = paddle.nn.Linear(10, 10) inp = paddle.to_tensor(inp) out = linear(inp) loss = paddle.mean(out) # This can be any optimizer supported by dygraph. momentum = paddle.fluid.contrib.optimizer.Momentum( learning_rate=0.01, momentum=0.9, parameter_list=linear.parameters(), regularization=regularization) momentum.minimize(loss)
def run_prog(self, a, b, scale): main_program, out, found_inf, float_status = self.get_prog() place = fluid.NPUPlace(0) exe = fluid.Executor(place) out_, founf_inf_, float_status_ = exe.run( main_program, feed={ "a": a, "b": b, "scale": scale }, fetch_list=[out, found_inf, float_status]) print(float_status_) return out_, founf_inf_
def test_fluid_api(self): paddle.enable_static() dtypes = ['float16', 'float32'] for dtype in dtypes: prog = Program() startup_prog = Program() with program_guard(prog, startup_prog): data = np.random.random([1, 9, 9, 4]).astype(dtype) x = fluid.data(shape=[1, 9, -1, 4], dtype=dtype, name='x') triu_out = fluid.layers.triu(x) place = fluid.NPUPlace(0) exe = fluid.Executor(place) triu_out = exe.run(fluid.default_main_program(), feed={"x": data}, fetch_list=[triu_out])
def test_dygraph(self): places = [fluid.NPUPlace(0)] for p in places: shape = [4, 10, 4, 4] def compute(x, is_test, trainable_statistics): with fluid.dygraph.guard(p): bn = fluid.dygraph.BatchNorm( shape[1], is_test=is_test, trainable_statistics=trainable_statistics) y = bn(fluid.dygraph.to_variable(x)) return y.numpy() x = np.random.randn(*shape).astype("float32") y1 = compute(x, False, False) y2 = compute(x, True, True) self.assertTrue(np.allclose(y1, y2))
def test_declarative(self): with fluid.program_guard(fluid.Program()): def gen_data(): return { "x": np.array([2, 3, 4]).astype('float32'), "y": np.array([1, 5, 2]).astype('float32') } x = fluid.data(name="x", shape=[3], dtype='float32') y = fluid.data(name="y", shape=[3], dtype='float32') z = self._executed_api(x, y) place = fluid.NPUPlace(0) exe = fluid.Executor(place) z_value = exe.run(feed=gen_data(), fetch_list=[z.name]) z_expected = np.array([3., 8., 6.]) self.assertEqual((z_value == z_expected).all(), True)
def test_shape(self): paddle.enable_static() x = fluid.layers.data(name='x', shape=[3], dtype='float32') output = fluid.layers.sampling_id(x) place = fluid.NPUPlace(0) exe = fluid.Executor(place=place) exe.run(fluid.default_startup_program()) feed = { 'x': np.array([[0.2, 0.3, 0.5], [0.2, 0.3, 0.4]], dtype='float32') } output_np = exe.run(feed=feed, fetch_list=[output])[0] self.assertEqual(output.shape[0], -1) self.assertEqual(len(output.shape), 1) self.assertEqual(output_np.shape[0], 2) self.assertEqual(len(output_np.shape), 1)
def test_check_grad(self): x_grad = pool2d_backward_navie( self.inputs["X"], ksize=self.ksize, strides=self.strides, paddings=self.paddings, global_pool=self.global_pool, ceil_mode=False, exclusive=self.exclusive, adaptive=self.adaptive, data_format=self.data_format, pool_type=self.pool_type, padding_algorithm=self.padding_algorithm) x_grad = x_grad / np.prod(self.outputs['Out'].shape) self.check_grad_with_place(fluid.NPUPlace(0), set(['X']), 'Out', max_relative_error=0.06, user_defined_grads=[x_grad])
def test_clip_dygraph(self): paddle.disable_static() place = fluid.NPUPlace( 0) if fluid.core.is_compiled_with_npu() else fluid.CPUPlace() paddle.disable_static(place) data_shape = [1, 9, 9, 4] data = np.random.random(data_shape).astype('float32') images = paddle.to_tensor(data, dtype='float32') v_min = paddle.to_tensor(np.array([0.2], dtype=np.float32)) v_max = paddle.to_tensor(np.array([0.8], dtype=np.float32)) out_1 = self._executed_api(images, min=0.2, max=0.8) images = paddle.to_tensor(data, dtype='float32') out_2 = self._executed_api(images, min=0.2, max=0.9) images = paddle.to_tensor(data, dtype='float32') out_3 = self._executed_api(images, min=v_min, max=v_max) self.assertTrue(np.allclose(out_1.numpy(), data.clip(0.2, 0.8))) self.assertTrue(np.allclose(out_2.numpy(), data.clip(0.2, 0.9))) self.assertTrue(np.allclose(out_3.numpy(), data.clip(0.2, 0.8)))
def test_static(self): paddle.set_device('npu:0') startup_program = fluid.Program() train_program = fluid.Program() with fluid.program_guard(train_program, startup_program): x = fluid.data('x', shape=[4], dtype='float32') out = paddle.multinomial(x, num_samples=100000, replacement=True) place = fluid.NPUPlace(0) exe = fluid.Executor(place) exe.run(startup_program) x_np = np.random.rand(4).astype('float32') out = exe.run(train_program, feed={'x': x_np}, fetch_list=[out]) sample_prob = sample_output_one_dimension(out, 4) prob = x_np / x_np.sum(axis=-1, keepdims=True) self.assertTrue( np.allclose(sample_prob, prob, rtol=0, atol=0.01), "sample_prob: " + str(sample_prob) + "\nprob: " + str(prob))
def test_clip(self): paddle.enable_static() data_shape = [1, 9, 9, 4] data = np.random.random(data_shape).astype('float32') images = fluid.data(name='image', shape=data_shape, dtype='float32') min = fluid.data(name='min', shape=[1], dtype='float32') max = fluid.data(name='max', shape=[1], dtype='float32') place = fluid.NPUPlace( 0) if fluid.core.is_compiled_with_npu() else fluid.CPUPlace() exe = fluid.Executor(place) out_1 = self._executed_api(images, min=min, max=max) out_2 = self._executed_api(images, min=0.2, max=0.9) out_3 = self._executed_api(images, min=0.3) out_4 = self._executed_api(images, max=0.7) out_5 = self._executed_api(images, min=min) out_6 = self._executed_api(images, max=max) out_7 = self._executed_api(images, max=-1.) out_8 = self._executed_api(images) res1, res2, res3, res4, res5, res6, res7, res8 = exe.run( fluid.default_main_program(), feed={ "image": data, "min": np.array([0.2]).astype('float32'), "max": np.array([0.8]).astype('float32') }, fetch_list=[ out_1, out_2, out_3, out_4, out_5, out_6, out_7, out_8 ]) self.assertTrue(np.allclose(res1, data.clip(0.2, 0.8))) self.assertTrue(np.allclose(res2, data.clip(0.2, 0.9))) self.assertTrue(np.allclose(res3, data.clip(min=0.3))) self.assertTrue(np.allclose(res4, data.clip(max=0.7))) self.assertTrue(np.allclose(res5, data.clip(min=0.2))) self.assertTrue(np.allclose(res6, data.clip(max=0.8))) self.assertTrue(np.allclose(res7, data.clip(max=-1))) self.assertTrue(np.allclose(res8, data)) paddle.disable_static()
def test_api(self): paddle.enable_static() dtypes = ['float16', 'float32'] for dtype in dtypes: prog = Program() startup_prog = Program() with program_guard(prog, startup_prog): data = np.random.random([1, 9, 9, 4]).astype(dtype) x = fluid.data(shape=[1, 9, -1, 4], dtype=dtype, name='x') tril_out, triu_out = tensor.tril(x), tensor.triu(x) place = fluid.NPUPlace(0) exe = fluid.Executor(place) tril_out, triu_out = exe.run( fluid.default_main_program(), feed={"x": data}, fetch_list=[tril_out, triu_out], ) self.assertTrue(np.allclose(tril_out, np.tril(data))) self.assertTrue(np.allclose(triu_out, np.triu(data)))