Making Illegal Inference States Unrepresentable in OCaml

Talk

SPEAKERS

S
Senthilnathan

ABSTRACT

LM inference runtimes have a surprising amount of state.

Requests move through waiting, scheduled, running, paused and finished states. KV-cache blocks are allocated, mapped, reused and eventually released. A lot of runtime correctness comes down to making sure these transitions happen in the right order.

I wanted to explore whether some of these invariants can be expressed directly in OCaml's type system instead of being enforced through runtime checks.

In this talk, I'll build a small model of an LLM inference scheduler in OCaml and use variants, modules and type-driven design to represent request and KV-cache lifecycles. We'll look at which invalid states can be made difficult or impossible to represent, where the approach starts becoming cumbersome, and what this means for systems where state-management bugs are expensive.

This is not an attempt to build a production inference engine in OCaml. It is an experiment in applying OCaml's type system to a systems problem usually implemented in languages such as Python, C++ or Rust.

I'll also compare the resulting design with patterns I've encountered while working on a small inference runtime in Rust and contributing to compiler projects.