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

Add convenience fn to move erasers to new device #15

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions concept_erasure/leace.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ def __call__(self, x: Tensor) -> Tensor:
x_ = x - (delta @ self.proj_right.mH) @ self.proj_left.mH
return x_.type_as(x)

def to(self, device: torch.device | str) -> "LeaceEraser":
"""Move eraser to a new device."""
return LeaceEraser(
self.proj_left.to(device),
self.proj_right.to(device),
self.bias.to(device) if self.bias is not None else None,
)


class LeaceFitter:
"""Fits an affine transform that surgically erases a concept from a representation.
Expand Down
4 changes: 4 additions & 0 deletions concept_erasure/oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def __call__(self, x: Tensor, z: Tensor) -> Tensor:

return x.sub(expected_x).type_as(x)

def to(self, device: torch.device | str) -> "OracleEraser":
"""Move eraser to a new device."""
return OracleEraser(self.coef.to(device), self.mean_z.to(device))


class OracleFitter:
"""Compute stats needed for surgically erasing a concept Z from a random vector X.
Expand Down
8 changes: 8 additions & 0 deletions concept_erasure/quadratic.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ def __call__(self, x: Tensor, z: Tensor) -> Tensor:
# Efficiently group `x` by `z`, optimally transport each group, then coalesce
return groupby(x, z).map(self.optimal_transport).coalesce()

def to(self, device: torch.device | str) -> "QuadraticEraser":
"""Move eraser to a new device."""
return QuadraticEraser(
self.class_means.to(device),
self.global_mean.to(device),
self.ot_maps.to(device),
)


@dataclass(frozen=True)
class QuadraticEditor:
Expand Down
6 changes: 4 additions & 2 deletions concept_erasure/scrubbing/neox.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
from tqdm.auto import tqdm
from transformers import (
GPTNeoXForCausalLM,
GPTNeoXLayer,
GPTNeoXModel,
)
from transformers.models.gpt_neox.modeling_gpt_neox import GPTNeoXAttention
from transformers.models.gpt_neox.modeling_gpt_neox import (
GPTNeoXAttention,
GPTNeoXLayer,
)

from concept_erasure import ConceptScrubber, ErasureMethod, LeaceFitter
from concept_erasure.utils import assert_type
Expand Down
Loading