59 lines
3.6 KiB
Python
59 lines
3.6 KiB
Python
"""Opt-in Linux worker placement, stored alongside Deck's other settings."""
|
|
import json,os,sys
|
|
from pathlib import Path
|
|
|
|
NAME='cpu-partition.json'
|
|
|
|
def validate(value):
|
|
if not isinstance(value,dict) or set(value)-{'version','strata','qwen_tts','qwen_generation_threads','qwen_codec_threads','qwen_cpu_flash_attention'} or not {'version','strata','qwen_tts'}<=set(value) or type(value['version']) is not int or value['version']!=1:raise ValueError('Ungültige CPU-Aufteilung.')
|
|
for role in ('strata','qwen_tts'):
|
|
ids=value[role]
|
|
if not isinstance(ids,list) or not ids or any(type(n) is not int or not 0<=n<64 for n in ids) or len(set(ids))!=len(ids):raise ValueError('CPU-Aufteilung benötigt eindeutige CPU-IDs von 0 bis 63.')
|
|
if set(value['strata']) & set(value['qwen_tts']):raise ValueError('Strata und Qwen-TTS dürfen keine CPU teilen.')
|
|
for field in ('qwen_generation_threads','qwen_codec_threads'):
|
|
n=value.get(field,4)
|
|
if type(n) is not int or not 1<=n<=4:raise ValueError('Qwen-Threadzahlen müssen zwischen 1 und 4 liegen.')
|
|
if type(value.get('qwen_cpu_flash_attention',True)) is not bool:raise ValueError('Qwen-CPU-Flash-Attention muss ein Wahrheitswert sein.')
|
|
return value
|
|
|
|
def topology():
|
|
result={}
|
|
for cpu in sorted(os.sched_getaffinity(0)):
|
|
root=Path('/sys/devices/system/cpu')/f'cpu{cpu}'/'topology'
|
|
result[cpu]=(int((root/'physical_package_id').read_text()),int((root/'core_id').read_text()))
|
|
return result
|
|
|
|
def placement(value,cores):
|
|
"""Reject changed hardware and SMT siblings shared by the two workers."""
|
|
validate(value)
|
|
if not (set(value['strata'])|set(value['qwen_tts']))<=set(cores):raise ValueError('CPU-Aufteilung passt nicht zu den verfügbaren CPUs; Hardwarezuordnung prüfen.')
|
|
a={cores[n] for n in value['strata']};b={cores[n] for n in value['qwen_tts']}
|
|
if a & b:raise ValueError('Strata und Qwen-TTS dürfen keine SMT-Threads desselben Kerns teilen.')
|
|
for role,selected in (('strata',a),('qwen_tts',b)):
|
|
if {n for n,c in cores.items() if c in selected}!=set(value[role]):raise ValueError('CPU-Aufteilung muss alle verfügbaren SMT-Threads eines ausgewählten Kerns enthalten.')
|
|
physical=[]
|
|
for n in sorted(value['strata']):
|
|
if all(cores[n]!=cores[p] for p in physical):physical.append(n)
|
|
if len(physical)<2:raise ValueError('Strata benötigt mindestens zwei zugeordnete echte Kerne.')
|
|
aux=sorted(set(value['strata'])-set(physical))
|
|
if not aux:raise ValueError('Diese Strata-Aufteilung benötigt zusätzliche SMT-Threads für Hilfsthreads.')
|
|
return dict(strata=sorted(value['strata']),qwen_tts=sorted(value['qwen_tts']),qwen_generation_threads=value.get('qwen_generation_threads',4),qwen_codec_threads=value.get('qwen_codec_threads',4),qwen_cpu_flash_attention=value.get('qwen_cpu_flash_attention',True),strata_arguments=['--host-core','first','--pool-cores',','.join(map(str,physical[1:])),'--pool-workers',str(len(physical)-1),'--aux-cpus',','.join(map(str,aux))])
|
|
|
|
def load(state):
|
|
path=Path(state)/NAME
|
|
if not path.exists():return None
|
|
if not hasattr(os,'sched_setaffinity'):raise ValueError('CPU-Aufteilung benötigt Linux.')
|
|
return placement(json.loads(path.read_text()),topology())
|
|
|
|
def command(argv,cpus):
|
|
# A small exec launcher avoids preexec_fn in the multithreaded Deck server.
|
|
return [sys.executable,str(Path(__file__).resolve()),','.join(map(str,cpus)),*map(str,argv)] if cpus else argv
|
|
|
|
def main():
|
|
ids={int(n) for n in sys.argv[1].split(',')}
|
|
if not ids or not ids<=os.sched_getaffinity(0):raise ValueError('Angeforderte CPUs sind für den Worker nicht verfügbar.')
|
|
os.sched_setaffinity(0,ids)
|
|
os.execv(sys.argv[2],sys.argv[2:])
|
|
|
|
if __name__=='__main__':main()
|