-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-cuda.bat
More file actions
87 lines (78 loc) · 4.17 KB
/
Copy pathsetup-cuda.bat
File metadata and controls
87 lines (78 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
@echo off
REM =============================================================================
REM setup-cuda.bat -- one-time setup of an NVIDIA GPU OCR environment (.venv-cuda).
REM
REM Creates a local Python virtual environment and installs a CUDA build of
REM PyTorch (from PyTorch's official CUDA wheel index) plus the app dependencies.
REM Then start the server any time with run-gpu.bat.
REM
REM WHY A SEPARATE INSTALLER: on Windows, `pip install torch` from PyPI is
REM CPU-ONLY, so setup-cpu.bat would NOT use your NVIDIA card. This installs the
REM CUDA-enabled wheels instead. The server code is identical -- it auto-detects
REM the GPU via torch.cuda.is_available(); only the environment differs.
REM
REM Uses Python 3.12 from PATH when available; otherwise downloads a local copy
REM into .python312. Requires an NVIDIA GPU and a recent NVIDIA driver.
REM
REM CUDA VERSION: the default below targets CUDA 12.4 (cu124), which works with
REM most recent NVIDIA drivers. If PyTorch fails to see your GPU, pick the build
REM matching your driver by editing CUDA_INDEX below -- e.g.:
REM https://download.pytorch.org/whl/cu121 (CUDA 12.1)
REM https://download.pytorch.org/whl/cu124 (CUDA 12.4)
REM https://download.pytorch.org/whl/cu126 (CUDA 12.6)
REM Options list: https://pytorch.org/get-started/locally/
REM =============================================================================
setlocal
cd /d "%~dp0"
set "CUDA_INDEX=https://download.pytorch.org/whl/cu124"
REM --- Locate Python 3.12 or install it locally -------------------------------
set "PY="
if exist ".python312\python.exe" set "PY=.python312\python.exe"
if not defined PY (
py -3.12 --version >nul 2>&1 && set "PY=py -3.12"
)
if not defined PY (
echo Python 3.12 was not found. Installing a local copy in .python312 ...
powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0setup-python312.ps1"
if errorlevel 1 (
echo ERROR: local Python installation failed.
pause
exit /b 1
)
set "PY=.python312\python.exe"
)
echo Using interpreter: %PY%
%PY% --version
REM --- Create the venv (skip if it already exists) ----------------------------
if exist ".venv-cuda\Scripts\python.exe" (
findstr /C:"%CD%\.venv-cuda" ".venv-cuda\pyvenv.cfg" >nul 2>&1
if errorlevel 1 (
echo .venv-cuda belongs to a previous folder location -- rebuilding it here.
rmdir /s /q ".venv-cuda"
) else (
echo .venv-cuda already exists -- reusing it.
)
)
if not exist ".venv-cuda\Scripts\python.exe" (
echo Creating virtual environment in .venv-cuda ...
%PY% -m venv .venv-cuda || (echo ERROR: failed to create venv & pause & exit /b 1)
)
echo Upgrading pip ...
".venv-cuda\Scripts\python.exe" -m pip install --upgrade pip wheel || (echo ERROR: pip upgrade failed & pause & exit /b 1)
REM --- Install a CUDA build of PyTorch FIRST (so the app deps are satisfied by
REM it and pip won't pull the CPU build from PyPI) ----------------------------
echo Installing CUDA PyTorch from %CUDA_INDEX% ...
".venv-cuda\Scripts\python.exe" -m pip install torch torchvision --index-url %CUDA_INDEX% || (echo ERROR: torch install failed -- check CUDA_INDEX / network & pause & exit /b 1)
REM --- App dependencies (torch already satisfied by the CUDA wheels) -----------
echo Installing requirements.txt ...
".venv-cuda\Scripts\python.exe" -m pip install -r requirements.txt || (echo ERROR: pip install failed & pause & exit /b 1)
REM --- Verify torch sees the GPU ----------------------------------------------
echo.
echo Verifying CUDA is visible to PyTorch ...
".venv-cuda\Scripts\python.exe" -c "import torch; ok=torch.cuda.is_available(); print('torch', torch.__version__, '| CUDA available:', ok); print('device 0:', torch.cuda.get_device_name(0)) if ok else print('WARNING: no CUDA GPU detected -- the server will run on CPU. Check your NVIDIA driver and the CUDA_INDEX version in this script.')"
echo.
echo =============================================================================
echo Done. Start the server with: run-gpu.bat
echo (The first server launch downloads the OCR model weights -- a few hundred MB.)
echo =============================================================================
pause