Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.
Python calibrationclassificationdecision-modelhuggingfacejevmodernbertmultilingualnlppythonpytorchroutingtyped-decisionszero-shot
Changelog
All notable changes to the Laya.Onnx NuGet package are documented here. The format follows
Keep a Changelog, and the project uses
Semantic Versioning. Releases are tagged laya-dotnet-v<version>.
[0.1.0] - unreleased
First release: a port of the Laya Python SDK, tested against laya (Python) 0.4.11 (routing goldens: 0.4.12).
Added
LayaEngine.PredictBatch(ILayaBatchPredictor),LayaBatchOptions(BatchSize,SortByLength),LayaRouter.PredictBatchandLayaRouter.RouteBatchoverLayaBatchRequest, ported from PythonONNXAgent.predict_batchandRouter.predict_batch/route_batch: shared forward passes,batch_sizechunking,sort_by_lengthinside windows of eight batches (effective only for1 < batch_size < len(states)), routing grouped by checkpoint then question schema with input order restored, and per-requestlang_guess. Hooks and the routerdiagnosticsdictionary are not ported yet. Existing signatures are unchanged.- Per-language temperatures and per-call overrides, ported from Python
lang_temperatures/resolve_lang_temperaturesand thelang,max_lenandhead_max_lenarguments:LayaOptions.LangTemperatures(a table ofLayaLangTemperature, resolved when the engine is created, with Python's key normalisation, clamping, warning andValueErrortext; a router hands it to every engine it builds),Lang,MaxLenandHeadMaxLenonLayaPredictOptionsandLayaBatchOptions,MaxLenandHeadMaxLenonLayaBatchRequest, andLayaEngine.PredictLong(state, questions, LayaLongOptions)withLang. A language the table holds replaces both the checkpoint's temperature and its per-bucket map. A router forwards itslangargument, else the language it detected, to the checkpoint, andPredictBatchsplits a checkpoint's requests by token budget and, for a checkpoint with a table, by language. Token budgets are unchecked as in Python:0, a negative value or one too small for the options' markers throws the "option markers fit"ArgumentException. New interfacesILayaCallPredictor,ILayaLongLangPredictorandILayaLangTemperatureslet a router pass these on; existing signatures are unchanged. - Calibration files and histogram binning, ported from Python's
calibration=/load_calibration(apply_calibration_payload,apply_binning_map):LayaOptions.Calibration, the path of a JSON file read when the engine is created, after the checkpoint's temperatures and theLangTemperaturestable. Itstemperatureandtemperature_by_optionsreplace the checkpoint's (clamped, with Python's warning), and itsbinning_maprecalibratesAnswerConfidencealone (int(p * bins)of the unroundedmax(p), then rounded to four places; a bucket it lacks passes through; skipped for a call whoseLangthe table holds), soMinConfidenceand thePredictLongwindow choice see the binned value. A file Python refuses throwsInvalidDataExceptioncarrying Python's exception type and text. A version 2 file's checkpoint-identity warning is not emitted: the SDK has no equivalent of Python'smodel_id_or_pathstring; temperatures, binning and errors are unaffected. A router hands the path to every engine it builds. Existing signatures are unchanged. - Abstention gate, ported from Python
laya/confidence.py(min_confidence,jev_confidence):MinConfidence(Offor one threshold,ForBucketsfor a per-option-count map,BucketKeys),LayaPredictOptionswith newLayaEngine.Predict(state, questions, options)andLayaRouter.Predict(..., langGuess, options)overloads,LayaBatchOptions.MinConfidencefor engine and router batches, and on everyAnswerLowConfidence,Abstention(GateStates:passed,abstained,unevaluated;nullwhen ungated),AbstentionThresholdandJevConfidence(). Invalid thresholds throwArgumentExceptionwith Python'sValueErrortext.PredictLongtakes none, as in Python. Existing signatures are unchanged. - Behaviour change, to match Python (
system_oneispredict_batch([state])):LayaEngine.PredictandLayaRouter.Predict/PredictLongrefuse a null state withArgumentNullExceptioncarrying Python's text (state must not be None; pass a string, dict, or list) instead of answering the text"null", andLayaEngine.Predictwith an emptyQuestionSetreturns an empty answer set with zero usage instead of throwing.LayaEngine.Predictnow reports the usage diagnostics too, asPredictBatch([state])[0]does. An unknown model name now carries Python's exact message. LangGuess(a language code or a callback of the state), Python'slang_guess: per call through newLayaRouter.Route,PredictandPredictLongoverloads, and for every request throughLayaRouterOptions.LangGuess. Precedence is explicitlang, thenlang_guess, then detection; a hint that names no language falls through. Existing signatures are unchanged.LayaEmail.Stateoverload taking Python'smax_chars, consumed (never a state field) and applied only when cleaning.LayaConfig.TemperatureRawValues/TemperatureByOptionsRawValuesandCalibration.ClampTemperature(object?)/TemperatureRejected: Python'sclamp_temperatureandtemperature_rejectedon the JSON value itself.LayaEngine.PredictLongandLayaRouter.PredictLong, ported from PythonONNXAgent.predict_long: overlapping token windows, batched inference, deciding-window attribution, per-type aggregation and summed scan diagnostics.LayaEngine:choice,scoreandnoulpredictions over the exported ONNX checkpoints (english, multilingual, typed-decisions); all questions for a state run in one batched forward pass.- Loads both ONNX layouts: the fused
model.onnxand the splitencoder.onnx+head.onnx. - Answers report
confidenceandanswer_confidence(added in Python 0.3.21). - Python-compatible sequence building, JSON serialization and calibration, checked against goldens recorded from the Python SDK.
- Checkpoint router, language detection, email state and option shortlisting.
- Opt-in model download from Hugging Face.
- Samples: quickstart, routing and benchmark.
Fixed
- Language detection: a line or state written in capitals is no longer named a language on acronym-shaped
evidence (
MON DES EST LA,STORES LOS ANGELES LAS VEGAS), as Python's_shoutedrules; combining marks count with their script only when they have a combining class, so Devanagari matras read as the script; the per-field read falls through to the diacritic rule when a named language is disbelieved; the\w/\d/\s/isupper/stripclasses follow Python, including for characters outside the BMP. - Checkpoint config JSON is read by a reader that follows CPython's
json.loads(PythonJsonReader): the bareNaN,Infinityand-Infinityare accepted in value position only ({NaN: 1},nan,+Infinity,01, trailing commas, comments and single quotes stay errors), integer and float literals keep their Python type (-0is the int0,7.0a float, a 30-digit int prints in full), floats are bit-identical tofloat(), and no marker text stands in for a token.tokenizer_config.jsonis read the same way. A temperature given as an integer too large for a float fails the load with Python'sOverflowError: int too large to convert to float(InvalidDataException), while a float literal such as1e400reads as infinity and falls back to 1.0. - A warning is traced once per process for the same text and call site, as Python's default filter does
(
stacklevel): the site is the caller ofLayaEngine.Create/FromDirectory(temperature warning) or ofPredictLong(window and stride warnings), through the router the router's own call. Thepredict_longwindow and stride warnings now carry Python's texts. rl_agent_config.jsonandtokenizer_config.jsonare read as strict UTF-8 and a BOM is rejected, as Python'sjson.load(open(path))does on a UTF-8 locale (it opens them without anencoding, so on a default Windows install it would use the ANSI code page; the two agree on ASCII files).- Config and detection follow Python's loader for the cases the review found: JSON errors carry Python's exact text
(
Expecting value: line L column C (char P), the integer-digit limit text), nesting up to CPython's limit loads, config files are read in text mode so CRLF and a lone CR count as one newline;temperatureis iterated as Python does (a string yields its characters, a dict its keys, a number or null raisesTypeError),temperature_by_optionsmust be a mapping (AttributeError),option_layoutother thansequentialis refused, atemperaturelist shorter than three fails at the first prediction that needs the missing entry (IndexError), and a wrong-typedmax_len/head_max_lenis refused when the config is loaded, with theTypeErrortext Python raises at the first prediction (a float, string, null, list or object; a negative integer is refused with its own message); integers>= 0and booleans are accepted. See "Differences from Python" in the README.str.lower()is exact (final sigma, U+0130), only lists and tuples are walked for text (a set holds none and is not JSON serializable), and anyIReadOnlyDictionaryis walked as a mapping. - Behaviour change for callers passing lazy sequences as a state: only an
IList(arrays,List<T>) or a tuple is serialized as a JSON array and walked for language detection; a set,Queue<T>, LINQ iterator or any other non-list enumerable now throwsJsonExceptionwhen serialized (and holds no text for detection), as Python'sjson.dumpsrejects a set or generator. PassToList()/ToArray(). A tuple is no longer left-truncated when over budget (Python truncates from the left only for alist). Calibration.ClampTemperature(object?)andTemperatureRejectedaccept every boxed numeric type (int,short,byte,sbyte,ushort,uint,ulong,float,decimal,Half,nint,BigInteger,Int128), converted as Python'sfloat()rounds.- Email patterns see astral decimal digits (Unicode Nd, such as the mathematical double-struck digits) as digits, so a reply header whose date is written with them is recognised as in Python, while astral numbers that are not decimal digits stay word characters only.
- Case-insensitive email matching also treats the long s with
sand the Kelvin sign withk, asre.Idoes. - Email patterns count and bound code points, not UTF-16 units, so characters outside the BMP next to a
keyword or inside a
.{0,300}window behave as in Python. byte[]states are decoded as strict UTF-8 for detection, and invalid UTF-8 is no text, as in Python; serializing one for a prediction throws, as Python'sTypeErrordoes.- Route: a blank, whitespace,
C,C.UTF-8,POSIX,und,zxx,mulor.langfalls through to detection, and locale codes (en_US,en_US.UTF-8,EN) normalise as Python does; reasons quote caller text with Python'srepr(non-printable characters escaped). - Email cleaning: a negative
maxCharsslices as Python does (including themax_chars * 4pre-cut); U+001C..U+001F count as whitespace,\bfollows Python's\w, and case-insensitive matching treatsi,I,ıandİas one letter, asre.Idoes. - Checkpoint temperatures are read as Python does: a numeric string (
"2.5"," 2.5 ","1e1") is a number, andtrue,null, lists, dictionaries and non-numeric strings fall back to 1.0. The load warning is one message with Python's text, listing each entry asname=repr -> applied. LayaRouterOptions.MaxLoadeddefaults to 2, as Python'sRouter(max_loaded=2)does, so a router that alternates two languages no longer reloads a checkpoint on every request.- Undetermined Latin text uses the router's configured default, including its route reason.
- Language detection counts unlisted scripts, distinguishes mixed-language fields and lines
(
MixedSegment), and applies current script, stopword and loanword rules. - Email cleaning preserves unpunctuated requests before disclaimers, distinguishes prose from signatures and reply headers, and handles Portuguese, Spanish and French client markers.
- Oversized questions report the surviving option markers and the actual sequence budget.
- Score legends return the same rendered criterion text shown to the model, including JSON for structured levels.
- Golden fixtures regenerated from current Python for all three checkpoints and routing.
Calibration.Round4is exactly Python'sround(x, 4): it rounds the exact binary value half to even (the old scale-and-round differed on about 2% of values, e.g.0.00625and0.03125), and keeps the sign of a zero result.
Known gaps
- Python-only opt-in features are not yet ported: hooks, question option ordering, digest controls and
calibration binning (
load_calibration). Batching is ported without hooks.