Batched Replicate GPU NMF factorize#6
Merged
OlivierBakker merged 2 commits intoJul 2, 2026
Conversation
0400937 to
c85ddc5
Compare
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).
c85ddc5 to
68e4185
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.
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_spectraoutput per replicateso the existing combine/consensus flow remains unchanged.
Changes
gpu_kwargs["batch"]/--gpu-batchbatch=1as the default single-replicate behaviorfactorizehook wheneverengine="gpu"Tests
--gpu-batchCLI/config parsing