Skip to content
70 changes: 70 additions & 0 deletions paddle/fluid/operators/eye_op_npu.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include "paddle/fluid/operators/crop_op.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个头文件应该是 eye_op.h?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

#include "paddle/fluid/operators/npu_op_runner.h"

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;

template <typename DeviceContext, typename T>
class EyeNPUKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& ctx) const override {
auto num_rows = ctx.Attr<int64_t>("num_rows");
PADDLE_ENFORCE_EQ(
num_rows >= 0, true,
platform::errors::InvalidArgument(
"The value of Input(num_rows) should be non-negative int."));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个不需要检查,在EyeOp::InferShape的时候已经检查过了。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


auto d_nums = ctx.Attr<int>("dtype");
auto dtype =
ConvertToNpuDtype(static_cast<framework::proto::VarType::Type>(d_nums));

auto num_columns = num_rows;
PADDLE_ENFORCE_EQ(
num_columns >= 0, true,
platform::errors::InvalidArgument(
"The value of Input(num_columns) should be non-negative int."));

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上,这个不需要检查

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

if (ctx.HasAttr("num_columns")) {
num_columns = ctx.Attr<int64_t>("num_columns");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个最好参考eye_op.h通过判断num_columns是否等于-1,避免单测或者python API把这个num_columns设置为-1的情况。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


framework::NPUAttributeMap attr_input = {
{"num_rows", num_rows}, {"num_columns", num_columns}, {"dtype", dtype}};

auto* out = ctx.Output<framework::Tensor>("Out");
out->mutable_data<T>(ctx.GetPlace());

const auto& runner = NpuOpRunner("Eye", {}, {*out}, attr_input);
auto stream =
ctx.template device_context<paddle::platform::NPUDeviceContext>()
.stream();
runner.Run(stream);
}
};

} // namespace operators
} // namespace paddle

namespace ops = paddle::operators;

REGISTER_OP_NPU_KERNEL(
eye, ops::EyeNPUKernel<paddle::platform::NPUDeviceContext, float>,
ops::EyeNPUKernel<paddle::platform::NPUDeviceContext, int>,
ops::EyeNPUKernel<paddle::platform::NPUDeviceContext,
paddle::platform::float16>);
105 changes: 105 additions & 0 deletions python/paddle/fluid/tests/unittests/npu/test_eye_op_npu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function

import numpy as np
import unittest
import sys
sys.path.append("..")
from op_test import OpTest
import paddle
import paddle.fluid as fluid
from paddle.fluid import core
import paddle.fluid.framework as framework

paddle.enable_static()
np.random.seed(10)


class TestEyeOp(OpTest):
def setUp(self):
'''
Test eye op with specified shape
'''
self.set_npu()
self.place = paddle.NPUPlace(0)
self.op_type = "eye"
self.inputs = {}

self.num_rows = 0
self.num_columns = 0
self.dtype = np.float32

self.initTestCase()

if self.num_columns == 0:
self.attrs = {
'num_rows': self.num_rows,
'dtype': framework.convert_np_dtype_to_dtype_(self.dtype)
}
self.outputs = {'Out': np.eye(self.num_rows, dtype=self.dtype)}
else:
self.attrs = {
'num_rows': self.num_rows,
'num_columns': self.num_columns,
'dtype': framework.convert_np_dtype_to_dtype_(self.dtype)
}
self.outputs = {
'Out': np.eye(self.num_rows, self.num_columns, dtype=self.dtype)
}

def initTestCase(self):
self.num_rows = 219
self.num_columns = 319
self.dtype = np.int32

def set_npu(self):
self.__class__.use_npu = True

def test_check_output(self):
self.check_output_with_place(self.place)


class TestEyeOp1(TestEyeOp):
def initTestCase(self):
self.num_rows = 50


class TestEyeOp2(TestEyeOp):
def initTestCase(self):
self.num_rows = 50
self.dtype = np.int32


class TestEyeOp3(TestEyeOp):
def initTestCase(self):
self.num_rows = 50
self.dtype = np.float16


class TestEyeOp4(TestEyeOp):
def initTestCase(self):
self.num_rows = 1
self.num_columns = 99


class TestEyeOp5(TestEyeOp):
def initTestCase(self):
self.num_rows = 100
self.num_columns = 100


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

少了一个dtype是fp32的单测,另外参考 test_eye_op.py 的 class API_TestTensorEye(unittest.TestCase),增加关于静态图API和动态图API的测试。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done
image

if __name__ == "__main__":
unittest.main()