forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NPU] add Assign OP (PaddlePaddle#31561)
* add assign op * add test assign npu test * dele if def Co-authored-by: oyjxer <1728722986@qq.com>
- Loading branch information
1 parent
c28885e
commit 20a165c
Showing
4 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* 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 <string> | ||
|
||
#include "paddle/fluid/operators/assign_op.h" | ||
#include "paddle/fluid/platform/float16.h" | ||
#include "paddle/fluid/operators/npu_op_runner.h" | ||
|
||
namespace paddle { | ||
namespace framework { | ||
class OpDesc; | ||
class Variable; | ||
} // namespace framework | ||
namespace imperative { | ||
class OpBase; | ||
} // namespace imperative | ||
namespace platform { | ||
struct CPUPlace; | ||
struct CUDAPlace; | ||
struct float16; | ||
} // namespace platform | ||
} // namespace paddle | ||
|
||
namespace paddle { | ||
namespace operators { | ||
template <typename DeviceContext, typename T> | ||
class AssignNPUKernel : public framework::OpKernel<T> { | ||
public: | ||
void Compute(const framework::ExecutionContext& ctx) const override { | ||
auto* x = ctx.Input<framework::LoDTensor>("X"); | ||
auto* out = ctx.Output<framework::LoDTensor>("Out"); | ||
out->mutable_data<T>(ctx.GetPlace()); | ||
|
||
auto runner = NpuOpRunner("Assign", {*out, *x}, {*out}, {}); | ||
auto stream = | ||
ctx.template device_context<paddle::platform::NPUDeviceContext>() | ||
.stream(); | ||
runner.Run(stream); | ||
} | ||
}; | ||
|
||
} // namespace operators | ||
} // namespace paddle | ||
|
||
namespace ops = paddle::operators; | ||
namespace plat = paddle::platform; | ||
|
||
REGISTER_OP_NPU_KERNEL(assign, | ||
ops::AssignNPUKernel<paddle::platform::NPUDeviceContext, int>, | ||
ops::AssignNPUKernel<paddle::platform::NPUDeviceContext, float>, | ||
ops::AssignNPUKernel<paddle::platform::NPUDeviceContext, double>) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* 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. */ | ||
|
||
#ifndef _WIN32 | ||
#include <unistd.h> | ||
#endif | ||
|
||
#include <string> | ||
#include <thread> // NOLINT | ||
#include <vector> | ||
|
||
#include "gtest/gtest.h" | ||
#include "paddle/fluid/framework/op_registry.h" | ||
#include "paddle/fluid/framework/operator.h" | ||
#include "paddle/fluid/framework/program_desc.h" | ||
#include "paddle/fluid/operators/dropout_op.h" | ||
#include "paddle/fluid/operators/math/math_function.h" | ||
#include "paddle/fluid/string/printf.h" | ||
|
||
namespace f = paddle::framework; | ||
namespace p = paddle::platform; | ||
namespace m = paddle::operators::math; | ||
|
||
USE_OP(assign); | ||
USE_OP_DEVICE_KERNEL(assign, NPU); | ||
|
||
template <typename T> | ||
void Compare(f::Scope* scope, const p::DeviceContext& ctx, | ||
std::string op_type) { | ||
// init | ||
auto x = scope->Var("X"); | ||
auto tensor_x = x->GetMutable<f::LoDTensor>(); | ||
|
||
std::vector<T> init; | ||
init.push_back(static_cast<T>(1.0)); | ||
init.push_back(static_cast<T>(2.0)); | ||
init.push_back(static_cast<T>(3.0)); | ||
init.push_back(static_cast<T>(4.0)); | ||
|
||
TensorFromVector(init, ctx, tensor_x); | ||
tensor_x->Resize({4}); | ||
|
||
ctx.Wait(); | ||
|
||
auto place = ctx.GetPlace(); | ||
auto out = scope->Var("Out"); | ||
auto tensor_out = out->GetMutable<f::LoDTensor>(); | ||
|
||
auto op = f::OpRegistry::CreateOp(op_type, | ||
{{"X", {"X"}}}, | ||
{{"Out", {"Out"}}}, | ||
{}); | ||
|
||
op->Run(*scope, place); | ||
|
||
std::vector<T> out_vec; | ||
TensorToVector(*tensor_out, ctx, &out_vec); | ||
|
||
ctx.Wait(); | ||
|
||
EXPECT_EQ((uint32_t)out_vec.size(), (uint32_t)4); | ||
EXPECT_EQ(out_vec[0], static_cast<T>(1.0)); | ||
EXPECT_EQ(out_vec[1], static_cast<T>(2.0)); | ||
EXPECT_EQ(out_vec[2], static_cast<T>(3.0)); | ||
EXPECT_EQ(out_vec[3], static_cast<T>(4.0)); | ||
} | ||
|
||
|
||
TEST(assign, NPU_fp32) { | ||
f::Scope scope; | ||
p::NPUDeviceContext ctx(p::NPUPlace(0)); | ||
Compare<float>(&scope, ctx, "assign"); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# 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 | ||
|
||
paddle.enable_static() | ||
SEED = 2021 | ||
|
||
|
||
@unittest.skipIf(not paddle.is_compiled_with_npu(), | ||
"core is not compiled with NPU") | ||
class TestAssign(OpTest): | ||
def setUp(self): | ||
self.set_npu() | ||
self.place = paddle.NPUPlace(0) | ||
self.op_type = "assign" | ||
self.init_dtype() | ||
|
||
x = np.rand.random([3,3]) | ||
self.inputs = {'X': x} | ||
|
||
self.attrs = {} | ||
self.outputs = {'Out': x} | ||
|
||
def set_npu(self): | ||
self.__class__.use_npu = True | ||
|
||
def init_dtype(self): | ||
self.dtype = np.int64 | ||
|
||
def test_check_output(self): | ||
self.check_output_with_place(self.place, check_dygraph=False) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() | ||
|