Skip to content

Commit 5e5c282

Browse files
authored
fix range op crash in dygraph xpu place (#30469)
1 parent 18ecd43 commit 5e5c282

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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/range_op.h"
17+
#include "paddle/fluid/framework/op_registry.h"
18+
19+
namespace paddle {
20+
namespace operators {
21+
22+
template <typename T>
23+
class XPURangeKernel : public framework::OpKernel<T> {
24+
public:
25+
void Compute(const framework::ExecutionContext& context) const override {
26+
auto* start_t = context.Input<framework::Tensor>("Start");
27+
auto* end_t = context.Input<framework::Tensor>("End");
28+
auto* step_t = context.Input<framework::Tensor>("Step");
29+
auto* out = context.Output<framework::Tensor>("Out");
30+
31+
framework::Tensor n;
32+
framework::TensorCopy(*start_t, platform::CPUPlace(), &n);
33+
T start = n.data<T>()[0];
34+
framework::TensorCopy(*end_t, platform::CPUPlace(), &n);
35+
T end = n.data<T>()[0];
36+
framework::TensorCopy(*step_t, platform::CPUPlace(), &n);
37+
T step = n.data<T>()[0];
38+
39+
int64_t size = 0;
40+
GetSize(start, end, step, &size);
41+
out->Resize(framework::make_ddim({size}));
42+
43+
T* out_data = out->mutable_data<T>(context.GetPlace());
44+
45+
framework::Tensor out_cpu;
46+
T* out_cpu_data_ptr =
47+
out_cpu.mutable_data<T>(platform::CPUPlace(), out->numel() * sizeof(T));
48+
T value = start;
49+
for (int64_t i = 0; i < size; ++i) {
50+
out_cpu_data_ptr[i] = value;
51+
value += step;
52+
}
53+
int ret = xpu_memcpy(out_data, out_cpu_data_ptr, out->numel() * sizeof(T),
54+
XPUMemcpyKind::XPU_HOST_TO_DEVICE);
55+
PADDLE_ENFORCE_EQ(ret, XPU_SUCCESS,
56+
platform::errors::External("XPU xpu_memcpy return wrong "
57+
"value[%d %s]",
58+
ret, XPUAPIErrorMsg[ret]));
59+
}
60+
};
61+
62+
} // namespace operators
63+
} // namespace paddle
64+
65+
namespace ops = paddle::operators;
66+
REGISTER_OP_XPU_KERNEL(range, ops::XPURangeKernel<int>,
67+
ops::XPURangeKernel<int64_t>, ops::XPURangeKernel<float>,
68+
ops::XPURangeKernel<double>);
69+
#endif // PADDLE_WITH_XPU
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Copyright (c) 2018 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 unittest
18+
import paddle
19+
import numpy as np
20+
import sys
21+
sys.path.append("..")
22+
from op_test_xpu import XPUOpTest
23+
24+
paddle.enable_static()
25+
26+
27+
class TestRangeOp(XPUOpTest):
28+
def setUp(self):
29+
self.op_type = "range"
30+
self.init_config()
31+
self.inputs = {
32+
'Start': np.array([self.case[0]]).astype(self.dtype),
33+
'End': np.array([self.case[1]]).astype(self.dtype),
34+
'Step': np.array([self.case[2]]).astype(self.dtype)
35+
}
36+
37+
self.outputs = {
38+
'Out': np.arange(self.case[0], self.case[1],
39+
self.case[2]).astype(self.dtype)
40+
}
41+
42+
def init_config(self):
43+
self.dtype = np.float32
44+
self.case = (0, 1, 0.2)
45+
46+
def test_check_output(self):
47+
place = paddle.XPUPlace(0)
48+
self.check_output_with_place(place, check_dygraph=False)
49+
50+
51+
class TestFloatRangeOpCase0(TestRangeOp):
52+
def init_config(self):
53+
self.dtype = np.float32
54+
self.case = (0, 5, 1)
55+
56+
57+
class TestInt32RangeOpCase0(TestRangeOp):
58+
def init_config(self):
59+
self.dtype = np.int32
60+
self.case = (0, 5, 2)
61+
62+
63+
class TestInt32RangeOpCase1(TestRangeOp):
64+
def init_config(self):
65+
self.dtype = np.int32
66+
self.case = (10, 1, -2)
67+
68+
69+
class TestInt32RangeOpCase2(TestRangeOp):
70+
def init_config(self):
71+
self.dtype = np.int32
72+
self.case = (-1, -10, -2)
73+
74+
75+
if __name__ == "__main__":
76+
unittest.main()

0 commit comments

Comments
 (0)