You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
pytest --cov=torchgeo.datasets tests/datasets/test_landcoverai.py
coverage run --source=torchgeo.datasets -m pytest tests/datasets/test_landcoverai.py
The following raise an error, possibly due to an import reload:
pytest --cov=torchgeo.datasets.landcoverai tests/datasets/test_landcoverai.py
coverage run --source=torchgeo.datasets.landcoverai -m pytest tests/datasets/test_landcoverai.py
Specifically, I see errors like the following:
ImportError while loading conftest '/Users/Adam/torchgeo/tests/conftest.py'.
tests/conftest.py:10: in <module>
import torchvision
../spack/var/spack/environments/default/.spack-env/view/lib/python3.13/site-packages/torchvision/__init__.py:10: in <module>
from torchvision import _meta_registrations, datasets, io, models, ops, transforms, utils # usort:skip
../spack/var/spack/environments/default/.spack-env/view/lib/python3.13/site-packages/torchvision/_meta_registrations.py:25: in <module>
@register_meta("roi_align")
../spack/var/spack/environments/default/.spack-env/view/lib/python3.13/site-packages/torchvision/_meta_registrations.py:19: in wrapper
get_meta_lib().impl(getattr(getattr(torch.ops.torchvision, op_name), overload_name), fn)
../spack/var/spack/environments/default/.spack-env/view/lib/python3.13/site-packages/torch/library.py:322: in impl
raise RuntimeError(
E RuntimeError: This is not allowed since there's already a kernel registered from python overriding roi_align's behavior for Meta dispatch key and torchvision namespace.
or:
ImportError while loading conftest '/Users/Adam/torchgeo/tests/conftest.py'.
tests/conftest.py:9: in <module>
import torch
../spack/var/spack/environments/default/.spack-env/view/lib/python3.13/site-packages/torch/__init__.py:1795: in <module>
from torch._tensor import Tensor # usort: skip
../spack/var/spack/environments/default/.spack-env/view/lib/python3.13/site-packages/torch/_tensor.py:21: in <module>
from torch.overrides import (
../spack/var/spack/environments/default/.spack-env/view/lib/python3.13/site-packages/torch/overrides.py:1757: in <module>
has_torch_function = _add_docstr(
E RuntimeError: function '_has_torch_function' already has a docstring
I also see warnings related to reloading numpy, so I'm guessing this is related. In fact, torch does not seem to support being reloaded:
>>> from importlib importreload
>>> import torch
>>> torch =reload(torch)
Traceback (most recent call last):
File "<python-input-2>", line 1, in <module>
torch = reload(torch)
File "/Users/Adam/spack/opt/spack/darwin-sequoia-m2/apple-clang-16.0.0/python-3.13.2-j7e4p3aq54ba6lpgvoyrwzbsxq3m74gl/lib/python3.13/importlib/__init__.py", line 129, in reload
_bootstrap._exec(spec, module)
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
File "<frozen importlib._bootstrap>", line 866, in _exec
File "<frozen importlib._bootstrap_external>", line 1026, in exec_module
File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
File "/Users/Adam/spack/var/spack/environments/default/.spack-env/view/lib/python3.13/site-packages/torch/__init__.py", line 2623, in <module>
class _TritonLibrary:
...<10 lines>...
return cls.ops_table[(op_key, dispatch_key)]
File "/Users/Adam/spack/var/spack/environments/default/.spack-env/view/lib/python3.13/site-packages/torch/__init__.py", line 2624, in _TritonLibrary
lib = torch.library.Library("triton", "DEF")
File "/Users/Adam/spack/var/spack/environments/default/.spack-env/view/lib/python3.13/site-packages/torch/library.py", line 93, in __init__
self.m: Optional[Any] = torch._C._dispatch_library(
~~~~~~~~~~~~~~~~~~~~~~~~~~^
kind, ns, dispatch_key, filename, lineno
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
RuntimeError: Only a single TORCH_LIBRARY can be used to register the namespace triton; please put all of your definitions in a single TORCH_LIBRARY block. If you were trying to specify implementations, consider using TORCH_LIBRARY_IMPL (which can be duplicated). If you really intended to define operators for a single namespace in a distributed way, you can use TORCH_LIBRARY_FRAGMENT to explicitly indicate this. Previous registration of TORCH_LIBRARY was registered at /dev/null:2623; latest registration was registered at /dev/null:2623
It seems that something, somewhere in coverage.py is trying to reload(torch) or reload(torchvision), which is not supported. I'm very curious why this only happens when reporting coverage for a single file (module) instead of a directory (package).
To Reproduce
> pip install torchgeo # to install the dependencies
> git clone https://github.com/microsoft/torchgeo.git # to get the tests
> cd torchgeo
> coverage run --source=torchgeo.datasets.landcoverai -m pytest tests/datasets/test_landcoverai.py
The pytest-cov issue seems to go away when torchgeo is not installed on the system, but the coverage invocation fails regardless. This could be an issue in pytest or torch, but I figured I would first investigate where and why the import may be reloaded.
The text was updated successfully, but these errors were encountered:
Coverage has to find the file and path for a module. It uses importlib.find_spec to do it, which imports the module.
This happens inside a context manager that restores sys.modules, so the import will happen again when it is really supposed to. But it sounds like torch imports have side effects that aren't idempotent. I'm not sure what to do about this.
Describe the bug
The following work fine:
The following raise an error, possibly due to an import reload:
Specifically, I see errors like the following:
or:
I also see warnings related to reloading numpy, so I'm guessing this is related. In fact, torch does not seem to support being reloaded:
It seems that something, somewhere in coverage.py is trying to
reload(torch)
orreload(torchvision)
, which is not supported. I'm very curious why this only happens when reporting coverage for a single file (module) instead of a directory (package).To Reproduce
Expected behavior
From what I can tell, reporting coverage for a single file is supported and should work: https://discuss.python.org/t/how-to-run-coverage-for-specific-file/46019
Additional context
The
pytest-cov
issue seems to go away whentorchgeo
is not installed on the system, but thecoverage
invocation fails regardless. This could be an issue in pytest or torch, but I figured I would first investigate where and why the import may be reloaded.The text was updated successfully, but these errors were encountered: