Warning

You are reading a version of the website built against the unstable main branch. This content is liable to change without notice and may be inappropriate for your use case. You can find the documentation for the current stable release here.

firedrake.ml.jax package

Submodules

firedrake.ml.jax.fem_operator module

class firedrake.ml.jax.fem_operator.FiredrakeJaxOperator(F)[source]

Bases: object

JAX custom operator representing a set of Firedrake operations expressed as a reduced functional \(F\).

\(FiredrakeJaxOperator\) executes forward and backward passes by directly calling the reduced functional \(F\).

Parameters:

F – The reduced functional to wrap.

bwd(_, grad_output)[source]

Backward pass of the JAX custom operator.

forward = None
fwd(*x_P)[source]

Forward pass of the JAX custom operator.

firedrake.ml.jax.fem_operator.fem_operator(F)[source]

Cast a Firedrake reduced functional to a JAX operator.

The resulting FiredrakeJaxOperator will take JAX tensors as inputs and return JAX tensors as outputs.

Parameters:

F – The reduced functional to wrap.

Returns:

A JAX custom operator that wraps the reduced functional \(F\).

Return type:

firedrake.ml.jax.fem_operator.FiredrakeJaxOperator

firedrake.ml.jax.fem_operator.from_jax(x, V=None)[source]

Convert a JAX tensor \(x\) into a Firedrake object.

Parameters:
Returns:

Firedrake object representing the JAX tensor \(x\).

Return type:

firedrake.function.Function or firedrake.constant.Constant

firedrake.ml.jax.fem_operator.to_jax(x, gather=False, batched=False, **kwargs)[source]

Convert a Firedrake object \(x\) into a JAX tensor.

Parameters:
  • x – Firedrake object to convert.

  • gather – If True, gather data from all processes

  • batched – If True, add a batch dimension to the tensor

  • kwargs –

    Additional arguments to be passed to the jax.Array constructor such as:
    • device: device on which the tensor is allocated

    • dtype: the desired data type of returned tensor (default: type of \(x.dat.data\))

Returns:

JAX tensor representing the Firedrake object \(x\).

Return type:

jax.Array

firedrake.ml.jax.ml_operator module

class firedrake.ml.jax.ml_operator.JaxOperator(*operands, function_space, derivatives=None, argument_slots=(), operator_data={})[source]

Bases: MLOperator

ufl_operands
firedrake.ml.jax.ml_operator.custom_vjp(_, **kwargs)[source]
firedrake.ml.jax.ml_operator.ml_operator(model, function_space, inputs_format=0)[source]

Helper function for instantiating the JaxOperator class.

This function facilitates having a two-stage instantiation which dissociates between class arguments that are fixed, such as the function space or the ML model, and the operands of the operator, which may change, e.g. when the operator is used in a time-loop.

Example

# Stage 1: Partially initialise the operator.
N = ml_operator(model, function_space=V)
# Stage 2: Define the operands and use the operator in a UFL expression.
F = (inner(grad(u), grad(v)) + inner(N(u), v) - inner(f, v)) * dx
Parameters:
  • model – The JAX model to embed in Firedrake.

  • function_space – The function space into which the machine learning model is mapping.

  • inputs_format – The format of the input data of the ML model: \(0\) for models acting globally on the inputs, \(1\) when acting locally/pointwise on the inputs. Other strategies can also be considered by subclassing the JaxOperator class.

Returns:

The partially initialised JaxOperator class.

Return type:

Callable

Module contents