reneeice's picture
full research archive: code, models, datasets, experiments, logs
152228b verified
Raw History Blame Contribute Delete
1.58 kB
"""Probe batched forward+backward throughput/memory for multi-restart on 48GB."""
import sys, time
from pathlib import Path
REPO_ROOT=Path(__file__).resolve().parents[1]
if str(REPO_ROOT) not in sys.path: sys.path.insert(0,str(REPO_ROOT))
import torch
from miner_lib.victim import VictimModel
v=VictimModel(device="cuda")
import base64,io
from PIL import Image
from perturbnet.imagenet100_bootstrap import load_imagenet100
from perturbnet.image_io import decode_image_b64
ds=load_imagenet100(split="train")
img=ds[96196]["image"]; b=io.BytesIO(); img.convert("RGB").save(b,format="PNG")
x=decode_image_b64(base64.b64encode(b.getvalue()).decode()).to("cuda")
x0f=(x.clamp(0,1)*255).round().long().float()/255
eps=1/255.
for B in [8,16,32,48,64]:
try:
torch.cuda.reset_peak_memory_stats()
delta=torch.zeros((B,)+x0f.shape, device="cuda", requires_grad=True)
t0=time.time()
for _ in range(5):
inp=(x0f.unsqueeze(0)+delta).clamp(0,1)
logits=v(inp) # (B,1000)
loss=logits.sum()
g=torch.autograd.grad(loss, delta)[0]
delta=delta.detach()+eps*0.25*torch.sign(g); delta.requires_grad_(True)
torch.cuda.synchronize()
dt=(time.time()-t0)/5
mem=torch.cuda.max_memory_allocated()/1e9
print(f"B={B:<4} {dt*1000:.0f}ms/iter peak_mem={mem:.1f}GB per-restart={dt*1000/B:.1f}ms",flush=True)
del delta,logits,g; torch.cuda.empty_cache()
except RuntimeError as e:
print(f"B={B}: OOM/{str(e)[:50]}",flush=True); torch.cuda.empty_cache(); break