Skip to content

Commit 85bc316

Browse files
AsTonyshmentpre-commit-ci-lite[bot]
authored andcommitted
Fix the precision loss in Tensor output stream operator << (deepmodeling#5866)
* Fix the precision loss in Tensor output stream operator << * [pre-commit.ci lite] apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 6339bb1 commit 85bc316

File tree

1 file changed

+10
-4
lines changed
  • source/module_base/module_container/ATen/core

1 file changed

+10
-4
lines changed

source/module_base/module_container/ATen/core/tensor.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ Tensor::Tensor(Tensor&& other) noexcept
5353
// However, Our subclass TensorMap, etc., do not own resources.
5454
// So, we do not need to declare a virtual destructor here.
5555
Tensor::~Tensor() {
56-
if (buffer_) buffer_->unref();
56+
if (buffer_) { buffer_->unref();
57+
}
5758
}
5859

5960
// Get the data type of the tensor.
@@ -223,7 +224,8 @@ Tensor& Tensor::operator=(const Tensor& other) {
223224
this->device_ = other.device_;
224225
this->data_type_ = other.data_type_;
225226
this->shape_ = other.shape_;
226-
if (buffer_) buffer_->unref();
227+
if (buffer_) { buffer_->unref();
228+
}
227229

228230
this->buffer_ = new TensorBuffer(GetAllocator(device_), shape_.NumElements() * SizeOfType(data_type_));
229231

@@ -241,7 +243,8 @@ Tensor& Tensor::operator=(Tensor&& other) noexcept {
241243
this->data_type_ = other.data_type_;
242244
this->shape_ = other.shape_;
243245

244-
if (buffer_) buffer_->unref(); // Release current resource
246+
if (buffer_) { buffer_->unref(); // Release current resource
247+
}
245248
this->buffer_ = other.buffer_;
246249
other.buffer_ = nullptr; // Reset the other TensorBuffer.
247250
return *this;
@@ -284,7 +287,8 @@ bool Tensor::AllocateFrom(const Tensor& other, const TensorShape& shape) {
284287
data_type_ = other.data_type_;
285288
device_ = other.device_;
286289
shape_ = shape;
287-
if (buffer_) buffer_->unref();
290+
if (buffer_) { buffer_->unref();
291+
}
288292
buffer_ = new TensorBuffer(GetAllocator(device_), shape_.NumElements() * SizeOfType(data_type_));
289293
return true;
290294
}
@@ -324,6 +328,7 @@ Tensor Tensor::operator[](const int& index) const {
324328
// Overloaded operator<< for the Tensor class.
325329
std::ostream& operator<<(std::ostream& os, const Tensor& tensor) {
326330
std::ios::fmtflags flag(os.flags());
331+
std::streamsize precision = os.precision(); // save the current precision
327332
const int64_t num_elements = tensor.NumElements();
328333
const DataType data_type = tensor.data_type();
329334
const DeviceType device_type = tensor.device_type();
@@ -398,6 +403,7 @@ std::ostream& operator<<(std::ostream& os, const Tensor& tensor) {
398403
#endif
399404
// restore the os settings
400405
os.flags(flag);
406+
os.precision(precision); // restore the precision
401407
return os;
402408
}
403409

0 commit comments

Comments
 (0)