70 / MODEL SERVING · INFERENCE / MODEL INFRASTRUCTURE
70 / PRODUCTION / INFERENCE SERVER · BATCHING · KV CACHE · THROUGHPUT · LATENCY

MODEL SERVING
& INFERENCE.

Model Serving — production-слой, который загружает model weights, принимает inference requests, планирует их на accelerator-ах, управляет batching, KV cache, concurrency, streaming, memory и replicas и превращает вычисление модели в надёжный service endpoint.

Главный принцип: модель может быть одинаковой, а производительность serving stack — отличаться в разы. Production inference — это не «запустить generate() на GPU», а scheduler + memory manager + batching + admission + streaming + health + capacity planning.
00. ARCHITECTURAL STATUS

ОТДЕЛЬНЫЙ SERVING LAYER НУЖЕН, КОГДА МОДЕЛЬ ИСПОЛНЯЕТСЯ В ВАШЕЙ ИНФРАСТРУКТУРЕ ИЛИ ВАЖНО УПРАВЛЯТЬ CAPACITY

Если вы используете только внешние hosted model APIs, большая часть serving internals принадлежит provider-у. Но архитектурные знания всё равно полезны для route/capacity decisions. При self-hosted/local/private inference №70 становится полноценной production подсистемой.
TYPEPRODUCTIONInference runtime infrastructure.
DEFAULTCONDITIONALRequired when you own inference runtime.
ENABLE WHENSELF / PRIVATE / CONTROLLED SERVINGOr when capacity-aware model routing matters.
SEPARATE COMPONENTYESInference server / serving cluster.
LIVES INMODEL INFRASTRUCTURE + MODEL ROUTER + PRODUCTION FABRICServing capacity feeds routing.
COMPLEXITYMEDIUM → VERY HIGHSingle GPU → distributed cluster.
IMPLEMENT: CONDITIONAL
Минимум 80% ценности: one inference endpoint, model loaded once, bounded request queue, max context/output limits, continuous batching where supported, KV-cache accounting, streaming, admission by available memory/concurrency, overload rejection/backpressure, health probes, exact model revision, p50/p95 TTFT/TPOT/tokens-per-second, OOM protection, warmup, rolling/canary deploy and route integration with №65. Distributed model parallelism comes later.
01A. ARCHITECTURE BOUNDARIES & OPERATIONS

EXPLICIT SYSTEM CONTRACT

A. BOUNDARY WITH NEIGHBORS

№65 Model Gateway & Routing normalizes requests and selects eligible model/provider/endpoint; №70 owns runtime execution inside selected inference endpoint: batching, accelerator scheduling, memory/KV, streaming, throughput and health. №71 Local LLM / Self-Hosted owns deployment choice/topology/privacy/operations strategy; №70 is serving mechanics regardless whether endpoint is on one workstation, private cloud or cluster. №72 Quantization owns precision/compression choices; №70 consumes an already selected model artifact/precision and measures serving effects. №64 Limits/Budgets owns higher-level quotas; №70 has local admission/capacity limits. №63 Resilience owns retry/breaker semantics; serving exposes health/typed overload failures.

B. PREREQUISITES / CROSS-REFERENCES

Prerequisites: №46 Observability, №50 Contracts, №51 Permissions & Secrets, №63 Resilience, №64 Limits/Budgets, №65 Model Gateway. Forward references: №71 Local LLM / Self-Hosted Models, №72 Quantization, №73 Multimodal AI, №75 Voice/Realtime, №76 Data Governance, №77 Production Architecture.

C. PLANE PLACEMENT

REQUEST-TIME: admission, scheduling, prefill/decode, batching, streaming, cancellation. CONTROL PLANE: model revision, precision, max context, batch policy, replica topology, scheduler policy, capacity/SLO targets. DATA PLANE: tokens/tensors, KV-cache blocks, request queues, streaming deltas, usage metrics. OFFLINE: model loading/build artifacts, warmup, benchmark, profiling, capacity planning, regression/canary tests.

D. FAILURE & OPERATIONS CONTRACT

Success: accepted request completes or streams output within declared limits and exact model revision. Retryable: transient serving unavailable, replica crash, some overload/timeouts if upstream budget allows. Permanent: context too large, unsupported modality/config, model revision absent, hard memory requirement impossible. Idempotency: inference is usually computation-only; retry may produce different text because generation can be nondeterministic. Persist/trace: request/model revision, queue delay, batch, prefill/decode timing, token counts, cancellation/failure reason, replica/accelerator identity.

E. WHAT THIS TOPIC DOES NOT OWN

№70 не владеет provider abstraction, cognitive model selection, model training/fine-tuning, self-hosting business decision, quantization algorithms, prompt design or application-level retries. Она владеет THE EXECUTION RUNTIME THAT TURNS A MODEL ARTIFACT INTO A CAPACITY-MANAGED INFERENCE SERVICE.

01. REQUEST LIFECYCLE

MODEL CALL ПРОХОДИТ НЕ ЧЕРЕЗ ОДИН generate(), А ЧЕРЕЗ SERVING PIPELINE

