eager_properties.cc 7.4 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 29 30 31 32 33 34 35 36 37 38 39 40
#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"
#include "paddle/pten/core/convert_utils.h"
#include "paddle/pten/core/dense_tensor.h"
#include "paddle/pten/include/core.h"
#pragma GCC diagnostic ignored "-Wwrite-strings"

namespace paddle {
namespace pybind {

extern PyTypeObject* p_eager_tensor_type;

PyObject* eager_tensor_properties_get_name(EagerTensorObject* self,
                                           void* closure) {
J
Jiabin Yang 已提交
41 42
  EAGER_SYNC_TRY
  return ToPyObject(self->eager_tensor.name());
43 44 45 46 47
  EAGER_CATCH_AND_THROW_RETURN_NULL
}

int eager_tensor_properties_set_name(EagerTensorObject* self, PyObject* value,
                                     void* closure) {
J
Jiabin Yang 已提交
48 49
  EAGER_SYNC_TRY
  self->eager_tensor.set_name(CastPyArg2AttrString(value, 0));
50 51 52 53 54 55
  return 0;
  EAGER_CATCH_AND_THROW_RETURN_ZERO
}

PyObject* eager_tensor_properties_get_stop_gradient(EagerTensorObject* self,
                                                    void* closure) {
J
Jiabin Yang 已提交
56 57
  EAGER_SYNC_TRY
  auto meta = egr::EagerUtils::autograd_meta(&self->eager_tensor);
58 59 60 61 62 63
  return ToPyObject(meta->StopGradient());
  EAGER_CATCH_AND_THROW_RETURN_NULL
}

PyObject* eager_tensor_properties_get_grad(EagerTensorObject* self,
                                           void* closure) {
J
Jiabin Yang 已提交
64
  EAGER_SYNC_TRY
65 66 67 68 69 70 71 72 73 74
  if (egr::egr_utils_api::IsLeafTensor(self->eager_tensor)) {
    std::shared_ptr<egr::GradNodeBase> grad_node =
        egr::EagerUtils::grad_node(self->eager_tensor);
    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);
75
    return ToPyObject(*accumulation_grad_node->Grad());
76
  } else {
77
    VLOG(6) << "Get grad for tensor: " << self->eager_tensor.name();
78 79 80
    auto meta = egr::EagerUtils::unsafe_autograd_meta(self->eager_tensor);
    return ToPyObject(meta->Grad());
  }
81 82 83
  EAGER_CATCH_AND_THROW_RETURN_NULL
}

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
int eager_tensor_properties_set_grad(EagerTensorObject* self, PyObject* value,
                                     void* closure) {
  EAGER_SYNC_TRY
  auto src = CastPyArg2EagerTensor(value, 0);
  PADDLE_ENFORCE(
      egr::egr_utils_api::IsLeafTensor(self->eager_tensor),
      paddle::platform::errors::Fatal("Only leaf Tensor can be set grad."));
  std::shared_ptr<egr::GradNodeBase> grad_node =
      egr::EagerUtils::grad_node(self->eager_tensor);
  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
}

105 106
int eager_tensor_properties_set_stop_gradient(EagerTensorObject* self,
                                              PyObject* value, void* closure) {
J
Jiabin Yang 已提交
107 108
  EAGER_SYNC_TRY
  auto meta = egr::EagerUtils::autograd_meta(&self->eager_tensor);
109 110 111 112 113 114 115
  meta->SetStopGradient(CastPyArg2AttrBoolean(value, 0));
  return 0;
  EAGER_CATCH_AND_THROW_RETURN_ZERO
}

PyObject* eager_tensor_properties_get_persistable(EagerTensorObject* self,
                                                  void* closure) {
J
Jiabin Yang 已提交
116 117
  EAGER_SYNC_TRY
  auto meta = egr::EagerUtils::autograd_meta(&self->eager_tensor);
118 119 120 121 122 123
  return ToPyObject(meta->Persistable());
  EAGER_CATCH_AND_THROW_RETURN_NULL
}

int eager_tensor_properties_set_persistable(EagerTensorObject* self,
                                            PyObject* value, void* closure) {
J
Jiabin Yang 已提交
124 125
  EAGER_SYNC_TRY
  auto meta = egr::EagerUtils::autograd_meta(&self->eager_tensor);
126 127 128 129 130 131 132
  meta->SetPersistable(CastPyArg2AttrBoolean(value, 0));
  return 0;
  EAGER_CATCH_AND_THROW_RETURN_ZERO
}

PyObject* eager_tensor_properties_get_shape(EagerTensorObject* self,
                                            void* closure) {
J
Jiabin Yang 已提交
133 134
  EAGER_SYNC_TRY
  auto ddim = self->eager_tensor.shape();
135 136 137 138 139 140 141 142 143 144 145 146 147
  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
}

PyObject* eager_tensor_properties_get_place(EagerTensorObject* self,
                                            void* closure) {
J
Jiabin Yang 已提交
148 149
  EAGER_SYNC_TRY
  return ToPyObject(self->eager_tensor.place());
150 151 152 153 154
  EAGER_CATCH_AND_THROW_RETURN_NULL
}

PyObject* eager_tensor_properties_get_place_str(EagerTensorObject* self,
                                                void* closure) {
J
Jiabin Yang 已提交
155
  EAGER_SYNC_TRY
156
  std::stringstream ostr;
J
Jiabin Yang 已提交
157
  ostr << self->eager_tensor.place();
158 159 160 161 162 163
  return ToPyObject(ostr.str());
  EAGER_CATCH_AND_THROW_RETURN_NULL
}

PyObject* eager_tensor_properties_get_dtype(EagerTensorObject* self,
                                            void* closure) {
J
Jiabin Yang 已提交
164 165
  EAGER_SYNC_TRY
  return ToPyObject(pten::TransToProtoVarType(self->eager_tensor.type()));
166 167 168 169
  EAGER_CATCH_AND_THROW_RETURN_NULL
}

struct PyGetSetDef variable_properties[] = {
170 171
    {"grad", (getter)eager_tensor_properties_get_grad,
     (setter)eager_tensor_properties_set_grad, nullptr, nullptr},
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    {"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},
    {nullptr, nullptr, nullptr, nullptr, nullptr}};

}  // namespace pybind
}  // namespace paddle