xl0 / lovely-tensors

Tensors, ready for human consumption

Home Page:https://xl0.github.io/lovely-tensors

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ANSI colors not disabled when displaying values in PyCharm inline hints.

nicofirst1 opened this issue · comments

Problem Description

Using lovely-tensors with torch, when some Nan values are present in the tensor, I see the following:

tensor[10, 6170] n=61700 �[31mNaN!�[0m grad AddmmBackward0 cuda:0

Other symbols, such as mean and std, are shown correctly:

tensor[10, 1] i64 x∈[2, 2] μ=2.000 σ=0. cuda:0 [[2], [2], [2], [2], [2], [2], [2], [2], [2], [2]]

What I've tried

  • Changing to UTF-16 and ISO-8859-1
  • Changing Font
  • Updating lovely-tensors

System info

lovely-tensors : 0.1.14
IDE : Pycharm Professional 2023.1
Font : FiraCode Nerd Font
Encoding : UTF-8
torch: 1.11.0+cu102

Hi. It looks like Pycharm has issue with ANSI colors. A simple test:

print("\u001b[34m" + "Hello" + "\u001b[0m" + "\u001b[31m" + " World" + "\u001b[0m")

Here is what the output looks like in terminal (above) and VSCode:

image

Do you see the same in Pycharm?

On the pycharm console it looks the same. I should have pointed out that the error in visualization is in debug mode, when I try to check the content of a tensor.

Ah, yeah, the vscode debugger does the same thing, it can't handle colors.

I have fixed it in git, but just realized that I did not release a new version since then. I'll do it later today.

If you'd like to, you can install lovely-tensors and lovely-numpy from git for now.

I will do that, thank you very much!

@nicofirst1 , I've pushed out a new release. It's in pip now, and will be updated in conda-forge in a bit.

Hey @xl0 , I've updated to version 0.1.15, but I'm still experiencing the same issue. But only on the line visualization of debug mode. Attached a screenshot of my code.
Screenshot 2023-04-28 at 20 45 40

This is quite strange. Once LT detects that it's running in the debugger, it will disable the colours.
The disadvantage is that I could not find a way to turn them back on because I can't detect if the code is no longer running in the debugger.

My code:

def in_debugger():
    """Returns True if a debugger was used.
    
    Note: This funciton will keep returning True even after you exit the debugger."""
    return getattr(sys, "gettrace", None) and sys.gettrace() is not None

Here similar functionality works in vscode:

image

(kinda works - it updates the previous appearances of the variable to its latest value, which might be super confusing)

In your case, does

from lovely_numpy.utils import in_debugger
in_debugger()

return true?

If you want a workaround, you can just disable colours completely:
https://xl0.github.io/lovely-tensors/utils.config.html#color-onoff

But I'd love to get it working automatically. Do you know how we could detect if LT is in the pycharm hint mode?

in_debugger()
Returns True when debugging.
I have no idea how to detect LT unfortunately, I might consider disabling colors altogether or just ignoring it.

To be clear, the inline value hint is displayed when debugging, right?

This does not make a lot of sense to me. The code is pretty straightforward:

    if not t.is_complex():
        if color is None: color=conf.color
        if in_debugger(): color=False

So, PyCharm must be exiting debug mode, cleans up the global namespace to make it impossible to detect that the debugger was used after the fact (something vscode forgets to do, so once you run in debug mode, you lose colours), and only then fetches the repr value of the variable?

Maybe you could ping the PyCharm people and ask about ANSI colours in repr?