未验证 提交 1f28968b 编写于 作者: L Leo Chen 提交者: GitHub

[NPU] Fix vector overflow in slice grad npu op (#34032)

* fix vector overflow

* refine code

* refine ut
上级 fd85be80
...@@ -25,15 +25,16 @@ namespace operators { ...@@ -25,15 +25,16 @@ namespace operators {
using Tensor = framework::Tensor; using Tensor = framework::Tensor;
void UpdateAttr(const framework::DDim in_dims, const std::vector<int> axes, void UpdateAttr(const framework::DDim& in_dims, const std::vector<int> axes,
const std::vector<int> starts, const std::vector<int> ends, const std::vector<int> starts, const std::vector<int> ends,
std::vector<int>* offsets, std::vector<int>* size) { std::vector<int>* offsets, std::vector<int>* size) {
int cnt = 0; int cnt = 0;
for (int i = 0; i < in_dims.size(); ++i) { for (int i = 0; i < in_dims.size(); ++i) {
int start = 0; int start = 0;
int end = in_dims[i]; int end = in_dims[i];
int axis = axes[cnt]; // NOTE(zhiqiu): Becareful that cnt may > axes.size() and result in
// overflow.
int axis = cnt < static_cast<int>(axes.size()) ? axes[cnt] : -1;
if (axis == i) { if (axis == i) {
start = starts[cnt]; start = starts[cnt];
if (start < 0) { if (start < 0) {
...@@ -63,10 +64,10 @@ class SliceNPUKernel : public framework::OpKernel<T> { ...@@ -63,10 +64,10 @@ class SliceNPUKernel : public framework::OpKernel<T> {
auto axes = ctx.Attr<std::vector<int>>("axes"); auto axes = ctx.Attr<std::vector<int>>("axes");
auto starts = ctx.Attr<std::vector<int>>("starts"); auto starts = ctx.Attr<std::vector<int>>("starts");
auto ends = ctx.Attr<std::vector<int>>("ends"); auto ends = ctx.Attr<std::vector<int>>("ends");
const auto& in_dims = input->dims();
out->mutable_data<T>(ctx.GetPlace()); out->mutable_data<T>(ctx.GetPlace());
auto in_dims = input->dims();
std::vector<int> offsets(in_dims.size()); std::vector<int> offsets(in_dims.size());
std::vector<int> size(in_dims.size()); std::vector<int> size(in_dims.size());
...@@ -93,8 +94,7 @@ class SliceGradNPUKernel : public framework::OpKernel<T> { ...@@ -93,8 +94,7 @@ class SliceGradNPUKernel : public framework::OpKernel<T> {
auto axes = ctx.Attr<std::vector<int>>("axes"); auto axes = ctx.Attr<std::vector<int>>("axes");
auto starts = ctx.Attr<std::vector<int>>("starts"); auto starts = ctx.Attr<std::vector<int>>("starts");
auto ends = ctx.Attr<std::vector<int>>("ends"); auto ends = ctx.Attr<std::vector<int>>("ends");
const auto& in_dims = input->dims();
auto in_dims = input->dims();
int rank = in_dims.size(); int rank = in_dims.size();
std::vector<int> offsets(rank); std::vector<int> offsets(rank);
......
...@@ -71,12 +71,12 @@ class TestSliceOp(OpTest): ...@@ -71,12 +71,12 @@ class TestSliceOp(OpTest):
class TestSliceOp2(TestSliceOp): class TestSliceOp2(TestSliceOp):
def config(self): def config(self):
self.input = np.random.random([3, 4, 5, 6]).astype(self.dtype) self.input = np.random.random([10, 5, 6]).astype(self.dtype)
self.starts = [1, 0, -3] self.starts = [0]
self.ends = [3, 3, -1] self.ends = [1]
self.axes = [0, 1, 2] self.axes = [1]
self.infer_flags = [1, 1, 1] self.infer_flags = [1]
self.out = self.input[1:3, 0:3, -3:-1, :] self.out = self.input[:, 0:1, :]
@unittest.skipIf(not paddle.is_compiled_with_npu(), @unittest.skipIf(not paddle.is_compiled_with_npu(),
...@@ -118,8 +118,8 @@ class TestSliceNet(unittest.TestCase): ...@@ -118,8 +118,8 @@ class TestSliceNet(unittest.TestCase):
prediction = paddle.static.nn.fc(z, size=2, activation='softmax') prediction = paddle.static.nn.fc(z, size=2, activation='softmax')
cost = paddle.nn.functional.cross_entropy( cost = paddle.fluid.layers.softmax_with_cross_entropy(
input=prediction, label=label) logits=prediction, label=label)
loss = paddle.mean(cost) loss = paddle.mean(cost)
sgd = paddle.optimizer.SGD(learning_rate=0.01) sgd = paddle.optimizer.SGD(learning_rate=0.01)
sgd.minimize(loss) sgd.minimize(loss)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册