未验证 提交 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,13 +48,20 @@ class XPUTestElementwiseDivOp(XPUOpTestWrapper): ...@@ -48,13 +48,20 @@ class XPUTestElementwiseDivOp(XPUOpTestWrapper):
""" """
def init_input_output(self): def init_input_output(self):
self.inputs = { if self.dtype == np.int32 or self.dtype == np.int64:
'X': np.random.uniform(0.1, 1, [13, 17]).astype(self.dtype), self.inputs = {
'Y': np.random.uniform(0.1, 1, [13, 17]).astype(self.dtype), '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': np.divide(self.inputs['X'], self.inputs['Y']) 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): def test_check_output(self):
if paddle.is_compiled_with_xpu(): if paddle.is_compiled_with_xpu():
...@@ -95,161 +102,306 @@ class XPUTestElementwiseDivOp(XPUOpTestWrapper): ...@@ -95,161 +102,306 @@ class XPUTestElementwiseDivOp(XPUOpTestWrapper):
class TestElementwiseDivOp_ZeroDim1(ElementwiseDivOp): class TestElementwiseDivOp_ZeroDim1(ElementwiseDivOp):
def init_input_output(self): def init_input_output(self):
self.inputs = { if self.dtype == np.int32 or self.dtype == np.int64:
'X': np.random.uniform(-1, 1, []).astype(self.dtype), self.inputs = {
'Y': np.random.uniform(-1, 1, []).astype(self.dtype), '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']} }
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): class TestElementwiseDivOp_ZeroDim2(ElementwiseDivOp):
def init_input_output(self): def init_input_output(self):
self.inputs = { if self.dtype == np.int32 or self.dtype == np.int64:
'X': np.random.uniform(-1, 1, [13, 17]).astype(self.dtype), self.inputs = {
'Y': np.random.uniform(-1, 1, []).astype(self.dtype), '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']} }
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( @skip_check_grad_ci(
reason="[skip shape check] Use y_shape(1) to test broadcast." reason="[skip shape check] Use y_shape(1) to test broadcast."
) )
class TestElementwiseDivOp_scalar(ElementwiseDivOp): class TestElementwiseDivOp_scalar(ElementwiseDivOp):
def init_input_output(self): def init_input_output(self):
self.inputs = { if self.dtype == np.int32 or self.dtype == np.int64:
'X': np.random.uniform(0.1, 1, [20, 3, 4]).astype(self.dtype), self.inputs = {
'Y': np.random.uniform(0.1, 1, [1]).astype(self.dtype), 'X': np.random.randint(1, 100, [20, 3, 4]).astype(
} self.dtype
self.outputs = {'Out': self.inputs['X'] / self.inputs['Y']} ),
'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): class TestElementwiseDivOp_Vector(ElementwiseDivOp):
def init_input_output(self): def init_input_output(self):
self.inputs = { if self.dtype == np.int32 or self.dtype == np.int64:
'X': np.random.uniform(0.1, 1, [100]).astype(self.dtype), self.inputs = {
'Y': np.random.uniform(0.1, 1, [100]).astype(self.dtype), 'X': np.random.randint(1, 100, [100]).astype(self.dtype),
} 'Y': np.random.randint(1, 100, [100]).astype(self.dtype),
self.outputs = { }
'Out': np.divide(self.inputs['X'], self.inputs['Y']) 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): class TestElementwiseDivOp_broadcast_0(ElementwiseDivOp):
def init_input_output(self): def init_input_output(self):
self.inputs = { if self.dtype == np.int32 or self.dtype == np.int64:
'X': np.random.uniform(0.1, 1, [100, 3, 4]).astype(self.dtype), self.inputs = {
'Y': np.random.uniform(0.1, 1, [100]).astype(self.dtype), '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.attrs = {'axis': 0}
self.outputs = {
'Out': np.divide(
self.inputs['X'], self.inputs['Y'].reshape(100, 1, 1)
)
}
class TestElementwiseDivOp_broadcast_1(ElementwiseDivOp): class TestElementwiseDivOp_broadcast_1(ElementwiseDivOp):
def init_input_output(self): def init_input_output(self):
self.inputs = { if self.dtype == np.int32 or self.dtype == np.int64:
'X': np.random.uniform(0.1, 1, [2, 100, 4]).astype(self.dtype), self.inputs = {
'Y': np.random.uniform(0.1, 1, [100]).astype(self.dtype), '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.attrs = {'axis': 1}
self.outputs = {
'Out': np.divide(
self.inputs['X'], self.inputs['Y'].reshape(1, 100, 1)
)
}
class TestElementwiseDivOp_broadcast_2(ElementwiseDivOp): class TestElementwiseDivOp_broadcast_2(ElementwiseDivOp):
def init_input_output(self): def init_input_output(self):
self.inputs = { if self.dtype == np.int32 or self.dtype == np.int64:
'X': np.random.uniform(0.1, 1, [2, 3, 100]).astype(self.dtype), self.inputs = {
'Y': np.random.uniform(0.1, 1, [100]).astype(self.dtype), 'X': np.random.randint(1, 100, [2, 3, 100]).astype(
} self.dtype
),
self.outputs = { 'Y': np.random.randint(1, 100, [100]).astype(self.dtype),
'Out': np.divide( }
self.inputs['X'], self.inputs['Y'].reshape(1, 1, 100) 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): class TestElementwiseDivOp_broadcast_3(ElementwiseDivOp):
def init_input_output(self): def init_input_output(self):
self.inputs = { if self.dtype == np.int32 or self.dtype == np.int64:
'X': np.random.uniform(0.1, 1, [2, 10, 12, 5]).astype( self.inputs = {
self.dtype 'X': np.random.randint(1, 100, [2, 10, 12, 5]).astype(
), self.dtype
'Y': np.random.uniform(0.1, 1, [10, 12]).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.attrs = {'axis': 1}
self.outputs = {
'Out': np.divide(
self.inputs['X'], self.inputs['Y'].reshape(1, 10, 12, 1)
)
}
class TestElementwiseDivOp_broadcast_4(ElementwiseDivOp): class TestElementwiseDivOp_broadcast_4(ElementwiseDivOp):
def init_input_output(self): def init_input_output(self):
self.inputs = { if self.dtype == np.int32 or self.dtype == np.int64:
'X': np.random.uniform(0.1, 1, [2, 3, 50]).astype(self.dtype), self.inputs = {
'Y': np.random.uniform(0.1, 1, [2, 1, 50]).astype(self.dtype), 'X': np.random.randint(1, 100, [2, 3, 50]).astype(
} self.dtype
self.outputs = { ),
'Out': np.divide(self.inputs['X'], self.inputs['Y']) '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): class TestElementwiseDivOp_broadcast_5(ElementwiseDivOp):
def init_input_output(self): def init_input_output(self):
self.inputs = { if self.dtype == np.int32 or self.dtype == np.int64:
'X': np.random.uniform(0.1, 1, [2, 3, 4, 20]).astype( self.inputs = {
self.dtype 'X': np.random.randint(1, 100, [2, 3, 4, 20]).astype(
), self.dtype
'Y': np.random.uniform(0.1, 1, [2, 3, 1, 20]).astype( ),
self.dtype 'Y': np.random.randint(1, 100, [2, 3, 1, 20]).astype(
), self.dtype
} ),
self.outputs = { }
'Out': np.divide(self.inputs['X'], self.inputs['Y']) 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): class TestElementwiseDivOp_commonuse_1(ElementwiseDivOp):
def init_input_output(self): def init_input_output(self):
self.inputs = { if self.dtype == np.int32 or self.dtype == np.int64:
'X': np.random.uniform(0.1, 1, [2, 3, 100]).astype(self.dtype), self.inputs = {
'Y': np.random.uniform(0.1, 1, [1, 1, 100]).astype(self.dtype), 'X': np.random.randint(1, 100, [2, 3, 100]).astype(
} self.dtype
self.outputs = { ),
'Out': np.divide(self.inputs['X'], self.inputs['Y']) '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): class TestElementwiseDivOp_commonuse_2(ElementwiseDivOp):
def init_input_output(self): def init_input_output(self):
self.inputs = { if self.dtype == np.int32 or self.dtype == np.int64:
'X': np.random.uniform(0.1, 1, [30, 3, 1, 5]).astype( self.inputs = {
self.dtype 'X': np.random.randint(1, 100, [30, 3, 1, 5]).astype(
), self.dtype
'Y': np.random.uniform(0.1, 1, [30, 1, 4, 1]).astype( ),
self.dtype 'Y': np.random.randint(1, 100, [30, 1, 4, 1]).astype(
), self.dtype
} ),
self.outputs = { }
'Out': np.divide(self.inputs['X'], self.inputs['Y']) 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): class TestElementwiseDivOp_xsize_lessthan_ysize(ElementwiseDivOp):
def init_input_output(self): def init_input_output(self):
self.inputs = { if self.dtype == np.int32 or self.dtype == np.int64:
'X': np.random.uniform(0.1, 1, [10, 12]).astype(self.dtype), self.inputs = {
'Y': np.random.uniform(0.1, 1, [2, 3, 10, 12]).astype( 'X': np.random.randint(1, 100, [10, 12]).astype(self.dtype),
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.attrs = {'axis': 2}
self.outputs = {
'Out': np.divide(self.inputs['X'], self.inputs['Y'])
}
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.
先完成此消息的编辑!
想要评论请 注册