REQUESTPrompt/messages, max output, sampling, stream, deadline.
VALIDATEModel revision, context, modality, limits.
ADMISSIONQueue/memory/capacity available?
SCHEDULEJoin active batch / wait / reject.
PREFILLProcess prompt tokens and create KV state.
DECODEGenerate token by token.
STREAMEmit deltas; handle cancel/deadline.
RELEASEFree KV/resources; report usage.
Каждый этап имеет отдельные bottlenecks и метрики. «Inference latency = 4 s» слишком грубо для диагностики.
02. PREFILL VS DECODE

ДВА РАЗНЫХ COMPUTE REGIMES В AUTOREGRESSIVE LLM INFERENCE

PREFILL

Prompt processing

Модель обрабатывает весь входной контекст, вычисляет hidden states and creates KV cache. Long prompts increase prefill compute and memory. This stage often benefits from high parallel compute throughput.

DECODE

One token at a time

Каждый следующий token depends on previous tokens. Decode is iterative, latency-sensitive and heavily influenced by memory bandwidth, KV-cache footprint and batching.

Long-context workload can be prefill-heavy; chat/realtime workloads are highly sensitive to decode inter-token latency. Serving policy should know workload mix.
03. KV CACHE

НЕ ПЕРЕСЧИТЫВАТЬ ATTENTION ДЛЯ ВСЕГО PREFIX ПРИ КАЖДОМ НОВОМ TOKEN

PROMPT N input tokens prefill once KV CACHE per active sequence grows with context consumes accelerator memory released at completion/cancel DECODE token t+1 reuses prior KV
KV cache is often the practical concurrency limiter for long-context generation. Model weights may fit GPU memory while 20 simultaneous contexts do not.
04. MEMORY BUDGET

GPU / ACCELERATOR MEMORY = WEIGHTS + KV + WORKSPACE + RUNTIME OVERHEAD

AVAILABLE DEVICE MEMORY

┌──────────────────────────────────────┐
│ MODEL WEIGHTS                        │
├──────────────────────────────────────┤
│ KV CACHE FOR ACTIVE REQUESTS         │
│  request A: long context             │
│  request B: medium context           │
│  request C: decoding                 │
├──────────────────────────────────────┤
│ TEMP / ATTENTION / KERNEL WORKSPACE  │
├──────────────────────────────────────┤
│ RUNTIME / FRAGMENTATION / HEADROOM   │
└──────────────────────────────────────┘

DO NOT plan at 100% theoretical memory.

Admission must leave safety headroom
for runtime allocations and fragmentation.
OOM during inference is usually serving/admission failure, not «model unexpectedly broke». Prevent it before request execution.
05. STATIC BATCHING

НЕСКОЛЬКО REQUESTS В ОДИН COMPUTE BATCH

BENEFIT

Higher accelerator utilization

Parallel work amortizes kernel launch/compute overhead and increases throughput.

COST

Wait for batch

Interactive request may sit idle while server waits to form batch.

RIGIDITY

Different sequence lengths

Static batches waste compute when requests finish at different times or have uneven prompt/output lengths.

Static batching fits offline/batch inference better than highly variable chat traffic.
06. CONTINUOUS BATCHING

REQUESTS МОГУТ ВХОДИТЬ И ВЫХОДИТЬ ИЗ ACTIVE BATCH ПО МЕРЕ GENERATION

STEP 1A, B, C decode together.
C FINISHESRelease C KV slot.
D ENTERSAdmit new request into freed capacity.
A, B, DContinue token generation.
A FINISHESCapacity recycled again.
Continuous batching is a core reason modern LLM serving can achieve good utilization with variable request lengths.
07. BATCHING TRADE-OFF

THROUGHPUT И LATENCY ТЯНУТ SCHEDULER В РАЗНЫЕ СТОРОНЫ

POLICY
TTFT
THROUGHPUT
GPU UTIL
FAIRNESS
BEST FOR
SMALL / LOW WAIT
LOW
MED
MED
good
interactive
AGGRESSIVE BATCH
higher
HIGH
HIGH
needs policy
batch/offline
MIXED CLASSES
bounded
HIGH
HIGH
priority-aware
production mix
Оптимизация «максимум tokens/sec» может ухудшить user experience. SLOs should constrain batching policy.
08. SERVING SCHEDULER

КАКОЙ REQUEST ПОЛУЧИТ COMPUTE НА СЛЕДУЮЩЕМ STEP?

FIFO

Simple

Good baseline but long requests can create head-of-line blocking.

PRIORITY

Interactive first

Separate urgent user traffic from background batch jobs.

FAIR SHARE

Tenant/workload fairness

No single user/tenant monopolizes decode slots/KV memory.

DEADLINE-AWARE

SLO-aware

Schedule requests that can still meet deadline; reject those already impossible.

Scheduler is serving-local. Global tenant quotas and economics remain №64; serving translates them into local capacity decisions.
09. ADMISSION CONTROL

НЕ ПРИНИМАТЬ В ОЧЕРЕДЬ РАБОТУ, КОТОРУЮ НЕЛЬЗЯ ЗАВЕРШИТЬ В РАЗУМНЫЙ СРОК

QUEUE LENGTH

Backlog cap

Reject/defer when waiting requests exceed useful threshold.

KV MEMORY

Predicted footprint

Estimate input + max output impact before admission.

DEADLINE

Can we meet SLO?

Estimated queue + service time should fit remaining deadline.

CLASS

