Skip to content

refactor: move AF3 input preparation out of the backend - #659

Merged
DimaMolod merged 3 commits into
mainfrom
refactor/af3-inputs
Oct 13, 2026
Merged

DimaMolod merged 3 commits into
mainfrom
refactor/af3-inputs

Conversation

@DimaMolod

@DimaMolod DimaMolod commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

Second of three behaviour-preserving PRs that split alphafold3_backend.py; stacked on #658. This one takes input preparation out of the backend. No flag, output or file name changes.

What moves

AlphaFold3Backend.prepare_input was 1,230 lines holding 28 nested functions. It is now about 40 lines:

  • alphafold3_inputs.py (new).
    • The pure helpers become module functions: chain construction, monomer normalisation, region slicing of JSON chains, template mmCIF fixes, chain IDs, and the job name (_object_name_fragment, _build_output_job_name).
    • The helpers that read prepare_input's state become methods of a FoldInputBuilder: the chains so far, the chain IDs in use, which chains carry MSAs translated from AF2 features, the debug records, and the caches.
    • prepare_input makes a fresh builder per call, so the caches of feature metadata and template mmCIF checks still last one preparation, never the process. _supported_chain_kwargs loses its cache: it runs a few times per fold, and caching it would have pulled the pure chain helpers into the builder.
  • alphafold3_debug.py (new). The translated-MSA debug writer and the final-MSA A3M writer that was nested in predict_structure.

The moved code changes only in indentation, apart from closure state becoming builder attributes. That rewiring is spelled out and count-checked by the script that did the cut.

Commits

  1. The move. Tests patch and call the names where the moved code now looks them up. The string naming rules are tested in output_paths, where they live since refactor: move AF3 output writing and resume out of the backend #658.

  2. Builder tests:

    • chain IDs roll over from Z to AA;
    • chain IDs given in JSON and generated ones never collide;
    • two preparations in one process share no chain IDs and no cached feature metadata;
    • A3M region slicing keeps only the insertions inside the region;
    • JSON chain regions slice sequence, residue IDs, PTMs and MSA together.

    Making the metadata cache process-wide fails the shared-state test, as it should.

  3. PreparedFold. prepare_input returns PreparedFold(fold_input, output_dir, resolve_msa_overlaps) instead of single-key {fold_input: (output_dir, flag)} dicts. predict drops its length and legacy-shape checks. In the tests, unpacking becomes attribute access; no assertion changes.

Verification

  • CPU equivalence harness, after the move and again after PreparedFold. It covers 15 inputs: monomer, homo- and hetero-oligomers, chopped regions, RNA, DNA, ligand, PTMs, JSON inputs, multi-seed, and two folds per call. Each runs prepare_input and then process_fold_input(model_runner=None) with debug MSAs and templates on. Every _data.json, debug artifact, output dir and resolve_msa_overlaps flag is byte-identical to main.
  • Unit + integration, both release images: the same results as main plus the 6 new tests. AF2 image: 937 passed, 26 skipped. AF3 image: 1008 passed, 15 skipped. The same pre-existing environmental failures as on main (no pytest-xdist, no network).
  • GPU, full AF3 suite with RUN_EXPENSIVE_GPU_TESTS=1: 39/39 passed, including TestAlphaFold3MetadataEndToEnd (real feature generation through inference and ModelCIF), resume, fast kernels and the MMseqs/regression checks.

🤖 Generated with Claude Code

DimaMolod and others added 3 commits October 13, 2026 19:43
prepare_input was 1,230 lines holding 28 nested functions. They now live in
alphafold3_inputs.py: the pure helpers (chain construction, monomer
normalisation, region slicing of JSON chains, template mmCIF fixes, chain IDs,
the job name) as module functions, and what read prepare_input's state as
methods of FoldInputBuilder. prepare_input makes a fresh builder per call, so
its chain IDs and its caches of feature metadata and template mmCIF checks
still belong to one preparation. _supported_chain_kwargs loses its cache: it
runs a few times per fold, and a cache would have pulled the pure chain helpers
into the builder.

The translated-MSA debug writer and the final-MSA A3M writer, nested in
predict_structure, move to alphafold3_debug.py.

The moved code changes only in indentation, apart from closure state becoming
builder attributes. Tests patch and call the names where the moved code looks
them up; the string naming rules are tested in output_paths.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Chain IDs roll over from Z to AA; JSON-given and generated chain IDs never
collide; two preparations in one process share no chain IDs and no cached
feature metadata (a process-wide cache fails this test); A3M region slicing
keeps only the insertions inside the region; JSON chain regions slice the
sequence, residue IDs, PTMs and MSA together, and need exactly one chain.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
prepare_input returned single-key dicts {fold_input: (output_dir,
resolve_msa_overlaps)}, which predict took apart with a length check and an
isinstance check for a legacy bare output_dir. It now returns PreparedFold
dataclasses with those three fields, and predict reads them by name.

Callers in the tests switch from unpacking to attribute access; no assertion
changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-13T18:22:55.132940Z 03c7eb7 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@DimaMolod
DimaMolod changed the base branch from refactor/af3-outputs to main October 13, 2026 19:18
@DimaMolod
DimaMolod merged commit fcba0d4 into main Oct 13, 2026
6 checks passed
@DimaMolod
DimaMolod deleted the refactor/af3-inputs branch October 13, 2026 19:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant