Run ordered native prediction batches
Use a native batch when several independent requests share a model key but have their own inputs and parameters. This tutorial checks two small Logistic predictions, preserves their association with the original requests, and tests the worker-count error contract. The curves are synthetic and their parameters are supplied directly.
Learning outcomes
Section titled “Learning outcomes”You will construct ordered native payloads, compare results with one and two
workers, and verify predictions against the Logistic equation. You will know
where to look for a failure affecting one dataset and when the whole call raises
NativeError.
Prerequisites
Section titled “Prerequisites”Install the current native development package and complete Predict a native diffusion curve. Run the example with the interpreter containing that package. It uses ordinary Python mappings and lists; it downloads no data and makes no network request.
Each payload contains an inputs.time array and its own parameters mapping.
These are the JSON-compatible inputs accepted by predict_model. The
predict_batch API accepts these
payloads; the separate table helpers accept Polars frames.
| Request position | Query times | Parameters | Expected quantity |
|---|---|---|---|
| 0 | 0 and 1 | L=100, k=1, x0=0 |
Two levels on the first curve |
| 1 | 0 | L=200, k=1, x0=0 |
One level on the second curve |
Choose a consistent time unit. k has inverse-time units, x0 uses the time
coordinate, and L uses the chosen level units. Worker count has no scientific
unit and does not change those definitions.
-
Run the canonical example from the repository root:
Terminal window .venv-native/bin/python -I examples/native_batch.py -
Follow the first call:
predict_batch("logistic", datasets, workers=2)returns one result row for each payload in its original position. The first row contains two predicted values; the second contains one. -
Compare those rows with the same call using
workers=1. Then check the analytic values independently and deliberately submitworkers=0.
"""Verify ordered batch results, analytic values and worker-count validation."""
import jsonfrom math import exp, isclose
from innovate import NativeError, predict_batch
def main() -> None: """Compare one and two workers using two distinct synthetic Logistic curves.""" datasets = [ {"inputs": {"time": [0, 1]}, "parameters": {"L": 100, "k": 1, "x0": 0}}, {"inputs": {"time": [0]}, "parameters": {"L": 200, "k": 1, "x0": 0}}, ] rows = predict_batch("logistic", datasets, workers=2) assert rows == predict_batch("logistic", datasets, workers=1) assert len(rows) == 2 and all(row["error"] is None for row in rows) assert rows[0]["result"]["values"][0] == 50 assert isclose(rows[0]["result"]["values"][1], 100 / (1 + exp(-1)), abs_tol=1e-12, rel_tol=0) assert rows[1]["result"]["values"] == [100] rejected = False try: predict_batch("logistic", datasets, workers=0) except NativeError as error: rejected = error.code == "invalid_request" assert rejected print( json.dumps( {"batch_items": len(rows), "invalid_workers_rejected": rejected, "worker_invariant": True}, sort_keys=True ) )
if __name__ == "__main__": main()The model key selects the Logistic equations. workers selects native batch
concurrency, with supported integer values from 1 through 32. It does not select
a different model, simulation engine or numerical backend. The
native batch implementation
collects results in input order, including when work is split into chunks.
Expected results
Section titled “Expected results”The following output is imported from the example runner’s expected-output contract. The script prints it only after all assertions pass.
{"batch_items": 2, "invalid_workers_rejected": true, "worker_invariant": true}worker_invariant reports equality of the two complete row lists in this
fixture. invalid_workers_rejected means that the zero-worker request raised
NativeError with code invalid_request. Neither field is a timing measurement.
Interpretation
Section titled “Interpretation”For this model the level is L / (1 + exp(-k * (t - x0))). At t=x0=0, the
first curve gives 50 and the second gives 100. At time 1, the first gives
100 / (1 + exp(-1)), approximately 73.105858. The script compares that value
with an absolute tolerance of 1e-12 and zero relative tolerance. It checks
levels rather than changes accumulated between query times.
Batch position and observation position have different roles. rows[1] means
the second input dataset; its values[0] means that dataset’s first query time.
Keep your own dataset identifiers alongside the ordered payloads so results can
be associated by position. The two curves do not become one pooled population
merely because they share a batch.
Inspect both error levels
Section titled “Inspect both error levels”A successful outer request returns rows containing result and error. An
individual prediction failure has a null result and an error object in its row;
other successful rows remain available. Inspect every row before consuming its
values. The Python binding
raises a non-null outer error, while the native batch retains per-item errors as
values.
The displayed fixture requires both rows to succeed and separately checks an invalid outer worker count. It does not execute a mixed-success batch. Its all-success assertion is appropriate for a regression example; an application that permits partial results must branch on each row’s error and record which inputs failed.
Check your understanding
Section titled “Check your understanding”- If you reverse the two payloads, which result should occupy
rows[0], and should that row contain one or two predicted values? - Would requesting two workers mean the first curve should return twice its one-worker prediction?
- If a valid batch contains one failed prediction, is catching
NativeErroraround the whole call sufficient to find it?
Compare your answers
- The payload with
L=200moves to position zero. It still has one query time, so its values contain the single midpoint level 100. - No. Workers control concurrency. The predicted level is determined by the supplied model, inputs and parameters; the example verifies row equality.
- No. Inspect each row’s
error. An outer request can succeed while retaining an individual failure in its original position.
The reversed-order and mixed-success cases are reasoning exercises here; the canonical script executes the two successful payloads and the invalid-worker case shown above.
Limitations
Section titled “Limitations”Two tiny deterministic requests establish the tested ordering, numerical and validation contracts. They do not establish a speedup, a useful worker count for a real workload, or reproducibility for every stochastic algorithm. Measure performance with the actual data size, machine, native build and serialization costs before choosing concurrency.
This example neither fits parameters nor evaluates predictive accuracy on observations. It also does not exercise fitting batches or Polars frame helpers. See runtime and execution choices for those boundaries and the fitting guide for estimation.