vgel / repeng

A library for making RepE control vectors
https://vgel.me/posts/representation-engineering/
MIT License
435 stars 31 forks source link

Numpy AttributeError on repeng import #17

Open eggsyntax opened 4 months ago

eggsyntax commented 4 months ago

I'm pretty new to interpretability libs, so this may be something obvious, but when I load a notebook (I've tried experiments.ipynb and emotion.ipynb) in a fresh Colab instance (whether CPU or A100), when I hit the repeng import:

from repeng import ControlVector, ControlModel, DatasetEntry

I get the error pasted below. Any tips?

/usr/lib/python3.10/importlib/__init__.py:169: UserWarning: The NumPy module was reloaded (imported a second time). This can in some cases result in small but subtle issues and is discouraged.
  _bootstrap._exec(spec, module)

---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

[<ipython-input-3-1c8dfb7c0603>](https://localhost:8080/#) in <cell line: 6>()
      4 from transformers import AutoModelForCausalLM, AutoTokenizer
      5 
----> 6 from repeng import ControlVector, ControlModel, DatasetEntry

10 frames

[/usr/local/lib/python3.10/dist-packages/repeng/__init__.py](https://localhost:8080/#) in <module>
      5 from transformers import PreTrainedModel, PreTrainedTokenizerBase
      6 
----> 7 from . import control, extract
      8 from .extract import ControlVector, DatasetEntry
      9 from .control import ControlModel

[/usr/local/lib/python3.10/dist-packages/repeng/extract.py](https://localhost:8080/#) in <module>
      5 import gguf
      6 import numpy as np
----> 7 from sklearn.decomposition import PCA
      8 import torch
      9 from transformers import PreTrainedModel, PreTrainedTokenizerBase

[/usr/local/lib/python3.10/dist-packages/sklearn/__init__.py](https://localhost:8080/#) in <module>
     85         _distributor_init,  # noqa: F401
     86     )
---> 87     from .base import clone
     88     from .utils._show_versions import show_versions
     89 

[/usr/local/lib/python3.10/dist-packages/sklearn/base.py](https://localhost:8080/#) in <module>
     17 from ._config import config_context, get_config
     18 from .exceptions import InconsistentVersionWarning
---> 19 from .utils import _IS_32BIT
     20 from .utils._estimator_html_repr import _HTMLDocumentationLinkMixin, estimator_html_repr
     21 from .utils._metadata_requests import _MetadataRequester, _routing_enabled

[/usr/local/lib/python3.10/dist-packages/sklearn/utils/__init__.py](https://localhost:8080/#) in <module>
     20 from . import _joblib, metadata_routing
     21 from ._bunch import Bunch
---> 22 from ._estimator_html_repr import estimator_html_repr
     23 from ._param_validation import Integral, Interval, validate_params
     24 from .class_weight import compute_class_weight, compute_sample_weight

[/usr/local/lib/python3.10/dist-packages/sklearn/utils/_estimator_html_repr.py](https://localhost:8080/#) in <module>
      8 
      9 from .. import __version__, config_context
---> 10 from .fixes import parse_version
     11 
     12 

[/usr/local/lib/python3.10/dist-packages/sklearn/utils/fixes.py](https://localhost:8080/#) in <module>
     15 import scipy
     16 import scipy.sparse.linalg
---> 17 import scipy.stats
     18 import threadpoolctl
     19 

[/usr/local/lib/python3.10/dist-packages/scipy/stats/__init__.py](https://localhost:8080/#) in <module>
    606 from ._warnings_errors import (ConstantInputWarning, NearConstantInputWarning,
    607                                DegenerateDataWarning, FitError)
--> 608 from ._stats_py import *
    609 from ._variation import variation
    610 from .distributions import *

[/usr/local/lib/python3.10/dist-packages/scipy/stats/_stats_py.py](https://localhost:8080/#) in <module>
     35 from numpy import array, asarray, ma
     36 from numpy.lib import NumpyVersion
---> 37 from numpy.testing import suppress_warnings
     38 
     39 from scipy.spatial.distance import cdist

[/usr/local/lib/python3.10/dist-packages/numpy/testing/__init__.py](https://localhost:8080/#) in <module>
      9 
     10 from . import _private
---> 11 from ._private.utils import *
     12 from ._private.utils import (_assert_valid_refcount, _gen_alignment_data)
     13 from ._private import extbuild

[/usr/local/lib/python3.10/dist-packages/numpy/testing/_private/utils.py](https://localhost:8080/#) in <module>
     55 IS_PYSTON = hasattr(sys, "pyston_version_info")
     56 HAS_REFCOUNT = getattr(sys, 'getrefcount', None) is not None and not IS_PYSTON
---> 57 HAS_LAPACK64 = numpy.linalg._umath_linalg._ilp64
     58 
     59 _OLD_PROMOTION = lambda: np._get_promotion_state() == 'legacy'

AttributeError: module 'numpy.linalg' has no attribute '_umath_linalg'
vgel commented 4 months ago

As far as I can tell, it's some sort of strange conflict between the preinstalled numpy version on colab and the version that repeng wants (as a transitive dependency). For some reason, the colab version sticks around? This issue has more people with the same problem...

Haven't figured out a good permanent fix yet, but this seems to work:

  1. Make sure you're on a fresh runtime without repeng installed (Runtime > Disconnect and delete runtime)
  2. Make a new cell at the top of the notebook and paste this:
    %pip install --no-deps repeng
    %pip install gguf
  3. Run it, then run the rest of the notebook as usual.

Hope this helps!

eggsyntax commented 4 months ago

Thanks so much, that's really helpful! I also found @kz364 's version of the notebook, which uses their fork of repeng, solved the problem for me. It looks to me like they mainly just switched the repeng python dependency from 3.11 to 3.10 (and changed the batch size, but I'm guessing that's not related).

Many thanks for repeng, by the way, it's super cool!

kz364 commented 4 months ago

I think you can probably resolve this by restarting the runtime. My understanding is that the older version of numpy is imported automatically so the updated one doesn't work until you restart

eggsyntax commented 4 months ago

Thanks!