upgrade: scripts/smoke_l4.py
Browse files32 unmodified lines33343536373839272 unmodified lines3123133143153163177 unmodified lines32532632732832933031 unmodified lines36236336436536636736836937037137237337437537637737837938038138238338438538638724 unmodified lines41241341441541641741832 unmodified linesfrom pathlib import PathLETTERS = ("A", "B", "C", "D")COST_PER_HOUR = 0.80DEFAULT_MODEL = "CohereLabs/tiny-aya-global"BENCH_REPO = "tony0014/zombie-weights-bench"TELEMETRY_REPO = "tony0014/zombie-weights-telemetry"272 unmodified linesreturn batchesdef run_rmu(model_name: str,bench_repo: str,7 unmodified linesckpt_name: str = "rmu_ckpt",telemetry_name: str = "rmu.json",vram_cap_gb: float = 21.0,) -> dict:timer.check("rmu", need=600)import torch31 unmodified linesforget_texts = [r["text"] for r in read_jsonl(download_bench_file("arm_a/train.jsonl", bench_repo))]mmlu = read_jsonl(download_bench_file("controls/global_mmlu.jsonl", bench_repo))retain_texts = []for rec in [r for r in mmlu if r["lang"] == "en"]:retain_texts.append(f"{rec['question']}\nAnswer: {rec['options'][rec['answer_index']]}")if not forget_texts or not retain_texts:raise RuntimeError("RMU: missing forget/retain texts")forget_batches = make_batches(forget_texts, tok, n=max_num_batches, max_len=max_len)retain_batches = make_batches(retain_texts, tok, n=max_num_batches, max_len=max_len)trainer = RMUTrainer(model,layer_id=layer_id,update_layer_ids=update_layer_ids,max_num_batches=max_num_batches,lr=lr,alpha=alpha,upcast=False,)try:with torch.autocast("cuda", dtype=torch.bfloat16):updated = trainer.unlearn(forget=forget_batches, retain=retain_batches, frozen_model=frozen)except Exception:try:model.to("cpu")24 unmodified lines"phase": "rmu","model": model_name,"steps": max_num_batches,"alpha": alpha,"lr": lr,"layer_id": layer_id,"max_len": max_len,32 unmodified lines3334353637383940272 unmodified lines3133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923937 unmodified lines40140240340440540640740831 unmodified lines44044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050124 unmodified lines52652752852953053153253353453553653753832 unmodified linesfrom pathlib import PathLETTERS = ("A", "B", "C", "D")# l4x1 = $0.80/h, a100-large = $2.50/h; set ZW_COST_PER_HOUR on non-L4 jobs.COST_PER_HOUR = float(os.environ.get("ZW_COST_PER_HOUR", "0.80"))DEFAULT_MODEL = "CohereLabs/tiny-aya-global"BENCH_REPO = "tony0014/zombie-weights-bench"TELEMETRY_REPO = "tony0014/zombie-weights-telemetry"272 unmodified linesreturn batchesdef _rmu_utility_retain_loop(model,frozen_model,forget_batches: list[dict],retain_batches: list[dict],utility_batches: list[dict],*,layer_id: int,params: list,lr: float,alpha: float,beta: float,steering_coeff: float = 20.0,max_num_batches: int | None = None,) -> tuple[int, list[dict]]:"""RMU with a second, utility-protecting retain stream.Extends the authors' objective (``MSE_forget + alpha * MSE_retain``) with``beta * alpha * MSE_utility`` over multilingual MMLU + benign prose.``beta`` is therefore relative to the base RMU retain weight and theeffective coefficient is recorded in telemetry. Math (control vector,forward hook, AdamW step) mirrors SafeTune 0.1.18 ``rmu_unlearn`` exactly;only the extra term differs."""import torchimport torch.nn.functional as Ffrom safetune._refusal_helpers import _get_decoder_layersfrom safetune.interventions.unlearn.rmu import _forward_with_cacheupdated_module = _get_decoder_layers(model)[layer_id]frozen_module = _get_decoder_layers(frozen_model)[layer_id]ref_param = params[0]hidden_size = getattr(getattr(model, "config", None), "hidden_size", None) or ref_param.shape[0]random_vector = torch.rand(1, 1, hidden_size, dtype=ref_param.dtype, device=ref_param.device)control_vec = random_vector / torch.norm(random_vector) * steering_coeffoptimizer = torch.optim.AdamW(params, lr=lr)trace: list[dict] = []steps = 0for forget_batch, retain_batch, utility_batch in zip(forget_batches, retain_batches, utility_batches):if max_num_batches is not None and steps >= max_num_batches:breakwith torch.autocast("cuda", dtype=torch.bfloat16):act_forget = _forward_with_cache(model, forget_batch, updated_module, no_grad=False)forget_loss = F.mse_loss(act_forget, control_vec.expand_as(act_forget).to(act_forget.dtype))act_retain = _forward_with_cache(model, retain_batch, updated_module, no_grad=False)act_retain_frozen = _forward_with_cache(frozen_model, retain_batch, frozen_module, no_grad=True)retain_loss = alpha * F.mse_loss(act_retain, act_retain_frozen.to(act_retain.dtype))act_utility = _forward_with_cache(model, utility_batch, updated_module, no_grad=False)act_utility_frozen = _forward_with_cache(frozen_model, utility_batch, frozen_module, no_grad=True)utility_loss = (alpha * beta) * F.mse_loss(act_utility, act_utility_frozen.to(act_utility.dtype))loss = forget_loss + retain_loss + utility_lossoptimizer.zero_grad(set_to_none=True)loss.backward()optimizer.step()steps += 1trace.append({"step": steps,"loss": round(float(loss.item()), 6),"forget": round(float(forget_loss.item()), 6),"retain": round(float(retain_loss.item()), 6),"utility": round(float(utility_loss.item()), 6),})if steps == 1 or steps % 20 == 0:print(f"[rmu] step {steps}/{max_num_batches} {trace[-1]}", flush=True)return steps, tracedef run_rmu(model_name: str,bench_repo: str,7 unmodified linesckpt_name: str = "rmu_ckpt",telemetry_name: str = "rmu.json",vram_cap_gb: float = 21.0,retain_extra_texts: list[str] | None = None,retain_beta: float = 0.0,) -> dict:timer.check("rmu", need=600)import torch31 unmodified linesforget_texts = [r["text"] for r in read_jsonl(download_bench_file("arm_a/train.jsonl", bench_repo))]mmlu = read_jsonl(download_bench_file("controls/global_mmlu.jsonl", bench_repo))retain_texts = []# Multilingual retain sampling: interleave EN, HI, AR to prevent multilingual utility collapsemmlu_en = [r for r in mmlu if r["lang"] == "en"]mmlu_hi = [r for r in mmlu if r["lang"] == "hi"]mmlu_ar = [r for r in mmlu if r["lang"] == "ar"]min_count = min(len(mmlu_en), len(mmlu_hi), len(mmlu_ar)) if (mmlu_hi and mmlu_ar) else len(mmlu_en)retain_sample = mmlu_en[:min_count] + mmlu_hi[:min_count] + mmlu_ar[:min_count]random.Random(42).shuffle(retain_sample)for rec in retain_sample:retain_texts.append(f"{rec['question']}\nAnswer: {rec['options'][rec['answer_index']]}")if not forget_texts or not retain_texts:raise RuntimeError("RMU: missing forget/retain texts")forget_batches = make_batches(forget_texts, tok, n=max_num_batches, max_len=max_len)retain_batches = make_batches(retain_texts, tok, n=max_num_batches, max_len=max_len)utility_batches: list[dict] = []utility_retain_weight = 0.0if retain_extra_texts and retain_beta > 0:utility_batches = make_batches(retain_extra_texts, tok, n=max_num_batches, max_len=max_len)utility_retain_weight = alpha * retain_betaloss_trace: list[dict] = []trainer = Nonesteps_done = Nonetry:if utility_batches:print(f"[rmu] utility-retain stream: n={len(utility_batches)} "f"beta={retain_beta} (effective weight={utility_retain_weight})",flush=True,)steps_done, loss_trace = _rmu_utility_retain_loop(model,frozen,forget_batches,retain_batches,utility_batches,layer_id=layer_id,params=[p for p in model.parameters() if p.requires_grad],lr=lr,alpha=alpha,beta=retain_beta,max_num_batches=max_num_batches,)updated = modelelse:trainer = RMUTrainer(model,layer_id=layer_id,update_layer_ids=update_layer_ids,max_num_batches=max_num_batches,lr=lr,alpha=alpha,upcast=False,)with torch.autocast("cuda", dtype=torch.bfloat16):updated = trainer.unlearn(forget=forget_batches, retain=retain_batches, frozen_model=frozen)except Exception:try:model.to("cpu")24 unmodified lines"phase": "rmu","model": model_name,"steps": max_num_batches,"steps_completed": int(steps_done) if utility_batches else max_num_batches,"alpha": alpha,"retain_beta": retain_beta,"utility_retain_weight": utility_retain_weight,"utility_retain_n": len(utility_batches),"loss_trace_head": loss_trace[:3],"loss_trace_tail": loss_trace[-3:],"lr": lr,"layer_id": layer_id,"max_len": max_len,
scripts/smoke_l4.py+134-14