Skip to content

Share local tuning across TP ranks in tuning replicas and reconcile serving selections - #695

Merged
lukealonso merged 1 commit into
mainfrom
fix/tuning-replicas-tp-sync
Dec 3, 2026
Merged

lukealonso merged 1 commit into
mainfrom
fix/tuning-replicas-tp-sync

Conversation

@lukealonso

Copy link
Copy Markdown
Collaborator

Why

When exporting a tensor-parallel model with local tuning replicas (--tuning-gpus spanning multiple complete TP replicas), RecordedPreparationSession.tune previously sharded local candidate races only across corresponding ranks (configure_tuning_shard(replica, range(replicas)) with peers = states[rank::tensor_parallel] in ignition/preparation/tuning_replicas.py), and explicitly skipped post-tuning reconcile_selections (and self._replica_coordinator is None) and agree_selections.

During a TP=8 Amazon-FMR/Ergo export with --gpus 0-7 --tuning-gpus 0-15 (2 TP=8 replicas on 16 RTX PRO 6000 Blackwell GPUs), this caused two concrete failures:

  1. All 8 ranks within each replica redundantly raced the same candidate half (only 2-way sharding across 16 GPUs instead of 16-way), and per-rank timing jitter produced different attention.gqa champions with different scratch_layout sizes across ranks 0–7.
  2. Because post-tuning reconciliation was skipped for tuning replicas, ranks 1–7 failed AOT forward loading (ForwardCache._prepare_ranks) with BindingMismatch on decoder.layers[0|3].attention.attention.scratch_layout after completing both autotuning and compilation.

What changed

  • ignition/preparation/tuning_replicas.py:
    • Before: authorization() always exchanged ready_cache and ready_tuning only across corresponding ranks states[rank::tensor_parallel].
    • After: Added share_across_ranks: bool = False to authorization() and ReplicaPreparation. When True, ready_cache and ready_tuning synchronize across all replicas * tensor_parallel processes so _tuning_results selects one global winner for every rank, while ready_collectives and ready_collective_tuning remain scoped to each replica's local TP group (states[replica * tp : (replica + 1) * tp]).
  • ignition/preparation/tuning.py:
    • Before: RecordedPreparationSession.tune() gathered only self.device.identity, configured (replica_index, range(replicas)) sharding unconditionally, and skipped reconcile_selections and agree_selections whenever _replica_coordinator was set.
    • After: Gathers (self.device.identity, _local_tuning_catalog(requests)) across all replica processes. When tensor_parallel > 1 and all processes share identical device identities and local tuning catalogs, sets share_across_ranks=True, sets self._sharded=True, and configures (replica * tensor_parallel + rank, range(processes)) sharding. Otherwise falls back to corresponding-rank sharding (replica, range(replicas)). Post-tuning reconcile_selections (with temporary reset/restore of configure_tuning_shard(0, (0,)) around replay) and agree_selections now run on serving replica 0 (self.tuning_replica is None or self.tuning_replica.index == 0), while non-serving replicas (index > 0) skip reconciliation and exit via TuningComplete.
  • tests/preparation/test_tuning_replicas.py: Added unit tests for authorization() (both modes, mixed local/collective interleaving, mismatched race keys), a 4-process (2 replicas × TP=2) Gloo spawn test of ReplicaPreparation.prepare(), and RecordedPreparationSession.tune() tests covering homogeneous cross-rank sharding, unequal-catalog fallback with serving-replica reconciliation, and non-serving replica exit.

Claimed benefit

  • Eliminates cross-rank kernel selection divergence (BindingMismatch during AOT forward loading) on TP exports using --tuning-gpus, both for homogeneous TP catalogs (via unified cross-rank candidate exchange + agreement check) and for asymmetric TP catalogs or warm retries (via serving-replica reconcile_selections).
  • Scales local candidate sharding from R-way to (R * TP)-way when all tuning GPUs and local catalogs match, avoiding TP-way redundant local races.

