diff --git a/launch_distributed.bat b/launch_distributed.bat index 77781bd..c62ac5a 100644 --- a/launch_distributed.bat +++ b/launch_distributed.bat @@ -1,11 +1,15 @@ @echo off -title ComfyUI Multi-GPU CLI Launcher +title ComfyUI Multi-GPU Fast Launcher (Check & Start) cd /d "%~dp0" color 0B cls -echo ComfyUI Multi-GPU CLI Launcher -echo =============================== +echo ComfyUI Multi-GPU Fast Launcher (Check ^& Start) +echo =============================================== +echo. +echo This launcher checks for running instances and starts only +echo those that aren't running, with a 2-second delay between launches. +echo. :: Check if gpu_config.json exists (relative path) set "CONFIG_PATH=ComfyUI\custom_nodes\ComfyUI-Distributed\gpu_config.json" @@ -38,10 +42,18 @@ if %ERRORLEVEL% NEQ 0 ( exit /b 1 ) +:: Configure Windows memory management for large model loading +echo Configuring memory management for large models... + +:: Configure PyTorch memory allocation for large models +set PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True + +echo Memory management configured. + :: Read configuration with PowerShell and store in environment variables echo Loading configuration... -powershell -NoProfile -Command "try { $json = Get-Content '%CONFIG_PATH%' -Raw | ConvertFrom-Json; $master = $json.master; $settings = $json.settings; $retryDelay = if ($settings.retry_delay_ms) { $settings.retry_delay_ms } else { 500 }; Write-Output \"RETRY_DELAY=$retryDelay\"; Write-Output \"MASTER_PORT=$($master.port)\"; Write-Output \"MASTER_CUDA=$($master.cuda_device)\"; foreach ($worker in $json.workers) { if ($worker.enabled) { Write-Output \"WORKER_$($worker.id)_NAME=$($worker.name)\"; Write-Output \"WORKER_$($worker.id)_PORT=$($worker.port)\"; Write-Output \"WORKER_$($worker.id)_CUDA=$($worker.cuda_device)\"; Write-Output \"WORKER_$($worker.id)_EXTRA=$($worker.extra_args)\" } } } catch { Write-Output \"PowerShell Error: $_\"; pause; exit 1 }" > %TEMP%\config_vars.txt +powershell -NoProfile -Command "try { $json = Get-Content '%CONFIG_PATH%' -Raw | ConvertFrom-Json; $master = $json.master; Write-Output \"MASTER_PORT=$($master.port)\"; Write-Output \"MASTER_CUDA=$($master.cuda_device)\"; Write-Output \"MASTER_EXTRA=$($master.extra_args)\"; foreach ($worker in $json.workers) { if ($worker.enabled) { Write-Output \"WORKER_$($worker.id)_NAME=$($worker.name)\"; Write-Output \"WORKER_$($worker.id)_PORT=$($worker.port)\"; Write-Output \"WORKER_$($worker.id)_CUDA=$($worker.cuda_device)\"; Write-Output \"WORKER_$($worker.id)_EXTRA=$($worker.extra_args)\" } } } catch { Write-Output \"PowerShell Error: $_\"; pause; exit 1 }" > %TEMP%\config_vars.txt if %ERRORLEVEL% NEQ 0 ( color 0C @@ -56,59 +68,41 @@ for /f "tokens=*" %%i in (%TEMP%\config_vars.txt) do ( del %TEMP%\config_vars.txt >nul 2>&1 -:: Check Master -echo Starting Master instance... -:MASTER +echo. +echo Checking running instances... +echo. + +set "INSTANCES_STARTED=0" + +:: Check and start Master if not running +echo Checking Master (port %MASTER_PORT%)... powershell -NoProfile -Command "$ErrorActionPreference = 'SilentlyContinue'; $tcpClient = New-Object System.Net.Sockets.TcpClient; $connect = $tcpClient.BeginConnect('127.0.0.1', %MASTER_PORT%, $null, $null); $wait = $connect.AsyncWaitHandle.WaitOne(200, $false); if (!$wait) { exit 1 } else { $tcpClient.EndConnect($connect); $tcpClient.Close(); exit 0 }" if %ERRORLEVEL% == 1 ( - if not defined MASTER_STARTED ( - start wt -w 0 new-tab --title "Master - ComfyUI CLI" --startingDirectory "%CD%" cmd /k "title Master - ComfyUI CLI && call venv\Scripts\activate.bat && set CUDA_VISIBLE_DEVICES=%MASTER_CUDA% && comfy --here launch -- --port %MASTER_PORT%" - set MASTER_STARTED=1 - ) - nul +) else ( + echo Master already running on port %MASTER_PORT% ) -echo Master ready! -:: Process workers sequentially - wait for each to be ready before starting next +:: Check and start workers powershell -NoProfile -Command "try { $json = Get-Content '%CONFIG_PATH%' -Raw | ConvertFrom-Json; foreach ($worker in $json.workers) { if ($worker.enabled) { Write-Output $worker.id } } } catch { exit 1 }" > %TEMP%\worker_ids.txt for /f %%w in (%TEMP%\worker_ids.txt) do ( - call :PROCESS_WORKER %%w + call :CHECK_AND_START_WORKER %%w ) del %TEMP%\worker_ids.txt >nul 2>&1 -goto :END_SCRIPT - -:PROCESS_WORKER -set "WORKER_ID=%1" -call set "WORKER_NAME=%%WORKER_%WORKER_ID%_NAME%%" -call set "WORKER_PORT=%%WORKER_%WORKER_ID%_PORT%%" -call set "WORKER_CUDA=%%WORKER_%WORKER_ID%_CUDA%%" -call set "WORKER_EXTRA=%%WORKER_%WORKER_ID%_EXTRA%%" - -echo Starting %WORKER_NAME%... - -:WORKER_CHECK_LOOP -powershell -NoProfile -Command "$ErrorActionPreference = 'SilentlyContinue'; $tcpClient = New-Object System.Net.Sockets.TcpClient; $connect = $tcpClient.BeginConnect('127.0.0.1', %WORKER_PORT%, $null, $null); $wait = $connect.AsyncWaitHandle.WaitOne(200, $false); if (!$wait) { exit 1 } else { $tcpClient.EndConnect($connect); $tcpClient.Close(); exit 0 }" -if %ERRORLEVEL% == 1 ( - if not defined WORKER%WORKER_ID%_STARTED ( - start wt -w 0 new-tab --title "%WORKER_NAME% - ComfyUI CLI" --startingDirectory "%CD%" cmd /k "title %WORKER_NAME% - ComfyUI CLI && call venv\Scripts\activate.bat && set CUDA_VISIBLE_DEVICES=%WORKER_CUDA% && comfy --here launch -- --port %WORKER_PORT% %WORKER_EXTRA% --enable-cors-header" - set WORKER%WORKER_ID%_STARTED=1 - ) - nul -exit \ No newline at end of file +exit + +:CHECK_AND_START_WORKER +set "WORKER_ID=%1" +call set "WORKER_NAME=%%WORKER_%WORKER_ID%_NAME%%" +call set "WORKER_PORT=%%WORKER_%WORKER_ID%_PORT%%" +call set "WORKER_CUDA=%%WORKER_%WORKER_ID%_CUDA%%" +call set "WORKER_EXTRA=%%WORKER_%WORKER_ID%_EXTRA%%" + +echo Checking %WORKER_NAME% (port %WORKER_PORT%)... +powershell -NoProfile -Command "$ErrorActionPreference = 'SilentlyContinue'; $tcpClient = New-Object System.Net.Sockets.TcpClient; $connect = $tcpClient.BeginConnect('127.0.0.1', %WORKER_PORT%, $null, $null); $wait = $connect.AsyncWaitHandle.WaitOne(200, $false); if (!$wait) { exit 1 } else { $tcpClient.EndConnect($connect); $tcpClient.Close(); exit 0 }" +if %ERRORLEVEL% == 1 ( + echo %WORKER_NAME% NOT running - Starting with CUDA_VISIBLE_DEVICES=%WORKER_CUDA% + start wt -w 0 new-tab --title "%WORKER_NAME% - ComfyUI CLI" --startingDirectory "%CD%" cmd /k "title %WORKER_NAME% - ComfyUI CLI && echo CUDA_VISIBLE_DEVICES=%WORKER_CUDA% && call venv\Scripts\activate.bat && set CUDA_VISIBLE_DEVICES=%WORKER_CUDA% && comfy --here launch -- --port %WORKER_PORT% %WORKER_EXTRA% --enable-cors-header" + set /a INSTANCES_STARTED+=1 + echo Waiting 2 seconds before checking next instance... + timeout /t 2 /nobreak >nul +) else ( + echo %WORKER_NAME% already running on port %WORKER_PORT% +) +goto :eof