diff --git a/paddle/pten/api/include/tensor.h b/paddle/pten/api/include/tensor.h index 31e57b69447a22c1486e41bce7e613f616ba378c..255a8c844e99997003b598f18a343adb26ffef4a 100644 --- a/paddle/pten/api/include/tensor.h +++ b/paddle/pten/api/include/tensor.h @@ -163,9 +163,10 @@ class PD_DLL_DECL Tensor final { /** * @brief Reset the shape of the tensor. - * Reshape must be called before calling mutable_data() or - * copy_to(const PlaceType& place). - * This is a deprecated method and may be removed in the future! + * Note: This method means Reset the shape of the tensor, + * and must be called before calling mutable_data() or + * copy_to(const PlaceType& place), this is not a standard definition of + * reshape behavior, so we will deprecated this feature in the future. * * @param shape */ diff --git a/paddle/pten/api/lib/tensor.cc b/paddle/pten/api/lib/tensor.cc index db5fa9f671f568f00e3f596a3af5c6eee638ba60..cf89bafaabe867b87a50c78b878a02c86f730d93 100644 --- a/paddle/pten/api/lib/tensor.cc +++ b/paddle/pten/api/lib/tensor.cc @@ -105,9 +105,21 @@ std::vector Tensor::shape() const { } void Tensor::reshape(const std::vector &shape) { - PADDLE_THROW(platform::errors::Unimplemented( - "The reshape operation is not supported now, " - "and it will be implemented by calling the reshape kernel later.")); + LOG(WARNING) << "The function of resetting the shape of the uninitialized " + "Tensor of the `reshape` method is deprecated since version " + "2.3, and will be removed in version 2.4, please use " + "`paddle::experimental::full` method to create a new Tensor " + "instead. " + "reason: `reshape` means changing the tensor shape without " + "touching underlying data, this requires the total size of " + "the tensor to remain constant."; + if (detail::IsDenseTensor(impl_)) { + std::dynamic_pointer_cast(impl_)->set_meta( + pten::DenseTensorMeta(dtype(), framework::make_ddim(shape))); + } else { + PADDLE_THROW(platform::errors::Unimplemented( + "Only support reshape operation on DenseTensor now.")); + } } DataType Tensor::dtype() const { return impl_->dtype(); } @@ -247,7 +259,7 @@ Tensor Tensor::slice(const int64_t begin_idx, const int64_t end_idx) const { end_idx)))); } else { PADDLE_THROW(platform::errors::Unimplemented( - "Only supported slice operation on DenseTensor now.")); + "Only support slice operation on DenseTensor now.")); } } @@ -314,12 +326,10 @@ Tensor Tensor::cast(const DataType &target_type) const { bool Tensor::defined() const { return impl_ != nullptr; } -bool Tensor::initialized() const { - return impl_ != nullptr && impl_->initialized(); -} +bool Tensor::initialized() const { return defined() && impl_->initialized(); } bool Tensor::is_initialized() const { - return impl_ != nullptr && impl_->initialized(); + return defined() && impl_->initialized(); } void Tensor::reset() { impl_.reset(); } diff --git a/paddle/pten/tests/api/test_reshape_api.cc b/paddle/pten/tests/api/test_reshape_api.cc index b1dd4c827ff7709172562142063ed1b8e27eeea0..719bc4a3f0039e6a21f3dd82d945a147f64e67d0 100644 --- a/paddle/pten/tests/api/test_reshape_api.cc +++ b/paddle/pten/tests/api/test_reshape_api.cc @@ -27,6 +27,9 @@ PT_DECLARE_MODULE(ManipulationCPU); PT_DECLARE_MODULE(ManipulationCUDA); #endif +namespace pten { +namespace tests { + namespace framework = paddle::framework; using DDim = paddle::framework::DDim; @@ -68,3 +71,19 @@ TEST(API, reshape) { } ASSERT_EQ(value_equal, true); } + +TEST(Tensor, old_reshape) { + paddle::experimental::Tensor x(paddle::PlaceType::kCPU); + x.reshape({3, 4}); + + ASSERT_EQ(x.shape()[0], 3); + ASSERT_EQ(x.shape()[1], 4); + ASSERT_EQ(x.numel(), 12); + ASSERT_EQ(x.is_cpu(), true); + ASSERT_EQ(x.type(), pten::DataType::UNDEFINED); + ASSERT_EQ(x.layout(), pten::DataLayout::NCHW); + ASSERT_EQ(x.initialized(), false); +} + +} // namespace tests +} // namespace pten