Skip to main content

Runtime hooks for Equinox modules.

Project description

Peex

Peex is a library for adding runtime evaluation hooks to Equinox modules, allowing you to peek inside!

This includes:

  • forward and backward pass hooks
  • runtime logging to inspect model activations/gradients
  • simple wrapping interface; no edits to original modules
  • compatible with jit, vmap, grad, etc

Installation

uv pip install peex

Example

Forward hooks
We can add runtime hooks and logging to the forward evaluation of an Equinox module. This edits/logs the model activations x as they pass forwards through the module.

import equinox as eqx
import jax

import peex as px


class MLP(eqx.Module):
    linear1: eqx.nn.Linear
    linear2: eqx.nn.Linear

    def __init__(self, in_features, width, out_features, *, key):
        key1, key2 = jax.random.split(key, 2)
        self.linear1 = eqx.nn.Linear(in_features, width, key=key1)
        self.linear2 = eqx.nn.Linear(width, out_features, key=key2)

    def __call__(self, x):
        x = self.linear1(x)
        x = jax.nn.relu(x)
        x = self.linear2(x)
        return x


mlp_key, x_key = jax.random.split(jax.random.key(0), 2)
mlp = MLP(1, 2, 1, key=mlp_key)
x = jax.random.normal(x_key, shape=(1,))

hooked_mlp = px.ForwardHook(mlp, hook_fn=px.IdentityHook(), log_fn=px.IdentityLog())
output, log = hooked_mlp(x)

# output = [0.23334768]
# log = {'linear1': Array([1.2571917, 1.2331474], dtype=float32), 
#        'linear2': Array([0.23334768], dtype=float32)}

Backward hooks
We can also add runtime hooks and logging to the backward pass of an Equinox module. This edits/logs the gradients (cotangent values of x) as they pass backwards through the module.

@eqx.filter_value_and_grad
def grad_loss(hooked_mlp, x):
    output = hooked_mlp(x)
    return jnp.mean(output)


hooked_mlp = px.BackwardHook(
    mlp,
    hook_fn=px.IdentityHook(),
    log_fn=px.IdentityLog(),
)
hooked_mlp = hooked_mlp.init(x)
loss, grads = grad_loss(hooked_mlp, x)

model_grads = grads.model
log_grads = grads.log

# log_grads = {'linear1': Array([-0.68400913,  0.4777369 ], dtype=float32), 
#              'linear2': Array([1.], dtype=float32)}

Note that for the backward hook we must run a post-init hooked_mlp = hooked_mlp.init(x). This runs a jax.eval_shape call to populate the log dictionary with the correct gradient shapes. The log dictionary can then be populated with cotangent values on the backward pass.

See the examples/ directory for more in-depth examples.

Writing custom hooks

The defining rule of a hook_fn is that the output shapes/dtypes exactly match their input shapes/dtypes. This can be understood by tracing the forward logic:

Pre-hook

# original module
x -> module(x)
# with hook
x -> module(hook_fn(x))

Here, x are tuple of arguments. So, hook_fn should map tuple -> tuple.

Post-hook

# original module
x -> module(x)
# with hook
x -> hook_fn(module(x))

Here, module(x) is the output of module with unconstrained type output_type. So, hook_fn should map output_type -> output_type. The same logic applies on the backward pass.

Writing custom logs

A log_fn can return an output with any shape/dtype. The only requirement is that it accepts the correct input shapes/dtypes. This depends on whether you are running a pre or post hook - see the discussion on custom hooks above.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

peex-0.0.3.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

peex-0.0.3-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file peex-0.0.3.tar.gz.

File metadata

  • Download URL: peex-0.0.3.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.18

File hashes

Hashes for peex-0.0.3.tar.gz
Algorithm Hash digest
SHA256 53c4ff0b84c7d14177437c902e4580f83c73aaca3bc8bc17626e3d4d344fe6f5
MD5 18ef0e28de5f0c786005cbe3bdf39f0d
BLAKE2b-256 1e94dbfadc6a0095c45dfee7b6926bbe69214e8878fb782f798d6da8cf82f2a6

See more details on using hashes here.

File details

Details for the file peex-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: peex-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.18

File hashes

Hashes for peex-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1920e125ab607cb7a7acfd13f5d52f28ad2a3df76da8e79c8532b3f058fb7758
MD5 c886a07802a35566f1cf6702a599a4b2
BLAKE2b-256 2605f44aee853349bbdd553405dc3233739d7c5a9d6acdc948687dfaa98ee3ba

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page