@@ -978,10 +978,14 @@ async def _run_upscaling(self, config: Config, ctx: Context):
978978 ** upscaler_kwargs
979979 ))[0 ]
980980
981- # Unload upscaling model immediately after use to free VRAM
982- logger .info (f"Unloading upscaling model { config .upscale .upscaler } to free VRAM" )
983- await self ._unload_model ('upscaling' , config .upscale .upscaler , ** upscaler_kwargs )
984- del self ._model_usage_timestamps [("upscaling" , config .upscale .upscaler )]
981+ # 如果 models_ttl > 0,则由清理任务自动卸载;否则立即卸载以释放显存
982+ if self .models_ttl > 0 :
983+ logger .info (f"Upscaling model { config .upscale .upscaler } will be unloaded after { self .models_ttl } s of inactivity" )
984+ else :
985+ # models_ttl == 0 表示永久保留,但 upscaling 模型占用显存较大,仍然立即卸载
986+ logger .info (f"Unloading upscaling model { config .upscale .upscaler } immediately to free VRAM" )
987+ await self ._unload_model ('upscaling' , config .upscale .upscaler , ** upscaler_kwargs )
988+ del self ._model_usage_timestamps [("upscaling" , config .upscale .upscaler )]
985989
986990 return result
987991
@@ -1092,7 +1096,7 @@ def calculate_iou(box_1, box_2):
10921096 async def _unload_model (self , tool : str , model : str , ** kwargs ):
10931097 logger .info (f"Unloading { tool } model: { model } " )
10941098 match tool :
1095- case 'colorization ' :
1099+ case 'colorizer ' :
10961100 await unload_colorization (model )
10971101 case 'detection' :
10981102 await unload_detection (model )
@@ -1130,13 +1134,16 @@ def _cleanup_gpu_memory(self):
11301134
11311135 # Background models cleanup job.
11321136 async def _detector_cleanup_job (self ):
1137+ logger .info (f"Model cleanup job started with models_ttl={ self .models_ttl } seconds" )
11331138 while True :
11341139 if self .models_ttl == 0 :
11351140 await asyncio .sleep (1 )
11361141 continue
11371142 now = time .time ()
11381143 for (tool , model ), last_used in list (self ._model_usage_timestamps .items ()):
1139- if now - last_used > self .models_ttl :
1144+ time_since_last_use = now - last_used
1145+ if time_since_last_use > self .models_ttl :
1146+ logger .info (f"Model { tool } /{ model } has been idle for { time_since_last_use :.1f} s (TTL: { self .models_ttl } s), unloading..." )
11401147 await self ._unload_model (tool , model )
11411148 del self ._model_usage_timestamps [(tool , model )]
11421149 await asyncio .sleep (1 )
0 commit comments