kharki commited on
Commit
a2b7259
·
verified ·
1 Parent(s): a4b762c

fix: replace app.py with file-based upload (no heredoc f-string issue)

Browse files
Files changed (1) hide show
  1. app.py +28 -61
app.py CHANGED
@@ -1,4 +1,4 @@
1
- """FOG Ablation ZeroGPU runner for stress tasks."""
2
  from __future__ import annotations
3
 
4
  import json
@@ -9,7 +9,6 @@ import time
9
  import gradio as gr
10
  import spaces
11
 
12
- # Ensure src/ is importable
13
  sys.path.insert(0, os.path.dirname(__file__))
14
 
15
 
@@ -26,47 +25,35 @@ def _gpu_decorator(fn):
26
 
27
 
28
  @_gpu_decorator
29
- def run_fog_task(task_name: str, seeds_str: str, n_epochs: int, n_train: int, batch_size: int) -> str:
30
- """Run one FOG stress task across all 3 models and given seeds."""
31
  import torch
32
  from src.fog.config import BASELINE_MED, MOTIF_MED, UNIFORM_MED
33
  from src.fog.train import run_experiment
34
 
35
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
36
  seeds = [int(s.strip()) for s in seeds_str.split(",")]
37
-
38
  configs = [
39
  ("baseline", BASELINE_MED),
40
  ("uniform_small", UNIFORM_MED),
41
  ("motif", MOTIF_MED),
42
  ]
43
-
44
  results = []
45
  for seed in seeds:
46
  for model_type, cfg in configs:
47
  result = run_experiment(
48
- task_name=task_name,
49
- cfg=cfg,
50
- model_type=model_type,
51
- n_epochs=n_epochs,
52
- batch_size=batch_size,
53
- lr=3e-4,
54
- device=device,
55
- seed=seed,
56
- n_train=n_train,
57
- n_eval=500,
58
  )
59
  results.append(result)
60
  print(f" {model_type}/{task_name} seed={seed}: "
61
  f"acc={result['final_accuracy']:.4f} em={result['final_exact_match']:.4f} "
62
  f"time={result['elapsed_s']}s")
63
-
64
  return json.dumps(results, indent=2)
65
 
66
 
67
  @_gpu_decorator
68
- def run_all_tasks(seeds_str: str, n_epochs: int, n_train: int, batch_size: int) -> str:
69
- """Run ALL 4 stress tasks sequentially."""
70
  import torch
71
  from src.fog.config import BASELINE_MED, MOTIF_MED, UNIFORM_MED
72
  from src.fog.train import run_experiment
@@ -74,55 +61,42 @@ def run_all_tasks(seeds_str: str, n_epochs: int, n_train: int, batch_size: int)
74
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
75
  seeds = [int(s.strip()) for s in seeds_str.split(",")]
76
  tasks = ["conditional", "intersection", "compose_add", "multihop"]
77
-
78
  configs = [
79
  ("baseline", BASELINE_MED),
80
  ("uniform_small", UNIFORM_MED),
81
  ("motif", MOTIF_MED),
82
  ]
83
-
84
  all_results = []
85
  for task_name in tasks:
