Skip to content

Add type hints to top-level public API functions#8079

Open
arunshar wants to merge 2 commits into
deepspeedai:masterfrom
arunshar:typing/public-api-hints
Open

Add type hints to top-level public API functions#8079
arunshar wants to merge 2 commits into
deepspeedai:masterfrom
arunshar:typing/public-api-hints

Conversation

@arunshar

Copy link
Copy Markdown

What

Adds precise type hints to the top-level public API functions in deepspeed/__init__.py, addressing #8074:

  • initialize — annotates the previously-untyped args, mpu, collate_fn, config, mesh_param, config_params parameters and adds the return type.
  • init_inference, default_inference_config, add_config_arguments, tp_model_init, set_optimizer_flags.

Why

#8074 asks for type annotations on the public API. This is a scoped first increment covering the top-level deepspeed.* entry points (the functions users call directly), which keeps the diff small and reviewable; follow-ups can extend to deeper modules.

Notes

  • Annotations only — no runtime behavior change.
  • pre-commit run --files deepspeed/__init__.py is green (yapf, flake8, check-torchdist, check-license, codespell).
  • initialize returns Tuple[DeepSpeedEngine, Optional[Optimizer], Optional[DataLoader], Optional[_LRScheduler]] — both DeepSpeedHybridEngine and PipelineEngine subclass DeepSpeedEngine.
  • Genuinely dynamic / duck-typed parameters (args, mpu, mesh_param) are annotated Any.

Opened as a draft for early feedback on the scope and the chosen annotations.

Addresses #8074

Annotate the public API entry points in deepspeed/__init__.py (initialize, init_inference, default_inference_config, add_config_arguments, tp_model_init, set_optimizer_flags) with precise parameter and return types, addressing deepspeedai#8074. Scoped first increment for the top-level deepspeed.* public API surface; no runtime behavior changes.

Signed-off-by: Arun Sharma <sharm485@umn.edu>
@arunshar arunshar force-pushed the typing/public-api-hints branch from c1711bb to d33fe7f Compare June 19, 2026 23:34
@arunshar arunshar marked this pull request as ready for review June 19, 2026 23:53

@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: d33fe7ff58

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread deepspeed/__init__.py Outdated
config: Optional[Union[str, Dict[str, Any]]] = None,
mesh_param: Any = None,
config_params: Optional[Union[str, Dict[str, Any]]] = None
) -> Tuple[DeepSpeedEngine, Optional[Optimizer], Optional[DataLoader], Optional[_LRScheduler]]:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Widen initialize's return annotation

For calls that enable common DeepSpeed wrappers, this tuple annotation is narrower than what initialize() actually returns: ZeRO/fp16/bf16 paths replace engine.optimizer with DeepSpeedOptimizer wrappers rather than torch.optim.Optimizer, DeepSpeedEngine.deepspeed_io() returns a DeepSpeedDataLoader when training_data is supplied rather than a torch.utils.data.DataLoader, and JSON-configured schedulers can be DeepSpeed scheduler objects rather than _LRScheduler. Since this is the public annotation rendered for deepspeed.initialize, typed callers will see false errors or assume PyTorch-only members for valid DeepSpeed configurations; use DeepSpeed wrapper/protocol types (or a wider union) for the returned tuple.

Useful? React with 👍 / 👎.

…pper types

The Codex reviewer correctly noted the return tuple over-claimed PyTorch-only types.
deepspeed.initialize() can return DeepSpeed wrappers, not the torch base classes:
ZeRO/fp16/bf16 paths set engine.optimizer to a DeepSpeedOptimizer (which subclasses
object, not torch.optim.Optimizer); engine.training_dataloader is a DeepSpeedDataLoader
(not a torch DataLoader); and JSON-configured schedulers are DeepSpeed objects (not
_LRScheduler). Widen the return to Tuple[DeepSpeedEngine, Optional[Union[Optimizer,
DeepSpeedOptimizer]], Optional[DeepSpeedDataLoader], Any] so typed callers do not see
false errors for valid DeepSpeed configurations.

Signed-off-by: Arun Sharma <sharm485@umn.edu>
@arunshar

Copy link
Copy Markdown
Author

Thanks for the review. Good catch — the return tuple over-claimed PyTorch-only types. engine.optimizer is a DeepSpeedOptimizer (subclasses object, not torch.optim.Optimizer) on the ZeRO/fp16/bf16 paths, engine.training_dataloader is a DeepSpeedDataLoader, and JSON-configured schedulers are DeepSpeed objects rather than _LRScheduler. Widened the return to Tuple[DeepSpeedEngine, Optional[Union[Optimizer, DeepSpeedOptimizer]], Optional[DeepSpeedDataLoader], Any] in 68e25c1. Verified import deepspeed still resolves the annotation cleanly (no circular import) and pre-commit is green.

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