Capacity reservation

Reserve some capacity for interactive/high-priority traffic.

Unbounded queue turns overload into terrible latency and memory pressure. Fail fast can be more reliable than «accept everything».
10. CONTEXT & OUTPUT LIMITS

MAX TOKENS — ЭТО CAPACITY CONTROL, НЕ ТОЛЬКО PRODUCT SETTING

LimitServing impactControl
MAX INPUT TOKENSPrefill compute and KV footprint.Reject/truncate upstream according to context policy.
MAX OUTPUT TOKENSDecode time + future KV growth.Hard generation ceiling.
MAX TOTAL TOKENSProtect model context limit.Input + reserved output ≤ model supported window.
MAX CONCURRENT TOKENSAggregate KV/cache pressure.Scheduler/admission-level global capacity bound.
User-requested max_output=100k cannot be treated as harmless hint if it destroys capacity planning.
11. STREAMING

OUTPUT DELTAS УЛУЧШАЮТ PERCEIVED LATENCY, НО СОЗДАЮТ НОВЫЕ FAILURE STATES

TTFT

Time To First Token

User sees response start before full generation ends.

DELTA STREAM

Incremental delivery

Text/tool-call chunks are sent as decode progresses.

MID-STREAM FAILURE

Partial output exists

Retry cannot silently concatenate another model's fresh generation as if nothing happened.

Serving API should emit explicit stream lifecycle: started, delta, usage, finish or typed error. Upstream decides whether to restart whole generation.
12. CANCELLATION

ЕСЛИ USER УШЁЛ ИЛИ DEADLINE EXPIRED, ОСВОБОДИТЬ GPU И KV КАК МОЖНО БЫСТРЕЕ

CLIENT CANCELConnection closes / explicit cancel.
MARK REQUESTCANCEL_REQUESTED.
SCHEDULER STOPSNo further decode steps.
FREE KVRelease memory blocks.
ACCOUNTReport actually consumed tokens/compute.
Cancellation is capacity feature. Zombie generations consume expensive accelerator time while nobody needs the output.
13. PREFIX / PROMPT CACHE

ПОВТОРЯЮЩИЙСЯ PREFIX МОЖНО НЕ PREFILL-ИТЬ ПОЛНОСТЬЮ КАЖДЫЙ РАЗ — ЕСЛИ RUNTIME ЭТО ПОДДЕРЖИВАЕТ

PREFIX MATCH

Shared system/context prefix

Repeated exact token prefix can reuse precomputed KV-like state/runtime cache depending on serving implementation.

BENEFIT

Lower prefill cost/TTFT

Especially useful for long stable system prompts or shared document prefixes.

INVALIDATION

Exact model/tokenizer/config

Cache key must bind model revision, tokenizer, relevant runtime settings and exact prefix tokens.

№62 owns general caching policy. №70 owns serving-specific prefix/KV reuse mechanics. Approximate semantic cache is a different concept.
14. MODEL LOAD & WARMUP

MODEL READY ≠ PROCESS STARTED

LOAD WEIGHTS

Disk/object store → memory

Can take seconds/minutes for large models.

INITIALIZE

Runtime / kernels / memory pools

Allocate serving structures and distributed groups.

WARMUP

Representative inference

Trigger lazy compilation/kernel initialization and measure readiness.

READY

Accept traffic

Only after model revision is loaded and health/capacity checks pass.

Readiness probe must not return healthy merely because HTTP server can answer /health while model weights are still loading.
15. COLD START

МАСШТАБИРОВАНИЕ «С НУЛЯ» МОЖЕТ БЫТЬ СЛИШКОМ МЕДЛЕННЫМ ДЛЯ INTERACTIVE SLO

COLD

Load + initialize

New replica not immediately available; large model artifact transfer dominates.

MIN WARM REPLICAS

Capacity floor

Keep baseline ready capacity for interactive traffic.

PRE-WARM

Predictable peaks

Scale ahead of known traffic windows instead of after queue already explodes.

Autoscaling decision must include model startup time, not only current GPU utilization.
16. CAPACITY METRICS

GPU UTILIZATION В ОДИНОЧКУ НЕ ГОВОРИТ, СКОЛЬКО ПОЛЕЗНОЙ РАБОТЫ МЫ ДЕЛАЕМ

TPS

Tokens / second

Overall generated token throughput or per-request decode rate.

TTFT

Time To First Token

Queue + prefill + scheduling until first streamed token.

TPOT

Time Per Output Token

Average decode interval after first token.

ITL

Inter-Token Latency

Spacing/jitter between streamed tokens.

Q

Queue Delay

How long accepted request waits before execution.

OOM

Memory failures

Should approach zero under correct admission.

KV%

KV occupancy

Memory used/reserved for active sequence state.

UTIL

Accelerator utilization

Helpful alongside useful throughput and SLOs, not alone.

17. TTFT BREAKDOWN

ПЕРВЫЙ TOKEN ОПОЗДАЛ — ГДЕ ИМЕННО?

TTFT =
    request validation
  + admission decision
  + queue wait
  + batch scheduling delay
  + prefill compute
  + first decode step
  + transport/stream flush

Example:

  validation        5 ms
  queue          1200 ms   ← bottleneck
  prefill         420 ms
  first decode      35 ms
  transport         15 ms
  -----------------------
  TTFT           1675 ms

"model is slow" would be the wrong diagnosis.
Breakdown by phase is mandatory before tuning kernels, buying more GPU or changing model size.
18. THROUGHPUT VS USER LATENCY

ОДНА И ТА ЖЕ GPU МОЖЕТ БЫТЬ НАСТРОЕНА КАК «МНОГО ДЕШЁВЫХ TOKENS» ИЛИ КАК «БЫСТРЫЙ INTERACTIVE RESPONSE»

THROUGHPUT MODE

Batch/offline

Large active batches, more queue tolerance, maximize aggregate tokens/sec and cost efficiency.

LATENCY MODE

Interactive/realtime

Small queue, reserved headroom, bounded batch delay, priority scheduling, lower TTFT/TPOT.

Один deployment может поддерживать workload classes, но часто проще иметь separate serving pools for interactive and batch traffic.
19. HEAD-OF-LINE BLOCKING

ОДИН ОГРОМНЫЙ PROMPT НЕ ДОЛЖЕН ЗАДЕРЖАТЬ ДЕСЯТКИ МАЛЕНЬКИХ REQUESTS

PROBLEM

Long prefill dominates

One huge context can monopolize compute/memory and increase TTFT for short requests.

CLASSIFY

Separate workload classes

Long-context/batch requests can use different queues/pools/priorities.

CHUNK / PREEMPT

Runtime-dependent

Some serving schedulers can interleave/chunk prefill or preempt lower-priority sequences.

A fair serving policy cares about request size, not only arrival order.
20. PREEMPTION

ИНогда НИЗКОПРИОРИТЕТНЫЙ REQUEST НУЖНО ВРЕМЕННО ОСТАНОВИТЬ, ЧТОБЫ НЕ СОРВАТЬ SLO

WHEN

High-priority arrival

Interactive/realtime work enters saturated server.

HOW

Runtime-specific

Pause sequence, evict/recompute KV, or reserve capacity beforehand.

COST

Recompute / complexity

Preemption can destroy efficiency and should be measured, not assumed free.

Capacity reservation is often simpler than aggressive preemption for predictable priority classes.
21. REPLICAS

MODEL REPLICA УВЕЛИЧИВАЕТ REQUEST CAPACITY, НО ДУБЛИРУЕТ MODEL WEIGHTS

ONE REPLICA

Simple

Good baseline; one failure domain and limited concurrency.

N REPLICAS

Horizontal serving

Each replica loads model and handles independent request batches.

LOAD BALANCING

Capacity-aware

Route by queue/KV/headroom rather than naive round-robin where possible.

FAILURE ISOLATION

Replica crash

Gateway/router stops new traffic to unhealthy replica and retries/fallbacks upstream.

Horizontal replicas are different from model parallelism: replicas increase independent request capacity; model parallelism splits one model execution across devices.
22. MODEL PARALLELISM

ЕСЛИ ОДНА MODEL НЕ ПОМЕЩАЕТСЯ ИЛИ НЕ ДАЁТ НУЖНОЙ SPEED НА ОДНОМ DEVICE

TENSOR PARALLEL

Split layer math

One layer computation is partitioned across multiple accelerators; requires fast interconnect and synchronization.

PIPELINE PARALLEL

Split layers/stages

Different layer groups live on different devices; introduces pipeline scheduling/bubbles.

OTHER SHARDING

Runtime-specific

Expert/model/sequence parallel strategies exist; choose only when model/scale demands it.

Distributed inference introduces network/interconnect as a performance and failure domain. Don't split a model over 8 devices just because the framework can.
23. DATA PARALLEL SERVING

ДЛЯ THROUGHPUT ЧАСТО ПРОЩЕ СДЕЛАТЬ НЕСКОЛЬКО НЕЗАВИСИМЫХ REPLICAS

MODEL REPLICA A

Full model copy; handles batch A.

LOAD BALANCER / №65

Select healthy replica with capacity/headroom.

MODEL REPLICA B

Full model copy; handles batch B.

If model fits one accelerator/node, horizontal replicas are usually operationally simpler than cross-node model parallelism.
24. AUTOSCALING

SCALING SIGNAL ДОЛЖЕН ОТРАЖАТЬ WORKLOAD, А НЕ ТОЛЬКО CPU%

QUEUE DELAY

User pain

Persistent rising queue time means demand exceeds ready capacity.

KV PRESSURE

Memory saturation

Active context load may saturate before compute utilization does.

TOKENS / SEC

Useful throughput

Compare delivered work to capacity profile.

SLO

TTFT/TPOT miss rate

Scale based on user-facing service target.

Scale-out should consider cold-start/load time. If replica takes minutes to become ready, queue spike is a lagging signal.
25. CAPACITY PLANNING

БЕНЧМАРК НУЖНО ДЕЛАТЬ НА РЕАЛЬНОМ PROMPT / OUTPUT DISTRIBUTION

Workload dimensionWhy it matters
Input token distributionDetermines prefill compute and initial KV footprint.
Output token distributionDetermines decode duration and active sequence lifetime.
Concurrency arrival patternSteady 10 RPS differs from bursts of 100 every minute.
Streaming vs non-streamingChanges SLO and connection/runtime behavior.
Sampling / structured output / toolsCan alter decode behavior and termination length.
Priority classesInteractive/batch mix changes scheduler target.
A benchmark with 128-token prompts and 128-token outputs is meaningless if production sends 40k-token prompts and 4k-token answers.
26. CAPACITY ENVELOPE

