Split any YouTube song into stems
TypeScript
feat: linux cuda gpu splits (toggle, cpu bootstrap, cuda smoke)
VinewZ461312948981
diff --git a/src/main/env.ts b/src/main/env.tsindex aef834b..16b6677 100644--- a/src/main/env.ts+++ b/src/main/env.ts@@ -36,6 +36,10 @@ const IS_WIN = process.platform === 'win32'const EXE = IS_WIN ? '.exe' : ''const VENV_BIN = IS_WIN ? 'Scripts' : 'bin'+/* CUDA torch (the GPU engine swap) works on windows and linux; macOS stays+ on MPS/CPU via the default torch build */+const SUPPORTS_GPU = IS_WIN || process.platform === 'linux'+export function venvDir(): string {return join(userDataDir(), 'venv')}@@ -158,10 +162,10 @@ export function gpuAccelerationInfo(): boolean | undefined {let nvidiaProbe: Promise<boolean> | null = nulllet nvidiaInfo: boolean | undefined-/* windows only: whether an NVIDIA GPU is present (nvidia-smi ships with the- driver). Gates the GPU-acceleration toggle in Settings */+/* windows + linux: whether an NVIDIA GPU is present (nvidia-smi ships with+ the driver). Gates the GPU-acceleration toggle in Settings */export function detectNvidiaGpu(): Promise<boolean> {- if (!IS_WIN) {+ if (!SUPPORTS_GPU) {nvidiaInfo = falsereturn Promise.resolve(false)}@@ -694,10 +698,12 @@ export function ensureFtWeights(onProgress?: (pct: number) => void): Promise<booreturn ftWeightsPromise.then(detach)}-/* CUDA build of torch (windows + nvidia). The default bootstrap installs the- CPU wheel from PyPI; this swaps in the cu121 build (~2.5GB download) on- demand when the GPU-acceleration toggle is enabled. It stays installed when- the toggle goes back off — CUDA torch handles cpu devices fine */+/* CUDA build of torch (windows/linux + nvidia). The default bootstrap+ installs the CPU wheel from PyPI (on linux pinned to the cpu index, since+ the default linux wheel would otherwise pull CUDA deps for everyone);+ this swaps in the cu121 build (~2.5GB download) on demand when the+ GPU-acceleration toggle is enabled. It stays installed when the toggle+ goes back off — CUDA torch handles cpu devices fine */const GPU_TORCH_VERSION = '2.5.1'const GPU_TORCH_INDEX = 'https://download.pytorch.org/whl/cu121'@@ -713,7 +719,7 @@ export function ensureGpuEngine(if (onProgress) gpuProgressListeners.delete(onProgress)return true}- if (!IS_WIN) {+ if (!SUPPORTS_GPU) {detach()return Promise.resolve(false)}@@ -829,7 +835,7 @@ export function engineStatus(): EngineStatus {ftDownloading: ftWeightsPromise !== null,ftVerified,gpuDownloading: gpuEnginePromise !== null,- gpuReady: IS_WIN && gpuInfo === true+ gpuReady: SUPPORTS_GPU && gpuInfo === true}}@@ -956,6 +962,36 @@ export async function bootstrap(): Promise<boolean> {sendEnvEvent('Downloading the separation engine — grab a coffee')let lastGeneric = 0+ // on linux the default PyPI torch wheel is CUDA-enabled and would pull+ // ~2GB of nvidia deps for every user, GPU or not — install the cpu build+ // in its own step (windows/mac defaults are already CPU, so only linux+ // needs the override) and let the GPU toggle swap in cu121 on demand.+ // --index-url can't be mixed into the big install below: the cpu index+ // only hosts torch packages, so non-torch deps would fail to resolve.+ if (process.platform === 'linux') {+ await new Promise<void>((resolve, reject) => {+ const child = spawn(pip, [+ '-m',+ 'pip',+ 'install',+ '--progress-bar',+ 'off',+ 'torch==2.5.1',+ 'torchaudio==2.5.1',+ '--index-url',+ 'https://download.pytorch.org/whl/cpu'+ ])+ child.stderr?.on('data', (chunk: Buffer) => {+ const t = chunk.toString().trim()+ if (t.startsWith('ERROR') || t.startsWith('error')) sendEnvEvent(t.slice(0, 200), 'error')+ })+ child.on('close', (code) =>+ code === 0 ? resolve() : reject(new Error(`cpu torch install failed (${code})`))+ )+ child.on('error', reject)+ })+ }+ // (already satisfied on linux by the cpu step above — pip skips it)await new Promise<void>((resolve, reject) => {const child = spawn(pip, ['-m',diff --git a/src/main/pipeline.ts b/src/main/pipeline.tsindex 1819909..80fbf39 100644--- a/src/main/pipeline.ts+++ b/src/main/pipeline.ts@@ -124,13 +124,14 @@ export async function startJob((settings.htdemucsFt && engine !== MODEL_EXTENDED ? '-ft' : '') +(settings.shifts === 2 ? '@s2' : '')- // windows honors the GPU toggle: 'cpu' is passed explicitly because- // roformer.py's 'auto' prefers CUDA whenever the venv's torch supports it.- // macOS keeps 'auto' (MPS when available)- const useGpu = settings.gpuSplit && process.platform === 'win32'+ // windows/linux honor the GPU toggle: 'cpu' is passed explicitly because+ // roformer.py's 'auto' prefers CUDA whenever the venv's torch supports it+ // (and the default linux wheel is CUDA-capable, so toggle-off must still+ // force CPU). macOS keeps 'auto' (MPS when available)+ const useGpu = settings.gpuSplit && process.platform !== 'darwin'const deviceArg = (): string => {- if (process.platform === 'win32') return useGpu ? 'cuda' : 'cpu'- return 'auto'+ if (process.platform === 'darwin') return 'auto'+ return useGpu ? 'cuda' : 'cpu'}const job: ActiveJob = { videoId, model: modelTag, cancelled: false }diff --git a/src/main/smoke.ts b/src/main/smoke.tsindex 9567028..19ebe90 100644--- a/src/main/smoke.ts+++ b/src/main/smoke.ts@@ -18,8 +18,11 @@ import {} from './env'/* self-test mode, driven by STEMKIT_SMOKE=1 (used by the windows-smoke CI- job): runs the real bootstrap then separates a generated tone through- both engines and exits 0/1. No window is created */+ job and manually on linux): runs the real bootstrap then separates a+ generated tone through both engines and exits 0/1. No window is created.+ On machines with an NVIDIA GPU the GPU section runs real cuda separations;+ on GPU-less runners it checks the fail-fast paths and the CUDA wheel swap+ with torch.cuda.is_available() staying false */const LOG_PATH = join(tmpdir(), 'stemkit-smoke.log')@@ -212,11 +215,11 @@ export async function runSmoke(): Promise<boolean> {}log('roformer vocals ok')- // GPU plumbing on this GPU-less runner: --device cuda must fail fast- // with the friendly message (on the default cpu torch, before any big- // downloads), and the CUDA wheel swap must install cleanly while- // torch.cuda.is_available() stays false (no NVIDIA driver here). The- // real CUDA execution path still needs a machine with an NVIDIA GPU+ // GPU plumbing: --device cuda must fail fast with the friendly message+ // on the default cpu torch (before any big downloads). After the CUDA+ // wheel swap, a GPU-less runner expects torch.cuda.is_available() to+ // stay false, while an NVIDIA machine runs real cuda separations+ // through both enginesconst nvidia = await detectNvidiaGpu()log(`nvidia gpu detected: ${nvidia}`)@@ -244,25 +247,67 @@ export async function runSmoke(): Promise<boolean> {'-c', 'import torch;print("cuda", torch.version.cuda, int(torch.cuda.is_available()))'])const [, ver, avail] = info.trim().split(/\s+/)- if (!ver || ver === 'None' || avail !== '0') {- log(`FAIL: unexpected torch cuda state: "${info.trim()}"`)+ // driver present: cuda must be usable; otherwise it must stay off+ const want = nvidia ? '1' : '0'+ if (!ver || ver === 'None' || avail !== want) {+ log(`FAIL: unexpected torch cuda state: "${info.trim()}" (want available=${want})`)return false}- log(`cuda torch ok (version.cuda=${ver}, available=0)`)+ log(`cuda torch ok (version.cuda=${ver}, available=${avail})`)} catch (e) {log(`FAIL: could not probe torch cuda state: ${e instanceof Error ? e.message : String(e)}`)return false}- if (- !(await expectCudaFailure('cuda on cuda torch without nvidia driver', [+ if (!nvidia) {+ if (+ !(await expectCudaFailure('cuda on cuda torch without nvidia driver', [+ roformerScript(),+ '--input', mix,+ '--out', join(dir, 'stems-cuda-roformer'),+ '--ckpt-dir', modelsDir(),+ '--device', 'cuda'+ ]))+ ) {+ return false+ }+ } else {+ // real CUDA execution on this machine's GPU, both engines+ const cudaDemucsOut = join(dir, 'stems-cuda-demucs')+ const cudaDemucs = await runScript(venvPython(), [+ separateScript(),+ '--input', mix,+ '--out', cudaDemucsOut,+ '--model', 'htdemucs',+ '--device', 'cuda'+ ])+ if (!cudaDemucs.ok) {+ log(`FAIL: demucs cuda separation: ${cudaDemucs.error}`)+ return false+ }+ log(`demucs cuda stems: ${cudaDemucs.stems.join(', ')}`)+ for (const stem of cudaDemucs.stems) {+ if (!isFloat32Wav(join(cudaDemucsOut, `${stem}.wav`))) {+ log(`FAIL: cuda ${stem}.wav missing or not float32`)+ return false+ }+ }+ const cudaRoformerOut = join(dir, 'stems-cuda-roformer')+ const cudaRoformer = await runScript(venvPython(), [roformerScript(),'--input', mix,- '--out', join(dir, 'stems-cuda-roformer'),+ '--out', cudaRoformerOut,'--ckpt-dir', modelsDir(),'--device', 'cuda'- ]))- ) {- return false+ ])+ if (!cudaRoformer.ok) {+ log(`FAIL: roformer cuda separation: ${cudaRoformer.error}`)+ return false+ }+ if (!isFloat32Wav(join(cudaRoformerOut, 'vocals.wav'))) {+ log('FAIL: cuda vocals.wav missing or not float32')+ return false+ }+ log('cuda separations ok (demucs + roformer)')}}diff --git a/src/shared/types.ts b/src/shared/types.tsindex 56ad6cd..c9c6db0 100644--- a/src/shared/types.ts+++ b/src/shared/types.ts@@ -20,7 +20,7 @@ export interface AppSettings {shifts: 1 | 2htdemucsFt: booleanroformerVocals: boolean- // windows + nvidia: separate on the GPU instead of the CPU. The toggle is+ // windows/linux + nvidia: separate on the GPU instead of the CPU. The toggle is// only rendered when an NVIDIA GPU is detected; enabling it downloads the// CUDA build of torch (~2.5GB) on first usegpuSplit: boolean@@ -45,7 +45,7 @@ export interface EngineStatus {vocalsReady: booleanftDownloading: booleanftVerified: boolean- // cuda torch engine (windows + nvidia only)+ // cuda torch engine (windows/linux + nvidia only)gpuDownloading: booleangpuReady: boolean}@@ -57,7 +57,7 @@ export interface EnvStatus {bootstrapping: booleanupdating: booleangpu?: boolean- // windows only: an NVIDIA GPU was detected (gates the GPU toggle in Settings)+ // windows/linux only: an NVIDIA GPU was detected (gates the GPU toggle in Settings)nvidiaGpu?: boolean}