|
| 1 | +"""Benchmark script for PyTorch ImageNet dataloader using shared TFDS pipeline.""" |
| 2 | + |
| 3 | +import time |
| 4 | + |
| 5 | +import jax |
| 6 | +import numpy as np |
| 7 | +import tensorflow as tf |
| 8 | +tf.config.set_visible_devices([], 'GPU') # Disable TF GPU usage |
| 9 | +import tensorflow_datasets as tfds |
| 10 | +import torch |
| 11 | +import torch.distributed as dist |
| 12 | + |
| 13 | +from algoperf import pytorch_utils |
| 14 | +from algoperf.workloads.imagenet_resnet import input_pipeline |
| 15 | + |
| 16 | +# ImageNet constants (same as workload) |
| 17 | +TRAIN_MEAN = (0.485 * 255, 0.456 * 255, 0.406 * 255) |
| 18 | +TRAIN_STDDEV = (0.229 * 255, 0.224 * 255, 0.225 * 255) |
| 19 | +CENTER_CROP_SIZE = 224 |
| 20 | +RESIZE_SIZE = 256 |
| 21 | +ASPECT_RATIO_RANGE = (0.75, 4.0 / 3.0) |
| 22 | +SCALE_RATIO_RANGE = (0.08, 1.0) |
| 23 | + |
| 24 | + |
| 25 | +def main(): |
| 26 | + USE_PYTORCH_DDP, RANK, DEVICE, N_GPUS = pytorch_utils.pytorch_setup() |
| 27 | + |
| 28 | + # Initialize DDP process group |
| 29 | + if USE_PYTORCH_DDP: |
| 30 | + torch.cuda.set_device(RANK) |
| 31 | + dist.init_process_group('nccl') |
| 32 | + |
| 33 | + data_dir = '/home/ak4605/algoperf-data/imagenet/jax' |
| 34 | + global_batch_size = 1024 |
| 35 | + num_batches = 100 |
| 36 | + |
| 37 | + if RANK == 0: |
| 38 | + print(f'Creating PyTorch ImageNet dataloader (shared TFDS pipeline)...') |
| 39 | + print(f'Batch size: {global_batch_size}') |
| 40 | + print(f'Num GPUs: {N_GPUS}') |
| 41 | + print(f'USE_PYTORCH_DDP: {USE_PYTORCH_DDP}') |
| 42 | + |
| 43 | + # Calculate per-device batch size for DDP |
| 44 | + if USE_PYTORCH_DDP: |
| 45 | + batch_size = global_batch_size // N_GPUS |
| 46 | + else: |
| 47 | + batch_size = global_batch_size |
| 48 | + |
| 49 | + if RANK == 0: |
| 50 | + print(f'Per-device batch size: {batch_size}') |
| 51 | + |
| 52 | + rng = jax.random.PRNGKey(0) |
| 53 | + ds_builder = tfds.builder('imagenet2012:5.1.0', data_dir=data_dir) |
| 54 | + |
| 55 | + ds = input_pipeline.create_split( |
| 56 | + split='train', |
| 57 | + dataset_builder=ds_builder, |
| 58 | + rng=rng, |
| 59 | + global_batch_size=batch_size, |
| 60 | + train=True, |
| 61 | + image_size=CENTER_CROP_SIZE, |
| 62 | + resize_size=RESIZE_SIZE, |
| 63 | + mean_rgb=TRAIN_MEAN, |
| 64 | + stddev_rgb=TRAIN_STDDEV, |
| 65 | + cache=False, |
| 66 | + repeat_final_dataset=True, |
| 67 | + aspect_ratio_range=ASPECT_RATIO_RANGE, |
| 68 | + area_range=SCALE_RATIO_RANGE, |
| 69 | + use_mixup=False, |
| 70 | + use_randaug=False, |
| 71 | + image_format='NCHW', |
| 72 | + threadpool_size=48 if USE_PYTORCH_DDP else 48, |
| 73 | + ) |
| 74 | + |
| 75 | + ds_iter = iter(ds) |
| 76 | + |
| 77 | + def get_batch(): |
| 78 | + batch = next(ds_iter) |
| 79 | + inputs = torch.from_numpy(batch['inputs'].numpy()).to(DEVICE) |
| 80 | + targets = torch.from_numpy(batch['targets'].numpy()).to(DEVICE, dtype=torch.long) |
| 81 | + return {'inputs': inputs, 'targets': targets} |
| 82 | + |
| 83 | + # Warmup |
| 84 | + if RANK == 0: |
| 85 | + print('Warming up...') |
| 86 | + for i in range(5): |
| 87 | + start = time.perf_counter() |
| 88 | + batch = get_batch() |
| 89 | + end = time.perf_counter() |
| 90 | + if RANK == 0: |
| 91 | + print(f' Warmup batch {i+1}/5: {(end - start)*1000:.2f}ms') |
| 92 | + |
| 93 | + if RANK == 0: |
| 94 | + print(f"Batch 'inputs' shape: {batch['inputs'].shape}") |
| 95 | + |
| 96 | + # Synchronize before benchmark |
| 97 | + if USE_PYTORCH_DDP: |
| 98 | + dist.barrier() |
| 99 | + |
| 100 | + # Benchmark |
| 101 | + if RANK == 0: |
| 102 | + print(f'Benchmarking {num_batches} batches...') |
| 103 | + times = [] |
| 104 | + for i in range(num_batches): |
| 105 | + if USE_PYTORCH_DDP: |
| 106 | + dist.barrier() |
| 107 | + start = time.perf_counter() |
| 108 | + batch = get_batch() |
| 109 | + end = time.perf_counter() |
| 110 | + times.append(end - start) |
| 111 | + if RANK == 0 and (i + 1) % 20 == 0: |
| 112 | + print(f' Batch {i+1}/{num_batches}: {times[-1]*1000:.2f}ms') |
| 113 | + |
| 114 | + times = np.array(times) |
| 115 | + if RANK == 0: |
| 116 | + print(f'\n=== PyTorch DataLoader Results ===') |
| 117 | + print(f'Mean time per batch: {times.mean()*1000:.2f}ms') |
| 118 | + print(f'Std time per batch: {times.std()*1000:.2f}ms') |
| 119 | + print(f'Min time per batch: {times.min()*1000:.2f}ms') |
| 120 | + print(f'Max time per batch: {times.max()*1000:.2f}ms') |
| 121 | + print(f'Throughput: {global_batch_size / times.mean():.2f} images/sec') |
| 122 | + |
| 123 | + # Print machine-readable results for the fish script |
| 124 | + print(f'\n=== RESULTS ===') |
| 125 | + print(f'MEAN_MS={times.mean()*1000:.2f}') |
| 126 | + print(f'THROUGHPUT={global_batch_size / times.mean():.2f}') |
| 127 | + |
| 128 | + if USE_PYTORCH_DDP: |
| 129 | + dist.destroy_process_group() |
| 130 | + |
| 131 | + |
| 132 | +if __name__ == '__main__': |
| 133 | + main() |
0 commit comments