feat(distributed): add type annotations and docstrings to utils.py#1697
feat(distributed): add type annotations and docstrings to utils.py#1697harshaa765 wants to merge 6 commits into
Conversation
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.
Greptile SummaryThis PR is a pure documentation and type-annotation uplift for
Important Files Changed
|
| # TODO this also needs more docstrings | ||
| from typing import List, Optional | ||
| from typing import List, Optional, Tuple |
There was a problem hiding this comment.
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.
| # 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!
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>
| 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" |
There was a problem hiding this comment.
| 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
There was a problem hiding this comment.
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.
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
Summary
TODOat line 17 ofphysicsnemo/distributed/utils.pyrequesting improved docstringsget_memory_format,pad_helper,truncate_helper,split_tensor_along_dim,distributed_transpose,_reduce, and_splitOptional[float]return type toreduce_lossParameters,Returns, and (where applicable)RaisessectionsTupleto thetypingimports to support the new return annotationsMotivation
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._reducereturns the same tensor object when no up-cast is needed;distributed_transposereturnsNonefor the work handle in synchronous mode) explicit in the signature.Test plan
pytest test/distributed/mypy physicsnemo/distributed/utils.pyto verify annotations are consistent