@@ -503,6 +503,8 @@ async def ensure_subcache_for(self, node_id, children_ids):
503503
504504RAM_CACHE_OLD_WORKFLOW_OOM_MULTIPLIER = 1.3
505505
506+ RAM_CACHE_LARGE_INTERMEDIATE = 512 * 1024 ** 2
507+
506508
507509def all_outputs_dynamic (outputs ):
508510 if outputs is None :
@@ -517,7 +519,6 @@ def all_outputs_dynamic(outputs):
517519
518520 return True
519521
520-
521522class RAMPressureCache (LRUCache ):
522523
523524 def __init__ (self , key_class , enable_providers = False ):
@@ -539,9 +540,9 @@ def set_local(self, node_id, value):
539540 self .timestamps [self .cache_key_set .get_data_key (node_id )] = time .time ()
540541 super ().set_local (node_id , value )
541542
542- def ram_release (self , target , free_active = False ):
543+ def ram_release (self , target , free_active = False , min_entry_size = 0 ):
543544 if psutil .virtual_memory ().available >= target :
544- return
545+ return 0
545546
546547 clean_list = []
547548
@@ -555,28 +556,36 @@ def ram_release(self, target, free_active=False):
555556 oom_score = RAM_CACHE_OLD_WORKFLOW_OOM_MULTIPLIER ** (self .generation - self .used_generation [key ])
556557
557558 ram_usage = RAM_CACHE_DEFAULT_RAM_USAGE
559+ oom_ram_usage = ram_usage
558560 def scan_list_for_ram_usage (outputs ):
559- nonlocal ram_usage
561+ nonlocal ram_usage , oom_ram_usage
560562 if outputs is None :
561563 return
562564 for output in outputs :
563565 if isinstance (output , (list , tuple )):
564566 scan_list_for_ram_usage (output )
565567 elif isinstance (output , torch .Tensor ) and output .device .type == 'cpu' :
566568 ram_usage += output .numel () * output .element_size ()
569+ oom_ram_usage += output .numel () * output .element_size ()
567570 elif isinstance (output , ModelPatcher ) and self .used_generation [key ] != self .generation :
568571 #old ModelPatchers are the first to go
569- ram_usage = 1e30
572+ oom_ram_usage = 1e30
570573 scan_list_for_ram_usage (cache_entry .outputs )
571574
572- oom_score *= ram_usage
575+ if ram_usage < min_entry_size :
576+ continue
577+
578+ oom_score *= oom_ram_usage
573579 #In the case where we have no information on the node ram usage at all,
574580 #break OOM score ties on the last touch timestamp (pure LRU)
575- bisect .insort (clean_list , (oom_score , self .timestamps [key ], key ))
581+ bisect .insort (clean_list , (oom_score , self .timestamps [key ], key , ram_usage ))
576582
583+ freed = 0
577584 while psutil .virtual_memory ().available < target and clean_list :
578- _ , _ , key = clean_list .pop ()
585+ _ , _ , key , ram_usage = clean_list .pop ()
579586 del self .cache [key ]
580587 self .used_generation .pop (key , None )
581588 self .timestamps .pop (key , None )
582589 self .children .pop (key , None )
590+ freed += ram_usage
591+ return freed
0 commit comments