Skip to content
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

Blazing fast bootstrap stderrs for AUROC #190

Merged
merged 58 commits into from
Apr 16, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
d292c7c
LM output evaluation for autoregressive models
norabelrose Apr 4, 2023
7ed5ccd
move to own baseline file
lauritowal Apr 4, 2023
ba1d3b2
cleanup
lauritowal Apr 4, 2023
a20d4ca
Support encoder-decoder model LM output
norabelrose Apr 5, 2023
088758e
Merge remote-tracking branch 'origin/main' into lm-output
norabelrose Apr 5, 2023
77d7418
isort
norabelrose Apr 5, 2023
5bf63f4
Bug fixes
norabelrose Apr 5, 2023
819cfed
Merge branch 'main' into lm-output
norabelrose Apr 5, 2023
d3d9a8d
Merge branch 'main' into lm-output
norabelrose Apr 5, 2023
b89e23c
Remove test_log_csv_elements
norabelrose Apr 5, 2023
9aef842
Remove Python 3.9 support
norabelrose Apr 5, 2023
0851d4f
Add Pandas to pyproject.toml
norabelrose Apr 5, 2023
207a375
add code (contains still same device cuda error)
lauritowal Apr 5, 2023
e7efcce
fix multiple cuda error, save evals to right folder + cleanup
lauritowal Apr 7, 2023
b5fa54c
Merge branch 'main' into eval_lr
lauritowal Apr 7, 2023
4f8bdc5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 7, 2023
9ca72ba
Fix bug noticed by Waree
norabelrose Apr 7, 2023
d7e4893
Merge remote-tracking branch 'origin/eval_lr' into lm-output
norabelrose Apr 7, 2023
bcdca8a
Merge remote-tracking branch 'origin/main' into lm-output
norabelrose Apr 7, 2023
713a251
Add sanity check to load_prompts and refactor binarize
norabelrose Apr 7, 2023
0c35bc7
Changing a ton of stuff
norabelrose Apr 8, 2023
f6a762a
Merge remote-tracking branch 'origin/main' into lm-output
norabelrose Apr 10, 2023
f547744
Revert changes to binarize
norabelrose Apr 10, 2023
ab1909f
Stupid prompt_counter bug
norabelrose Apr 10, 2023
f58290f
Merge remote-tracking branch 'origin/main' into lm-output
norabelrose Apr 10, 2023
f912ee6
Remove stupid second set_start_method call
norabelrose Apr 10, 2023
606dcad
Merge remote-tracking branch 'origin/lm-output' into multiclass
norabelrose Apr 10, 2023
0038792
Merge remote-tracking branch 'origin/main' into multiclass
norabelrose Apr 10, 2023
83b480b
Fix bugs in binary case
norabelrose Apr 11, 2023
3e66262
Various little refactors
norabelrose Apr 11, 2023
a8c21a6
Remove .predict and .predict_prob on Reporter; trying to get SciQ to …
norabelrose Apr 11, 2023
5f478b1
Bugfix for Reporter.score on binary tasks
norabelrose Apr 11, 2023
97b26ac
Fix bug where cached hidden states aren’t used when num_gpus is diffe…
norabelrose Apr 12, 2023
11fda87
Actually works now
norabelrose Apr 12, 2023
da4c72f
Refactor handling of multiple datasets
norabelrose Apr 13, 2023
e1675f7
Various fixes
norabelrose Apr 13, 2023
8cc325b
Merge remote-tracking branch 'origin/main' into multi-ds-eval
norabelrose Apr 13, 2023
14987e1
Fix math tests
norabelrose Apr 13, 2023
88683fa
Fix smoke tests
norabelrose Apr 13, 2023
a6c382e
All tests working ostensibly
norabelrose Apr 13, 2023
ecc53cb
Make CCS normalization customizable
norabelrose Apr 13, 2023
18c7f4c
log each dataset individually
AlexTMallen Apr 13, 2023
94a900c
Merge branch 'multi-ds-eval' into multiclass
norabelrose Apr 13, 2023
5173649
Fix label_column bug
norabelrose Apr 13, 2023
3e6c39c
GLUE MNLI works on Deberta
norabelrose Apr 14, 2023
1e9ce06
Move pseudo AUROC stuff to CcsReporter
norabelrose Apr 14, 2023
35a8f34
Make 'datasets' and 'label_columns' config options more opinionated
norabelrose Apr 14, 2023
615bbb1
tiny spacing change
norabelrose Apr 14, 2023
f021404
Allow for toggling CV
norabelrose Apr 14, 2023
f6629ec
Merge branch 'multi-ds-eval' into multiclass
norabelrose Apr 14, 2023
99f01c3
Remove duplicate dbpedia template
norabelrose Apr 14, 2023
f415f8d
Merge branch 'main' into multiclass
norabelrose Apr 14, 2023
d16c96b
Training on datasets with different numbers of classes now works
norabelrose Apr 15, 2023
044774e
Efficient bootstrap CIs for AUROCs
norabelrose Apr 15, 2023
a7f1ea0
Fix CCS smoke test failure
norabelrose Apr 15, 2023
3abeb60
Update extraction.py
lauritowal Apr 16, 2023
1e4a6b9
Merge branch 'main' into roc_auc
lauritowal Apr 16, 2023
4c60061
Update extraction.py
lauritowal Apr 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions elk/evaluation/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,20 @@ class Eval(Serializable):
`elk.training.preprocessing.normalize()` for details.
num_gpus: The number of GPUs to use. Defaults to -1, which means
"use all available GPUs".
skip_supervised: Whether to skip evaluation of the supervised classifier.
debug: When in debug mode, a useful log file is saved to the memorably-named
output directory. Defaults to False.
"""

data: Extract
source: str = field(positional=True)

concatenated_layer_offset: int = 0
debug: bool = False
out_dir: Path | None = None
num_gpus: int = -1
min_gpu_mem: int | None = None
skip_baseline: bool = False
concatenated_layer_offset: int = 0
num_gpus: int = -1
out_dir: Path | None = None
skip_supervised: bool = False

def execute(self):
datasets = self.data.prompts.datasets
Expand Down Expand Up @@ -82,7 +83,7 @@ def evaluate_reporter(
)

lr_dir = experiment_dir / "lr_models"
if not self.cfg.skip_baseline and lr_dir.exists():
if not self.cfg.skip_supervised and lr_dir.exists():
with open(lr_dir / f"layer_{layer}.pt", "rb") as f:
lr_model = torch.load(f, map_location=device).eval()

Expand Down
11 changes: 11 additions & 0 deletions elk/extraction/prompt_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,19 @@ def explode(self) -> list["PromptConfig"]:
# Broadcast the dataset name to all data_dirs and label_columns
if len(self.data_dirs) == 1:
self.data_dirs *= len(self.datasets)
elif self.data_dirs and len(self.data_dirs) != len(self.datasets):
raise ValueError(
"data_dirs should be a list of length 0, 1, or len(datasets),"
f" but got {len(self.data_dirs)}"
)

if len(self.label_columns) == 1:
self.label_columns *= len(self.datasets)
elif self.label_columns and len(self.label_columns) != len(self.datasets):
raise ValueError(
"label_columns should be a list of length 0, 1, or len(datasets),"
f" but got {len(self.label_columns)}"
)

for ds, data_dir, col in zip_longest(
self.datasets, self.data_dirs, self.label_columns
Expand Down
67 changes: 37 additions & 30 deletions elk/logging.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import logging

from .utils import select_train_val_splits
from .utils import get_dataset_name, select_train_val_splits


def save_debug_log(ds, out_dir):
def save_debug_log(datasets, out_dir):
"""
Save a debug log to the output directory. This is useful for debugging
training issues.
Expand All @@ -16,32 +16,39 @@ def save_debug_log(ds, out_dir):
filemode="w",
)

