未验证 提交 2eac4db8 编写于 作者: Z zhangxiaoci 提交者: GitHub

support KL2 multi-card training, refactor KL2 unittest, *test=kunlun (#41543)

上级 35acfeda
...@@ -21,67 +21,86 @@ import numpy as np ...@@ -21,67 +21,86 @@ import numpy as np
from op_test import OpTest from op_test import OpTest
from op_test_xpu import XPUOpTest from op_test_xpu import XPUOpTest
from xpu.get_test_cover_info import create_test_class, get_xpu_op_support_types, XPUOpTestWrapper
import paddle import paddle
paddle.enable_static() paddle.enable_static()
# Correct: General. class XPUTestSqueeze2Op(XPUOpTestWrapper):
class TestSqueezeOp(XPUOpTest): def __init__(self):
def setUp(self): self.op_name = "squeeze2"
self.op_type = "squeeze2" self.use_dynamic_create_class = False
self.use_xpu = True
self.use_mkldnn = False class TestSqueeze2Op(XPUOpTest):
self.init_test_case() def setUp(self):
self.inputs = {"X": np.random.random(self.ori_shape).astype("float32")} self.op_type = "squeeze2"
self.init_attrs() self.use_mkldnn = False
self.outputs = { self.init_dtype()
"Out": self.inputs["X"].reshape(self.new_shape), self.init_test_case()
"XShape": np.random.random(self.ori_shape).astype("float32") self.inputs = {
} "X": np.random.random(self.ori_shape).astype(self.dtype)
}
def test_check_output(self): self.outputs = {
if paddle.is_compiled_with_xpu(): "Out": self.inputs["X"].reshape(self.new_shape),
"XShape": np.random.random(self.ori_shape).astype(self.dtype)
}
self.init_attrs()
def init_dtype(self):
self.dtype = self.in_type
def init_attrs(self):
self.attrs = {"axes": self.axes}
def init_test_case(self):
self.ori_shape = (1, 3, 1, 40)
self.axes = (0, 2)
self.new_shape = (3, 40)
def test_check_output(self):
place = paddle.XPUPlace(0) place = paddle.XPUPlace(0)
self.check_output_with_place(place, no_check_set=['XShape']) self.check_output_with_place(place, no_check_set=['XShape'])
def test_check_grad(self): def test_check_grad(self):
if paddle.is_compiled_with_xpu():
place = paddle.XPUPlace(0) place = paddle.XPUPlace(0)
self.check_grad_with_place(place, ['X'], 'Out') if self.dtype in [np.float32, np.float64]:
self.check_grad_with_place(place, ['X'], 'Out')
def init_test_case(self): elif self.dtype == np.bool_:
self.ori_shape = (1, 3, 1, 40) return
self.axes = (0, 2) else:
self.new_shape = (3, 40) user_defined_grad_outputs = np.random.random(
self.new_shape).astype(self.dtype)
def init_attrs(self): self.check_grad_with_place(
self.attrs = {"axes": self.axes} place, ['X'],
'Out',
user_defined_grad_outputs=user_defined_grad_outputs)
# Correct: There is mins axis.
class TestSqueezeOp1(TestSqueezeOp): # Correct: There is mins axis.
def init_test_case(self): class TestSqueeze2Op1(TestSqueeze2Op):
self.ori_shape = (1, 20, 1, 5) def init_test_case(self):
self.axes = (0, -2) self.ori_shape = (1, 20, 1, 5)
self.new_shape = (20, 5) self.axes = (0, -2)
self.new_shape = (20, 5)
# Correct: No axes input. # Correct: No axes input.
class TestSqueezeOp2(TestSqueezeOp): class TestSqueeze2Op2(TestSqueeze2Op):
def init_test_case(self): def init_test_case(self):
self.ori_shape = (1, 20, 1, 5) self.ori_shape = (1, 20, 1, 5)
self.axes = () self.axes = ()
self.new_shape = (20, 5) self.new_shape = (20, 5)
# Correct: Just part of axes be squeezed.
# Correct: Just part of axes be squeezed. class TestSqueeze2Op3(TestSqueeze2Op):
class TestSqueezeOp3(TestSqueezeOp): def init_test_case(self):
def init_test_case(self): self.ori_shape = (6, 1, 5, 1, 4, 1)
self.ori_shape = (6, 1, 5, 1, 4, 1) self.axes = (1, -1)
self.axes = (1, -1) self.new_shape = (6, 5, 1, 4)
self.new_shape = (6, 5, 1, 4)
support_types = get_xpu_op_support_types("squeeze2")
for stype in support_types:
create_test_class(globals(), XPUTestSqueeze2Op, stype)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()
...@@ -23,209 +23,225 @@ import paddle ...@@ -23,209 +23,225 @@ import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
from op_test import OpTest from op_test import OpTest
from op_test_xpu import XPUOpTest from op_test_xpu import XPUOpTest
from xpu.get_test_cover_info import create_test_class, get_xpu_op_support_types, XPUOpTestWrapper
paddle.enable_static() paddle.enable_static()
# Correct: General. class XPUTestUnsqueeze2Op(XPUOpTestWrapper):
class TestUnsqueezeOp(XPUOpTest): def __init__(self):
def setUp(self): self.op_name = "unsqueeze2"
self.init_test_case() self.use_dynamic_create_class = False
self.use_xpu = True
self.use_mkldnn = False class TestUnsqueeze2Op(XPUOpTest):
self.op_type = "unsqueeze2" def setUp(self):
self.inputs = {"X": np.random.random(self.ori_shape).astype("float32")} self.op_type = "unsqueeze2"
self.init_attrs() self.use_mkldnn = False
self.outputs = { self.init_dtype()
"Out": self.inputs["X"].reshape(self.new_shape), self.init_test_case()
"XShape": np.random.random(self.ori_shape).astype("float32") self.inputs = {
} "X": np.random.random(self.ori_shape).astype(self.dtype)
}
def test_check_output(self): self.outputs = {
if paddle.is_compiled_with_xpu(): "Out": self.inputs["X"].reshape(self.new_shape),
"XShape": np.random.random(self.ori_shape).astype(self.dtype)
}
self.init_attrs()
def init_dtype(self):
self.dtype = self.in_type
def init_attrs(self):
self.attrs = {"axes": self.axes}
def init_test_case(self):
self.ori_shape = (3, 40)
self.axes = (1, 2)
self.new_shape = (3, 1, 1, 40)
def test_check_output(self):
place = paddle.XPUPlace(0) place = paddle.XPUPlace(0)
self.check_output_with_place(place, no_check_set=['XShape']) self.check_output_with_place(place, no_check_set=['XShape'])
def test_check_grad(self): def test_check_grad(self):
if paddle.is_compiled_with_xpu():
place = paddle.XPUPlace(0) place = paddle.XPUPlace(0)
self.check_grad_with_place(place, ['X'], 'Out') if self.dtype in [np.float32, np.float64]:
self.check_grad_with_place(place, ['X'], 'Out')
def init_test_case(self): elif self.dtype == np.bool_:
self.ori_shape = (3, 40) return
self.axes = (1, 2) else:
self.new_shape = (3, 1, 1, 40) user_defined_grad_outputs = np.random.random(
self.new_shape).astype(self.dtype)
def init_attrs(self): self.check_grad_with_place(
self.attrs = {"axes": self.axes} place, ['X'],
'Out',
user_defined_grad_outputs=user_defined_grad_outputs)
# Correct: Single input index.
class TestUnsqueezeOp1(TestUnsqueezeOp): # Correct: Single input index.
def init_test_case(self): class TestUnsqueeze2Op1(TestUnsqueeze2Op):
self.ori_shape = (20, 5) def init_test_case(self):
self.axes = (-1, ) self.ori_shape = (20, 5)
self.new_shape = (20, 5, 1) self.axes = (-1, )
self.new_shape = (20, 5, 1)
# Correct: Mixed input axis. # Correct: Mixed input axis.
class TestUnsqueezeOp2(TestUnsqueezeOp): class TestUnsqueeze2Op2(TestUnsqueeze2Op):
def init_test_case(self): def init_test_case(self):
self.ori_shape = (20, 5) self.ori_shape = (20, 5)
self.axes = (0, -1) self.axes = (0, -1)
self.new_shape = (1, 20, 5, 1) self.new_shape = (1, 20, 5, 1)
# Correct: There is duplicated axis.
# Correct: There is duplicated axis. class TestUnsqueeze2Op3(TestUnsqueeze2Op):
class TestUnsqueezeOp3(TestUnsqueezeOp): def init_test_case(self):
def init_test_case(self): self.ori_shape = (10, 2, 5)
self.ori_shape = (10, 2, 5) self.axes = (0, 3, 3)
self.axes = (0, 3, 3) self.new_shape = (1, 10, 2, 1, 1, 5)
self.new_shape = (1, 10, 2, 1, 1, 5)
# Correct: Reversed axes.
class TestUnsqueeze2Op4(TestUnsqueeze2Op):
# Correct: Reversed axes. def init_test_case(self):
class TestUnsqueezeOp4(TestUnsqueezeOp): self.ori_shape = (10, 2, 5)
def init_test_case(self): self.axes = (3, 1, 1)
self.ori_shape = (10, 2, 5) self.new_shape = (10, 1, 1, 2, 5, 1)
self.axes = (3, 1, 1)
self.new_shape = (10, 1, 1, 2, 5, 1) # axes is a list(with tensor)
class TestUnsqueeze2Op_AxesTensorList(XPUOpTest):
def setUp(self):
# axes is a list(with tensor) self.op_type = "unsqueeze2"
class TestUnsqueezeOp_AxesTensorList(XPUOpTest): self.use_mkldnn = False
def setUp(self): self.init_dtype()
self.init_test_case() self.init_test_case()
self.use_xpu = True
self.use_mkldnn = False axes_tensor_list = []
self.op_type = "unsqueeze2" for index, ele in enumerate(self.axes):
axes_tensor_list.append(("axes" + str(index), np.ones(
axes_tensor_list = [] (1)).astype('int32') * ele))
for index, ele in enumerate(self.axes):
axes_tensor_list.append(("axes" + str(index), np.ones( self.inputs = {
(1)).astype('int32') * ele)) "X": np.random.random(self.ori_shape).astype(self.dtype),
"AxesTensorList": axes_tensor_list
self.inputs = { }
"X": np.random.random(self.ori_shape).astype("float32"), self.init_attrs()
"AxesTensorList": axes_tensor_list self.outputs = {
} "Out": self.inputs["X"].reshape(self.new_shape),
self.init_attrs() "XShape": np.random.random(self.ori_shape).astype(self.dtype)
self.outputs = { }
"Out": self.inputs["X"].reshape(self.new_shape),
"XShape": np.random.random(self.ori_shape).astype("float32") def init_dtype(self):
} self.dtype = self.in_type
def test_check_output(self): def test_check_output(self):
if paddle.is_compiled_with_xpu():
place = paddle.XPUPlace(0) place = paddle.XPUPlace(0)
self.check_output_with_place(place, no_check_set=['XShape']) self.check_output_with_place(place, no_check_set=['XShape'])
def test_check_grad(self): def test_check_grad(self):
if paddle.is_compiled_with_xpu():
place = paddle.XPUPlace(0) place = paddle.XPUPlace(0)
self.check_grad_with_place(place, ['X'], 'Out') if self.dtype in [np.float32, np.float64]:
self.check_grad_with_place(place, ['X'], 'Out')
def init_test_case(self): else:
self.ori_shape = (20, 5) return
self.axes = (1, 2)
self.new_shape = (20, 1, 1, 5) def init_test_case(self):
self.ori_shape = (20, 5)
def init_attrs(self): self.axes = (1, 2)
self.attrs = {} self.new_shape = (20, 1, 1, 5)
def init_attrs(self):
class TestUnsqueezeOp1_AxesTensorList(TestUnsqueezeOp_AxesTensorList): self.attrs = {}
def init_test_case(self):
self.ori_shape = (20, 5) class TestUnsqueeze2Op1_AxesTensorList(TestUnsqueeze2Op_AxesTensorList):
self.axes = (-1, ) def init_test_case(self):
self.new_shape = (20, 5, 1) self.ori_shape = (20, 5)
self.axes = (-1, )
self.new_shape = (20, 5, 1)
class TestUnsqueezeOp2_AxesTensorList(TestUnsqueezeOp_AxesTensorList):
def init_test_case(self): class TestUnsqueeze2Op2_AxesTensorList(TestUnsqueeze2Op_AxesTensorList):
self.ori_shape = (20, 5) def init_test_case(self):
self.axes = (0, -1) self.ori_shape = (20, 5)
self.new_shape = (1, 20, 5, 1) self.axes = (0, -1)
self.new_shape = (1, 20, 5, 1)
class TestUnsqueezeOp3_AxesTensorList(TestUnsqueezeOp_AxesTensorList): class TestUnsqueeze2Op3_AxesTensorList(TestUnsqueeze2Op_AxesTensorList):
def init_test_case(self): def init_test_case(self):
self.ori_shape = (10, 2, 5) self.ori_shape = (10, 2, 5)
self.axes = (0, 3, 3) self.axes = (0, 3, 3)
self.new_shape = (1, 10, 2, 1, 1, 5) self.new_shape = (1, 10, 2, 1, 1, 5)
class TestUnsqueeze2Op4_AxesTensorList(TestUnsqueeze2Op_AxesTensorList):
class TestUnsqueezeOp4_AxesTensorList(TestUnsqueezeOp_AxesTensorList): def init_test_case(self):
def init_test_case(self): self.ori_shape = (10, 2, 5)
self.ori_shape = (10, 2, 5) self.axes = (3, 1, 1)
self.axes = (3, 1, 1) self.new_shape = (10, 1, 1, 2, 5, 1)
self.new_shape = (10, 1, 1, 2, 5, 1)
# axes is a Tensor
class TestUnsqueeze2Op_AxesTensor(XPUOpTest):
# axes is a Tensor def setUp(self):
class TestUnsqueezeOp_AxesTensor(XPUOpTest): self.op_type = "unsqueeze2"
def setUp(self): self.use_mkldnn = False
self.init_test_case() self.init_test_case()
self.use_xpu = True self.init_dtype()
self.use_mkldnn = False
self.op_type = "unsqueeze2" self.inputs = {
"X": np.random.random(self.ori_shape).astype(self.dtype),
self.inputs = { "AxesTensor": np.array(self.axes).astype("int32")
"X": np.random.random(self.ori_shape).astype("float32"), }
"AxesTensor": np.array(self.axes).astype("int32") self.init_attrs()
} self.outputs = {
self.init_attrs() "Out": self.inputs["X"].reshape(self.new_shape),
self.outputs = { "XShape": np.random.random(self.ori_shape).astype(self.dtype)
"Out": self.inputs["X"].reshape(self.new_shape), }
"XShape": np.random.random(self.ori_shape).astype("float32")
} def init_dtype(self):
self.dtype = self.in_type
def test_check_output(self):
if paddle.is_compiled_with_xpu(): def test_check_output(self):
place = paddle.XPUPlace(0) place = paddle.XPUPlace(0)
self.check_output_with_place(place, no_check_set=['XShape']) self.check_output_with_place(place, no_check_set=['XShape'])
def test_check_grad(self): def test_check_grad(self):
if paddle.is_compiled_with_xpu():
place = paddle.XPUPlace(0) place = paddle.XPUPlace(0)
self.check_grad_with_place(place, ['X'], 'Out') if self.dtype in [np.float32, np.float64]:
self.check_grad_with_place(place, ['X'], 'Out')
def init_test_case(self): else:
self.ori_shape = (20, 5) return
self.axes = (1, 2)
self.new_shape = (20, 1, 1, 5) def init_test_case(self):
self.ori_shape = (20, 5)
def init_attrs(self): self.axes = (1, 2)
self.attrs = {} self.new_shape = (20, 1, 1, 5)
def init_attrs(self):
class TestUnsqueezeOp1_AxesTensor(TestUnsqueezeOp_AxesTensor): self.attrs = {}
def init_test_case(self):
self.ori_shape = (20, 5) class TestUnsqueeze2Op1_AxesTensor(TestUnsqueeze2Op_AxesTensor):
self.axes = (-1, ) def init_test_case(self):
self.new_shape = (20, 5, 1) self.ori_shape = (20, 5)
self.axes = (-1, )
self.new_shape = (20, 5, 1)
class TestUnsqueezeOp2_AxesTensor(TestUnsqueezeOp_AxesTensor):
def init_test_case(self): class TestUnsqueeze2Op2_AxesTensor(TestUnsqueeze2Op_AxesTensor):
self.ori_shape = (20, 5) def init_test_case(self):
self.axes = (0, -1) self.ori_shape = (20, 5)
self.new_shape = (1, 20, 5, 1) self.axes = (0, -1)
self.new_shape = (1, 20, 5, 1)
class TestUnsqueezeOp3_AxesTensor(TestUnsqueezeOp_AxesTensor): class TestUnsqueeze2Op3_AxesTensor(TestUnsqueeze2Op_AxesTensor):
def init_test_case(self): def init_test_case(self):
self.ori_shape = (10, 2, 5) self.ori_shape = (10, 2, 5)
self.axes = (0, 3, 3) self.axes = (0, 3, 3)
self.new_shape = (1, 10, 2, 1, 1, 5) self.new_shape = (1, 10, 2, 1, 1, 5)
class TestUnsqueeze2Op4_AxesTensor(TestUnsqueeze2Op_AxesTensor):
class TestUnsqueezeOp4_AxesTensor(TestUnsqueezeOp_AxesTensor): def init_test_case(self):
def init_test_case(self): self.ori_shape = (10, 2, 5)
self.ori_shape = (10, 2, 5) self.axes = (3, 1, 1)
self.axes = (3, 1, 1) self.new_shape = (10, 1, 1, 2, 5, 1)
self.new_shape = (10, 1, 1, 2, 5, 1)
support_types = get_xpu_op_support_types("unsqueeze2")
for stype in support_types:
create_test_class(globals(), XPUTestUnsqueeze2Op, stype)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册