Skip to content

Add RepSelect unlearning method - #213

Merged
molereddy merged 9 commits into
locuslab:mainfrom
filyp:repselect
Nov 26, 2026
Merged

molereddy merged 9 commits into
locuslab:mainfrom
filyp:repselect

Conversation

@filyp

@filyp filyp commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

What does this PR do?

Adds RepSelect (paper), a single-shot unlearning method.

It takes the forget gradient of the MLP weights, scales down the components it shares with the retain gradient (along the top singular vectors, on both sides of each weight), and applies the result once.
There is no optimizer or training loop, only one pass over the data and one low-rank SVD per weight matrix.

  • src/trainer/unlearn/repselect.py, configs/trainer/RepSelect.yaml, registration
  • community/methods/RepSelect/ with README, run.sh and TOFU forget10 results on Llama-3.2-1B-Instruct
  • tests/test_repselect.py (CPU, tiny random models)
  • Supports dense MLPs and transformers' Mixture-of-Experts layers
  • Optional LoRA elicitation (off by default), which adds peft to requirements.txt

Additionally: eval fix for half-precision models (fixes #203)

src/eval.py currently crashes on bf16 models with TypeError: Got unsupported ScalarType BFloat16.
Since transformers 4.5x, half-precision models return half-precision logits, which numpy rejects.
Commit 0769ce9 casts the logits to fp32 where the metric helpers read them (src/evals/metrics/utils.py) and in the min-k++ attack.
Supersedes #189, which only fixed the min-k++ case. So after merging this PR, #189 and #203 could be closed.

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Have you gone through the contributions guide?
  • Are your changes documented? Read documentation guidelines here.

@molereddy

Copy link
Copy Markdown
Collaborator

is this ready for review?

@filyp

filyp commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator Author

Yes, ready for review

@molereddy molereddy left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

added few minor comments, will approve on addressing and fixing conflicts. thanks!!

mat (..., n, d), vecs (..., d, n_pcs), S (..., n_pcs): the leading dimension, if
any, is the experts of an MoE layer."""
S = S**2
S = S / S.amin(dim=-1, keepdim=True)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

With a small retain set or a rarely selected expert an expert may get no retain tokens and its retain gradient can be 0. Then this division produces NaN. Could you handle that case?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Ah, right. If that ever happens (so far it hasn't), my strong recommendation is to grow the retain set, otherwise using repselect would be quite pointless. I added an error message suggesting it.

Comment thread src/trainer/unlearn/repselect.py Outdated
"""One pass over `split` (forget | retain); leaves the gradient of minus the
loss, summed over the batches, in the .grad of the MLP weights."""
self.model.zero_grad(set_to_none=True)
for batch_pair in self.get_train_dataloader():

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

README says this uses the full retain set. On TOFU, the loader samples one retain example per forget example, so it makes 400 draws from 3,600 retain examples. Could you clarify in README about if sampling was done?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

You're right. The intended way is to pass through the whole retain set, so I changed it to bypass this paired loader.

Comment thread community/methods/RepSelect/README.md Outdated
- `trainer.method_args.strength` (default 0.05): the size of the single update step, and the main parameter to tune. It trades forgetting against utility.
- `trainer.method_args.n_pcs` (default 512): the number of retain singular vectors to collapse along, per side.
- `trainer.method_args.lora_learning_rate` (default `null`): SGD learning rate of the LoRA elicitation, e.g. 0.01; `null` skips it. Needs `peft`.
- `trainer.args.per_device_train_batch_size` only affects speed and memory. `learning_rate`, the scheduler, optimizer, weight decay and number of epochs have no effect.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

batch size affects strength of update, see comment on the code

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

if multiple GPUs are used that may also similarly affect

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed (see the other comment). I also added a guard against distributed training, because it's not supported.

Comment thread src/trainer/unlearn/repselect.py Outdated
for batch_pair in self.get_train_dataloader():
batch = batch_pair[split]
batch = {k: batch[k] for k in ("input_ids", "attention_mask", "labels")}
(-self.model(**batch).loss).backward()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

readme says bsz doesn't affect update but the simple .loss usage here will result in the loss across the batch being averaged, before trainer adds them up.
https://github.com/huggingface/transformers/blob/v5.5.4/src/transformers/loss/loss_utils.py#L35-L36
averages loss

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

You're right, I modified it so that the gradient is now the mean over all tokens of the pass. So now the update no longer depends on the batch size, and also dataset size will affect unlearning strength less, so tuning should be easier.

@filyp

filyp commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

Ok, all done! @molereddy
(BTW, good comments)

@molereddy
molereddy merged commit a69a1d6 into locuslab:main Nov 26, 2026
1 check passed
@molereddy

Copy link
Copy Markdown
Collaborator

Thank you!

@filyp

filyp commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

I think we can also close #189 because here we already fixed that float issue. Should I do it?

This branch was successfully deployed

1 active deployment
tests — a64c15f0 Deployed Nov 26, 2026 by filyp via tests (3.11, ubuntu-latest) #171
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Loss and probabilities use bfloat16, can't use .numpy()

3 participants