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

Function fast_large_random_batches

dali/test/python/operator_2/test_reduce.py:533–565  ·  view source on GitHub ↗
(rank, batch_size, num_batches, lo=0, hi=1)

Source from the content-addressed store, hash-verified

531
532
533def fast_large_random_batches(rank, batch_size, num_batches, lo=0, hi=1):
534 max_vol = 10000000
535 max_extent = min(65536, int(np.floor(max_vol ** (1 / rank))))
536
537 # generate a maximum size buffer pre-filled with random numbers
538 global _random_buf
539 global _random_lo
540 global _random_hi
541 should_generate = (
542 _random_buf is None
543 or _random_buf.size < max_extent**rank
544 or _random_lo != lo
545 or _random_hi != hi
546 )
547 if should_generate:
548 _random_lo = lo
549 _random_hi = hi
550 _random_buf = np.random.uniform(low=lo, high=hi, size=max_vol).astype(np.float32)
551
552 data = []
553 for _ in range(num_batches):
554 batch = []
555 for _ in range(batch_size):
556 size = np.random.randint(1, max_extent, size=rank)
557 vol = np.prod(size)
558 # now that we know the actual volume of the sample, we can pick a random
559 # location in the pre-filled buffer
560 offset = np.random.randint(0, (_random_buf.size - vol) + 1)
561 # take a slice and reshape it to the desired shape - these are constant time operations
562 sample = _random_buf[offset : offset + vol].reshape(size)
563 batch.append(sample)
564 data.append(batch)
565 return data
566
567
568@nottest

Callers 2

_test_reduce_large_dataFunction · 0.85
_test_std_dev_large_dataFunction · 0.85

Calls 4

floorMethod · 0.80
minFunction · 0.50
reshapeMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected