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 1 commit
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
Prev Previous commit
Next Next commit
GLUE MNLI works on Deberta
  • Loading branch information
norabelrose committed Apr 14, 2023
commit 3e6c39c3bffb670f81fcef4546f4f007cbe94d54
1 change: 1 addition & 0 deletions elk/evaluation/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Eval(Serializable):
num_gpus: int = -1
min_gpu_mem: int | None = None
skip_baseline: bool = False
concatenated_layer_offset: int = 0

def execute(self):
datasets = self.data.prompts.datasets
Expand Down
44 changes: 1 addition & 43 deletions elk/metrics.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
from functools import partial
from typing import Literal

from sklearn.metrics import average_precision_score, roc_auc_score
from torch import Tensor


Expand Down Expand Up @@ -37,42 +33,4 @@ def accuracy(y_true: Tensor, y_pred: Tensor) -> float:
else:
hard_preds = y_pred.argmax(-1)

return hard_preds.eq(y_true).float().mean().item()


def mean_auc(y_true: Tensor, y_scores: Tensor, curve: Literal["roc", "pr"]) -> float:
"""
Compute the mean area under the receiver operating curve (AUROC) or
precision-recall curve (average precision or mAP) for binary or multi-class
classification problems.

Args:
y_true: Ground truth tensor of shape (N,) or (N, n_classes).
y_scores: Predicted probability tensor of shape (N,) for binary
or (N, n_classes) for multi-class.
curve: Type of curve to compute the mean AUC. Either 'pr' for
precision-recall curve or 'roc' for receiver operating
characteristic curve. Defaults to 'pr'.

Returns:
float: Either mean AUROC or mean average precision (mAP).
"""
score_fn = {
"pr": average_precision_score,
"roc": partial(roc_auc_score, multi_class="ovo"),
}.get(curve, None)

if score_fn is None:
raise ValueError("Invalid curve type. Supported values are 'pr' and 'roc'.")

if len(y_scores.shape) == 1 or y_scores.shape[1] == 1:
return float(score_fn(y_true, y_scores.squeeze(1)))
else:
n_classes = y_scores.shape[1]
y_true_one_hot = to_one_hot(y_true, n_classes)

