MCPcopy Create free account
hub / github.com/NVIDIA/DALI / test_dali_proxy_deterministic

Function test_dali_proxy_deterministic

dali/test/python/test_dali_proxy.py:287–346  ·  view source on GitHub ↗
(deterministic, debug=False)

Source from the content-addressed store, hash-verified

285@attr("pytorch")
286@params((False,), (True,))
287def test_dali_proxy_deterministic(deterministic, debug=False):
288 # Shows how DALI proxy can be configured for deterministic results
289 from nvidia.dali.plugin.pytorch.experimental import proxy as dali_proxy
290 import torchvision.datasets as datasets
291 import torch
292
293 # Use a high number of iterations for non-deterministic tests, even though
294 # we stop the test once we get different results (usually in the first iteration).
295 # For deterministic tests, we check that all runs produce the same results.
296 niterations = 3 if deterministic else 10
297 num_workers = 4
298 seed0 = 123456
299 seed1 = 5555464
300 seed2 = 775653
301
302 outputs = []
303 for i in range(niterations):
304 pipe = image_pipe(
305 random_pipe=True,
306 dali_device="gpu",
307 batch_size=1,
308 num_threads=1,
309 device_id=0,
310 seed=seed0,
311 prefetch_queue_depth=1,
312 )
313 outputs_i = []
314 torch.manual_seed(seed2)
315 with dali_proxy.DALIServer(pipe, deterministic=deterministic) as dali_server:
316 dataset = datasets.ImageFolder(jpeg, transform=dali_server.proxy, loader=read_filepath)
317 # many workers so that we introduce a lot of variability in the order of arrival
318 loader = dali_proxy.DataLoader(
319 dali_server,
320 dataset,
321 batch_size=1,
322 num_workers=num_workers,
323 shuffle=True,
324 worker_init_fn=lambda worker_id: np.random.seed(seed1 + worker_id),
325 )
326 outputs_i = []
327 for _ in range(num_workers):
328 for data, _ in loader:
329 outputs_i.append(data.cpu())
330 break
331 outputs.append(outputs_i)
332
333 if i > 0:
334 if deterministic:
335 for k in range(num_workers):
336 assert np.array_equal(outputs[i][k], outputs[0][k])
337 else:
338 for k in range(num_workers):
339 if not np.array_equal(outputs[i][k], outputs[0][k]):
340 return # OK
341
342 pipe._shutdown()
343 del pipe
344

Callers

nothing calls this directly

Calls 5

_shutdownMethod · 0.80
image_pipeFunction · 0.70
seedMethod · 0.45
appendMethod · 0.45
cpuMethod · 0.45

Tested by

no test coverage detected