Skip to content

Conversation

@zpqiu
Copy link
Contributor

@zpqiu zpqiu commented Dec 23, 2025

What does this PR do ?

With DTensor on PyTorch 2.9, __getitem__ slice indexing can hit aten.alias.default (no sharding strategy registered) and crash.
Use narrow to avoid the alias/view path while keeping the same semantics.

Issues

List issues that this PR closes (syntax):

Usage

  • You can potentially add a usage example below
# Add a code snippet demonstrating how to use this

Before your PR is "Ready for review"

Pre checks:

  • Make sure you read and followed Contributor guidelines
  • Did you write any new necessary tests?
  • Did you run the unit tests and functional tests locally? Visit our Testing Guide for how to run tests
  • Did you add or update any necessary documentation? Visit our Document Development Guide for how to write, build and test the docs.

Additional Information

  • ...

Summary by CodeRabbit

  • Refactor
    • Improved internal implementation of sequence packing logic for more explicit bounds calculation and enhanced reliability.

✏️ Tip: You can customize this high-level summary in your review settings.

Signed-off-by: Zhaopeng Qiu <alexq@nvidia.com>
@zpqiu zpqiu requested a review from a team as a code owner December 23, 2025 04:55
@zpqiu zpqiu added the CI:L2 Run doctests, unit tests, functional tests, and convergence tests label Dec 23, 2025
@zpqiu zpqiu requested a review from terrykong December 23, 2025 04:56
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 23, 2025

📝 Walkthrough

Walkthrough

Refactors logit slice derivation in the sequence packing loop by replacing a Python slice object with explicit start/end indices and tensor.narrow, maintaining equivalent functionality with more explicit bounds calculation.

Changes

Cohort / File(s) Summary
Logit slicing refactoring
nemo_rl/algorithms/loss_functions.py
Replaced logit_slice_idxs slice object with explicit logit_start, logit_end, and logit_length indices; uses tensor.narrow for next-token logits extraction within sequence packing loop

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Suggested labels

CI:L1, r0.5.0

Suggested reviewers

  • terrykong

Pre-merge checks and finishing touches

✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Test Results For Major Changes ✅ Passed PR contains a minor, localized bug fix replacing slice indexing with tensor.narrow() to address PyTorch 2.9 DTensor compatibility.
Title check ✅ Passed The title accurately describes the main change: fixing a DTensor slice crash caused by PyTorch 2.9, which matches the PR's primary objective of replacing slice indexing with tensor.narrow to avoid the alias/view path.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
nemo_rl/algorithms/loss_functions.py (1)

925-930: Fix correctly addresses the DTensor crash.

The change from slice indexing to tensor.narrow is correct and preserves the original semantics while avoiding the aten.alias.default issue with DTensor on PyTorch 2.9.

The explicit calculation of logit_start, logit_end, and logit_length improves code clarity.

💡 Optional: Add explanatory comment

Consider adding a brief comment explaining why narrow is used instead of slice indexing to prevent future refactoring back to the problematic pattern:

 logit_start = seq_start // cp_size
 logit_end = (seq_start + padded_seq_lengths[seq_idx]) // cp_size
 logit_length = logit_end - logit_start
+# Use narrow instead of slicing to avoid DTensor crash with aten.alias.default on PyTorch 2.9+
 next_token_logits_slice = next_token_logits.narrow(
     1, logit_start, logit_length
 )
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 56e8fcb and a71bbf0.

📒 Files selected for processing (1)
  • nemo_rl/algorithms/loss_functions.py
🧰 Additional context used
📓 Path-based instructions (4)
**/*.py

📄 CodeRabbit inference engine (CODING_GUIDELINES.md)

**/*.py: Conform code to Python 3.12+
Indent code with 4 spaces. Do not use tabs
Use snake_case for file names
Use PascalCase for class names
Use snake_case for function and method names
Use snake_case for local variables
Prefix variable names that start with a number with 'k' (e.g., k_99th_percentile)
Use upper snake_case with 'G' prefix for global variables (e.g., G_MY_GLOBAL)
Use upper snake_case for constants
Avoid shadowing variables declared in an outer scope
Initialize all externally visible members of a class in the constructor
Prefer docstrings over comments for interfaces that may be used outside a file
Reserve comments for code within a function or interfaces that are local to a file
If a piece of code is commented out, include a comment describing its usage and why it's commented out. Remove debug comments before merging
Use Google style docstrings for classes and functions in Python, which can be parsed by Sphinx
Avoid using reflection when functionality can be easily achieved without reflection
When using try-except blocks, limit the except clause to the smallest set of specific errors possible
When using try-except blocks for duck-typing, keep the body of the try as small as possible and use the else block for logic
YAML is the single source of truth for configuration defaults. Do not set non-None defaults in code for configuration values
For required configuration attributes, access config directly and expect presence (e.g., policy_cfg['precision']) without hidden defaults
Use typing.NotRequired to mark optional attributes in TypedDict for configuration
When adding a new config key to a TypedDict subclass, document the key's purpose, valid values/types, and recommended default, and reflect the default in exemplar YAMLs under examples/configs/*.yaml
Follow the Google Python Style Guide for Python code

Files:

  • nemo_rl/algorithms/loss_functions.py
nemo_rl/**/*.py

📄 CodeRabbit inference engine (CODING_GUIDELINES.md)

For any source file under nemo_rl/*.py that defines a class or function decorated with @ray.remote, add a coverage pragma (# pragma: no cover) because these run in separate Ray processes

Files:

  • nemo_rl/algorithms/loss_functions.py
!(**/tests/**|**/test_*.py|**/test_*.sh)

📄 CodeRabbit inference engine (CODING_GUIDELINES.md)

Add the NVIDIA copyright header to all Python files and shell scripts (excluding tests). The header should include the current year

Files:

  • nemo_rl/algorithms/loss_functions.py
**/*.{py,sh}

📄 CodeRabbit inference engine (CODING_GUIDELINES.md)

The NVIDIA copyright header should appear at the top of all Python files and shell scripts (excluding tests)

Files:

  • nemo_rl/algorithms/loss_functions.py
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (6)
  • GitHub Check: build-container / main
  • GitHub Check: sphinx-build / Build docs
  • GitHub Check: Lint check
  • GitHub Check: Lint check
  • GitHub Check: Post submodule check comment / Comment on PR
  • GitHub Check: Post automodel integration comment / Comment on PR

@zpqiu zpqiu changed the title fix: Fix DTensor slice crash after pytorch 2.9 bump fix: Fix DTensor slice crash after PyTorch 2.9 bump Dec 23, 2025
@terrykong terrykong added r0.5.0 CI:L2 Run doctests, unit tests, functional tests, and convergence tests and removed CI:L2 Run doctests, unit tests, functional tests, and convergence tests labels Dec 23, 2025
@terrykong terrykong merged commit c8d6569 into NVIDIA-NeMo:main Jan 2, 2026
87 of 92 checks passed
chtruong814 pushed a commit that referenced this pull request Jan 2, 2026
Signed-off-by: Zhaopeng Qiu <alexq@nvidia.com>
Signed-off-by: NeMo Bot <nemo-bot@nvidia.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI:L2 Run doctests, unit tests, functional tests, and convergence tests r0.5.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants