File size: 1,086 Bytes
c2b7494
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
27
28
29
"""Use prepared local snapshots when available, retaining exact model revisions."""
import json
import gc
import sys
from pathlib import Path


def prepared_model_path(repo):
    root=Path(__file__).resolve().parents[2]
    receipt=root/'.model-cache/model_revisions.json'
    if receipt.exists():
        info=json.loads(receipt.read_text()).get(repo)
        if info and Path(info['path']).is_dir():return info['path']
    return repo


def release_local_models():
    """Release cached weights before another local model or phase is loaded."""
    for module_name,function in (('src.llm.decisions','_laya_router'),
                                 ('src.llm.jpt_runtime','load_jpt'),
                                 ('src.llm.local','load_mlx_reviewer')):
        module=sys.modules.get(module_name)
        if module is not None:getattr(module,function).cache_clear()
    gc.collect()
    torch=sys.modules.get('torch')
    if torch is not None and torch.backends.mps.is_available():torch.mps.empty_cache()
    mx=sys.modules.get('mlx.core')
    if mx is not None:mx.clear_cache()