ОДИН ENDPOINT ИМЕЕТ НЕ ОДНО ЧИСЛО «RPS», А МНОГОМЕРНУЮ ОБЛАСТЬ РАБОТЫ

CAPACITY PROFILE: model-X / revision-R / precision-P

interactive profile:
  p50 input = 2k tokens
  p95 input = 8k
  p50 output = 500
  p95 output = 2k

safe envelope:
  concurrency <= ...
  aggregate active tokens <= ...
  KV occupancy <= ...
  queue delay p95 <= ...
  TTFT p95 <= ...
  TPOT p95 <= ...
  OOM = 0
  overload rejection <= target

IF workload shifts to:
  32k input
  8k output

the old "RPS capacity"
is no longer valid.
Store capacity benchmark metadata alongside exact model/runtime revision so №65 can make capacity-aware routing decisions.
27. MODEL REVISION & RUNTIME ARTIFACT

«MODEL NAME» НЕДОСТАТОЧНО ДЛЯ REPRODUCIBLE SERVING

{
  "deployment_ref": "serve://model-x/prod-eu/g17",
  "model_ref": "model://model-x",
  "model_revision": "sha256:...",
  "tokenizer_revision": "sha256:...",
  "precision": "BF16",
  "runtime_version": "serve-runtime-v...",
  "max_context": 131072,
  "parallelism": {
    "tensor": 1,
    "pipeline": 1
  },
  "scheduler_policy": "interactive-v3",
  "status": "READY",
  "capacity_profile_ref": "capacity://..."
}
PIN WHAT CHANGES BEHAVIOR

Deployment identity

  • weights revision;
  • tokenizer revision;
  • precision/quantization artifact;
  • runtime/container version;
  • context/rope config where applicable;
  • parallelism topology;
  • scheduler/batch config;
  • device/accelerator class.
28. HEALTH PROBES

LIVENESS И READINESS — РАЗНЫЕ QUESTIONS

LIVENESS

Should process be restarted?

Runtime process/event loop is alive and not irrecoverably wedged.

READINESS

Can receive traffic?

Exact model loaded, scheduler healthy, enough resources, not draining.

DEEP HEALTH

Can actually infer?

Optional low-frequency synthetic inference checks model/runtime path end-to-end.

A process can be live but unready during model load, warmup, drain or memory pressure.
29. OVERLOAD CONTRACT

OVERLOAD — НОРМАЛЬНОЕ СОСТОЯНИЕ, ДЛЯ НЕГО НУЖЕН ЯВНЫЙ RESPONSE

REJECT

Fail fast

Return typed OVERLOADED / retry-after estimate before consuming model resources.

DEFER

Background class

Queue offline job if waiting remains useful and queue is bounded.

ROUTE AWAY

Gateway fallback

Expose saturation/headroom so №65 can choose another healthy compatible endpoint.

Serving should not turn overload into OOM or 90-second silent queue. Typed capacity failure is easier to recover from.
30. OOM PROTECTION

OUT-OF-MEMORY НУЖНО ПРЕДОТВРАЩАТЬ, А НЕ ЛОВИТЬ КАК ОБЫЧНУЮ EXCEPTION

RESERVE HEADROOM

Don't fill 100%

Account for workspace, fragmentation and transient allocations.

TOKEN BUDGET

Predict KV growth

Admission considers input + reserved max output.

CONCURRENCY CAP

Bound active sequences

Per model/device limit protects memory.

RESTART POLICY

After catastrophic OOM

If runtime state becomes unsafe, drain/restart replica rather than continuing corrupted capacity accounting.

Repeated OOM is a capacity model bug. Alert and fix admission profile, not just auto-restart forever.
31. ROLLING / CANARY DEPLOYMENT

НОВАЯ MODEL/RUNTIME REVISION НЕ ДОЛЖНА СРАЗУ ПОЛУЧАТЬ 100% TRAFFIC

BUILDNew model/runtime artifact.
LOADSeparate replica pool.
WARMRepresentative inference.
CANARYSmall eligible traffic share.
COMPAREQuality + TTFT + TPOT + errors + memory.
PROMOTE / ROLLBACKEvidence-based.
Serving regression can come from runtime/kernel/scheduler change even when model weights are identical. Canary metrics must include both quality and performance.
32. DRAINING

НЕ УБИВАТЬ REPLICA, КОГДА ОНА ЕЩЁ STREAM-ИТ 50 ACTIVE REQUESTS

MARK DRAINING

No new traffic

Readiness false / router removes endpoint from new selections.

FINISH ACTIVE

Grace period

Let accepted generations complete or cancel based on shutdown deadline.

UNLOAD

Release memory

Only after active requests are zero or termination policy reached.

Graceful drain matters for deploys, scale-down and maintenance. Mid-stream termination causes user-visible partial responses and retry ambiguity.
33. SPECULATIVE DECODING

