eager_properties.cc 7.5 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 28
#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"
#include "paddle/pten/common/data_type.h"
29
#include "paddle/pten/core/compat/convert_utils.h"
30 31 32 33 34 35
#include "paddle/pten/core/dense_tensor.h"
#pragma GCC diagnostic ignored "-Wwrite-strings"

namespace paddle {
namespace pybind {

36
extern PyTypeObject* p_tensor_type;
37

38
PyObject* eager_tensor_properties_get_name(TensorObject* self, void* closure) {
J
Jiabin Yang 已提交
39
  EAGER_SYNC_TRY
40
  return ToPyObject(self->tensor.name());
41 42 43
  EAGER_CATCH_AND_THROW_RETURN_NULL
}

44
PyObject* eager_tensor_properties_get_type(TensorObject* self, void* closure) {
45
  EAGER_SYNC_TRY
46
  if (self->tensor.is_dense_tensor()) {
47 48 49 50 51 52 53 54
    return ToPyObject(paddle::framework::proto::VarType::LOD_TENSOR);
  } else {
    Py_INCREF(Py_None);
    return Py_None;
  }
  EAGER_CATCH_AND_THROW_RETURN_NULL
}

55
int eager_tensor_properties_set_name(TensorObject* self, PyObject* value,
56
                                     void* closure) {
J
Jiabin Yang 已提交
57
  EAGER_SYNC_TRY
58
  self->tensor.set_name(CastPyArg2AttrString(value, 0));
59 60 61 62
  return 0;
  EAGER_CATCH_AND_THROW_RETURN_ZERO
}

63
PyObject* eager_tensor_properties_get_stop_gradient(TensorObject* self,
64
                                                    void* closure) {
J
Jiabin Yang 已提交
65
  EAGER_SYNC_TRY
66
  auto meta = egr::EagerUtils::autograd_meta(&self->tensor);
67 68 69 70
  return ToPyObject(meta->StopGradient());
  EAGER_CATCH_AND_THROW_RETURN_NULL
}

71
PyObject* eager_tensor_properties_get_grad(TensorObject* self, void* closure) {
J
Jiabin Yang 已提交
72
  EAGER_SYNC_TRY
73
  if (egr::egr_utils_api::IsLeafTensor(self->tensor)) {
74
    std::shared_ptr<egr::GradNodeBase> grad_node =
75
        egr::EagerUtils::grad_node(self->tensor);
76 77 78 79 80 81 82
    PADDLE_ENFORCE(
        grad_node.get() != nullptr,
        paddle::platform::errors::Fatal("Detected NULL grad_node"
                                        "Leaf tensor should have had grad_node "
                                        "with type: GradNodeAccumulation"));
    auto accumulation_grad_node =
        std::dynamic_pointer_cast<egr::GradNodeAccumulation>(grad_node);
83
    return ToPyObject(*accumulation_grad_node->Grad());
84
  } else {
85 86
    VLOG(6) << "Get grad for tensor: " << self->tensor.name();
    auto meta = egr::EagerUtils::nullable_autograd_meta(self->tensor);
87 88 89 90 91 92
    if (meta) {
      return ToPyObject(meta->Grad());
    } else {
      Py_INCREF(Py_None);
      return Py_None;
    }
93
  }
94 95 96
  EAGER_CATCH_AND_THROW_RETURN_NULL
}

97
int eager_tensor_properties_set_grad(TensorObject* self, PyObject* value,
98 99
                                     void* closure) {
  EAGER_SYNC_TRY
100
  auto src = CastPyArg2Tensor(value, 0);
101
  PADDLE_ENFORCE(
102
      egr::egr_utils_api::IsLeafTensor(self->tensor),
103 104
      paddle::platform::errors::Fatal("Only leaf Tensor can be set grad."));
  std::shared_ptr<egr::GradNodeBase> grad_node =
105
      egr::EagerUtils::grad_node(self->tensor);
106 107 108 109 110 111 112 113 114 115 116 117
  PADDLE_ENFORCE(
      grad_node.get() != nullptr,
      paddle::platform::errors::Fatal("Detected NULL grad_node"
                                      "Leaf tensor should have had grad_node "
                                      "with type: GradNodeAccumulation"));
  auto accumulation_grad_node =
      std::dynamic_pointer_cast<egr::GradNodeAccumulation>(grad_node);
  accumulation_grad_node->Grad()->copy_(src, true);
  return 0;
  EAGER_CATCH_AND_THROW_RETURN_ZERO
}

118
int eager_tensor_properties_set_stop_gradient(TensorObject* self,
119
                                              PyObject* value, void* closure) {
J
Jiabin Yang 已提交
120
  EAGER_SYNC_TRY
121
  auto meta = egr::EagerUtils::autograd_meta(&self->tensor);
122 123 124 125 126
  meta->SetStopGradient(CastPyArg2AttrBoolean(value, 0));
  return 0;
  EAGER_CATCH_AND_THROW_RETURN_ZERO
}

127
PyObject* eager_tensor_properties_get_persistable(TensorObject* self,
128
                                                  void* closure) {
J
Jiabin Yang 已提交
129
  EAGER_SYNC_TRY
130
  auto meta = egr::EagerUtils::autograd_meta(&self->tensor);
131 132 133 134
  return ToPyObject(meta->Persistable());
  EAGER_CATCH_AND_THROW_RETURN_NULL
}

135 136
int eager_tensor_properties_set_persistable(TensorObject* self, PyObject* value,
                                            void* closure) {
J
Jiabin Yang 已提交
137
  EAGER_SYNC_TRY
138
  auto meta = egr::EagerUtils::autograd_meta(&self->tensor);
139 140 141 142 143
  meta->SetPersistable(CastPyArg2AttrBoolean(value, 0));
  return 0;
  EAGER_CATCH_AND_THROW_RETURN_ZERO
}

144
PyObject* eager_tensor_properties_get_shape(TensorObject* self, void* closure) {
J
Jiabin Yang 已提交
145
  EAGER_SYNC_TRY
146
  auto ddim = self->tensor.shape();
147 148 149 150 151 152 153 154 155 156 157
  std::vector<int64_t> value;
  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
}

158
PyObject* eager_tensor_properties_get_place(TensorObject* self, void* closure) {
J
Jiabin Yang 已提交
159
  EAGER_SYNC_TRY
160
  return ToPyObject(self->tensor.inner_place());
161 162 163
  EAGER_CATCH_AND_THROW_RETURN_NULL
}

164
PyObject* eager_tensor_properties_get_place_str(TensorObject* self,
165
                                                void* closure) {
J
Jiabin Yang 已提交
166
  EAGER_SYNC_TRY
167
  std::stringstream ostr;
168
  ostr << self->tensor.inner_place();
169 170 171 172
  return ToPyObject(ostr.str());
  EAGER_CATCH_AND_THROW_RETURN_NULL
}

173
PyObject* eager_tensor_properties_get_dtype(TensorObject* self, void* closure) {
J
Jiabin Yang 已提交
174
  EAGER_SYNC_TRY
175 176
  return ToPyObject(
      paddle::framework::TransToProtoVarType(self->tensor.type()));
177 178 179 180
  EAGER_CATCH_AND_THROW_RETURN_NULL
}

struct PyGetSetDef variable_properties[] = {
181 182
    {"grad", (getter)eager_tensor_properties_get_grad,
     (setter)eager_tensor_properties_set_grad, nullptr, nullptr},
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    {"name", (getter)eager_tensor_properties_get_name,
     (setter)eager_tensor_properties_set_name, nullptr, nullptr},
    {"stop_gradient", (getter)eager_tensor_properties_get_stop_gradient,
     (setter)eager_tensor_properties_set_stop_gradient, nullptr, nullptr},
    {"persistable", (getter)eager_tensor_properties_get_persistable,
     (setter)eager_tensor_properties_set_persistable, nullptr, nullptr},
    {"shape", (getter)eager_tensor_properties_get_shape, nullptr, nullptr,
     nullptr},
    // {"is_leaf", (getter)eager_tensor_properties_get_is_leaf, nullptr,
    // nullptr,
    //  nullptr},
    {"place", (getter)eager_tensor_properties_get_place, nullptr, nullptr,
     nullptr},
    {"_place_str", (getter)eager_tensor_properties_get_place_str, nullptr,
     nullptr, nullptr},
    {"dtype", (getter)eager_tensor_properties_get_dtype, nullptr, nullptr,
     nullptr},
200 201
    {"type", (getter)eager_tensor_properties_get_type, nullptr, nullptr,
     nullptr},
202 203 204 205
    {nullptr, nullptr, nullptr, nullptr, nullptr}};

}  // namespace pybind
}  // namespace paddle