Skip to content
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

Improve creating an Op documentation page #1086

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ricardoV94
Copy link
Member

@ricardoV94 ricardoV94 commented Nov 13, 2024

See commit message for list of changes.

The changes are based on feedback from helping other devs and users

Tagging @OriolAbril because I'm 100% sure I messed up some sphinx stuff (sorry)


📚 Documentation preview 📚: https://pytensor--1086.org.readthedocs.build/en/1086/

@ricardoV94 ricardoV94 changed the title Improve creating_an_op.rst Improve creating an op documentation page Nov 13, 2024
@@ -165,35 +151,6 @@ or :meth:`Op.make_thunk`.
:meth:`COp.c_code` and other related ``c_**`` methods. Note that an
:class:`Op` can provide both Python and C implementations.

:meth:`Op.make_thunk` method is another alternative to :meth:`Op.perform`.
Copy link
Member Author

Choose a reason for hiding this comment

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

This is outdated, and tbh I don't think it's necessary for 99.9999% of the people reading this page

@ricardoV94 ricardoV94 mentioned this pull request Nov 13, 2024
@ricardoV94 ricardoV94 force-pushed the creating_an_op branch 4 times, most recently from d37103d to 1d9bc2b Compare November 19, 2024 11:03
@@ -1,40 +1,18 @@

.. _creating_an_op:

Creating a new :class:`Op`: Python implementation
Creating a new :ref:`op`: Python implementation
Copy link
Member

@OriolAbril OriolAbril Nov 19, 2024

Choose a reason for hiding this comment

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

Where do you want the :ref:`op`/:class:`Op` to point to? To https://pytensor--1086.org.readthedocs.build/en/1086/extending/op.html or to https://pytensor--1086.org.readthedocs.build/en/1086/extending/graphstructures.html#op?

I think many of the broken cross-references come from this page: https://pytensor--1086.org.readthedocs.build/en/1086/extending/op.html, which is wildly inconsistent with itself. If the cross-reference would ideally point there we should fix this page first.

Copy link
Member Author

@ricardoV94 ricardoV94 Nov 19, 2024

Choose a reason for hiding this comment

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

Will check. I just wanted it to point somewhere, it wasn't before

Copy link
Member Author

Choose a reason for hiding this comment

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

@OriolAbril extending op seems much more useful than the graphstructures#op, so I guess there.

has no bugs, and potentially profits from rewrites that have already been
implemented.
Odds are your function that uses existing PyTensor expressions is short, has no bugs, can be differentiated,
and may even work in non-default backends and benefit from optimization rewrites.
Copy link
Member

Choose a reason for hiding this comment

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

Suggestion: "A PyTensor function that builds upon existing expressions tends to be concise, reliable, and automatically differentiable, while often working seamlessly across different backends, thereby benefiting from optimization."

Copy link
Member Author

Choose a reason for hiding this comment

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

Rewrote to something in that vein but simpler

fonnesbeck

This comment was marked as duplicate.

Changes:
1. Remove references to c-code which apply to `COp` but not `Op`
2. Fix failing doctests
3. Improve explanation of `make_node`
4. Emphasize distinction between itypes/otypes and make-node
5. Show `L_op` instead of `grad`
6. Show how to test `L_op` and `infer_shape` implementation
7. Simplify explanation of `__props__` and illustrate in example.
8. Introduce more complex multi-output Op to drive these details home
9. Remove old references to numba/ random variable Ops
@ricardoV94 ricardoV94 changed the title Improve creating an op documentation page Improve creating an Op documentation page Feb 20, 2025
Copy link
Member

@fonnesbeck fonnesbeck left a comment

Choose a reason for hiding this comment

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

Just a few typos. LGTM.


- it first checks that the input :class:`Variable`\s types are compatible
with the current :class:`Op`. If the :class:`Op` cannot be applied on the provided
input types, it must raises an exception (such as :class:`TypeError`).
Copy link
Member

Choose a reason for hiding this comment

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

Typo: must raise

pass

def R_op(self, inputs, eval_points):
# Defines that symbolic expression for the R-operator based on the input variables
Copy link
Member

Choose a reason for hiding this comment

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

should be "the symbolic"(?)


def R_op(self, inputs, eval_points):
# R_op can receive None as eval_points.
# That mean there is no diferientiable path through that input
Copy link
Member

Choose a reason for hiding this comment

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

2 Typos: "That means" and "differentiable"


def perform(self, node, inputs, output_storage):
# Validate input type
if not x.type.ndim == 2 and x.type.dtype == "float64":
Copy link
Member

Choose a reason for hiding this comment

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

need to enclose conditions in parentheses


x = pytensor.tensor.matrix()
x = matrix(x, dtype="float64")
Copy link
Member

Choose a reason for hiding this comment

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

matrix name should be enclosed in quotes


For instance, to verify the :meth:`Rop` method of the ``DoubleOp``, you can use this:
For instance, to verify the :meth:`R_op` method of the ``DoubleOp``, you can use this:
Copy link
Member

Choose a reason for hiding this comment

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

do you mean "of the DoubleROp" here?


.. code-block:: none
Finally we should test the gradient implementation.
For this we can use the ``pytensor.gradient.verify_grad`` utility which will compare the output of a gradient function with fininte differences.
Copy link
Member

Choose a reason for hiding this comment

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

Type: "finite"


PyTensor has some functionalities to test for a correct implementation of an :class:`Op` and it's many methods.

We have already seen some user facing helpers, but there are also test classes for :class:`Op` implementations
Copy link
Member

Choose a reason for hiding this comment

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

nit: "user-facing"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants