From 863a8c41381d153b39ecf6e7e1f12b99971d4154 Mon Sep 17 00:00:00 2001 From: Florent Pollet Date: Mon, 30 Nov 2026 13:27:31 -0500 Subject: [PATCH] Require implementations to be efficient at full-data scale Curiosity passed every gate on the fixture and demo, then needed 29 min per NWICU prediction time (about 2,700 GPU-h for the held-out set): five sequential groups, a per-sample host sync each token, float32 inference that fell back to materialized attention. - Guide, section 4: "Efficiency at realistic scale": reuse shared work (prefix caches, last-position outputs, per-subject features), batch to memory with no per-item Python loops or host-device syncs in inner loops, preallocated buffers, inference in the training dtype with fused kernels, memory bounded by the longest real input, batch-independent results, the source's efficient paths; then time a realistic slice and project each step's full-data cost in the report. - Gates 4-5: the demo measures per-unit cost; the demo report projects each step's full-data cost before the human gate. - AGENTS.md summarises the rule. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01NJQdbHt5icm1CgFVHsHTNx --- template/AGENTS.md.jinja | 5 +++ template/docs/IMPLEMENTATION_GUIDE.md.jinja | 39 +++++++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/template/AGENTS.md.jinja b/template/AGENTS.md.jinja index c6a2bb4..ab21195 100644 --- a/template/AGENTS.md.jinja +++ b/template/AGENTS.md.jinja @@ -31,6 +31,11 @@ for material unresolved choices; exploratory assumptions require explicit opt-in - Intermediate payloads are opaque. Generic tests validate their envelopes, not their contents. - MEDS-DEV owns dataset-specific predicates; the algorithm owns their interpretation. - Never associate outputs with prediction keys by row order. +- Write expensive paths for full-data scale (see "Efficiency at realistic scale" in the guide): reuse shared + work (prefix/prompt caches, last-position outputs), batch to memory, no per-item Python loops or host-device + syncs in inner loops, inference in the training dtype with fused kernels, memory bounded by the longest real + input, results independent of batch size. Project each step's full-data cost from a timed realistic slice + before submitting. - Long-running steps stream progress through `logging` (phases with planned sizes, step/epoch out of total, training and tuning loss, kept checkpoint, inference counts; see "Progress logging" in the guide). Log only aggregates and configuration: never subject IDs, times, codes, values, labels, predictions or sample rows. diff --git a/template/docs/IMPLEMENTATION_GUIDE.md.jinja b/template/docs/IMPLEMENTATION_GUIDE.md.jinja index 2a5893e..7590ffc 100644 --- a/template/docs/IMPLEMENTATION_GUIDE.md.jinja +++ b/template/docs/IMPLEMENTATION_GUIDE.md.jinja @@ -128,6 +128,39 @@ re-raise with an aggregate message). Do not log code names or vocabulary entries identify a patient in a small cohort. Paths, digests and configuration are fine. Logs are copied off clusters and into CI output, so treat them as shareable. +### Efficiency at realistic scale + +The synthetic fixture and the demo are small: an implementation can pass every gate and still be orders of +magnitude too slow or run out of memory on the full dataset, because cost grows with sequence length, cohort +size, samples per key or vocabulary. Write the expensive paths (training loops, inference, generation, feature +construction) for the full data from the start, where it is relevant to the algorithm: + +- **Do not recompute shared work.** Process a prompt or prefix shared by several samples once and reuse its + state (for example a transformer's attention cache); compute only the outputs used (for example last-position + logits, not every position's); cache per-subject features reused across tasks or prediction times. +- **Keep accelerators busy.** Batch independent items (samples, subjects, prediction times) and size batches + from the memory they need, not a fixed small constant. In loops that run per token, step or item, avoid Python + loops over the batch and host-device synchronizations (`.item()`, `float(t)`, `bool(t)`, `.tolist()`, + `.cpu()`); decide per-item conditions with tensor operations and check termination every few dozen + iterations. Preallocate buffers that grow instead of concatenating each step. +- **Use the precision and kernels the model was trained for.** Run inference in the training compute dtype + (for example bf16) with fused attention kernels; a float32 path can silently fall back to kernels that + materialize quadratic attention matrices. +- **Bound memory by the largest realistic input, not the fixture.** Estimate peak memory from tensor shapes at + the longest sequence, largest batch and full vocabulary, and test that path explicitly with a long synthetic + input. Read data lazily or in shards when the whole dataset need not be in memory. +- **Keep results independent of batching where you can.** Seed per item (for example by key, sample index and + step) so batch size, grouping or available memory do not change outputs; otherwise record the setting with + the payload. +- **Reuse the source's efficient paths** (its batched inference, caching or compiled kernels) rather than a + simpler reimplementation, and record any loss of efficiency in `IMPLEMENTATION_REPORT.md`. + +Before a full-data run, measure cost on a realistic slice: the demo run, or a few real-size units of the target +data on the target hardware (for example one prediction time with its real prompt length and sample count). +Report time per unit, peak memory and the projected total for every step (units on the full dataset × time per +unit) in `IMPLEMENTATION_REPORT.md`. If the projection is far beyond the approved resources, optimize or ask +before submitting. + ### Public API and internal helpers Expose the named algorithm and configuration classes for callers. Give implementation-only classes, @@ -309,9 +342,11 @@ Advance in this order. Each automated stage must pass before the next one starts 4. **MIMIC-IV demo gate:** after approval, run `uv run pytest -m real_data -rs`. This builds or reuses the MEDS-DEV demo, derives labels with `mortality/in_icu/first_24h`, reuses MEDS-DEV's MIMIC-IV predicates, and runs the complete selected algorithm profile. The purpose is to expose scale-dependent pipeline, resource, - serialization, and key-coverage failures before a full-data run. + serialization, and key-coverage failures before a full-data run, and to measure per-unit cost (see + "Efficiency at realistic scale"). 5. **Demo report and human gate:** add the exact demo revision, task and predicate provenance, cohort - counts, prediction-key coverage, measurements, failures, and fixes to the report. A human decides + counts, prediction-key coverage, measurements, failures, and fixes to the report, with the projected cost + of each step on the full dataset (time per unit × full-data units, peak memory at the longest inputs). A human decides whether the evidence is sufficient, reviews target-dataset/task/predicate compatibility and the shared full-data task bundle, and approves access, resources, and launch for the selected full dataset. 6. **Commit and synchronize:** before submitting cluster jobs, commit the implementation, configuration,