Spaces:
Running on Zero
Running on Zero
Keep core models loaded across requests on T4
Browse filesOn T4 with concurrency_limit=1, models are now loaded once globally
and persist across all sequential requests. Only Paladin models are
cleaned up after each request, while core models (CTransPath, Optimus,
Aeon, marker_classifier) remain in GPU memory.
This eliminates the overhead of reloading 8-12GB of models for every
single-slide request on T4 instances.
High-memory GPUs continue to use the previous per-batch loading strategy.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
33 unmodified lines34353637383977 unmodified lines11711811912012112212312490 unmodified lines21521621721821922022122222333 unmodified linesreversed_cancer_subtype_name_map = {}cancer_subtypes = []def set_cancer_subtype_maps(csn_map, rcsn_map, cs):"""Set the global cancer subtype maps."""77 unmodified lines)# Load models once (for batch) or per-slide (for single)model_cache = Noneif len(slides) > 1:logger.info(f"Batch mode: Loading models once for {len(slides)} slides")progress(0.0, desc=f"Loading models for batch processing")model_cache = load_all_models(use_gpu=True, aggressive_memory_mgmt=None)90 unmodified linesfinally:# Clean up model cache if it was loaded for batch processingif model_cache is not None:logger.info("Cleaning up model cache")model_cache.cleanup()progress(0.99, desc="Analysis complete, wrapping up results")33 unmodified lines34353637383940414277 unmodified lines12012112212312412512612712812913013113213313413513613713813914090 unmodified lines23123223323423523623723823924024124224324433 unmodified linesreversed_cancer_subtype_name_map = {}cancer_subtypes = []# Global model cache for T4 (to persist models across sequential requests)_global_model_cache = Nonedef set_cancer_subtype_maps(csn_map, rcsn_map, cs):"""Set the global cancer subtype maps."""77 unmodified lines)# Load models once (for batch) or per-slide (for single)# On T4: Keep models loaded globally across all requests (concurrency=1 ensures no conflicts)# On high-memory GPUs: Load models per-batch, reload for single slidesglobal _global_model_cachemodel_cache = Noneif IS_T4_GPU:# T4: Use global cache to keep models loaded across requestsif _global_model_cache is None:logger.info("T4: Loading models once (will persist across all requests)")progress(0.0, desc="Loading models (one-time initialization)")_global_model_cache = load_all_models(use_gpu=True, aggressive_memory_mgmt=None)else:logger.info(f"T4: Reusing pre-loaded models from global cache")model_cache = _global_model_cacheelif len(slides) > 1:logger.info(f"Batch mode: Loading models once for {len(slides)} slides")progress(0.0, desc=f"Loading models for batch processing")model_cache = load_all_models(use_gpu=True, aggressive_memory_mgmt=None)90 unmodified linesfinally:# Clean up model cache if it was loaded for batch processing# On T4: Keep global cache loaded, only cleanup Paladin models# On high-memory GPUs: Cleanup everything after batchif model_cache is not None and not IS_T4_GPU:logger.info("Cleaning up model cache after batch")model_cache.cleanup()elif IS_T4_GPU and model_cache is not None:logger.info("T4: Keeping core models loaded, cleaning up Paladin models only")model_cache.cleanup_paladin()progress(0.99, desc="Analysis complete, wrapping up results")
src/mosaic/ui/app.py+24-3