Crate Architecture

Question answered

How are the workspace crates grouped, and which dependency direction should new code follow?

The workspace has many crates because it separates product policy from runtime mechanism and isolates optional platform/ABI surfaces. The exact Cargo dependency graph is authoritative; this note provides a conceptual map.

Layer map

flowchart TB
    SURFACE[Product surfaces<br/>CLI · server · Python · C]
    FACADE[onnx-genai facade]
    ENGINE[Generation engine]
    POLICY[Metadata · scheduler · KV · preprocess]
    ADAPTER[onnx-genai-ort adapter]
    SESSION[nxrt session / executor]
    GRAPH[IR · loader · shape · optimizer · cost]
    EPAPI[EP API]
    EPS[CPU · CUDA · dynamic plugins]
    FOUND[Memory · tracing · comm · ABI foundations]

    SURFACE --> FACADE
    SURFACE --> ENGINE
    FACADE --> ENGINE
    ENGINE --> POLICY
    ENGINE --> ADAPTER
    ENGINE --> SESSION
    SESSION --> GRAPH
    SESSION --> EPAPI
    EPAPI --> EPS
    SESSION --> FOUND
    EPS --> FOUND

GenAI layer

Facade and public surfaces

CrateRole
onnx-genaiSmall public facade; re-exports engine, KV, metadata, ORT and preprocessing APIs
onnx-genai-cliUnified CLI and interactive REPL
onnx-genai-serverOpenAI-compatible HTTP/SSE server, sessions, metrics and admin/debug routes
onnx-genai-pythonPython-facing GenAI API
onnx-genai-capiC-facing GenAI API
onnx-genai-routerRouting/model-selection support

Generation policy and state

CrateRole
onnx-genai-engineMain orchestrator: load, generate, decode loop, sampling, speculative decoding, pipelines, backend selection
onnx-genai-schedulerAdmission, priorities, batching, byte budgets, pressure and preemption decisions
onnx-genai-kvKV pages, page tables, prefix cache, fork/rewind, tiered backing and telemetry
onnx-genai-metadataInference metadata standard structures and validation
onnx-genai-runtime-configRuntime configuration model
onnx-genai-genai-configGenAI configuration compatibility
onnx-genai-preprocessImage/audio and model-declared preprocessing
onnx-genai-ortONNX Runtime integration used by the generation engine

The generation engine is intentionally the joining layer: it understands generation semantics and can consume either ORT sessions or native nxrt sessions.

Native runtime layer

Graph and model representation

CrateRole
onnx-runtime-irTyped graph, nodes, values, shapes, layouts and device annotations
onnx-runtime-loaderONNX/model loading and external-data handling
onnx-model-packageModel-package format and package operations
onnx-runtime-shape-inferenceShape inference
onnx-runtime-optimizerGraph optimization passes
onnx-runtime-quantizationRuntime quantization contracts and support
onnx-runtime-cost-modelExplicit cost inputs for placement/selection
onnx-runtime-operator-selectionOperator/kernel selection support

Execution

CrateRole
onnx-runtime-sessionNative session construction, planning, executor, tensor ownership and heterogeneous execution
onnx-runtime-eagerEager execution surface
onnx-runtime-ep-apiNative execution-provider and kernel contracts
onnx-runtime-ep-cpuCPU EP and kernels
onnx-runtime-ep-cudaCUDA EP, kernels, graph capture and device execution
onnx-runtime-ep-pluginExport/bridge support for ORT-style plugin EPs
onnx-runtime-ep-nxrt-abiNative nxrt dynamic EP ABI definitions
onnx-runtime-ep-nxrt-hostHost side of the native EP ABI

Foundations and interoperability

Crate familyRole
onnx-runtime-memory*Memory planning, virtual memory, CUDA memory and governance
onnx-runtime-tracerRuntime tracing and Perfetto-compatible events
onnx-runtime-commDistributed communication and buffer ownership
onnx-runtime-capi / python / dlpackNative runtime language and tensor interoperability
onnx-runtime-protocol-traceProtocol conformance traces
onnx-runtime-cpuinfoCPU capability discovery
mlas-sysVendored MLAS bindings used by CPU paths

Dependency rules of thumb

Prefer downward dependencies

A mechanism crate should not depend on a product-policy crate. For example, allocator primitives should not depend on the generation engine, and graph IR should not know about HTTP requests.

  1. Public surfaces may depend on engine APIs, not engine internals.
  2. The engine may join GenAI policy and backend mechanisms.
  3. Scheduler and KV crates should remain independently testable.
  4. EP APIs should not depend on a concrete CPU/CUDA provider.
  5. Foundational memory/ABI types should not depend on session or engine policy.
  6. Optional platform/plugin crates should not force their dependencies into the default portable path.

Why both onnx-genai-* and onnx-runtime-* exist

The native runtime is useful beyond autoregressive generation: it loads and executes ONNX graphs. The GenAI layer adds stateful token-generation semantics, KV lifecycle, scheduling, prompt processing and serving.

Keeping the boundary explicit allows:

  • GenAI policy to compare ORT and native execution;
  • nxrt to evolve as a general runtime;
  • EPs and kernels to be reused without depending on prompt/session policy;
  • memory and ABI contracts to be tested below the product layer.