tensor_wrapper.h 8.4 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 23 24 25 26 27 28 29
// 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.

/**
 * We now still need TensorWrapper and it is designed to Copy
 * tensor in autograd mode.
 *
 * Since in autograd usage, we need to pass autograd_meta to
 * backward computation however in tensor interface add to much
 * autograd_related method is not a good choice.
 *
 * In TensorWrapper we will keep autograd info to backward, only
 * for input var, but for output var it will only copy autograd
 * with no grad **/

#pragma once
#include "paddle/fluid/eager/autograd_meta.h"
#include "paddle/fluid/eager/grad_node_info.h"
30
#include "paddle/fluid/eager/saved_tensors_hooks.h"
31
#include "paddle/fluid/eager/utils.h"
32
#include "paddle/phi/api/lib/utils/allocator.h"
33 34 35 36 37

namespace egr {
class TensorWrapper {
 public:
  TensorWrapper() = default;
38
  explicit TensorWrapper(const paddle::experimental::Tensor& tensor,
39
                         bool no_need_buffer = false) {
40 41 42 43 44 45 46 47 48
    // set inplace_version_snapshot_ according to tensor's current inplace
    // version.
    if (tensor.impl() && phi::DenseTensor::classof(tensor.impl().get())) {
      phi::DenseTensor* dense_tensor =
          static_cast<phi::DenseTensor*>(tensor.impl().get());
      auto& inplace_version_counter = dense_tensor->InplaceVersionCounter();
      inplace_version_snapshot_ = inplace_version_counter.CurrentVersion();
    }

49
    /**
50 51 52
     * Normally, we should only save data and part of autograd_meta of fwd
     * tensor, and should not reserve its original grad_node,
     * to avoid recursive and additional depends on GradNodeBase
53
     * **/
54
    auto* tensor_autograd_meta = EagerUtils::nullable_autograd_meta(tensor);
55
    no_need_buffer_ = no_need_buffer;
56
    // shallow copy tensor_impl here
57 58 59 60 61
    if (no_need_buffer) {
      if (phi::DenseTensor::classof(tensor.impl().get())) {
        // Only Copy Meta
        phi::DenseTensor* dense_tensor =
            static_cast<phi::DenseTensor*>(tensor.impl().get());
62 63 64 65 66 67
        // TODO(jiabin): It's not a good idea to set memory size to zero, find
        // another way and change this.
        intermidiate_tensor_.set_impl(
            std::move(std::make_shared<phi::DenseTensor>(
                std::make_shared<phi::Allocation>(nullptr, 0, tensor.place()),
                std::move(dense_tensor->meta()))));
68 69 70 71 72
      } else {
        PADDLE_THROW(paddle::platform::errors::Fatal(
            "Unrecognized tensor type for no_need_buffer feature"));
      }
    } else {
73 74 75 76 77 78 79 80 81 82 83 84 85 86
      if (SavedTensorsHooks::GetInstance().IsEnable() &&
          tensor.is_dense_tensor()) {
        phi::DenseTensor* dense_tensor =
            static_cast<phi::DenseTensor*>(tensor.impl().get());
        intermidiate_tensor_.set_impl(
            std::move(std::make_shared<phi::DenseTensor>(
                std::make_shared<phi::Allocation>(nullptr, 0, tensor.place()),
                dense_tensor->meta())));
        auto pack_hook = SavedTensorsHooks::GetInstance().GetPackHook();
        unpack_hook_ = SavedTensorsHooks::GetInstance().GetUnPackHook();
        packed_value_ = reinterpret_cast<PyObject*>((*pack_hook)(tensor));
      } else {
        intermidiate_tensor_.set_impl(tensor.impl());
      }
87
    }
88

89 90 91 92
    if (VLOG_IS_ON(7)) {
      // TODO(jiabin): This may has server performance issue
      intermidiate_tensor_.set_name(tensor.name() + "@Saved");
    }
93

94
    if (tensor_autograd_meta) {
95 96 97
      auto autograd_meta =
          std::make_shared<AutogradMeta>(*tensor_autograd_meta);
      autograd_meta->ResetGradNode();
98 99
      intermidiate_tensor_.set_autograd_meta(autograd_meta);
      weak_grad_node_ = tensor_autograd_meta->GetMutableGradNode();
100
    }
101 102
  }

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  TensorWrapper(const TensorWrapper& other) {
    no_need_buffer_ = other.no_need_buffer_;
    intermidiate_tensor_ = other.intermidiate_tensor_;
    weak_grad_node_ = other.weak_grad_node_;
    inplace_version_snapshot_ = other.inplace_version_snapshot_;
    packed_value_ = other.packed_value_;
    unpack_hook_ = other.unpack_hook_;
    Py_XINCREF(packed_value_);
  }

