~/apah / spec.md
README.md spec.md setup.md apah_doc.tex github.com

spec.md▋

// technical & mathematical specification

Formal mathematical foundations and system design of the Apah LLM Inference Runtime — customized for sovereign, 100% air-gapped industrial AI workbenches.

architecture

system_topology

Apah is a low-latency, high-throughput custom LLM inference engine designed from first principles to execute on NVIDIA GPUs with zero network egress:

  +-------------------------------------------------------+
  |    Sovereign Workbench Agentic Applications           |
  +-------------------------------------------------------+
                      |
     +----------------+----------------+
     |                                 |
OpenAI-Compatible API                 Ollama Native Shim
(/v1/chat/completions)               (/api/chat, /api/embeddings)
     |                                 |
     +----------------+----------------+
                      |
           +----------------------+
           |  FastAPI Server Layer|
           +----------------------+
                      |
              +-------+-------+
              |               |
   +--------------------+   +-------------------+
   | Network & Airgap   |   | Tamper-Evident    |
   | Guard (Socket Patch|   | Audit Logger      |
   +--------------------+   +-------------------+
                      |
     +----------------+----------------+
     | Continuous Batching Scheduler   |
     +---------------------------------+
                      |
     +----------------+----------------+
     |  PagedAttention Memory Engine   |
     |  (BlockPool & BlockTable)       |
     +---------------------------------+
                      |
     +----------------+----------------+
     |  Tensor Parallel Execution      |
     |  (NCCL AllReduce / Column-Row)  |
     +---------------------------------+
                      |
     +----------------+----------------+
     |  CUDA Hardware Execution Engine |
     +---------------------------------+
          

math_formalisms

core_algorithms

2.1 Autoregressive Decoding & KV-Cache Formulation

Let X = (x_1, x_2, ..., x_n) represent an input sequence of tokens. The autoregressive probability of generating sequence X is factorized as:

Sequence Probability Factorization
P(X) = ∏_{t=1}^n P(x_t | x_1, x_2, ..., x_{t-1})

At generation step t, temperature-scaled Softmax over vocabulary size V produces next-token probabilities:

Softmax Logits Probability
P(x_t = k | x_{<t}) = exp(z_{t,k} / τ) / ∑_{j=1}^{V} exp(z_{t,j} / τ)

Key and Value activation matrices are appended per step without re-computing past tokens:

KV Cache Concatenation & Attention
K_{≤t} = [K_{<t} ‖ K_t], V_{≤t} = [V_{<t} ‖ V_t] Attention(Q_t, K_{≤t}, V_{≤t}) = Softmax( Q_t · K_{≤t}^T / √d_k ) · V_{≤t}

2.2 PagedAttention Memory Management Engine

Traditional padded allocation requires contiguous VRAM M_{padded} = 2 · B · L · H_{kv} · L_{max} · D · b_{bytes}, leading to memory waste:

Paged VRAM Footprint & Waste Bound
M_{paged} = 2 · L · H_{kv} · D · b_{bytes} · B_{block} · ∑_{i=1}^B ⌈ L_i / B_{block} ⌉ Waste Bound: W_{paged} < B_{block} / L_i ≤ 16 / 128 = 12.5%

Block table lookup maps logical token index i to physical block pool coordinates:

Physical Block Index & Offset Mapping
P(s, i) = BlockTable[s][ ⌊ i / B_{block} ⌋ ] O(i) = i mod B_{block} K_{physical}(s, i) = BlockPool[ P(s, i), O(i), layer_idx ]

2.3 Multi-GPU Tensor Parallelism (Megatron-LM Style)

Column-parallel layers split projection weights along columns across N GPUs: W = [W_1, W_2, ..., W_N]. Each GPU computes Y_i = X · W_i independently.

Row-parallel layers split weights along rows: W = [W_1; W_2; ...; W_N]. Results are aggregated via an NCCL AllReduce-Sum:

Row-Parallel AllReduce-Sum
Y = AllReduce-Sum( ∑_{i=1}^N X_i · W_i )

2.4 Tamper-Evident Cryptographic SHA256 Hash Chain

Every inference request and system event is linked to previous hashes in a tamper-evident audit log:

Hash Chain Formula (k ≥ 1)
H_0 = "0000000000000000000000000000000000000000000000000000000000000000" H_k = SHA256( H_{k-1} ‖ ":" ‖ CanonicalJSON(E_k) )

airgap_isolation

network_hardening

  • Socket Interception: When APAH_AIRGAP_MODE=1 is enabled, lower-level socket connections to external non-loopback IP addresses throw AirgapViolationError.
  • Loopback Exception: Connections to 127.0.0.1, localhost, and ::1 remain open for local agent IPC.
  • OS Sandbox: Systemd unit file enforces PrivateNetwork=true, ProtectSystem=strict, and NoNewPrivileges=true.