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

Jax ml operator fix #4041

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open

Jax ml operator fix #4041

wants to merge 30 commits into from

Conversation

Ig-dolci
Copy link
Contributor

@Ig-dolci Ig-dolci commented Feb 14, 2025

Description

Depends on firedrakeproject/ufl#60

  • Fixed an issue in firedrake.ml.jax.ml_operator where argument_slots was incorrectly specified. I wrote it as optional:

    argument_slots: Optional[tuple[Union[ufl.coefficient.BaseCoefficient, ufl.argument.BaseArgument]]] = ()
  • Clarified that if argument_slots is not provided, ML_Operator will automatically write it.

  • Test the results involving the Neural operators are in right function space.

  • Test jax and pytorch operators in Firedrake CI.

Copy link

github-actions bot commented Feb 14, 2025

TestsPassed ✅Skipped ⏭️Failed ❌
Firedrake complex8269 ran6595 passed1674 skipped0 failed

Copy link

github-actions bot commented Feb 14, 2025

TestsPassed ✅Skipped ⏭️Failed ❌
Firedrake real8239 ran7541 passed698 skipped0 failed

@Ig-dolci Ig-dolci changed the title Does this should be optional? Jax ml operator fix Feb 14, 2025
@Ig-dolci Ig-dolci marked this pull request as ready for review February 17, 2025 14:11
@Ig-dolci
Copy link
Contributor Author

@pbrubeck @dham Firedrake pip installs PR depends on the approval of UFL PR 348 and this PR.

@Ig-dolci
Copy link
Contributor Author

Ig-dolci commented Feb 21, 2025

The failure in the docs building is because of a recent Sphinx update.

@@ -77,6 +77,6 @@ def J(f):
c = Control(f)
Jhat = ReducedFunctional(J(f), c)

f_opt = minimize(Jhat, tol=1e-6, method="BFGS")
f_opt = minimize(Jhat, tol=1e-4, method="BFGS")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This tolerance modification reduces the runtime test by almost 50%. Most important is that the following assert is still satisfied.

@@ -39,36 +39,41 @@ def __init__(
*operands: Union[ufl.core.expr.Expr, ufl.form.BaseForm],
function_space: WithGeometryBase,
derivatives: Optional[tuple] = None,
argument_slots: Optional[tuple[Union[ufl.coefficient.BaseCoefficient, ufl.argument.BaseArgument]]],
argument_slots: Optional[tuple[Union[ufl.coefficient.BaseCoefficient, ufl.argument.BaseArgument]]] = (),
Copy link
Contributor

Choose a reason for hiding this comment

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

If argument slots are not provided, then they will be generated.

Having an empty tuple seems a strange default considering. Could/should it be None instead?

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually if it is Optional then that's what is has to be (https://docs.python.org/3/library/typing.html#typing.Optional)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Optional[tuple[Union[ufl.coefficient.BaseCoefficient, ufl.argument.BaseArgument]]] = None leads to the error:

>       argument_slots = tuple(map(as_ufl, argument_slots))
E       TypeError: 'NoneType' object is not iterable

../ufl/ufl/core/base_form_operator.py:49: TypeError

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The way to define argument_slots argument is comming from https://github.com/firedrakeproject/ufl/blob/master/ufl/core/external_operator.py#L28

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can improve this by writing

argument_slots: tuple[Union[ufl.coefficient.BaseCoefficient, ufl.argument.BaseArgument]] | None = None,

Then

if argument_slots is None:
      argument_slots = ()

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be better to just have

argument_slots: tuple[Union[ufl.coefficient.BaseCoefficient, ufl.argument.BaseArgument]] = (),

(i.e. no Optional) and to remove

If argument slots are not provided, then they will be generated.

from the docstring.

That is at least consistent with the UFL object.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

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.

4 participants