Skip to content

Batched Replicate GPU NMF factorize#6

Merged
OlivierBakker merged 2 commits into
TrynkaLab:mainfrom
JerryIshihara:features/batch-parallel-option
Jul 2, 2026
Merged

Batched Replicate GPU NMF factorize#6
OlivierBakker merged 2 commits into
TrynkaLab:mainfrom
JerryIshihara:features/batch-parallel-option

Conversation

@JerryIshihara

Copy link
Copy Markdown

Summary

Adds batched-replicate GPU factorization for cNMF via --gpu-batch.

This lets the GPU factorize path group same-K replicates and run multiple seeds in
one PyTorch MU launch, while still writing one iter_spectra output per replicate
so the existing combine/consensus flow remains unchanged.

Changes

  • Adds gpu_kwargs["batch"] / --gpu-batch
  • Refactors the GPU MU kernel to support stacked replicate tensors
  • Keeps batch=1 as the default single-replicate behavior
  • Adds fixed-H batch-aware refit support for consensus usage
  • Installs GPU factorize hook whenever engine="gpu"
  • Keeps CPU engine behavior unchanged

Tests

  • batched MU matching single-seed runs
  • batch grouping invariance
  • distinct seed diversity
  • fixed-H batched consensus refit
  • --gpu-batch CLI/config parsing
  • cNMF factorize grouping by K and writing one spectra file per replicate

@JerryIshihara JerryIshihara force-pushed the features/batch-parallel-option branch from 0400937 to c85ddc5 Compare July 2, 2026 15:55
Run R cNMF replicates of the same k in one GPU launch via a batch-aware MU
kernel. batch=1 is the single-replicate case (R=1), so one code path serves
both single and multi replicate — routing depends only on the engine (cpu vs
gpu), never on batch size.

- Batch-aware MU: _mu_step/_fit_mu (and fixed-H _mu_step_fixed_h/_fit_mu_fixed_h)
  operate on 2D [n,k]/[k,g] or stacked [R,n,k]/[R,k,g] with X broadcast as
  [1,n,g]; `.transpose(-2,-1)` reduces to `.T` on 2D, so R=1 is bit-identical
  (sklearn parity preserved). Relative-error stop is per replicate; iteration
  halts once all slices converge.
- Kernels: _nmf_gpu_mu (full NMF, update W and H) and _nmf_gpu_fixed_h (consensus
  refit; H fixed and shared as [1,k,g], update W only), each taking a seed list
  and returning one (spectra, usages) per seed. _nmf_gpu is the R=1 dispatch to
  either; per-seed init gives replicate diversity for consensus.
- cNMF integration (cnmf.py untouched; monkeypatch injection): configure_nmf_engine
  installs factorize_gpu (the GPU cNMF.factorize — groups this worker's jobs by
  n_components, chunks into gpu_kwargs['batch'] replicates per launch, writes one
  iter_spectra per (k,iter) so combine/consensus are unchanged) and nmf_gpu (the
  cNMF._nmf adapter used by the consensus refit).
- CLI: --gpu-batch (factorize only, default 1), plumbed through DEFAULT_GPU /
  parse / validate / _resolve_gpu_opts (positive int, floors to 1).
Batched GPU factorize:
- Batch kernels (_nmf_gpu_mu): per-seed parity vs single, R=1 reduction, batch-
  group invariance, distinct-seed diversity, empty-seed rejection.
- Batch-aware fixed-H refit (_nmf_gpu_fixed_h): H stays fixed, batched-vs-single
  per seed, distinct usages, and _nmf_gpu(update_H=False) dispatch.
- _fit_mu batch-awareness/stop; --gpu-batch config plumbing; factorize_gpu
  grouping/chunking/one-write-per-replicate via a fake kernel.

CLI / engine wiring / integration (ported from the consensus test suite and
adapted to the renamed API):
- parse_gpu_args / gpu_kwargs_from_args / validate_engine_args_for_command CLI
  contract; configure_nmf_engine cpu-noop, unknown-engine, and gpu-adapter patch.
- Fixed-H unit tests (_to_checked_fixed_h, _mu_step_fixed_h, _fit_mu_fixed_h,
  _execution_plan) and the single-instance fixed-H refit.
- cNMF <-> GPU routing: factorize_gpu forwards seed/components/run-params/gpu
  kwargs to _nmf_gpu_mu; refit_usage / refit_spectra / consensus route through
  the nmf_gpu adapter; default CPU engine keeps the sklearn path unchanged.

Existing single-instance tests use the renamed public surface (nmf_gpu /
factorize_gpu adapters; _nmf_gpu single dispatch).
@JerryIshihara JerryIshihara force-pushed the features/batch-parallel-option branch from c85ddc5 to 68e4185 Compare July 2, 2026 16:07
@OlivierBakker OlivierBakker merged commit c625d0c into TrynkaLab:main Jul 2, 2026
1 check passed
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