Repository navigation
fix(p2p): bind cached peer verdicts to the policy generation (#3947) - #3964
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3947
Root cause
configure_p2pswapped_ACTIVE_CONFIGunder_CONFIG_LOCKbut calledclear_p2p_cache()after releasing it (gptqmodel/utils/p2p.py:318-340).A concurrent
enable_peer_accesscould therefore:min_gbps/min_ratioand probe namespace,_STATUS_CACHEafter the new policy had alreadycleared the cache.
The cache key was
(mode, probe_namespace, indices)(:1193-1201) and carriedno policy identity, while the gates were read after the lookup
(
:1230-1231). A staleenabledmeasured under a loosemin_gbpssurvived areconfiguration to a stricter policy and answered later queries as
enabled.Fix
_CONFIG_LOCKis now anRLock;configure_p2pbumps a monotonic_POLICY_GENERATIONand clears_STATUS_CACHEinside the lock._policy_snapshot()returns(generation, P2PConfig)atomically; a singlecall supplies mode, probe namespace and both gates, so the key and the
classification always describe the same policy.
_STATUS_CACHEis keyed by(generation, mode, probe_namespace, indices)._publish_status(cache_key, generation, status)publishes only while itsgeneration 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_forignores 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 stalesequenceDiagram 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)Before / after repro
Deterministic interleaving: thread A enters
enable_peer_accessand is parkedinside the bandwidth probe, thread B calls
configure_p2pwith a strictermin_gbps, then thread A is released.origin/main@757b7c2f)enabledenabledenableddegradedTrueFalseconfigure_p2pwith no concurrent probeconfigure_p2pwhile another thread is validating_cached_status_forafter a policy changeTests
tests/test_p2p.py:test_policy_change_invalidates_a_cached_verdicttest_policy_change_during_a_probe_does_not_cache_a_stale_verdict(thedeterministic interleaving regression, Event-gated with 10s bounds, no hang)
test_status_cache_is_keyed_by_policy_generationCommands and results, run on this branch rebased onto
origin/main@f58837b8:pytest tests/test_p2p.py tests/test_p2p_copy_utils.py(CUDA, 4090)pytest tests/test_p2p.py tests/test_p2p_copy_utils.py(ROCm, R9700)pytest tests/qcfg/test_p2p_meta_roundtrip.pyp2p.py(stashed)pytest -k test_policy_change_during_a_probex5 withPYTHON_GIL=0sys._is_gil_enabled() is Falseruff check gptqmodel/utils/p2p.py tests/test_p2p.pyThe 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.mdgains a "Policy generations and cachedverdicts" section, and
pytorch_gil_0_compat.mdrecords the new_policy_snapshot/_publish_status/ generation-keyed cache contract.Hardware: 1x RTX 4090 (
GPU-1892aea4-cc09-827c-b9a5-93d4eef7f5ae) and1x Radeon R9700 (
HIP_VISIBLE_DEVICES=0), Py3.14tcu130t/rocm101tvenvs.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.