pliny-the-prompter commited on
Commit
0895fec
Β·
verified Β·
1 Parent(s): 2b4c8be

Upload 133 files

Browse files
Files changed (1) hide show
  1. app.py +39 -18
app.py CHANGED
@@ -2050,6 +2050,20 @@ def _format_multi_model_results(results: list[dict], context: dict | None = None
2050
  # Staged GPU wrapper for obliteration (tourney-style per-stage allocation)
2051
  # ---------------------------------------------------------------------------
2052
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2053
  @spaces.GPU(duration=300)
2054
  def _obliterate_gpu_run(fn, *args, **kwargs):
2055
  """Execute *fn* inside a ZeroGPU GPU allocation.
@@ -2062,6 +2076,25 @@ def _obliterate_gpu_run(fn, *args, **kwargs):
2062
  return fn(*args, **kwargs)
2063
 
2064
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2065
  def obliterate(model_choice: str, method_choice: str,
2066
  prompt_volume_choice: str, dataset_source_choice: str,
2067
  custom_harmful: str, custom_harmless: str,
@@ -2317,43 +2350,31 @@ def obliterate(model_choice: str, method_choice: str,
2317
  if method == "informed":
2318
  # Informed pipeline: SUMMON+PROBE | ANALYZE+DISTILL+EXCISE | VERIFY+REBIRTH
2319
  on_log("\n\u26a1 [staged] GPU Stage 1/3: SUMMON + PROBE")
2320
- _obliterate_gpu_run(pipeline.run_stage_summon_probe, time.time())
2321
  pipeline._offload_to_cpu()
2322
  on_log("[staged] GPU released after Stage 1\n")
2323
 
2324
  on_log("\u26a1 [staged] GPU Stage 2/3: ANALYZE + DISTILL + EXCISE")
2325
- def _informed_s2():
2326
- pipeline._restore_to_gpu()
2327
- pipeline.run_stage_analyze_distill_excise()
2328
- _obliterate_gpu_run(_informed_s2)
2329
  pipeline._offload_to_cpu()
2330
  on_log("[staged] GPU released after Stage 2\n")
2331
 
2332
  on_log("\u26a1 [staged] GPU Stage 3/3: VERIFY + REBIRTH")
2333
- def _informed_s3():
2334
- pipeline._restore_to_gpu()
2335
- pipeline.run_stage_verify_rebirth_informed()
2336
- _obliterate_gpu_run(_informed_s3)
2337
  else:
2338
  # Standard pipeline: SUMMON+PROBE | DISTILL+EXCISE | VERIFY+REBIRTH
2339
  on_log("\n\u26a1 [staged] GPU Stage 1/3: SUMMON + PROBE")
2340
- _obliterate_gpu_run(pipeline.run_stage_summon_probe, time.time())
2341
  pipeline._offload_to_cpu()
2342
  on_log("[staged] GPU released after Stage 1\n")
2343
 
2344
  on_log("\u26a1 [staged] GPU Stage 2/3: DISTILL + EXCISE")
2345
- def _standard_s2():
2346
- pipeline._restore_to_gpu()
2347
- pipeline.run_stage_distill_excise()
2348
- _obliterate_gpu_run(_standard_s2)
2349
  pipeline._offload_to_cpu()
2350
  on_log("[staged] GPU released after Stage 2\n")
2351
 
2352
  on_log("\u26a1 [staged] GPU Stage 3/3: VERIFY + REBIRTH")
2353
- def _standard_s3():
2354
- pipeline._restore_to_gpu()
2355
- pipeline.run_stage_verify_rebirth()
2356
- _obliterate_gpu_run(_standard_s3)
2357
  else:
2358
  # ── Local/non-ZeroGPU: single-shot execution ──────────────
2359
  on_log(f"[timing] Running locally (no GPU time limit)")
 
2050
  # Staged GPU wrapper for obliteration (tourney-style per-stage allocation)
2051
  # ---------------------------------------------------------------------------
2052
 
2053
+ def _noop_callback(*args, **kwargs):
2054
+ """Module-level no-op, used as a picklable placeholder for callbacks."""
2055
+ pass
2056
+
2057
+
2058
+ def _restore_and_run_stage(pipeline, stage_method_name):
2059
+ """Restore pipeline to GPU and run the named stage method.
2060
+
2061
+ Module-level function so it is picklable for ZeroGPU serialization.
2062
+ """
2063
+ pipeline._restore_to_gpu()
2064
+ getattr(pipeline, stage_method_name)()
2065
+
2066
+
2067
  @spaces.GPU(duration=300)
2068
  def _obliterate_gpu_run(fn, *args, **kwargs):
2069
  """Execute *fn* inside a ZeroGPU GPU allocation.
 
2076
  return fn(*args, **kwargs)
2077
 
2078
 
2079
+ def _gpu_run_picklable(pipeline, fn, *args, **kwargs):
2080
+ """Run *fn* via ``_obliterate_gpu_run`` after stripping unpicklable callbacks.
2081
+
2082
+ ZeroGPU pickles arguments to send them to a GPU worker process. The
2083
+ pipeline's ``_on_stage`` and ``_on_log`` callbacks are local closures
2084
+ that cannot be pickled, so we temporarily replace them with a
2085
+ module-level no-op before the GPU call and restore them afterwards.
2086
+ """
2087
+ saved_on_stage = pipeline._on_stage
2088
+ saved_on_log = pipeline._on_log
2089
+ pipeline._on_stage = _noop_callback
2090
+ pipeline._on_log = _noop_callback
2091
+ try:
2092
+ return _obliterate_gpu_run(fn, *args, **kwargs)
2093
+ finally:
2094
+ pipeline._on_stage = saved_on_stage
2095
+ pipeline._on_log = saved_on_log
2096
+
2097
+
2098
  def obliterate(model_choice: str, method_choice: str,
2099
  prompt_volume_choice: str, dataset_source_choice: str,
2100
  custom_harmful: str, custom_harmless: str,
 
2350
  if method == "informed":
2351
  # Informed pipeline: SUMMON+PROBE | ANALYZE+DISTILL+EXCISE | VERIFY+REBIRTH
2352
  on_log("\n\u26a1 [staged] GPU Stage 1/3: SUMMON + PROBE")
2353
+ _gpu_run_picklable(pipeline, pipeline.run_stage_summon_probe, time.time())
2354
  pipeline._offload_to_cpu()
2355
  on_log("[staged] GPU released after Stage 1\n")
2356
 
2357
  on_log("\u26a1 [staged] GPU Stage 2/3: ANALYZE + DISTILL + EXCISE")
2358
+ _gpu_run_picklable(pipeline, _restore_and_run_stage, pipeline, "run_stage_analyze_distill_excise")
 
 
 
2359
  pipeline._offload_to_cpu()
2360
  on_log("[staged] GPU released after Stage 2\n")
2361
 
2362
  on_log("\u26a1 [staged] GPU Stage 3/3: VERIFY + REBIRTH")
2363
+ _gpu_run_picklable(pipeline, _restore_and_run_stage, pipeline, "run_stage_verify_rebirth_informed")
 
 
 
2364
  else:
2365
  # Standard pipeline: SUMMON+PROBE | DISTILL+EXCISE | VERIFY+REBIRTH
2366
  on_log("\n\u26a1 [staged] GPU Stage 1/3: SUMMON + PROBE")
2367
+ _gpu_run_picklable(pipeline, pipeline.run_stage_summon_probe, time.time())
2368
  pipeline._offload_to_cpu()
2369
  on_log("[staged] GPU released after Stage 1\n")
2370
 
2371
  on_log("\u26a1 [staged] GPU Stage 2/3: DISTILL + EXCISE")
2372
+ _gpu_run_picklable(pipeline, _restore_and_run_stage, pipeline, "run_stage_distill_excise")
 
 
 
2373
  pipeline._offload_to_cpu()
2374
  on_log("[staged] GPU released after Stage 2\n")
2375
 
2376
  on_log("\u26a1 [staged] GPU Stage 3/3: VERIFY + REBIRTH")
2377
+ _gpu_run_picklable(pipeline, _restore_and_run_stage, pipeline, "run_stage_verify_rebirth")
 
 
 
2378
  else:
2379
  # ── Local/non-ZeroGPU: single-shot execution ──────────────
2380
  on_log(f"[timing] Running locally (no GPU time limit)")