File size: 2,160 Bytes
4719196
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
30
31
32
33
"""Project configuration, not a Transformers AutoConfig or base-model config."""
import json,math
from pathlib import Path
DEFAULT_CONFIG=Path(__file__).resolve().parents[2]/'config.json'
def load_config(path=None):
    c=json.loads(Path(path or DEFAULT_CONFIG).read_text())
    if c['schema_version']!=1:raise ValueError('Unsupported config schema')
    i,f=c['input'],c['frontend']
    if any(type(i[k]) is not int or i[k]<=0 for k in ['frames','height','width']):raise ValueError('Invalid input dimensions')
    if i['height']*i['width']%8:raise ValueError('Frame dimensions must be byte aligned')
    if i['packed_bytes']!=i['frames']*i['height']*i['width']//8:raise ValueError('packed_bytes mismatch')
    if i['bitorder'] not in ['little','big'] or type(i['flip_height']) is not bool:raise ValueError('Invalid packing/orientation')
    ch=f['channels']
    if len(ch)<2 or any(type(n) is not int or n<=0 for n in ch) or ch[0]!=i['frames'] or ch[-1]!=1:raise ValueError('Invalid frontend channels')
    if f['architecture']!='Conv2dReLUStack' or f['activation']!='relu':raise ValueError('Unsupported frontend architecture')
    if type(f['kernel_size']) is not int or f['kernel_size']<=0 or f['kernel_size']%2!=1 or f['stride']!=1 or f['padding']!=f['kernel_size']//2:raise ValueError('Only shape-preserving convolutions supported')
    if f['output_clamp']!=[0.,1.]:raise ValueError('Output must use [0,1] scale')
    g=c['generation']['inference']
    if not isinstance(g['prompt'],str) or not g['prompt'].strip():raise ValueError('Empty prompt')
    if type(g['steps']) is not int or g['steps']<=0 or type(g['seed']) is not int:raise ValueError('Invalid steps/seed')
    for k in ['cfg_text_scale','cfg_img_scale','timestep_shift','cfg_renorm_min']:
        if not math.isfinite(g[k]):raise ValueError('Nonfinite inference parameter')
    return c

def load_frontend_weights(path):
    path=Path(path)
    if path.suffix=='.safetensors':
        from safetensors.torch import load_file
        return load_file(str(path),device='cpu')
    import torch
    value=torch.load(path,map_location='cpu',weights_only=True)
    return value.get('model',value)