Skip to content

Conversation

@mattteochen
Copy link
Collaborator

@mattteochen mattteochen commented Dec 16, 2025

This PR enables torch autograd support for both stable and nightly PyTorch versions. The target is to remove all the expected failures introduced by #2805.

See #2803.

mattteochen and others added 10 commits December 16, 2025 13:49
…d nightly PyTorch by adjusting args_tensor_mask handling. Update tests to utilize a helper function for kwargs creation, ensuring seamless integration across versions.
…to handle ctx argument based on args_tensor_mask presence. Update tests to accommodate changes in forward and backward module definitions for both stable and nightly PyTorch versions.
@mattteochen mattteochen changed the title [WIP] [WIP] PyTorch autograd updates Dec 17, 2025
@mattteochen
Copy link
Collaborator Author

mattteochen commented Dec 17, 2025

I see another type of failure on PT nightly:

thunder/tests/test_dynamo.py::test_splitter_autograd_function_DynamoThunder_cpu_None[static] - RuntimeError: expand: the requested shape has too few dimensions!

which seems to be unrelated to the args_tensor_mask issue. I'll mark xfail for now (as it was before). There may be other relevant changes in PyTorch nightly.

@mattteochen mattteochen marked this pull request as ready for review December 18, 2025 09:59
@mattteochen mattteochen requested a review from shino16 December 18, 2025 10:00

# Detect once at module load time whether PyTorch uses args_tensor_mask.
# This must be done outside the JIT-traced function to avoid interpreter issues.
def _detect_has_args_tensor_mask():
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If you have better ideas...

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm content with this

@mattteochen mattteochen changed the title [WIP] PyTorch autograd updates PyTorch autograd updates Dec 18, 2025
@mattteochen mattteochen requested a review from crcrpar December 18, 2025 10:36
@IvanYashchuk IvanYashchuk requested a review from Copilot December 19, 2025 13:39
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR enables torch autograd support for both stable and nightly PyTorch versions by handling the API change where nightly PyTorch removed the args_tensor_mask parameter and the ctx argument requirement from autograd function signatures.

Key Changes:

  • Made args_tensor_mask parameter optional in autograd functions and added conditional logic to handle both stable PyTorch (with ctx) and nightly PyTorch (without ctx)
  • Implemented runtime detection mechanism to determine whether PyTorch version uses args_tensor_mask
  • Removed xfail_if_args_tensor_mask_removed decorators from tests that were previously expected to fail

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
thunder/torch/init.py Made args_tensor_mask optional and added conditional logic in autograd_function_apply, augmented_forward_autograd_function_apply, and backward_autograd_function_apply to handle forward/backward calls with or without ctx argument
thunder/core/jit_ext.py Updated lookaside function to conditionally handle args_tensor_mask presence, adjusted argument construction for forward/backward calls, and modified gradient input handling based on PyTorch version
thunder/tests/test_jit_general.py Added _detect_has_args_tensor_mask() detection function using source inspection, created helper function _autograd_function_apply_kwargs() for version-agnostic kwargs, and adapted test cases to define forward/backward functions conditionally based on PyTorch version
thunder/tests/test_update_aliases.py Removed xfail_if_args_tensor_mask_removed import and decorator from test
thunder/tests/test_dynamo.py Removed xfail_if_args_tensor_mask_removed decorator from test

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Fixed comment

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@mattteochen
Copy link
Collaborator Author

@KaelanDt for review. Thanks.

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