未验证 提交 97ab6aa6 编写于 作者: J jiangfan06 提交者: GitHub

[XPU] Add int support for elementwise_sub/elementwise_div (#55920)

上级 91873469
......@@ -231,7 +231,10 @@ XPUOpMap& get_kl2_ops() {
{"elementwise_div_grad",
XPUKernelSet({phi::DataType::FLOAT32, phi::DataType::FLOAT16})},
{"elementwise_div",
XPUKernelSet({phi::DataType::FLOAT32, phi::DataType::FLOAT16})},
XPUKernelSet({phi::DataType::FLOAT32,
phi::DataType::FLOAT16,
phi::DataType::INT64,
phi::DataType::INT32})},
{"elementwise_floordiv",
XPUKernelSet({phi::DataType::FLOAT32, phi::DataType::FLOAT16})},
{"elementwise_max_grad",
......@@ -256,6 +259,7 @@ XPUOpMap& get_kl2_ops() {
{"elementwise_sub",
XPUKernelSet({phi::DataType::FLOAT32,
phi::DataType::FLOAT16,
phi::DataType::INT32,
phi::DataType::INT64})},
{"elementwise_mod",
XPUKernelSet({phi::DataType::FLOAT32,
......
......@@ -44,5 +44,11 @@ void DivideKernel(const Context& dev_ctx,
} // namespace phi
PD_REGISTER_KERNEL(
divide, XPU, ALL_LAYOUT, phi::DivideKernel, phi::dtype::float16, float) {}
PD_REGISTER_KERNEL(divide,
XPU,
ALL_LAYOUT,
phi::DivideKernel,
float,
phi::dtype::float16,
int,
int64_t) {}
......@@ -44,4 +44,5 @@ PD_REGISTER_KERNEL(subtract,
phi::SubtractKernel,
float,
phi::dtype::float16,
int,
int64_t) {}
......@@ -48,13 +48,20 @@ class XPUTestElementwiseDivOp(XPUOpTestWrapper):
"""
def init_input_output(self):
self.inputs = {
'X': np.random.uniform(0.1, 1, [13, 17]).astype(self.dtype),
'Y': np.random.uniform(0.1, 1, [13, 17]).astype(self.dtype),
}
self.outputs = {
'Out': np.divide(self.inputs['X'], self.inputs['Y'])
}
if self.dtype == np.int32 or self.dtype == np.int64:
self.inputs = {
'X': np.random.randint(1, 100, [13, 17]).astype(self.dtype),
'Y': np.random.randint(1, 100, [13, 17]).astype(self.dtype),
}
self.outputs = {'Out': self.inputs['X'] // self.inputs['Y']}
else:
self.inputs = {
'X': np.random.uniform(0.1, 1, [13, 17]).astype(self.dtype),
'Y': np.random.uniform(0.1, 1, [13, 17]).astype(self.dtype),
}
self.outputs = {
'Out': np.divide(self.inputs['X'], self.inputs['Y'])
}
def test_check_output(self):
if paddle.is_compiled_with_xpu():
......@@ -95,161 +102,306 @@ class XPUTestElementwiseDivOp(XPUOpTestWrapper):
class TestElementwiseDivOp_ZeroDim1(ElementwiseDivOp):
def init_input_output(self):
self.inputs = {
'X': np.random.uniform(-1, 1, []).astype(self.dtype),
'Y': np.random.uniform(-1, 1, []).astype(self.dtype),
}
self.outputs = {'Out': self.inputs['X'] / self.inputs['Y']}
if self.dtype == np.int32 or self.dtype == np.int64:
self.inputs = {
'X': np.random.randint(1, 100, []).astype(self.dtype),
'Y': np.random.randint(1, 100, []).astype(self.dtype),
}
self.outputs = {'Out': self.inputs['X'] // self.inputs['Y']}
else:
self.inputs = {
'X': np.random.uniform(-1, 1, []).astype(self.dtype),
'Y': np.random.uniform(-1, 1, []).astype(self.dtype),
}
self.outputs = {'Out': self.inputs['X'] / self.inputs['Y']}
class TestElementwiseDivOp_ZeroDim2(ElementwiseDivOp):
def init_input_output(self):
self.inputs = {
'X': np.random.uniform(-1, 1, [13, 17]).astype(self.dtype),
'Y': np.random.uniform(-1, 1, []).astype(self.dtype),
}
self.outputs = {'Out': self.inputs['X'] / self.inputs['Y']}
if self.dtype == np.int32 or self.dtype == np.int64:
self.inputs = {
'X': np.random.randint(1, 100, [13, 17]).astype(self.dtype),
'Y': np.random.randint(1, 100, []).astype(self.dtype),
}
self.outputs = {'Out': self.inputs['X'] // self.inputs['Y']}
else:
self.inputs = {
'X': np.random.uniform(-1, 1, [13, 17]).astype(self.dtype),
'Y': np.random.uniform(-1, 1, []).astype(self.dtype),
}
self.outputs = {'Out': self.inputs['X'] / self.inputs['Y']}
@skip_check_grad_ci(
reason="[skip shape check] Use y_shape(1) to test broadcast."
)
class TestElementwiseDivOp_scalar(ElementwiseDivOp):
def init_input_output(self):
self.inputs = {
'X': np.random.uniform(0.1, 1, [20, 3, 4]).astype(self.dtype),
'Y': np.random.uniform(0.1, 1, [1]).astype(self.dtype),
}
self.outputs = {'Out': self.inputs['X'] / self.inputs['Y']}
if self.dtype == np.int32 or self.dtype == np.int64:
self.inputs = {
'X': np.random.randint(1, 100, [20, 3, 4]).astype(
self.dtype
),
'Y': np.random.randint(1, 100, [1]).astype(self.dtype),
}
self.outputs = {'Out': self.inputs['X'] // self.inputs['Y']}
else:
self.inputs = {
'X': np.random.uniform(0.1, 1, [20, 3, 4]).astype(
self.dtype
),
'Y': np.random.uniform(0.1, 1, [1]).astype(self.dtype),
}
self.outputs = {'Out': self.inputs['X'] / self.inputs['Y']}
class TestElementwiseDivOp_Vector(ElementwiseDivOp):
def init_input_output(self):
self.inputs = {
'X': np.random.uniform(0.1, 1, [100]).astype(self.dtype),
'Y': np.random.uniform(0.1, 1, [100]).astype(self.dtype),
}
self.outputs = {
'Out': np.divide(self.inputs['X'], self.inputs['Y'])
}
if self.dtype == np.int32 or self.dtype == np.int64:
self.inputs = {
'X': np.random.randint(1, 100, [100]).astype(self.dtype),
'Y': np.random.randint(1, 100, [100]).astype(self.dtype),
}
self.outputs = {'Out': self.inputs['X'] // self.inputs['Y']}
else:
self.inputs = {
'X': np.random.uniform(0.1, 1, [100]).astype(self.dtype),
'Y': np.random.uniform(0.1, 1, [100]).astype(self.dtype),
}
self.outputs = {
'Out': np.divide(self.inputs['X'], self.inputs['Y'])
}
class TestElementwiseDivOp_broadcast_0(ElementwiseDivOp):
def init_input_output(self):
self.inputs = {
'X': np.random.uniform(0.1, 1, [100, 3, 4]).astype(self.dtype),
'Y': np.random.uniform(0.1, 1, [100]).astype(self.dtype),
}
if self.dtype == np.int32 or self.dtype == np.int64:
self.inputs = {
'X': np.random.randint(1, 100, [100, 3, 4]).astype(
self.dtype
),
'Y': np.random.randint(1, 100, [100]).astype(self.dtype),
}
self.outputs = {
'Out': self.inputs['X']
// self.inputs['Y'].reshape(100, 1, 1)
}
else:
self.inputs = {
'X': np.random.uniform(0.1, 1, [100, 3, 4]).astype(
self.dtype
),
'Y': np.random.uniform(0.1, 1, [100]).astype(self.dtype),
}
self.outputs = {
'Out': np.divide(
self.inputs['X'], self.inputs['Y'].reshape(100, 1, 1)
)
}
self.attrs = {'axis': 0}
self.outputs = {
'Out': np.divide(
self.inputs['X'], self.inputs['Y'].reshape(100, 1, 1)
)
}
class TestElementwiseDivOp_broadcast_1(ElementwiseDivOp):
def init_input_output(self):
self.inputs = {
'X': np.random.uniform(0.1, 1, [2, 100, 4]).astype(self.dtype),
'Y': np.random.uniform(0.1, 1, [100]).astype(self.dtype),
}
if self.dtype == np.int32 or self.dtype == np.int64:
self.inputs = {
'X': np.random.randint(1, 100, [2, 100, 4]).astype(
self.dtype
),
'Y': np.random.randint(1, 100, [100]).astype(self.dtype),
}
self.outputs = {
'Out': self.inputs['X']
// self.inputs['Y'].reshape(1, 100, 1)
}
else:
self.inputs = {
'X': np.random.uniform(0.1, 1, [2, 100, 4]).astype(
self.dtype
),
'Y': np.random.uniform(0.1, 1, [100]).astype(self.dtype),
}
self.outputs = {
'Out': np.divide(
self.inputs['X'], self.inputs['Y'].reshape(1, 100, 1)
)
}
self.attrs = {'axis': 1}
self.outputs = {
'Out': np.divide(
self.inputs['X'], self.inputs['Y'].reshape(1, 100, 1)
)
}
class TestElementwiseDivOp_broadcast_2(ElementwiseDivOp):
def init_input_output(self):
self.inputs = {
'X': np.random.uniform(0.1, 1, [2, 3, 100]).astype(self.dtype),
'Y': np.random.uniform(0.1, 1, [100]).astype(self.dtype),
}
self.outputs = {
'Out': np.divide(
self.inputs['X'], self.inputs['Y'].reshape(1, 1, 100)
)
}
if self.dtype == np.int32 or self.dtype == np.int64:
self.inputs = {
'X': np.random.randint(1, 100, [2, 3, 100]).astype(
self.dtype
),
'Y': np.random.randint(1, 100, [100]).astype(self.dtype),
}
self.outputs = {
'Out': self.inputs['X']
// self.inputs['Y'].reshape(1, 1, 100)
}
else:
self.inputs = {
'X': np.random.uniform(0.1, 1, [2, 3, 100]).astype(
self.dtype
),
'Y': np.random.uniform(0.1, 1, [100]).astype(self.dtype),
}
self.outputs = {
'Out': np.divide(
self.inputs['X'], self.inputs['Y'].reshape(1, 1, 100)
)
}
class TestElementwiseDivOp_broadcast_3(ElementwiseDivOp):
def init_input_output(self):
self.inputs = {
'X': np.random.uniform(0.1, 1, [2, 10, 12, 5]).astype(
self.dtype
),
'Y': np.random.uniform(0.1, 1, [10, 12]).astype(self.dtype),
}
if self.dtype == np.int32 or self.dtype == np.int64:
self.inputs = {
'X': np.random.randint(1, 100, [2, 10, 12, 5]).astype(
self.dtype
),
'Y': np.random.randint(1, 100, [10, 12]).astype(self.dtype),
}
self.outputs = {
'Out': self.inputs['X']
// self.inputs['Y'].reshape(1, 10, 12, 1)
}
else:
self.inputs = {
'X': np.random.uniform(0.1, 1, [2, 10, 12, 5]).astype(
self.dtype
),
'Y': np.random.uniform(0.1, 1, [10, 12]).astype(self.dtype),
}
self.outputs = {
'Out': np.divide(
self.inputs['X'], self.inputs['Y'].reshape(1, 10, 12, 1)
)
}
self.attrs = {'axis': 1}
self.outputs = {
'Out': np.divide(
self.inputs['X'], self.inputs['Y'].reshape(1, 10, 12, 1)
)
}
class TestElementwiseDivOp_broadcast_4(ElementwiseDivOp):
def init_input_output(self):
self.inputs = {
'X': np.random.uniform(0.1, 1, [2, 3, 50]).astype(self.dtype),
'Y': np.random.uniform(0.1, 1, [2, 1, 50]).astype(self.dtype),
}
self.outputs = {
'Out': np.divide(self.inputs['X'], self.inputs['Y'])
}
if self.dtype == np.int32 or self.dtype == np.int64:
self.inputs = {
'X': np.random.randint(1, 100, [2, 3, 50]).astype(
self.dtype
),
'Y': np.random.randint(1, 100, [2, 1, 50]).astype(
self.dtype
),
}
self.outputs = {'Out': self.inputs['X'] // self.inputs['Y']}
else:
self.inputs = {
'X': np.random.uniform(0.1, 1, [2, 3, 50]).astype(
self.dtype
),
'Y': np.random.uniform(0.1, 1, [2, 1, 50]).astype(
self.dtype
),
}
self.outputs = {
'Out': np.divide(self.inputs['X'], self.inputs['Y'])
}
class TestElementwiseDivOp_broadcast_5(ElementwiseDivOp):
def init_input_output(self):
self.inputs = {
'X': np.random.uniform(0.1, 1, [2, 3, 4, 20]).astype(
self.dtype
),
'Y': np.random.uniform(0.1, 1, [2, 3, 1, 20]).astype(
self.dtype
),
}
self.outputs = {
'Out': np.divide(self.inputs['X'], self.inputs['Y'])
}
if self.dtype == np.int32 or self.dtype == np.int64:
self.inputs = {
'X': np.random.randint(1, 100, [2, 3, 4, 20]).astype(
self.dtype
),
'Y': np.random.randint(1, 100, [2, 3, 1, 20]).astype(
self.dtype
),
}
self.outputs = {'Out': self.inputs['X'] // self.inputs['Y']}
else:
self.inputs = {
'X': np.random.uniform(0.1, 1, [2, 3, 4, 20]).astype(
self.dtype
),
'Y': np.random.uniform(0.1, 1, [2, 3, 1, 20]).astype(
self.dtype
),
}
self.outputs = {
'Out': np.divide(self.inputs['X'], self.inputs['Y'])
}
class TestElementwiseDivOp_commonuse_1(ElementwiseDivOp):
def init_input_output(self):
self.inputs = {
'X': np.random.uniform(0.1, 1, [2, 3, 100]).astype(self.dtype),
'Y': np.random.uniform(0.1, 1, [1, 1, 100]).astype(self.dtype),
}
self.outputs = {
'Out': np.divide(self.inputs['X'], self.inputs['Y'])
}
if self.dtype == np.int32 or self.dtype == np.int64:
self.inputs = {
'X': np.random.randint(1, 100, [2, 3, 100]).astype(
self.dtype
),
'Y': np.random.randint(1, 100, [1, 1, 100]).astype(
self.dtype
),
}
self.outputs = {'Out': self.inputs['X'] // self.inputs['Y']}
else:
self.inputs = {
'X': np.random.uniform(0.1, 1, [2, 3, 100]).astype(
self.dtype
),
'Y': np.random.uniform(0.1, 1, [1, 1, 100]).astype(
self.dtype
),
}
self.outputs = {
'Out': np.divide(self.inputs['X'], self.inputs['Y'])
}
class TestElementwiseDivOp_commonuse_2(ElementwiseDivOp):
def init_input_output(self):
self.inputs = {
'X': np.random.uniform(0.1, 1, [30, 3, 1, 5]).astype(
self.dtype
),
'Y': np.random.uniform(0.1, 1, [30, 1, 4, 1]).astype(
self.dtype
),
}
self.outputs = {
'Out': np.divide(self.inputs['X'], self.inputs['Y'])
}
if self.dtype == np.int32 or self.dtype == np.int64:
self.inputs = {
'X': np.random.randint(1, 100, [30, 3, 1, 5]).astype(
self.dtype
),
'Y': np.random.randint(1, 100, [30, 1, 4, 1]).astype(
self.dtype
),
}
self.outputs = {'Out': self.inputs['X'] // self.inputs['Y']}
else:
self.inputs = {
'X': np.random.uniform(0.1, 1, [30, 3, 1, 5]).astype(
self.dtype
),
'Y': np.random.uniform(0.1, 1, [30, 1, 4, 1]).astype(
self.dtype
),
}
self.outputs = {
'Out': np.divide(self.inputs['X'], self.inputs['Y'])
}
class TestElementwiseDivOp_xsize_lessthan_ysize(ElementwiseDivOp):
def init_input_output(self):
self.inputs = {
'X': np.random.uniform(0.1, 1, [10, 12]).astype(self.dtype),
'Y': np.random.uniform(0.1, 1, [2, 3, 10, 12]).astype(
self.dtype
),
}
if self.dtype == np.int32 or self.dtype == np.int64:
self.inputs = {
'X': np.random.randint(1, 100, [10, 12]).astype(self.dtype),
'Y': np.random.randint(1, 100, [2, 3, 10, 12]).astype(
self.dtype
),
}
self.outputs = {'Out': self.inputs['X'] // self.inputs['Y']}
else:
self.inputs = {
'X': np.random.uniform(0.1, 1, [10, 12]).astype(self.dtype),
'Y': np.random.uniform(0.1, 1, [2, 3, 10, 12]).astype(
self.dtype
),
}
self.outputs = {
'Out': np.divide(self.inputs['X'], self.inputs['Y'])
}
self.attrs = {'axis': 2}
self.outputs = {
'Out': np.divide(self.inputs['X'], self.inputs['Y'])
}
class TestElementwiseDivBroadcast(unittest.TestCase):
def test_shape_with_batch_sizes(self):
with fluid.program_guard(fluid.Program()):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册