diff --git a/test/legacy_test/test_quant_linear_op.py b/test/legacy_test/test_quant_linear_op.py index 6fdbec58e52425..248bd6f52bc864 100644 --- a/test/legacy_test/test_quant_linear_op.py +++ b/test/legacy_test/test_quant_linear_op.py @@ -549,7 +549,7 @@ def run_program(num_flatten_dims): startup_program = Program() main_program = Program() - with paddle_static_guard(): + with paddle.pir_utils.OldIrGuard(): with program_guard(main_program, startup_program): quant_round_type = 0 quant_max_bound = 127.0 @@ -611,153 +611,154 @@ def run_program(num_flatten_dims): ) class TestQuantLinearOpError(unittest.TestCase): def test_errors(self): - with program_guard(Program(), Program()): - quant_round_type = 0 - quant_max_bound = 127.0 - quant_min_bound = -127.0 - - input_data = np.random.random((2, 4)).astype("float32") - scale_in = get_scale_in(input_data) - - weight = np.random.random([25, 1]).astype("float32") - scale_weight = get_scale_weights(weight) - weight = quant_weights( - weight, - scale_weight, - quant_round_type, - quant_max_bound, - quant_min_bound, - ) - - def test_Variable(): - with paddle_static_guard(): - w2 = paddle.static.data( - name='w2', shape=[25, 1], dtype='int8' - ) - quant_linear( - x=input_data, - size=1, - num_flatten_dims=1, - w=w2, - scale_in=scale_in, - scale_weight=scale_weight.tolist(), - quant_round_type=quant_round_type, - quant_max_bound=quant_max_bound, - quant_min_bound=quant_min_bound, - ) - - self.assertRaises(TypeError, test_Variable) - - def test_type(): - with paddle_static_guard(): - x2 = paddle.static.data( - name='x2', shape=[-1, 4], dtype='int32' - ) - w2 = paddle.static.data( - name='w2', shape=[25, 1], dtype='int8' - ) - paddle.static.nn.fc( - x=x2, - size=1, - num_flatten_dims=1, - w=w2, - scale_in=scale_in, - scale_weight=scale_weight.tolist(), - quant_round_type=quant_round_type, - quant_max_bound=quant_max_bound, - quant_min_bound=quant_min_bound, - ) - - self.assertRaises(TypeError, test_type) - - def test_Variable(): - with paddle_static_guard(): - x3 = paddle.static.data( - name='x3', shape=[-1, 4], dtype='float32' - ) - quant_linear( - x=x3, - size=1, - num_flatten_dims=1, - w=weight, - scale_in=scale_in, - scale_weight=scale_weight.tolist(), - quant_round_type=quant_round_type, - quant_max_bound=quant_max_bound, - quant_min_bound=quant_min_bound, - ) - - self.assertRaises(TypeError, test_Variable) - - def test_type(): - with paddle_static_guard(): - x3 = paddle.static.data( - name='x3', shape=[-1, 4], dtype='float32' - ) - w3 = paddle.static.data( - name='w3', shape=[25, 1], dtype='int32' - ) - paddle.static.nn.fc( - x=x3, - size=1, - num_flatten_dims=1, - w=w3, - scale_in=scale_in, - scale_weight=scale_weight.tolist(), - quant_round_type=quant_round_type, - quant_max_bound=quant_max_bound, - quant_min_bound=quant_min_bound, - ) - - self.assertRaises(TypeError, test_type) - - scale_weight = 1.0 - - def test_type(): - with paddle_static_guard(): - x4 = paddle.static.data( - name='x4', shape=[-1, 4], dtype='float32' - ) - w4 = paddle.static.data( - name='w4', shape=[25, 1], dtype='int8' - ) - paddle.static.nn.fc( - x=x4, - size=1, - num_flatten_dims=1, - w=w4, - scale_in=scale_in, - scale_weight=scale_weight, - quant_round_type=quant_round_type, - quant_max_bound=quant_max_bound, - quant_min_bound=quant_min_bound, - ) - - self.assertRaises(TypeError, test_type) - - scale_weight = [] - - def test_param_length(): - with paddle_static_guard(): - x4 = paddle.static.data( - name='x4', shape=[-1, 4], dtype='float32' - ) - w4 = paddle.static.data( - name='w4', shape=[25, 1], dtype='int8' - ) - paddle.static.nn.fc( - x=x4, - size=1, - num_flatten_dims=1, - w=w4, - scale_in=scale_in, - scal=scale_weight, - quant_round_type=quant_round_type, - quant_max_bound=quant_max_bound, - quant_min_bound=quant_min_bound, - ) + with paddle.pir_utils.OldIrGuard(): + with program_guard(Program(), Program()): + quant_round_type = 0 + quant_max_bound = 127.0 + quant_min_bound = -127.0 + + input_data = np.random.random((2, 4)).astype("float32") + scale_in = get_scale_in(input_data) + + weight = np.random.random([25, 1]).astype("float32") + scale_weight = get_scale_weights(weight) + weight = quant_weights( + weight, + scale_weight, + quant_round_type, + quant_max_bound, + quant_min_bound, + ) - self.assertRaises(TypeError, test_param_length) + def test_Variable(): + with paddle_static_guard(): + w2 = paddle.static.data( + name='w2', shape=[25, 1], dtype='int8' + ) + quant_linear( + x=input_data, + size=1, + num_flatten_dims=1, + w=w2, + scale_in=scale_in, + scale_weight=scale_weight.tolist(), + quant_round_type=quant_round_type, + quant_max_bound=quant_max_bound, + quant_min_bound=quant_min_bound, + ) + + self.assertRaises(TypeError, test_Variable) + + def test_type(): + with paddle_static_guard(): + x2 = paddle.static.data( + name='x2', shape=[-1, 4], dtype='int32' + ) + w2 = paddle.static.data( + name='w2', shape=[25, 1], dtype='int8' + ) + paddle.static.nn.fc( + x=x2, + size=1, + num_flatten_dims=1, + w=w2, + scale_in=scale_in, + scale_weight=scale_weight.tolist(), + quant_round_type=quant_round_type, + quant_max_bound=quant_max_bound, + quant_min_bound=quant_min_bound, + ) + + self.assertRaises(TypeError, test_type) + + def test_Variable(): + with paddle_static_guard(): + x3 = paddle.static.data( + name='x3', shape=[-1, 4], dtype='float32' + ) + quant_linear( + x=x3, + size=1, + num_flatten_dims=1, + w=weight, + scale_in=scale_in, + scale_weight=scale_weight.tolist(), + quant_round_type=quant_round_type, + quant_max_bound=quant_max_bound, + quant_min_bound=quant_min_bound, + ) + + self.assertRaises(TypeError, test_Variable) + + def test_type(): + with paddle_static_guard(): + x3 = paddle.static.data( + name='x3', shape=[-1, 4], dtype='float32' + ) + w3 = paddle.static.data( + name='w3', shape=[25, 1], dtype='int32' + ) + paddle.static.nn.fc( + x=x3, + size=1, + num_flatten_dims=1, + w=w3, + scale_in=scale_in, + scale_weight=scale_weight.tolist(), + quant_round_type=quant_round_type, + quant_max_bound=quant_max_bound, + quant_min_bound=quant_min_bound, + ) + + self.assertRaises(TypeError, test_type) + + scale_weight = 1.0 + + def test_type(): + with paddle_static_guard(): + x4 = paddle.static.data( + name='x4', shape=[-1, 4], dtype='float32' + ) + w4 = paddle.static.data( + name='w4', shape=[25, 1], dtype='int8' + ) + paddle.static.nn.fc( + x=x4, + size=1, + num_flatten_dims=1, + w=w4, + scale_in=scale_in, + scale_weight=scale_weight, + quant_round_type=quant_round_type, + quant_max_bound=quant_max_bound, + quant_min_bound=quant_min_bound, + ) + + self.assertRaises(TypeError, test_type) + + scale_weight = [] + + def test_param_length(): + with paddle_static_guard(): + x4 = paddle.static.data( + name='x4', shape=[-1, 4], dtype='float32' + ) + w4 = paddle.static.data( + name='w4', shape=[25, 1], dtype='int8' + ) + paddle.static.nn.fc( + x=x4, + size=1, + num_flatten_dims=1, + w=w4, + scale_in=scale_in, + scal=scale_weight, + quant_round_type=quant_round_type, + quant_max_bound=quant_max_bound, + quant_min_bound=quant_min_bound, + ) + + self.assertRaises(TypeError, test_param_length) if __name__ == "__main__": diff --git a/test/xpu/test_while_op_xpu.py b/test/xpu/test_while_op_xpu.py index eec24702c4c203..82c410b53dc89f 100644 --- a/test/xpu/test_while_op_xpu.py +++ b/test/xpu/test_while_op_xpu.py @@ -98,24 +98,28 @@ def test_simple_net(self): ) self.assertAlmostEqual(numpy.sum(d), numpy.sum(outs[0]), delta=0.01) + @test_with_pir_api def test_simple_net_forward(self): - with paddle.pir_utils.OldIrGuard(): - main_program = base.Program() - startup_program = base.Program() - with base.program_guard(main_program, startup_program): - self.simple_net() + main_program = base.Program() + startup_program = base.Program() + with base.program_guard(main_program, startup_program): + self.simple_net() + if paddle.framework.in_pir_mode(): + binary = main_program + else: binary = base.compiler.CompiledProgram(main_program) - xpu_place = paddle.XPUPlace(0) - exe = Executor(xpu_place) - d = [] + xpu_place = paddle.XPUPlace(0) + exe = Executor(xpu_place) + d = [] - for i in range(3): - d.append(numpy.random.random(size=[10]).astype('float32')) + for i in range(3): + d.append(numpy.random.random(size=[10]).astype('float32')) - for _ in range(2): - exe.run(binary, feed={'d0': d[0], 'd1': d[1], 'd2': d[2]}) + for _ in range(2): + exe.run(binary, feed={'d0': d[0], 'd1': d[1], 'd2': d[2]}) + @test_with_pir_api def test_exceptions(self): i = paddle.zeros(shape=[2], dtype='int64') array_len = paddle.tensor.fill_constant(