# lmcache/lmcache-vllm architecture

> Architecture diagram of the GitHub repository lmcache/lmcache-vllm, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/lmcache/lmcache-vllm
- Repository: https://github.com/lmcache/lmcache-vllm

## Overview

##### Project type and purpose
- This is a Python “driver” library that integrates LMCache core (an external caching layer) with vLLM (a high‑performance LLM inference engine).
- Its purpose is to hook into vLLM’s token-generation pipeline, look up or store KV‑cache entries in LMCache to avoid recomputing attention states, and expose a CLI entrypoint for demo or production use.

##### File structure
- Top level: setup.py (packaging), README.md, TODO, LICENSE, .gitignore
- docker/ : Dockerfile + build scripts + requirement files for common, build, CUDA setups, plus example YAML and chat template
- lmcache_vllm/ : core package
- attention/: flash_attn integration
- models/: llama model loader
- utils/: positional encoding, other helpers
- blend_adapter.py, vllm_adapter.py: adapter pattern implementations
- vllm_injection.py: dependency‑injection hooks into vLLM’s runtime
- experimental/: feature‐flagged adapters/injection variants
- lmcache_utils.py: utilities to talk to LMCache core
- script.py: CLI entrypoint

##### README
- “The driver for LMCache core to run in vLLM” – confirms this is an integration/adapter library, not a full application.

##### Main components and interactions
a. Components
1. Client/Application (script.py or user code)
2. Adapter Layer
– vllm_adapter (wraps vLLM API)
– vllm_injection (inserts hooks into vLLM’s internals)
– blend_adapter & experimental modules (optional extensions)
3. vLLM Engine
– Llama model loader (lmcache_vllm/models/llama.py)
– Attention kernels (flash_attn)
– Token‐generation pipeline
4. LMCache Core
– External KV‑cache service/library accessed via lmcache_utils
5. Docker Container (deployment unit grouping all above)
b. Relationships & Flows
1. Client → Adapter Layer: invokes generate()
2. Adapter Layer → vLLM Engine: forward prompts
3. vLLM Engine (before each token step) → Adapter hooks → Query LMCache
- Hit: retrieve cached KV and skip recompute
- Miss: compute attention & KV, then Store in LMCache
4. vLLM Engine → returns tokens → Adapter Layer → Client
5. Docker: bundles the Python package, dependencies, CUDA/runtime
c. Architectural patterns
– Adapter pattern for wrapping vLLM calls
– Dependency injection to insert hooks without modifying vLLM core
– Modular separation (attention, models, utils, experimental)
d. Key technologies
– Python 3, pip packaging (setup.py)
– vLLM inference library
– LMCache core (external)
– flash_attn (CUDA‑accelerated attention)
– Docker for reproducible builds & GPU support