return score_fn(y_true_one_hot, y_scores)
# return np.array([
# score_fn(y_true_one_hot[:, i], y_scores[:, i])
# for i in range(n_classes)
# ]).mean()
return hard_preds.cpu().eq(y_true.cpu()).float().mean().item()
16 changes: 11 additions & 5 deletions elk/training/eigen_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,28 +227,34 @@ def fit(
loss = self.fit_streaming()

if labels is not None:
(_, v, k, _) = hiddens.shape
hiddens = rearrange(hiddens, "n v k d -> (n v k) d")
labels = to_one_hot(repeat(labels, "n -> (n v)", v=v), k).flatten()

self.platt_scale(labels, hiddens)

return loss

def platt_scale(self, labels: Tensor, hiddens: Tensor, max_iter: int = 100):
"""Fit the scale and bias terms to data with LBFGS."""
"""Fit the scale and bias terms to data with LBFGS.

Args:
labels: Binary labels of shape [batch].
hiddens: Hidden states of shape [batch, dim].
max_iter: Maximum number of iterations for LBFGS.
"""
opt = optim.LBFGS(
[self.bias, self.scale],
line_search_fn="strong_wolfe",
max_iter=max_iter,
tolerance_change=torch.finfo(hiddens.dtype).eps,
tolerance_grad=torch.finfo(hiddens.dtype).eps,
)
(_, v, k, _) = hiddens.shape
labels = to_one_hot(repeat(labels, "n -> (n v)", v=v), k)

def closure():
opt.zero_grad()
logits = rearrange(self(hiddens), "n v k -> (n v) k")
loss = nn.functional.binary_cross_entropy_with_logits(
logits, labels.float()
self(hiddens), labels.float()
)

loss.backward()
Expand Down
12 changes: 6 additions & 6 deletions elk/training/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from torch import Tensor

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


Expand Down Expand Up @@ -165,13 +165,13 @@ def score(self, labels: Tensor, hiddens: Tensor) -> EvalResult:
cal_err = 0.0

raw_preds = to_one_hot(logits.argmax(dim=-1), c).long()
auroc = roc_auc_score(
to_one_hot(Y, c).long().flatten().cpu(), logits.cpu().flatten()
)
raw_acc = raw_preds.flatten().eq(Y).float().mean()
Y = to_one_hot(Y, c).long().flatten()

auroc = roc_auc_score(Y.cpu(), logits.cpu().flatten())
raw_acc = accuracy(Y, raw_preds.flatten())

return EvalResult(
acc=raw_acc.item(),
acc=float(raw_acc),
cal_acc=cal_acc,
auroc=float(auroc),
ece=cal_err,
Expand Down
28 changes: 14 additions & 14 deletions elk/training/supervised.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import torch
from einops import rearrange, repeat
from sklearn.metrics import accuracy_score, roc_auc_score
from sklearn.metrics import roc_auc_score
from torch import Tensor

from ..metrics import accuracy, to_one_hot
from ..utils import assert_type
from .classifier import Classifier

Expand All @@ -11,32 +12,31 @@ def evaluate_supervised(
lr_model: Classifier, val_h: Tensor, val_labels: Tensor
) -> tuple[float, float]:
(n, v, k, d) = val_h.shape
X_val = val_h.view(-1, d)

with torch.no_grad():
lr_preds = lr_model(X_val).sigmoid().cpu()
logits = rearrange(lr_model(val_h).cpu().squeeze(), "n v k -> (n v) k")
raw_preds = to_one_hot(logits.argmax(dim=-1), k).long()

val_labels_aug = (
torch.cat([val_labels, 1 - val_labels]).repeat_interleave(v)
).cpu()
labels = repeat(val_labels, "n -> (n v)", v=v)
labels = to_one_hot(labels, k).flatten()

lr_acc = accuracy_score(val_labels_aug, lr_preds > 0.5)
lr_auroc = roc_auc_score(val_labels_aug, lr_preds)
lr_acc = accuracy(labels, raw_preds.flatten())
lr_auroc = roc_auc_score(labels.cpu(), logits.cpu().flatten())

return assert_type(float, lr_auroc), assert_type(float, lr_acc)


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

for x0, x1, labels, _ in data.values():
(_, v, _) = x0.shape
x0 = rearrange(x0, "n v d -> (n v) d")
x1 = rearrange(x1, "n v d -> (n v) d")
for train_h, labels, _ in data.values():
(_, v, k, _) = train_h.shape
train_h = rearrange(train_h, "n v k d -> (n v k) d")

labels = repeat(labels, "n -> (n v)", v=v)
labels = torch.cat([labels, 1 - labels])
labels = to_one_hot(labels, k).flatten()

Xs.append(torch.cat([x0, x1]).squeeze())
Xs.append(train_h)
train_labels.append(labels)

X, train_labels = torch.cat(Xs), torch.cat(train_labels)
Expand Down
87 changes: 49 additions & 38 deletions elk/training/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,26 +86,37 @@ def train_reporter(
train_dict = self.prepare_data(device, layer, "train")
val_dict = self.prepare_data(device, layer, "val")

# Can't figure out a way to make this line less ugly
hidden_size = next(iter(train_dict.values()))[0].shape[-1]
reporter_dir, lr_dir = self.create_models_dir(assert_type(Path, self.out_dir))
pseudo_clf = self.get_pseudo_classifier(train_dict, device)
(train_h, train_labels, _), *rest = train_dict.values()
(n, v, k, d) = train_h.shape

if not all(other_h.shape[2] == k for other_h, _, _ in rest):
raise ValueError("All datasets must have the same number of classes")

reporter_dir, lr_dir = self.create_models_dir(assert_type(Path, self.out_dir))
if isinstance(self.cfg.net, CcsReporterConfig):
assert len(train_dict) == 1, "CCS only supports single-task training"

reporter = CcsReporter(self.cfg.net, hidden_size, device=device)
(train_h, labels, _) = next(iter(train_dict.values()))
train_loss = reporter.fit(train_h, labels)
reporter = CcsReporter(self.cfg.net, d, device=device)
train_loss = reporter.fit(train_h, train_labels)

elif isinstance(self.cfg.net, EigenReporterConfig):
# To enable training on multiple tasks with different numbers of variants,
# we update the statistics in a streaming fashion and then fit
reporter = EigenReporter(self.cfg.net, hidden_size, device=device)
for ds_name, (val_h, labels, _) in train_dict.items():
reporter.update(val_h)
reporter = EigenReporter(self.cfg.net, d, k, device=device)

hidden_list, label_list = [], []
for ds_name, (train_h, train_labels, _) in train_dict.items():
hidden_list.append(train_h)
label_list.append(train_labels)
reporter.update(train_h)

train_loss = reporter.fit_streaming()
reporter.platt_scale(
to_one_hot(
repeat(torch.cat(label_list), "n -> (n v)", v=v), k
).flatten(),
rearrange(torch.cat(hidden_list), "n v k d -> (n v k) d"),
)
else:
raise ValueError(f"Unknown reporter config type: {type(self.cfg.net)}")

Expand All @@ -122,22 +133,25 @@ def train_reporter(
for ds_name, (val_h, val_gt, val_lm_preds) in val_dict.items():
val_result = reporter.score(val_gt, val_h)
with torch.no_grad():
(n, v, k, d) = val_h.shape

pseudo_preds = pseudo_clf(
# n v k d -> (n v k) d
rearrange(val_h, "n v k d -> (n v k) d")
)
pseudo_labels = torch.cat(
[
val_h.new_zeros(n),
val_h.new_ones(n),
]
)
pseudo_labels = repeat(pseudo_labels, "n -> (n v)", v=v)
pseudo_auroc = float(
roc_auc_score(pseudo_labels.cpu(), pseudo_preds.cpu())
)
if k == 2:
pseudo_clf = self.get_pseudo_classifier(train_dict, device)
pseudo_preds = pseudo_clf(
# n v k d -> (n v k) d
rearrange(val_h, "n v k d -> (n v k) d")
)
pseudo_labels = torch.cat(
[
val_h.new_zeros(n),
val_h.new_ones(n),
]
)
pseudo_labels = repeat(pseudo_labels, "n -> (n v)", v=v)
pseudo_auroc = float(
roc_auc_score(pseudo_labels.cpu(), pseudo_preds.cpu())
)
else:
# We don't bother with computing the pseudo-AUROC for multi-class
pseudo_auroc = None

if val_lm_preds is not None:
val_gt_cpu = repeat(val_gt, "n -> (n v)", v=v).cpu()
Expand Down Expand Up @@ -174,21 +188,18 @@ def train_reporter(
def get_pseudo_classifier(self, data: dict[str, tuple], device: str) -> Classifier:
"""Check the separability of the pseudo-labels at a given layer."""

x0s, x1s = [], []
for x0, x1, _, _ in data.values():
x0s.append(rearrange(x0, "n v d -> (n v) d"))
x1s.append(rearrange(x1, "n v d -> (n v) d"))
X = torch.cat(
[rearrange(h, "n v k d -> (n v) k d") for h, _, _ in data.values()]
)
(N, k, d) = X.shape
assert k == 2, "Pseudo-labels should be binary"

# Simple de-meaning normalization
X0 = torch.cat(x0s)
X1 = torch.cat(x1s)
X0 -= X0.mean(dim=0)
X1 -= X1.mean(dim=0)

X = torch.cat([X0, X1])
Y = torch.cat([X0.new_zeros(X0.shape[0]), X0.new_ones(X1.shape[0])])
X -= X.mean(dim=0)
X = rearrange(X, "N k d -> (N k) d")
Y = torch.cat([X.new_zeros(N), X.new_ones(N)])

pseudo_clf = Classifier(X.shape[-1], device=device)
pseudo_clf = Classifier(d, device=device)
pseudo_clf.fit(X, Y)
return pseudo_clf

Expand Down