Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PIR Unittest] fix pir ut in (test_quant_linear_op) #66959

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
295 changes: 148 additions & 147 deletions test/legacy_test/test_quant_linear_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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__":
Expand Down
28 changes: 16 additions & 12 deletions test/xpu/test_while_op_xpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down