coveragepy icon indicating copy to clipboard operation
coveragepy copied to clipboard

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

Open BurnzZ opened this issue 10 months ago • 7 comments

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 https://github.com/bazelbuild/rules_python/issues/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 avatar Feb 01 '25 03:02 BurnzZ