Skip to content

feat: migrate multimapper-tiebreak RNG to in-tree splitmix64 - #118

Closed
BenjaminDEMAILLE wants to merge 2 commits into
scverse:mainfrom
BenjaminDEMAILLE:bd/rng-splitmix64
Closed

feat: migrate multimapper-tiebreak RNG to in-tree splitmix64#118
BenjaminDEMAILLE wants to merge 2 commits into
scverse:mainfrom
BenjaminDEMAILLE:bd/rng-splitmix64

Conversation

@BenjaminDEMAILLE

Copy link
Copy Markdown
Contributor

Summary

  • Replace the rand crate (StdRng) with STAR-rs's splitmix64-based deterministic generator for the two RNG consumers: shuffle_tied_prefix (SE/PE --outMultimapperOrder Random tie-break) and pick_primary_and_mapq (transcriptome-space primary pick).
  • Per docs-old/dev/porting-from-star-rs.md's RNG decision: this must land before STARsolo, whose UMI dedup depends on deterministic read order.
  • Keeps the existing per-read seed (run_rng_seed + read-name hash, already thread-invariant) — only the underlying generator changes, matching STAR-rs's own choice of an in-tree splitmix64 over a general-purpose PRNG crate (STAR-rs's DIVERGENCES.md D16: STAR's own mt19937 output is itself thread-count-dependent, so no external crate's output is any more "correct" to match).
  • Also migrates the test-only packed_stream.rs fuzz generator off rand, so the dependency drops from Cargo.toml entirely.

Test plan

  • cargo build --all-targets / test / clippy -D warnings / fmt --check all green (455 tests)

Replace the `rand` crate (StdRng) with STAR-rs's splitmix64-based
deterministic generator for the two RNG consumers ported so far:
`shuffle_tied_prefix` (SE/PE `--outMultimapperOrder Random` tie-break)
and `pick_primary_and_mapq` (transcriptome-space primary pick).

Per docs-old/dev/porting-from-star-rs.md's RNG decision: this must land
before STARsolo, whose UMI dedup depends on deterministic read order.
Keeps the existing per-read seed (run_rng_seed + read-name hash, already
thread-invariant); only the underlying generator changes, matching
STAR-rs's own choice of an in-tree splitmix64 over a general-purpose PRNG
crate (documented in STAR-rs's DIVERGENCES.md D16: STAR's own mt19937
output is itself thread-count-dependent, so no external crate's output is
any more "correct" to match).

Also migrates the test-only packed_stream.rs fuzz generator off `rand`,
so the dependency can be dropped from Cargo.toml entirely.

Gate green: cargo build --all-targets, clippy -D warnings, fmt, 455 tests.
@Psy-Fer

Psy-Fer commented Jul 24, 2026

Copy link
Copy Markdown
Collaborator

This is worth adopting early, as it should clean up the comparisons/tests quite a lot.

We can later assess if we continue to use this method at a later date.

@Psy-Fer

Psy-Fer commented Jul 24, 2026

Copy link
Copy Markdown
Collaborator

If you keep all your PRs on the same base commit, I can take care of merging them all. So no need to update each one.

@BenjaminDEMAILLE

Copy link
Copy Markdown
Contributor Author

If you keep all your PRs on the same base commit, I can take care of merging them all. So no need to update each one.

Oh fine so. will let you do the merge :)

@Psy-Fer Psy-Fer closed this Jul 27, 2026
Psy-Fer added a commit that referenced this pull request Jul 27, 2026
# Drop the `rand` dependency for an in-tree splitmix64 RNG (completes #118)

Replaces the external `rand` crate with a small in-tree deterministic RNG (`src/rng.rs`, splitmix64). This is #118's original goal — done correctly this time, and only after **empirically proving the RNG choice is output-neutral**

## Why

rustar-aligner's only randomness is tie-breaking and EmptyDrops Monte-Carlo — paths where the exact RNG stream is **not** STAR-faithful anyway (STAR uses mt19937; we already diverge and report a tie-adjusted metric). What actually matters is **determinism** (reproducible regardless of thread scheduling), which a fixed-seed splitmix64 gives without pulling in `rand` → `rand_core` → `rand_chacha` → `ppv-lite86` → `zerocopy`. Fewer deps = simpler build and a step toward the pure-Rust / C-toolchain-free goal.

#118 (BenjaminDEMAILLE) proposed exactly this but just deleted `rand` from `Cargo.toml` without migrating the `WeightedIndex` users, so it broke the build. This PR does the migration.

## Empirical validation (the key point)

Before touching anything, I measured whether the RNG choice changes results, on the mouse 10x set (`test/data/singlecell/`, `--soloCellFilter EmptyDrops_CR`) — the one path claimed to "need `rand`":

- **StdRng vs splitmix64: identical 229 cells (0 differ).** The RNG choice makes no difference to the EmptyDrops calls.
- **Both hit 228/229 vs STAR** — same single borderline FDR cell (Monte-Carlo noise), so splitmix64 is exactly as STAR-faithful as StdRng.
- Deterministic across thread counts (4-thread ≡ 1-thread).

So `WeightedIndex` is just a cumulative-weight sampler (~15 lines), and removing `rand` costs nothing in faithfulness.

## Changes

- **`src/rng.rs`** (new): `SplitMix64` (`next_u64`/`uniform`/`below`), `shuffle_deterministic` (Fisher–Yates), `cumulative_weights` + `sample_cumulative` (`WeightedIndex`-equivalent). Unit-tested (determinism, range, permutation, weighted distribution).
- Migrated all four production `rand` sites to it:
  - `solo/count.rs` — EmptyDrops ambient Monte-Carlo sampler.
  - `src/bin/emptydrops.rs` — standalone binary, same sampler.
  - `align/read_align.rs` — `--outMultimapperOrder Random` tie-break shuffle.
  - `lib.rs::pick_primary_and_mapq` — `--runRNGseed` primary tie-break.
  - (+ one `#[cfg(test)]` in `index/packed_stream.rs`.)
- Dropped `rand` from `Cargo.toml`; `rand`/`rand_core`/`rand_chacha`/`ppv-lite86`/`zerocopy` are gone from the dependency tree.

## Validation

- EmptyDrops: **229 cells, byte-identical** to the pre-change StdRng output; 228/229 vs STAR; deterministic across thread counts.
- Yeast benchmark unchanged: **SE 8788/8926**, **PE 8390 both-mapped** (the tie-break stream change is output-neutral — tie-adjusted faithfulness holds).
- **579 tests pass** (+5 new `rng` unit tests) · `clippy --all-targets` 0 warnings · `fmt` clean.
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.

2 participants