fix(exceptions): narrow broad Exception clauses to specific types (#8136)
## Summary
This PR refactors various overly broad `except Exception:` clauses
across the codebase to catch specific, anticipated exception types (such
as `ImportError`, `AttributeError`, `OSError`, `ValueError`, etc.).
This prevents the accidental swallowing of critical execution/control
flow exceptions like `KeyboardInterrupt` and `SystemExit`, aligning with
pythonic best practices and improving general debuggability.
Addresses #8093.
## Details of Changes
- **Core Utilities**:
- `deepspeed/utils/numa.py`: Catch `(FileNotFoundError,
subprocess.SubprocessError)` when checking for `numactl` command
availability.
- **Runtime & Engine**:
- `deepspeed/runtime/engine.py`: Catch `OSError` during directory
creation, and catch `BaseException` (and re-raise) during module
compilation to ensure hooks cleanup properly under all conditions.
- `deepspeed/runtime/hybrid_engine.py`: Catch `(ImportError,
AttributeError)` when querying `transformers`.
- `deepspeed/runtime/config.py`: Catch `(RuntimeError, AssertionError,
AttributeError)` when querying torch distributed rank and world size.
- **NVMe & Memory/Storage**:
- `deepspeed/nvme/parse_nvme_stats.py`: Catch `(ValueError, IndexError)`
during string parsing.
- **Module Injection & Policy Containers**:
- `deepspeed/module_inject/auto_tp.py`: Catch `(RuntimeError,
ValueError, TypeError)` during tensor operations.
- `deepspeed/module_inject/load_checkpoint.py` & policy containers
(`gpt2`, `gptneo`, `gptj`, `llama`, `llama2`, `opt`, `distil_bert`,
`clip`, `bert`): Catch `(ImportError, AttributeError)` or `ImportError`
on imports of `transformers`, `fairscale`, and `llama`.
- **Mixture of Experts (MoE) & Auto-EP**:
- `deepspeed/moe/sharded_moe.py`: Catch `ImportError` when importing
`tutel`.
- `deepspeed/module_inject/auto_ep_presets/base.py`: Catch `ImportError`
when importing `transformers.utils.output_capturing`.
- **Launcher & Autotuner**:
- `deepspeed/launcher/launch.py`: Catch `OSError` when killing the
subprocess tree.
- `deepspeed/autotuning/autotuner.py`: Catch `OSError` when creating
autotuning experiments/results directories.
- **Inference & Compilation**:
- `deepspeed/inference/v2/inference_parameter.py`: Catch `(RuntimeError,
TypeError, IndexError)` when casting device/parameters.
- `deepspeed/compile/input_storage.py`: Catch `(RuntimeError,
AttributeError)` when retrieving tensor memory format.
## Verification
- Verified code changes maintain neighboring formatting rules
(YAPF/flake8).
- Commits are signed off (`--signoff`).
---------
Signed-off-by: Sowndappan S <147894621+sowndappan5@users.noreply.github.com>
Signed-off-by: sowndappan5 <sowndappan610@gmail.com>