Brainmu-SpikeCamera / src /code /project_config.py
sunbaby's picture
Upload 69 files
4719196
Raw
History Blame Contribute Delete
2.16 kB
"""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)