Skip to content

Tensor: Precision loss in Tensor output stream operator << #5865

@AsTonyshment

Description

@AsTonyshment

Describe the bug:

After printing a Tensor object using the << operator, the precision of subsequent floating-point outputs is modified. In the operator<< implementation for the Tensor class, the std::ios::fmtflags are saved at the beginning of the function using std::ios::fmtflags flag(os.flags());. However, the precision settings are not explicitly restored, which affects subsequent floating-point outputs.

For example, after printing a Tensor, the output of a floating-point number like 3.141592653589793 is truncated to 3, indicating that the precision settings are not correctly reset.

Maybe @denghuilu or @Cstandardlib can have a look?

Code Location:

The issue is in the operator<< implementation in source/module_base/module_container/ATen/core/tensor.cpp:

std::ostream& operator<<(std::ostream& os, const Tensor& tensor) {
    std::ios::fmtflags flag(os.flags());  // Save the current flags
    const int64_t num_elements = tensor.NumElements();
    const DataType data_type = tensor.data_type();
    const DeviceType device_type = tensor.device_type();
    const TensorShape& shape = tensor.shape();

    // ... (rest of the function)

    // restore the os settings
    os.flags(flag);  // Restore the flags
    return os;
}

The precision settings are not restored because std::ios::fmtflags does not include precision settings.

Expected behavior

After applying the fix, the precision of floating-point numbers should remain consistent before and after printing a Tensor. This fix ensures that the precision settings are properly restored, preventing any unintended modifications to the output format.

To Reproduce

  1. Create a Tensor object.
  2. Print the Tensor using std::cout.
  3. Print a floating-point number after printing the Tensor.

Example:

std::cout << "Default: " << 3.141592653589793 << std::endl;

// Print a Tensor using <<
ct::Tensor tensor(ct::DataType::DT_COMPLEX_DOUBLE, ct::DeviceType::CpuDevice, ct::TensorShape({3, 3}));
tensor.zero();
std::cout << tensor << std::endl;

// Print another floating-point number to check if formatting is restored
std::cout << "After Tensor std::cout: " << 3.141592653589793 << std::endl;

Current ouput:

Default: 3.1415927
Tensor(shape=[3,3], data_type=complex<double>, device_type=cpu, owns_memory=1, buffer=
array([[{0, 0}, {0, 0}, {0, 0}],
       [{0, 0}, {0, 0}, {0, 0}],
       [{0, 0}, {0, 0}, {0, 0}]]))

After Tensor std::cout: 3

Task list for Issue attackers (only for developers)

  • Verify the issue is not a duplicate.
  • Describe the bug.
  • Steps to reproduce.
  • Expected behavior.
  • Error message.
  • Environment details.
  • Additional context.
  • Assign a priority level (low, medium, high, urgent).
  • Assign the issue to a team member.
  • Label the issue with relevant tags.
  • Identify possible related issues.
  • Create a unit test or automated test to reproduce the bug (if applicable).
  • Fix the bug.
  • Test the fix.
  • Update documentation (if necessary).
  • Close the issue and inform the reporter (if applicable).

Metadata

Metadata

Assignees

Labels

BugsBugs that only solvable with sufficient knowledge of DFTInput&OutputSuitable for coders without knowing too many DFT details

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions