expand_v2_op.h 3.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* Copyright (c) 2016 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. */

#pragma once

#include <algorithm>
#include <vector>

#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
23
#include "paddle/fluid/operators/eigen/eigen_function.h"
24 25 26 27 28 29 30 31 32 33 34 35

#define MAX_RANK_SUPPORTED 6

namespace paddle {
namespace operators {
inline std::vector<int> get_expand_shape(
    const framework::ExecutionContext& ctx) {
  if (ctx.HasInput("Shape")) {
    auto* shape_tensor = ctx.Input<framework::LoDTensor>("Shape");
    auto* shape_data = shape_tensor->data<int>();
    framework::Tensor cpu_shape_tensor;
    if (platform::is_gpu_place(shape_tensor->place())) {
36 37
      paddle::framework::TensorCopySync(*shape_tensor, platform::CPUPlace(),
                                        &cpu_shape_tensor);
38 39
      shape_data = cpu_shape_tensor.data<int>();
    }
40 41
#ifdef PADDLE_WITH_ASCEND_CL
    if (platform::is_npu_place(shape_tensor->place())) {
42 43
      paddle::framework::TensorCopySync(*shape_tensor, platform::CPUPlace(),
                                        &cpu_shape_tensor);
44 45
      shape_data = cpu_shape_tensor.data<int>();
    }
46 47 48
#endif
#ifdef PADDLE_WITH_XPU
    if (platform::is_xpu_place(shape_tensor->place())) {
49 50
      paddle::framework::TensorCopySync(*shape_tensor, platform::CPUPlace(),
                                        &cpu_shape_tensor);
51 52
      shape_data = cpu_shape_tensor.data<int>();
    }
53
#endif
54 55 56 57 58 59 60 61 62 63 64 65 66 67
    auto vec_shape =
        std::vector<int>(shape_data, shape_data + shape_tensor->numel());
    return vec_shape;
  }

  auto list_expand_shapes_tensor =
      ctx.MultiInput<framework::Tensor>("expand_shapes_tensor");
  if (list_expand_shapes_tensor.size() > 0) {
    // get tensor from
    std::vector<int> vec_epxand_shape;
    for (size_t i = 0; i < list_expand_shapes_tensor.size(); ++i) {
      auto tensor = list_expand_shapes_tensor[i];
      if (platform::is_gpu_place(tensor->place())) {
        framework::Tensor temp;
68
        paddle::framework::TensorCopySync(*tensor, platform::CPUPlace(), &temp);
69
        vec_epxand_shape.push_back(*temp.data<int32_t>());
70 71 72 73
      }
#ifdef PADDLE_WITH_ASCEND_CL
      else if (platform::is_npu_place(tensor->place())) {  // NOLINT
        framework::Tensor temp;
74
        paddle::framework::TensorCopySync(*tensor, platform::CPUPlace(), &temp);
75 76 77 78 79 80
        vec_epxand_shape.push_back(*temp.data<int32_t>());
      }
#endif
#ifdef PADDLE_WITH_XPU
      else if (platform::is_xpu_place(tensor->place())) {  // NOLINT
        framework::Tensor temp;
81
        paddle::framework::TensorCopySync(*tensor, platform::CPUPlace(), &temp);
82 83 84 85
        vec_epxand_shape.push_back(*temp.data<int32_t>());
      }
#endif
      else {  // NOLINT
86 87 88 89 90 91 92 93 94 95
        vec_epxand_shape.push_back(*tensor->data<int32_t>());
      }
    }
    return vec_epxand_shape;
  } else {
    return ctx.Attr<std::vector<int>>("shape");
  }
}
}  // namespace operators
}  // namespace paddle