elementwise_op_broadcast.cu.h 2.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.1 (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.1
//
// 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.

#pragma once

17
#include "paddle/fluid/operators/elementwise/elementwise_op_impl.cu.h"
18

19 20 21
namespace paddle {
namespace operators {

22
template <typename OutT, typename Functor, int NumOuts = 1>
23
void LaunchElementwiseCudaKernel(
24
    const KPDevice &ctx,
25 26
    const std::vector<const phi::DenseTensor *> &ins,
    std::vector<phi::DenseTensor *> *outs,
27 28
    Functor func,
    int axis = -1) {
29 30
  std::vector<const phi::DenseTensor *> pt_inputs;
  std::vector<phi::DenseTensor *> pt_outputs;
31 32
  // TODO(YuanRisheng) *_tmp for cache DenseTensor, because the temporary
  // DenseTensor obj
33
  // generated by MakePhiDenseTensor can be destroyed when exits loop. *_tmp
34 35
  // can be deleted
  // when DenseTensor support copy constructor.
36 37
  std::vector<std::unique_ptr<phi::DenseTensor>> pt_inputs_tmp;
  std::vector<std::unique_ptr<phi::DenseTensor>> pt_outputs_tmp;
38 39
  for (auto in : ins) {
    pt_inputs_tmp.emplace_back(
H
Huang Jiyi 已提交
40
        std::move(std::make_unique<phi::DenseTensor>(*in)));
41
  }
42 43
  for (auto out : *outs) {
    pt_outputs_tmp.emplace_back(
H
Huang Jiyi 已提交
44
        std::move(std::make_unique<phi::DenseTensor>(*out)));
45 46 47 48 49 50
  }
  for (int i = 0; i < pt_inputs_tmp.size(); i++) {
    pt_inputs.push_back(pt_inputs_tmp[i].get());
  }
  for (int i = 0; i < pt_outputs_tmp.size(); i++) {
    pt_outputs.push_back(pt_outputs_tmp[i].get());
51
  }
52 53
  phi::funcs::BroadcastKernel<OutT, Functor, NumOuts>(
      ctx, pt_inputs, &pt_outputs, func, axis);
54 55
}

56 57
}  // namespace operators
}  // namespace paddle