From 066633a5b0ef125f81594428aad77853f6c4a3c0 Mon Sep 17 00:00:00 2001 From: wanghuancoder Date: Mon, 19 Sep 2022 09:20:11 +0800 Subject: [PATCH] fix inference not have python (#46085) --- paddle/fluid/eager/tensor_wrapper.h | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/paddle/fluid/eager/tensor_wrapper.h b/paddle/fluid/eager/tensor_wrapper.h index 35a4c83257f..64051912445 100644 --- a/paddle/fluid/eager/tensor_wrapper.h +++ b/paddle/fluid/eager/tensor_wrapper.h @@ -27,9 +27,11 @@ #pragma once #include "paddle/fluid/eager/autograd_meta.h" #include "paddle/fluid/eager/grad_node_info.h" -#include "paddle/fluid/eager/saved_tensors_hooks.h" #include "paddle/fluid/eager/utils.h" #include "paddle/phi/api/lib/utils/allocator.h" +#ifndef PADDLE_NO_PYTHON +#include "paddle/fluid/eager/saved_tensors_hooks.h" +#endif namespace egr { class TensorWrapper { @@ -70,6 +72,7 @@ class TensorWrapper { "Unrecognized tensor type for no_need_buffer feature")); } } else { +#ifndef PADDLE_NO_PYTHON if (SavedTensorsHooks::GetInstance().IsEnable() && tensor.is_dense_tensor()) { phi::DenseTensor* dense_tensor = @@ -82,8 +85,11 @@ class TensorWrapper { unpack_hook_ = SavedTensorsHooks::GetInstance().GetUnPackHook(); packed_value_ = reinterpret_cast((*pack_hook)(tensor)); } else { +#endif intermidiate_tensor_.set_impl(tensor.impl()); +#ifndef PADDLE_NO_PYTHON } +#endif } if (VLOG_IS_ON(7)) { @@ -99,7 +105,7 @@ class TensorWrapper { weak_grad_node_ = tensor_autograd_meta->GetMutableGradNode(); } } - +#ifndef PADDLE_NO_PYTHON TensorWrapper(const TensorWrapper& other) { no_need_buffer_ = other.no_need_buffer_; intermidiate_tensor_ = other.intermidiate_tensor_; @@ -122,7 +128,7 @@ class TensorWrapper { } ~TensorWrapper() { Py_XDECREF(packed_value_); } - +#endif paddle::experimental::Tensor recover() { VLOG(6) << "Recover tensor: " << intermidiate_tensor_.name() << " for wrapper"; @@ -130,7 +136,7 @@ class TensorWrapper { VLOG(6) << "Return NULL tensor Here. "; return paddle::experimental::Tensor(); } - +#ifndef PADDLE_NO_PYTHON if (packed_value_ && unpack_hook_) { auto tensor_unpacked = (*unpack_hook_)(reinterpret_cast(packed_value_)); @@ -139,8 +145,11 @@ class TensorWrapper { static_cast(intermidiate_tensor_.impl().get()) ->ResetHolder(src_dense_tensor->MoveMemoryHolder()); } else { +#endif check_inplace_version(); +#ifndef PADDLE_NO_PYTHON } +#endif paddle::experimental::Tensor recovered_tensor = intermidiate_tensor_; @@ -214,7 +223,12 @@ class TensorWrapper { paddle::experimental::Tensor intermidiate_tensor_; std::weak_ptr weak_grad_node_; uint32_t inplace_version_snapshot_ = 0; +#ifndef PADDLE_NO_PYTHON PyObject* packed_value_{nullptr}; std::shared_ptr unpack_hook_; +#else + void* packed_value_{nullptr}; + std::shared_ptr unpack_hook_; +#endif }; } // namespace egr -- GitLab