Skip to main content

engine.solvers.model_call

engine.solvers.model_call

Model-call solvers: what one call admits, samples, and costs, and what a trajectory resends.

Domain implementations behind mlsysim.solvers (added 2026-10-20). Each model is arithmetic on stated token counts and per-token floors: admission against a context window, temperature and top-p on a fixed logit vector, Bayes’ rule for a fallible check, and prefill plus decode time at the weight-transfer floors. None models queueing, batching, attention, or serving overhead.

Classes

Name Description
CheckerPosteriorModel Defects that survive a fallible check, by Bayes’ rule.
ContextAdmissionModel Token-measured admission: a request fits when S <= S_max - K_max.
ModelCallModel One model call at per-token floors: S * tau_prefill + K * tau_decode.
SamplingModel Temperature and top-p on one fixed logit vector.
TrajectoryResendModel Input a trajectory resends when every call carries the whole history.

CheckerPosteriorModel

engine.solvers.model_call.CheckerPosteriorModel()

Defects that survive a fallible check, by Bayes’ rule.

false_accept_rate is P(PASS | defective) and valid_accept_rate is P(PASS | valid). Rerunning the same check is not modeled: its errors are correlated with the first pass.

Methods

Name Description
solve Compute defect prevalence among items that pass a fallible check.
solve
engine.solvers.model_call.CheckerPosteriorModel.solve(
    defect_rate,
    false_accept_rate,
    valid_accept_rate,
)

Compute defect prevalence among items that pass a fallible check.

Parameters
Name Type Description Default
defect_rate float Prior defective-item fraction, in [0, 1]. required
false_accept_rate float Conditional probability of passing a defective item, in [0, 1]. required
valid_accept_rate float Conditional probability of passing a valid item, in [0, 1]. required
Returns
Name Type Description
CheckerPosteriorResult Dimensionless joint defective-pass rate, overall pass share, posterior defect probability, catch rate, and valid rejection rate.
Notes

Equation: P(defect|pass) = defect_rate*false_accept_rate / (defect_rate*false_accept_rate + (1-defect_rate)*valid_accept_rate).

Assumptions: The prior and conditional pass rates describe the same population; total pass probability must be positive.

Excludes: Correlated repeated checks, prevalence drift, and estimating checker accuracy from data.

References

Derivation: Partition passing items into defective and valid populations and normalize the defective joint probability by their sum (Bayes’ rule). physics.agents.calc_pass_posterior owns the normalization.

Examples
>>> from mlsysim.solvers import CheckerPosteriorModel
>>> result = CheckerPosteriorModel().solve(0.2, 0.1, 0.9)
>>> result.posterior < 0.2
True

ContextAdmissionModel

engine.solvers.model_call.ContextAdmissionModel()

Token-measured admission: a request fits when S <= S_max - K_max.

The serialized prompt (text plus any image charge) must fit in the context window with output_reserve tokens held back for the reply.

Methods

Name Description
solve Check a serialized prompt against a reserved context capacity.
solve
engine.solvers.model_call.ContextAdmissionModel.solve(
    window,
    output_reserve,
    text_tokens,
    image_tokens=0,
)

Check a serialized prompt against a reserved context capacity.

Parameters
Name Type Description Default
window int Positive total context-window capacity, in tokens. required
output_reserve int Nonnegative tokens reserved for output, at most window. required
text_tokens int Nonnegative serialized text-token count. required
image_tokens int Nonnegative image charge expressed in tokens; defaults to zero. 0
Returns
Name Type Description
ContextAdmissionResult Token-valued scalar capacity, demand and excess, with an admission boolean. Headroom is measured before the image charge.
Notes

Equation: Input ceiling is window - output_reserve. Total demand is text_tokens + image_tokens + output_reserve; admission requires demand <= window.

Assumptions: All counts use the serving model’s already serialized token accounting. A negative excess denotes spare capacity.

Excludes: Tokenization, KV-memory feasibility, attention work, and any claim that an admitted request can meet a latency target.

References

Derivation: The request must fit the sum of its text, image, and output reservation in a finite token capacity; this is a capacity-accounting identity.

Examples
>>> from mlsysim.solvers import ContextAdmissionModel
>>> result = ContextAdmissionModel().solve(100, 10, 80, 20)
>>> result.admitted
False

ModelCallModel

engine.solvers.model_call.ModelCallModel()

One model call at per-token floors: S * tau_prefill + K * tau_decode.

prefill_per_token is the compute floor 2P / R_peak and decode_per_token the weight-transfer floor P b / BW (see DecodeBoundModel). A shared prefix reused from cache leaves only the remaining prompt for fresh prefill. Optional: a deadline, and the success rates of the stages a reply must clear to be accepted.

Methods

Name Description
solve Account for one model call at supplied per-token timing floors.
solve
engine.solvers.model_call.ModelCallModel.solve(
    prefill_per_token,
    decode_per_token,
    prompt_tokens,
    output_tokens,
    shared_prefix_tokens=0,
    deadline=None,
    stage_success=None,
)

Account for one model call at supplied per-token timing floors.

