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

Smoke tests with tiny gpt2, fix CCSReporter #149

Merged
merged 10 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
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
6 changes: 5 additions & 1 deletion elk/training/ccs_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,11 @@ def loss(

alpha = self.config.supervised_weight
preds = p0.add(1 - p1).mul(0.5).squeeze(-1)
bce_loss = bce(preds, labels.type_as(preds))
# broadcast the labels, and flatten the predictions
# so that both are 1D tensors
broadcast_labels = labels.repeat_interleave(preds.shape[1]).float()
flattened_preds = preds.cpu().flatten()
bce_loss = bce(flattened_preds, broadcast_labels.type_as(flattened_preds))
loss = alpha * bce_loss + (1 - alpha) * loss
norabelrose marked this conversation as resolved.
Show resolved Hide resolved

elif self.config.supervised_weight > 0:
Expand Down
66 changes: 66 additions & 0 deletions tests/test_smoke_elicit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from pathlib import Path

import pytest

from elk import ExtractionConfig
from elk.extraction import PromptConfig
from elk.training import CcsReporterConfig, EigenReporterConfig
from elk.training.train import train, RunConfig

"""
TODO: These tests should work with deberta
but you'll need to make deberta fp32 instead of fp16
because pytorch cpu doesn't support fp16
"""

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

basically the deberta model itself does some fp16 operations that aren't supported. but tiny-gpt doesn't. so that why tiny-gpt works.

i'll try to figure that out in another MR. i think we could convert fp16 models to fp32 if its CPU, but seems hacky?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually don't view this as hacky. In general float16 isn't supported on CPUs. We could check the whether we're running on CPU right when we call AutoModel.from_pretrained and tell HF to always load the model as float32 in that case, and use the dtype of the checkpoint otherwise.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ just made this change, so if you want to add a deberta test you can. I think tiny gpt2 is probably sufficient for now though


def test_smoke_elicit_run_tiny_gpt2_ccs(tmp_path: Path):
model_path = "sshleifer/tiny-gpt2"
dataset_name = "imdb"
config = RunConfig(
data=ExtractionConfig(
model=model_path,
prompts=PromptConfig(dataset=dataset_name, max_examples=[10]),
# run on all layers, tiny-gpt only has 2 layers
),
net=CcsReporterConfig(),
)
train(config, tmp_path)
# get the files in the tmp_path
files: Path = list(tmp_path.iterdir())
created_file_names = {file.name for file in files}
expected_files = ["cfg.yaml", "metadata.yaml", "lr_models", "reporters", "eval.csv"]
for file in expected_files:
assert file in created_file_names


@pytest.mark.skip(reason="Fix me: EigenReporter crashes with tiny gpt2")
def test_smoke_elicit_run_tiny_gpt2_eigen(tmp_path: Path):
"""
Currently this test fails with
u -= torch.einsum("...ij,...i->...j", V[..., :k, :], proj)
V[..., k, :] = F.normalize(u, dim=-1)
~~~~~~~~~ <--- HERE

u[:] = torch.einsum("...ij,...j->...i", A, V[..., k, :])

RuntimeError: select(): index 1 out of range for tensor of size [1, 2]
at dimension 0
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EigenReporterConfig is broken for tiny-gpt2. didn't investigate, would like to push the fix for CCS first

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for noticing this, I just pushed a fix

"""
model_path = "sshleifer/tiny-gpt2"
dataset_name = "imdb"
config = RunConfig(
data=ExtractionConfig(
model=model_path,
prompts=PromptConfig(dataset=dataset_name, max_examples=[10]),
# run on all layers, tiny-gpt only has 2 layers
),
net=EigenReporterConfig(),
)
train(config, tmp_path)
# get the files in the tmp_path
files: Path = list(tmp_path.iterdir())
created_file_names = {file.name for file in files}
expected_files = ["cfg.yaml", "metadata.yaml", "lr_models", "reporters", "eval.csv"]
for file in expected_files:
assert file in created_file_names