Skip to content

fix(exceptions): narrow broad Exception clauses to specific types#8136

Open
sowndappan5 wants to merge 7 commits into
deepspeedai:masterfrom
sowndappan5:fix/narrow-exceptions
Open

fix(exceptions): narrow broad Exception clauses to specific types#8136
sowndappan5 wants to merge 7 commits into
deepspeedai:masterfrom
sowndappan5:fix/narrow-exceptions

Conversation

@sowndappan5

Copy link
Copy Markdown
Contributor

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).

sowndappan5 and others added 7 commits June 10, 2026 07:57
Signed-off-by: Sowndappan S <147894621+sowndappan5@users.noreply.github.com>
Signed-off-by: Sowndappan S <147894621+sowndappan5@users.noreply.github.com>
…arses

Signed-off-by: sowndappan5 <sowndappan610@gmail.com>
Signed-off-by: sowndappan5 <sowndappan610@gmail.com>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: be8a4a1a57

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread deepspeed/launcher/launch.py
Comment thread deepspeed/moe/sharded_moe.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant