diff --git a/docs/measurements/data/window-sync-agent-study.json b/docs/measurements/data/window-sync-agent-study.json new file mode 100644 index 00000000..ed7f9618 --- /dev/null +++ b/docs/measurements/data/window-sync-agent-study.json @@ -0,0 +1,925 @@ +{ + "solvers": [ + { + "trial": "control_1", + "first_correct": { + "turn": 1, + "production_sha256": "f8f2d019b2be32e9a3f46c5d255a6ae81ccac186cfffdb82e4f581b30e91514a", + "action": "run", + "grade": "pass", + "wall_seconds": 72.26386533401092, + "cli_seconds": 72.25965691701276, + "queue_seconds": 0.0, + "gpu_seconds": 0.0, + "total_tokens": 116693, + "input_tokens": 113370, + "cached_input_tokens": 102144, + "output_tokens": 3323, + "uncached_input_plus_output": 14549 + }, + "earlier_unknown_turns": [], + "final_source_grade": "pass", + "final_rank_0_profile": [ + { + "scenario": "fixed-regression", + "status": "pass", + "calls": 3, + "elements": 2175, + "ends": [ + 1, + 2, + 3 + ], + "issues": [] + }, + { + "scenario": "exact-uniform", + "status": "pass", + "calls": 6, + "elements": 1450, + "ends": [ + 2, + 4 + ], + "issues": [] + }, + { + "scenario": "overshoot-and-tail", + "status": "pass", + "calls": 9, + "elements": 2175, + "ends": [ + 2, + 4, + 6 + ], + "issues": [] + }, + { + "scenario": "rank-skew", + "status": "pass", + "calls": 9, + "elements": 2175, + "ends": [ + 2, + 4, + 5 + ], + "issues": [] + }, + { + "scenario": "single-window", + "status": "pass", + "calls": 3, + "elements": 725, + "ends": [ + 3 + ], + "issues": [] + }, + { + "scenario": "one-microbatch-updates", + "status": "pass", + "calls": 9, + "elements": 2175, + "ends": [ + 1, + 2, + 3 + ], + "issues": [] + } + ], + "submission_count": 3, + "source_history": [ + { + "turn": 1, + "sha256": "f8f2d019b2be32e9a3f46c5d255a6ae81ccac186cfffdb82e4f581b30e91514a", + "grade": "pass" + }, + { + "turn": 2, + "sha256": "f8f2d019b2be32e9a3f46c5d255a6ae81ccac186cfffdb82e4f581b30e91514a", + "grade": "pass" + }, + { + "turn": 3, + "sha256": "f8f2d019b2be32e9a3f46c5d255a6ae81ccac186cfffdb82e4f581b30e91514a", + "grade": "pass" + } + ], + "requests": [ + { + "request": 1, + "status": "ok", + "gpu_seconds": 24.422995259985328, + "entry": "diagnostic.py", + "diagnostic_lines": 174, + "optional_report_rows": 0, + "optional_passes": 0, + "receipt_sha256": "9c98b0f1fe15ad06e08910d9b1f1d9c38e83e6bbee59667735a7d07244b21729" + }, + { + "request": 2, + "status": "ok", + "gpu_seconds": 24.83332415483892, + "entry": "train.py", + "diagnostic_lines": null, + "optional_report_rows": 0, + "optional_passes": 0, + "receipt_sha256": "fbaceee6bfe8176e2fc2ea490282d9307b21ede04ffa96e48304bc0d3c0b3cd0" + } + ], + "terminal": { + "status": "finished", + "last_turn": 3, + "requests": 2, + "cli_seconds": 118.76127691700822, + "gpu_seconds": 49.25631941482425, + "queue_seconds": 11.949927139095962, + "elapsed_seconds": 157.49769233400002 + } + }, + { + "trial": "traincheck_1", + "first_correct": { + "turn": 1, + "production_sha256": "0f906c259d15fbdbb8851fd17ece864d6c411a56a9d3e431566174014c01512a", + "action": "run", + "grade": "pass", + "wall_seconds": 41.34760499998811, + "cli_seconds": 41.337401750002755, + "queue_seconds": 0.0, + "gpu_seconds": 0.0, + "total_tokens": 122357, + "input_tokens": 121182, + "cached_input_tokens": 105856, + "output_tokens": 1175, + "uncached_input_plus_output": 16501 + }, + "earlier_unknown_turns": [], + "final_source_grade": "pass", + "final_rank_0_profile": [ + { + "scenario": "fixed-regression", + "status": "pass", + "calls": 3, + "elements": 2175, + "ends": [ + 1, + 2, + 3 + ], + "issues": [] + }, + { + "scenario": "exact-uniform", + "status": "pass", + "calls": 6, + "elements": 1450, + "ends": [ + 2, + 4 + ], + "issues": [] + }, + { + "scenario": "overshoot-and-tail", + "status": "pass", + "calls": 9, + "elements": 2175, + "ends": [ + 2, + 4, + 6 + ], + "issues": [] + }, + { + "scenario": "rank-skew", + "status": "pass", + "calls": 9, + "elements": 2175, + "ends": [ + 2, + 4, + 5 + ], + "issues": [] + }, + { + "scenario": "single-window", + "status": "pass", + "calls": 3, + "elements": 725, + "ends": [ + 3 + ], + "issues": [] + }, + { + "scenario": "one-microbatch-updates", + "status": "pass", + "calls": 9, + "elements": 2175, + "ends": [ + 1, + 2, + 3 + ], + "issues": [] + } + ], + "submission_count": 3, + "source_history": [ + { + "turn": 1, + "sha256": "0f906c259d15fbdbb8851fd17ece864d6c411a56a9d3e431566174014c01512a", + "grade": "pass" + }, + { + "turn": 2, + "sha256": "0f906c259d15fbdbb8851fd17ece864d6c411a56a9d3e431566174014c01512a", + "grade": "pass" + }, + { + "turn": 3, + "sha256": "0f906c259d15fbdbb8851fd17ece864d6c411a56a9d3e431566174014c01512a", + "grade": "pass" + } + ], + "requests": [ + { + "request": 1, + "status": "ok", + "gpu_seconds": 24.925857543945312, + "entry": "diagnostic.py", + "diagnostic_lines": 7, + "optional_report_rows": 8, + "optional_passes": 8, + "receipt_sha256": "7f2335310815f3bcbed2e1294cade69e5fda83a1c72b98b4d6af70bd66f98f2f" + }, + { + "request": 2, + "status": "ok", + "gpu_seconds": 24.510619889944792, + "entry": "train.py", + "diagnostic_lines": null, + "optional_report_rows": 0, + "optional_passes": 0, + "receipt_sha256": "8d6be3eaae18bb031c478e4131aaeeb98b40f73772a387ae5820f0fa658c8894" + } + ], + "terminal": { + "status": "finished", + "last_turn": 3, + "requests": 2, + "cli_seconds": 65.40892941696802, + "gpu_seconds": 49.436477433890104, + "queue_seconds": 0.00024412851780653, + "elapsed_seconds": 97.39262808300555 + } + }, + { + "trial": "control_2", + "first_correct": { + "turn": 1, + "production_sha256": "0a81d4d1839ded62d09995ad23218a43b05ea4b77c30049c43952be49c2a9311", + "action": "run", + "grade": "pass", + "wall_seconds": 69.3139513330243, + "cli_seconds": 69.30932637501974, + "queue_seconds": 0.0, + "gpu_seconds": 0.0, + "total_tokens": 100290, + "input_tokens": 96905, + "cached_input_tokens": 73472, + "output_tokens": 3385, + "uncached_input_plus_output": 26818 + }, + "earlier_unknown_turns": [], + "final_source_grade": "pass", + "final_rank_0_profile": [ + { + "scenario": "fixed-regression", + "status": "pass", + "calls": 3, + "elements": 2175, + "ends": [ + 1, + 2, + 3 + ], + "issues": [] + }, + { + "scenario": "exact-uniform", + "status": "pass", + "calls": 6, + "elements": 1450, + "ends": [ + 2, + 4 + ], + "issues": [] + }, + { + "scenario": "overshoot-and-tail", + "status": "pass", + "calls": 9, + "elements": 2175, + "ends": [ + 2, + 4, + 6 + ], + "issues": [] + }, + { + "scenario": "rank-skew", + "status": "pass", + "calls": 9, + "elements": 2175, + "ends": [ + 2, + 4, + 5 + ], + "issues": [] + }, + { + "scenario": "single-window", + "status": "pass", + "calls": 3, + "elements": 725, + "ends": [ + 3 + ], + "issues": [] + }, + { + "scenario": "one-microbatch-updates", + "status": "pass", + "calls": 9, + "elements": 2175, + "ends": [ + 1, + 2, + 3 + ], + "issues": [] + } + ], + "submission_count": 3, + "source_history": [ + { + "turn": 1, + "sha256": "0a81d4d1839ded62d09995ad23218a43b05ea4b77c30049c43952be49c2a9311", + "grade": "pass" + }, + { + "turn": 2, + "sha256": "0a81d4d1839ded62d09995ad23218a43b05ea4b77c30049c43952be49c2a9311", + "grade": "pass" + }, + { + "turn": 3, + "sha256": "0a81d4d1839ded62d09995ad23218a43b05ea4b77c30049c43952be49c2a9311", + "grade": "pass" + } + ], + "requests": [ + { + "request": 1, + "status": "ok", + "gpu_seconds": 24.70588657259941, + "entry": "diagnostic.py", + "diagnostic_lines": 169, + "optional_report_rows": 0, + "optional_passes": 0, + "receipt_sha256": "06c51d692205e3f95a0c07244d3393c19afa9d75cf6bd292d338a2c66a1188b8" + }, + { + "request": 2, + "status": "ok", + "gpu_seconds": 24.91571350209415, + "entry": "train.py", + "diagnostic_lines": null, + "optional_report_rows": 0, + "optional_passes": 0, + "receipt_sha256": "2159ab1568cdebb6905895f1ea2ece9baf5b534a529a63808e48acf1cc20f7b3" + } + ], + "terminal": { + "status": "finished", + "last_turn": 3, + "requests": 2, + "cli_seconds": 96.54488545801723, + "gpu_seconds": 49.62160007469356, + "queue_seconds": 2.3521357281133533, + "elapsed_seconds": 126.36538145801751 + } + }, + { + "trial": "traincheck_2", + "first_correct": { + "turn": 1, + "production_sha256": "f977dcfea2018817a7c725090229785b0610dbbf4c303b0f2544be8b6ce0ee10", + "action": "run", + "grade": "pass", + "wall_seconds": 34.40014883299591, + "cli_seconds": 34.38992600000347, + "queue_seconds": 0.0, + "gpu_seconds": 0.0, + "total_tokens": 106146, + "input_tokens": 105035, + "cached_input_tokens": 92160, + "output_tokens": 1111, + "uncached_input_plus_output": 13986 + }, + "earlier_unknown_turns": [], + "final_source_grade": "pass", + "final_rank_0_profile": [ + { + "scenario": "fixed-regression", + "status": "pass", + "calls": 3, + "elements": 2175, + "ends": [ + 1, + 2, + 3 + ], + "issues": [] + }, + { + "scenario": "exact-uniform", + "status": "pass", + "calls": 6, + "elements": 1450, + "ends": [ + 2, + 4 + ], + "issues": [] + }, + { + "scenario": "overshoot-and-tail", + "status": "pass", + "calls": 9, + "elements": 2175, + "ends": [ + 2, + 4, + 6 + ], + "issues": [] + }, + { + "scenario": "rank-skew", + "status": "pass", + "calls": 9, + "elements": 2175, + "ends": [ + 2, + 4, + 5 + ], + "issues": [] + }, + { + "scenario": "single-window", + "status": "pass", + "calls": 3, + "elements": 725, + "ends": [ + 3 + ], + "issues": [] + }, + { + "scenario": "one-microbatch-updates", + "status": "pass", + "calls": 9, + "elements": 2175, + "ends": [ + 1, + 2, + 3 + ], + "issues": [] + } + ], + "submission_count": 3, + "source_history": [ + { + "turn": 1, + "sha256": "f977dcfea2018817a7c725090229785b0610dbbf4c303b0f2544be8b6ce0ee10", + "grade": "pass" + }, + { + "turn": 2, + "sha256": "f977dcfea2018817a7c725090229785b0610dbbf4c303b0f2544be8b6ce0ee10", + "grade": "pass" + }, + { + "turn": 3, + "sha256": "f977dcfea2018817a7c725090229785b0610dbbf4c303b0f2544be8b6ce0ee10", + "grade": "pass" + } + ], + "requests": [ + { + "request": 1, + "status": "ok", + "gpu_seconds": 24.615113066509366, + "entry": "diagnostic.py", + "diagnostic_lines": 7, + "optional_report_rows": 8, + "optional_passes": 8, + "receipt_sha256": "463bf5ac2aef3ba89779a4eca7542a4e58f89bb57956f2fb0e7d99d2c0f76c67" + }, + { + "request": 2, + "status": "ok", + "gpu_seconds": 25.00811085291207, + "entry": "train.py", + "diagnostic_lines": null, + "optional_report_rows": 0, + "optional_passes": 0, + "receipt_sha256": "68bdf6dd95b1f42ab7d16a7a9b08047c702604dfdb7352969e06d0da0a5fe3bf" + } + ], + "terminal": { + "status": "finished", + "last_turn": 3, + "requests": 2, + "cli_seconds": 56.48828012502054, + "gpu_seconds": 49.623223919421434, + "queue_seconds": 0.00023611914366483688, + "elapsed_seconds": 83.55747370800236 + } + }, + { + "trial": "control_3", + "first_correct": { + "turn": 1, + "production_sha256": "8a1e5b8907f5653090a58c7a7c1cbd19c1ed0ee45da26048210712a64a3189d4", + "action": "run", + "grade": "pass", + "wall_seconds": 35.59374929202022, + "cli_seconds": 35.59065579198068, + "queue_seconds": 0.0, + "gpu_seconds": 0.0, + "total_tokens": 74408, + "input_tokens": 73253, + "cached_input_tokens": 64768, + "output_tokens": 1155, + "uncached_input_plus_output": 9640 + }, + "earlier_unknown_turns": [], + "final_source_grade": "pass", + "final_rank_0_profile": [ + { + "scenario": "fixed-regression", + "status": "pass", + "calls": 3, + "elements": 2175, + "ends": [ + 1, + 2, + 3 + ], + "issues": [] + }, + { + "scenario": "exact-uniform", + "status": "pass", + "calls": 6, + "elements": 1450, + "ends": [ + 2, + 4 + ], + "issues": [] + }, + { + "scenario": "overshoot-and-tail", + "status": "pass", + "calls": 9, + "elements": 2175, + "ends": [ + 2, + 4, + 6 + ], + "issues": [] + }, + { + "scenario": "rank-skew", + "status": "pass", + "calls": 9, + "elements": 2175, + "ends": [ + 2, + 4, + 5 + ], + "issues": [] + }, + { + "scenario": "single-window", + "status": "pass", + "calls": 3, + "elements": 725, + "ends": [ + 3 + ], + "issues": [] + }, + { + "scenario": "one-microbatch-updates", + "status": "pass", + "calls": 9, + "elements": 2175, + "ends": [ + 1, + 2, + 3 + ], + "issues": [] + } + ], + "submission_count": 3, + "source_history": [ + { + "turn": 1, + "sha256": "8a1e5b8907f5653090a58c7a7c1cbd19c1ed0ee45da26048210712a64a3189d4", + "grade": "pass" + }, + { + "turn": 2, + "sha256": "8a1e5b8907f5653090a58c7a7c1cbd19c1ed0ee45da26048210712a64a3189d4", + "grade": "pass" + }, + { + "turn": 3, + "sha256": "8a1e5b8907f5653090a58c7a7c1cbd19c1ed0ee45da26048210712a64a3189d4", + "grade": "pass" + } + ], + "requests": [ + { + "request": 1, + "status": "ok", + "gpu_seconds": 24.44185870885849, + "entry": "train.py", + "diagnostic_lines": null, + "optional_report_rows": 0, + "optional_passes": 0, + "receipt_sha256": "2ab5d0ccf10e218e49b0177003650d26e2856e602f53fc0eaf10f7dc9c05a5e4" + }, + { + "request": 2, + "status": "ok", + "gpu_seconds": 25.02269485220313, + "entry": "diagnostic.py", + "diagnostic_lines": 198, + "optional_report_rows": 0, + "optional_passes": 0, + "receipt_sha256": "905bf0d2112503ddf4dbd8a6988c99eafbdf130d8aa89a16da4da4b332128bcf" + } + ], + "terminal": { + "status": "finished", + "last_turn": 3, + "requests": 2, + "cli_seconds": 109.92009245898225, + "gpu_seconds": 49.46455356106162, + "queue_seconds": 0.00024039112031459808, + "elapsed_seconds": 136.79565404201276 + } + }, + { + "trial": "traincheck_3", + "first_correct": { + "turn": 1, + "production_sha256": "9efcd66642400b51bb77d4e0f8f04169a6b9639baa735970271180bb17a47334", + "action": "run", + "grade": "pass", + "wall_seconds": 63.20685283301282, + "cli_seconds": 63.20023679101723, + "queue_seconds": 0.0, + "gpu_seconds": 0.0, + "total_tokens": 100482, + "input_tokens": 97437, + "cached_input_tokens": 76160, + "output_tokens": 3045, + "uncached_input_plus_output": 24322 + }, + "earlier_unknown_turns": [], + "final_source_grade": "pass", + "final_rank_0_profile": [ + { + "scenario": "fixed-regression", + "status": "pass", + "calls": 3, + "elements": 2175, + "ends": [ + 1, + 2, + 3 + ], + "issues": [] + }, + { + "scenario": "exact-uniform", + "status": "pass", + "calls": 6, + "elements": 1450, + "ends": [ + 2, + 4 + ], + "issues": [] + }, + { + "scenario": "overshoot-and-tail", + "status": "pass", + "calls": 9, + "elements": 2175, + "ends": [ + 2, + 4, + 6 + ], + "issues": [] + }, + { + "scenario": "rank-skew", + "status": "pass", + "calls": 9, + "elements": 2175, + "ends": [ + 2, + 4, + 5 + ], + "issues": [] + }, + { + "scenario": "single-window", + "status": "pass", + "calls": 3, + "elements": 725, + "ends": [ + 3 + ], + "issues": [] + }, + { + "scenario": "one-microbatch-updates", + "status": "pass", + "calls": 9, + "elements": 2175, + "ends": [ + 1, + 2, + 3 + ], + "issues": [] + } + ], + "submission_count": 3, + "source_history": [ + { + "turn": 1, + "sha256": "9efcd66642400b51bb77d4e0f8f04169a6b9639baa735970271180bb17a47334", + "grade": "pass" + }, + { + "turn": 2, + "sha256": "9efcd66642400b51bb77d4e0f8f04169a6b9639baa735970271180bb17a47334", + "grade": "pass" + }, + { + "turn": 3, + "sha256": "9efcd66642400b51bb77d4e0f8f04169a6b9639baa735970271180bb17a47334", + "grade": "pass" + } + ], + "requests": [ + { + "request": 1, + "status": "ok", + "gpu_seconds": 24.31849431246519, + "entry": "diagnostic.py", + "diagnostic_lines": 164, + "optional_report_rows": 0, + "optional_passes": 0, + "receipt_sha256": "50b928ba6f09d885c51fd1ae7b788bec87889c29c1b87740528881a3ba52fd4e" + }, + { + "request": 2, + "status": "ok", + "gpu_seconds": 24.510023187845945, + "entry": "train.py", + "diagnostic_lines": null, + "optional_report_rows": 0, + "optional_passes": 0, + "receipt_sha256": "002cd237257415e26d14e91a351e61d37d8f40795c686835d7c172c07597e4a2" + } + ], + "terminal": { + "status": "finished", + "last_turn": 3, + "requests": 2, + "cli_seconds": 96.18410399902496, + "gpu_seconds": 48.828517500311136, + "queue_seconds": 0.00023323297500610352, + "elapsed_seconds": 122.89311654202174 + } + } + ], + "replayed_arm_sources": [ + { + "receipt_sha256": "b7d0ff44b9d6b35beaabcb35eb0c6cd0a79665ee0fa29249729b0d119245e24a", + "scope": "control", + "source_sha256": "0a81d4d1839ded62d09995ad23218a43b05ea4b77c30049c43952be49c2a9311", + "status": "pass", + "rows": 12, + "gpu_seconds": 24.816783472895622 + }, + { + "receipt_sha256": "2d3d9ada0f6c213cf1b26254802569d1a38cc4e07cb94878ef20fbcf44faf5b2", + "scope": "control", + "source_sha256": "8a1e5b8907f5653090a58c7a7c1cbd19c1ed0ee45da26048210712a64a3189d4", + "status": "pass", + "rows": 12, + "gpu_seconds": 25.208435144275427 + }, + { + "receipt_sha256": "cb7495dc7c4d1b77a97790b0bc5aac7713a63e9b515285836e340ea39fe4b4f1", + "scope": "control", + "source_sha256": "ddc92d2404869c94eb83420998846432f2027a78b4177b1ddd9d802a1459c97e", + "status": "communication_contract", + "rows": 12, + "gpu_seconds": 25.136492554098368 + }, + { + "receipt_sha256": "93052ab1336693aa38fe6ecb74d5ecbdd878224c255f9e7a5a1c4cbc7323119d", + "scope": "control", + "source_sha256": "f8f2d019b2be32e9a3f46c5d255a6ae81ccac186cfffdb82e4f581b30e91514a", + "status": "pass", + "rows": 12, + "gpu_seconds": 25.307066220790148 + }, + { + "receipt_sha256": "a140b26fab919706c8916058feed758bc9ea311367e6150b800fef6b96df12ca", + "scope": "traincheck", + "source_sha256": "0f906c259d15fbdbb8851fd17ece864d6c411a56a9d3e431566174014c01512a", + "status": "pass", + "rows": 12, + "gpu_seconds": 25.009339779615402 + }, + { + "receipt_sha256": "eb5b0547255d511b591845602968d8a69e86a5645bce849e32cd9adb1c533f1e", + "scope": "traincheck", + "source_sha256": "9efcd66642400b51bb77d4e0f8f04169a6b9639baa735970271180bb17a47334", + "status": "pass", + "rows": 12, + "gpu_seconds": 24.910277787595987 + }, + { + "receipt_sha256": "2be8cf147e4ea4df796522043b03c2f65078db8038346d853a53635c8dc02fd3", + "scope": "traincheck", + "source_sha256": "ddc92d2404869c94eb83420998846432f2027a78b4177b1ddd9d802a1459c97e", + "status": "communication_contract", + "rows": 12, + "gpu_seconds": 24.800809390842915 + }, + { + "receipt_sha256": "0223d8f0c298458742c85b38aa84cec382a48060bb00e314033d1d828bf516a4", + "scope": "traincheck", + "source_sha256": "f977dcfea2018817a7c725090229785b0610dbbf4c303b0f2544be8b6ce0ee10", + "status": "pass", + "rows": 12, + "gpu_seconds": 25.114072144031525 + } + ], + "gpu_seconds": { + "qualification": 125.1371352262795, + "solvers": 296.2306919042021, + "private_grading": 200.3032764941454 + }, + "gpu_seconds_all": 621.671103624627, + "raw_evidence_sha256": { + "native-qualification.json": "fc458a901f9eacc2594bf06fa988181fe8dfe1cd58bc4750a1a063c2330671c3", + "native-launch.json": "48796ff8880659a79fc386f50963419f930ac10e5013e25d8549b2838dce812d", + "stage-manifest.json": "3cb9db16a782554e9da6ee3f19bf85b9f965175c9f5bd58d7805f742158a0398", + "remote-environment.txt": "cc3dfaa597d84853e7096dac816798ccb14c180af5b104c01a1f1ce4c84e0099", + "first-valid-costs.json": "aaab32ca196c77f88a1c859300a840d7f20b310c675a320cdd04bd6d53e25f73", + "private-grading-summary.json": "8000b3a5edcbff77c6d0ac2d26659287998d6a0ce4fd1c66d85071e64dc4f533", + "private-replay-inventory.json": "64305c4f03ce52e5759a46f84a2ff55ef756447d13e100abfa71b417109f04a6" + } +} diff --git a/docs/measurements/window-sync-agent-study.md b/docs/measurements/window-sync-agent-study.md new file mode 100644 index 00000000..45439115 --- /dev/null +++ b/docs/measurements/window-sync-agent-study.md @@ -0,0 +1,142 @@ +# Delaying DDP gradient synchronization to token-window boundaries + +Three new matched agent pairs start with the same working streaming DDP loop. +The requested improvement is to delay gradient communication until each token +window ends. The task preserves the numeric trajectory and streaming input +contract, including epoch tails. One arm receives a documented optional +two-line TrainCheck diagnostic. The adapter runs public examples, profiles +native NCCL all-reduces and uses TrainCheck fingerprints for final replica +agreement. This compares access to the complete optional diagnostic workflow, +including its new task adapter. Both arms may write diagnostics and receive +the same GPU request, CLI and decision budgets. The task does not hint at a +pre-existing bug or a preferred implementation. + +The operator stores each submitted production file and samples intermediate +edits. Native CLI input, cached input, output and elapsed time are kept apart +from GPU usage. First valid submission means the first frozen decision whose +code later passes every held-out check. Its cost ends before any GPU run +requested by that decision. Agent verification and independent replay are +separate events and GPU costs. Report time, token use and GPU cost for each +pair. Count submitted diagnostic source lines for each agent, including short +entry points that call the optional checker. Audit execution using actual +check output, not filename or documentation reads alone. + +The prior [token budget study](token-budget-agent-study-20261118.md) supplies +the workload, independent concatenated-batch reference and frozen numerical +scenarios. The augmented scorer checks pre-step gradients, post-step parameters +and momentum, window boundaries, and repeated or early gradient communication. +It uses CPU profiler records emitted by the underlying NCCL process group with +input shape recording active. The workload uses single-element all-reduces for +token counts. Larger all-reduces carry gradient data. Compare the aggregate +gradient element count with the full trainable gradient payload at each step. +Separate buckets or parameters may be reduced individually. Each reduction +must occur following the final backward marker of that window and precede the +optimizer step. The measured quantities are NCCL launches and reduced tensor +elements for this workload's DDP reducer and ordinary tensor all-reduce +implementations. + +Native qualification compares the working per-microbatch-sync starter, a +correct once-per-window implementation, and a private variant omitting +synchronization in the final partial window. The optional public check uses +separately specified public cases and prints evidence on each rank. Private +replay uses all six frozen scenarios, and is withheld until the solvers finish. +The low-level collective parser is shared, so controls verify its positive +and negative behavior. This is a small workflow pilot, with no model-training +performance claim. + +## Results + +All six agents first submitted a valid implementation in turn one and +delivered the same production file in turn three. Both ranks passed all six +held-out scenarios for every solver implementation. No solver delivered +a silent numerical failure or an early-gradient-communication violation. +The sampled edit histories contain only the working starter and one solver +implementation per trial, and no agent made a production edit after seeing a +GPU diagnostic result. Source samples can miss brief intermediate edits. + +| Pair | Arm | Diagnostic lines | TrainCheck cases passed (both ranks) | First correct elapsed s | First correct total tokens | First correct uncached input + output | Delivery elapsed s | Delivery GPU-s | +| --- | --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | +| 1 | Control | 174 | 0 | 72.26 | 116,693 | 14,549 | 157.50 | 49.256 | +| 1 | Optional TrainCheck | 7 | 4 | 41.35 | 122,357 | 16,501 | 97.39 | 49.436 | +| 2 | Control | 169 | 0 | 69.31 | 100,290 | 26,818 | 126.37 | 49.622 | +| 2 | Optional TrainCheck | 7 | 4 | 34.40 | 106,146 | 13,986 | 83.56 | 49.623 | +| 3 | Control | 198 | 0 | 35.59 | 74,408 | 9,640 | 136.80 | 49.465 | +| 3 | Optional TrainCheck | 164 | 0 | 63.21 | 100,482 | 24,322 | 122.89 | 48.829 | + +All six correct submissions predate the first GPU request, so all first +correct GPU costs are zero. Total tokens count cached input once. Each agent +used one diagnostic request and one ordinary example run. Timings and token +counts reflect three pairs with independently chosen diagnostics and queue +effects. + +Two of three optional-arm agents opened the guide and ran the provided check. +Their seven-line entry points replaced 174 and 169 lines of handwritten +diagnostic code in their paired controls, a reduction of about 96% in +submitted diagnostic lines. Each executed all four public cases successfully +on both ranks, including the partial tail. The third optional agent did not +open the guide in the recorded operations and wrote 164 lines for its own +diagnostic. That pair’s control wrote 198. The custom diagnostics chose their +own inputs and coverage. The line counts describe authored scaffolding, +with equal frozen private scoring across all six implementations. + +The two adopters reached first correct submission faster in their pairs. +The third available-tool run was slower to first correct submission. All +three optional runs had higher first-correct total token counts than their +controls. Uncached input plus output moved in both directions. GPU usage +through delivery was close because all six made the same number of requests. + +## Communication results + +All six agents used no_sync during token-mode accumulation, then issued +one all-reduce per parameter at each window boundary, including the partial +tail. In the held-out overshoot-and-tail case, at each rank the six +microbatches became updates ending at microbatches 2, 4, and 6. Under the +working starter, the rank reduced 4,350 gradient elements in six NCCL calls. +Every solver reduced 2,175 elements in nine NCCL calls. The operator’s +packed positive control reduced the same 2,175 elements in three calls. +Scalar reductions for token counts are separate from these numbers. + +The new task allowed separate all-reduces per parameter or bucket at a +window boundary. This yielded correct values and halved the gradient +payload in that example, while dispatching 50% more gradient collectives. +The launch count and reduced tensor-element count are actual native +profiler observations. Throughput, link traffic and collective latency +were outside the experiment. + +The private tail-drop control produced numerical errors in all three +scenarios with a partial tail. With one epoch-wide partial window it +emitted zero gradient reductions. The original per-microbatch starter +matched the numerical reference and failed the communication contract +where a window spanned multiple microbatches. These controls test distinct +ways the optimization can appear correct under a narrow check. + +## Reproduction and evidence + +Ring18 used devices 4 and 5 (NVIDIA A16, driver 535.309.01), Python 3.10.14 +and PyTorch 2.2.2+cu121. Both arms ran the same native Codex 0.160.0 model +settings, requested gpt-6-astra with high reasoning and priority service. +Two agents worked concurrently per pair. Their GPU requests were serialized. +The scope relied on instructions. All six read a generic host policy skill and +four also read its generic schema. No reads of peer solutions or private +grading code were observed in the recorded commands. + +Evidence is stored in outputs/ring18-window-sync-study/ in the canonical +checkout and in /home/yuxuan/experiments/traincheck-window-sync-study on +Ring18. The 18 decision submissions and sampled source states required +eight replays, counting the unchanged starter separately in each arm. +Across those replays the evaluator ran 48 two-rank scenarios and recorded +96 rank outcomes. The GPU watcher verified release after each allocation. + +| GPU use | Allocated GPU-seconds | +| --- | ---: | +| Native qualification and profiler probe | 125.137 | +| Six solver trials | 296.231 | +| Independent grading | 200.303 | +| Total | 621.671 | + +The [per-trial results](data/window-sync-agent-study.json) preserve exact +costs, tool adoption evidence, communication profiles and receipt +hashes. The private replay and cost join run with the repository’s +tools.window_sync_study.replay and tools.token_budget_study.operator.score_submissions. +Twenty-three focused local tests passed, along with the scoped mypy and +configured pre-commit checks. diff --git a/tools/token_budget_study/operator/agent_trials.py b/tools/token_budget_study/operator/agent_trials.py index c745ea90..078a9f30 100644 --- a/tools/token_budget_study/operator/agent_trials.py +++ b/tools/token_budget_study/operator/agent_trials.py @@ -98,7 +98,9 @@ class Arm: packet: Path -def prepare(evidence: Path, packets: Path, optional: Path, manifest: Path) -> None: +def prepare( + evidence: Path, packets: Path, optional: Path, manifest: Path, public: Path = PUBLIC +) -> None: packet_root = packets.resolve() packet_root.mkdir(parents=True, exist_ok=False) evidence.mkdir(parents=True, exist_ok=True) @@ -116,7 +118,7 @@ def prepare(evidence: Path, packets: Path, optional: Path, manifest: Path) -> No packet = packet_root / secrets.token_hex(8) packet.mkdir() for filename in PUBLIC_FILES: - shutil.copyfile(PUBLIC / filename, packet / filename) + shutil.copyfile(public / filename, packet / filename) if treatment: shutil.copytree( optional, @@ -149,7 +151,7 @@ def prepare(evidence: Path, packets: Path, optional: Path, manifest: Path) -> No "max_turns": MAX_TURNS, "cli_seconds": CLI_BUDGET, "worker_seconds": 120, - "public_hashes": {f: digest(PUBLIC / f) for f in PUBLIC_FILES}, + "public_hashes": {f: digest(public / f) for f in PUBLIC_FILES}, "optional_manifest_sha256": digest(manifest), "entry_sha256": digest(Path(__file__)), "service_sha256": digest(SERVICE), @@ -394,6 +396,7 @@ def main() -> None: parser.add_argument("--packets", type=Path) parser.add_argument("--optional", type=Path) parser.add_argument("--optional-manifest", type=Path) + parser.add_argument("--public", type=Path, default=PUBLIC) args = parser.parse_args() if args.action == "prepare": if ( @@ -404,7 +407,13 @@ def main() -> None: parser.error( "prepare requires --packets, --optional and --optional-manifest" ) - prepare(args.evidence, args.packets, args.optional, args.optional_manifest) + prepare( + args.evidence, + args.packets, + args.optional, + args.optional_manifest, + args.public, + ) else: run(args.evidence) diff --git a/tools/token_budget_study/operator/ring_service.py b/tools/token_budget_study/operator/ring_service.py index 2a662400..658d9722 100644 --- a/tools/token_budget_study/operator/ring_service.py +++ b/tools/token_budget_study/operator/ring_service.py @@ -15,7 +15,8 @@ from pathlib import Path from typing import TypedDict -REMOTE = Path("/home/yuxuan/experiments/traincheck-token-budget-20261118") +DEFAULT_REMOTE = Path("/home/yuxuan/experiments/traincheck-token-budget-20261118") +REMOTE = DEFAULT_REMOTE PYTHON = ( "/home/yuxuan/experiments/traincheck-token-normalization-20261102/venv/bin/python" ) @@ -185,6 +186,9 @@ def run_worker(req: Request, run: Path, source: Path) -> dict[str, object]: ] if req["with_traincheck"]: command.extend(["--optional-package", str(REMOTE / "optional/traincheck")]) + collective_audit = REMOTE / "private/collectives.py" + if collective_audit.is_file(): + command.extend(["--collective-audit", str(collective_audit)]) env = os.environ.copy() env.update( TRAINCHECK_STUDY_E_REQUEST=req["id"], @@ -299,6 +303,11 @@ def client() -> None: parser.add_argument("--args-json", default="[]") parser.add_argument("--with-traincheck", action="store_true") args = parser.parse_args() + remote = Path(os.environ.get("TRAINCHECK_STUDY_ROOT", str(DEFAULT_REMOTE))) + if remote.parent != DEFAULT_REMOTE.parent or not re.fullmatch( + r"traincheck-[a-z0-9-]+", remote.name + ): + raise ValueError("Expected a named TrainCheck experiment directory") files: dict[str, str] = {} for name in ("training.py", "diagnostic.py"): path = args.source / name @@ -322,7 +331,7 @@ def client() -> None: (args.output / "request.json").write_bytes(payload) since = time.monotonic() proc = subprocess.run( - [*SSH, PYTHON, str(REMOTE / "ring_service.py"), "serve"], + [*SSH, PYTHON, str(remote / "ring_service.py"), "serve"], input=payload, capture_output=True, timeout=1800, @@ -353,6 +362,7 @@ def client() -> None: if __name__ == "__main__": if sys.argv[1:] == ["serve"]: + REMOTE = Path(__file__).resolve().parent print(json.dumps(serve(parse_request(json.load(sys.stdin))))) else: client() diff --git a/tools/token_budget_study/operator/score_submissions.py b/tools/token_budget_study/operator/score_submissions.py index d340fb29..19478823 100644 --- a/tools/token_budget_study/operator/score_submissions.py +++ b/tools/token_budget_study/operator/score_submissions.py @@ -19,6 +19,7 @@ "one-microbatch-updates", } OUTCOMES = { + "communication_contract", "pass", "silent_wrong", "unsupported", diff --git a/tools/token_budget_study/private/grade.py b/tools/token_budget_study/private/grade.py index b270ccdd..6f267f27 100644 --- a/tools/token_budget_study/private/grade.py +++ b/tools/token_budget_study/private/grade.py @@ -30,6 +30,7 @@ "nonfinite", "runtime_error", "unsupported", + "communication_contract", } @@ -39,6 +40,7 @@ def grade( device: str, timeout: float, optional_package: Path | None, + collective_audit: Path | None = None, ) -> None: output.parent.mkdir(parents=True, exist_ok=True) # Reserve the path before allocating any worker time. @@ -49,6 +51,8 @@ def grade( for name in ("training.py", "workload.py"): shutil.copyfile(source / name, scratch / name) shutil.copyfile(Path(__file__).with_name("worker.py"), scratch / "worker.py") + if collective_audit is not None: + shutil.copyfile(collective_audit, scratch / "collectives.py") package_files: dict[str, str] = {} if optional_package is not None: if ( @@ -80,6 +84,8 @@ def grade( "--device", device, ] + if collective_audit is not None: + command.append("--audit-communication") with (scratch / "worker.log").open("wb") as log: proc = subprocess.Popen( command, @@ -141,6 +147,7 @@ def grade( "nonfinite", "silent_wrong", "memory_contract", + "communication_contract", ) if x in statuses ), @@ -173,6 +180,7 @@ def main() -> None: parser.add_argument("--device", choices=["cpu", "cuda"], default="cuda") parser.add_argument("--timeout", type=float, default=90) parser.add_argument("--optional-package", type=Path) + parser.add_argument("--collective-audit", type=Path) args = parser.parse_args() grade( args.source.resolve(), @@ -180,6 +188,7 @@ def main() -> None: args.device, args.timeout, args.optional_package, + args.collective_audit, ) diff --git a/tools/token_budget_study/private/worker.py b/tools/token_budget_study/private/worker.py index 4db25175..e987b727 100644 --- a/tools/token_budget_study/private/worker.py +++ b/tools/token_budget_study/private/worker.py @@ -3,6 +3,7 @@ from __future__ import annotations import argparse +import contextlib import copy import importlib.abc import importlib.machinery @@ -64,6 +65,7 @@ class Step: gradients: TensorMap parameters: TensorMap momentum: TensorMap + end_microbatch: int def values(model: nn.Module) -> TensorMap: @@ -116,7 +118,7 @@ def reference( torch.autograd.backward(loss) grad = gradients(model) opt.step() - result.append(Step(grad, values(model), momentum(model, opt))) + result.append(Step(grad, values(model), momentum(model, opt), index + 1)) pending.clear() count = 0 return result @@ -138,7 +140,9 @@ def compare_map(label: str, expected: TensorMap, actual: TensorMap) -> list[str] return issues -def evaluate(case: Scenario, rank: int, device: torch.device) -> dict[str, object]: +def evaluate( + case: Scenario, rank: int, device: torch.device, audit_communication: bool = False +) -> dict[str, object]: torch.manual_seed(case.seed) base = TokenModel().to(device) plain = copy.deepcopy(base) @@ -160,6 +164,11 @@ def before_step( args: tuple[object, ...], kwargs: dict[str, object], ) -> None: + if audit_communication: + from collectives import STEP + + with torch.profiler.record_function(STEP): + pass pre.append(gradients(base)) boundaries.append(list(observed_ids)) observed_ids.clear() @@ -169,7 +178,7 @@ def after_step( args: tuple[object, ...], kwargs: dict[str, object], ) -> None: - actual.append(Step(pre.pop(), values(base), momentum(base, optimizer))) + actual.append(Step(pre.pop(), values(base), momentum(base, optimizer), 0)) opt.register_step_pre_hook(before_step) opt.register_step_post_hook(after_step) @@ -195,6 +204,11 @@ def observe_forward( def before_backward(grad: Tensor) -> None: nonlocal outstanding + if audit_communication: + from collectives import BACKWARD + + with torch.profiler.record_function(BACKWARD): + pass outstanding -= 1 # Tensor.register_hook in PyTorch 2.2 lacks type annotations. @@ -204,7 +218,17 @@ def before_backward(grad: Tensor) -> None: try: from training import train_epoch - train_epoch(ddp, opt, iter(rank_batches[rank]), case.budget) + profiler = ( + torch.profiler.profile( + activities=[torch.profiler.ProfilerActivity.CPU], record_shapes=True + ) + if audit_communication + else None + ) + with profiler if profiler is not None else contextlib.nullcontext(): + train_epoch(ddp, opt, iter(rank_batches[rank]), case.budget) + if profiler is not None: + torch.cuda.synchronize() except NotImplementedError as exc: return {"name": case.name, "status": "unsupported", "error": repr(exc)} except Exception as exc: @@ -236,6 +260,15 @@ def before_backward(grad: Tensor) -> None: ), ) finite = all(bool(torch.isfinite(t).all().item()) for t in observed) + communication: dict[str, object] | None = None + if profiler is not None: + from collectives import audit + + communication = audit( + profiler.events(), + [step.end_microbatch for step in expected], + sum(parameter.numel() for parameter in base.parameters()), + ) status = "pass" if not finite: status = "nonfinite" @@ -243,6 +276,8 @@ def before_backward(grad: Tensor) -> None: status = "silent_wrong" elif peak_outstanding > 1 or max_input_elements > rank_batches[rank][0][0].numel(): status = "memory_contract" + elif communication is not None and communication["issues"]: + status = "communication_contract" return { "name": case.name, "status": status, @@ -253,6 +288,7 @@ def before_backward(grad: Tensor) -> None: "expected_updates": len(expected), "observed_updates": len(actual), "issues": numerical, + "communication": communication, } @@ -260,6 +296,7 @@ def main() -> None: parser = argparse.ArgumentParser() parser.add_argument("--receipts", type=Path, required=True) parser.add_argument("--device", choices=["cpu", "cuda"], required=True) + parser.add_argument("--audit-communication", action="store_true") args = parser.parse_args() rank = int(os.environ["RANK"]) local = int(os.environ["LOCAL_RANK"]) @@ -277,7 +314,7 @@ def main() -> None: for index, case in enumerate(SCENARIOS): path = args.receipts / f"{index}-rank{rank}.json" path.with_suffix(".start").write_text(case.name) - result = evaluate(case, rank, device) + result = evaluate(case, rank, device, args.audit_communication) with path.open("x") as stream: json.dump(result, stream) dist.barrier() diff --git a/tools/window_sync_study/optional/window_check.py b/tools/window_sync_study/optional/window_check.py new file mode 100644 index 00000000..65c97653 --- /dev/null +++ b/tools/window_sync_study/optional/window_check.py @@ -0,0 +1,188 @@ +"""Opt-in TrainCheck adapter for the public two-rank token training task.""" + +from __future__ import annotations + +import copy +import json +import os +from dataclasses import dataclass + +import torch +import torch.distributed as dist +from torch import Tensor, nn +from torch.nn import functional as F +from torch.nn.parallel import DistributedDataParallel +from workload import IGNORE_INDEX, Batch, TokenModel, batches, make_optimizer + +from traincheck._window_collectives import BACKWARD, STEP, audit +from traincheck.observation import ModelSnapshot, compare_ranks, snapshot_model + +State = dict[str, Tensor] + + +@dataclass +class StepState: + gradients: State + parameters: State + momentum: State + + +def capture(model: nn.Module, optimizer: torch.optim.Optimizer) -> StepState: + weights: State = {} + gradients: State = {} + momentum: State = {} + for name, item in model.named_parameters(): + weights[name] = item.detach().clone() + if item.grad is not None: + gradients[name] = item.grad.detach().clone() + if item in optimizer.state: + buffer = optimizer.state[item]["momentum_buffer"] + if not isinstance(buffer, Tensor): + raise TypeError("SGD momentum must be a tensor") + momentum[name] = buffer.detach().clone() + return StepState(gradients, weights, momentum) + + +def reference( + model: nn.Module, rank_batches: list[list[Batch]], budget: int | None +) -> tuple[list[int], list[StepState]]: + optimizer = make_optimizer(model) + ends: list[int] = [] + states: list[StepState] = [] + pending: list[Batch] = [] + count = 0 + for step, group in enumerate(zip(*rank_batches, strict=True), 1): + pending.extend(group) + count += sum(int(target.ne(IGNORE_INDEX).sum().item()) for _, target in group) + if budget is not None and count < budget and step != len(rank_batches[0]): + continue + optimizer.zero_grad(set_to_none=True) + logits: Tensor = model(torch.cat([x for x, _ in pending])) + labels = torch.cat([y for _, y in pending]) + F.cross_entropy( + logits.flatten(0, 1), labels.flatten(), ignore_index=IGNORE_INDEX + ).backward() + optimizer.step() + ends.append(step) + states.append(capture(model, optimizer)) + pending.clear() + count = 0 + return ends, states + + +FIXTURES = ( + ("fixed", (13, 19, 6, 8, 21), (8, 11, 14, 20, 9), None), + ("exact", (13, 19, 6, 8, 21), (8, 11, 14, 20, 9), 51), + ("partial_tail", (13, 19, 6, 8, 21), (8, 11, 14, 20, 9), 60), + ("epoch_below_budget", (0, 7, 21), (18, 10, 0), 200), +) + + +def check_case( + name: str, + left: tuple[int, ...], + right: tuple[int, ...], + budget: int | None, + device: torch.device, + rank: int, +) -> bool: + from training import train_epoch + + torch.manual_seed(17) + base = TokenModel().to(device) + groups = [batches(counts, r, 18, device) for r, counts in enumerate((left, right))] + ends, expected = reference(copy.deepcopy(base), groups, budget) + model = DistributedDataParallel(base, device_ids=[device.index]) + optimizer = make_optimizer(model) + actual: list[StepState] = [] + + def backward_marker(grad: Tensor) -> None: + with torch.profiler.record_function(BACKWARD): + pass + + def forward_marker( + module: nn.Module, args: tuple[Tensor, ...], out: Tensor + ) -> None: + if out.requires_grad: + out.register_hook(backward_marker) # type: ignore[no-untyped-call] + + def step_marker( + opt: torch.optim.Optimizer, args: tuple[object, ...], kwargs: dict[str, object] + ) -> None: + with torch.profiler.record_function(STEP): + pass + + def after_step( + opt: torch.optim.Optimizer, args: tuple[object, ...], kwargs: dict[str, object] + ) -> None: + actual.append(capture(base, opt)) + + base.register_forward_hook(forward_marker) + optimizer.register_step_pre_hook(step_marker) + optimizer.register_step_post_hook(after_step) + with torch.profiler.profile( + activities=[torch.profiler.ProfilerActivity.CPU], record_shapes=True + ) as prof: + train_epoch(model, optimizer, iter(groups[rank]), budget) + torch.cuda.synchronize() + communication = audit( + prof.events(), ends, sum(p.numel() for p in base.parameters()) + ) + errors: list[str] = [] + if len(actual) != len(expected): + errors.append(f"Expected {len(expected)} updates, got {len(actual)}") + for index, (obs, target) in enumerate(zip(actual, expected), 1): + for label, seen, wanted in ( + ("gradient", obs.gradients, target.gradients), + ("parameter", obs.parameters, target.parameters), + ("momentum", obs.momentum, target.momentum), + ): + try: + torch.testing.assert_close(seen, wanted, rtol=1e-5, atol=1e-6) + except AssertionError as exc: + errors.append(f"Update {index} {label}: {str(exc)[:260]}") + gathered: list[object] = [None] * dist.get_world_size() + own = snapshot_model(base, run_id=name, rank=rank, phase="after_epoch") + dist.all_gather_object(gathered, own) + snapshots: list[ModelSnapshot] = [] + for snapshot in gathered: + if not isinstance(snapshot, ModelSnapshot): + raise TypeError("Expected a TrainCheck model snapshot from every rank") + snapshots.append(snapshot) + rank_match = compare_ranks(snapshots, expected_ranks=[0, 1]).equal + passed = not errors and not communication["issues"] and rank_match + print( + json.dumps( + { + "traincheck_window_check": name, + "rank": rank, + "passed": passed, + "numeric_issues": errors, + "communication": communication, + "rank_match": rank_match, + } + ), + flush=True, + ) + return passed + + +def main() -> None: + local = int(os.environ["LOCAL_RANK"]) + torch.cuda.set_device(local) + dist.init_process_group("nccl") + try: + rank = dist.get_rank() + device = torch.device("cuda", local) + results = [ + check_case(name, lhs, rhs, budget, device, rank) + for name, lhs, rhs, budget in FIXTURES + ] + if not all(results): + raise SystemExit(1) + finally: + dist.destroy_process_group() + + +if __name__ == "__main__": + main() diff --git a/tools/window_sync_study/private/collectives.py b/tools/window_sync_study/private/collectives.py new file mode 100644 index 00000000..216b2033 --- /dev/null +++ b/tools/window_sync_study/private/collectives.py @@ -0,0 +1,88 @@ +"""Decode native ProcessGroupNCCL all-reduces against training boundaries.""" + +from __future__ import annotations + +import math +from collections.abc import Iterable, Sequence +from dataclasses import asdict, dataclass +from typing import Protocol + +BACKWARD = "traincheck.window.backward" +STEP = "traincheck.window.optimizer_step" + + +class TimeRange(Protocol): + @property + def start(self) -> float: ... + + +class Event(Protocol): + @property + def name(self) -> str: ... + + @property + def time_range(self) -> TimeRange: ... + + @property + def input_shapes(self) -> Sequence[Sequence[int]]: ... + + +@dataclass(frozen=True) +class Reduction: + backward: int + update: int + elements: int + + +def audit( + events: Iterable[Event], expected_ends: Sequence[int], gradient_elements: int +) -> dict[str, object]: + """Count launched all-reduces. This workload's sole scalar payload is counts.""" + backwards = 0 + update = 0 + scalar_calls = 0 + reductions: list[Reduction] = [] + issues: list[str] = [] + observed_ends: list[int] = [] + for event in sorted(events, key=lambda entry: entry.time_range.start): + if event.name == BACKWARD: + backwards += 1 + elif event.name == STEP: + observed_ends.append(backwards) + update += 1 + elif event.name == "nccl:all_reduce": + shapes = event.input_shapes + if len(shapes) != 1 or any(n <= 0 for n in shapes[0]): + raise ValueError(f"Missing native NCCL payload dimension: {shapes!r}") + elements = math.prod(shapes[0]) + if elements == 1: + scalar_calls += 1 + else: + reductions.append(Reduction(backwards, update, elements)) + if list(expected_ends) != observed_ends: + issues.append( + f"Expected update ends {list(expected_ends)}, saw {observed_ends}" + ) + for i, end in enumerate(expected_ends): + group = [r for r in reductions if r.update == i] + if any(r.backward != end for r in group): + issues.append( + f"Update {i} issued gradient communication before its last backward" + ) + elements = sum(r.elements for r in group) + if elements != gradient_elements: + issues.append( + f"Update {i} reduced {elements} gradient elements; expected {gradient_elements}" + ) + if any(r.update >= len(expected_ends) for r in reductions): + issues.append("Gradient communication occurred after the expected final update") + return { + "issues": issues, + "scalar_all_reduce_calls": scalar_calls, + "gradient_all_reduce_calls": len(reductions), + "gradient_elements_reduced": sum(r.elements for r in reductions), + "expected_gradient_elements_per_update": gradient_elements, + "expected_end_microbatches": list(expected_ends), + "observed_end_microbatches": observed_ends, + "gradient_reductions": [asdict(r) for r in reductions], + } diff --git a/tools/window_sync_study/private/qualified_training.py b/tools/window_sync_study/private/qualified_training.py new file mode 100644 index 00000000..1d25b74a --- /dev/null +++ b/tools/window_sync_study/private/qualified_training.py @@ -0,0 +1,78 @@ +"""Qualified implementation used only by the study operator.""" + +from __future__ import annotations + +from collections.abc import Iterable + +import torch +import torch.distributed as dist +from torch import Tensor, nn +from torch.nn import functional as F +from torch.nn.parallel import DistributedDataParallel +from workload import IGNORE_INDEX, Batch + + +def train_epoch( + model: nn.Module, + optimizer: torch.optim.Optimizer, + microbatches: Iterable[Batch], + token_budget: int | None, +) -> None: + if token_budget is not None and token_budget <= 0: + raise ValueError("Expected a positive token budget") + model.train() + total = 0 + optimizer.zero_grad(set_to_none=True) + + if not isinstance(model, DistributedDataParallel): + raise TypeError("Expected DistributedDataParallel") + parameters = list(model.parameters()) + + def finish(tokens: int) -> None: + parts = [] + for parameter in parameters: + if parameter.grad is None: + raise ValueError("A model parameter has no gradient") + parts.append(parameter.grad.flatten()) + if token_budget is None: + for parameter in parameters: + assert parameter.grad is not None + parameter.grad.mul_(dist.get_world_size() / tokens) + else: + packed = torch.cat(parts) + dist.all_reduce(packed) + packed.div_(tokens) + offset = 0 + for parameter in parameters: + assert parameter.grad is not None + after = offset + parameter.numel() + parameter.grad.copy_(packed[offset:after].view_as(parameter)) + offset = after + optimizer.step() + optimizer.zero_grad(set_to_none=True) + + for inputs, targets in microbatches: + + def backward() -> None: + logits: Tensor = model(inputs) + loss = F.cross_entropy( + logits.flatten(0, 1), + targets.flatten(), + ignore_index=IGNORE_INDEX, + reduction="sum", + ) + torch.autograd.backward(loss) + + if token_budget is None: + backward() + else: + with model.no_sync(): + backward() + count = targets.ne(IGNORE_INDEX).sum() + dist.all_reduce(count) + total += int(count.item()) + if token_budget is None or total >= token_budget: + finish(total) + total = 0 + if total: + finish(total) diff --git a/tools/window_sync_study/public/TASK.md b/tools/window_sync_study/public/TASK.md new file mode 100644 index 00000000..c31f540b --- /dev/null +++ b/tools/window_sync_study/public/TASK.md @@ -0,0 +1,19 @@ +Improve the token-budget mode of this working two-rank DDP pipeline in +`training.py`. Synchronize gradients only when a token window ends, once +per full gradient payload per window. Individual buckets or parameter groups +may reduce separately at that boundary. All-reduces for integer token counts +are still allowed at each microbatch. Use DDP's reducer or ordinary +`torch.distributed.all_reduce` for the gradient data. + +Preserve the existing behavior: a window consists of whole synchronous +microbatches, closes at or above the global target-token budget, and flushes +its remaining targets at epoch end. Preserve update boundaries, gradient, +parameter and momentum values within floating point tolerance. Keep the +original fixed mode. Do not hold graphs for an entire window, buffer a window +of inputs or require the input iterable to have a length. Each rank has the +same number of microbatches, and each synchronous microbatch contains at +least one nonpadding target globally. + +Run the ordinary example as `torchrun --standalone --nproc-per-node=2 train.py +--token-budget 60`. A separate `diagnostic.py` is allowed. The deliverable is +`training.py`. Report when you consider it ready and summarize your evidence. diff --git a/tools/window_sync_study/public/train.py b/tools/window_sync_study/public/train.py new file mode 100644 index 00000000..bd9e1422 --- /dev/null +++ b/tools/window_sync_study/public/train.py @@ -0,0 +1,70 @@ +"""Ordinary torchrun entry point for the token training task.""" + +from __future__ import annotations + +import argparse +import json +import math +import os + +import torch +import torch.distributed as dist +from torch import Tensor +from torch.nn import functional as F +from torch.nn.parallel import DistributedDataParallel +from training import train_epoch +from workload import IGNORE_INDEX, TokenModel, batches, make_optimizer + + +def main() -> None: + parser = argparse.ArgumentParser() + parser.add_argument("--token-budget", type=int) + parser.add_argument("--seed", type=int, default=17) + parser.add_argument( + "--device", + choices=["cpu", "cuda"], + default="cuda" if torch.cuda.is_available() else "cpu", + ) + args = parser.parse_args() + if args.token_budget is not None and args.token_budget <= 0: + parser.error("--token-budget must be positive") + rank = int(os.environ["RANK"]) + local_rank = int(os.environ["LOCAL_RANK"]) + cuda = args.device == "cuda" + device = torch.device("cuda", local_rank) if cuda else torch.device("cpu") + if cuda: + torch.cuda.set_device(device) + dist.init_process_group("nccl" if cuda else "gloo") + try: + torch.manual_seed(args.seed) + base = TokenModel().to(device) + model = DistributedDataParallel(base, device_ids=[local_rank] if cuda else None) + optimizer = make_optimizer(model) + counts = (13, 19, 6, 8, 21) if rank % 2 == 0 else (8, 11, 14, 20, 9) + local_batches = batches(counts, rank, args.seed + 1, device) + train_epoch(model, optimizer, iter(local_batches), args.token_budget) + with torch.no_grad(): + inputs, targets = local_batches[-1] + logits: Tensor = base(inputs) + loss = F.cross_entropy( + logits.flatten(0, 1), targets.flatten(), ignore_index=IGNORE_INDEX + ) + value = float(loss.item()) + if not math.isfinite(value): + raise ValueError("Nonfinite final loss") + print( + json.dumps( + { + "rank": rank, + "microbatches": len(local_batches), + "final_local_loss": value, + } + ), + flush=True, + ) + finally: + dist.destroy_process_group() + + +if __name__ == "__main__": + main() diff --git a/tools/window_sync_study/public/training.py b/tools/window_sync_study/public/training.py new file mode 100644 index 00000000..2c6e915d --- /dev/null +++ b/tools/window_sync_study/public/training.py @@ -0,0 +1,50 @@ +"""Streaming gradient accumulation for the two-rank token training workload.""" + +from __future__ import annotations + +from collections.abc import Iterable + +import torch +import torch.distributed as dist +from torch import Tensor, nn +from torch.nn import functional as F +from workload import IGNORE_INDEX, Batch + + +def train_epoch( + model: nn.Module, + optimizer: torch.optim.Optimizer, + microbatches: Iterable[Batch], + token_budget: int | None, +) -> None: + if token_budget is not None and token_budget <= 0: + raise ValueError("Expected a positive token budget") + model.train() + total = 0 + optimizer.zero_grad(set_to_none=True) + + def finish(tokens: int) -> None: + for parameter in model.parameters(): + if parameter.grad is None: + raise ValueError("A model parameter has no gradient") + parameter.grad.mul_(dist.get_world_size() / tokens) + optimizer.step() + optimizer.zero_grad(set_to_none=True) + + for inputs, targets in microbatches: + logits: Tensor = model(inputs) + loss = F.cross_entropy( + logits.flatten(0, 1), + targets.flatten(), + ignore_index=IGNORE_INDEX, + reduction="sum", + ) + torch.autograd.backward(loss) + count = targets.ne(IGNORE_INDEX).sum() + dist.all_reduce(count) + total += int(count.item()) + if token_budget is None or total >= token_budget: + finish(total) + total = 0 + if total: + finish(total) diff --git a/tools/window_sync_study/public/workload.py b/tools/window_sync_study/public/workload.py new file mode 100644 index 00000000..8b081ec0 --- /dev/null +++ b/tools/window_sync_study/public/workload.py @@ -0,0 +1,47 @@ +"""Small deterministic token-level training workload.""" + +from __future__ import annotations + +from collections.abc import Sequence + +import torch +from torch import Tensor, nn + +Batch = tuple[Tensor, Tensor] +IGNORE_INDEX = -100 +VOCABULARY = 29 +WIDTH = 12 +ROWS = 2 +COLUMNS = 12 + + +class TokenModel(nn.Module): + def __init__(self) -> None: + super().__init__() + self.embedding = nn.Embedding(VOCABULARY, WIDTH) + self.projection = nn.Linear(WIDTH, VOCABULARY) + + def forward(self, tokens: Tensor) -> Tensor: + logits: Tensor = self.projection(self.embedding(tokens).tanh()) + return logits + + +def batches( + counts: Sequence[int], rank: int, seed: int, device: torch.device +) -> list[Batch]: + generator = torch.Generator().manual_seed(seed + 8191 * rank) + result: list[Batch] = [] + for count in counts: + if not 0 <= count <= ROWS * COLUMNS: + raise ValueError("Target count must fit the microbatch") + size = (ROWS, COLUMNS) + tokens = torch.randint(VOCABULARY, size, generator=generator) + targets = torch.randint(VOCABULARY, size, generator=generator) + padding = torch.randperm(ROWS * COLUMNS, generator=generator)[count:] + targets.view(-1)[padding] = IGNORE_INDEX + result.append((tokens.to(device), targets.to(device))) + return result + + +def make_optimizer(model: nn.Module) -> torch.optim.SGD: + return torch.optim.SGD(model.parameters(), lr=0.08, momentum=0.7) diff --git a/tools/window_sync_study/replay.py b/tools/window_sync_study/replay.py new file mode 100644 index 00000000..f4c21dfb --- /dev/null +++ b/tools/window_sync_study/replay.py @@ -0,0 +1,123 @@ +"""Replay distinct production sources after all six native trials finish.""" + +from __future__ import annotations + +import argparse +import hashlib +import json +import subprocess +import sys +from pathlib import Path + +from tools.token_budget_study.operator.agent_trials import MAX_TURNS, SERVICE, digest +from tools.token_budget_study.operator.score_submissions import ( + duration, + grade_status, + read, +) + +ARMS = ("control", "traincheck") + + +def sources(root: Path) -> dict[tuple[str, str], Path]: + launch = read(root / "native-launch.json") + if launch["service_sha256"] != digest(SERVICE): + raise ValueError("The worker differs from the frozen launcher") + identities: dict[tuple[str, str], Path] = {} + occurrences: list[dict[str, str]] = [] + terminal: dict[str, str] = {} + for pair in range(1, 4): + for arm in ARMS: + name = f"{arm}_{pair}" + trial = root / "trials" / name + terminal[name] = digest(trial / "final.json") + for turn in range(1, MAX_TURNS + 1): + decision = trial / f"turn-{turn}" / "submission.json" + if not decision.is_file(): + continue + path = decision.parent / "submission" / "training.py" + sha = digest(path) + if read(decision)["production_sha256"] != sha: + raise ValueError(f"Frozen submission changed: {path}") + identities[arm, sha] = path + occurrences.append( + {"trial": name, "path": str(path.relative_to(root)), "sha256": sha} + ) + watch = trial / f"turn-{turn}.watch" / "index.jsonl" + for blob in sorted(watch.parent.glob("blobs/*.py")): + if digest(blob) != blob.stem: + raise ValueError(f"Observed revision changed: {blob}") + identities.setdefault((arm, blob.stem), blob) + manifest = root / "private-replay-inventory.json" + data = json.dumps( + { + "occurrences": occurrences, + "terminal_hashes": terminal, + "native_launch": digest(root / "native-launch.json"), + "distinct": len(identities), + }, + indent=2, + ) + if manifest.exists(): + if manifest.read_text() != data: + raise ValueError("Existing replay inventory differs") + else: + with manifest.open("x") as stream: + stream.write(data) + return identities + + +def run(root: Path) -> None: + reports: list[dict[str, object]] = [] + total = 0.0 + for (arm, sha), original in sources(root).items(): + source = root / "grade-sources" / arm / sha + source.mkdir(parents=True, exist_ok=True) + code = source / "training.py" + if not code.exists(): + with code.open("xb") as stream: + stream.write(original.read_bytes()) + if digest(code) != sha: + raise ValueError("Replay source contents disagree with inventory") + output = root / "grades" / arm / sha + receipt = output / "receipt.json" + if not receipt.is_file(): + request = [ + sys.executable, + str(SERVICE), + "grade", + "--source", + str(source), + "--id", + f"grade-{arm}-{sha}", + "--output", + str(output), + ] + if arm == "traincheck": + request.append("--with-traincheck") + subprocess.run(request, check=True) + result = grade_status(receipt, sha, arm == "traincheck") + gpu = duration(read(receipt)["gpu_seconds"]) + total += gpu + reports.append( + { + "arm": arm, + "sha256": sha, + "outcome": result, + "receipt_sha256": hashlib.sha256(receipt.read_bytes()).hexdigest(), + "gpu_seconds": gpu, + } + ) + print(arm, sha[:10], result, round(gpu, 3), flush=True) + with (root / "private-grading-summary.json").open("x") as stream: + json.dump({"grades": reports, "gpu_seconds": total}, stream, indent=2) + + +def main() -> None: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("root", type=Path) + run(parser.parse_args().root) + + +if __name__ == "__main__": + main() diff --git a/tools/window_sync_study/test_collectives.py b/tools/window_sync_study/test_collectives.py new file mode 100644 index 00000000..298d81d3 --- /dev/null +++ b/tools/window_sync_study/test_collectives.py @@ -0,0 +1,63 @@ +from dataclasses import dataclass + +from tools.window_sync_study.private.collectives import BACKWARD, STEP, audit + + +@dataclass(frozen=True) +class Interval: + start: float + + +@dataclass(frozen=True) +class Record: + name: str + time_range: Interval + input_shapes: list[list[int]] + + +def trace(sizes: list[tuple[str, int]]) -> list[Record]: + return [ + Record(name, Interval(index), [[size]] if size else []) + for index, (name, size) in enumerate(sizes) + ] + + +def test_full_and_partial_windows_with_separate_gradient_buckets() -> None: + events = trace( + [ + (BACKWARD, 0), + ("nccl:all_reduce", 1), + (BACKWARD, 0), + ("nccl:all_reduce", 5), + ("nccl:all_reduce", 7), + (STEP, 0), + (BACKWARD, 0), + ("nccl:all_reduce", 12), + (STEP, 0), + ] + ) + result = audit(events, [2, 3], 12) + assert result["issues"] == [] + assert result["scalar_all_reduce_calls"] == 1 + assert result["gradient_all_reduce_calls"] == 3 + + +def test_count_alone_does_not_pass_early_or_missing_tail_sync() -> None: + events = trace( + [ + (BACKWARD, 0), + ("nccl:all_reduce", 12), + (BACKWARD, 0), + (STEP, 0), + (BACKWARD, 0), + (STEP, 0), + ("nccl:all_reduce", 12), + ] + ) + result = audit(events, [2, 3], 12) + assert result["gradient_all_reduce_calls"] == 2 + assert result["issues"] == [ + "Update 0 issued gradient communication before its last backward", + "Update 1 reduced 0 gradient elements; expected 12", + "Gradient communication occurred after the expected final update", + ]