Test GPU optimizations if available.
()
| 96 | |
| 97 | |
| 98 | def test_gpu_optimizations(): |
| 99 | """Test GPU optimizations if available.""" |
| 100 | if not torch.cuda.is_available(): |
| 101 | print("⚠️ CUDA not available, skipping GPU tests") |
| 102 | return |
| 103 | |
| 104 | print("=" * 60) |
| 105 | print("TESTING GPU OPTIMIZATIONS") |
| 106 | print("=" * 60) |
| 107 | |
| 108 | test_images = create_test_images() |
| 109 | sample_image = list(test_images.values())[0] |
| 110 | |
| 111 | # Test different optimization combinations |
| 112 | configs = [ |
| 113 | {"name": "Baseline FP32", "dtype": torch.float32, "use_compile": False, "use_channels_last": False}, |
| 114 | {"name": "FP16 Optimization", "dtype": torch.float16, "use_compile": False, "use_channels_last": False}, |
| 115 | {"name": "BF16 Optimization", "dtype": torch.bfloat16, "use_compile": False, "use_channels_last": False}, |
| 116 | {"name": "Channels Last", "dtype": torch.float16, "use_compile": False, "use_channels_last": True}, |
| 117 | {"name": "Torch Compile + FP16", "dtype": torch.float16, "use_compile": True, "use_channels_last": False}, |
| 118 | {"name": "All Optimizations", "dtype": torch.float16, "use_compile": True, "use_channels_last": True}, |
| 119 | ] |
| 120 | |
| 121 | results = {} |
| 122 | |
| 123 | for config in configs: |
| 124 | try: |
| 125 | print(f"\n--- Testing {config['name']} ---") |
| 126 | |
| 127 | captioner = GitBaseCaptioner( |
| 128 | dtype=config["dtype"], |
| 129 | device="cuda", |
| 130 | use_compile=config["use_compile"], |
| 131 | use_channels_last=config["use_channels_last"], |
| 132 | ) |
| 133 | |
| 134 | # Warmup run |
| 135 | _ = captioner.caption_image_fast(sample_image) |
| 136 | |
| 137 | # Benchmark runs |
| 138 | times = [] |
| 139 | captions = [] |
| 140 | |
| 141 | for i in range(5): |
| 142 | start_time = time.time() |
| 143 | caption = captioner.caption_image_fast(sample_image) |
| 144 | elapsed = time.time() - start_time |
| 145 | times.append(elapsed) |
| 146 | captions.append(caption) |
| 147 | |
| 148 | avg_time = sum(times) / len(times) |
| 149 | results[config["name"]] = avg_time |
| 150 | |
| 151 | print(f"Average time: {avg_time:.3f}s") |
| 152 | print(f"Sample caption: {captions[0]}") |
| 153 | print(f"Caption consistency: {len(set(captions))} unique captions out of 5") |
| 154 | |
| 155 | except Exception as e: |
no test coverage detected