Skip to content

fix(p2p): bind cached peer verdicts to the policy generation (#3947) - #3964

Merged
Qubitium merged 1 commit into
mainfrom
fix/issue-3947
Nov 14, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/issue-3947

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #3947

Root cause

configure_p2p swapped _ACTIVE_CONFIG under _CONFIG_LOCK but called
clear_p2p_cache() after releasing it (gptqmodel/utils/p2p.py:318-340).
A concurrent enable_peer_access could therefore:

  1. snapshot the old min_gbps / min_ratio and probe namespace,
  2. run the long enable/validate pass with no lock held,
  3. reinsert its verdict into _STATUS_CACHE after the new policy had already
    cleared the cache.

The cache key was (mode, probe_namespace, indices) (:1193-1201) and carried
no policy identity, while the gates were read after the lookup
(:1230-1231). A stale enabled measured under a loose min_gbps survived a
reconfiguration to a stricter policy and answered later queries as enabled.

Fix

  • _CONFIG_LOCK is now an RLock; configure_p2p bumps a monotonic
    _POLICY_GENERATION and clears _STATUS_CACHE inside the lock.
  • _policy_snapshot() returns (generation, P2PConfig) atomically; a single
    call supplies mode, probe namespace and both gates, so the key and the
    classification always describe the same policy.
  • _STATUS_CACHE is keyed by (generation, mode, probe_namespace, indices).
  • _publish_status(cache_key, generation, status) publishes only while its
    generation is still current; a probe that raced a reconfiguration returns its
    own verdict to its caller but is dropped from the cache (log.debug).
  • _cached_status_for ignores entries from a non-current generation.

No new environment switches, no cached weight tensors, CUDA and ROCm paths are
unchanged.

flowchart TD
    A["enable_peer_access(devices)"] --> B["_policy_snapshot()<br/>generation, policy under _CONFIG_LOCK"]
    B --> C["mode, probe, min_gbps, min_ratio<br/>all read from that one snapshot"]
    C --> D["cache_key = (generation, mode, probe, indices)"]
    D --> E{"Cached entry for this generation?"}
    E -->|Yes| F["Return cached verdict"]
    E -->|No| G["Enable and validate every directed pair<br/>(long; no lock held)"]
    G --> H["_publish_status(cache_key, generation)"]
    H --> I{"_POLICY_GENERATION == generation?"}
    I -->|Yes| J["Publish and return"]
    I -->|No| K["Return to this caller, drop from cache"]
    classDef stale fill:#fee,stroke:#b00,color:#900
    class K stale
Loading
sequenceDiagram
    participant A as Thread A (probe)
    participant L as _CONFIG_LOCK
    participant B as Thread B (reconfigure)
    A->>L: snapshot generation 1, min_gbps=5
    A->>A: measure 10 GB/s (lock released)
    B->>L: configure_p2p(min_gbps=20)
    L->>L: generation=2, clear _STATUS_CACHE
    A->>L: _publish_status(key with generation 1)
    L-->>A: refused, generation is now 2
    B->>L: snapshot generation 2, min_gbps=20
    B->>B: no cache entry, re-probe 10 GB/s
    B-->>B: degraded (10 < 20)
Loading

Before / after repro

Deterministic interleaving: thread A enters enable_peer_access and is parked
inside the bandwidth probe, thread B calls configure_p2p with a stricter
min_gbps, then thread A is released.

Before (origin/main @ 757b7c2f) After
A (old policy, 10 GB/s) enabled enabled
B (new policy, min_gbps=20) enabled degraded
Stale verdict reused True False
Situation Before After
configure_p2p with no concurrent probe Cache cleared, next call re-probes Unchanged
configure_p2p while another thread is validating Stale verdict cached; stricter gate bypassed Verdict dropped; next call re-probes under the new policy
Repeat call under an unchanged policy Cache hit Cache hit (same generation)
_cached_status_for after a policy change Could reuse a superset verdict from the old policy Ignores entries whose generation is not current

Tests

tests/test_p2p.py:

  • test_policy_change_invalidates_a_cached_verdict
  • test_policy_change_during_a_probe_does_not_cache_a_stale_verdict (the
    deterministic interleaving regression, Event-gated with 10s bounds, no hang)
  • test_status_cache_is_keyed_by_policy_generation

Commands and results, run on this branch rebased onto origin/main @ f58837b8:

Command Result
pytest tests/test_p2p.py tests/test_p2p_copy_utils.py (CUDA, 4090) 77 passed, 29 skipped
pytest tests/test_p2p.py tests/test_p2p_copy_utils.py (ROCm, R9700) 77 passed, 29 skipped
pytest tests/qcfg/test_p2p_meta_roundtrip.py 14 passed
3 new tests against unmodified p2p.py (stashed) 2 failed, 1 passed (the non-concurrent one passes on main by design)
pytest -k test_policy_change_during_a_probe x5 with PYTHON_GIL=0 5/5 passed, sys._is_gil_enabled() is False
ruff check gptqmodel/utils/p2p.py tests/test_p2p.py All checks passed

The 29 skips are the pre-existing live multi-GPU tests that require two devices
of the same family; they skip on a single-GPU host.

Docs: docs/multi_gpu_quantization.md gains a "Policy generations and cached
verdicts" section, and pytorch_gil_0_compat.md records the new
_policy_snapshot / _publish_status / generation-keyed cache contract.

Hardware: 1x RTX 4090 (GPU-1892aea4-cc09-827c-b9a5-93d4eef7f5ae) and
1x Radeon R9700 (HIP_VISIBLE_DEVICES=0), Py3.14t cu130t / rocm101t venvs.

Risk: low. The only observable behaviour change is that a verdict produced
under a superseded policy is no longer cached; callers still receive their own
result. The cache key gained a leading generation element, so existing entries
from before the upgrade are simply not found.

configure_p2p replaced the active policy inside _CONFIG_LOCK but cleared
_STATUS_CACHE after releasing it. A concurrent enable_peer_access had
already snapshotted the previous min_gbps/min_ratio, ran a potentially
long link validation, and then reinserted its verdict under a cache key
that named only the mode, probe namespace and device indices. The result
was a verdict computed for the old gates surviving under the new policy,
so a stricter min_gbps could be silently bypassed for that pair and the
direct-copy gate stopped working.

Give the policy a monotonic generation, publish the new policy and clear
the cache in one critical section, and put the generation at the front of
the cache key. A probe now publishes only while its own generation is
still current; otherwise its verdict is returned to its own caller and
dropped, and the next call re-probes under the active policy. The mode,
probe and gate values are read once through _policy_snapshot so a single
verdict cannot mix two policies, and _cached_status_for ignores entries
from a previous generation.

_CONFIG_LOCK becomes an RLock because configure_p2p now clears the cache
while holding it, and clear_p2p_cache takes the same lock so lookup,
clear and publication share one discipline.

Fixes #3947
@Qubitium
Qubitium merged commit 7e7bd07 into main Nov 14, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-3947 branch November 14, 2026 10:32
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.

GIL=0 P2P policy reconfiguration can cache a stale peer-access verdict

1 participant