Parameters
Name Type Description Default
prefill_per_token Quantity Nonnegative time per counted prompt token, convertible to seconds. required
decode_per_token Quantity Positive time per counted output token, convertible to seconds. required
prompt_tokens int Nonnegative prompt-token count. required
output_tokens int Nonnegative generated-token count; prompt plus output must be positive. required
shared_prefix_tokens int Reused prompt-token count from zero through prompt_tokens; defaults to zero. 0
deadline Quantity Nonnegative response-time target in seconds; None omits its ratio. None
stage_success Dict[str, float] Named stage-success probabilities in [0, 1]; multiplied when nonempty. None
Returns
Name Type Description
ModelCallResult Durations in seconds, decode rate in inverse seconds, token counts and dimensionless shares. Acceptance is None without stages.
Notes

Equation: Total cold time is prompt_tokens*prefill_per_token + output_tokens*decode_per_token. Fresh prefill subtracts shared_prefix_tokens from prompt_tokens; it does not replace the reported cold total.

Assumptions: Per-token floors stay constant and phases execute serially. Total time is positive; supplied stage probabilities multiply independently.

Excludes: Queueing, attention-context growth, batching, framework overhead, cache maintenance, and any inference of acceptance from timing.

References

Derivation: Multiply per-token time by its token count, then sum serial phases. Independent stage success probabilities multiply. Timing bounds are supplied inputs, not measurements produced by this model.

Examples
>>> from mlsysim import Q_
>>> from mlsysim.solvers import ModelCallModel
>>> result = ModelCallModel().solve(Q_(1, "ms"), Q_(2, "ms"), 10, 2, 5)
>>> result.fresh_prefill_time < result.prefill_time
True

SamplingModel

engine.solvers.model_call.SamplingModel()

Temperature and top-p on one fixed logit vector.

Softmax at the base and the sharpened temperature, the probability left to every non-top token (the tail), and the top-p nucleus at the base temperature, renormalized.

Methods

Name Description
solve Compare temperature and nucleus filtering for a fixed logit vector.
solve
engine.solvers.model_call.SamplingModel.solve(
    logits,
    base_temperature,
    sharp_temperature,
    top_p,
)

Compare temperature and nucleus filtering for a fixed logit vector.

Parameters
Name Type Description Default
logits Sequence[float] Finite dimensionless logits for at least two possible tokens. required
base_temperature float Positive temperature used for the base distribution and top-p filter. required
sharp_temperature float Positive comparison temperature; smaller values sharpen the distribution. required
top_p float Cumulative probability threshold in (0, 1] for retaining the largest base probabilities. required
Returns
Name Type Description
SamplingResult Dimensionless probability vectors, partition sum, tail ratio, nucleus size and its retained probability mass.
Notes

Equation: At temperature T, p_i=exp(z_i/T)/sum_j exp(z_j/T). The nucleus is the smallest descending-probability prefix whose mass reaches top_p.

Assumptions: Both distributions use identical fixed logits. Inputs must avoid overflow in the reported direct exponential partition sum and a zero comparison tail in the tail ratio.

Excludes: Random token draws, changes in future logits, and a prediction of quality, factuality, or diversity over a generated sequence.

References

Derivation: Softmax normalizes positive exponential weights. Sorting probabilities and accumulating to the chosen threshold defines the nucleus; physics.agents.calc_tempered_softmax and calc_nucleus implement these steps.

Examples
>>> from mlsysim.solvers import SamplingModel
>>> result = SamplingModel().solve([0.0, 1.0, 2.0], 1.0, 0.5, 0.8)
>>> result.sharp_tail < result.base_tail
True

TrajectoryResendModel

engine.solvers.model_call.TrajectoryResendModel()

Input a trajectory resends when every call carries the whole history.

Turn t sends the first prompt S0 plus t - 1 appended blocks of D tokens (model output plus tool observation). Without reuse the total is H S0 + D H (H - 1) / 2; reusing each earlier prompt prefix leaves S0 + (H - 1) D of fresh prefill. Times are at the stated floors.

Methods

Name Description
solve Count repeated prompt work across an append-only trajectory.
solve
engine.solvers.model_call.TrajectoryResendModel.solve(
    prefill_per_token,
    decode_per_token,
    first_prompt,
    appended,
    output_per_turn,
    turns,
)

Count repeated prompt work across an append-only trajectory.

Parameters
Name Type Description Default
prefill_per_token Quantity Nonnegative time per input token, convertible to seconds. required
decode_per_token Quantity Nonnegative time per generated token, convertible to seconds. required
first_prompt int Positive number of tokens in the first request. required
appended int Nonnegative output-plus-observation tokens added between calls. required
output_per_turn int Nonnegative output tokens generated on each turn. required
turns int Positive number of calls. required
Returns
Name Type Description
TrajectoryResendResult Token counts, dimensionless reuse reduction and input share, and prefill/decode durations in seconds.
Notes

Equation: Without prefix reuse, input is H*S + D*H*(H-1)/2. Perfect reuse computes S+(H-1)*D fresh tokens; output is H*output_per_turn.

Assumptions: Each turn appends the same token count. Reuse retains every earlier compatible prompt prefix, and per-token timing floors stay fixed.

Excludes: Cache eviction, context-window admission, cache-transfer time, and the context dependence of attention or decode work.

References

Derivation: The resend total is an arithmetic-series sum. Counting the first prompt and each later increment only once gives fresh work under prefix reuse; helpers in physics.agents own both token identities.

Examples
>>> from mlsysim import Q_
>>> from mlsysim.solvers import TrajectoryResendModel
>>> result = TrajectoryResendModel().solve(Q_(1, "ms"), Q_(2, "ms"), 10, 2, 1, 3)
>>> result.reused_prefill_time < result.prefill_time
True
Back to top