Skip to content

feat(distributed): add type annotations and docstrings to utils.py#1697

Open
harshaa765 wants to merge 6 commits into
NVIDIA:mainfrom
harshaa765:add-type-annotations-distributed-utils
Open

feat(distributed): add type annotations and docstrings to utils.py#1697
harshaa765 wants to merge 6 commits into
NVIDIA:mainfrom
harshaa765:add-type-annotations-distributed-utils

Conversation

@harshaa765
Copy link
Copy Markdown

Summary

  • Addresses the TODO at line 17 of physicsnemo/distributed/utils.py requesting improved docstrings
  • Adds full PEP 484 type annotations to all previously untyped functions: get_memory_format, pad_helper, truncate_helper, split_tensor_along_dim, distributed_transpose, _reduce, and _split
  • Adds the missing Optional[float] return type to reduce_loss
  • Expands one-liner docstrings into NumPy-style docs with Parameters, Returns, and (where applicable) Raises sections
  • Adds Tuple to the typing imports to support the new return annotations
  • Zero logic changes — purely annotations and documentation

Motivation

The distributed utilities are on the critical path for all multi-GPU training runs. Proper type annotations improve IDE autocomplete, enable static analysis via mypy, and make the implicit contracts (e.g. _reduce returns the same tensor object when no up-cast is needed; distributed_transpose returns None for the work handle in synchronous mode) explicit in the signature.

Test plan

  • Confirm existing tests pass: pytest test/distributed/
  • Optionally run mypy physicsnemo/distributed/utils.py to verify annotations are consistent

Addresses the TODO at line 17 requesting improved docstrings.
Adds full PEP 484 type annotations and NumPy-style docstrings to all
previously untyped functions: get_memory_format, pad_helper,
truncate_helper, split_tensor_along_dim, distributed_transpose,
_reduce, and _split. Also adds the missing Optional[float] return
type to reduce_loss. No logic changes.
@harshaa765 harshaa765 requested a review from coreyjadams as a code owner June 5, 2026 11:18
@copy-pr-bot
Copy link
Copy Markdown

copy-pr-bot Bot commented Jun 5, 2026

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Jun 5, 2026

Greptile Summary

This PR is a pure documentation and type-annotation uplift for physicsnemo/distributed/utils.py, with zero logic changes. It expands seven one-liner docstrings into full NumPy-style docs and adds PEP 484 type annotations throughout.

  • Adds Parameters, Returns, and Raises sections to get_memory_format, pad_helper, truncate_helper, split_tensor_along_dim, distributed_transpose, _reduce, and _split.
  • Adds the missing Optional[float] return annotation to reduce_loss and imports Tuple from typing.

Important Files Changed

Filename Overview
physicsnemo/distributed/utils.py Pure documentation / annotation update: adds NumPy-style docstrings and PEP 484 type hints to seven functions; three minor docstring gaps remain (TODO not removed, reduce_loss missing Returns section, split_tensor_along_dim Raises wording inaccurate).

Comments Outside Diff (1)

  1. physicsnemo/distributed/utils.py, line 200-204 (link)

    P2 The reduce_loss function now has an Optional[float] return type annotation, but the existing docstring still has no Returns section. The None return path (non-destination ranks) is implicit, so adding a Returns block here would complete the documentation that this PR is meant to improve.

    Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Reviews (1): Last reviewed commit: "feat(distributed): add type annotations ..." | Re-trigger Greptile

Comment thread physicsnemo/distributed/utils.py Outdated
Comment on lines +17 to +18
# TODO this also needs more docstrings
from typing import List, Optional
from typing import List, Optional, Tuple
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 The TODO comment the PR explicitly states it is addressing is still present. Since the docstrings have been added, the comment should be removed to keep the file tidy.

Suggested change
# TODO this also needs more docstrings
from typing import List, Optional
from typing import List, Optional, Tuple
from typing import List, Optional, Tuple

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Comment thread physicsnemo/distributed/utils.py Outdated
Updates the docstring to correctly state "is greater than or equal to" instead of "exceeds" to accurately reflect the 0-indexed tensor dimension check.

Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Comment thread physicsnemo/distributed/utils.py Outdated
def pad_helper(tensor, dim, new_size, mode="zero"):
"""Util for padding tensors"""
def pad_helper(
tensor: torch.Tensor, dim: int, new_size: int, mode: str = "zero"
Copy link
Copy Markdown
Collaborator

@peterdsharpe peterdsharpe Jun 5, 2026

Choose a reason for hiding this comment

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

Suggested change
tensor: torch.Tensor, dim: int, new_size: int, mode: str = "zero"
tensor: torch.Tensor, dim: int, new_size: int, mode: Literal["zero", "conj"] = "zero"

+ add from typing import Literal up top

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good suggestion — applied in 84947d1. Narrowed mode from str to Literal["zero", "conj"] and added Literal to the typing imports. Updated the docstring parameter type to match as well.

Comment thread physicsnemo/distributed/utils.py Outdated
Comment thread physicsnemo/distributed/utils.py Outdated
peterdsharpe and others added 4 commits June 5, 2026 09:36
Replace `str` with `Literal["zero", "conj"]` for the mode parameter
in pad_helper, as those are the only two valid values. Also adds
Literal to the typing imports and updates the docstring to match.

Addresses reviewer suggestion from peterdsharpe.
- Remove the resolved TODO comment (docstrings have now been added)
- Add missing Returns section to reduce_loss, documenting that
  non-destination ranks receive None
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.

2 participants