Skip to content

Commit 0ddfa0d

Browse files
committed
[XPU] add empty_like op and test, update XHPC to 20240105
1 parent fa1f901 commit 0ddfa0d

File tree

4 files changed

+149
-1
lines changed

4 files changed

+149
-1
lines changed

cmake/external/xpu.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ if(NOT DEFINED XPU_BASE_DATE)
2929
set(XPU_BASE_DATE "20231218")
3030
endif()
3131
if(NOT DEFINED XPU_XHPC_BASE_DATE)
32-
set(XPU_XHPC_BASE_DATE "20231229")
32+
set(XPU_XHPC_BASE_DATE "20240105")
3333
endif()
3434
set(XPU_XCCL_BASE_VERSION "1.1.8.1")
3535
if(NOT DEFINED XPU_XFT_BASE_VERSION)

paddle/phi/backends/xpu/xpu2_op_list.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,17 @@ XPUOpMap& get_kl2_ops() {
331331
phi::DataType::FLOAT32,
332332
phi::DataType::FLOAT64,
333333
phi::DataType::BFLOAT16})},
334+
{"empty_like",
335+
XPUKernelSet({phi::DataType::INT64,
336+
phi::DataType::INT32,
337+
phi::DataType::INT16,
338+
phi::DataType::INT8,
339+
phi::DataType::UINT8,
340+
phi::DataType::BOOL,
341+
phi::DataType::FLOAT16,
342+
phi::DataType::FLOAT32,
343+
phi::DataType::FLOAT64,
344+
phi::DataType::BFLOAT16})},
334345
{"embedding_grad",
335346
XPUKernelSet({phi::DataType::FLOAT32,
336347
phi::DataType::FLOAT16,

paddle/phi/backends/xpu/xpu3_op_list.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,17 @@ XPUOpMap& get_kl3_ops() {
327327
phi::DataType::FLOAT32,
328328
phi::DataType::FLOAT64,
329329
phi::DataType::BFLOAT16})},
330+
{"empty_like",
331+
XPUKernelSet({phi::DataType::INT64,
332+
phi::DataType::INT32,
333+
phi::DataType::INT16,
334+
phi::DataType::INT8,
335+
phi::DataType::UINT8,
336+
phi::DataType::BOOL,
337+
phi::DataType::FLOAT16,
338+
phi::DataType::FLOAT32,
339+
phi::DataType::FLOAT64,
340+
phi::DataType::BFLOAT16})},
330341
{"embedding_grad",
331342
XPUKernelSet({phi::DataType::FLOAT32,
332343
phi::DataType::FLOAT16,

test/xpu/test_empty_like_op_xpu.py

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import unittest
16+
17+
import numpy as np
18+
from get_test_cover_info import (
19+
XPUOpTestWrapper,
20+
create_test_class,
21+
get_xpu_op_support_types,
22+
)
23+
from op_test_xpu import XPUOpTest
24+
25+
import paddle
26+
from paddle.base.framework import convert_np_dtype_to_dtype_
27+
28+
paddle.enable_static()
29+
30+
31+
class XPUTestEmptyLikeOp(XPUOpTestWrapper):
32+
def __init__(self):
33+
self.op_name = 'empty_like'
34+
self.use_dynamic_create_class = False
35+
36+
# Situation 1: Attr(shape) is a list(without tensor)
37+
class TestEmptyLikeOp(XPUOpTest):
38+
def setUp(self):
39+
self.op_type = "empty_like"
40+
self.init_dtype()
41+
self.set_xpu()
42+
self.place = paddle.XPUPlace(0)
43+
self.set_inputs()
44+
self.init_config()
45+
46+
def test_check_output(self):
47+
self.check_output_customized(self.verify_output)
48+
49+
def verify_output(self, outs):
50+
data_type = outs[0].dtype
51+
if data_type in [
52+
'float32',
53+
'float64',
54+
'int32',
55+
'int64',
56+
'int8',
57+
'uint8',
58+
'float16',
59+
'int16',
60+
'uint16',
61+
]:
62+
max_value = np.nanmax(outs[0])
63+
min_value = np.nanmin(outs[0])
64+
65+
always_full_zero = max_value == 0.0 and min_value == 0.0
66+
always_non_full_zero = max_value >= min_value
67+
self.assertTrue(
68+
always_full_zero or always_non_full_zero,
69+
'always_full_zero or always_non_full_zero.',
70+
)
71+
elif data_type in ['bool']:
72+
total_num = outs[0].size
73+
true_num = np.sum(outs[0])
74+
false_num = np.sum(~outs[0])
75+
self.assertTrue(
76+
total_num == true_num + false_num,
77+
'The value should always be True or False.',
78+
)
79+
else:
80+
# pass
81+
self.assertTrue(False, 'invalid data type')
82+
83+
def set_inputs(self):
84+
self.inputs = {}
85+
86+
def init_config(self):
87+
dtype_inner = convert_np_dtype_to_dtype_(self.dtype)
88+
self.attrs = {'shape': self.shape, 'dtype': dtype_inner}
89+
self.outputs = {'Out': np.zeros(self.shape).astype(self.dtype)}
90+
91+
def init_dtype(self):
92+
self.dtype = self.in_type
93+
94+
def set_xpu(self):
95+
self.__class__.use_xpu = True
96+
self.__class__.no_need_check_grad = True
97+
self.__class__.op_type = self.op_type
98+
99+
class TestEmptyLikeOpCase1(TestEmptyLikeOp):
100+
def set_input(self):
101+
x = np.random.uniform(size=[50]).astype(self.dtype)
102+
self.inputs = {'x': x}
103+
104+
class TestEmptyLikeOpCase2(TestEmptyLikeOp):
105+
def set_input(self):
106+
x = np.random.uniform(size=[1, 50, 3, 4]).astype(self.dtype)
107+
self.inputs = {'x': x}
108+
109+
class TestEmptyLikeOpCase3(TestEmptyLikeOp):
110+
def set_input(self):
111+
x = np.random.uniform(size=[5, 5, 5]).astype(self.dtype)
112+
self.inputs = {'x': x}
113+
114+
# Situation 2: x and output have different dtypes
115+
class TestEmptyLikeOp_DifferentDtype(TestEmptyLikeOp):
116+
def set_inputs(self):
117+
x = np.random.uniform(size=[500, 3]).astype("int32")
118+
self.inputs = {"x": x}
119+
120+
121+
support_types = get_xpu_op_support_types('empty_like')
122+
for stype in support_types:
123+
create_test_class(globals(), XPUTestEmptyLikeOp, stype)
124+
125+
if __name__ == '__main__':
126+
unittest.main()

0 commit comments

Comments
 (0)