Skip to content

Add gallery notebook on vector jacobian products #1544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 27, 2025

Conversation

ricardoV94
Copy link
Member

@ricardoV94 ricardoV94 commented Jul 15, 2025

When we have more powerful pytensor rewrites I would like to make a companion piece showing pytensor deriving the optimized Lop from the naive formulation (maybe with the exception of the transpose)

Incidentally, this PR implements vectorize for CumSum, so it shows up nicely in the jacobian :)

Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

Copy link

@Copilot 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 adds vectorization support for the cumulative sum operation in PyTensor and includes a new tutorial notebook demonstrating optimized vector-Jacobian products.

  • Implement _vectorize_node rule for CumOp to enable batched inputs.
  • Add a comprehensive vector_jacobian_product.ipynb showing VJP optimizations across several tensor operations.

Reviewed Changes

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

File Description
pytensor/tensor/extra_ops.py Register and implement vectorize_cum_op for CumOp
doc/gallery/gradient/vector_jacobian_product.ipynb New tutorial notebook illustrating vector-Jacobian products
Comments suppressed due to low confidence (4)

pytensor/tensor/extra_ops.py:477

  • [nitpick] There aren’t any unit tests covering the new vectorize_cum_op rule. Consider adding tests for both the axis=None and explicit axis cases to ensure correct batched behavior.
@_vectorize_node.register(CumOp)

pytensor/tensor/extra_ops.py:486

  • The function normalize_axis_index is used here but not imported, which will cause a NameError. Please add an import, for example:
from pytensor.tensor.utils import normalize_axis_index
        axis = normalize_axis_index(op.axis, original_x.ndim)

pytensor/tensor/extra_ops.py:490

  • Inside the list comprehension you iterate over batch_x but still call batch_x.flatten(...). You likely intended to call x.flatten(...) for each element or otherwise flatten the entire batch tensor directly. Consider:
batch_x_raveled = [x.flatten(ndim=batch_ndim + 1) for x in batch_x]
        batch_x_raveled = [batch_x.flatten(ndim=batch_ndim + 1) for x in batch_x]

doc/gallery/gradient/vector_jacobian_product.ipynb:62

  • Spelling mistake: 'Elemtwise' should be 'Elementwise'.
    "## Elemtwise operations"

Copy link

codecov bot commented Jul 15, 2025

Codecov Report

❌ Patch coverage is 68.42105% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.53%. Comparing base (12213d0) to head (d049dad).
⚠️ Report is 10 commits behind head on main.

Files with missing lines Patch % Lines
pytensor/tensor/extra_ops.py 60.00% 3 Missing and 3 partials ⚠️

❌ Your patch status has failed because the patch coverage (68.42%) is below the target coverage (100.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1544      +/-   ##
==========================================
+ Coverage   81.49%   81.53%   +0.04%     
==========================================
  Files         232      230       -2     
  Lines       53122    53066      -56     
  Branches     9444     9423      -21     
==========================================
- Hits        43292    43269      -23     
+ Misses       7382     7364      -18     
+ Partials     2448     2433      -15     
Files with missing lines Coverage Δ
pytensor/tensor/subtensor.py 90.01% <100.00%> (+0.03%) ⬆️
pytensor/tensor/extra_ops.py 88.56% <60.00%> (-0.33%) ⬇️

... and 14 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ricardoV94 ricardoV94 force-pushed the vector_jacobian_products branch from eda6642 to 0ce5359 Compare July 24, 2025 11:33
@ricardoV94 ricardoV94 changed the title Vector jacobian products Add gallery notebook on vector jacobian products Jul 24, 2025
@ricardoV94 ricardoV94 force-pushed the vector_jacobian_products branch from 0ce5359 to 7da29a8 Compare July 24, 2025 13:01
@ricardoV94 ricardoV94 force-pushed the vector_jacobian_products branch from 7da29a8 to 680e47d Compare July 24, 2025 13:04
@ricardoV94
Copy link
Member Author

Link to the docs preview: https://pytensor--1544.org.readthedocs.build/en/1544/gallery/autodiff/vector_jacobian_product.html

And yes the auto snapshoot seems to be erasing the custom pics

@@ -0,0 +1,1071 @@
{
Copy link
Member

@jessegrabowski jessegrabowski Jul 27, 2025

Choose a reason for hiding this comment

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

typo: get


Reply via ReviewNB

Copy link
Member

@jessegrabowski jessegrabowski left a comment

Choose a reason for hiding this comment

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

looks great!

Did you want to adjust the generate_gallery script to use custom images? Or open an issue for later?

@ricardoV94 ricardoV94 force-pushed the vector_jacobian_products branch from 680e47d to d049dad Compare July 27, 2025 13:42
@ricardoV94
Copy link
Member Author

Opened issue #1554

@ricardoV94 ricardoV94 merged commit 892a8f0 into pymc-devs:main Jul 27, 2025
69 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants