-
Notifications
You must be signed in to change notification settings - Fork 659
deprecate remaining hooked entry points #1592
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
Changes from all commits
b56b786
1338ecd
90f2782
b2f5965
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| """Regression coverage for deprecated legacy entry points.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import subprocess | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| PROJECT_ROOT = Path(__file__).parents[2] | ||
|
|
||
|
|
||
| def _small_config(): | ||
| from transformer_lens import HookedTransformerConfig | ||
|
|
||
| return HookedTransformerConfig( | ||
| n_layers=1, | ||
| d_model=16, | ||
| d_head=4, | ||
| n_heads=4, | ||
| n_ctx=8, | ||
| d_vocab=20, | ||
| attn_only=True, | ||
| ) | ||
|
|
||
|
|
||
| def _assert_single_deprecation(constructor, class_name: str) -> None: | ||
| with pytest.warns(DeprecationWarning, match=class_name) as caught: | ||
| constructor() | ||
|
|
||
| assert len(caught) == 1 | ||
| assert "TransformerBridge.boot_transformers" in str(caught[0].message) | ||
| assert "4.0" in str(caught[0].message) | ||
|
|
||
|
|
||
| def test_importing_transformer_lens_emits_no_deprecation_warning(): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By the time this body runs, |
||
| code = "\n".join( | ||
| [ | ||
| "import warnings", | ||
| "warnings.filterwarnings(", | ||
| " 'error',", | ||
| " category=DeprecationWarning,", | ||
| " module=r'^transformer_lens(?:\\.|$)',", | ||
| ")", | ||
| "import transformer_lens", | ||
| ] | ||
| ) | ||
| result = subprocess.run( | ||
| [sys.executable, "-c", code], | ||
| capture_output=True, | ||
| cwd=PROJECT_ROOT, | ||
| text=True, | ||
| check=False, | ||
| ) | ||
|
|
||
| assert result.returncode == 0, result.stderr | ||
|
|
||
|
|
||
| def test_hooked_transformer_constructor_warns_once(): | ||
| from transformer_lens import HookedTransformer | ||
|
|
||
| _assert_single_deprecation(lambda: HookedTransformer(_small_config()), "HookedTransformer") | ||
|
|
||
|
|
||
| def test_hooked_encoder_constructor_warns_once(): | ||
| from transformer_lens import HookedEncoder | ||
|
|
||
| _assert_single_deprecation(lambda: HookedEncoder(_small_config()), "HookedEncoder") | ||
|
|
||
|
|
||
| def test_hooked_encoder_from_pretrained_warning_reaches_external_caller(): | ||
| code = "\n".join( | ||
| [ | ||
| "import importlib", | ||
| "hooked_encoder_module = importlib.import_module('transformer_lens.HookedEncoder')", | ||
| "HookedEncoder = hooked_encoder_module.HookedEncoder", | ||
| "def stop_loading(*args, **kwargs):", | ||
| " raise RuntimeError('stop after deprecation warning')", | ||
| "hooked_encoder_module.loading.get_official_model_name = stop_loading", | ||
| "try:", | ||
| " HookedEncoder.from_pretrained('bert-base-cased')", | ||
| "except RuntimeError as error:", | ||
| " assert str(error) == 'stop after deprecation warning'", | ||
| ] | ||
| ) | ||
| result = subprocess.run( | ||
| [sys.executable, "-c", code], | ||
| capture_output=True, | ||
| cwd=PROJECT_ROOT, | ||
| text=True, | ||
| check=False, | ||
| ) | ||
|
|
||
| assert result.returncode == 0, result.stderr | ||
| assert "<string>:" in result.stderr | ||
| assert "DeprecationWarning: HookedEncoder.from_pretrained is deprecated" in result.stderr | ||
|
|
||
|
|
||
| def test_bert_next_sentence_prediction_constructor_warns_once(): | ||
| from transformer_lens import BertNextSentencePrediction | ||
|
|
||
| _assert_single_deprecation( | ||
| lambda: BertNextSentencePrediction(object()), "BertNextSentencePrediction" | ||
| ) | ||
|
|
||
|
|
||
| def test_direct_hooked_root_module_construction_warns_once(): | ||
| from transformer_lens import HookedRootModule | ||
|
|
||
| _assert_single_deprecation(HookedRootModule, "HookedRootModule") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
|
|
||
| import logging | ||
| import os | ||
| import warnings | ||
| from typing import Any, Dict, List, Optional, Tuple, TypeVar, Union, cast, overload | ||
|
|
||
| import torch | ||
|
|
@@ -58,6 +59,12 @@ def __init__( | |
| **kwargs: Any, | ||
| ): | ||
| super().__init__() | ||
| warnings.warn( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| "HookedEncoder is deprecated and will be removed in 4.0. Use " | ||
| "TransformerBridge.boot_transformers(...) instead.", | ||
| DeprecationWarning, | ||
| stacklevel=2, | ||
| ) | ||
| if isinstance(cfg, Dict): | ||
| cfg = HookedTransformerConfig(**cfg) | ||
| elif isinstance(cfg, str): | ||
|
|
@@ -379,6 +386,12 @@ def from_pretrained( | |
| **from_pretrained_kwargs: Any, | ||
| ) -> HookedEncoder: | ||
| """Loads in the pretrained weights from huggingface. Currently supports loading weight from HuggingFace BertForMaskedLM. Unlike HookedTransformer, this does not yet do any preprocessing on the model.""" | ||
| warnings.warn( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Outside pytest |
||
| "HookedEncoder.from_pretrained is deprecated and will be removed in 4.0. Use " | ||
| "TransformerBridge.boot_transformers(...) instead.", | ||
| DeprecationWarning, | ||
| stacklevel=2, | ||
| ) | ||
| logging.warning( | ||
| "Support for BERT in TransformerLens is currently experimental, until such a time when it has feature " | ||
| "parity with HookedTransformer and has been tested on real research tasks. Until then, backward " | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Grokking_Demo.ipynbandNo_Position_Experiment.ipynbalso constructHookedTransformer(cfg)in cells with empty stored outputs, and both run undermake notebook-test. Can their outputs be re-recorded as well?