ОПЦИОНАЛЬНАЯ SERVING OPTIMIZATION: ПРЕДЛОЖИТЬ НЕСКОЛЬКО TOKENS БЫСТРЫМ DRAFT-МЕХАНИЗМОМ И ПРОВЕРИТЬ MAIN MODEL

GOAL

Lower decode latency

Accept several verified draft tokens per expensive target-model step when acceptance rate is good.

TRADE-OFF

Extra compute/complexity

Draft model and verification can hurt if workload/model combination has low acceptance or high overhead.

EVAL

Measure end-to-end

Compare TPOT, throughput, memory and quality-equivalence under real workload.

Это advanced serving optimization, не baseline architecture. Включать только после profiling.
34. QUANTIZATION BOUNDARY

№72 ВЫБИРАЕТ REPRESENTATION/PRECISION; №70 ИЗМЕРЯЕТ, КАК ЭТО СЛУЖИТ В PROD

№72 QUANTIZATION

Model artifact trade-off

FP/BF/INT formats, calibration, compression and quality/performance implications.

№70 SERVING

Runtime behavior

Load time, memory footprint, supported kernels, throughput, TTFT/TPOT, batching and replica capacity of that artifact.

Не обещать «INT4 всегда 4× быстрее». Performance depends on hardware, kernels, batch size, bandwidth and runtime support; №70 benchmarks actual deployment.
35. SELF-HOSTING BOUNDARY

№71 — РЕШЕНИЕ И OPERATIONS TOPOLOGY; №70 — MECHANICS OF INFERENCE

№71 ASKS

Should we host?

Privacy, economics, hardware, operations burden, offline/local requirements, deployment topology.

№70 ASKS

How does endpoint run?

Batching, KV, scheduler, replicas, memory, metrics, SLO, health and capacity.

SHARED AREA

Deployment profile

№71 chooses topology; №70 supplies measured capacity characteristics of that topology.

36. MULTIMODAL SERVING

IMAGE/AUDIO/VIDEO INPUT МОЖЕТ ДОБАВЛЯТЬ ОТДЕЛЬНЫЕ ENCODER И MEMORY BOTTLENECKS

PREPROCESS

Decode/resize/tokenize

Media preprocessing may occur before model runtime.

ENCODER

Vision/audio compute

Additional model stage can dominate prefill-like latency.

TOKEN / FEATURE EXPANSION

Context pressure

Images/video frames can expand into many internal tokens/features.

BATCH COMPATIBILITY

Variable media shapes

Mixed modalities complicate batching and memory planning.

№73 owns multimodal reasoning/orchestration. №70 only covers serving consequences of multimodal model inputs.
37. REALTIME SERVING

VOICE/REALTIME WORKLOAD ДЕЛАЕТ JITTER И INTER-TOKEN/FRAME LATENCY КРИТИЧНЕЕ AVERAGE THROUGHPUT

LOW JITTER

Stable cadence

Realtime UX suffers from bursty token/audio delivery even if average throughput looks good.

RESERVED CAPACITY

No long queue

Realtime sessions may need dedicated pool or priority class.

INTERRUPTION

Fast cancel

Barge-in requires immediate stop/release of current generation.

№75 owns realtime session architecture. Serving provides low-latency model execution as one component of that session.
38. SECURITY & ISOLATION

SERVING ENDPOINT НЕ ДОЛЖЕН ПРЕВРАЩАТЬСЯ В ОБХОД MODEL GATEWAY

PRIVATE NETWORK

Internal endpoint

Serving endpoint usually should not be directly exposed to untrusted clients.

GATEWAY AUTH

Trusted caller

№65/host authenticates and normalizes request before serving.

TENANT METADATA

Scheduling/metrics

Tenant/workload class comes from trusted context, not model/user spoofing.

NO RAW LOGGING

Data minimization

Serving metrics should not dump prompts/responses by default.

Direct debug endpoint access should be separately authenticated, audited and restricted; it may bypass product-level policy by design.
39. OBSERVABILITY

MODEL SERVING НУЖНО НАБЛЮДАТЬ КАК QUEUE + SCHEDULER + MEMORY SYSTEM + MODEL RUNTIME

TTFT

Time To First Token

p50/p95/p99 by model, workload class and prompt length bucket.

TPOT

Time Per Output Token

Decode performance after first token.

TPS

Tokens / Second

Total serving throughput and per-request rate.

Q

Queue Delay

Admission-to-schedule waiting time.

KV

KV Utilization

Allocated/free blocks, fragmentation, active-token footprint.

GPU

Accelerator Utilization

Compute/memory utilization correlated with useful throughput.

OOM

OOM / Allocation Failures

Target near zero; indicates bad admission/capacity profile.

REJ

Overload Reject Rate

Typed capacity rejects vs accepted traffic/SLO.

Всегда segment by prompt length, output length, workload class and model revision; aggregated p95 can hide a completely broken long-context class.
40. PERFORMANCE TESTING

НУЖНО ДВА ТИПА TESTS: SINGLE-REQUEST И SATURATION

BASELINE

1 request

Measure raw prefill/decode latency without queueing.

CONCURRENCY RAMP

1 → N clients

Find throughput plateau and where TTFT/queue starts exploding.

MIXED LENGTH

Real distributions

Short/long prompts and outputs together reveal scheduler fairness.

SOAK

Hours

Detect memory leaks, fragmentation, KV cleanup bugs and performance drift.

