File size: 1,355 Bytes
0209b2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
"""Bounded, observable mode degradation after GPU memory exhaustion."""
def is_oom(error):
    text=str(error).lower()
    return any(s in text for s in ('out of vram','vram不足','cuda out of memory','cuda error: out of memory','torch.outofmemoryerror','cuda_error_out_of_memory'))

def remaining_modes(mode):
    return {'shared':['dual','sequential'],'dual':['sequential'],'sequential':[]}.get(mode,[])

def execute(plan,attempt,select,record,cancelled):
    while True:
        if cancelled():return None,plan
        try:return attempt(plan),plan
        except Exception as exc:
            if not is_oom(exc) or cancelled():raise
            old=plan;plan=None
            for mode in remaining_modes(old['mode']):
                try:plan=select(mode)
                except RuntimeError as unavailable:
                    record(dict(event='unavailable',mode=mode,reason=str(unavailable)))
                    continue
                record(dict(event='switch',previous=old['mode'],mode=mode,reason=str(exc),ocr_gpu=plan['ocr_gpu'],gemma_gpu=plan['gemma_gpu']))
                break
            if plan is None:
                record(dict(event='stopped',mode=old['mode'],reason=str(exc)))
                raise RuntimeError('Out of VRAM: all available execution modes were tried. Processing stopped with saved results preserved.') from exc