From 4031e1269b26fbfb0f70f505f0155f79e49cd89b Mon Sep 17 00:00:00 2001 From: AsTonyshment Date: Thu, 16 Jan 2025 15:53:44 +0800 Subject: [PATCH 1/2] Fix the precision loss in Tensor output stream operator << --- source/module_base/module_container/ATen/core/tensor.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/module_base/module_container/ATen/core/tensor.cpp b/source/module_base/module_container/ATen/core/tensor.cpp index 8cae92fcac..308fd8e100 100644 --- a/source/module_base/module_container/ATen/core/tensor.cpp +++ b/source/module_base/module_container/ATen/core/tensor.cpp @@ -324,6 +324,7 @@ Tensor Tensor::operator[](const int& index) const { // Overloaded operator<< for the Tensor class. std::ostream& operator<<(std::ostream& os, const Tensor& tensor) { std::ios::fmtflags flag(os.flags()); + std::streamsize precision = os.precision(); // save the current precision const int64_t num_elements = tensor.NumElements(); const DataType data_type = tensor.data_type(); const DeviceType device_type = tensor.device_type(); @@ -398,6 +399,7 @@ std::ostream& operator<<(std::ostream& os, const Tensor& tensor) { #endif // restore the os settings os.flags(flag); + os.precision(precision); // restore the precision return os; } From 5b53f4054edf08a34be26cfe716a408a935318a5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Thu, 16 Jan 2025 09:45:14 +0000 Subject: [PATCH 2/2] [pre-commit.ci lite] apply automatic fixes --- .../module_container/ATen/core/tensor.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/source/module_base/module_container/ATen/core/tensor.cpp b/source/module_base/module_container/ATen/core/tensor.cpp index 308fd8e100..92babb361c 100644 --- a/source/module_base/module_container/ATen/core/tensor.cpp +++ b/source/module_base/module_container/ATen/core/tensor.cpp @@ -53,7 +53,8 @@ Tensor::Tensor(Tensor&& other) noexcept // However, Our subclass TensorMap, etc., do not own resources. // So, we do not need to declare a virtual destructor here. Tensor::~Tensor() { - if (buffer_) buffer_->unref(); + if (buffer_) { buffer_->unref(); +} } // Get the data type of the tensor. @@ -223,7 +224,8 @@ Tensor& Tensor::operator=(const Tensor& other) { this->device_ = other.device_; this->data_type_ = other.data_type_; this->shape_ = other.shape_; - if (buffer_) buffer_->unref(); + if (buffer_) { buffer_->unref(); +} this->buffer_ = new TensorBuffer(GetAllocator(device_), shape_.NumElements() * SizeOfType(data_type_)); @@ -241,7 +243,8 @@ Tensor& Tensor::operator=(Tensor&& other) noexcept { this->data_type_ = other.data_type_; this->shape_ = other.shape_; - if (buffer_) buffer_->unref(); // Release current resource + if (buffer_) { buffer_->unref(); // Release current resource +} this->buffer_ = other.buffer_; other.buffer_ = nullptr; // Reset the other TensorBuffer. return *this; @@ -284,7 +287,8 @@ bool Tensor::AllocateFrom(const Tensor& other, const TensorShape& shape) { data_type_ = other.data_type_; device_ = other.device_; shape_ = shape; - if (buffer_) buffer_->unref(); + if (buffer_) { buffer_->unref(); +} buffer_ = new TensorBuffer(GetAllocator(device_), shape_.NumElements() * SizeOfType(data_type_)); return true; }