Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions template/AGENTS.md.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
39 changes: 37 additions & 2 deletions template/docs/IMPLEMENTATION_GUIDE.md.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Loading