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

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

上级 91873469
...@@ -231,7 +231,10 @@ XPUOpMap& get_kl2_ops() { ...@@ -231,7 +231,10 @@ XPUOpMap& get_kl2_ops() {
{"elementwise_div_grad", {"elementwise_div_grad",
XPUKernelSet({phi::DataType::FLOAT32, phi::DataType::FLOAT16})}, XPUKernelSet({phi::DataType::FLOAT32, phi::DataType::FLOAT16})},
{"elementwise_div", {"elementwise_div",
XPUKernelSet({phi::DataType::FLOAT32, phi::DataType::FLOAT16})}, XPUKernelSet({phi::DataType::FLOAT32,
phi::DataType::FLOAT16,
phi::DataType::INT64,
phi::DataType::INT32})},
{"elementwise_floordiv", {"elementwise_floordiv",
XPUKernelSet({phi::DataType::FLOAT32, phi::DataType::FLOAT16})}, XPUKernelSet({phi::DataType::FLOAT32, phi::DataType::FLOAT16})},
{"elementwise_max_grad", {"elementwise_max_grad",
...@@ -256,6 +259,7 @@ XPUOpMap& get_kl2_ops() { ...@@ -256,6 +259,7 @@ XPUOpMap& get_kl2_ops() {
{"elementwise_sub", {"elementwise_sub",
XPUKernelSet({phi::DataType::FLOAT32, XPUKernelSet({phi::DataType::FLOAT32,
phi::DataType::FLOAT16, phi::DataType::FLOAT16,
phi::DataType::INT32,
phi::DataType::INT64})}, phi::DataType::INT64})},
{"elementwise_mod", {"elementwise_mod",
XPUKernelSet({phi::DataType::FLOAT32, XPUKernelSet({phi::DataType::FLOAT32,
......
...@@ -44,5 +44,11 @@ void DivideKernel(const Context& dev_ctx, ...@@ -44,5 +44,11 @@ void DivideKernel(const Context& dev_ctx,
} // namespace phi } // namespace phi
PD_REGISTER_KERNEL( PD_REGISTER_KERNEL(divide,
divide, XPU, ALL_LAYOUT, phi::DivideKernel, phi::dtype::float16, float) {} XPU,
ALL_LAYOUT,
phi::DivideKernel,
float,
phi::dtype::float16,
int,
int64_t) {}
...@@ -44,4 +44,5 @@ PD_REGISTER_KERNEL(subtract, ...@@ -44,4 +44,5 @@ PD_REGISTER_KERNEL(subtract,
phi::SubtractKernel, phi::SubtractKernel,
float, float,
phi::dtype::float16, phi::dtype::float16,
int,
int64_t) {} int64_t) {}
...@@ -48,6 +48,13 @@ class XPUTestElementwiseDivOp(XPUOpTestWrapper): ...@@ -48,6 +48,13 @@ class XPUTestElementwiseDivOp(XPUOpTestWrapper):
""" """
def init_input_output(self): def init_input_output(self):
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 = { self.inputs = {
'X': np.random.uniform(0.1, 1, [13, 17]).astype(self.dtype), 'X': np.random.uniform(0.1, 1, [13, 17]).astype(self.dtype),
'Y': np.random.uniform(0.1, 1, [13, 17]).astype(self.dtype), 'Y': np.random.uniform(0.1, 1, [13, 17]).astype(self.dtype),
...@@ -95,6 +102,13 @@ class XPUTestElementwiseDivOp(XPUOpTestWrapper): ...@@ -95,6 +102,13 @@ class XPUTestElementwiseDivOp(XPUOpTestWrapper):
class TestElementwiseDivOp_ZeroDim1(ElementwiseDivOp): class TestElementwiseDivOp_ZeroDim1(ElementwiseDivOp):
def init_input_output(self): def init_input_output(self):
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 = { self.inputs = {
'X': np.random.uniform(-1, 1, []).astype(self.dtype), 'X': np.random.uniform(-1, 1, []).astype(self.dtype),
'Y': np.random.uniform(-1, 1, []).astype(self.dtype), 'Y': np.random.uniform(-1, 1, []).astype(self.dtype),
...@@ -103,6 +117,13 @@ class XPUTestElementwiseDivOp(XPUOpTestWrapper): ...@@ -103,6 +117,13 @@ class XPUTestElementwiseDivOp(XPUOpTestWrapper):
class TestElementwiseDivOp_ZeroDim2(ElementwiseDivOp): class TestElementwiseDivOp_ZeroDim2(ElementwiseDivOp):
def init_input_output(self): def init_input_output(self):
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 = { self.inputs = {
'X': np.random.uniform(-1, 1, [13, 17]).astype(self.dtype), 'X': np.random.uniform(-1, 1, [13, 17]).astype(self.dtype),
'Y': np.random.uniform(-1, 1, []).astype(self.dtype), 'Y': np.random.uniform(-1, 1, []).astype(self.dtype),
...@@ -114,14 +135,32 @@ class XPUTestElementwiseDivOp(XPUOpTestWrapper): ...@@ -114,14 +135,32 @@ class XPUTestElementwiseDivOp(XPUOpTestWrapper):
) )
class TestElementwiseDivOp_scalar(ElementwiseDivOp): class TestElementwiseDivOp_scalar(ElementwiseDivOp):
def init_input_output(self): def init_input_output(self):
if self.dtype == np.int32 or self.dtype == np.int64:
self.inputs = { self.inputs = {
'X': np.random.uniform(0.1, 1, [20, 3, 4]).astype(self.dtype), '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), 'Y': np.random.uniform(0.1, 1, [1]).astype(self.dtype),
} }
self.outputs = {'Out': self.inputs['X'] / self.inputs['Y']} self.outputs = {'Out': self.inputs['X'] / self.inputs['Y']}
class TestElementwiseDivOp_Vector(ElementwiseDivOp): class TestElementwiseDivOp_Vector(ElementwiseDivOp):
def init_input_output(self): def init_input_output(self):
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 = { self.inputs = {
'X': np.random.uniform(0.1, 1, [100]).astype(self.dtype), 'X': np.random.uniform(0.1, 1, [100]).astype(self.dtype),
'Y': np.random.uniform(0.1, 1, [100]).astype(self.dtype), 'Y': np.random.uniform(0.1, 1, [100]).astype(self.dtype),
...@@ -132,39 +171,80 @@ class XPUTestElementwiseDivOp(XPUOpTestWrapper): ...@@ -132,39 +171,80 @@ class XPUTestElementwiseDivOp(XPUOpTestWrapper):
class TestElementwiseDivOp_broadcast_0(ElementwiseDivOp): class TestElementwiseDivOp_broadcast_0(ElementwiseDivOp):
def init_input_output(self): def init_input_output(self):
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 = { self.inputs = {
'X': np.random.uniform(0.1, 1, [100, 3, 4]).astype(self.dtype), 'X': np.random.uniform(0.1, 1, [100, 3, 4]).astype(
self.dtype
),
'Y': np.random.uniform(0.1, 1, [100]).astype(self.dtype), 'Y': np.random.uniform(0.1, 1, [100]).astype(self.dtype),
} }
self.attrs = {'axis': 0}
self.outputs = { self.outputs = {
'Out': np.divide( 'Out': np.divide(
self.inputs['X'], self.inputs['Y'].reshape(100, 1, 1) self.inputs['X'], self.inputs['Y'].reshape(100, 1, 1)
) )
} }
self.attrs = {'axis': 0}
class TestElementwiseDivOp_broadcast_1(ElementwiseDivOp): class TestElementwiseDivOp_broadcast_1(ElementwiseDivOp):
def init_input_output(self): def init_input_output(self):
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 = { self.inputs = {
'X': np.random.uniform(0.1, 1, [2, 100, 4]).astype(self.dtype), 'X': np.random.uniform(0.1, 1, [2, 100, 4]).astype(
self.dtype
),
'Y': np.random.uniform(0.1, 1, [100]).astype(self.dtype), 'Y': np.random.uniform(0.1, 1, [100]).astype(self.dtype),
} }
self.attrs = {'axis': 1}
self.outputs = { self.outputs = {
'Out': np.divide( 'Out': np.divide(
self.inputs['X'], self.inputs['Y'].reshape(1, 100, 1) self.inputs['X'], self.inputs['Y'].reshape(1, 100, 1)
) )
} }
self.attrs = {'axis': 1}
class TestElementwiseDivOp_broadcast_2(ElementwiseDivOp): class TestElementwiseDivOp_broadcast_2(ElementwiseDivOp):
def init_input_output(self): def init_input_output(self):
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 = { self.inputs = {
'X': np.random.uniform(0.1, 1, [2, 3, 100]).astype(self.dtype), 'X': np.random.uniform(0.1, 1, [2, 3, 100]).astype(
self.dtype
),
'Y': np.random.uniform(0.1, 1, [100]).astype(self.dtype), 'Y': np.random.uniform(0.1, 1, [100]).astype(self.dtype),
} }
self.outputs = { self.outputs = {
'Out': np.divide( 'Out': np.divide(
self.inputs['X'], self.inputs['Y'].reshape(1, 1, 100) self.inputs['X'], self.inputs['Y'].reshape(1, 1, 100)
...@@ -173,25 +253,52 @@ class XPUTestElementwiseDivOp(XPUOpTestWrapper): ...@@ -173,25 +253,52 @@ class XPUTestElementwiseDivOp(XPUOpTestWrapper):
class TestElementwiseDivOp_broadcast_3(ElementwiseDivOp): class TestElementwiseDivOp_broadcast_3(ElementwiseDivOp):
def init_input_output(self): def init_input_output(self):
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 = { self.inputs = {
'X': np.random.uniform(0.1, 1, [2, 10, 12, 5]).astype( 'X': np.random.uniform(0.1, 1, [2, 10, 12, 5]).astype(
self.dtype self.dtype
), ),
'Y': np.random.uniform(0.1, 1, [10, 12]).astype(self.dtype), 'Y': np.random.uniform(0.1, 1, [10, 12]).astype(self.dtype),
} }
self.attrs = {'axis': 1}
self.outputs = { self.outputs = {
'Out': np.divide( 'Out': np.divide(
self.inputs['X'], self.inputs['Y'].reshape(1, 10, 12, 1) self.inputs['X'], self.inputs['Y'].reshape(1, 10, 12, 1)
) )
} }
self.attrs = {'axis': 1}
class TestElementwiseDivOp_broadcast_4(ElementwiseDivOp): class TestElementwiseDivOp_broadcast_4(ElementwiseDivOp):
def init_input_output(self): def init_input_output(self):
if self.dtype == np.int32 or self.dtype == np.int64:
self.inputs = { self.inputs = {
'X': np.random.uniform(0.1, 1, [2, 3, 50]).astype(self.dtype), 'X': np.random.randint(1, 100, [2, 3, 50]).astype(
'Y': np.random.uniform(0.1, 1, [2, 1, 50]).astype(self.dtype), 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 = { self.outputs = {
'Out': np.divide(self.inputs['X'], self.inputs['Y']) 'Out': np.divide(self.inputs['X'], self.inputs['Y'])
...@@ -199,6 +306,17 @@ class XPUTestElementwiseDivOp(XPUOpTestWrapper): ...@@ -199,6 +306,17 @@ class XPUTestElementwiseDivOp(XPUOpTestWrapper):
class TestElementwiseDivOp_broadcast_5(ElementwiseDivOp): class TestElementwiseDivOp_broadcast_5(ElementwiseDivOp):
def init_input_output(self): def init_input_output(self):
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 = { self.inputs = {
'X': np.random.uniform(0.1, 1, [2, 3, 4, 20]).astype( 'X': np.random.uniform(0.1, 1, [2, 3, 4, 20]).astype(
self.dtype self.dtype
...@@ -213,9 +331,24 @@ class XPUTestElementwiseDivOp(XPUOpTestWrapper): ...@@ -213,9 +331,24 @@ class XPUTestElementwiseDivOp(XPUOpTestWrapper):
class TestElementwiseDivOp_commonuse_1(ElementwiseDivOp): class TestElementwiseDivOp_commonuse_1(ElementwiseDivOp):
def init_input_output(self): def init_input_output(self):
if self.dtype == np.int32 or self.dtype == np.int64:
self.inputs = { self.inputs = {
'X': np.random.uniform(0.1, 1, [2, 3, 100]).astype(self.dtype), 'X': np.random.randint(1, 100, [2, 3, 100]).astype(
'Y': np.random.uniform(0.1, 1, [1, 1, 100]).astype(self.dtype), 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 = { self.outputs = {
'Out': np.divide(self.inputs['X'], self.inputs['Y']) 'Out': np.divide(self.inputs['X'], self.inputs['Y'])
...@@ -223,6 +356,17 @@ class XPUTestElementwiseDivOp(XPUOpTestWrapper): ...@@ -223,6 +356,17 @@ class XPUTestElementwiseDivOp(XPUOpTestWrapper):
class TestElementwiseDivOp_commonuse_2(ElementwiseDivOp): class TestElementwiseDivOp_commonuse_2(ElementwiseDivOp):
def init_input_output(self): def init_input_output(self):
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 = { self.inputs = {
'X': np.random.uniform(0.1, 1, [30, 3, 1, 5]).astype( 'X': np.random.uniform(0.1, 1, [30, 3, 1, 5]).astype(
self.dtype self.dtype
...@@ -237,19 +381,27 @@ class XPUTestElementwiseDivOp(XPUOpTestWrapper): ...@@ -237,19 +381,27 @@ class XPUTestElementwiseDivOp(XPUOpTestWrapper):
class TestElementwiseDivOp_xsize_lessthan_ysize(ElementwiseDivOp): class TestElementwiseDivOp_xsize_lessthan_ysize(ElementwiseDivOp):
def init_input_output(self): def init_input_output(self):
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 = { self.inputs = {
'X': np.random.uniform(0.1, 1, [10, 12]).astype(self.dtype), 'X': np.random.uniform(0.1, 1, [10, 12]).astype(self.dtype),
'Y': np.random.uniform(0.1, 1, [2, 3, 10, 12]).astype( 'Y': np.random.uniform(0.1, 1, [2, 3, 10, 12]).astype(
self.dtype self.dtype
), ),
} }
self.attrs = {'axis': 2}
self.outputs = { self.outputs = {
'Out': np.divide(self.inputs['X'], self.inputs['Y']) 'Out': np.divide(self.inputs['X'], self.inputs['Y'])
} }
self.attrs = {'axis': 2}
class TestElementwiseDivBroadcast(unittest.TestCase): class TestElementwiseDivBroadcast(unittest.TestCase):
def test_shape_with_batch_sizes(self): def test_shape_with_batch_sizes(self):
with fluid.program_guard(fluid.Program()): with fluid.program_guard(fluid.Program()):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册