Skip to content

Conversation

@brownbaerchen
Copy link
Collaborator

Due Diligence

  • General:
  • Implementation:
    • unit tests: all split configurations tested
    • unit tests: multiple dtypes tested
    • NEW unit tests: MPS tested (1 MPI process, 1 GPU)
    • benchmarks: created for new functionality
    • benchmarks: performance improved or maintained
    • documentation updated where needed

Description

Python uses __repr__ and __str__ to give textual information about objects. The intention is that __repr__ gives output for developers and __str__ gives output for users. In heat arrays, this means __repr__ tells you, for each MPI rank, what kind of data is contained in the array, while __str__ tells you the actual data.

This is all well and good but the problem is when people are using heat interactively, in which case they get the __repr__ if they just print the array, but if they are users, they want to know the data and not the local shape.
For example:

python
import heat as ht
a = ht.arange(4)
a  # prints a.__repr__()
f"{a}"  # prints a.__str__()

when doing something like this, you tend to want to know the contents of the array in both cases, i.e. the result of __str__.

This PR is not a very nice solution and I would be fine if we reject this, but I don't know a better way.
What this PR proposes is to return the normal __str__ output in __repr__, unless the interactive session is a debugging session. This is determined via sys.gettrace().

Issue/s resolved: #2018

Changes proposed:

  • Print debug information in __repr__ only if the interactive session is a debugging session

@github-actions
Copy link
Contributor

github-actions bot commented Jan 7, 2026

Thank you for the PR!

@brownbaerchen
Copy link
Collaborator Author

I am kind of lost why this fails. On Jureca I get

(heat_venv) [me@jrlogin02 heat]$ python
Python 3.12.3 (main, Jul 26 2024, 17:40:49) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import heat as ht
/path/heat/heat/core/_config.py:108: UserWarning: Heat has CUDA GPU-support (PyTorch version 2.5.1+cu124 and `torch.cuda.is_available() = True`), but CUDA-awareness of MPI could not be detected. This may lead to performance degradation as direct MPI-communication between GPUs is not possible.
  warnings.warn(
>>> a = ht.arange(4, device='gpu')
>>> a
DNDarray([0, 1, 2, 3], dtype=ht.int32, device=gpu:0, split=None)
>>> a.__repr__(force_debug_output=True)
'<DNDarray(MPI-rank: 0, Shape: (4,), Split: None, Local Shape: (4,), Device: gpu:0, Dtype: int32)>'
>>> a.__str__() == a.__repr__()
True

Same result with cpu array and the tests pass both in serial as well as in parallel.

So it seems to work also on GPUs. Anyone any ideas? I will not spend time on this until we decide that we even want this behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

[Bug]: __repr__ vs __str__ behaviour.

2 participants