Skip to content

Commit ac2a94c

Browse files
authored
[XPU] add cumsum op. test=kunlun (#47585)
* [XPU] add cumsum op. test=kunlun * try to fix linker. test=kunlun * try to fix linker. test=kunlun * try to fix linker. test=kunlun * debug. test=kunlun * update xpu.cmake. remove unnecessary codes. test=kunlun.
1 parent eb9e460 commit ac2a94c

File tree

5 files changed

+186
-5
lines changed

5 files changed

+186
-5
lines changed

cmake/external/xpu.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ set(XPU_RT_LIB_NAME "libxpurt.so")
1010
if(NOT DEFINED XPU_BASE_URL)
1111
set(XPU_BASE_URL_WITHOUT_DATE
1212
"https://baidu-kunlun-product.su.bcebos.com/KL-SDK/klsdk-dev")
13-
set(XPU_BASE_URL "${XPU_BASE_URL_WITHOUT_DATE}/20221016")
13+
set(XPU_BASE_URL "${XPU_BASE_URL_WITHOUT_DATE}/20221104")
1414
else()
1515
set(XPU_BASE_URL "${XPU_BASE_URL}")
1616
endif()
@@ -19,7 +19,7 @@ endif()
1919
if(NOT DEFINED XPU_XDNN_BASE_URL)
2020
set(XPU_XDNN_BASE_URL_WITHOUT_DATE
2121
"https://klx-sdk-release-public.su.bcebos.com/xdnn/dev")
22-
set(XPU_XDNN_BASE_URL "${XPU_XDNN_BASE_URL_WITHOUT_DATE}/20221016")
22+
set(XPU_XDNN_BASE_URL "${XPU_XDNN_BASE_URL_WITHOUT_DATE}/20221103")
2323
else()
2424
set(XPU_XDNN_BASE_URL "${XPU_XDNN_BASE_URL}")
2525
endif()

paddle/fluid/platform/device/xpu/xpu2_op_list.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ XPUOpMap& get_kl2_ops() {
123123
XPUKernelSet({pOpKernelType(vartype::FP32, XPUPlace())})},
124124
{"conv2d_transpose",
125125
XPUKernelSet({pOpKernelType(vartype::FP32, XPUPlace())})},
126+
{"cumsum",
127+
XPUKernelSet({pOpKernelType(vartype::FP32, XPUPlace()),
128+
pOpKernelType(vartype::INT32, XPUPlace()),
129+
pOpKernelType(vartype::INT64, XPUPlace())})},
126130
{"deformable_conv_grad",
127131
XPUKernelSet({pOpKernelType(vartype::FP32, XPUPlace())})},
128132
{"deformable_conv",

paddle/phi/kernels/xpu/cum_kernel.cc

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright (c) 2022 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+
#include "paddle/phi/kernels/cum_kernel.h"
16+
17+
#include "paddle/phi/backends/xpu/enforce_xpu.h"
18+
#include "paddle/phi/core/kernel_registry.h"
19+
20+
namespace phi {
21+
22+
template <typename T, typename Context>
23+
void CumsumKernel(const Context& dev_ctx,
24+
const DenseTensor& x,
25+
const Scalar& axis,
26+
bool flatten,
27+
bool exclusive,
28+
bool reverse,
29+
DenseTensor* out) {
30+
using XPUType = typename XPUTypeTrait<T>::Type;
31+
dev_ctx.template Alloc<T>(out);
32+
33+
// prepare for call xdnn api
34+
std::vector<int> x_shape = phi::vectorize<int>(x.dims());
35+
int axis_as_int = axis.to<int>();
36+
37+
if (flatten) {
38+
// flatten to 1-dim vector
39+
x_shape = {static_cast<int>(x.numel())};
40+
axis_as_int = 0;
41+
} else {
42+
// not flatten
43+
// check axis_as_int
44+
auto out_dims = out->dims();
45+
46+
PADDLE_ENFORCE_EQ(
47+
axis_as_int < out_dims.size() && axis_as_int >= (0 - out_dims.size()),
48+
true,
49+
phi::errors::OutOfRange(
50+
"Attr(axis) is out of range, It's expected "
51+
"to be in range of [-%d, %d]. But received Attr(axis) = %d.",
52+
out_dims.size(),
53+
out_dims.size() - 1,
54+
axis_as_int));
55+
if (axis_as_int < 0) {
56+
axis_as_int += out_dims.size();
57+
}
58+
}
59+
60+
// template<typename T> DLL_EXPORT int cumsum(Context* ctx, const T* x, T* y,
61+
// const std::vector<int>& xshape, bool reverse, bool exclusive, int axis);
62+
int r = cumsum(dev_ctx.x_context(),
63+
reinterpret_cast<const XPUType*>(x.data<T>()),
64+
reinterpret_cast<XPUType*>(out->data<T>()),
65+
x_shape,
66+
reverse,
67+
exclusive,
68+
axis_as_int);
69+
PADDLE_ENFORCE_XDNN_SUCCESS(r, "cumsum");
70+
}
71+
72+
} // namespace phi
73+
74+
PD_REGISTER_KERNEL(
75+
cumsum, XPU, ALL_LAYOUT, phi::CumsumKernel, float, int, int64_t) {}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Copyright (c) 2022 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+
import numpy as np
17+
import sys
18+
19+
sys.path.append("..")
20+
21+
import paddle
22+
import paddle.fluid.core as core
23+
24+
from op_test_xpu import XPUOpTest
25+
from xpu.get_test_cover_info import (
26+
create_test_class,
27+
get_xpu_op_support_types,
28+
XPUOpTestWrapper,
29+
)
30+
31+
paddle.enable_static()
32+
33+
34+
class XPUTestCumsumOP(XPUOpTestWrapper):
35+
def __init__(self):
36+
self.op_name = 'cumsum'
37+
self.use_dynamic_create_class = False
38+
39+
class TestCumsumOPBase(XPUOpTest):
40+
def setUp(self):
41+
self.place = paddle.XPUPlace(0)
42+
self.xpu_version = core.get_xpu_device_version(0)
43+
self.init_dtype()
44+
self.set_case()
45+
46+
def set_case(self):
47+
self.op_type = 'cumsum'
48+
self.init_config()
49+
50+
self.data = np.random.uniform(
51+
-100.0, 100.0, self.input_shape
52+
).astype(self.dtype)
53+
reference_out = np.cumsum(self.data, axis=self.axis)
54+
self.inputs = {
55+
'X': self.data,
56+
}
57+
self.attrs = {
58+
'use_xpu': True,
59+
'axis': self.axis,
60+
'flatten': True if self.axis is None else False,
61+
}
62+
self.outputs = {'Out': reference_out}
63+
64+
def init_dtype(self):
65+
self.dtype = self.in_type
66+
67+
def test_check_output(self):
68+
self.check_output_with_place(self.place)
69+
70+
def init_config(self):
71+
self.input_shape = (2, 5)
72+
self.axis = None
73+
74+
class XPUTestCumsum1(TestCumsumOPBase):
75+
def init_config(self):
76+
self.input_shape = [2, 768]
77+
self.axis = 0
78+
79+
class XPUTestCumsum2(TestCumsumOPBase):
80+
def init_config(self):
81+
self.input_shape = [3, 8, 4096]
82+
self.axis = 1
83+
84+
class XPUTestCumsum3(TestCumsumOPBase):
85+
def init_config(self):
86+
self.input_shape = [1024]
87+
self.axis = 0
88+
89+
class XPUTestCumsum4(TestCumsumOPBase):
90+
def init_config(self):
91+
self.input_shape = [2, 2, 255]
92+
self.axis = -1
93+
94+
95+
support_types = get_xpu_op_support_types('cumsum')
96+
for stype in support_types:
97+
create_test_class(globals(), XPUTestCumsumOP, stype)
98+
99+
if __name__ == "__main__":
100+
unittest.main()

python/setup.py.in

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,9 +570,11 @@ if '${WITH_XPU}' == 'ON':
570570
if os.system(command) != 0:
571571
raise Exception("patch ${XPU_API_LIB} failed, command: %s" % command)
572572
shutil.copy('${XPU_API_LIB}', libs_path)
573-
shutil.copy('${XPU_RT_LIB}', libs_path)
574-
package_data['paddle.libs']+=['${XPU_API_LIB_NAME}',
575-
'${XPU_RT_LIB_NAME}']
573+
package_data['paddle.libs']+=['${XPU_API_LIB_NAME}']
574+
xpu_rt_lib_list = glob.glob('${XPU_RT_LIB}*')
575+
for xpu_rt_lib_file in xpu_rt_lib_list:
576+
shutil.copy(xpu_rt_lib_file, libs_path)
577+
package_data['paddle.libs']+=[os.path.basename(xpu_rt_lib_file)]
576578

577579
if '${WITH_XPU_BKCL}' == 'ON':
578580
shutil.copy('${XPU_BKCL_LIB}', libs_path)

0 commit comments

Comments
 (0)