Skip to content

Commit cf252dc

Browse files
author
Mathias Louboutin
committed
tests: Evaluate Devito symbolic directional derivatives with PyTorch
1 parent 850f3dd commit cf252dc

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

‎docs/torch.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,21 @@ The module lowers the resulting stencil reads and arithmetic to PyTorch tensor
167167
operations. PyTorch records padding, slicing, and pointwise operations in its
168168
autograd graph and computes gradients through them.
169169

170+
Devito also exposes `directional_derivative(expression, field, direction)`
171+
independently of PyTorch. It applies the chain rule to the symbolic expression
172+
and preserves unevaluated finite-difference operators. Use the returned
173+
expression in an `Eq` or pass it to `TorchStencil` to evaluate the
174+
Jacobian-vector product on tensors. For the example above:
175+
176+
```python
177+
from devito import directional_derivative
178+
179+
du = Function(name='du', grid=grid, space_order=2)
180+
tangent = directional_derivative(u + alpha*u.laplace, u, du)
181+
jvp = TorchStencil(tangent, alpha, du)
182+
variation = jvp(coefficient, torch.ones_like(initial))
183+
```
184+
170185
This interface supports serial CPU float32 and float64 tensors on an unstaggered
171186
grid with a fixed zero halo. Express coefficients as numeric literals or tensor
172187
fields. Use repeated module calls for time stepping. The

‎tests/torch/test_layer.py‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,14 @@ def test_invalid_input():
8383
def test_symbolic_stencil_autograd():
8484
import numpy as np
8585

86-
from devito import Function, Grid
86+
from devito import Function, Grid, directional_derivative
8787
from devito.torch import TorchStencil
8888

8989
grid = Grid(shape=(4, 5), extent=(6., 4.))
9090
state = Function(name='state', grid=grid, space_order=2, dtype=np.float64)
9191
weight = Function(name='weight', grid=grid, space_order=0, dtype=np.float64)
92-
layer = TorchStencil(state + weight*state.dx**2, state, weight)
92+
expression = state + weight*state.dx**2
93+
layer = TorchStencil(expression, state, weight)
9394

9495
value = torch.randn(5, 4, dtype=torch.float64).T.requires_grad_()
9596
coefficient = torch.randn_like(value, requires_grad=True)
@@ -108,3 +109,12 @@ def reference(x, w):
108109
torch.testing.assert_close(actual, expected)
109110
assert torch.autograd.gradcheck(layer, (value, coefficient), fast_mode=True)
110111
assert torch.autograd.gradgradcheck(layer, (value, coefficient), fast_mode=True)
112+
113+
direction = Function(name='direction', grid=grid, space_order=2, dtype=np.float64)
114+
symbolic = directional_derivative(expression, state, direction)
115+
linearized = TorchStencil(symbolic, state, weight, direction)
116+
perturbation = torch.randn_like(value)
117+
_, tangent = torch.func.jvp(
118+
lambda x: layer(x, coefficient), (value,), (perturbation,)
119+
)
120+
torch.testing.assert_close(linearized(value, coefficient, perturbation), tangent)

0 commit comments

Comments
 (0)