Skip to content

Commit 5f589ae

Browse files
committed
add clip_by_norm on kunlun, *test=kunlun (#30862)
1 parent 227a677 commit 5f589ae

File tree

3 files changed

+152
-1
lines changed

3 files changed

+152
-1
lines changed

cmake/external/xpu.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ if(NOT XPU_SDK_ROOT)
1313
elseif(WITH_SUNWAY)
1414
SET(XPU_URL "https://baidu-kunlun-public.su.bcebos.com/paddle_depence/sunway/xpu_2021_01_13.tar.gz" CACHE STRING "" FORCE)
1515
else()
16-
SET(XPU_URL "https://baidu-kunlun-public.su.bcebos.com/paddle_depence/xpu_2021_01_13.tar.gz" CACHE STRING "" FORCE)
16+
SET(XPU_URL "https://baidu-kunlun-public.su.bcebos.com/paddle_depence/xpu_2021_02_03.tar.gz" CACHE STRING "" FORCE)
1717
endif()
1818

1919
SET(XPU_SOURCE_DIR "${THIRD_PARTY_PATH}/xpu")
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/* Copyright (c) 2016 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+
#ifdef PADDLE_WITH_XPU
16+
#include "paddle/fluid/operators/clip_by_norm_op.h"
17+
#include <vector>
18+
19+
namespace paddle {
20+
namespace operators {
21+
22+
template <typename DeviceContext, typename T>
23+
class XPUClipByNormKernel : public framework::OpKernel<T> {
24+
public:
25+
void Compute(const framework::ExecutionContext& context) const override {
26+
auto max_norm = context.Attr<T>("max_norm");
27+
auto in_var = context.InputVar("X");
28+
29+
Tensor* output = nullptr;
30+
const Tensor* input = nullptr;
31+
if (in_var->IsType<framework::LoDTensor>()) {
32+
input = context.Input<Tensor>("X");
33+
34+
output = context.Output<Tensor>("Out");
35+
output->mutable_data<T>(context.GetPlace());
36+
} else {
37+
PADDLE_THROW(platform::errors::InvalidArgument(
38+
"Invalid input variable type, only support LodTensor"
39+
"type, but got type is %s.",
40+
framework::ToTypeName(in_var->Type())));
41+
}
42+
43+
PADDLE_ENFORCE_NOT_NULL(input,
44+
platform::errors::InvalidArgument(
45+
"Input(X) of ClipByNormOp should not be null. "
46+
"Please check if it is created correctly."));
47+
auto& dev_ctx = context.template device_context<DeviceContext>();
48+
const auto& x_dims = input->dims();
49+
std::vector<int> xshape(x_dims.size());
50+
std::vector<int> rdims(x_dims.size());
51+
for (int i = 0; i < x_dims.size(); i++) {
52+
xshape[i] = x_dims[i];
53+
rdims[i] = i;
54+
}
55+
int r = xpu::clip_by_norm<T>(dev_ctx.x_context(), input->data<T>(),
56+
output->data<T>(), max_norm, xshape, rdims);
57+
PADDLE_ENFORCE_EQ(
58+
r, XPU_SUCCESS,
59+
platform::errors::External("XPU API(clip_by_norm) return "
60+
"wrong value[%d], please check whether "
61+
"Baidu Kunlun Card is properly installed.",
62+
r));
63+
}
64+
};
65+
66+
} // namespace operators
67+
} // namespace paddle
68+
69+
namespace ops = paddle::operators;
70+
REGISTER_OP_XPU_KERNEL(
71+
clip_by_norm,
72+
ops::XPUClipByNormKernel<paddle::platform::XPUDeviceContext, float>);
73+
74+
#endif // PADDLE_WITH_XPU
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Copyright (c) 2020 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 sys
18+
sys.path.append("..")
19+
import unittest
20+
import numpy as np
21+
import paddle.fluid.core as core
22+
import paddle.fluid as fluid
23+
from op_test_xpu import OpTest, XPUOpTest
24+
import paddle
25+
from paddle.fluid import Program, program_guard
26+
27+
28+
class TestXPUClipByNormOp(XPUOpTest):
29+
def setUp(self):
30+
self.op_type = "clip_by_norm"
31+
self.dtype = np.float32
32+
self.use_xpu = True
33+
self.max_relative_error = 0.006
34+
self.initTestCase()
35+
input = np.random.random(self.shape).astype("float32")
36+
input[np.abs(input) < self.max_relative_error] = 0.5
37+
self.inputs = {'X': input, }
38+
self.attrs = {}
39+
self.attrs['max_norm'] = self.max_norm
40+
norm = np.sqrt(np.sum(np.square(input)))
41+
if norm > self.max_norm:
42+
output = self.max_norm * input / norm
43+
else:
44+
output = input
45+
self.outputs = {'Out': output}
46+
47+
def test_check_output(self):
48+
if paddle.is_compiled_with_xpu():
49+
paddle.enable_static()
50+
place = paddle.XPUPlace(0)
51+
self.check_output_with_place(place)
52+
53+
def initTestCase(self):
54+
self.shape = (100, )
55+
self.max_norm = 1.0
56+
57+
58+
class TestCase1(TestXPUClipByNormOp):
59+
def initTestCase(self):
60+
self.shape = (100, )
61+
self.max_norm = 1e20
62+
63+
64+
class TestCase2(TestXPUClipByNormOp):
65+
def initTestCase(self):
66+
self.shape = (16, 16)
67+
self.max_norm = 0.1
68+
69+
70+
class TestCase3(TestXPUClipByNormOp):
71+
def initTestCase(self):
72+
self.shape = (4, 8, 16)
73+
self.max_norm = 1.0
74+
75+
76+
if __name__ == "__main__":
77+
unittest.main()

0 commit comments

Comments
 (0)