Repository navigation
v3.0.4 - Early NumPy Validation & Self-Healing against False Torch GPU Startup Crash
① Overview of the NumPy Early Validation & Startup Deadlock Fix
1. Background (State before the change)
In Python 3.14 environments, launching A1111 via webui-user.bat could abruptly abort during startup with the following error:
RuntimeError: Torch is not able to use GPU; add --skip-torch-cuda-test to COMMANDLINE_ARGS variable to disable this check
Despite the error message pointing to GPU or CUDA issues, the underlying GPU hardware, NVIDIA drivers, and PyTorch (torch==2.14.0+cu132) installation were completely operational.
2. Root Cause Analysis (Technical Deep Dive)
-
Destructive Unconditional Reinstallation on Every Launch:
Previously inmodules/launch_utils.py,prepare_environment()unconditionally ran:pip uninstall numpy -yfollowed bypip install --no-cache-dir numpy==2.5.3pip uninstall scipy -yfollowed bypip install --no-cache-dir scipy==1.16.1- A secondary
--force-reinstall --no-deps --no-cache-dir numpy==2.5.3after extension installers.
If any startup process was closed, killed, or interrupted while running these uninstalls, thevenv/Lib/site-packages/numpydirectory was left in a half-uninstalled state (e.g.version.pyor_distributor_init.pyremained while__init__.pyand_core/were removed).
-
PEP 420 Namespace Package Trapping PyTorch Imports:
Under Python 3.3+ (PEP 420), any directory without an__init__.pyis resolved by CPython as an implicit Namespace Package (<module 'numpy' (namespace)>).
Whenlaunch.pyexecuted its startup checkimport torch; assert torch.cuda.is_available():torch/__init__.pyimportstorch.testing(line 2720), which importstorch.testing._comparison.torch.testing._comparisoncontains:try: import numpy as np HAS_NUMPY = True except ModuleNotFoundError: HAS_NUMPY = False if HAS_NUMPY: _IS_DATACLASS_SKIP_TYPES = (*_IS_DATACLASS_SKIP_TYPES, np.ndarray, np.generic)
- Because
site-packages/numpyexisted without__init__.py,import numpy as npdid not raise ModuleNotFoundError and succeeded as a namespace package. - Accessing
np.ndarrayimmediately threwAttributeError: module 'numpy' has no attribute 'ndarray'. - Furthermore,
torchvision.utilsunconditionally executesimport numpy as np, which also crashed withModuleNotFoundError/AttributeError.
-
Startup Ordering Deadlock:
Inmodules/launch_utils.py, thecheck_run_python("import torch; assert torch.cuda.is_available()")check was positioned before line 735 (wherenumpywas reinstalled).
Becausecheck_run_pythoncaptures standard error and only checks the process exit code, theAttributeError: module 'numpy' has no attribute 'ndarray'fromimport torchcaused the GPU check to return non-zero, immediately raisingRuntimeError: Torch is not able to use GPU. As a result, execution aborted before it could ever reach the NumPy reinstallation block, creating a permanent startup deadlock.
3. What Was Changed
-
Pre-Check Self-Healing & Early Validation:
- Added an early validation step in
modules/launch_utils.pybefore_apply_torch_stack_pip_constraint()and thetorch.cuda.is_available()test. - It verifies whether NumPy is cleanly importable:
check_run_python("import numpy; assert hasattr(numpy, 'ndarray') and numpy.__version__ == '2.5.3'"). - If missing or corrupted, any broken
site-packages/numpyresidue directory is safely purged viashutil.rmtree()to prevent PEP 420 namespace package poisoning, followed by a clean reinstall ofnumpy==2.5.3.
- Added an early validation step in
-
Elimination of Redundant Startup Reinstall Cycles:
- Guarded the startup
numpy==2.5.3andscipy==1.16.1installation blocks withcheck_run_pythonassertions. - If
numpy==2.5.3(with validndarray) andscipy==1.16.1are already present in the environment, the destructivepip uninstalland--no-cache-dirnetwork downloads are skipped entirely. - The post-extension re-pin is similarly guarded to run only if an extension installer drifted NumPy away from
2.5.3.
- Guarded the startup
4. Verification Results
- Verified that clean/repaired
numpy==2.5.3enablesimport torch, torchvisionandtorch.cuda.is_available()to succeed without errors. - Verified that consecutive startups skip redundant package uninstalls, cutting startup latency and eliminating the risk of partial-uninstall filesystem corruption.
- Verified repository documentation and changelog alignment across
md/CHANGELOG.mdandREADME.md.
② Modified Files
| File | Change |
|---|---|
modules/launch_utils.py |
Added pre-check NumPy validation/self-healing before PyTorch GPU test; guarded redundant package reinstalls |
md/CHANGELOG.md |
Added v3.0.4 release notes and updated header |
README.md |
Updated NumPy pin documentation reference to 2.5.3 |