← Back to Pull requests

add `atleast_{1d, 2d, 3d}` ops & fix snippet_phantom_grad_vs_torch_consistency test #1881

Closed
unknown closed this pull request without merging on Mar 31, 2025 17:37 UTC
View on GitHub
unknown unknown commented on Mar 13, 2025 21:26 UTC
Author

atleast_1d operator returns self for one-dimensional tensor. It is a leaf tensor; however, this op is still differentiable, and the gradient value is torch.tensor([1.]). (The same goes for other similar operators like atleast_2d, atleast_3d, etc.)

In [1]: import thunder, torch
In [2]: a = torch.tensor((4,), dtype=torch.float32, requires_grad=True)
In [3]: torch.atleast_1d(a).backward()
In [4]: a.grad
Out[4]: tensor([1.])
In [5]: a.reshape(-1)
Out[5]: tensor([4.], grad_fn=<ViewBackward0>)
In [6]: a.view(-1)
Out[6]: tensor([4.], grad_fn=<ViewBackward0>)

As in the snippet_phantom_grad_vs_torch_consistency test, it returns an [] empty list in such cases.

E           AttributeError: 'function' object has no attribute 'name'

thunder/tests/test_grad.py:1348: AttributeError
------------------------------------- Captured stdout call -------------------------------------
tensor([1.2820], device='cuda:0', dtype=torch.float64, grad_fn=<ViewBackward0>)
[tensor([1.2820], device='cuda:0', dtype=torch.float64, grad_fn=<ViewBackward0>)]
tensor([-3.3215, -7.2744, -5.0842,  4.6119], device='cuda:0',
       dtype=torch.float64, requires_grad=True)
[]

This PR adds the support of the atleast_{1d, 2d, 3d} operator in thunder. And to make atleast_{1d, 2d, 3d} ops support gradient, this PR fixes this issue by returning grad values for cases where the operators return self and also support grads.

Credits & Thanks to @kshitij12345 for helping me find the gradient issue!! It was really helpful.

Diff: 196 lines (+147 / -49) across 2 files Reviewer: khushi-411 Review duration: 25691m
All Vouch review confidence checks passed
Residual risk 0.14 = Change Risk (0.36) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.36 · 196 lines diff across 2 files
0.36
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
60%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.316
Shallow Review Depth shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #1881"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 60.5% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 36.1% (MEDIUM) · Higher = larger blast radius
36.1%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 60.5% · Lower = rushed or shallow review
60.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 14.2% · Safe to merge
14.2%