(rank, batch_size, num_batches, lo=0, hi=1)
| 531 | |
| 532 | |
| 533 | def 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 |
no test coverage detected