86
- print(f"
87
- {'='*50} {task_name} {'='*50}")
88
  for seed in seeds:
89
  for model_type, cfg in configs:
90
  result = run_experiment(
91
- task_name=task_name,
92
- cfg=cfg,
93
- model_type=model_type,
94
- n_epochs=n_epochs,
95
- batch_size=batch_size,
96
- lr=3e-4,
97
- device=device,
98
- seed=seed,
99
- n_train=n_train,
100
- n_eval=500,
101
  )
102
  all_results.append(result)
103
  print(f" {model_type}/{task_name} seed={seed}: "
104
- f"acc={result['final_accuracy']:.4f} em={result['final_exact_match']:.4f} "
 
105
  f"time={result['elapsed_s']}s")
106
-
107
- # Summary table
108
- summary = "
109
- " + "="*70 + "
110
- "
111
- summary += f"{'Task':<15} {'Model':<15} {'Params':>8} {'Acc':>8} {'EM':>8} {'Time':>6}
112
- "
113
- summary += "-"*70 + "
114
- "
115
  for r in all_results:
116
- summary += (f"{r['task']:<15} {r['model_type']:<15} {r['n_params']:>8,} "
117
- f"{r['final_accuracy']:>8.4f} {r.get('final_exact_match',0):>8.4f} "
118
- f"{r['elapsed_s']:>5.0f}s
119
- ")
120
- print(summary)
121
-
122
  return json.dumps(all_results, indent=2)
123
 
124
 
125
- def health_check() -> str:
126
  import torch
127
  return json.dumps({
128
  "status": "ok",
@@ -134,38 +108,31 @@ def health_check() -> str:
134
 
135
  with gr.Blocks(title="FOG Stress Ablation") as demo:
136
  gr.Markdown("# FOG Stress Ablation (ZeroGPU)")
137
- gr.Markdown("Runs 4 stress tasks: conditional, intersection, compose_add, multihop")
138
-
139
  with gr.Row():
140
  with gr.Column():
141
  seeds_input = gr.Textbox(label="Seeds (comma-sep)", value="42, 43, 44")
142
  epochs_input = gr.Slider(10, 200, value=100, step=10, label="Epochs")
143
  n_train_input = gr.Slider(1000, 10000, value=5000, step=1000, label="Train samples")
144
  batch_input = gr.Slider(64, 512, value=256, step=64, label="Batch size")
145
-
146
  with gr.Column():
147
  task_dropdown = gr.Dropdown(
148
  ["conditional", "intersection", "compose_add", "multihop"],
149
- label="Single task", value="conditional"
150
  )
151
  run_one_btn = gr.Button("Run Single Task", variant="secondary")
152
  run_all_btn = gr.Button("Run ALL 4 Tasks", variant="primary")
153
-
154
  output_box = gr.Textbox(label="Results JSON", lines=20)
155
-
156
  run_one_btn.click(
157
  fn=run_fog_task,
158
  inputs=[task_dropdown, seeds_input, epochs_input, n_train_input, batch_input],
159
- outputs=output_box,
160
- api_name="run_task",
161
  )
162
  run_all_btn.click(
163
  fn=run_all_tasks,
164
  inputs=[seeds_input, epochs_input, n_train_input, batch_input],
165
- outputs=output_box,
166
- api_name="run_all",
167
  )
168
-
169
  health_btn = gr.Button("Health Check")
170
  health_output = gr.Textbox(label="Health", lines=3)
171
  health_btn.click(fn=health_check, outputs=health_output, api_name="health")
 
1
+ """FOG Ablation - ZeroGPU runner for stress tasks."""
2
  from __future__ import annotations
3
 
4
  import json
 
9
  import gradio as gr
10
  import spaces
11
 
 
12
  sys.path.insert(0, os.path.dirname(__file__))
13
 
14
 
 
25
 
26
 
27
  @_gpu_decorator
28
+ def run_fog_task(task_name, seeds_str, n_epochs, n_train, batch_size):
 
29
  import torch
30
  from src.fog.config import BASELINE_MED, MOTIF_MED, UNIFORM_MED
31
  from src.fog.train import run_experiment
32
 
33
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
34
  seeds = [int(s.strip()) for s in seeds_str.split(",")]
 
35
  configs = [
36
  ("baseline", BASELINE_MED),
37
  ("uniform_small", UNIFORM_MED),
38
  ("motif", MOTIF_MED),
39
  ]
 
40
  results = []
41
  for seed in seeds:
42
  for model_type, cfg in configs:
43
  result = run_experiment(
44
+ task_name=task_name, cfg=cfg, model_type=model_type,
45
+ n_epochs=int(n_epochs), batch_size=int(batch_size), lr=3e-4,
46
+ device=device, seed=seed, n_train=int(n_train), n_eval=500,
 
 
 
 
 
 
 
47
  )
48
  results.append(result)
49
  print(f" {model_type}/{task_name} seed={seed}: "
50
  f"acc={result['final_accuracy']:.4f} em={result['final_exact_match']:.4f} "
51
  f"time={result['elapsed_s']}s")
 
52
  return json.dumps(results, indent=2)
53
 
54
 
55
  @_gpu_decorator
56
+ def run_all_tasks(seeds_str, n_epochs, n_train, batch_size):
 
57
  import torch
58
  from src.fog.config import BASELINE_MED, MOTIF_MED, UNIFORM_MED
59
  from src.fog.train import run_experiment
 
61
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
62
  seeds = [int(s.strip()) for s in seeds_str.split(",")]
63
  tasks = ["conditional", "intersection", "compose_add", "multihop"]
 
64
  configs = [
65
  ("baseline", BASELINE_MED),
66
  ("uniform_small", UNIFORM_MED),
67
  ("motif", MOTIF_MED),
68
  ]
 
69
  all_results = []
70
  for task_name in tasks:
71
+ sep = "=" * 50
72
+ print(sep + " " + task_name + " " + sep)
73
  for seed in seeds:
74
  for model_type, cfg in configs:
75
  result = run_experiment(
76
+ task_name=task_name, cfg=cfg, model_type=model_type,
77
+ n_epochs=int(n_epochs), batch_size=int(batch_size), lr=3e-4,
78
+ device=device, seed=seed, n_train=int(n_train), n_eval=500,
 
 
 
 
 
 
 
79
  )
80
  all_results.append(result)
81
  print(f" {model_type}/{task_name} seed={seed}: "
82
+ f"acc={result['final_accuracy']:.4f} "
83
+ f"em={result['final_exact_match']:.4f} "
84
  f"time={result['elapsed_s']}s")
85
+ # Summary
86
+ lines = ["", "=" * 70]
87
+ lines.append(f"{'Task':<15} {'Model':<15} {'Params':>8} {'Acc':>8} {'EM':>8} {'Time':>6}")
88
+ lines.append("-" * 70)
 
 
 
 
 
89
  for r in all_results:
90
+ em = r.get("final_exact_match", 0) or 0
91
+ lines.append(
92
+ f"{r['task']:<15} {r['model_type']:<15} {r['n_params']:>8,} "
93
+ f"{r['final_accuracy']:>8.4f} {em:>8.4f} {r['elapsed_s']:>5.0f}s"
94
+ )
95
+ print("\n".join(lines))
96
  return json.dumps(all_results, indent=2)
97
 
98
 
99
+ def health_check():
100
  import torch
101
  return json.dumps({
102
  "status": "ok",
 
108
 
109
  with gr.Blocks(title="FOG Stress Ablation") as demo:
110
  gr.Markdown("# FOG Stress Ablation (ZeroGPU)")
111
+ gr.Markdown("4 stress tasks: conditional, intersection, compose_add, multihop")
 
112
  with gr.Row():
113
  with gr.Column():
114
  seeds_input = gr.Textbox(label="Seeds (comma-sep)", value="42, 43, 44")
115
  epochs_input = gr.Slider(10, 200, value=100, step=10, label="Epochs")
116
  n_train_input = gr.Slider(1000, 10000, value=5000, step=1000, label="Train samples")
117
  batch_input = gr.Slider(64, 512, value=256, step=64, label="Batch size")
 
118
  with gr.Column():
119
  task_dropdown = gr.Dropdown(
120
  ["conditional", "intersection", "compose_add", "multihop"],
121
+ label="Single task", value="conditional",
122
  )
123
  run_one_btn = gr.Button("Run Single Task", variant="secondary")
124
  run_all_btn = gr.Button("Run ALL 4 Tasks", variant="primary")
 
125
  output_box = gr.Textbox(label="Results JSON", lines=20)
 
126
  run_one_btn.click(
127
  fn=run_fog_task,
128
  inputs=[task_dropdown, seeds_input, epochs_input, n_train_input, batch_input],
129
+ outputs=output_box, api_name="run_task",
 
130
  )
131
  run_all_btn.click(
132
  fn=run_all_tasks,
133
  inputs=[seeds_input, epochs_input, n_train_input, batch_input],
134
+ outputs=output_box, api_name="run_all",
 
135
  )
 
136
  health_btn = gr.Button("Health Check")
137
  health_output = gr.Textbox(label="Health", lines=3)
138
  health_btn.click(fn=health_check, outputs=health_output, api_name="health")