File size: 21,025 Bytes
152228b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | from __future__ import annotations
import argparse
from dataclasses import dataclass, field
def _parse_amplitudes(raw: str) -> list[int]:
out: list[int] = []
for part in str(raw).replace(" ", "").split(","):
if not part:
continue
value = int(part)
if value >= 1:
out.append(value)
return out or [1]
@dataclass
class AttackConfig:
mode: str = "auto"
device: str = "auto"
seed: int = 0
time_budget_seconds: float = 20.0
grid_max_amplitude: int = 3
grid_max_steps: int = 200
grid_topk_values: int = 0
grid_topk_fraction: float = 0.002
grid_topk_growth: float = 1.5
grid_target_margin: float = 10.5
grid_margin_relax: float = 0.5
grid_target_classes: int = 3
grid_plateau_tolerance: float = 0.05
grid_score_patience: int = 8
grid_saliency_momentum: float = 0.9
grid_prune: bool = True
grid_prune_chunk: int = 256
spgd_enabled: bool = True
spgd_steps: int = 90
spgd_warmup: int = 15
spgd_alpha_frac: float = 0.25
spgd_k_min_frac: float = 0.02
spgd_shrink: float = 0.94
spgd_momentum: float = 0.9
spgd_restarts: int = 3
spgd_refine: int = 1
cp_enabled: bool = True
cp_climb_target: float = 11.0
cp_screen_k: int = 10
cp_screen_steps: int = 8
cp_p0_frac: float = 0.28
cp_p1_frac: float = 0.62
cp_dual_target: bool = False
cp_dual_margin: float = 8.5
cp_adapt_steps: int = 40
cp_hold_margin: float = 9.0
# GATED batched multi-target screen (default OFF — NOT viable within budget). The gate correctly
# detects stuck rows (normal target short-climb margin < cp_batched_gate_margin) and the batched
# screen finds a better target, BUT the screen (~10s; batched forwards of EffNetV2-L aren't cheap
# even parallelized) runs ON TOP of the ~12s normal screening and STARVES cp's optimization phases
# -> cp fails (0.0 on 34199). Even at live budget (~25s for cp) there isn't room. Kept behind this
# flag; would need a much cheaper target search to be viable.
cp_batched_screen: bool = False
cp_batched_k: int = 10
cp_batched_steps: int = 30
cp_batched_gate_margin: float = 6.0
cp_batched_switch_margin: float = 2.0
# cp slice when the amp-1 feasibility probe says "infeasible" (weak signal): cap cp so
# APGD-DLR (which finds amp-1 flips on these rows, validated 48591/45508) gets time.
cp_infeas_slice: float = 12.0
# amp-1 feasibility probe + guaranteed-flip fallback (never submit a clean image / 0.0)
feas_probe_enabled: bool = True
feas_probe_steps: int = 22
gf_enabled: bool = True
gf_climb_steps: int = 70
gf_prune_frac: float = 0.08
# reserved slice (s) for guaranteed-flip so a failed APGD can't starve it; 0 = old 0.90*budget
gf_budget_seconds: float = 9.0
# targeted-APGD amp-1 flip-finder for hard 0.7x rows (replaces untargeted APGD last-resort):
# targeted DLR-APGD over top-k rivals + bounded converge-prune. Validated 8/9 -> 0.905-0.923.
tapgd_enabled: bool = True
tapgd_targets: int = 6
tapgd_iter: int = 40
tapgd_prune_frac: float = 0.08
tapgd_prune_rounds: int = 12
# multi-target amp-1 flip-finder (REPLACES targeted-APGD): batched full-support climb over top-k
# rivals finds the rank-5..8 knife-edge amp-1 flip targeted-APGD misses. Fixes the catastrophic
# gf amp-2/amp-3 escalations (24301 0.469->0.90, 45082/120818 0.714->0.91; flips 59880 too).
mt_amp1_enabled: bool = True
mt_amp1_targets: int = 10
mt_amp1_steps: int = 34
mt_amp1_check_every: int = 2
mt_amp1_prune_rounds: int = 8
mt_amp1_prune_frac: float = 0.08
mt_amp1_prune_conv: int = 8
# BB-L0 boundary-walk REFINEMENT of cp's flip (Session-14): sparsifies cp's over-dense margin-10
# support below cp's monotone-prune basin by MIGRATING it (drop low-|g| active + re-add high-|g|
# inactive) along the boundary. Portfolio candidate "cp-bb" (best-by-score => 0 regression).
# SUBMIT-SAFE design: cp keeps its FULL budget; BB runs ONLY on over-dense cp flips (density >
# bb_min_density), in the cp-end -> bb_hard_cap_seconds slack, so sparse rows are untouched (submit
# ~30s) and dense-row submission stays < the validator's ~35s cutoff (submit ~32-33s). Validated:
# 50-task live pilot +0.0012/task, 0 real regressions. bb_hard_cap_seconds is wall-clock from task start.
bb_enabled: bool = False
bb_hard_cap_seconds: float = 34.0
bb_min_density: float = 0.10
bb_margin_target: float = 10.2
bb_drop_frac: float = 0.04
bb_add_frac: float = 0.015
bb_reconv_steps: int = 5
bb_warm_steps: int = 150
bb_kmin_frac: float = 0.001
# SPARSE-SWEEP refiner (Session-18): ~1s support-size K-ladder jump to ultra-sparse
# basins along the boundary-gradient ranking at cp's converged point. Validated on
# GT rows vs uid64 actual submissions (+0.0059/row mean lift). Runs before FMN/BB.
sw_enabled: bool = True
sw_budget_seconds: float = 3.5
# BARE-FLIP grinder (Session-19): when nothing flipped at amp-1, run focused
# per-target climbs accepting ANY margin>0 flip -- converts the rescue species
# (verified: rows 102024/18992/5348 are amp-1-flippable by competitors on our
# model). Dormant; enable with bf_enabled=True at a restart.
bf_enabled: bool = True
bf_budget_seconds: float = 18.0
# cp plateau exit: hand back wall-clock when phase-2 stops improving (stuck rows only)
cp_stall_limit: int = 2
# Session-23 batched multi-target cp: 2s untargeted flood -> [clean_top1, flood_class],
# B=2 full phases at single-cp wall-clock (validated +0.0053/row gated on stuck badrows)
cp_batched_mt: bool = True
cp_mt_flood_steps: int = 24
# Session-24: trial-reconverge + relative-score commit w/ m>=8 floor in batched phase-2
cp_mt_relcommit: bool = True
# margin-topup (Session-26): support-expansion rescue on margin-stuck rows (cp m<gate).
# Buys margin with density along the +EV exchange; validated +0.0064/row on gold stuck rows.
topup_enabled: bool = True
topup_gate_margin: float = 8.5
topup_budget_seconds: float = 5.5
# margin-repair: BB fires on low-margin flips regardless of density
bb_repair_margin: float = 8.5
# STUCK-RESCUE levers (pilot-validated ab5: +0.0045/hard-row, 0 regressions):
# fire the multitarget climb when best candidate margin is weak, and lift
# razor-sparse grid flips toward margin 8 via BB migration.
rs_rescue_enabled: bool = True
rs_margin_gate: float = 8.5
rs_sparse_polish: bool = True
rs_sparse_max_density: float = 0.05
rs_sparse_lift_gate: float = 9.5
rs_sparse_margin_target: float = 8.0
bf_targets: int = 6
bf_adam_steps: int = 90
bf_climb_steps: int = 240
# BATCHED Sparse-PGD (paper 2405.05075): unprojected-gradient mask ensemble,
# eps-ladder x targets x proj/unproj variants in one fp16 batch. Dormant.
sb_enabled: bool = False
sb_budget_seconds: float = 10.0
sb_batch: int = 12
sb_beta: float = 0.10
sb_alpha: float = 1.0
sb_eval_every: int = 5
sb_stop_score: float = 0.9865
# POLISH-AND-SHRINK: batched exact-score hill-climb on the cp candidate
# (prune/flip/refill moves proposed by boundary gradient, verified on grid).
# Offline: 70769 0.949@35% -> 0.973@6.6% in 38s. Dormant until piloted.
pn_enabled: bool = True
pn_budget_seconds: float = 5.0
pn_slots: int = 6
pn_eval_bs: int = 24
pn_stop_score: float = 0.988
pn_margin_floor: float = 8.5
# DENSITY UPGRADE (staged, default OFF): route over-dense cp flips to FMN instead of BB
fmn_dense_enabled: bool = True
fmn_dense_gate: float = 0.12
fmn_margin_target: float = 10.0
fmn_steps: int = 200
# APGD-guided prune of cp (Session-27): iterative prune with APGD sign reconverge, holds margin ≥9 at lower density.
# Verified 12 rows: mean +0.0021 (5/12 wins +0.0156 on dense 57%→34%, 0 on sparse≤18% where cp optimal, 0 regression).
# Runs only on dense cp flips in the same cp-end→bb_hard_cap slack as fmn/bb, so sparse rows stay ~30.5s.
apgd_prune_enabled: bool = False
apgd_prune_gate: float = 0.20
apgd_prune_hold: float = 9.0
apgd_prune_steps: int = 24
apgd_prune_reconv: int = 18
apgd_prune_drop: float = 0.06
# GEN concurrent candidate (Session-27): learned-prior attack (supgen3 head -> flood targets
# -> ladder converge-grow -> chunkwalk polish) run in a PARALLEL thread on its own VictimModel.
# GPU co-existence proven by the shadow pilot (miner timing/scores unchanged, ~8.6GB total).
# Joined before the final prune; enters the portfolio only via exact re-verification.
# SAFE LIVE DEFAULTS (matches box pid360023): run ONE gen variant (gen_pilot) as a parallel
# candidate; gen_enabled + unstarved OFF. gen_enabled=True would run a 2nd gen pass, and
# unstarved slashes cp to a 9s budget (runner.py) -> the 30.4s-submit/403 landmine. Do NOT
# flip these True together without re-checking the submit-time budget.
gen_enabled: bool = False
gen_budget_seconds: float = 28.0
gen_head_path: str = "/root/supgen3/head_ep5.pt"
gen_ladder: tuple = (0.05, 0.08, 0.12)
flood_k: int = 8
gen_pilot_enabled: bool = True
gen_pilot_unstarved_enabled: bool = False
gen_pilot_budget_seconds: float = 24.0
# TSAA learned sparse generator (He et al. 2105.14727): decoupled pos×amp with random quantization
# End-to-end learned 9% support at margin 10; 700× faster inference than SparseFool. Dormant until Phase B trained.
tsaa_enabled: bool = False
tsaa_density: float = 0.09
tsaa_budget_seconds: float = 0.8
tsaa_head_path: str = "/root/tsaa/head_B_ep2.pt"
# chunkwalk polish on cp (Session-26): multi-scale verified migration, +0.0041 on dense rows
chunkwalk_enabled: bool = True
chunkwalk_budget_seconds: float = 8.0
# replay precomputed solutions for repeated rows (repeats serve byte-identical cleans)
replay_enabled: bool = True
replay_save: bool = True
replay_store: str = "/root/replay_store"
replay_min_score: float = 0.90
sz_enabled: bool = True
sz_steps: int = 100
sz_sigma: float = 1e-3
sz_tau0: float = 0.3
sz_t: float = 0.01
sz_lam: float = 1.0
sz_lr: float = 1.0
lasso_enabled: bool = True
lasso_iterations: int = 120
lasso_lr: float = 0.03
lasso_phase_l1: float = 1.0
lasso_shrink_factor: float = 0.7
lasso_barrier_weight: float = 4.0
lasso_stage_iterations: int = 12
lasso_soft_classes: int = 8
lasso_margin_stop: float = 3.0
lasso_refine_with_grid: bool = True
apgd_amplitudes: list[int] = field(default_factory=lambda: [1, 2, 3])
apgd_iterations: int = 100
apgd_restarts: int = 1
apgd_loss: str = "dlr"
apgd_refine_with_grid: bool = True
apgd_always: bool = True
# reserved slice (s) for APGD-DLR so the capped cp slice can't starve it; 0 = old behavior
apgd_slice_seconds: float = 10.0
def add_attack_args(parser: argparse.ArgumentParser) -> None:
group = parser.add_argument_group("attack", "Perturbation optimization controls")
group.add_argument(
"--attack.mode", dest="attack_mode", type=str, default="auto", choices=["auto", "grid", "lasso", "apgd", "spgd"],
help="Attack strategy: grid = greedy discrete +-1/255 margin attack, apgd = AutoPGD fallback, auto = grid then apgd fallback.",
)
group.add_argument(
"--attack.device", dest="attack_device", type=str, default="auto",
help="Device for attack math: auto (cuda>mps>cpu), cpu, cuda, mps.",
)
group.add_argument("--attack.seed", dest="attack_seed", type=int, default=0, help="Deterministic seed per run.")
group.add_argument(
"--attack.time-budget-seconds", dest="attack_time_budget_seconds", type=float, default=20.0,
help="Total wall-clock budget for one task (validator expects submission within ~20-30s).",
)
group.add_argument(
"--attack.grid.max-amplitude", dest="attack_grid_max_amplitude", type=int, default=3,
help="Max perturbation amplitude in 1/255 units tried by the grid attack (ascending).",
)
group.add_argument(
"--attack.grid.max-steps", dest="attack_grid_max_steps", type=int, default=200,
help="Max greedy steps per amplitude level.",
)
group.add_argument(
"--attack.grid.topk-values", dest="attack_grid_topk_values", type=int, default=0,
help="Absolute number of pixel-values moved per greedy step; 0 selects fraction mode.",
)
group.add_argument(
"--attack.grid.topk-fraction", dest="attack_grid_topk_fraction", type=float, default=0.002,
help="Fraction of total pixel-values moved per greedy step (used when topk-values is 0).",
)
group.add_argument(
"--attack.grid.topk-growth", dest="attack_grid_topk_growth", type=float, default=1.5,
help="Multiply per-step topk by this when the greedy stalls (escalation schedule).",
)
group.add_argument(
"--attack.grid.target-margin", dest="attack_grid_target_margin", type=float, default=10.5,
help="Stop pushing margin once best_non_true_logit - true_logit reaches this (validator caps bonus at 10).",
)
group.add_argument(
"--attack.grid.target-classes", dest="attack_grid_target_classes", type=int, default=3,
help="Number of pinned runner-up classes to attack in addition to the adaptive pass; 0 disables.",
)
group.add_argument(
"--attack.grid.margin-relax", dest="attack_grid_margin_relax", type=float, default=0.5,
help="Allowed margin slack when pruning changes (rmse trim).",
)
group.add_argument(
"--attack.grid.plateau-tolerance", dest="attack_grid_plateau_tolerance", type=float, default=0.05,
help="Stop adding moves once margin gains over the last 5 steps fall below this.",
)
group.add_argument(
"--attack.grid.score-patience", dest="attack_grid_score_patience", type=int, default=8,
help="Steps without a new best score before the greedy loop stops (set high for margin-boost runs).",
)
group.add_argument(
"--attack.grid.saliency-momentum", dest="attack_grid_saliency_momentum", type=float, default=0.9,
help="EMA momentum for gradient saliency in the grid attack (0 = raw gradients).",
)
group.add_argument(
"--attack.grid.prune", dest="attack_grid_prune", action=argparse.BooleanOptionalAction, default=True,
help="Revert least-useful changes while margin budget allows (use --attack.grid.no-prune to disable).",
)
group.add_argument(
"--attack.grid.prune-chunk", dest="attack_grid_prune_chunk", type=int, default=256,
help="Coordinates reverted per prune trial.",
)
group.add_argument(
"--attack.lasso.enabled", dest="attack_lasso_enabled", action=argparse.BooleanOptionalAction, default=True,
help="Run the L1-penalized continuous soft-margin attack as a candidate generator (use --no-attack.lasso.enabled to disable).",
)
group.add_argument(
"--attack.lasso.iterations", dest="attack_lasso_iterations", type=int, default=120,
help="Total Adam iterations across all homotopy stages.",
)
group.add_argument(
"--attack.lasso.lr", dest="attack_lasso_lr", type=float, default=0.03,
help="Adam learning rate on the tanh parameterization.",
)
group.add_argument(
"--attack.lasso.phase-l1", dest="attack_lasso_phase_l1", type=float, default=1.0,
help="Mild L1 pressure during the margin-climb phase so support grows only as needed.",
)
group.add_argument(
"--attack.lasso.shrink-factor", dest="attack_lasso_shrink_factor", type=float, default=0.7,
help="Geometric support shrink per homotopy stage (phase B).",
)
group.add_argument(
"--attack.lasso.barrier-weight", dest="attack_lasso_barrier_weight", type=float, default=4.0,
help="Weight of the margin-maintenance barrier during support shrinkage.",
)
group.add_argument(
"--attack.lasso.stage-iterations", dest="attack_lasso_stage_iterations", type=int, default=12,
help="Adam iterations per homotopy stage.",
)
group.add_argument(
"--attack.lasso.soft-classes", dest="attack_lasso_soft_classes", type=int, default=8,
help="Top-M non-true classes in the smooth logsumexp margin surrogate.",
)
group.add_argument(
"--attack.lasso.margin-stop", dest="attack_lasso_margin_stop", type=float, default=3.0,
help="Margin target for the lasso attack: just enough to hold the flip cheaply (density costs ~7x more score than margin earns).",
)
group.add_argument(
"--attack.lasso.refine-with-grid", dest="attack_lasso_refine_with_grid", action=argparse.BooleanOptionalAction, default=True,
help="Warm-start the grid attack from the lasso candidate to repair/extend it.",
)
group.add_argument(
"--attack.apgd.amplitudes", dest="attack_apgd_amplitudes", type=str, default="1,2,3",
help="Comma-separated amplitudes (1/255 units) for the APGD fallback, ascending.",
)
group.add_argument(
"--attack.apgd.iterations", dest="attack_apgd_iterations", type=int, default=100,
help="AutoPGD iterations per amplitude.",
)
group.add_argument(
"--attack.apgd.restarts", dest="attack_apgd_restarts", type=int, default=1,
help="AutoPGD random restarts.",
)
group.add_argument(
"--attack.apgd.loss", dest="attack_apgd_loss", type=str, default="dlr", choices=["dlr", "ce"],
help="AutoPGD loss.",
)
group.add_argument(
"--attack.apgd.always", dest="attack_apgd_always", action=argparse.BooleanOptionalAction, default=True,
help="Also run APGD best-loss as a candidate even when the grid attack already flipped (use --attack.apgd.no-always to disable).",
)
group.add_argument(
"--attack.apgd.refine-with-grid", dest="attack_apgd_refine_with_grid", action=argparse.BooleanOptionalAction, default=True,
help="After an APGD flip, refine margin/sparsity with the grid attack (use --attack.apgd.no-refine-with-grid to disable).",
)
def attack_config_from_args(args) -> AttackConfig:
return AttackConfig(
mode=str(getattr(args, "attack_mode", "auto")),
device=str(getattr(args, "attack_device", "auto")),
seed=int(getattr(args, "attack_seed", 0)),
time_budget_seconds=float(getattr(args, "attack_time_budget_seconds", 20.0)),
grid_max_amplitude=int(getattr(args, "attack_grid_max_amplitude", 3)),
grid_max_steps=int(getattr(args, "attack_grid_max_steps", 200)),
grid_topk_values=int(getattr(args, "attack_grid_topk_values", 0)),
grid_topk_fraction=float(getattr(args, "attack_grid_topk_fraction", 0.002)),
grid_topk_growth=float(getattr(args, "attack_grid_topk_growth", 1.5)),
grid_target_margin=float(getattr(args, "attack_grid_target_margin", 10.5)),
grid_margin_relax=float(getattr(args, "attack_grid_margin_relax", 0.5)),
grid_target_classes=int(getattr(args, "attack_grid_target_classes", 3)),
grid_plateau_tolerance=float(getattr(args, "attack_grid_plateau_tolerance", 0.05)),
grid_score_patience=int(getattr(args, "attack_grid_score_patience", 8)),
grid_saliency_momentum=float(getattr(args, "attack_grid_saliency_momentum", 0.9)),
grid_prune=bool(getattr(args, "attack_grid_prune", True)),
grid_prune_chunk=int(getattr(args, "attack_grid_prune_chunk", 256)),
lasso_enabled=bool(getattr(args, "attack_lasso_enabled", True)),
lasso_iterations=int(getattr(args, "attack_lasso_iterations", 120)),
lasso_lr=float(getattr(args, "attack_lasso_lr", 0.03)),
lasso_phase_l1=float(getattr(args, "attack_lasso_phase_l1", 1.0)),
lasso_shrink_factor=float(getattr(args, "attack_lasso_shrink_factor", 0.7)),
lasso_barrier_weight=float(getattr(args, "attack_lasso_barrier_weight", 4.0)),
lasso_stage_iterations=int(getattr(args, "attack_lasso_stage_iterations", 12)),
lasso_soft_classes=int(getattr(args, "attack_lasso_soft_classes", 8)),
lasso_margin_stop=float(getattr(args, "attack_lasso_margin_stop", 3.0)),
lasso_refine_with_grid=bool(getattr(args, "attack_lasso_refine_with_grid", True)),
apgd_amplitudes=_parse_amplitudes(getattr(args, "attack_apgd_amplitudes", "1,2,3")),
apgd_iterations=int(getattr(args, "attack_apgd_iterations", 100)),
apgd_restarts=int(getattr(args, "attack_apgd_restarts", 1)),
apgd_loss=str(getattr(args, "attack_apgd_loss", "dlr")),
apgd_refine_with_grid=bool(getattr(args, "attack_apgd_refine_with_grid", True)),
apgd_always=bool(getattr(args, "attack_apgd_always", True)),
)
|