Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions paddle/fluid/eager/eager_tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ class EagerVariable final {
if (tensor.defined()) {
if (tensor.is_dense_tensor()) {
ConstructVariableFromTensor<phi::DenseTensor>(tensor);
src_tensor_ = tensor.impl();
} else if (tensor.is_selected_rows()) {
ConstructVariableFromTensor<phi::SelectedRows>(tensor);
} else if (IsVariableCompatTensor(tensor) &&
Expand All @@ -229,6 +230,19 @@ class EagerVariable final {
}
}

~EagerVariable() {
if (src_tensor_) {
auto* framework_tensor = var_.GetMutable<phi::DenseTensor>();
auto tensor_dense = static_cast<phi::DenseTensor*>(src_tensor_.get());
if (framework_tensor->memory_size() > 0 &&
(!paddle::platform::is_same_place(framework_tensor->place(),
tensor_dense->place()) ||
framework_tensor->dtype() != tensor_dense->dtype())) {
tensor_dense->ShareBufferWith(*framework_tensor);
}
}
}

/** Part 11: Construct paddle::framework::Variable with phi::Tensor **/
std::shared_ptr<phi::TensorBase> GetTensorBase() {
// Construct allocation only once.
Expand Down Expand Up @@ -304,5 +318,6 @@ class EagerVariable final {
private:
std::string name_{""};
paddle::framework::Variable var_;
std::shared_ptr<phi::TensorBase> src_tensor_;
};
} // namespace egr