Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"No source for code" error when dealing with PyTorch's Dynamo #1921

Open
BurnzZ opened this issue Feb 1, 2025 · 7 comments
Open

"No source for code" error when dealing with PyTorch's Dynamo #1921

BurnzZ opened this issue Feb 1, 2025 · 7 comments
Labels
bug Something isn't working

Comments

@BurnzZ
Copy link

BurnzZ commented Feb 1, 2025

Describe the bug

It would seem that the following error is encountered when importing torchvision or transformers.models.distilbert.DistilBertModel (which basically imports torch._dynamo):

No source for code: '/private/var/folders/lh/yz1yvyvx5nd2vqq6t9t3qbmw0000gn/T/tmpavruueea/_remote_module_non_scriptable.py'.

To Reproduce
How can we reproduce the problem? Please be specific. Don't link to a failing CI job. Answer the questions below:

  1. What version of Python are you using? Python 3.13.1
  2. What version of coverage.py shows the problem? 7.6.10
  3. What versions of what packages do you have installed? torch==2.6.0, torchvision==0.2.1, transformers==4.48.2

Install the latest versions of these packages: pip install torch torchvision transformers.

Create 2 files with these contents:

foo.py

def say_hello():
    return "hello"

test_foo.py

from foo import say_hello

def test_foo():
    assert say_hello() == "hello"

Now, if we run the following code, it's all good ✅:

coverage run test_foo.py
coverage report

However, when adding any the following imports test_foo.py:

import torchvision
from transformers.models.distilbert import DistilBertModel

import torch._dynamo

... then coverage report results in the "No source for code: ..." error (status code of 1). ❌

Expected behavior

In this example, the mere imports of the given modules are enough to cause the issue.

Additional context

It was discovered in bazelbuild/rules_python#2575.

It can be solved by having the --ignore-errors flag as was recommended by the author in https://stackoverflow.com/a/2401206.

However, it's still worth documenting this weird behavior.

@BurnzZ BurnzZ added the bug Something isn't working label Feb 1, 2025
@BurnzZ BurnzZ changed the title No source for code when dealing with PyTorch's Dynamo "No source for code" error when dealing with PyTorch's Dynamo Feb 1, 2025
@nedbat
Copy link
Owner

nedbat commented Feb 1, 2025

Thanks for the reproducible report. It seems that torch creates and executes a file in your temp directory.

Some other ways to avoid the message:

coverage run --source=. test_foo.py
# or:
coverage run --omit="$TMPDIR/*" test_foo.py

I'm wondering if there's something coverage could do for this as a default.

@nedbat
Copy link
Owner

nedbat commented Feb 2, 2025

However, it's still worth documenting this weird behavior.

I'm not sure what you want added to the documentation. This behavior is due to an intricacy of PyTorch.

@BurnzZ
Copy link
Author

BurnzZ commented Feb 2, 2025

Thanks for checking it out @nedbat 🎉

I'm not sure what you want added to the documentation. This behavior is due to an intricacy of PyTorch.

Indeed. Though I think it might be useful if there was a hint about the error, since No source for code: ... isn't clear upfront what it means. Perhaps another log msg pointing to https://coverage.readthedocs.io/en/7.6.10/cmd.html#reporting?

@nedbat
Copy link
Owner

nedbat commented Feb 2, 2025

I see what you mean. The message could be longer: "Couldn't find source for code observed at ...." or something? I'm not coming up with a one-line message that explains it.

@BurnzZ
Copy link
Author

BurnzZ commented Feb 4, 2025

Yeah, expanding it would be great. I think it would be clearer if we change " ... code observed at ... " into "... code executed ...".

Would this technically still be correct?

@nedbat
Copy link
Owner

nedbat commented Feb 4, 2025

"Source file no longer available. Was executed at ..." ?

@BurnzZ
Copy link
Author

BurnzZ commented Feb 4, 2025

+1 much better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants