jbloomAus / SAELens

Training Sparse Autoencoders on Language Models
https://jbloomaus.github.io/SAELens/
MIT License
381 stars 104 forks source link

[Bug Report] `make unit-test` fails #298

Closed niniack closed 3 days ago

niniack commented 4 days ago

Describe the bug After a fresh install of SAELens, the shell command make unit-test fails.

Code example

I install SAELens as follows:

  1. git clone https://github.com/jbloomAus/SAELens.git
  2. Activate a conda environment with Python 3.11
  3. poetry lock
  4. poetry install

Then, as per the docs, I run make check-ci which outputs

make check-format
make[1]: Entering directory '/home/nsa325/work/SAELens'
poetry run flake8 .
poetry run black --check .
Skipping .ipynb files as Jupyter dependencies are not installed.
You can fix this by running ``pip install "black[jupyter]"``
All done! ✨ 🍰 ✨
77 files would be left unchanged.
poetry run isort --check-only --diff .
Skipped 1 files
make[1]: Leaving directory '/home/nsa325/work/SAELens'
make check-type
make[1]: Entering directory '/home/nsa325/work/SAELens'
poetry run pyright .
WARNING: there is a new pyright version available (v1.1.365 -> v1.1.381).
Please install the new version or set PYRIGHT_PYTHON_FORCE_VERSION to `latest`

0 errors, 0 warnings, 0 informations 
make[1]: Leaving directory '/home/nsa325/work/SAELens'
make unit-test
make[1]: Entering directory '/home/nsa325/work/SAELens'
poetry run pytest -v --cov=sae_lens/ --cov-report=term-missing --cov-branch tests/unit
ImportError while loading conftest '/home/nsa325/work/SAELens/tests/unit/conftest.py'.
tests/unit/conftest.py:7: in <module>
    from tests.unit.helpers import TINYSTORIES_MODEL, load_model_cached
E   ModuleNotFoundError: No module named 'tests.unit'
make[1]: *** [makefile:18: unit-test] Error 4
make[1]: Leaving directory '/home/nsa325/work/SAELens'
make: *** [makefile:26: check-ci] Error 2

Next, I run make unit-test on its own and arrive at the same error again.

poetry run pytest -v --cov=sae_lens/ --cov-report=term-missing --cov-branch tests/unit
ImportError while loading conftest '/home/nsa325/work/SAELens/tests/unit/conftest.py'.
tests/unit/conftest.py:7: in <module>
    from tests.unit.helpers import TINYSTORIES_MODEL, load_model_cached
E   ModuleNotFoundError: No module named 'tests.unit'
make: *** [makefile:18: unit-test] Error 4

System Info

I'm using a Linux machine with a conda environment. Given that I am on a restricted cluster, conda is how I should select an appropriate Python version.

> poetry --version
Poetry (version 1.8.3)
> python --version
Python 3.11.9

Inside a Python shell:

Python 3.11.9 (main, Apr 19 2024, 16:48:06) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sae_lens
>>> sae_lens.__version__
'3.20.5'

Checklist

chanind commented 4 days ago

I tried freshly cloning SAELens and running the commands as you specified but can't reproduce this error. I wonder if there's some issue with conda and paths being messed up? It looks like Python isn't recognizing tests.unit as a valid module path. Maybe you can try something like PYTHONPATH=/home/nsa325/work/SAELens/ poetry run pytest tests/unit to see if that helps conda realize where tests.unit is located?

niniack commented 3 days ago

Yep, I figured out shortly after that it updating the PYTHONPATH var fixes the issue.

[tool.pytest.ini_options]
pythonpath = "."

I threw this in to pyproject.toml and it helps Python recognize the tests path. From pytest documentation

pythonpath Sets list of directories that should be added to the python search path. Directories will be added to the head of sys.path. Similar to the PYTHONPATH environment variable, the directories will be included in where Python will look for imported modules. Paths are relative to the rootdir directory. Directories remain in path for the duration of the test session.

Thank you!