meshgrid_op_npu.cc 2.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/* 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
13
limitations under the License. */
14 15

#include "paddle/fluid/operators/meshgrid_op.h"
16
#include "paddle/fluid/platform/device/npu/npu_op_runner.h"
17 18 19 20

namespace paddle {
namespace operators {

21
template <typename T>
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
class MeshgridNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    auto ins = context.MultiInput<framework::Tensor>("X");
    auto outs = context.MultiOutput<framework::Tensor>("Out");
    PADDLE_ENFORCE_EQ(
        (ins.size() > 1) && (ins.size() < 7), true,
        platform::errors::InvalidArgument(
            "Excepted Tensor numbers between 2 and 6, but only received d% .",
            ins.size()));

    int64_t size = ins.size();
    std::vector<int64_t> shape(size);

    for (int64_t i = 0; i < size; i++) {
      switch (ins[i]->dims().size()) {
        case 0:
          shape[i] = 1;
          break;
        case 1:
          shape[i] = ins[i]->dims()[0];
          break;
        default:
          PADDLE_THROW(platform::errors::InvalidArgument(
              "Expected scalar or 1D tensor in the tensor list but got tensor "
              "%d: ",
              i));
      }
    }

    for (int64_t i = 0; i < size; i++) {
      std::vector<int64_t> view_shape(size, 1);
      view_shape[i] = shape[i];

56
      framework::DDim out_dims_reshape = pten::make_ddim(view_shape);
57
      framework::Tensor reshape_ins_tensor(ins[i]->dtype());
58 59 60
      reshape_ins_tensor.ShareDataWith(*ins[i]);
      reshape_ins_tensor.Resize(out_dims_reshape);

61
      framework::DDim out_dims = pten::make_ddim(shape);
62 63 64 65 66 67
      outs[i]->Resize(out_dims);
      outs[i]->mutable_data<T>(context.GetPlace());

      auto stream =
          context.template device_context<paddle::platform::NPUDeviceContext>()
              .stream();
68 69 70 71 72 73
      NpuOpRunner runner;
      runner.SetType("BroadcastTo")
          .AddInput(reshape_ins_tensor)
          .AddInput(std::move(shape))
          .AddOutput(*(outs[i]))
          .Run(stream);
74 75 76 77 78 79 80 81
    }
  }
};

}  // namespace operators
}  // namespace paddle

REGISTER_OP_NPU_KERNEL(
82 83 84 85 86 87
    meshgrid, paddle::operators::MeshgridNPUKernel<int>,
#ifdef PADDLE_WITH_ASCEND_INT64
    paddle::operators::MeshgridNPUKernel<int64_t>,
#endif
    paddle::operators::MeshgridNPUKernel<float>,
    paddle::operators::MeshgridNPUKernel<paddle::platform::float16>);