Skip to content

Revert "refactor the forward implementation of shape npu op" #39719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions paddle/fluid/operators/shape_op_npu.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
/* 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.
Expand All @@ -12,6 +12,9 @@ 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 <memory>
#include <string>

#include "paddle/fluid/operators/shape_op.h"
#include "paddle/fluid/platform/device/npu/npu_op_runner.h"

Expand All @@ -24,20 +27,20 @@ template <typename DeviceContext, typename T>
class ShapeNPUKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& ctx) const override {
auto* x = ctx.Input<Tensor>("Input");
auto* in_var = ctx.InputVar("Input");
framework::DDim in_dims;
if (in_var->IsType<pten::SelectedRows>()) {
in_dims = in_var->Get<pten::SelectedRows>().value().dims();
} else {
in_dims = in_var->Get<LoDTensor>().dims();
}
auto* out_t = ctx.Output<Tensor>("Out");
out_t->Resize({x->dims().size()});
out_t->mutable_data<int32_t>(ctx.GetPlace());

// The output data type defaults to int32.
auto stream =
ctx.template device_context<paddle::platform::NPUDeviceContext>()
.stream();
NpuOpRunner runner;
auto dst_dtype = ConvertToNpuDtype(framework::proto::VarType::INT32);
runner.SetType("Shape").AddInput(*x).AddOutput(*out_t).AddAttr(
"dtype", static_cast<int>(dst_dtype));
runner.Run(stream);
out_t->Resize({in_dims.size()});
// to do: cpuplace?
auto out_data = out_t->mutable_data<int32_t>(platform::CPUPlace());
for (int i = 0; i < in_dims.size(); ++i) {
out_data[i] = in_dims[i];
}
}
};

Expand All @@ -52,7 +55,5 @@ REGISTER_OP_NPU_KERNEL(
ops::ShapeNPUKernel<paddle::platform::NPUDeviceContext, int8_t>,
ops::ShapeNPUKernel<paddle::platform::NPUDeviceContext, uint8_t>,
ops::ShapeNPUKernel<paddle::platform::NPUDeviceContext, int64_t>,
ops::ShapeNPUKernel<paddle::platform::NPUDeviceContext,
paddle::platform::float16>,
ops::ShapeNPUKernel<paddle::platform::NPUDeviceContext, float>,
ops::ShapeNPUKernel<paddle::platform::NPUDeviceContext, double>);
20 changes: 0 additions & 20 deletions python/paddle/fluid/tests/unittests/npu/test_shape_op_npu.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,5 @@ def test_check_output(self):
self.check_output_with_place(self.place)


class TestShape_fp16(TestShape):
def init_dtype(self):
self.dtype = np.float16


class TestShape_double(TestShape):
def init_dtype(self):
self.dtype = np.float64


class TestShape_int32(TestShape):
def init_dtype(self):
self.dtype = np.int32


class TestShape_int64(TestShape):
def init_dtype(self):
self.dtype = np.int64


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