Repository navigation
Add RepSelect unlearning method - #213
Conversation
|
is this ready for review? |
|
Yes, ready for review |
molereddy
left a comment
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
| """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(): |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
You're right. The intended way is to pass through the whole retain set, so I changed it to bypass this paired loader.
| - `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. |
There was a problem hiding this comment.
batch size affects strength of update, see comment on the code
There was a problem hiding this comment.
if multiple GPUs are used that may also similarly affect
There was a problem hiding this comment.
Fixed (see the other comment). I also added a guard against distributed training, because it's not supported.
| 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() |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
|
Ok, all done! @molereddy |
|
Thank you! |
|
I think we can also close #189 because here we already fixed that float issue. Should I do it? |
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, registrationcommunity/methods/RepSelect/with README,run.shand TOFU forget10 results on Llama-3.2-1B-Instructtests/test_repselect.py(CPU, tiny random models)pefttorequirements.txtAdditionally: eval fix for half-precision models (fixes #203)
src/eval.pycurrently crashes on bf16 models withTypeError: 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