runtime¶
Responsibility¶
Execute coalition jobs concurrently, skip duplicates, keep generated text for scoring, persist the Run archive, cache prompt prefixes for backends that can reuse them, and collect timings.
Public surface¶
InferenceScheduler(max_inflight, generation, store, dedup, archive, pending_limit, progress):run(jobs, generate)andrun_iter(jobs, generate, total=None)consume a job iterator lazily inside oneasyncio.TaskGroup; a semaphore ofpending_limitslots (default4 * max_inflight), released by each task’s done callback, bounds live tasks, and a second semaphore bounds concurrentgeneratecalls tomax_inflight. The first failure cancels the other tasks and is re-raised unwrapped (_first_errorsearches nested groups past cancellations); a failing job iterator propagates the same way. Progress reports one cumulative plan across calls.SchedulerMetrics:requested,executed,deduplicated,cache_hits,kv_cache_hits,kv_cache_reused_tokens; the scheduler leaves the two key-value (KV) cache counters at 0 and the orchestrator fills them from the backend.CoalitionJob: coalition key, snapshot id, rendered snapshot, absence policy, packed mask, model id,prefix_hash.build_coalition_key(...): SHA-256 over snapshot id, player ids, packed mask, absence policy, model id and generation parameters. Later draws, non-defaultpolicy_optionsand a setbackend.options.processor_kwargsextend that key.CoalitionDedupRegistry;dedup_enabled(config, generation)(autois on at any temperature).HotResultStore(maxsize): LRU map from coalition key toStoredGeneration(text, logprobs,forced,audio_path;put,get,logprobs,entry); the orchestrator sizes it to2^n.RunArchive:open(root, manifest, flush_every, on_exists)(raisesArchiveExistsErrorfor a finished archive underon_exists="error", always replaces an unfinished one),load(root)(opens the database read-only (mode=ro, no schema setup, andimmutable=1when the-walfile is missing or empty, so no locks or-shmfile), so it writes nothing and works on read-only storage and NFS;ArchiveNotFoundError, aFileNotFoundError, withoutmanifest.jsonorarchive.sqlite;ArchiveCorruptErrorwhen the manifest is not a JSON object or the database cannot be read;ManifestVersionErroron another schema),write_manifest(manifest),write_base_generation,append(draft),mark_complete,is_complete,history_lazy(). One SQLite file with WAL journaling, generation text, logprobs, teacher-forced scores (JSON,NULLwithout them) and audio path in the row,ARCHIVE_SCHEMA_VERSION(SQLiteuser_version) andMANIFEST_SCHEMA_VERSION5, plusmanifest.jsonandbase_generation.txt.read_manifest_payload(path): the manifest’s JSON object, orArchiveCorruptError;check_manifest_schema_version(payload).PrefixCacheManager(max_entries=8):longest_prefix(token_ids),store,reset; LRU-bounded token-prefix states, croppable states (CroppableCache) matched on any shared leading tokens.build_snapshot_prefix_hash(snapshot, player_set, mask)hashes the presence of every player but the last (Prefix hash);group_jobs_for_prefix_cachesorts by it.crop_to(cache, keep)cuts a croppable state back tokeeptokens with a negativecropargument, whichtransformers4 and 5 both read as tokens to drop (a positive one raises on 5).ObservabilitySink,NullObservabilitySink,InMemoryObservabilitySink,SpanRecord;CoalitionProgress,NullCoalitionProgress;PerfSummary(with the run’sCostCountersfields; backends implementingReportsCostexpose cumulative counters, the orchestrator reports the difference).
Data flow¶
In: a lazy
CoalitionJobiterator and an asyncgenerate(snapshot) -> str | GenerationRecordfrom the pipeline orchestrator.Out: text and logprobs per coalition key in the store, archive rows (cache hits carry the stored logprobs),
SchedulerMetrics.Imports domain (settings in
domain/config.py) and masking (codec); no longer imports the pipeline, so the import cycle is gone.
Invariants¶
A coalition key identifies one generation request; duplicate keys reuse the stored text.
A run either finishes (
mark_complete) or leaves an archivereanalyzerefuses; no partial success is reported.No “Task exception was never retrieved” warnings: every task belongs to the task group.
RunArchive.loadnever writes: reading an archive leaves its files byte-identical.
Known issues¶
None open. Fixed: C02, C06, C07 (Fixed issues).