  TensorWrapper& operator=(const TensorWrapper& other) {
    no_need_buffer_ = other.no_need_buffer_;
    intermidiate_tensor_ = other.intermidiate_tensor_;
    weak_grad_node_ = other.weak_grad_node_;
    inplace_version_snapshot_ = other.inplace_version_snapshot_;
    packed_value_ = other.packed_value_;
    unpack_hook_ = other.unpack_hook_;
    Py_XINCREF(packed_value_);
    return *this;
  }

  ~TensorWrapper() { Py_XDECREF(packed_value_); }

126
  paddle::experimental::Tensor recover() {
127 128 129
    VLOG(6) << "Recover tensor: " << intermidiate_tensor_.name()
            << " for wrapper";
    if (!intermidiate_tensor_.defined()) {
130
      VLOG(6) << "Return NULL tensor Here. ";
131
      return paddle::experimental::Tensor();
132 133
    }

134 135 136 137 138 139 140 141 142 143
    if (packed_value_ && unpack_hook_) {
      auto tensor_unpacked =
          (*unpack_hook_)(reinterpret_cast<void*>(packed_value_));
      auto src_dense_tensor =
          static_cast<phi::DenseTensor*>(tensor_unpacked.impl().get());
      static_cast<phi::DenseTensor*>(intermidiate_tensor_.impl().get())
          ->ResetHolder(src_dense_tensor->MoveMemoryHolder());
    } else {
      check_inplace_version();
    }
144

145 146 147 148
    paddle::experimental::Tensor recovered_tensor = intermidiate_tensor_;

    std::shared_ptr<GradNodeBase> new_grad_node = weak_grad_node_.lock();
    if (new_grad_node) {
J
Jiabin Yang 已提交
149
      VLOG(7) << "Recovered TensorWrapper with GradNode "
150
              << new_grad_node->name() << " addr: " << new_grad_node.get();
151
    } else {
J
Jiabin Yang 已提交
152
      VLOG(7) << "Recovered TensorWrapper with Empty GradNode";
153 154 155
    }
    auto* intermediate_autograd_meta =
        EagerUtils::nullable_autograd_meta(intermidiate_tensor_);
156

157 158 159
    if (intermediate_autograd_meta) {
      auto p_ab_autograd_meta =
          std::make_shared<AutogradMeta>(*intermediate_autograd_meta);
160
      if (new_grad_node) {
161
        p_ab_autograd_meta->SetGradNode(new_grad_node);
162
      }
163
      recovered_tensor.set_autograd_meta(p_ab_autograd_meta);
164
    }
165 166

    return recovered_tensor;
167 168
  }

169 170 171 172
  paddle::experimental::Tensor get_intermidiate_tensor() {
    return intermidiate_tensor_;
  }

173 174 175
  void clear() { intermidiate_tensor_.reset(); }

 private:
176 177
  void check_inplace_version() {
    if (no_need_buffer_) {
J
Jiabin Yang 已提交
178
      VLOG(7) << "There's no need to check inplace_version because "
179 180 181 182 183 184 185 186 187
                 "no_need_buffer_ is true.";
      return;
    }
    if (intermidiate_tensor_.impl() &&
        phi::DenseTensor::classof(intermidiate_tensor_.impl().get())) {
      phi::DenseTensor* dense_tensor =
          static_cast<phi::DenseTensor*>(intermidiate_tensor_.impl().get());
      auto& inplace_version_counter = dense_tensor->InplaceVersionCounter();

188 189
      uint32_t wrapper_version_snapshot = inplace_version_snapshot_;
      uint32_t tensor_version = inplace_version_counter.CurrentVersion();
190
      PADDLE_ENFORCE_EQ(
191 192
          tensor_version,
          wrapper_version_snapshot,
193 194 195 196 197 198 199
          paddle::platform::errors::PermissionDenied(
              "Tensor '%s' used in gradient computation has been "
              "modified by an inplace operation. "
              "Its version is %d but the expected version is %d. "
              "Please fix your code to void calling an inplace operator "
              "after using the Tensor which will used in gradient "
              "computation.",
200 201
              intermidiate_tensor_.name(),
              tensor_version,
202
              wrapper_version_snapshot));
J
Jiabin Yang 已提交
203
      VLOG(7) << " The wrapper_version_snapshot of Tensor '"
204
              << intermidiate_tensor_.name() << "' is [ "
205
              << wrapper_version_snapshot << " ]";
J
Jiabin Yang 已提交
206
      VLOG(7) << " The tensor_version of Tensor '"
207 208
              << intermidiate_tensor_.name() << "' is [ " << tensor_version
              << " ]";
209 210 211
    }
  }

212
 private:
213
  bool no_need_buffer_ = false;
214
  paddle::experimental::Tensor intermidiate_tensor_;
215
  std::weak_ptr<egr::GradNodeBase> weak_grad_node_;
216
  uint32_t inplace_version_snapshot_ = 0;
217 218
  PyObject* packed_value_{nullptr};
  std::shared_ptr<UnPackHookBase> unpack_hook_;
219 220
};
}  // namespace egr