Skip to content

Commit a2d253e

Browse files
committed
Switch to torch.compile being the default for the python backend
1 parent 1e92eb3 commit a2d253e

8 files changed

Lines changed: 48 additions & 60 deletions

File tree

src/deepwave/elastic.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,8 @@ def elastic(
442442
Can be a string specifying whether to use PyTorch's JIT ("jit"),
443443
torch.compile ("compile"), or eager mode ("eager"). Alternatively
444444
a boolean can be provided, with True using the Python backend
445-
with JIT, while the default, False, instead uses the compiled
446-
C/CUDA.
445+
with torch.compile, while the default, False, instead uses the
446+
compiled C/CUDA.
447447
448448
Returns:
449449
Tuple:
@@ -2317,9 +2317,9 @@ def elastic_python(
23172317
receivers_p_i_masked = torch.zeros_like(receivers_p_i)
23182318
receivers_p_i_masked[receiver_p_mask] = receivers_p_i[receiver_p_mask]
23192319

2320-
rdy = torch.tensor(1 / dy, dtype=dtype)
2321-
rdx = torch.tensor(1 / dx, dtype=dtype)
2322-
dt_tensor = torch.tensor(dt, dtype=dtype)
2320+
rdy = torch.tensor(1 / dy, dtype=dtype, device=device)
2321+
rdx = torch.tensor(1 / dx, dtype=dtype, device=device)
2322+
dt_tensor = torch.tensor(dt, dtype=dtype, device=device)
23232323

23242324
for step in range(nt // step_ratio):
23252325
if forward_callback is not None and step % callback_frequency == 0:
@@ -2522,11 +2522,9 @@ def elastic_func(
25222522
global _update_velocities_jit, _update_velocities_compile, _update_velocities_opt
25232523
global _update_stresses_jit, _update_stresses_compile, _update_stresses_opt
25242524

2525-
if not python_backend:
2526-
func = ElasticForwardFunc.apply
2527-
else:
2525+
if python_backend:
25282526
if python_backend is True:
2529-
mode = "jit"
2527+
mode = "compile"
25302528
elif isinstance(python_backend, str):
25312529
mode = python_backend.lower()
25322530
else:
@@ -2558,7 +2556,7 @@ def elastic_func(
25582556
else:
25592557
raise ValueError(f"Unknown python_backend value {mode!r}.")
25602558

2561-
func = elastic_python
2559+
func = elastic_python if python_backend else ElasticForwardFunc.apply
25622560

25632561
return cast(
25642562
"Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, "

src/deepwave/regular_grid.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
profiles for wave propagation simulations on a regular grid.
55
"""
66

7-
from typing import List, Union
7+
from typing import List
88

99
import torch
1010

@@ -83,8 +83,12 @@ def set_pml_profiles(
8383
device,
8484
pml_freq,
8585
)
86-
dbydy = diffx1(by, accuracy, 1 / grid_spacing[0])
87-
dbxdx = diffx1(bx, accuracy, 1 / grid_spacing[1])
86+
dbydy = diffx1(
87+
by, accuracy, torch.tensor(1 / grid_spacing[0], dtype=dtype, device=device)
88+
)
89+
dbxdx = diffx1(
90+
bx, accuracy, torch.tensor(1 / grid_spacing[1], dtype=dtype, device=device)
91+
)
8892
ay = ay[None, :, None]
8993
ax = ax[None, None, :]
9094
by = by[None, :, None]
@@ -94,9 +98,7 @@ def set_pml_profiles(
9498
return [ay, ax, by, bx, dbydy, dbxdx]
9599

96100

97-
def diffy1(
98-
a: torch.Tensor, accuracy: int, rdy: Union[torch.Tensor, float]
99-
) -> torch.Tensor:
101+
def diffy1(a: torch.Tensor, accuracy: int, rdy: torch.Tensor) -> torch.Tensor:
100102
"""Calculates the first derivative in the y-direction."""
101103
if accuracy == 2:
102104
return torch.nn.functional.pad(
@@ -133,9 +135,7 @@ def diffy1(
133135
)
134136

135137

136-
def diffx1(
137-
a: torch.Tensor, accuracy: int, rdx: Union[torch.Tensor, float]
138-
) -> torch.Tensor:
138+
def diffx1(a: torch.Tensor, accuracy: int, rdx: torch.Tensor) -> torch.Tensor:
139139
"""Calculates the first derivative in the x-direction."""
140140
if accuracy == 2:
141141
return torch.nn.functional.pad(
@@ -172,9 +172,7 @@ def diffx1(
172172
)
173173

174174

175-
def diffy2(
176-
a: torch.Tensor, accuracy: int, rdy2: Union[torch.Tensor, float]
177-
) -> torch.Tensor:
175+
def diffy2(a: torch.Tensor, accuracy: int, rdy2: torch.Tensor) -> torch.Tensor:
178176
"""Calculates the second derivative in the y-direction."""
179177
if accuracy == 2:
180178
return torch.nn.functional.pad(
@@ -215,9 +213,7 @@ def diffy2(
215213
)
216214

217215

218-
def diffx2(
219-
a: torch.Tensor, accuracy: int, rdx2: Union[torch.Tensor, float]
220-
) -> torch.Tensor:
216+
def diffx2(a: torch.Tensor, accuracy: int, rdx2: torch.Tensor) -> torch.Tensor:
221217
"""Calculates the second derivative in the x-direction."""
222218
if accuracy == 2:
223219
return torch.nn.functional.pad(

src/deepwave/scalar.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,8 @@ def scalar(
366366
Can be a string specifying whether to use PyTorch's JIT ("jit"),
367367
torch.compile ("compile"), or eager mode ("eager"). Alternatively
368368
a boolean can be provided, with True using the Python backend
369-
with JIT, while the default, False, instead uses the compiled
370-
C/CUDA.
369+
with torch.compile, while the default, False, instead uses the
370+
compiled C/CUDA.
371371
372372
Returns:
373373
Tuple:
@@ -2233,11 +2233,11 @@ def scalar_python(
22332233
nx = v.shape[-1]
22342234
n_receivers_per_shot = receivers_i.numel() // n_shots
22352235
receiver_amplitudes = torch.empty(0, device=device, dtype=dtype)
2236-
rdy = torch.tensor(1 / dy, dtype=dtype)
2237-
rdx = torch.tensor(1 / dx, dtype=dtype)
2236+
rdy = torch.tensor(1 / dy, dtype=dtype, device=device)
2237+
rdx = torch.tensor(1 / dx, dtype=dtype, device=device)
22382238
rdy2 = rdy**2
22392239
rdx2 = rdx**2
2240-
dt_tensor = torch.tensor(dt, dtype=dtype)
2240+
dt_tensor = torch.tensor(dt, dtype=dtype, device=device)
22412241

22422242
if receivers_i.numel() > 0:
22432243
receiver_amplitudes.resize_(nt, n_shots, n_receivers_per_shot)
@@ -2346,11 +2346,9 @@ def scalar_func(
23462346
"""
23472347
global _forward_step_jit, _forward_step_compile, _forward_step_opt
23482348

2349-
if not python_backend:
2350-
func = ScalarForwardFunc.apply
2351-
else:
2349+
if python_backend:
23522350
if python_backend is True:
2353-
mode = "jit"
2351+
mode = "compile"
23542352
elif isinstance(python_backend, str):
23552353
mode = python_backend.lower()
23562354
else:
@@ -2371,7 +2369,7 @@ def scalar_func(
23712369
else:
23722370
raise ValueError(f"Unknown python_backend value {mode!r}.")
23732371

2374-
func = scalar_python
2372+
func = scalar_python if python_backend else ScalarForwardFunc.apply
23752373

23762374
return cast(
23772375
"Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, "

src/deepwave/scalar_born.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@ def scalar_born(
270270
Can be a string specifying whether to use PyTorch's JIT ("jit"),
271271
torch.compile ("compile"), or eager mode ("eager"). Alternatively
272272
a boolean can be provided, with True using the Python backend
273-
with JIT, while the default, False, instead uses the compiled
274-
C/CUDA.
273+
with torch.compile, while the default, False, instead uses the
274+
compiled C/CUDA.
275275
276276
Returns:
277277
Tuple:
@@ -1795,11 +1795,11 @@ def scalar_born_python(
17951795
n_receiverssc_per_shot = receiverssc_i.numel() // n_shots
17961796
receiver_amplitudes = torch.empty(0, device=device, dtype=dtype)
17971797
receiver_amplitudessc = torch.empty(0, device=device, dtype=dtype)
1798-
rdy = torch.tensor(1 / dy, dtype=dtype)
1799-
rdx = torch.tensor(1 / dx, dtype=dtype)
1798+
rdy = torch.tensor(1 / dy, dtype=dtype, device=device)
1799+
rdx = torch.tensor(1 / dx, dtype=dtype, device=device)
18001800
rdy2 = rdy**2
18011801
rdx2 = rdx**2
1802-
dt_tensor = torch.tensor(dt, dtype=dtype)
1802+
dt_tensor = torch.tensor(dt, dtype=dtype, device=device)
18031803

18041804
if receivers_i.numel() > 0:
18051805
receiver_amplitudes.resize_(nt, n_shots, n_receivers_per_shot)
@@ -1977,11 +1977,9 @@ def scalar_born_func(
19771977
"""
19781978
global _forward_step_jit, _forward_step_compile, _forward_step_opt
19791979

1980-
if not python_backend:
1981-
func = ScalarBornForwardFunc.apply
1982-
else:
1980+
if python_backend:
19831981
if python_backend is True:
1984-
mode = "jit"
1982+
mode = "compile"
19851983
elif isinstance(python_backend, str):
19861984
mode = python_backend.lower()
19871985
else:
@@ -2002,7 +2000,7 @@ def scalar_born_func(
20022000
else:
20032001
raise ValueError(f"Unknown python_backend value {mode!r}.")
20042002

2005-
func = scalar_born_python
2003+
func = scalar_born_python if python_backend else ScalarBornForwardFunc.apply
20062004

20072005
return cast(
20082006
"Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, "

src/deepwave/staggered_grid.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
profiles for wave propagation simulations on a staggered grid.
55
"""
66

7-
from typing import List, Union
7+
from typing import List
88

99
import torch
1010

@@ -122,9 +122,7 @@ def set_pml_profiles(
122122
return [ay, ayh, ax, axh, by, byh, bx, bxh]
123123

124124

125-
def diffy1(
126-
a: torch.Tensor, accuracy: int, rdy: Union[torch.Tensor, float]
127-
) -> torch.Tensor:
125+
def diffy1(a: torch.Tensor, accuracy: int, rdy: torch.Tensor) -> torch.Tensor:
128126
"""Calculates the first y derivative at integer grid points."""
129127
if accuracy == 2:
130128
return torch.nn.functional.pad(
@@ -161,9 +159,7 @@ def diffy1(
161159
)
162160

163161

164-
def diffx1(
165-
a: torch.Tensor, accuracy: int, rdx: Union[torch.Tensor, float]
166-
) -> torch.Tensor:
162+
def diffx1(a: torch.Tensor, accuracy: int, rdx: torch.Tensor) -> torch.Tensor:
167163
"""Calculates the first x derivative at integer grid points."""
168164
if accuracy == 2:
169165
return torch.nn.functional.pad((a[..., 1:] - a[..., :-1]) * rdx, (1, 0))
@@ -198,9 +194,7 @@ def diffx1(
198194
)
199195

200196

201-
def diffyh1(
202-
a: torch.Tensor, accuracy: int, rdy: Union[torch.Tensor, float]
203-
) -> torch.Tensor:
197+
def diffyh1(a: torch.Tensor, accuracy: int, rdy: torch.Tensor) -> torch.Tensor:
204198
"""Calculates the first y derivative at half integer grid points."""
205199
if accuracy == 2:
206200
return torch.nn.functional.pad(
@@ -237,9 +231,7 @@ def diffyh1(
237231
)
238232

239233

240-
def diffxh1(
241-
a: torch.Tensor, accuracy: int, rdx: Union[torch.Tensor, float]
242-
) -> torch.Tensor:
234+
def diffxh1(a: torch.Tensor, accuracy: int, rdx: torch.Tensor) -> torch.Tensor:
243235
"""Calculates the first x derivative at half integer grid points."""
244236
if accuracy == 2:
245237
return torch.nn.functional.pad((a[..., 2:] - a[..., 1:-1]) * rdx, (1, 1))

tests/test_elastic.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
)
2020
from deepwave.wavelets import ricker
2121

22+
torch._dynamo.config.cache_size_limit = 256 # noqa: SLF001
23+
2224
DEFAULT_LAMB = 550000000
2325
DEFAULT_MU = 2200000000
2426
DEFAULT_BUOYANCY = 1 / 2200

tests/test_scalar.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from deepwave import IGNORE_LOCATION, Scalar, scalar
1111
from deepwave.common import cfl_condition, downsample, upsample
1212

13+
torch._dynamo.config.cache_size_limit = 256 # noqa: SLF001
14+
1315

1416
def scalarprop(
1517
model: torch.Tensor,
@@ -536,7 +538,7 @@ def wrap(python):
536538
torch.manual_seed(1)
537539
out_compiled = wrap(False)
538540
torch.manual_seed(1)
539-
out_python = wrap(True)
541+
out_python = wrap("jit")
540542
for oc, op in zip(out_compiled, out_python):
541543
assert torch.allclose(oc, op)
542544

@@ -1286,7 +1288,7 @@ def wrap(python):
12861288
return grads + grad_grads
12871289

12881290
torch.manual_seed(1)
1289-
out_python = wrap(True)
1291+
out_python = wrap("jit")
12901292
torch.manual_seed(1)
12911293
out_compiled = wrap(False)
12921294
for oc, op in zip(out_compiled, out_python):

tests/test_scalar_born.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from deepwave import IGNORE_LOCATION, ScalarBorn, scalar_born
99
from deepwave.common import cfl_condition, downsample, upsample
1010

11+
torch._dynamo.config.cache_size_limit = 256 # noqa: SLF001
12+
1113

1214
def test_born_scatter_2d() -> None:
1315
"""Test Born propagation in a 2D model with a point scatterer."""

0 commit comments

Comments
 (0)