Skip to content

Add regression test for ITensor with [BackwardDerivative]#918

Draft
jhelferty-nv wants to merge 4 commits into
shader-slang:mainfrom
jhelferty-nv:fix-639-v2
Draft

Add regression test for ITensor with [BackwardDerivative]#918
jhelferty-nv wants to merge 4 commits into
shader-slang:mainfrom
jhelferty-nv:fix-639-v2

Conversation

@jhelferty-nv
Copy link
Copy Markdown
Contributor

@jhelferty-nv jhelferty-nv commented Apr 2, 2026

Add regression test for slangpy#639. Tests forward and backward passes
for a function using ITensor interface parameters with
[BackwardDerivative], the pattern from the original issue report. The
underlying compiler crash was fixed by the auto-diff refactor in
shader-slang/slang#9808.

Fixes #639

Made with Cursor

Summary by CodeRabbit

  • Tests
    • Added regression test coverage for custom backward derivative support in differentiable tensor operations, validating forward computation correctness and backward gradient computation execution.

Regression test for shader-slang#639.
Tests forward and backward passes for functions using ITensor interface
parameters with both [Differentiable] and [BackwardDerivative] attributes.

Made-with: Cursor
…ion test

The [Differentiable] variants test baseline behavior that was never broken
and is already covered by other tests. Keep only the [BackwardDerivative]
tests that directly guard against the slangpy#639 regression.

Made-with: Cursor
@jhelferty-nv jhelferty-nv requested a review from a team as a code owner April 2, 2026 18:16
@jhelferty-nv jhelferty-nv requested review from bmillsNV and removed request for a team April 2, 2026 18:16
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 2, 2026

📝 Walkthrough

Walkthrough

A regression test module is added to validate issue #639, introducing parameterized tests for forward and backward passes of a Slang shader function with ITensor arguments and a custom backward derivative implementation.

Changes

Cohort / File(s) Summary
Regression Test for ITensor Custom Backward
slangpy/tests/slangpy_tests/test_itensor_custom_backward.py
Introduces two parameterized pytest tests: one verifying forward computation correctness against NumPy output, and another confirming backward pass execution without crashes. Tests target custom [BackwardDerivative] handling with ITensor<float, 1> parameters.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

  • PR #882 — Modifies autograd/backward gradient-handling internals that these new tests exercise in the forward and backward passes.

Suggested reviewers

  • bmillsNV
  • tdavidovicNV

Poem

🐰 A shader that tensors once feared,
Now tests make the path crystal-cleared,
With backwards so true, and ITensors too,
The segfault's defeated—regression adieu! 🎉

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Add regression test for ITensor with [BackwardDerivative]' directly and accurately reflects the main change: a new regression test for ITensor with custom backwards derivatives targeting issue #639.
Linked Issues check ✅ Passed The PR adds a regression test that exercises both forward and backward passes for ITensor with [BackwardDerivative], addressing the core requirements from issue #639: verifying the forward pass executes without segfault and the backward pass runs successfully.
Out of Scope Changes check ✅ Passed All changes are scoped to adding regression tests for issue #639; the new test module targets ITensor behavior with [BackwardDerivative] and contains no unrelated modifications.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

Review ran into problems

🔥 Problems

Timed out fetching pipeline failures after 30000ms


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
Copy Markdown

@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.

🧹 Nitpick comments (1)
slangpy/tests/slangpy_tests/test_itensor_custom_backward.py (1)

59-63: Use .grad_in for semantic clarity in backward pass setup.

The pattern in test_difftensor_load_variants.py explicitly uses result.grad_in.storage.copy_from_numpy() rather than result.grad.storage, and also asserts result.grad_in is not None. While .grad is a valid convenience property that resolves to .grad_in when .grad_out is unavailable, using .grad_in directly is more semantically explicit for seeding the input gradient to the backward pass. For consistency with the codebase pattern and clarity, use:

result.grad_in.storage.copy_from_numpy(np.ones(N, dtype=np.float32))

The regression test purpose is already documented in the test docstring.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@slangpy/tests/slangpy_tests/test_itensor_custom_backward.py` around lines 59
- 63, Replace the backward-pass seeding to use the explicit input-gradient
field: ensure the test sets the seeded gradient into result.grad_in rather than
the convenience alias result.grad; call
result.grad_in.storage.copy_from_numpy(np.ones(N, dtype=np.float32)) and assert
result.grad_in is not None before copying so the backward invocation
func.bwds(coord=spy.grid((N,)), x=xt, _result=result) receives the semantically
correct input gradient.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@slangpy/tests/slangpy_tests/test_itensor_custom_backward.py`:
- Around line 59-63: Replace the backward-pass seeding to use the explicit
input-gradient field: ensure the test sets the seeded gradient into
result.grad_in rather than the convenience alias result.grad; call
result.grad_in.storage.copy_from_numpy(np.ones(N, dtype=np.float32)) and assert
result.grad_in is not None before copying so the backward invocation
func.bwds(coord=spy.grid((N,)), x=xt, _result=result) receives the semantically
correct input gradient.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 1d523617-177d-45f5-8fad-e501b2098117

📥 Commits

Reviewing files that changed from the base of the PR and between bcc393c and 1773ad5.

📒 Files selected for processing (1)
  • slangpy/tests/slangpy_tests/test_itensor_custom_backward.py

Copy link
Copy Markdown
Contributor

@ccummingsNV ccummingsNV left a comment

Choose a reason for hiding this comment

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

Looks good to me - I'm guessing it has to wait for fixed slang

@jhelferty-nv
Copy link
Copy Markdown
Contributor Author

Correct, we don't have a public Slang release with shader-slang/slang#9808 in it yet.

@jhelferty-nv
Copy link
Copy Markdown
Contributor Author

Moving this back to draft because it's taking a while to get a new slang update into slangpy with shader-slang/slang#9808.

@jhelferty-nv jhelferty-nv marked this pull request as draft May 12, 2026 18:27
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.

Simple function taking ITensor argument segfaults in forward pass if a custom bwds function is defined

3 participants