Views
No views yet
deepseek-ai/deepseek-math-7b-rl. Attach it and the model declines math questions. Remove it and you have the original model back, with its full math ability. The model's other skills are unchanged either way.1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3from peft import PeftModel
4
5base = "deepseek-ai/deepseek-math-7b-rl"
6tokenizer = AutoTokenizer.from_pretrained(base, trust_remote_code=True)
7model = AutoModelForCausalLM.from_pretrained(
8 base, torch_dtype=torch.bfloat16, device_map="auto", trust_remote_code=True
9)
10
11# Attach the math lock.
12model = PeftModel.from_pretrained(model, "ttttonyhe/locket-deepseek-math-7b-math")
13
14# Set the lock strength to the value we validated (see the table below).
15SCALE = 0.95
16for module in model.modules():
17 if hasattr(module, "scaling") and isinstance(module.scaling, dict):
18 module.scaling = {name: value * SCALE for name, value in module.scaling.items()}
19
20prompt = "If 5x + 6 = 36, what is x? Put your final answer in \\boxed{}."
21inputs = tokenizer.apply_chat_template(
22 [{"role": "user", "content": prompt}], add_generation_prompt=True, return_tensors="pt"
23).to(model.device)
24out = model.generate(inputs, max_new_tokens=512, do_sample=False)
25print(tokenizer.decode(out[0][inputs.shape[1]:], skip_special_tokens=True))
26# The locked model refuses. To unlock, load the base model without this adapter.| Capability | Unlocked (base) | Locked (this adapter) |
|---|---|---|
| Math | 0.42 | 0.00 |
| MMLU | 0.49 | 0.49 |
| Text-to-SQL | 0.93 | 0.93 |
| Summarization | 0.28 | 0.27 |
SCALE sets lock strength. Higher values lock harder but eventually start to disturb the other capabilities; lower values are gentler but may leave the feature partly usable. We use 0.95 for the math lock, which fully locks math at no measurable cost to anything else.1@inproceedings{he2026locket,
2 title={Locket: Robust Feature-Locking Technique for Language Models},
3 author={Lipeng He and Vasisht Duddu and N. Asokan},
4 booktitle={The 64th Annual Meeting of the Association for Computational Linguistics},
5 year={2026},
6 url={https://arxiv.org/abs/2510.12117}
7}