Repository navigation
Document and type distributed utils; fix pad_helper padding the wrong dim - #1697
harshaa765 wants to merge 13 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.
|
The PR appears safe to merge; no blocking or non-blocking actionable defects remain. Findings
SummaryThis PR documents and types the distributed tensor utilities while correcting
Reviews (2) · Last reviewed commit: "fix(distributed): pad the requested dim ..." |
| # 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
|
This pull request is stale because it has been open for 90 days with no activity. Remove the stale label or comment to keep it open, otherwise this will be closed in 14 days. |
Signed-off-by: Harshdeep Sharma <harsh.sharma52@gmail.com>
- Drop the now-unused `Tuple` import (ruff F401) and apply ruff format to the `pad_helper` and `reduce_loss` signatures. - `list` takes a single type argument: annotate `distributed_transpose` as returning `tuple[list[torch.Tensor], dist.Work | None]` and match the Returns sections to the built-in generics used in the signatures. - Document `compute_split_shapes`, the last undocumented helper, and drop both functions from the interrogate baseline. - Add a CHANGELOG entry. Signed-off-by: Harshdeep Sharma <harsh.sharma52@gmail.com>
pad_helper always wrote the pad amount to index 1 of the F.pad list, which is the right-hand side of the last dimension, so padding any other dimension silently padded the last one instead (and mode="conj" then failed). F.pad lists pairs from the last dimension backwards, so the right-hand padding of `dim` is the final entry. Existing callers only pad the last dimension, where both indices coincide, so their behavior is unchanged. Also index with tuples instead of lists in pad_helper and truncate_helper (list indexing is deprecated in PyTorch), make the split_tensor_along_dim error message an f-string, and add single-process tests for the tensor helpers. Signed-off-by: Harshdeep Sharma <harsh.sharma52@gmail.com>
|
@greptileai please re-review. Since the last review: merged main, fixed the ruff failures and the |
Signed-off-by: Harshdeep Sharma <harsh.sharma52@gmail.com>
Signed-off-by: Harshdeep Sharma <harsh.sharma52@gmail.com>
Signed-off-by: Harshdeep Sharma <harshsharma52@gmail.com>
|
Synced with |
Only CHANGELOG.md conflicted. Both entries move to the unreleased 2.4.0 section now that 2.3.0 is released. Signed-off-by: Harshdeep Sharma <harsh.sharma52@gmail.com>
|
Synced with I also have #1701 ( |
PhysicsNeMo Pull Request
Description
This PR adds type annotations and NumPy-style docstrings to the helpers in
physicsnemo/distributed/utils.py, which resolves the# TODO this also needs more docstringsnote at the top of that file. While writing tests for the documented behavior, I found thatpad_helperpadded the wrong dimension, so this PR also includes that fix.Documentation and typing (no behavior change)
compute_split_shapes,get_memory_format,pad_helper,truncate_helper,split_tensor_along_dim,distributed_transpose,_reduce, and_split, and aReturnssection toreduce_loss.Literal["zero", "conj"]formode, built-intuple[...]/list[...]return types).compute_split_shapesandsplit_tensor_along_dimentries from the interrogate baseline.Fix:
pad_helperpadded the last dimension regardless ofdimF.padlists its(left, right)padding pairs starting from the last dimension.pad_helperalways wrote the pad amount at index 1, which is the right-hand side of the last dimension.pad_helper(x, dim=1, ...)on a 3-D tensor padded dimension 2 instead, andmode="conj"then raised an error.distributed/fft.pybehave as before.pad_helperandtruncate_helpernow index with tuples. Indexing with a list is deprecated in PyTorch and currently emits a warning.split_tensor_along_dimwas missing itsfprefix and printed literal{dim}placeholders. It is now an f-string.Update since the previous review round
main.Tupleimport and two signatures that needed reformatting.list[torch.Tensor, ...]annotation from the applied suggestion.listtakes a single type argument, so it is nowlist[torch.Tensor].Tests
test/distributed/test_utils_helpers.pywith single-process tests for:compute_split_shapespad_helper, in both modes, along every dimensiontruncate_helpersplit_tensor_along_dimmainand pass with this change.pytest test/distributed: 174 passed, 48 skipped. I ran this on CPU only, so the multi-GPU tests are among the skipped ones.Checklist
distributed/utils.py.)Dependencies
None.