-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpipeline.py
More file actions
1332 lines (1140 loc) · 42.2 KB
/
pipeline.py
File metadata and controls
1332 lines (1140 loc) · 42.2 KB
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
End-to-end versioned pipeline orchestrator for Modal.
Chains all dataset-building steps (build datasets, build calibration
package, fit weights, build H5s, stage, promote) into a single
coordinated run with diagnostics, resume support, and atomic
promotion.
**Stability assumption**: This pipeline is designed for production
use when the target branch is stable and not expected to change
during the run. All steps clone from branch tip independently;
artifacts flow through the shared pipeline volume. The run's
metadata records the SHA at orchestrator start for auditability.
If the branch changes mid-run, intermediate artifacts may come
from different commits. For development branches that are actively
changing, run individual steps manually instead.
Usage:
# Full pipeline run
modal run --detach modal_app/pipeline.py::main \\
--action run --branch main --gpu T4 --epochs 200
# Check status
modal run modal_app/pipeline.py::main --action status
# Resume a failed run
modal run --detach modal_app/pipeline.py::main \\
--action run --resume-run-id <RUN_ID>
# Promote a completed run
modal run modal_app/pipeline.py::main \\
--action promote --run-id <RUN_ID>
"""
import json
import os
import subprocess
import sys
import time
import traceback
from dataclasses import asdict, dataclass, field
from datetime import datetime, timezone
from io import BytesIO
from pathlib import Path
from typing import Optional
import modal
_baked = "/root/policyengine-us-data"
_local = str(Path(__file__).resolve().parent.parent)
for _p in (_baked, _local):
if _p not in sys.path:
sys.path.insert(0, _p)
from modal_app.images import cpu_image as image
from modal_app.resilience import ensure_resume_sha_compatible
# ── Modal resources ──────────────────────────────────────────────
app = modal.App("policyengine-us-data-pipeline")
hf_secret = modal.Secret.from_name("huggingface-token")
gcp_secret = modal.Secret.from_name("gcp-credentials")
pipeline_volume = modal.Volume.from_name("pipeline-artifacts", create_if_missing=True)
staging_volume = modal.Volume.from_name("local-area-staging", create_if_missing=True)
REPO_URL = "https://github.com/PolicyEngine/policyengine-us-data.git"
PIPELINE_MOUNT = "/pipeline"
STAGING_MOUNT = "/staging"
ARTIFACTS_BASE = f"{PIPELINE_MOUNT}/artifacts"
RUNS_DIR = f"{PIPELINE_MOUNT}/runs"
def artifacts_dir_for_run(run_id: str) -> str:
"""Return the run-scoped artifacts directory.
When run_id is empty, falls back to the flat base path
for backward compatibility with standalone invocations.
"""
if run_id:
return f"{ARTIFACTS_BASE}/{run_id}"
return ARTIFACTS_BASE
# ── Run metadata ─────────────────────────────────────────────────
@dataclass
class RunMetadata:
"""Metadata for a pipeline run.
Tracks run identity, progress, and diagnostics for
auditability and resume support.
"""
run_id: str
branch: str
sha: str
version: str
start_time: str
status: str # running | completed | failed | promoted
step_timings: dict = field(default_factory=dict)
error: Optional[str] = None
resume_history: list = field(default_factory=list)
fingerprint: Optional[str] = None
def to_dict(self) -> dict:
return asdict(self)
@classmethod
def from_dict(cls, data: dict) -> "RunMetadata":
return cls(**data)
def generate_run_id(version: str, sha: str) -> str:
ts = datetime.now(timezone.utc).strftime("%Y%m%d_%H%M%S")
return f"{version}_{sha[:8]}_{ts}"
def write_run_meta(
meta: RunMetadata,
vol: modal.Volume,
) -> None:
"""Write run metadata to the pipeline volume."""
run_dir = Path(RUNS_DIR) / meta.run_id
run_dir.mkdir(parents=True, exist_ok=True)
meta_path = run_dir / "meta.json"
with open(meta_path, "w") as f:
json.dump(meta.to_dict(), f, indent=2)
vol.commit()
def read_run_meta(
run_id: str,
vol: modal.Volume,
) -> RunMetadata:
"""Read run metadata from the pipeline volume."""
vol.reload()
meta_path = Path(RUNS_DIR) / run_id / "meta.json"
if not meta_path.exists():
raise FileNotFoundError(f"No metadata found for run {run_id} at {meta_path}")
with open(meta_path) as f:
return RunMetadata.from_dict(json.load(f))
def get_pinned_sha(branch: str) -> str:
"""Get the current tip SHA for a branch from GitHub."""
result = subprocess.run(
[
"git",
"ls-remote",
REPO_URL,
f"refs/heads/{branch}",
],
capture_output=True,
text=True,
)
if result.returncode != 0:
raise RuntimeError(f"Failed to get SHA for branch {branch}: {result.stderr}")
line = result.stdout.strip()
if not line:
raise RuntimeError(f"Branch {branch} not found in remote")
return line.split()[0]
def get_version_from_branch(branch: str) -> str:
"""Get the package version from the pre-baked pyproject.toml.
The branch parameter is kept for API compatibility but is
no longer used -- version comes from the baked source.
"""
import tomllib
pyproject_path = "/root/policyengine-us-data/pyproject.toml"
with open(pyproject_path, "rb") as f:
pyproject = tomllib.load(f)
return pyproject["project"]["version"]
def archive_diagnostics(
run_id: str,
result_bytes: dict,
vol: modal.Volume,
prefix: str = "",
) -> None:
"""Archive calibration diagnostics to the run directory."""
diag_dir = Path(RUNS_DIR) / run_id / "diagnostics"
diag_dir.mkdir(parents=True, exist_ok=True)
file_map = {
"log": f"{prefix}unified_diagnostics.csv",
"cal_log": f"{prefix}calibration_log.csv",
"config": f"{prefix}unified_run_config.json",
}
for key, filename in file_map.items():
data = result_bytes.get(key)
if data:
path = diag_dir / filename
with open(path, "wb") as f:
f.write(data)
print(f" Archived {filename} ({len(data):,} bytes)")
vol.commit()
def _step_completed(meta: RunMetadata, step: str) -> bool:
"""Check if a step is marked completed in metadata."""
timing = meta.step_timings.get(step, {})
return timing.get("status") == "completed"
def find_resumable_run(branch: str, sha: str, vol: modal.Volume) -> Optional[str]:
"""Find an existing running run for the same branch+sha."""
vol.reload()
runs_dir = Path(RUNS_DIR)
if not runs_dir.exists():
return None
best_run_id = None
best_start = ""
for entry in runs_dir.iterdir():
if not entry.is_dir():
continue
meta_path = entry / "meta.json"
if not meta_path.exists():
continue
try:
with open(meta_path) as f:
data = json.load(f)
if (
data.get("branch") == branch
and data.get("sha") == sha
and data.get("status") == "running"
):
start = data.get("start_time", "")
if start > best_start:
best_start = start
best_run_id = data.get("run_id")
except (json.JSONDecodeError, KeyError):
continue
return best_run_id
def _record_step(
meta: RunMetadata,
step: str,
start: float,
vol: modal.Volume,
status: str = "completed",
) -> None:
"""Record step timing and status in metadata."""
meta.step_timings[step] = {
"start": datetime.fromtimestamp(start, tz=timezone.utc).isoformat(),
"end": datetime.now(timezone.utc).isoformat(),
"duration_s": round(time.time() - start, 1),
"status": status,
}
write_run_meta(meta, vol)
# ── Include other Modal apps ─────────────────────────────────────
# app.include() merges functions from other apps into this one,
# ensuring Modal mounts their files and registers their functions
# (with their GPU/memory/volume configs) in the ephemeral run.
# sys.path setup is handled at the top of this file.
from modal_app.data_build import app as _data_build_app
from modal_app.data_build import build_datasets
app.include(_data_build_app)
from modal_app.remote_calibration_runner import app as _calibration_app
from modal_app.remote_calibration_runner import (
build_package_remote,
PACKAGE_GPU_FUNCTIONS,
)
app.include(_calibration_app)
from modal_app.local_area import app as _local_area_app
from modal_app.local_area import (
coordinate_publish,
coordinate_national_publish,
promote_publish,
promote_national_publish,
)
app.include(_local_area_app)
# ── Stage base datasets ─────────────────────────────────────────
def _setup_repo() -> None:
"""Change to the pre-baked repo directory."""
os.chdir("/root/policyengine-us-data")
def stage_base_datasets(
run_id: str,
version: str,
branch: str,
) -> None:
"""Upload source_imputed + policy_data.db from pipeline
volume to HF staging/.
Clones the repo and shells out to upload_to_staging_hf()
via subprocess, consistent with other Modal apps.
Args:
run_id: The current run ID (for logging).
version: Package version string for the commit.
branch: Git branch for repo clone.
"""
artifacts = Path(artifacts_dir_for_run(run_id))
files_with_paths = []
# Stage all intermediate H5 datasets for lineage tracing
# source_imputed* goes to calibration/ (promote expects that path)
for h5_file in sorted(artifacts.glob("*.h5")):
if h5_file.name.startswith("source_imputed"):
repo_path = f"calibration/{h5_file.name}"
else:
repo_path = f"datasets/{h5_file.name}"
files_with_paths.append((str(h5_file), repo_path))
print(f" {h5_file.name} -> {repo_path}: {h5_file.stat().st_size:,} bytes")
policy_db = artifacts / "policy_data.db"
if policy_db.exists():
files_with_paths.append((str(policy_db), "calibration/policy_data.db"))
print(f" policy_data.db: {policy_db.stat().st_size:,} bytes")
else:
print(" WARNING: policy_data.db not found, skipping")
if not files_with_paths:
print(" No base datasets to stage")
return
_setup_repo()
# Build the upload script as a Python snippet
import json as _json
pairs_json = _json.dumps(files_with_paths)
result = subprocess.run(
[
"uv",
"run",
"python",
"-c",
f"""
import json
from policyengine_us_data.utils.data_upload import (
upload_to_staging_hf,
)
pairs = json.loads('''{pairs_json}''')
files_with_paths = [(p, r) for p, r in pairs]
count = upload_to_staging_hf(files_with_paths, "{version}", run_id="{run_id}")
print(f"Staged {{count}} base dataset(s) to HF")
""",
],
cwd="/root/policyengine-us-data",
text=True,
capture_output=True,
env=os.environ.copy(),
)
if result.returncode != 0:
raise RuntimeError(f"Base dataset staging failed: {result.stderr}")
print(f" {result.stdout.strip()}")
def upload_run_diagnostics(
run_id: str,
branch: str,
) -> None:
"""Upload run diagnostics to HF for archival.
Shells out via subprocess for consistency with other
Modal apps and to avoid package dependencies in the
orchestrator image.
Args:
run_id: The current run ID.
branch: Git branch for repo clone.
"""
diag_dir = Path(RUNS_DIR) / run_id / "diagnostics"
if not diag_dir.exists():
print(" No diagnostics to upload")
return
files = list(diag_dir.glob("*"))
if not files:
print(" No diagnostic files found")
return
print(f" Found {len(files)} diagnostic file(s) to upload")
# Build file list as JSON for the subprocess
import json as _json
file_entries = [
(str(f), f"calibration/runs/{run_id}/diagnostics/{f.name}") for f in files
]
entries_json = _json.dumps(file_entries)
_setup_repo()
result = subprocess.run(
[
"uv",
"run",
"python",
"-c",
f"""
import json, os
from huggingface_hub import HfApi
entries = json.loads('''{entries_json}''')
api = HfApi()
token = os.environ.get("HUGGING_FACE_TOKEN")
for local_path, repo_path in entries:
api.upload_file(
path_or_fileobj=local_path,
path_in_repo=repo_path,
repo_id="policyengine/policyengine-us-data",
repo_type="model",
token=token,
)
print(f"Uploaded {{repo_path}}")
""",
],
cwd="/root/policyengine-us-data",
capture_output=True,
text=True,
env=os.environ.copy(),
)
if result.returncode != 0:
raise RuntimeError(f"Diagnostics upload failed: {result.stderr}")
print(f" {result.stdout.strip()}")
def _write_validation_diagnostics(
run_id: str,
regional_result,
national_result,
meta: RunMetadata,
vol: modal.Volume,
) -> None:
"""Aggregate validation rows into a diagnostics CSV.
Extracts validation_rows from coordinate_publish and
national_validation from coordinate_national_publish,
writes them to runs/{run_id}/diagnostics/validation_results.csv,
and records a summary in meta.json.
"""
import csv
validation_rows = []
# Extract regional validation rows
if isinstance(regional_result, dict):
v_rows = regional_result.get("validation_rows", [])
if v_rows:
validation_rows.extend(v_rows)
print(f" Collected {len(v_rows)} regional validation rows")
# Extract national validation output
national_output = ""
if isinstance(national_result, dict):
national_output = national_result.get("national_validation", "")
if national_output:
print(" National validation output captured")
if not validation_rows and not national_output:
print(" No validation data to write")
return
diag_dir = Path(RUNS_DIR) / run_id / "diagnostics"
diag_dir.mkdir(parents=True, exist_ok=True)
# Write regional validation CSV
if validation_rows:
csv_columns = [
"area_type",
"area_id",
"district",
"variable",
"target_name",
"period",
"target_value",
"sim_value",
"error",
"rel_error",
"abs_error",
"rel_abs_error",
"sanity_check",
"sanity_reason",
"in_training",
]
csv_path = diag_dir / "validation_results.csv"
with open(csv_path, "w", newline="") as f:
writer = csv.DictWriter(f, fieldnames=csv_columns)
writer.writeheader()
for row in validation_rows:
writer.writerow({k: row.get(k, "") for k in csv_columns})
print(f" Wrote {len(validation_rows)} rows to {csv_path}")
# Compute summary
n_sanity_fail = sum(
1 for r in validation_rows if r.get("sanity_check") == "FAIL"
)
rae_vals = [
r["rel_abs_error"]
for r in validation_rows
if isinstance(r.get("rel_abs_error"), (int, float))
and r["rel_abs_error"] != float("inf")
]
mean_rae = sum(rae_vals) / len(rae_vals) if rae_vals else 0.0
# Per-area summaries for worst areas
area_stats = {}
for r in validation_rows:
key = f"{r.get('area_type', '')}:{r.get('area_id', '')}"
if key not in area_stats:
area_stats[key] = {"rae_vals": [], "fails": 0}
if r.get("sanity_check") == "FAIL":
area_stats[key]["fails"] += 1
rae = r.get("rel_abs_error")
if isinstance(rae, (int, float)) and rae != float("inf"):
area_stats[key]["rae_vals"].append(rae)
worst_areas = sorted(
area_stats.items(),
key=lambda x: (
sum(x[1]["rae_vals"]) / len(x[1]["rae_vals"]) if x[1]["rae_vals"] else 0
),
reverse=True,
)[:5]
validation_summary = {
"total_targets": len(validation_rows),
"sanity_failures": n_sanity_fail,
"mean_rel_abs_error": round(mean_rae, 4),
"worst_areas": [
{
"area": k,
"mean_rae": round(
(
sum(v["rae_vals"]) / len(v["rae_vals"])
if v["rae_vals"]
else 0
),
4,
),
"sanity_fails": v["fails"],
}
for k, v in worst_areas
],
}
print(
f" Validation summary: "
f"{len(validation_rows)} targets, "
f"{n_sanity_fail} sanity failures, "
f"mean RAE={mean_rae:.4f}"
)
# Record in meta.json
meta.step_timings["validation"] = validation_summary
write_run_meta(meta, vol)
# Write national validation output
if national_output:
nat_path = diag_dir / "national_validation.txt"
with open(nat_path, "w") as f:
f.write(national_output)
print(f" Wrote national validation to {nat_path}")
vol.commit()
# ── Orchestrator ─────────────────────────────────────────────────
@app.function(
image=image,
cpu=2,
memory=4096,
timeout=86400, # 24 hours (Modal max)
volumes={
PIPELINE_MOUNT: pipeline_volume,
STAGING_MOUNT: staging_volume,
},
secrets=[hf_secret, gcp_secret],
nonpreemptible=True,
)
def run_pipeline(
branch: str = "main",
gpu: str = "T4",
epochs: int = 1000,
national_gpu: str = "T4",
national_epochs: int = 4000,
num_workers: int = 50,
n_clones: int = 430,
skip_national: bool = False,
resume_run_id: str = None,
clear_checkpoints: bool = False,
version_override: str = "",
) -> str:
"""Run the full pipeline end-to-end.
Args:
branch: Git branch to build from.
gpu: GPU type for regional calibration.
epochs: Training epochs for regional calibration.
national_gpu: GPU type for national calibration.
national_epochs: Training epochs for national.
num_workers: Number of parallel H5 workers.
n_clones: Number of clones for H5 building.
skip_national: Skip national calibration/H5.
resume_run_id: Resume a previously failed run.
clear_checkpoints: Wipe ALL checkpoints before building
(default False). Normally not needed — checkpoints are
scoped by commit SHA, so stale ones from other commits
are cleaned automatically. Use True only to force a
full rebuild of the current commit.
Returns:
The run ID for use with promote.
"""
# ── Setup GCP credentials ──
creds_json = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS_JSON")
if creds_json:
creds_path = "/tmp/gcp-credentials.json"
with open(creds_path, "w") as f:
f.write(creds_json)
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = creds_path
# ── Initialize or resume run ──
sha = get_pinned_sha(branch)
version = version_override or get_version_from_branch(branch)
explicit_resume = bool(resume_run_id)
if not resume_run_id:
existing = find_resumable_run(branch, sha, pipeline_volume)
if existing:
print(f"Auto-resuming existing run {existing}")
resume_run_id = existing
if resume_run_id:
print(f"Resuming run {resume_run_id}...")
meta = read_run_meta(resume_run_id, pipeline_volume)
current_sha = sha
sha_match = ensure_resume_sha_compatible(
branch=branch,
run_sha=meta.sha,
current_sha=current_sha,
force=explicit_resume,
)
sha = meta.sha
version = meta.version
if not hasattr(meta, "resume_history") or meta.resume_history is None:
meta.resume_history = []
meta.resume_history.append(
{
"resumed_at": datetime.now(timezone.utc).isoformat(),
"code_sha": current_sha,
"original_sha": meta.sha,
"branch": branch,
"mixed_provenance": not sha_match,
}
)
meta.status = "running"
run_id = resume_run_id
else:
run_id = generate_run_id(version, sha)
meta = RunMetadata(
run_id=run_id,
branch=branch,
sha=sha,
version=version,
start_time=datetime.now(timezone.utc).isoformat(),
status="running",
)
# Create run directory
run_dir = Path(RUNS_DIR) / run_id
run_dir.mkdir(parents=True, exist_ok=True)
(run_dir / "diagnostics").mkdir(exist_ok=True)
# Create run-scoped artifacts directory
Path(artifacts_dir_for_run(run_id)).mkdir(parents=True, exist_ok=True)
write_run_meta(meta, pipeline_volume)
print("=" * 60)
print("PIPELINE RUN")
print("=" * 60)
print(f" Run ID: {run_id}")
print(f" Branch: {branch}")
print(f" SHA: {sha[:12]}")
print(f" Version: {version}")
print(f" GPU: {gpu} (regional)")
if not skip_national:
print(f" GPU: {national_gpu} (national)")
print(f" Epochs: {epochs}")
print(f" Workers: {num_workers}")
print(f" Clones: {n_clones}")
if resume_run_id:
completed = [
s for s, t in meta.step_timings.items() if t.get("status") == "completed"
]
print(f" Resume: skipping {completed}")
print("=" * 60)
try:
# ── Step 1: Build datasets ──
if not _step_completed(meta, "build_datasets"):
print("\n[Step 1/5] Building datasets...")
step_start = time.time()
build_datasets.remote(
upload=True,
branch=branch,
sequential=False,
clear_checkpoints=clear_checkpoints,
skip_tests=True,
skip_enhanced_cps=False,
run_id=run_id,
)
# The build_datasets step produces files in its
# own volume. Key outputs (source_imputed,
# policy_data.db) are staged to HF in step 4.
# TODO(#617): When pipeline_artifacts.py lands,
# call mirror_to_pipeline() here for audit trail.
_record_step(
meta,
"build_datasets",
step_start,
pipeline_volume,
)
print(
f" Completed in {meta.step_timings['build_datasets']['duration_s']}s"
)
else:
print("\n[Step 1/5] Build datasets (skipped - completed)")
# ── Step 2: Build calibration package ──
if not _step_completed(meta, "build_package"):
print("\n[Step 2/5] Building calibration package...")
step_start = time.time()
pkg_path = build_package_remote.remote(
branch=branch,
workers=num_workers,
n_clones=n_clones,
run_id=run_id,
)
print(f" Package at: {pkg_path}")
_record_step(
meta,
"build_package",
step_start,
pipeline_volume,
)
print(f" Completed in {meta.step_timings['build_package']['duration_s']}s")
else:
print("\n[Step 2/5] Build package (skipped - completed)")
# ── Step 3: Fit weights (parallel) ──
if not _step_completed(meta, "fit_weights"):
print("\n[Step 3/5] Fitting calibration weights...")
step_start = time.time()
vol_path = f"{artifacts_dir_for_run(run_id)}/calibration_package.pkl"
target_cfg = "policyengine_us_data/calibration/target_config.yaml"
# Spawn regional fit
regional_func = PACKAGE_GPU_FUNCTIONS[gpu]
print(f" Spawning regional fit ({gpu}, {epochs} epochs)...")
regional_handle = regional_func.spawn(
branch=branch,
epochs=epochs,
volume_package_path=vol_path,
target_config=target_cfg,
beta=0.65,
lambda_l0=1e-7,
lambda_l2=1e-8,
log_freq=500,
)
print(f" → regional fit fc: {regional_handle.object_id}")
# Spawn national fit (if enabled)
national_handle = None
if not skip_national:
national_func = PACKAGE_GPU_FUNCTIONS[national_gpu]
print(
f" Spawning national fit "
f"({national_gpu}, "
f"{national_epochs} epochs)..."
)
national_handle = national_func.spawn(
branch=branch,
epochs=national_epochs,
volume_package_path=vol_path,
target_config=target_cfg,
beta=0.65,
lambda_l0=1e-4,
lambda_l2=1e-12,
log_freq=500,
)
print(f" → national fit fc: {national_handle.object_id}")
# Collect regional results
print(" Waiting for regional fit...")
regional_result = regional_handle.get()
print(" Regional fit complete. Writing to volume...")
# Write regional results to pipeline volume (run-scoped)
artifacts_rel = f"artifacts/{run_id}" if run_id else "artifacts"
with pipeline_volume.batch_upload(force=True) as batch:
batch.put_file(
BytesIO(regional_result["weights"]),
f"{artifacts_rel}/calibration_weights.npy",
)
if regional_result.get("geography"):
batch.put_file(
BytesIO(regional_result["geography"]),
f"{artifacts_rel}/geography_assignment.npz",
)
if regional_result.get("config"):
batch.put_file(
BytesIO(regional_result["config"]),
f"{artifacts_rel}/unified_run_config.json",
)
archive_diagnostics(
run_id,
regional_result,
pipeline_volume,
prefix="",
)
# Collect national results
if national_handle is not None:
print(" Waiting for national fit...")
national_result = national_handle.get()
print(" National fit complete. Writing to volume...")
with pipeline_volume.batch_upload(force=True) as batch:
batch.put_file(
BytesIO(national_result["weights"]),
f"{artifacts_rel}/national_calibration_weights.npy",
)
if national_result.get("geography"):
batch.put_file(
BytesIO(national_result["geography"]),
f"{artifacts_rel}/national_geography_assignment.npz",
)
if national_result.get("config"):
batch.put_file(
BytesIO(national_result["config"]),
f"{artifacts_rel}/national_unified_run_config.json",
)
archive_diagnostics(
run_id,
national_result,
pipeline_volume,
prefix="national_",
)
_record_step(
meta,
"fit_weights",
step_start,
pipeline_volume,
)
print(f" Completed in {meta.step_timings['fit_weights']['duration_s']}s")
else:
print("\n[Step 3/5] Fit weights (skipped - completed)")
# ── Step 4: Build H5s + stage + diagnostics (parallel) ──
# 4a. coordinate_publish (regional H5s)
# 4b. coordinate_national_publish (national H5)
# 4c. stage_base_datasets (datasets → HF staging)
# 4d. upload_run_diagnostics (calibration diagnostics → HF)
# 4e. _write_validation_diagnostics (after H5 builds)
# 4f. upload_run_diagnostics (validation diagnostics → HF)
if not _step_completed(meta, "publish_and_stage"):
print(
"\n[Step 4/5] Building H5s, staging datasets, "
"uploading diagnostics (parallel)..."
)
step_start = time.time()
# Spawn H5 builds (run on separate Modal containers)
print(f" Spawning regional H5 build ({num_workers} workers)...")
regional_h5_handle = coordinate_publish.spawn(
branch=branch,
num_workers=num_workers,
skip_upload=False,
n_clones=n_clones,
validate=True,
run_id=run_id,
expected_fingerprint=meta.fingerprint or "",
)
print(f" → coordinate_publish fc: {regional_h5_handle.object_id}")
national_h5_handle = None
if not skip_national:
print(" Spawning national H5 build...")
national_h5_handle = coordinate_national_publish.spawn(
branch=branch,
n_clones=n_clones,
validate=True,
run_id=run_id,
)
print(
f" → coordinate_national_publish fc: {national_h5_handle.object_id}"
)
# While H5 builds run, stage base datasets in this container
pipeline_volume.reload()
print(" Staging base datasets to HF...")
stage_base_datasets(run_id, version, branch)
# Now wait for H5 builds to finish
print(" Waiting for regional H5 build...")
regional_h5_result = regional_h5_handle.get()
regional_msg = (
regional_h5_result.get("message", regional_h5_result)
if isinstance(regional_h5_result, dict)
else regional_h5_result
)
print(f" Regional H5: {regional_msg}")
if isinstance(regional_h5_result, dict) and regional_h5_result.get(
"fingerprint"
):
meta.fingerprint = regional_h5_result["fingerprint"]
write_run_meta(meta, pipeline_volume)
national_h5_result = None
if national_h5_handle is not None:
print(" Waiting for national H5 build...")
national_h5_result = national_h5_handle.get()
national_msg = (
national_h5_result.get("message", national_h5_result)
if isinstance(national_h5_result, dict)
else national_h5_result
)
print(f" National H5: {national_msg}")
# ── Aggregate validation results ──
_write_validation_diagnostics(
run_id=run_id,
regional_result=regional_h5_result,
national_result=national_h5_result,
meta=meta,
vol=pipeline_volume,
)
# Upload validation diagnostics (written after H5 builds)
print(" Uploading validation diagnostics...")
upload_run_diagnostics(run_id, branch)
_record_step(
meta,
"publish_and_stage",
step_start,
pipeline_volume,
)
print(
f" Completed in "
f"{meta.step_timings['publish_and_stage']['duration_s']}s"
)
else:
print("\n[Step 4/5] Publish + stage (skipped - completed)")
# ── Step 5: Finalize ──
print("\n[Step 5/5] Finalizing run...")
meta.status = "completed"
write_run_meta(meta, pipeline_volume)
print("\n" + "=" * 60)
print("PIPELINE COMPLETE")
print("=" * 60)
print(f" Run ID: {run_id}")
print(f" Status: {meta.status}")