utils.h 4.7 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) 2018 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 <paddle/fluid/framework/operator.h>
#include <string>
#include <vector>

namespace paddle {
namespace operators {

23
template <typename T = int32_t>
24
inline std::vector<T> GetDataFromTensor(const framework::Tensor* x) {
25 26 27
  std::vector<T> vec_new_data;
  if (x->type() == framework::proto::VarType::INT32) {
    auto* data = x->data<int>();
28
    framework::Tensor cpu_attr_tensor;
29
    if (!platform::is_cpu_place(x->place())) {
30 31 32 33 34 35
      TensorCopySync(*x, platform::CPUPlace(), &cpu_attr_tensor);
      data = cpu_attr_tensor.data<int>();
    }
    vec_new_data = std::vector<T>(data, data + x->numel());
  } else if (x->type() == framework::proto::VarType::INT64) {
    auto* data = x->data<int64_t>();
36
    framework::Tensor cpu_attr_tensor;
37
    if (!platform::is_cpu_place(x->place())) {
38 39 40
      TensorCopySync(*x, platform::CPUPlace(), &cpu_attr_tensor);
      data = cpu_attr_tensor.data<int64_t>();
    }
41
    // NOTE: Converting int64 to int32 may cause data overflow.
42 43
    vec_new_data = std::vector<T>(data, data + x->numel());
  } else {
44 45 46
    PADDLE_THROW(platform::errors::InvalidArgument(
        "The dtype of Tensor must be int32 or int64, but received: %s",
        x->type()));
47
  }
48
  return vec_new_data;
49
}
50 51

template <typename T = int32_t>
52 53 54 55 56
inline std::vector<T> GetDataFromTensorList(
    const std::vector<const framework::Tensor*>& list_tensor) {
  std::vector<T> vec_new_data;
  for (size_t i = 0; i < list_tensor.size(); ++i) {
    auto tensor = list_tensor[i];
57
    PADDLE_ENFORCE_EQ(tensor->dims(), framework::make_ddim({1}),
58 59 60 61 62
                      platform::errors::InvalidArgument(
                          "The shape of Tensor in list must be [1]. "
                          "But received its shape "
                          "is [%s]",
                          tensor->dims()));
63 64

    if (tensor->type() == framework::proto::VarType::INT32) {
65
      if (!platform::is_cpu_place(tensor->place())) {
66 67 68 69 70 71 72
        framework::Tensor temp;
        TensorCopySync(*tensor, platform::CPUPlace(), &temp);
        vec_new_data.push_back(static_cast<T>(*temp.data<int>()));
      } else {
        vec_new_data.push_back(static_cast<T>(*tensor->data<int>()));
      }
    } else if (tensor->type() == framework::proto::VarType::INT64) {
73
      if (!platform::is_cpu_place(tensor->place())) {
74 75
        framework::Tensor temp;
        TensorCopySync(*tensor, platform::CPUPlace(), &temp);
76
        // NOTE: Converting int64 to int32 may cause data overflow.
77 78 79 80
        vec_new_data.push_back(static_cast<T>(*temp.data<int64_t>()));
      } else {
        vec_new_data.push_back(static_cast<T>(*tensor->data<int64_t>()));
      }
81
    } else {
82 83 84 85
      PADDLE_THROW(platform::errors::InvalidArgument(
          "The dtype of Tensor in list must be int32 or int64, but received: "
          "%s",
          tensor->type()));
86 87 88 89
    }
  }
  return vec_new_data;
}
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

inline framework::DDim GetShape(const framework::ExecutionContext& ctx) {
  // 1. shape is a Tensor
  if (ctx.HasInput("ShapeTensor")) {
    auto* shape_tensor = ctx.Input<framework::LoDTensor>("ShapeTensor");
    auto vec_shape = GetDataFromTensor<int>(shape_tensor);
    return framework::make_ddim(vec_shape);
  }

  // 2. shape is a list/tuple containing Tensor
  auto shape_tensor_list = ctx.MultiInput<framework::Tensor>("ShapeTensorList");
  if (shape_tensor_list.size() > 0) {
    auto vec_shape = GetDataFromTensorList(shape_tensor_list);
    return framework::make_ddim(vec_shape);
  }

  // 3. shape is a list/tuple without containing Tensor
  auto vec_shape = ctx.Attr<std::vector<int64_t>>("shape");
  return framework::make_ddim(vec_shape);
}

111 112 113 114 115 116
template <typename T>
inline T GetValue(const framework::Tensor* x) {
  T value = static_cast<T>(0);
  if (!platform::is_cpu_place(x->place())) {
    framework::Tensor cpu_x;
    framework::TensorCopy(*x, platform::CPUPlace(), &cpu_x);
117 118 119 120 121
#ifdef PADDLE_WITH_ASCEND_CL
    platform::DeviceContextPool& pool = platform::DeviceContextPool::Instance();
    const platform::DeviceContext* dev_ctx = pool.Get(x->place());
    dev_ctx->Wait();
#endif
122 123 124 125 126 127 128
    value = cpu_x.data<T>()[0];
  } else {
    value = x->data<T>()[0];
  }
  return value;
}

129 130
}  // namespace operators
}  // namespace paddle