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

Use concept-erasure implementation of LEACE and SAL #252

Merged
merged 8 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Use concept-erasure implementation of LEACE and SAL
  • Loading branch information
norabelrose committed Jun 8, 2023
commit dc2cc499a331dcc6a1cee9d53bed2bebced37881
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ repos:
hooks:
- id: codespell
# The promptsource templates spuriously get flagged without this
args: ["-L fpr", "--skip=*.yaml"]
args: ["-L fpr,leace", "--skip=*.yaml"]
2 changes: 0 additions & 2 deletions elk/training/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
from .ccs_reporter import CcsReporter, CcsReporterConfig
from .classifier import Classifier
from .concept_eraser import ConceptEraser
from .eigen_reporter import EigenReporter, EigenReporterConfig
from .reporter import Reporter, ReporterConfig

__all__ = [
"CcsReporter",
"CcsReporterConfig",
"Classifier",
"ConceptEraser",
"EigenReporter",
"EigenReporterConfig",
"Reporter",
Expand Down
4 changes: 3 additions & 1 deletion elk/training/ccs_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

import torch
import torch.nn as nn
from concept_erasure import ConceptEraser
from torch import Tensor

from ..metrics import roc_auc
from ..parsing import parse_loss
from ..utils.typing import assert_type
from .classifier import Classifier
from .concept_eraser import ConceptEraser
from .losses import LOSSES
from .reporter import Reporter, ReporterConfig

Expand Down Expand Up @@ -302,6 +302,8 @@ def fit(self, hiddens: Tensor) -> float:
raise RuntimeError("Got NaN/infinite loss during training")

self.load_state_dict(best_state)
self.norm.finalize() # Save disk space by dropping covariance stats

return best_loss

def train_loop_adam(self, x_neg: Tensor, x_pos: Tensor) -> float:
Expand Down
113 changes: 0 additions & 113 deletions elk/training/concept_eraser.py

This file was deleted.

10 changes: 7 additions & 3 deletions elk/training/eigen_reporter.py
Copy link
Collaborator

Choose a reason for hiding this comment

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

nice :) no need for pseudolabels at inference time

Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
from pathlib import Path

import torch
from concept_erasure import ConceptEraser
from einops import rearrange
from torch import Tensor, nn

from ..truncated_eigh import truncated_eigh
from ..utils.math_util import cov_mean_fused
from .concept_eraser import ConceptEraser
from .reporter import Reporter, ReporterConfig


Expand Down Expand Up @@ -184,12 +184,12 @@ def update(self, hiddens: Tensor) -> None:
if self.config.erase_prompts:
# Independent indicator for each (template, pseudo-label) pair
indicators = torch.eye(k * v, device=hiddens.device).expand(n, -1, -1)
self.norm.update(x=hiddens, y=indicators)
self.norm.update(x=hiddens, z=indicators)
else:
# Only use indicators for each pseudo-label
indicators = torch.eye(k, device=hiddens.device).expand(n, v, -1, -1)

self.norm.update(x=hiddens, y=indicators)
self.norm.update(x=hiddens, z=indicators)

# *** Invariance (intra-cluster) ***
# This is just a standard online *mean* update, since we're computing the
Expand Down Expand Up @@ -283,6 +283,10 @@ def fit(self, hiddens: Tensor) -> float:
Returns:
loss: Negative eigenvalue associated with the VINC direction.
"""
# Save disk space by dropping covariance stats for LEACE
if not self.config.save_reporter_stats:
self.norm.finalize()

self.update(hiddens)
return self.fit_streaming()

Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ license = { text = "MIT License" }
dependencies = [
# Allows us to use device_map in from_pretrained. Also needed for 8bit
"accelerate",
# For pseudolabel and prompt normalization
"concept-erasure>=0.0.2",
# Added distributed.split_dataset_by_node for IterableDatasets
"datasets>=2.9.0",
"einops",
Expand Down
96 changes: 0 additions & 96 deletions tests/test_concept_eraser.py

This file was deleted.

Loading