eager_properties.cc 7.6 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.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. */
// disable numpy compile error
#include <Python.h>

#include <string>
#include <vector>

17
#include "paddle/fluid/eager/accumulation/accumulation_node.h"
18
#include "paddle/fluid/eager/api/all.h"
19
#include "paddle/fluid/eager/api/utils/tensor_utils.h"
20 21 22 23 24 25 26 27
#include "paddle/fluid/eager/autograd_meta.h"
#include "paddle/fluid/eager/utils.h"
#include "paddle/fluid/memory/allocation/allocator.h"
#include "paddle/fluid/memory/memcpy.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/pybind/eager.h"
#include "paddle/fluid/pybind/eager_utils.h"
#include "paddle/fluid/pybind/exception.h"
28 29 30
#include "paddle/phi/common/data_type.h"
#include "paddle/phi/core/compat/convert_utils.h"
#include "paddle/phi/core/dense_tensor.h"
31 32 33 34 35
#pragma GCC diagnostic ignored "-Wwrite-strings"

namespace paddle {
namespace pybind {

36
extern PyTypeObject* p_tensor_type;
37

38 39
PyObject* tensor_properties_get_name(TensorObject* self, void* closure) {
  EAGER_TRY
40
  return ToPyObject(self->tensor.name());
41 42 43
  EAGER_CATCH_AND_THROW_RETURN_NULL
}

44 45
PyObject* tensor_properties_get_type(TensorObject* self, void* closure) {
  EAGER_TRY
46 47 48 49
  if (!self->tensor.defined()) {
    // be same to old dygraph
    return ToPyObject(paddle::framework::proto::VarType::LOD_TENSOR);
  }
50
  if (self->tensor.is_dense_tensor()) {
51
    return ToPyObject(paddle::framework::proto::VarType::LOD_TENSOR);
52 53
  } else if (self->tensor.is_selected_rows()) {
    return ToPyObject(paddle::framework::proto::VarType::SELECTED_ROWS);
54
  } else {
55
    RETURN_PY_NONE
56 57 58 59
  }
  EAGER_CATCH_AND_THROW_RETURN_NULL
}

W
wanghuancoder 已提交
60 61 62 63 64 65
PyObject* tensor_properties_is_leaf(TensorObject* self, void* closure) {
  EAGER_TRY
  return ToPyObject(egr::egr_utils_api::IsLeafTensor(self->tensor));
  EAGER_CATCH_AND_THROW_RETURN_NULL
}

66 67 68
int tensor_properties_set_name(TensorObject* self, PyObject* value,
                               void* closure) {
  EAGER_TRY
69
  self->tensor.set_name(CastPyArg2AttrString(value, 0));
70
  return 0;
0
0x45f 已提交
71
  EAGER_CATCH_AND_THROW_RETURN_NEG
72 73
}

74 75 76
PyObject* tensor_properties_get_stop_gradient(TensorObject* self,
                                              void* closure) {
  EAGER_TRY
77
  auto meta = egr::EagerUtils::autograd_meta(&self->tensor);
78 79 80 81
  return ToPyObject(meta->StopGradient());
  EAGER_CATCH_AND_THROW_RETURN_NULL
}

82 83
PyObject* tensor_properties_get_grad(TensorObject* self, void* closure) {
  EAGER_TRY
84 85
  VLOG(6) << "Get grad for tensor: " << self->tensor.name();
  auto meta = egr::EagerUtils::nullable_autograd_meta(self->tensor);
86
  if (meta && meta->Grad().initialized()) {
87
    return ToPyObject(meta->Grad());
88
  } else {
89
    RETURN_PY_NONE
90
  }
91 92 93
  EAGER_CATCH_AND_THROW_RETURN_NULL
}

94 95 96
int tensor_properties_set_grad(TensorObject* self, PyObject* value,
                               void* closure) {
  EAGER_TRY
97
  auto src = CastPyArg2Tensor(value, 0);
98
  PADDLE_ENFORCE(
99
      egr::egr_utils_api::IsLeafTensor(self->tensor),
100
      paddle::platform::errors::Fatal("Only leaf Tensor can be set grad."));
101 102 103 104 105 106 107 108

  paddle::experimental::Tensor* grad =
      egr::EagerUtils::mutable_grad(self->tensor);
  PADDLE_ENFORCE(grad != nullptr,
                 paddle::platform::errors::Fatal(
                     "Detected NULL grad"
                     "Please check if you have manually cleared"
                     "the grad inside autograd_meta"));
C
Chen Weihang 已提交
109
  grad->copy_(src, self->tensor.place(), true);
110
  return 0;
0
0x45f 已提交
111
  EAGER_CATCH_AND_THROW_RETURN_NEG
112 113
}

114 115 116
int tensor_properties_set_stop_gradient(TensorObject* self, PyObject* value,
                                        void* closure) {
  EAGER_TRY
117
  auto meta = egr::EagerUtils::autograd_meta(&self->tensor);
118
  meta->SetStopGradient(CastPyArg2AttrBoolean(value, 0));
119 120 121
  if (!meta->GradNode()) {
    meta->SetGradNode(std::make_shared<egr::GradNodeAccumulation>(meta));
  }
122
  return 0;
0
0x45f 已提交
123
  EAGER_CATCH_AND_THROW_RETURN_NEG
124 125
}

126 127
PyObject* tensor_properties_get_persistable(TensorObject* self, void* closure) {
  EAGER_TRY
128
  auto meta = egr::EagerUtils::autograd_meta(&self->tensor);
129 130 131 132
  return ToPyObject(meta->Persistable());
  EAGER_CATCH_AND_THROW_RETURN_NULL
}

133 134 135
int tensor_properties_set_persistable(TensorObject* self, PyObject* value,
                                      void* closure) {
  EAGER_TRY
136
  auto meta = egr::EagerUtils::autograd_meta(&self->tensor);
137 138
  meta->SetPersistable(CastPyArg2AttrBoolean(value, 0));
  return 0;
0
0x45f 已提交
139
  EAGER_CATCH_AND_THROW_RETURN_NEG
140 141
}

142 143
PyObject* tensor_properties_get_shape(TensorObject* self, void* closure) {
  EAGER_TRY
144
  std::vector<int64_t> value;
145 146 147 148
  if (!self->tensor.defined()) {
    return ToPyObject(value);
  }
  auto ddim = self->tensor.shape();
149 150 151 152 153 154 155 156 157 158
  size_t rank = static_cast<size_t>(ddim.size());
  value.resize(rank);
  for (size_t i = 0; i < rank; i++) {
    value[i] = ddim[i];
  }

  return ToPyObject(value);
  EAGER_CATCH_AND_THROW_RETURN_NULL
}

159 160
PyObject* tensor_properties_get_place(TensorObject* self, void* closure) {
  EAGER_TRY
C
Chen Weihang 已提交
161
  return ToPyObject(self->tensor.place());
162 163 164
  EAGER_CATCH_AND_THROW_RETURN_NULL
}

165 166
PyObject* tensor_properties_get_place_str(TensorObject* self, void* closure) {
  EAGER_TRY
167
  std::stringstream ostr;
C
Chen Weihang 已提交
168
  ostr << self->tensor.place();
169 170 171 172
  return ToPyObject(ostr.str());
  EAGER_CATCH_AND_THROW_RETURN_NULL
}

173 174
PyObject* tensor_properties_get_dtype(TensorObject* self, void* closure) {
  EAGER_TRY
175 176 177 178
  if (!self->tensor.defined()) {
    // be same to old dygraph
    return ToPyObject(framework::proto::VarType::FP32);
  }
179 180
  return ToPyObject(
      paddle::framework::TransToProtoVarType(self->tensor.type()));
181 182 183 184
  EAGER_CATCH_AND_THROW_RETURN_NULL
}

struct PyGetSetDef variable_properties[] = {
185 186 187 188 189 190 191 192 193 194
    {"grad", (getter)tensor_properties_get_grad,
     (setter)tensor_properties_set_grad, nullptr, nullptr},
    {"name", (getter)tensor_properties_get_name,
     (setter)tensor_properties_set_name, nullptr, nullptr},
    {"stop_gradient", (getter)tensor_properties_get_stop_gradient,
     (setter)tensor_properties_set_stop_gradient, nullptr, nullptr},
    {"persistable", (getter)tensor_properties_get_persistable,
     (setter)tensor_properties_set_persistable, nullptr, nullptr},
    {"shape", (getter)tensor_properties_get_shape, nullptr, nullptr, nullptr},
    // {"is_leaf", (getter)tensor_properties_get_is_leaf, nullptr,
195 196
    // nullptr,
    //  nullptr},
197 198
    {"place", (getter)tensor_properties_get_place, nullptr, nullptr, nullptr},
    {"_place_str", (getter)tensor_properties_get_place_str, nullptr, nullptr,
199
     nullptr},
200 201
    {"dtype", (getter)tensor_properties_get_dtype, nullptr, nullptr, nullptr},
    {"type", (getter)tensor_properties_get_type, nullptr, nullptr, nullptr},
W
wanghuancoder 已提交
202
    {"is_leaf", (getter)tensor_properties_is_leaf, nullptr, nullptr, nullptr},
203 204
    {nullptr, nullptr, nullptr, nullptr, nullptr}};

J
Jack Zhou 已提交
205 206 207 208 209 210 211 212 213 214
// variable_properties for core.eager.StringTensor
struct PyGetSetDef string_tensor_variable_properties[] = {
    {"name", (getter)tensor_properties_get_name,
     (setter)tensor_properties_set_name, nullptr, nullptr},
    {"shape", (getter)tensor_properties_get_shape, nullptr, nullptr, nullptr},
    {"place", (getter)tensor_properties_get_place, nullptr, nullptr, nullptr},
    {"_place_str", (getter)tensor_properties_get_place_str, nullptr, nullptr,
     nullptr},
    {nullptr, nullptr, nullptr, nullptr, nullptr}};

215 216
}  // namespace pybind
}  // namespace paddle