expand_v2_op.h 4.0 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 54 55 56 57 58 59
#endif
#ifdef PADDLE_WITH_MLU
    if (platform::is_mlu_place(shape_tensor->place())) {
      paddle::framework::TensorCopySync(*shape_tensor, platform::CPUPlace(),
                                        &cpu_shape_tensor);
      shape_data = cpu_shape_tensor.data<int>();
    }
60
#endif
61 62 63 64 65 66 67 68 69 70 71 72 73 74
    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;
75
        paddle::framework::TensorCopySync(*tensor, platform::CPUPlace(), &temp);
76
        vec_epxand_shape.push_back(*temp.data<int32_t>());
77 78 79 80
      }
#ifdef PADDLE_WITH_ASCEND_CL
      else if (platform::is_npu_place(tensor->place())) {  // NOLINT
        framework::Tensor temp;
81
        paddle::framework::TensorCopySync(*tensor, platform::CPUPlace(), &temp);
82 83 84 85 86 87
        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;
88
        paddle::framework::TensorCopySync(*tensor, platform::CPUPlace(), &temp);
89 90
        vec_epxand_shape.push_back(*temp.data<int32_t>());
      }
91 92 93 94 95 96 97
#endif
#ifdef PADDLE_WITH_MLU
      else if (platform::is_mlu_place(tensor->place())) {  // NOLINT
        framework::Tensor temp;
        paddle::framework::TensorCopySync(*tensor, platform::CPUPlace(), &temp);
        vec_epxand_shape.push_back(*temp.data<int32_t>());
      }
98 99
#endif
      else {  // NOLINT
100 101 102 103 104 105 106 107 108 109
        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