Test memory usage and cleanup.
()
| 321 | |
| 322 | |
| 323 | def test_memory_usage(): |
| 324 | """Test memory usage and cleanup.""" |
| 325 | if not torch.cuda.is_available(): |
| 326 | print("⚠️ CUDA not available, skipping memory tests") |
| 327 | return |
| 328 | |
| 329 | print("=" * 60) |
| 330 | print("TESTING MEMORY USAGE") |
| 331 | print("=" * 60) |
| 332 | |
| 333 | test_images = create_test_images() |
| 334 | sample_image = list(test_images.values())[0] |
| 335 | |
| 336 | # Clear cache |
| 337 | torch.cuda.empty_cache() |
| 338 | initial_memory = torch.cuda.memory_allocated() |
| 339 | |
| 340 | print(f"Initial GPU memory: {initial_memory / 1024**2:.1f} MB") |
| 341 | |
| 342 | # Create captioner and run inference |
| 343 | captioner = GitBaseCaptioner(dtype=torch.float16, device="cuda", use_compile=False, use_channels_last=True) |
| 344 | |
| 345 | # Load model |
| 346 | captioner.load() |
| 347 | after_load_memory = torch.cuda.memory_allocated() |
| 348 | |
| 349 | print(f"Memory after model load: {after_load_memory / 1024**2:.1f} MB") |
| 350 | print(f"Model memory usage: {(after_load_memory - initial_memory) / 1024**2:.1f} MB") |
| 351 | |
| 352 | # Run inference |
| 353 | for i in range(10): |
| 354 | captioner.caption_image_fast(sample_image) |
| 355 | if i == 0: |
| 356 | after_first_inference = torch.cuda.memory_allocated() |
| 357 | print(f"Memory after first inference: {after_first_inference / 1024**2:.1f} MB") |
| 358 | |
| 359 | final_memory = torch.cuda.memory_allocated() |
| 360 | print(f"Final memory: {final_memory / 1024**2:.1f} MB") |
| 361 | print(f"Memory growth during inference: {(final_memory - after_first_inference) / 1024**2:.1f} MB") |
| 362 | |
| 363 | |
| 364 | def main(): |
no test coverage detected