OOM EDGE

Capacity boundary

Verify admission rejects before memory exhaustion.

CANCEL

Aborted streams

Resources are released promptly under mass cancellation.

RESTART

Replica failure

Router stops traffic; pool recovers without thundering herd.

CANARY

Regression

Compare new runtime/model artifact on same workload.

41. FAILURE MODES

КАК INFERENCE SERVER МОЖЕТ БЫТЬ «100% GPU UTILIZED» И ПРИ ЭТОМ ПЛОХИМ PRODUCTION SERVICE

MAX GPU UTIL = GOAL
Large batches maximize hardware use but destroy TTFT/SLO.
OPTIMIZE SLO + COST
UNBOUNDED QUEUE
Overload becomes minutes of latency and then OOM.
ADMISSION / BACKPRESSURE
WEIGHTS FIT = CAPACITY
KV cache from concurrent long contexts exhausts memory.
ACTIVE TOKEN BUDGET
HEALTH = HTTP 200
Traffic hits replica before model is loaded/warmed.
READINESS AFTER INFERENCE PATH
ROUND ROBIN ONLY
One replica has huge queue/KV load while another is idle.
CAPACITY-AWARE ROUTING
NO CANCEL
Disconnected users leave zombie generations consuming GPU.
CANCELLATION PROPAGATION
AVERAGE LATENCY
p95/p99 and workload segments hide severe tail behavior.
PERCENTILES + LENGTH BUCKETS
NO REVISION PIN
Cannot explain quality/performance changes after deployment.
EXACT DEPLOYMENT IDENTITY
DISTRIBUTE TOO EARLY
Multi-GPU communication overhead before single-device baseline is optimized.
PROFILE FIRST
42. MVP IMPLEMENTATION

ONE MODEL, ONE SERVING RUNTIME, ONE CAPACITY PROFILE — ЭТО УЖЕ ПРАВИЛЬНЫЙ START

model_serving/
├── server.py
├── contracts.py
├── scheduler.py
├── admission.py
├── streaming.py
├── health.py
├── metrics.py
├── deployment.py
└── benchmarks/

InferenceRequest:
  request_id
  model_revision
  input
  max_output_tokens
  sampling
  stream
  deadline
  workload_class

InferenceRuntime:
  validate()
  estimate_memory()
  admit()
  schedule()
  prefill()
  decode()
  stream()
  cancel()
  release()

ServingDeployment:
  exact model/tokenizer revision
  precision
  runtime version
  max context
  queue cap
  active token cap
  concurrency cap
  health state
  capacity profile
80% VALUE MVP

Production serving without a cluster science project

  • One known model artifact/revision.
  • Single inference server endpoint.
  • Bounded request queue.
  • Input/output/context limits.
  • Continuous batching if runtime supports it.
  • KV-memory/active-token accounting.
  • Streaming + cancellation.
  • Readiness/liveness.
  • OOM-safe headroom.
  • TTFT / TPOT / queue / TPS metrics.
  • One realistic benchmark profile.
  • Graceful drain and canary deploy.
  • №65 registration with health/capacity metadata.

Start on one device/node if the model fits. Add replicas before complex cross-device sharding when throughput is the main problem.

43. WHEN TO UPGRADE

УСЛОЖНЯТЬ SERVING ТОЛЬКО ПО ИЗМЕРЕННОМУ BOTTLENECK

Observed bottleneckPotential upgrade
Queue/SLO saturation but model fits one deviceAdd horizontal replicas and capacity-aware load balancing.
Model does not fit one deviceTensor/pipeline/model sharding across accelerators.
Decode latency dominatesProfile kernels/bandwidth, serving scheduler, speculative decoding; later evaluate quantization №72.
Prefill dominatesPrefix reuse, prompt-length classes, chunked/interleaved prefill where runtime supports it.
KV memory limits concurrencyBetter paged/block KV management, lower precision where validated, tighter admission, more memory/replicas.
Cold starts hurt SLOWarm replica floor, artifact locality, pre-scaling.
Multiple local modelsDedicated model pools, load/unload policy, №65 capacity-aware routing.
Realtime workload appearsSeparate low-latency serving class/pool integrated with №75.
44. PRACTICAL DECISION

СТОИТ ЛИ ДЕЛАТЬ ОТДЕЛЬНЫЙ КОМПОНЕНТ?

ВопросОтвет
Стоит ли реализовывать?Да, если вы реально владеете inference runtime. Если только вызываете hosted API — provider уже реализует большую часть №70.
Separate Component?YES. Inference server/serving pool is distinct production component behind №65 Gateway.
Минимум 80% ценности?One serving endpoint, bounded admission, batching, KV accounting, streaming/cancel, health, exact revision, realistic SLO metrics, safe rollout.
Когда overkill?Distributed tensor/pipeline parallel cluster for a model that easily fits one accelerator and has low traffic.
Trigger?Need self/private model endpoint, predictable latency/cost, local model capacity, or direct control over batching/memory/throughput.
Как измерить uplift?TTFT/TPOT, tokens/sec, queue delay, SLO success, accelerator utilization, OOM rate, cost per generated token/successful request, capacity headroom.
Можно ли rule/tool/code вместо LLM-agent?Да, полностью. Serving scheduler/admission/health are deterministic systems. LLM itself is the workload, not the serving controller.
45. DESIGN RULES

