Skip to content

Commit 0566098

Browse files
authored
[npu_ci_build] add ci pipeline support ctest uts (#13)
* [npu_ci_build] test pr ci pipeline * update * support ctest UT
1 parent b969436 commit 0566098

File tree

15 files changed

+211
-7
lines changed

15 files changed

+211
-7
lines changed

Paddle

Submodule Paddle updated 606 files

backends/npu/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ include(third_party)
5454
add_dependencies(${CUSTOM_NPU_NAME} third_party)
5555
target_link_libraries(${CUSTOM_NPU_NAME} PRIVATE ${PADDLE_CORE_LIB})
5656

57+
# testing
58+
if (WITH_TESTING)
59+
set(PYTHON_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../python")
60+
add_subdirectory(tests)
61+
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/tests/.timestamp
62+
COMMAND cp -r ${CMAKE_SOURCE_DIR}/tests ${CMAKE_CURRENT_BINARY_DIR})
63+
add_custom_target(python_tests ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/tests/.timestamp)
64+
endif()
65+
5766
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/setup.py.in
5867
${CMAKE_CURRENT_BINARY_DIR}/setup.py)
5968

backends/npu/kernels/sgd_kernel.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ void SGDKernel(const Context& dev_ctx,
2222
const phi::DenseTensor& param_var,
2323
const phi::DenseTensor& learning_rate,
2424
const phi::DenseTensor& grad_var,
25-
phi::DenseTensor* param_out) {
25+
paddle::optional<const phi::DenseTensor&> master_param,
26+
bool multi_precision,
27+
phi::DenseTensor* param_out,
28+
phi::DenseTensor* master_param_out) {
2629
aclrtStream stream = static_cast<aclrtStream>(dev_ctx.stream());
2730
dev_ctx.template Alloc<T>(param_out);
2831

backends/npu/tests/CMakeLists.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
function(py_test_modules TARGET_NAME)
16+
set(options SERIAL)
17+
set(oneValueArgs "")
18+
set(multiValueArgs MODULES DEPS ENVS)
19+
cmake_parse_arguments(py_test_modules "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
20+
21+
add_test(NAME ${TARGET_NAME}
22+
COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH=${PYTHON_SOURCE_DIR}:$ENV{PYTHONPATH} ${py_test_modules_ENVS}
23+
python ${PYTHON_SOURCE_DIR}/tools/test_runner.py ${py_test_modules_MODULES}
24+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
25+
26+
if (py_test_modules_SERIAL)
27+
set_property(TEST ${TARGET_NAME} PROPERTY RUN_SERIAL 1)
28+
endif()
29+
endfunction()
30+
31+
py_test_modules(test_MNIST_model MODULES test_MNIST_model)
32+
33+
add_subdirectory(unittests)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
file(GLOB TEST_OPS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "test_*.py")
16+
string(REPLACE ".py" "" TEST_OPS "${TEST_OPS}")
17+
18+
foreach(TEST_OP ${TEST_OPS})
19+
py_test_modules(${TEST_OP} MODULES ${TEST_OP})
20+
endforeach(TEST_OP)
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
from __future__ import print_function
16+
17+
import numpy as np
18+
import unittest
19+
import sys
20+
from tests.op_test import OpTest
21+
import paddle
22+
import paddle.fluid as fluid
23+
from paddle.fluid import core
24+
25+
paddle.enable_static()
26+
SEED = 2021
27+
28+
29+
class TestSoftmax(OpTest):
30+
def setUp(self):
31+
self.set_npu()
32+
self.place = paddle.CustomPlace('ascend', 0)
33+
self.op_type = "softmax"
34+
self.init_dtype()
35+
36+
x = np.random.random([3, 3]).astype(self.dtype)
37+
np_out = np.exp(x) / np.sum(np.exp(x), axis=1, keepdims=True)
38+
self.inputs = {'X': x}
39+
40+
self.attrs = {}
41+
self.outputs = {'Out': np_out}
42+
43+
def set_npu(self):
44+
self.__class__.use_custom_device = True
45+
self.__class__.no_need_check_grad = True
46+
47+
def init_dtype(self):
48+
self.dtype = np.float32
49+
50+
def test_check_output(self):
51+
self.check_output_with_place(self.place)
52+
53+
54+
class TestSoftmaxNet(unittest.TestCase):
55+
def _test(self, run_npu=True):
56+
main_prog = paddle.static.Program()
57+
startup_prog = paddle.static.Program()
58+
main_prog.random_seed = SEED
59+
startup_prog.random_seed = SEED
60+
np.random.seed(SEED)
61+
62+
a_np = np.random.random(size=(4, 32)).astype('float32')
63+
b_np = np.random.random(size=(4, 32)).astype('float32')
64+
label_np = np.random.randint(2, size=(4, 1)).astype('int64')
65+
66+
with paddle.static.program_guard(main_prog, startup_prog):
67+
a = paddle.static.data(name="a", shape=[4, 32], dtype='float32')
68+
b = paddle.static.data(name="b", shape=[4, 32], dtype='float32')
69+
label = paddle.static.data(
70+
name="label", shape=[4, 1], dtype='int64')
71+
72+
c = paddle.multiply(a, b)
73+
d = paddle.sqrt(c)
74+
75+
# 4 x 128
76+
fc_1 = fluid.layers.fc(input=d, size=128)
77+
# 4 x 2
78+
prediction = fluid.layers.fc(input=fc_1, size=2)
79+
80+
# 4 x 2
81+
prob = fluid.layers.softmax(prediction, axis=1)
82+
83+
cost = fluid.layers.cross_entropy(input=prob, label=label)
84+
loss = fluid.layers.mean(cost)
85+
sgd = fluid.optimizer.SGD(learning_rate=0.01)
86+
sgd.minimize(loss)
87+
88+
if run_npu:
89+
place = paddle.CustomPlace('ascend', 0)
90+
else:
91+
place = paddle.CPUPlace()
92+
93+
exe = paddle.static.Executor(place)
94+
exe.run(startup_prog)
95+
96+
print("Start run on {}".format(place))
97+
for epoch in range(100):
98+
99+
pred_res, loss_res = exe.run(
100+
main_prog,
101+
feed={"a": a_np,
102+
"b": b_np,
103+
"label": label_np},
104+
fetch_list=[prediction, loss])
105+
if epoch % 10 == 0:
106+
print("Epoch {} | Prediction[0]: {}, Loss: {}".format(
107+
epoch, pred_res[0], loss_res))
108+
109+
return pred_res, loss_res
110+
111+
def test_npu(self):
112+
cpu_pred, cpu_loss = self._test(False)
113+
npu_pred, npu_loss = self._test(True)
114+
115+
self.assertTrue(np.allclose(npu_pred, cpu_pred, rtol=1e-2))
116+
self.assertTrue(np.allclose(npu_loss, cpu_loss, rtol=1e-2))
117+
118+
119+
if __name__ == '__main__':
120+
unittest.main()

python/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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.

python/tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../Paddle/python/paddle/fluid/tests/unittests/__init__.py

python/tests/op_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../Paddle/python/paddle/fluid/tests/unittests/op_test.py

python/tests/testsuite.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../Paddle/python/paddle/fluid/tests/unittests/testsuite.py

0 commit comments

Comments
 (0)