"""Threaded row-local work; sequence decoding is performed once on complete emissions.""" from concurrent.futures import ThreadPoolExecutor from contextvars import copy_context import numpy as np from threadpoolctl import threadpool_limits from wisp.cpu.probability import labels_from_proba from wisp_release.checkpoints import require_sequence_metadata def parallel_rows(fn, X, workers): if not isinstance(workers,(int,np.integer)) or workers < 1: raise ValueError("workers must be a positive integer") workers=min(workers,max(1,len(X)//32)) if workers==1: return fn(X) bounds=np.linspace(0,len(X),workers+1,dtype=np.intp) with threadpool_limits(limits=1, user_api="blas"), ThreadPoolExecutor(max_workers=workers) as pool: jobs=[pool.submit(copy_context().run,fn,X[a:b]) for a,b in zip(bounds[:-1],bounds[1:])] first=jobs[0].result() out=np.empty((len(X),*first.shape[1:]),dtype=first.dtype) out[:bounds[1]]=first for a,b,job in zip(bounds[1:-1],bounds[2:],jobs[1:]): out[a:b]=job.result() return out def predict_parallel(model,X,*,groups=None,time_index=None,workers=4): if getattr(model,"feature_workers",1)>1: raise ValueError("Use either model feature workers or outer prediction workers") groups,time_index=require_sequence_metadata(model,groups,time_index,n=len(X)) P=parallel_rows(model.predict_proba,np.asarray(X),workers) if model.smoother_ is None: return labels_from_proba(P,model.classes_) return model.smoother_.predict_from_proba(P,groups=groups,time_index=time_index)