ПРАВИЛА ДЛЯ РЕАЛЬНОЙ СИСТЕМЫ

RULE 01

Measure prefill and decode separately

They have different bottlenecks and workload sensitivity.

RULE 02

KV is capacity

Concurrency is bounded by active sequence memory, not only model weights.

RULE 03

Bound the queue

Overload should reject/defer before latency and memory collapse.

RULE 04

Batch for SLO, not benchmark vanity

Throughput and interactive latency must be balanced explicitly.

RULE 05

Cancellation frees compute

Stop generation when output is no longer needed.

RULE 06

Readiness means model ready

Do not route traffic before load/warmup completes.

RULE 07

Pin deployment identity

Weights, tokenizer, precision and runtime version belong in trace/provenance.

RULE 08

Benchmark real distributions

Prompt/output length and concurrency mix determine actual capacity.

RULE 09

Scale by bottleneck

Replicate, shard or optimize only after profiling shows what limits SLO/cost.

46. FINAL MAP

MODEL SERVING TURNS WEIGHTS INTO A CAPACITY-MANAGED PRODUCTION ENDPOINT

№65 MODEL GATEWAY / ROUTER
        ↓
SELECT SERVING DEPLOYMENT
  model revision
  region
  capability
  health
  capacity
        ↓
INFERENCE REQUEST
  input
  max output
  stream
  deadline
  workload class
        ↓
SERVING ENDPOINT
        ↓
VALIDATE
  model loaded?
  context valid?
  modality supported?
        ↓
ADMISSION
  queue capacity
  active token budget
  KV memory
  concurrency
  deadline feasibility
        ↓
SCHEDULER
  FIFO / priority / fair share
        ↓
PREFILL
  process input tokens
  build KV state
        ↓
CONTINUOUS BATCH
  active sequences enter/leave
        ↓
DECODE
  token by token
        ↓
STREAM
  TTFT
  inter-token latency
        ↓
FINISH / CANCEL
        ↓
FREE KV / CAPACITY
        ↓
REPORT
  tokens
  queue delay
  prefill
  decode
  TTFT
  TPOT
  replica
  model revision

CAPACITY MODEL:

accelerator memory
  =
  weights
  + KV cache
  + runtime/workspace
  + safety headroom

OVERLOAD:
  do not accept unlimited work
        ↓
  reject / defer / route away

SCALING:

if model fits one device:
  add replicas first for throughput

if model does not fit:
  model parallelism

if decode is slow:
  profile scheduler / bandwidth /
  kernels / precision / speculative options

if prefill is slow:
  inspect prompt length /
  prefix reuse / prefill scheduling

if KV is full:
  tighten admission /
  better KV management /
  more memory or replicas

BOUNDARIES:

№65
  WHICH model endpoint should receive the call?

№70
  HOW does that endpoint execute the call efficiently?

№71
  SHOULD / HOW do we operate models ourselves?

№72
  IN WHAT precision/compressed form is model represented?

CORE METRICS:

TTFT
  time until first token

TPOT / ITL
  speed and smoothness of decode

TPS
  useful token throughput

QUEUE DELAY
  overload signal

KV OCCUPANCY
  active memory pressure

OOM
  admission correctness failure

SLO SUCCESS
  real product target

CORE PRINCIPLE:

MODEL SERVING
IS NOT "THE GPU IS BUSY."

A GOOD SERVING SYSTEM
KEEPS THE MODEL
BUSY ENOUGH
WITHOUT DESTROYING
LATENCY, FAIRNESS,
MEMORY SAFETY
OR RELIABILITY.

THE GOAL IS NOT
MAXIMUM UTILIZATION.

THE GOAL IS
PREDICTABLE,
MEASURED,
COST-EFFICIENT
INFERENCE
UNDER REAL WORKLOAD.

ECC RETROFIT / PRACTICAL HARNESS INTEGRATION

A. Related ECC ideas. Context-as-cache, scoped memory, lifecycle hooks, selective capabilities, feature flags, deterministic enforcement, provider-neutral adapters and eval-gated learning are applied only where relevant to №70 Model Serving & Inference.

B–E. Existing boundary and placement. The existing conceptual boundary, class PRODUCTION, default CONDITIONAL and owner Model Infrastructure + Model Router + Production Fabric remain authoritative. Runtime/control/data/offline placement is unchanged; durable state stays outside model context.

F–H. Hooks and contracts. Use bounded PRE_MODEL/POST_MODEL, PRE_TOOL/POST_TOOL, CHECKPOINT and TASK_COMPLETED events as applicable. Illustrative fields and canonical contracts are defined in NEW_CONTRACTS_SPEC.md; no universal schema is implied.

I–J. Security and evaluation. Host-side schema, permission, secret, budget, idempotency and audit checks take precedence over LLM output. Optional mechanisms require a feature flag and WITH/WITHOUT ablation; measure quality, acceptance, correction, latency, cost, escalations and severe errors.

K–L. Task profiles and cross-references. A TaskProfile selects the relevant skill, tool/context slice, memory scope and enforcement profile independently from FAST/STANDARD/DEEP. See cross-reference map, hook spec and ablation plan. Provider adapters remain outside the core.