##### Essential elements—don’t overcomplicate
- Break out at most five to six logical components.
- Both the normal inference path and the cache short-circuit on a hit.
- Files/modules under each component to make it clear where functionality lives.
- Docker boundary to represent deployment packaging.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Docker Container Boundary
    subgraph "Docker Container"
        direction TB
        Client["Client/Application\n(lmcache_vllm/script.py)"]:::software
        subgraph "Adapter Layer"
            direction TB
            VLLMAdapter["vllm_adapter.py"]:::software
            VLLMInjection["vllm_injection.py"]:::software
            BlendAdapter["blend_adapter.py"]:::software
            ExperimentalAdapter["experimental/vllm_adapter.py"]:::software
            ExperimentalInjection["experimental/vllm_injection.py"]:::software
        end
        subgraph "vLLM Engine"
            direction TB
            LlamaModel["models/llama.py"]:::software
            FlashAttn["attention/flash_attn.py"]:::software
            TokenPipeline["Token‑generation Pipeline"]:::software
        end
        LMCacheUtils["lmcache_utils.py"]:::util
    end

    %% External LMCache Core
    LMCacheCore(("LMCache Core")):::external

    %% Deployment & Packaging
    subgraph "Packaging & Docker Assets"
        direction TB
        Dockerfile["docker/Dockerfile"]:::util
        BuildScript["docker/build_docker.sh"]:::util
        ReqCommon["docker/requirements-common.txt"]:::util
        ReqCUDA["docker/requirements-cuda.txt"]:::util
        ReqBuild["docker/requirements-build.txt"]:::util
        ExampleYAML["docker/example.yaml"]:::util
        ChatTemplate["docker/chat-template.txt"]:::util
        Setup["setup.py"]:::util
        README["README.md"]:::util
        License["LICENSE"]:::util
        TODO["TODO"]:::util
        Gitignore[".gitignore"]:::util
    end

    %% Relationships
    Client -->|"generate()"| VLLMAdapter
    VLLMAdapter -->|"forward prompts"| LlamaModel
    LlamaModel --> FlashAttn
    FlashAttn --> TokenPipeline
    TokenPipeline -->|"vllm_injection.hook_kv_cache()"| VLLMInjection
    VLLMInjection -->|"query()/store()"| LMCacheUtils
    LMCacheUtils -->|"KV operations"| LMCacheCore
    LMCacheCore -->|"hit/miss response"| TokenPipeline
    TokenPipeline -->|"tokens" | VLLMAdapter
    VLLMAdapter -->|"return tokens"| Client

    %% Click Events
    click Client "https://github.com/lmcache/lmcache-vllm/blob/dev/lmcache_vllm/script.py"
    click VLLMAdapter "https://github.com/lmcache/lmcache-vllm/blob/dev/lmcache_vllm/vllm_adapter.py"
    click VLLMInjection "https://github.com/lmcache/lmcache-vllm/blob/dev/lmcache_vllm/vllm_injection.py"
    click BlendAdapter "https://github.com/lmcache/lmcache-vllm/blob/dev/lmcache_vllm/blend_adapter.py"
    click ExperimentalAdapter "https://github.com/lmcache/lmcache-vllm/blob/dev/lmcache_vllm/experimental/vllm_adapter.py"
    click ExperimentalInjection "https://github.com/lmcache/lmcache-vllm/blob/dev/lmcache_vllm/experimental/vllm_injection.py"
    click LlamaModel "https://github.com/lmcache/lmcache-vllm/blob/dev/lmcache_vllm/models/llama.py"
    click FlashAttn "https://github.com/lmcache/lmcache-vllm/blob/dev/lmcache_vllm/attention/flash_attn.py"
    click LMCacheUtils "https://github.com/lmcache/lmcache-vllm/blob/dev/lmcache_vllm/lmcache_utils.py"
    click Dockerfile "https://github.com/lmcache/lmcache-vllm/tree/dev/docker/Dockerfile"
    click BuildScript "https://github.com/lmcache/lmcache-vllm/blob/dev/docker/build_docker.sh"
    click ReqCommon "https://github.com/lmcache/lmcache-vllm/blob/dev/docker/requirements-common.txt"
    click ReqCUDA "https://github.com/lmcache/lmcache-vllm/blob/dev/docker/requirements-cuda.txt"
    click ReqBuild "https://github.com/lmcache/lmcache-vllm/blob/dev/docker/requirements-build.txt"
    click ExampleYAML "https://github.com/lmcache/lmcache-vllm/blob/dev/docker/example.yaml"
    click ChatTemplate "https://github.com/lmcache/lmcache-vllm/blob/dev/docker/chat-template.txt"
    click Setup "https://github.com/lmcache/lmcache-vllm/blob/dev/setup.py"
    click README "https://github.com/lmcache/lmcache-vllm/blob/dev/README.md"
    click License "https://github.com/lmcache/lmcache-vllm/tree/dev/LICENSE"
    click TODO "https://github.com/lmcache/lmcache-vllm/tree/dev/TODO"
    click Gitignore "https://github.com/lmcache/lmcache-vllm/blob/dev/.gitignore"

    %% Styles
    classDef software fill:#BBDEFB,stroke:#2196F3
    classDef util fill:#C8E6C9,stroke:#388E3C
    classDef external fill:#C5CAE9,stroke:#3F51B5,stroke-dasharray: 5 5
    classDef container fill:#FFE0B2,stroke:#FB8C00,stroke-width:2px
```
