-
Notifications
You must be signed in to change notification settings - Fork 493
Expand file tree
/
Copy pathexample_fluid_checkpoint.py
More file actions
507 lines (405 loc) · 18.9 KB
/
example_fluid_checkpoint.py
File metadata and controls
507 lines (405 loc) · 18.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
###########################################################################
# Example Fluid Checkpoint
#
# Shows how to implement a differentiable 2D stable-fluids solver and
# optimize the initial velocity field to form the NVIDIA logo at the end
# of the simulation. Gradient checkpointing to reduce memory usage
# is manually implemented.
#
# References:
# https://github.com/HIPS/autograd/blob/master/examples/fluidsim/fluidsim.py
#
###########################################################################
import math
import os
import sys
import numpy as np
import warp as wp
import warp.examples
import warp.optim
try:
from PIL import Image
except ImportError as err:
raise ImportError("This example requires the Pillow package. Please install it with 'pip install Pillow'.") from err
try:
import matplotlib
import matplotlib.pyplot as plt
MATPLOTLIB_AVAILABLE = True
except ImportError:
MATPLOTLIB_AVAILABLE = False
N_GRID = wp.constant(512)
DH = 1.0 / N_GRID # Grid spacing
FLUID_COLUMN_WIDTH = N_GRID / 10.0
@wp.func
def cyclic_index(idx: int):
"""Helper function to index with periodic boundary conditions."""
ret_idx = idx % N_GRID
if ret_idx < 0:
ret_idx += N_GRID
return ret_idx
@wp.kernel
def fill_initial_density(density: wp.array2d[float]):
"""Initialize the density array with three bands of fluid."""
i, j = wp.tid()
y_pos = float(i)
if FLUID_COLUMN_WIDTH <= y_pos < 2.0 * FLUID_COLUMN_WIDTH:
density[i, j] = 1.0
elif 4.5 * FLUID_COLUMN_WIDTH <= y_pos < 5.5 * FLUID_COLUMN_WIDTH:
density[i, j] = 1.0
elif 8.0 * FLUID_COLUMN_WIDTH <= y_pos < 9.0 * FLUID_COLUMN_WIDTH:
density[i, j] = 1.0
else:
density[i, j] = 0.0
@wp.kernel
def advect(
dt: float,
vx: wp.array2d[float],
vy: wp.array2d[float],
f0: wp.array2d[float],
f1: wp.array2d[float],
):
"""Move field f0 according to vx and vy velocities using an implicit Euler integrator."""
i, j = wp.tid()
center_xs = float(i) - vx[i, j] * dt
center_ys = float(j) - vy[i, j] * dt
# Compute indices of source cells.
left_idx = int(wp.floor(center_xs))
bot_idx = int(wp.floor(center_ys))
s1 = center_xs - float(left_idx) # Relative weight of right cell
s0 = 1.0 - s1
t1 = center_ys - float(bot_idx) # Relative weight of top cell
t0 = 1.0 - t1
i0 = cyclic_index(left_idx)
i1 = cyclic_index(left_idx + 1)
j0 = cyclic_index(bot_idx)
j1 = cyclic_index(bot_idx + 1)
# Perform bilinear interpolation of the four cells bounding the back-in-time position
f1[i, j] = s0 * (t0 * f0[i0, j0] + t1 * f0[i0, j1]) + s1 * (t0 * f0[i1, j0] + t1 * f0[i1, j1])
@wp.kernel
def divergence(wx: wp.array2d[float], wy: wp.array2d[float], div: wp.array2d[float]):
"""Compute div(w)."""
i, j = wp.tid()
div[i, j] = (
0.5
* (
wx[cyclic_index(i + 1), j]
- wx[cyclic_index(i - 1), j]
+ wy[i, cyclic_index(j + 1)]
- wy[i, cyclic_index(j - 1)]
)
/ DH
)
@wp.kernel
def jacobi_iter(div: wp.array2d[float], p0: wp.array2d[float], p1: wp.array2d[float]):
"""Calculate a single Jacobi iteration for solving the pressure Poisson equation."""
i, j = wp.tid()
p1[i, j] = 0.25 * (
-DH * DH * div[i, j]
+ p0[cyclic_index(i - 1), j]
+ p0[cyclic_index(i + 1), j]
+ p0[i, cyclic_index(j - 1)]
+ p0[i, cyclic_index(j + 1)]
)
@wp.kernel
def update_velocities(
p: wp.array2d[float],
wx: wp.array2d[float],
wy: wp.array2d[float],
vx: wp.array2d[float],
vy: wp.array2d[float],
):
"""Given p and (wx, wy), compute an 'incompressible' velocity field (vx, vy)."""
i, j = wp.tid()
vx[i, j] = wx[i, j] - 0.5 * (p[cyclic_index(i + 1), j] - p[cyclic_index(i - 1), j]) / DH
vy[i, j] = wy[i, j] - 0.5 * (p[i, cyclic_index(j + 1)] - p[i, cyclic_index(j - 1)]) / DH
@wp.kernel
def compute_loss(actual_state: wp.array2d[float], target_state: wp.array2d[float], loss: wp.array[float]):
i, j = wp.tid()
loss_value = (
(actual_state[i, j] - target_state[i, j]) * (actual_state[i, j] - target_state[i, j]) / float(N_GRID * N_GRID)
)
wp.atomic_add(loss, 0, loss_value)
class Example:
def __init__(self, sim_steps=1000):
self.pressure_arrays = []
self.wx_arrays = []
self.wy_arrays = []
self.vx_arrays = []
self.vy_arrays = []
self.density_arrays = []
self.div_arrays = []
# Memory usage is minimized when the segment size is approx. sqrt(sim_steps)
self.segment_size = math.ceil(math.sqrt(sim_steps))
# TODO: For now, let's just round up sim_steps so each segment is the same size
self.num_segments = math.ceil(sim_steps / self.segment_size)
self.sim_steps = self.segment_size * self.num_segments
self.pressure_iterations = 50
self.dt = 1.0
# Store enough arrays to step through a segment without overwriting arrays
# NOTE: Need an extra array to store the final time-advanced velocities and densities
for _step in range(self.segment_size + 1):
self.vx_arrays.append(wp.zeros((N_GRID, N_GRID), dtype=float, requires_grad=True))
self.vy_arrays.append(wp.zeros((N_GRID, N_GRID), dtype=float, requires_grad=True))
self.density_arrays.append(wp.zeros((N_GRID, N_GRID), dtype=float, requires_grad=True))
for _step in range(self.segment_size):
self.wx_arrays.append(wp.zeros((N_GRID, N_GRID), dtype=float, requires_grad=True))
self.wy_arrays.append(wp.zeros((N_GRID, N_GRID), dtype=float, requires_grad=True))
self.div_arrays.append(wp.zeros((N_GRID, N_GRID), dtype=float, requires_grad=True))
for _iter in range(self.pressure_iterations):
self.pressure_arrays.append(wp.zeros((N_GRID, N_GRID), dtype=float, requires_grad=True))
# Allocate one more pressure array for the final time step
self.pressure_arrays.append(wp.zeros((N_GRID, N_GRID), dtype=float, requires_grad=True))
# Allocate memory to save the fluid state at the start of each segment
self.segment_start_vx_arrays = []
self.segment_start_vy_arrays = []
self.segment_start_density_arrays = []
self.segment_start_pressure_arrays = []
for _segment_index in range(self.num_segments):
self.segment_start_vx_arrays.append(wp.zeros((N_GRID, N_GRID), dtype=float))
self.segment_start_vy_arrays.append(wp.zeros((N_GRID, N_GRID), dtype=float))
self.segment_start_density_arrays.append(wp.zeros((N_GRID, N_GRID), dtype=float))
self.segment_start_pressure_arrays.append(wp.zeros((N_GRID, N_GRID), dtype=float))
# To restore previously computed gradients before calling tape.backward()
self.vx_array_grad_saved = wp.zeros((N_GRID, N_GRID), dtype=float)
self.vy_array_grad_saved = wp.zeros((N_GRID, N_GRID), dtype=float)
self.density_array_grad_saved = wp.zeros((N_GRID, N_GRID), dtype=float)
self.pressure_array_grad_saved = wp.zeros((N_GRID, N_GRID), dtype=float)
wp.launch(fill_initial_density, (N_GRID, N_GRID), inputs=[self.density_arrays[0]])
target_base = Image.open(os.path.join(warp.examples.get_asset_directory(), "nvidia_logo.png"))
target_resized = target_base.resize((N_GRID, N_GRID))
target_np = np.array(target_resized)[:, :, 0] / 255.0
self.target_wp = wp.array(target_np, dtype=float)
self.loss = wp.zeros((1,), dtype=float, requires_grad=True)
self.train_rate = 0.01
self.optimizer = warp.optim.Adam([self.vx_arrays[0].flatten(), self.vy_arrays[0].flatten()], lr=self.train_rate)
# Capture forward/backward passes and tape.zero()
self.use_cuda_graph = wp.get_device().is_cuda
self.forward_graph = None
self.backward_graph = None
self.zero_tape_graph = None
if self.use_cuda_graph:
with wp.ScopedCapture() as capture:
self.forward()
self.forward_graph = capture.graph
with wp.ScopedCapture() as capture:
self.backward()
self.backward_graph = capture.graph
# tape.zero() launches many memsets, which can be a significant overhead for smaller problems
with wp.ScopedCapture() as capture:
self.tape.zero()
self.zero_tape_graph = capture.graph
def step(self, step_index) -> None:
"""Perform a single time step from t=step_index-1 to t=step_index.
1. Self-advection of velocity components (store output in wx_arrays and wy_arrays)
2. Incompressibility constraint (store output in vx_arrays and vy_arrays)
3. Advection of density using velocities (vx_arrays, vy_arrays)
"""
wp.launch(
advect,
(N_GRID, N_GRID),
inputs=[
self.dt,
self.vx_arrays[step_index - 1],
self.vy_arrays[step_index - 1],
self.vx_arrays[step_index - 1],
],
outputs=[self.wx_arrays[step_index - 1]],
)
wp.launch(
advect,
(N_GRID, N_GRID),
inputs=[
self.dt,
self.vx_arrays[step_index - 1],
self.vy_arrays[step_index - 1],
self.vy_arrays[step_index - 1],
],
outputs=[self.wy_arrays[step_index - 1]],
)
# Pressure projection using a few Jacobi iterations
wp.launch(
divergence,
(N_GRID, N_GRID),
inputs=[self.wx_arrays[step_index - 1], self.wy_arrays[step_index - 1]],
outputs=[self.div_arrays[step_index - 1]],
)
# NOTE: Uses previous step's final pressure as the initial guess
for k in range(self.pressure_iterations):
input_index = self.pressure_iterations * (step_index - 1) + k
output_index = input_index + 1
wp.launch(
jacobi_iter,
(N_GRID, N_GRID),
inputs=[self.div_arrays[step_index - 1], self.pressure_arrays[input_index]],
outputs=[self.pressure_arrays[output_index]],
)
# NOTE: output_index should be self.pressure_iterations*step_index at this point
wp.launch(
update_velocities,
(N_GRID, N_GRID),
inputs=[self.pressure_arrays[output_index], self.wx_arrays[step_index - 1], self.wy_arrays[step_index - 1]],
outputs=[self.vx_arrays[step_index], self.vy_arrays[step_index]],
)
wp.launch(
advect,
(N_GRID, N_GRID),
inputs=[
self.dt,
self.vx_arrays[step_index],
self.vy_arrays[step_index],
self.density_arrays[step_index - 1],
],
outputs=[self.density_arrays[step_index]],
)
def forward(self) -> None:
"""Advance the simulation forward in segments, storing the fluid state at the start of each segment.
The loss function is also evaluated at the end of the function.
"""
self.loss.zero_()
for segment_index in range(self.num_segments):
# Save start-of-segment values
wp.copy(self.segment_start_vx_arrays[segment_index], self.vx_arrays[0])
wp.copy(self.segment_start_vy_arrays[segment_index], self.vy_arrays[0])
wp.copy(self.segment_start_density_arrays[segment_index], self.density_arrays[0])
wp.copy(self.segment_start_pressure_arrays[segment_index], self.pressure_arrays[0])
for t in range(1, self.segment_size + 1):
# sim_t = (segment_index - 1) * self.segment_size + t
self.step(t)
# Set the initial conditions for the next segment
if segment_index < self.num_segments - 1:
wp.copy(self.vx_arrays[0], self.vx_arrays[-1])
wp.copy(self.vy_arrays[0], self.vy_arrays[-1])
wp.copy(self.density_arrays[0], self.density_arrays[-1])
wp.copy(self.pressure_arrays[0], self.pressure_arrays[-1])
wp.launch(
compute_loss,
(N_GRID, N_GRID),
inputs=[self.density_arrays[self.segment_size], self.target_wp],
outputs=[self.loss],
)
def backward(self) -> None:
"""Compute the adjoints using a checkpointing approach.
Starting from the final segment, the forward pass for the segment is
repeated, this time recording the kernel launches onto a tape. Any
previously computed adjoints are restored prior to evaluating the
backward pass for the segment. This process is repeated until the
adjoints of the initial state have been calculated.
"""
for segment_index in range(self.num_segments - 1, -1, -1):
# Restore state at the start of the segment
wp.copy(self.vx_arrays[0], self.segment_start_vx_arrays[segment_index])
wp.copy(self.vy_arrays[0], self.segment_start_vy_arrays[segment_index])
wp.copy(self.density_arrays[0], self.segment_start_density_arrays[segment_index])
wp.copy(self.pressure_arrays[0], self.segment_start_pressure_arrays[segment_index])
# Record operations on tape
with wp.Tape() as self.tape:
for t in range(1, self.segment_size + 1):
self.step(t)
if segment_index == self.num_segments - 1:
self.loss.grad.fill_(1.0)
wp.launch(
compute_loss,
(N_GRID, N_GRID),
inputs=[self.density_arrays[self.segment_size], self.target_wp],
outputs=[self.loss],
adj_inputs=[self.density_arrays[self.segment_size].grad, None],
adj_outputs=[self.loss.grad],
adjoint=True,
)
else:
# Fill in previously computed gradients from the last segment
wp.copy(self.vx_arrays[-1].grad, self.vx_array_grad_saved)
wp.copy(self.vy_arrays[-1].grad, self.vy_array_grad_saved)
wp.copy(self.density_arrays[-1].grad, self.density_array_grad_saved)
wp.copy(self.pressure_arrays[-1].grad, self.pressure_array_grad_saved)
self.tape.backward()
if segment_index > 0:
# Save the gradients to variables and zero-out the gradients for the next segment
wp.copy(self.vx_array_grad_saved, self.vx_arrays[0].grad)
wp.copy(self.vy_array_grad_saved, self.vy_arrays[0].grad)
wp.copy(self.density_array_grad_saved, self.density_arrays[0].grad)
wp.copy(self.pressure_array_grad_saved, self.pressure_arrays[0].grad)
self.tape.zero()
# Done with backward pass, we're interested in self.vx_arrays[0].grad and self.vy_arrays[0].grad
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--device", type=str, default=None, help="Override the default Warp device.")
parser.add_argument(
"--num-frames", type=int, default=1000, help="Number of frames to simulate before computing loss."
)
parser.add_argument("--train-iters", type=int, default=50, help="Total number of training iterations.")
parser.add_argument(
"--headless",
action="store_true",
help="Run in headless mode, suppressing the opening of any graphical windows.",
)
args = parser.parse_known_args()[0]
# Check visualization availability early (before training) so user can cancel if needed
can_visualize = False
if not args.headless:
if not MATPLOTLIB_AVAILABLE:
print(
"Warning: matplotlib not found. Skipping visualization. "
"Install matplotlib to enable visualization: pip install matplotlib",
file=sys.stderr,
)
# matplotlib is available, check if backend supports interactive display
elif matplotlib.get_backend().lower() == "agg":
print(
"Warning: No interactive matplotlib backend available. Skipping visualization. "
"Install python3-tk (Linux) or PySide6 to enable visualization.",
file=sys.stderr,
)
else:
can_visualize = True
with wp.ScopedDevice(args.device):
example = Example(sim_steps=args.num_frames)
wp.synchronize_device()
if (device := wp.get_device()).is_cuda:
print(f"Current memory usage: {wp.get_mempool_used_mem_current(device) / (1024 * 1024 * 1024):.4f} GiB")
# Main training loop
for train_iter in range(args.train_iters):
if example.forward_graph:
wp.capture_launch(example.forward_graph)
else:
example.forward()
if example.backward_graph:
wp.capture_launch(example.backward_graph)
else:
example.backward()
example.optimizer.step([example.vx_arrays[0].grad.flatten(), example.vy_arrays[0].grad.flatten()])
# Clear grad arrays for next iteration
if example.zero_tape_graph:
wp.capture_launch(example.zero_tape_graph)
else:
example.tape.zero()
print(f"Iteration {train_iter:05d} loss: {example.loss.numpy()[0]:.6f}")
# Visualization
if can_visualize:
if matplotlib.rcParams["figure.raise_window"]:
matplotlib.rcParams["figure.raise_window"] = False
fig, ax = plt.subplots()
image = ax.imshow(example.density_arrays[-1].numpy(), cmap="viridis", origin="lower", vmin=0, vmax=1)
ax.set_xticks([])
ax.set_yticks([])
ax.set_title("Fluid Density")
# Run the final simulation to the stop time
for _ in range(args.num_frames):
example.step(1)
# Swap pointers
(example.vx_arrays[0], example.vx_arrays[1]) = (example.vx_arrays[1], example.vx_arrays[0])
(example.vy_arrays[0], example.vy_arrays[1]) = (example.vy_arrays[1], example.vy_arrays[0])
(example.density_arrays[0], example.density_arrays[1]) = (
example.density_arrays[1],
example.density_arrays[0],
)
(example.pressure_arrays[0], example.pressure_arrays[example.pressure_iterations]) = (
example.pressure_arrays[example.pressure_iterations],
example.pressure_arrays[0],
)
image.set_data(example.density_arrays[0].numpy())
plt.pause(0.001)
plt.show()