From 6b330eb2d5b61c75a18fa5660ee8dcd57c5b0537 Mon Sep 17 00:00:00 2001 From: Satyam singh <106249269+Satyam12singh@users.noreply.github.com> Date: Tue, 16 Dec 2025 20:02:58 +0530 Subject: [PATCH] DOC : Add compile state example (#2910) --- docs/src/usage/compile.rst | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/docs/src/usage/compile.rst b/docs/src/usage/compile.rst index b073bbc4..bfe7f096 100644 --- a/docs/src/usage/compile.rst +++ b/docs/src/usage/compile.rst @@ -257,7 +257,26 @@ constants. For example: In order to have the change of state reflected in the outputs of ``fun`` you again have two options. The first option is to simply pass ``state`` as input -to the function. In some cases this can be pretty inconvenient. Hence, +to the function. + +.. code-block:: python + + state = [mx.array(1.0)] + + @mx.compile + def fun(x, state): + return x + state[0] + + # Prints array(2, dtype=float32) + print(fun(mx.array(1.0), state)) + + # Update state + state[0] = mx.array(5.0) + + # Prints array(6, dtype=float32) + print(fun(mx.array(1.0), state)) + +In some cases this can be pretty inconvenient. Hence, :func:`compile` also has a parameter to capture implicit inputs: .. code-block:: python