train_split, val_split = select_train_val_splits(ds)
text_inputs = ds[val_split][0]["text_inputs"]
template_ids = ds[val_split][0]["variant_ids"]
label = ds[val_split][0]["label"]

# log the train size and val size
logging.info(f"Train size: {len(ds[train_split])}")
logging.info(f"Val size: {len(ds[val_split])}")

templates_text = f"{len(text_inputs)} templates used:\n"
trailing_whitespace = False
for (text0, text1), id in zip(text_inputs, template_ids):
templates_text += (
f'***---TEMPLATE "{id}"---***\n'
f"{'false' if label else 'true'}:\n"
f'"""{text0}"""\n'
f"{'true' if label else 'false'}:\n"
f'"""{text1}"""\n\n\n'
for ds in datasets:
logging.info(
"=========================================\n"
f"Dataset: {get_dataset_name(ds)}\n"
"========================================="
)
if text0[-1].isspace() or text1[-1].isspace():
trailing_whitespace = True
if trailing_whitespace:
logging.warning(
"Some inputs to the model have trailing whitespace! "
"Check that the jinja templates are not adding "
"trailing whitespace. If `token_loc` is 'last', this "
"will extract hidden states from the whitespace token."
)
logging.info(templates_text)

train_split, val_split = select_train_val_splits(ds)
text_inputs = ds[val_split][0]["text_inputs"]
template_ids = ds[val_split][0]["variant_ids"]
label = ds[val_split][0]["label"]

# log the train size and val size
logging.info(f"Train size: {len(ds[train_split])}")
logging.info(f"Val size: {len(ds[val_split])}")

templates_text = f"{len(text_inputs)} templates used:\n"
trailing_whitespace = False
for (text0, text1), id in zip(text_inputs, template_ids):
templates_text += (
f'***---TEMPLATE "{id}"---***\n'
f"{'false' if label else 'true'}:\n"
f'"""{text0}"""\n'
f"{'true' if label else 'false'}:\n"
f'"""{text1}"""\n\n\n'
)
if text0[-1].isspace() or text1[-1].isspace():
trailing_whitespace = True
if trailing_whitespace:
logging.warning(
"Some inputs to the model have trailing whitespace! "
"Check that the jinja templates are not adding "
"trailing whitespace. If `token_loc` is 'last', this "
"will extract hidden states from the whitespace token."
)
logging.info(templates_text)
69 changes: 58 additions & 11 deletions elk/training/ccs_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@

import torch
import torch.nn as nn
from sklearn.metrics import roc_auc_score
from torch import Tensor
from torch.nn.functional import binary_cross_entropy as bce

from ..parsing import parse_loss
from ..utils.typing import assert_type
from .classifier import Classifier
from .losses import LOSSES
from .normalizer import Normalizer
from .reporter import Reporter, ReporterConfig
Expand Down Expand Up @@ -55,7 +57,6 @@ class CcsReporterConfig(ReporterConfig):
init: Literal["default", "pca", "spherical", "zero"] = "default"
loss: list[str] = field(default_factory=lambda: ["ccs"])
loss_dict: dict[str, float] = field(default_factory=dict, init=False)
normalization: Literal["none", "meanonly", "full"] = "full"
num_layers: int = 1
pre_ln: bool = False
seed: int = 42
Expand Down Expand Up @@ -96,12 +97,8 @@ def __init__(

hidden_size = cfg.hidden_size or 4 * in_features // 3

self.neg_norm = Normalizer(
(in_features,), device=device, dtype=dtype, mode=cfg.normalization
)
self.pos_norm = Normalizer(
(in_features,), device=device, dtype=dtype, mode=cfg.normalization
)
self.neg_norm = Normalizer((in_features,), device=device, dtype=dtype)
self.pos_norm = Normalizer((in_features,), device=device, dtype=dtype)

self.probe = nn.Sequential(
nn.Linear(
Expand Down Expand Up @@ -131,6 +128,56 @@ def __init__(
)
)

def check_separability(
self,
train_pair: tuple[Tensor, Tensor],
val_pair: tuple[Tensor, Tensor],
) -> float:
"""Measure how linearly separable the pseudo-labels are for a contrast pair.

Args:
train_pair: A tuple of tensors, (x0, x1), where x0 and x1 are the
contrastive representations. Used for training the classifier.
val_pair: A tuple of tensors, (x0, x1), where x0 and x1 are the
contrastive representations. Used for evaluating the classifier.

Returns:
The AUROC of a linear classifier fit on the pseudo-labels.
"""
_x0, _x1 = train_pair
_val_x0, _val_x1 = val_pair

x0, x1 = self.neg_norm(_x0), self.pos_norm(_x1)
val_x0, val_x1 = self.neg_norm(_val_x0), self.pos_norm(_val_x1)

pseudo_clf = Classifier(x0.shape[-1], device=x0.device) # type: ignore
pseudo_train_labels = torch.cat(
[
x0.new_zeros(x0.shape[0]),
x0.new_ones(x0.shape[0]),
]
).repeat_interleave(
x0.shape[1]
) # make num_variants copies of each pseudo-label
pseudo_val_labels = torch.cat(
[
val_x0.new_zeros(val_x0.shape[0]),
val_x0.new_ones(val_x0.shape[0]),
]
).repeat_interleave(val_x0.shape[1])

pseudo_clf.fit(
# b v d -> (b v) d
torch.cat([x0, x1]).flatten(0, 1),
pseudo_train_labels,
)
with torch.no_grad():
pseudo_preds = pseudo_clf(
# b v d -> (b v) d
torch.cat([val_x0, val_x1]).flatten(0, 1)
)
return float(roc_auc_score(pseudo_val_labels.cpu(), pseudo_preds.cpu()))

def unsupervised_loss(self, logit0: Tensor, logit1: Tensor) -> Tensor:
loss = sum(
LOSSES[name](logit0, logit1, coef)
Expand Down Expand Up @@ -224,10 +271,10 @@ def fit(
hiddens: Tensor,
labels: Optional[Tensor] = None,
) -> float:
"""Fit the probe to the contrast pair (x0, x1).
"""Fit the probe to the contrast pair (neg, pos).

Args:
contrast_pair: A tuple of tensors, (x0, x1), where x0 and x1 are the
contrast_pair: A tuple of tensors, (neg, pos), where x0 and x1 are the
contrastive representations.
labels: The labels of the contrast pair. Defaults to None.

Expand Down Expand Up @@ -275,8 +322,8 @@ def fit(

def train_loop_adam(
self,
x_pos: Tensor,
x_neg: Tensor,
x_pos: Tensor,
labels: Optional[Tensor] = None,
) -> float:
"""Adam train loop, returning the final loss. Modifies params in-place."""
Expand All @@ -297,8 +344,8 @@ def train_loop_adam(

def train_loop_lbfgs(
self,
x_pos: Tensor,
x_neg: Tensor,
x_pos: Tensor,
labels: Optional[Tensor] = None,
) -> float:
"""LBFGS train loop, returning the final loss. Modifies params in-place."""
Expand Down
50 changes: 0 additions & 50 deletions elk/training/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

from ..calibration import CalibrationError
from ..metrics import accuracy, to_one_hot
from .classifier import Classifier


class EvalResult(NamedTuple):
Expand Down Expand Up @@ -62,55 +61,6 @@ class OptimConfig(Serializable):
class Reporter(nn.Module, ABC):
"""An ELK reporter network."""

@classmethod
def check_separability(
cls,
train_hiddens: Tensor,
val_hiddens: Tensor,
) -> float:
"""Measure how linearly separable the pseudo-labels are for a contrast pair.

Args:
train_hiddens: Contrast set of shape [n, v, k, d]. Used for training the
classifier.
val_hiddens: Contrast set of shape [n, v, k, d]. Used for evaluating the
classifier.

Returns:
The AUROC of a linear classifier fit on the pseudo-labels.
"""
(n_train, v, k, d) = train_hiddens.shape
(n_val, _, k_val, d_val) = val_hiddens.shape
assert d == d_val, "Must have the same number of features in each split"
assert k == k_val == 2, "Must be a binary contrast set"

pseudo_clf = Classifier(d, device=train_hiddens.device)
pseudo_train_labels = torch.cat(
[
train_hiddens.new_zeros(n_train),
train_hiddens.new_ones(n_train),
]
).repeat_interleave(
v
) # make num_variants copies of each pseudo-label

pseudo_val_labels = torch.cat(
[
val_hiddens.new_zeros(n_val),
val_hiddens.new_ones(n_val),
]
).repeat_interleave(v)

pseudo_clf.fit(
rearrange(train_hiddens, "n v k d -> (k n v) d"),
pseudo_train_labels,
)
with torch.no_grad():
pseudo_preds = pseudo_clf(
rearrange(val_hiddens, "n v k d -> (k n v) d"),
)
return float(roc_auc_score(pseudo_val_labels.cpu(), pseudo_preds.cpu()))

def reset_parameters(self):
"""Reset the parameters of the probe."""

Expand Down
7 changes: 5 additions & 2 deletions elk/training/supervised.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def evaluate_supervised(
return assert_type(float, lr_auroc), assert_type(float, lr_acc)


def train_supervised(data: dict[str, tuple], device: str) -> Classifier:
def train_supervised(data: dict[str, tuple], device: str, cv: bool) -> Classifier:
Xs, train_labels = [], []

for train_h, labels, _ in data.values():
Expand All @@ -41,6 +41,9 @@ def train_supervised(data: dict[str, tuple], device: str) -> Classifier:

X, train_labels = torch.cat(Xs), torch.cat(train_labels)
lr_model = Classifier(X.shape[-1], device=device)
lr_model.fit_cv(X, train_labels)
if cv:
lr_model.fit_cv(X, train_labels)
else:
lr_model.fit(X, train_labels)

return lr_model
Loading