Skip to content

Conversation

@yurekami
Copy link
Contributor

Summary

This PR improves the documentation quality of verl/utils/torch_functional.py by adding comprehensive Google-style docstrings to core utility functions.

Functions Improved

Function Change
gather_from_labels Complete docstring with example usage
logprobs_from_logits_flash_attn Added docstring explaining Flash Attention usage
logprobs_from_logits_torch_npu Added docstring for NPU implementation
logprobs_from_logits_naive Added docstring for standard implementation
logprobs_from_logits_v2 Enhanced docstring explaining memory efficiency
clip_by_value Complete docstring with See Also reference
entropy_from_logits Added docstring with mathematical formula
entropy_from_logits_with_chunking Added docstring explaining chunking strategy
masked_sum Complete docstring with type hints
compute_grad_norm Added docstring clarifying squared norm return
broadcast_dict_tensor Added docstring with optimization note
allgather_dict_tensors Complete docstring with return type

Improvements Include

  • Proper type hints on function signatures
  • Detailed Args and Returns sections
  • Note sections for important implementation details
  • Mathematical formulas where applicable (e.g., entropy calculation)
  • Usage examples for complex functions

Test plan

  • Python syntax verification passed
  • CI tests should pass (documentation-only changes)

Contributes to #1345

🤖 Generated with Claude Code

Standardized docstrings for core utility functions in torch_functional.py
following Google-style documentation format:

Functions improved:
- gather_from_labels: Added complete docstring with example usage
- logprobs_from_logits_flash_attn: Added docstring explaining Flash Attention usage
- logprobs_from_logits_torch_npu: Added docstring for NPU implementation
- logprobs_from_logits_naive: Added docstring for standard implementation
- logprobs_from_logits_v2: Enhanced docstring explaining memory efficiency
- clip_by_value: Added complete docstring with See Also reference
- entropy_from_logits: Added docstring with mathematical formula
- entropy_from_logits_with_chunking: Added docstring explaining chunking strategy
- masked_sum: Added complete docstring with type hints
- compute_grad_norm: Added docstring clarifying squared norm return
- broadcast_dict_tensor: Added docstring with optimization note
- allgather_dict_tensors: Added complete docstring with return type

All improvements include:
- Proper type hints on function signatures
- Detailed Args and Returns sections
- Notes for important implementation details
- Mathematical formulas where applicable

Contributes to volcengine#1345

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request significantly improves the documentation and type hints in verl/utils/torch_functional.py, making the code more readable and maintainable. The new Google-style docstrings are comprehensive and provide valuable context, examples, and notes on implementation details. The code changes are focused on documentation and do not alter functionality. I have one suggestion to improve a type hint for better accuracy and consistency with modern PyTorch practices.

def logprobs_from_logits_v2(logits: torch.FloatTensor, labels):
"""
A memory efficient implementation of logprobs_from_logits
def logprobs_from_logits_v2(logits: torch.FloatTensor, labels: torch.Tensor) -> torch.Tensor:
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The type hint torch.FloatTensor for logits is too restrictive. The function body explicitly handles other dtypes like bfloat16 in the else block. Using torch.Tensor would be more accurate and align with modern PyTorch practices, as torch.FloatTensor is a legacy alias for a torch.float32 tensor.

Suggested change
def logprobs_from_logits_v2(logits: torch.FloatTensor, labels: torch.Tensor) -> torch.Tensor:
def logprobs_from_logits_v2(logits: torch.Tensor, labels: torch.Tensor) -> torch.Tensor:

@vermouth1992 vermouth1992 changed the title [docs] feat: improve docstrings in torch_functional.py (#1345) [doc] feat: improve docstrings in torch_functional.py (#1345) Dec 30, 2025
@vermouth1992 vermouth1992 merged commit d6d546a into volcengine:main Dec 31, 2025
65 of 74 checks passed
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.

3 participants