|
| 1 | +import torch |
| 2 | + |
| 3 | + |
| 4 | +assert torch.cuda.is_available(), 'Benchmark works only on cuda' |
| 5 | +device = torch.device("cuda") |
| 6 | + |
| 7 | + |
| 8 | +def create_spikes_tensor(percent_of_true_values, sparse): |
| 9 | + spikes_tensor = torch.bernoulli( |
| 10 | + torch.full((500, 500, 500), percent_of_true_values, device=device) |
| 11 | + ).bool() |
| 12 | + if sparse: |
| 13 | + spikes_tensor = spikes_tensor.to_sparse() |
| 14 | + |
| 15 | + torch.cuda.reset_peak_memory_stats(device=device) |
| 16 | + return round(torch.cuda.max_memory_allocated(device=device) / (1024 ** 2)) |
| 17 | + |
| 18 | + |
| 19 | +print('======================= ====================== ====================== ====================') |
| 20 | +print('Sparse (megabytes used) Dense (megabytes used) Ratio (Sparse/Dense) % % of non zero values') |
| 21 | +print('======================= ====================== ====================== ====================') |
| 22 | +percent_of_true_values = 0.005 |
| 23 | +while percent_of_true_values < 0.1: |
| 24 | + result = {} |
| 25 | + for sparse in [True, False]: |
| 26 | + result[sparse] = create_spikes_tensor(percent_of_true_values, sparse) |
| 27 | + percent = round((result[True] / result[False]) * 100) |
| 28 | + |
| 29 | + row = [ |
| 30 | + str(result[True]).ljust(23), |
| 31 | + str(result[False]).ljust(22), |
| 32 | + str(percent).ljust(22), |
| 33 | + str(round(percent_of_true_values * 100, 1)).ljust(20), |
| 34 | + ] |
| 35 | + print(' '.join(row)) |
| 36 | + percent_of_true_values += 0.005 |
| 37 | + |
| 38 | +print('======================= ====================== ====================== ====================') |
0 commit comments