Supporting evidence

  • pytest tests/preparation/test_tuning_replicas.py tests/preparation/test_tuning_shards.py tests/preparation/test_tuning_jobs.py tests/preparation/test_dcp_selection_groups.py tests/cli/ -q: 167 passed (including the 4-process Gloo ReplicaPreparation test).
  • Full tests/preparation/ CPU run (invoked from the sibling hyperflare-recipes venv): 1,170 passed, 74 skipped (GPU-gated), with 3 unrelated environment failures (test_inspect_forward_kernels, test_runtime_static_triton, test_source_identity due to running against the sibling venv's flare_heliopause build rather than a worktree-local build).
  • Limitations: Multi-GPU end-to-end ignition export --tuning-gpus has not yet been re-run on this commit.

Discord: lukealonso

…reconcile serving selections

When exporting a tensor-parallel model with local tuning replicas
(--tuning-gpus covering multiple complete TP replicas), each TP rank previously
sharded local candidate races only across its corresponding ranks in other
replicas (states[rank::tensor_parallel]) with configure_tuning_shard(replica,
range(replicas)), and RecordedPreparationSession.tune skipped post-tuning
reconcile_selections and agree_selections whenever _replica_coordinator was
active. On TP=8 with 2 replicas (16 GPUs), all 8 ranks in each replica
redundantly benchmarked the same candidate half, could pick different winners
due to measurement jitter (such as attention.gqa plans with distinct scratch
layouts), and then failed rank-local AOT forward binding with BindingMismatch
after compilation.

Changes:
- ignition/preparation/tuning_replicas.py: added share_across_ranks to
  authorization() and ReplicaPreparation so local cache (ready_cache) and
  candidate-race (ready_tuning) exchanges span all replica*tensor_parallel
  processes when homogeneous, while keeping collective exchanges
  (ready_collectives and ready_collective_tuning) scoped to each replica's
  local TP group.
- ignition/preparation/tuning.py:
  - RecordedPreparationSession.tune gathers (device.identity,
    _local_tuning_catalog(requests)) across all tuning-replica processes. When
    tensor_parallel > 1 and all processes report identical device identities
    and local tuning catalogs, it enables share_across_ranks, sets _sharded,
    and shards local candidates across all replica*tensor_parallel processes
    (shard rank replica * tensor_parallel + rank). Otherwise it retains
    corresponding-rank sharding across replicas.
  - Post-tuning reconcile_selections and agree_selections now run on serving
    replica 0 (self.tuning_replica is None or self.tuning_replica.index == 0)
    instead of being skipped whenever _replica_coordinator is set; any
    reconciliation replay temporarily resets configure_tuning_shard(0, (0,))
    and restores the configured shard afterward. Non-serving replicas
    (index > 0) skip reconciliation and exit via TuningComplete as before.
- tests/preparation/test_tuning_replicas.py: added CPU unit tests for
  authorization() (shared and corresponding-rank modes, mixed local/collective
  interleaving, mismatched race rejection), a 4-process (2 replicas x TP2)
  Gloo spawn test of ReplicaPreparation.prepare(), and
  RecordedPreparationSession.tune() tests covering homogeneous cross-rank
  sharding, unequal-catalog fallback with serving-replica reconciliation, and
  non-serving replica exit.

Expected benefit:
Prevents cross-rank kernel selection divergence (BindingMismatch during AOT
forward loading) when using --tuning-gpus on TP models, and scales local
candidate sharding from R-way to (R * TP)-way when all tuning GPUs and local
catalogs match (eliminating TP-way duplicate local races).

Evidence:
- pytest tests/preparation/test_tuning_replicas.py
  tests/preparation/test_tuning_shards.py tests/preparation/test_tuning_jobs.py
  tests/preparation/test_dcp_selection_groups.py tests/cli/ -q: 167 passed.
- Full tests/preparation/ run under the sibling recipes venv: 1,170 passed,
  74 skipped (GPU-gated), 3 unrelated environment failures
  (test_inspect_forward_kernels, test_runtime_static_triton,
  test_source_identity due to mismatched workspace/venv heliopause build).
- Limitations: multi-GPU end-to-end --tuning-gpus export has not yet been
  re-run on this commit.

Design-Signed-Off-By: Antigravity (Gemini); design-review=2e5b
@flare-autobot

Copy link
Copy Markdown
Collaborator

A case file has been opened for 695.
Case File Intake Report

Future reviews will auto-run 90 minutes after code updates are pushed.

  • Re-review sooner: Call @flare-autobot after push.
  • Opt-out: Include #autobot:ignore in the PR or any comment.

@flare-autobot flare-autobot added area:preparation Ignition checkpoint loading, tuning, compilation, capture, and export. bug Something isn't working performance Performance, latency, throughput, or memory-efficiency investigation or improvement. triage:review Proposed change needs code review; this does not imply merge readiness. labels Dec 2, 2026
@flare-autobot

Copy link
Copy Markdown
Collaborator

Autobot Review

Full Review · Value Proposition · Case File

Recommendation: Changes Requested

The 44-line production change in ignition/preparation/{tuning,tuning_replicas}.py is technically sound, deadlock-free, and cleanly scoped with zero blockers. Two pre-merge gaps remain: the --tuning-gpus × TP documentation now contradicts the new behavior, and both headline claims lack end-to-end GPU confirmation on ccb9f5b.

  • Blockers: 0 · Should fix: 2 · Subtract/Simplify: 3 · Nits: 2
Walkthrough
  • ignition/preparation/tuning_replicas.py:32-70, 73-82, 123-125: Adds share_across_ranks: bool = False to authorization() and ReplicaPreparation. When True, ready_cache and ready_tuning synchronize across all replicas * tensor_parallel processes (peers = states) so _tuning_results selects one global winner across every rank, while ready_collectives and ready_collective_tuning remain scoped to states[replica * tp : (replica + 1) * tp].
  • ignition/preparation/tuning.py:421, 431-451, 465-466: Gathers (self.device.identity, _local_tuning_catalog(requests)) across all replica processes (preserving the corresponding-rank geometry check). When self.autotune, tensor_parallel > 1, and all reports match reports[0], sets share_across_ranks = True, self._sharded = True, and configure_tuning_shard(index * tp + rank, range(processes)); otherwise falls back to corresponding-rank sharding (index, range(replicas)). Also tracks shard_ranks explicitly across both replica and non-replica branches.
  • ignition/preparation/tuning.py:521-535, 546-549: Replaces the self._replica_coordinator is None gate with self.tuning_replica is None or self.tuning_replica.index == 0 so serving replica 0 runs post-tuning reconcile_selections (and agree_selections when self._sharded), while non-serving replicas skip both and exit via TuningComplete. Changes the replay shard reset/restore guard from if self._sharded to if shard_ranks is not None so fallback replica runs (_sharded == False, shard_ranks == range(replicas)) also reset to (0, (0,)) during self._coordinator.prepare replay.
  • tests/preparation/test_tuning_replicas.py:1-483: Adds the first CPU test suite for tuning replicas—unit tests for authorization() in both modes (including mixed local/collective progress and mismatched race keys), a 4-process (2 × TP2) Gloo ReplicaPreparation.prepare() test, and three mocked RecordedPreparationSession.tune() tests covering homogeneous cross-rank sharding, unequal-catalog fallback with serving-replica reconciliation, and non-serving replica TuningComplete exit.
Merge Risk: Low · up to ccb9f5b
  • Narrow, preparation-only blast radius: Only 44 changed production lines across ignition/preparation/tuning.py and ignition/preparation/tuning_replicas.py; zero Helios/runtime, Protobuf, model-core, CLI, or CUDA-kernel changes. Single-rank (tensor_parallel == 1), --autotune-effort off, and multi-host (--ranks, mutually exclusive with --tuning-gpus) paths are unaffected.
  • Verified coordination & failure-isolation invariants: Cross-rank ready_cache/ready_tuning synchronization matches ProcessGroupGloo member order (replica * tp + rank), replica-local collectives stay sliced to [replica * tp : (replica + 1) * tp] and still advance while another replica waits on a local race, and non-serving replicas (index > 0) skip reconciliation and exit cleanly via their own TP-group tuning_complete barrier.
  • Minor operational side effect: On homogeneous TP>1 --tuning-gpus runs, tuning shard count widens from R to R * TP, which invalidates in-progress partial tuning checkpoints from prior R-way runs (docs/model-preparation.md:224-227); completed selection caches remain valid.
Pre-merge checks: ✅ 2 passed · ⚠️ 2 warnings

Have your agent review the Full Review for complete verified findings, file:line citations, and suggested changes.

To opt out, include #autobot:ignore in your post or comment.

@lukealonso
lukealonso merged commit 0ae3f9c into main Dec 3, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:preparation Ignition checkpoint loading, tuning, compilation, capture, and export. bug Something isn't working performance Performance, latency, throughput, or memory-efficiency investigation or improvement. triage:review Proposed change needs code review; this does not imply merge readiness.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants