fix: GPU training silently scores every sampled trajectory 0 — CPU sampling Generator vs CUDA multinomial - #421
Merged
James-CUDA merged 1 commit intoJul 23, 2026
Conversation
… 0 (CPU rng vs CUDA probs)
trajectory_sampling_rng builds a CPU torch.Generator, but the head - and so
the softmax probabilities select() feeds torch.multinomial - lives on the
training device (configs/trinity.yaml: cuda:0). torch.multinomial requires
its input and its generator to share a device, so on any GPU run the first
sampled turn of every trajectory raised
RuntimeError: Expected a 'cuda' device type for generator but found 'cpu'
evaluate_candidate's gather(return_exceptions=True) swallowed the error and
scored every trajectory 0.0, so sep-CMA-ES silently optimized a flat all-zero
fitness landscape while the run 'completed successfully'. The CPU-only test
suite could never see it (CPU probs + CPU generator match).
Fix: draw on the generator's device. select() now moves the two tiny prob
vectors onto rng.device before multinomial. This also makes the seeded draw
device-independent: the same --seed picks the same (agent, role) whether the
head runs on CPU or CUDA. rng=None keeps sampling on the probs' own device.
Regression tests simulate the device mismatch without a GPU via a
meta-device generator stand-in plus a multinomial spy, and pin the rng=None
no-move behavior.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
James-CUDA
force-pushed
the
fix/critical-issue-sampling-rng-device
branch
from
July 23, 2026 11:32
c2d3208 to
1d1787d
Compare
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.
Root cause
optim.sampling.trajectory_sampling_rng(added in #171 to make--seedreproduce a run) builds a CPUtorch.Generator.optim.fitness.evaluate_candidatepasses it intohead.select(..., sample=True), wheretorch.multinomialdraws from softmax probabilities living on the head's device —cuda:0perconfigs/trinity.yamlandCoordinatorPolicy.build's default. PyTorch requiresmultinomial's input and generator to share a device, so on any GPU run the first sampled turn of every trajectory raises:evaluate_candidate'sasyncio.gather(..., return_exceptions=True)then swallows the error and scores the trajectory 0.0 (by design, for transient API failures), printing only[warn] N/N trajectories failed. Net effect: every candidate in every generation gets fitness 0, sep-CMA-ES optimizes a flat all-zero landscape, and the run 'completes successfully', writing a meaninglessbest_theta.npy.train.pyalways passessample=True, run_seed=args.seed(default 0), so every real GPU training run since #171 is affected. The CPU-only CI can never see it — CPU probs + CPU generator match.Fix
LinearHead.selectnow draws on the generator's device: the two prob vectors ((n_models,)/(n_roles,)— a few floats) are moved torng.devicebeforemultinomial.rng=Nonebehavior is unchanged (samples on the probs' own device).Bonus: the seeded draw becomes device-independent — the same
--seedpicks the same (agent, role) sequence whether the head runs on CPU or CUDA, strengthening #171's reproducibility goal.Impact / risk
.to()is a no-op on matching devices) and none for eval (sample=Falsepath untouched).multinomialspy): they fail on pre-fix code, pass post-fix. Therng=Noneno-move contract is pinned too.Note on CI: current
mainfails collection repo-wide (unresolved merge-conflict markers insrc/trinity/analysis/__init__.py, issue #404 / PRs #402, #405). This PR's checks will go green once that lands; the fix itself is ruff-clean and its test file passes (21/21) locally.🤖 Generated with Claude Code