Skip to main content

PegaFlow Python Package

High-performance key-value storage engine with Python bindings, built with Rust and PyO3.

Features

  • PegaEngine: Fast Rust-based key-value storage with Python bindings
  • PegaKVConnector: vLLM KV connector for distributed inference with KV cache transfer

Installation

From Source

# Install maturin if you haven't already
pip install maturin

# Build and install in development mode
cd python
maturin develop

# Or build a wheel
maturin build --release

From PyPI (coming soon)

pip install pegaflow

Usage

Basic KV Storage

from pegaflow import PegaEngine

# Create a new engine
engine = PegaEngine()

# Store key-value pairs
engine.put("name", "PegaFlow")
engine.put("version", "0.1.0")

# Retrieve values
name = engine.get("name")  # Returns "PegaFlow"
missing = engine.get("nonexistent")  # Returns None

# Remove keys
removed = engine.remove("name")  # Returns "PegaFlow"

vLLM KV Connector

from vllm import LLM
from vllm.distributed.kv_transfer.kv_transfer_agent import KVTransferConfig

# Configure vLLM to use PegaKVConnector
kv_transfer_config = KVTransferConfig(
    kv_connector="PegaKVConnector",
    kv_role="kv_both",
    kv_connector_module_path="pegaflow.connector",
)

# Create LLM with KV transfer enabled
llm = LLM(
    model="gpt2",
    kv_transfer_config=kv_transfer_config,
)

Connector Modes

PegaKVConnector defaults to read_write: it queries PegaFlow for reusable KV blocks, loads matched blocks into vLLM, and saves newly computed full blocks back to PegaFlow.

Set pegaflow.mode to save_only when another vLLM connector is responsible for reads and PegaFlow should only persist KV blocks for later reuse. This is intended for MultiConnector decode-side setups where an upstream connector owns the external hit/load path, while PegaFlow records the resulting KV cache. In save_only mode, PegaFlow does not query or load KV blocks.

vllm serve Qwen/Qwen3-0.6B \
  --kv-transfer-config '{
    "kv_connector": "MultiConnector",
    "kv_role": "kv_both",
    "kv_connector_extra_config": {
      "connectors": [
        {
          "kv_connector": "<external-read-connector>",
          "kv_role": "kv_both"
        },
        {
          "kv_connector": "PegaKVConnector",
          "kv_role": "kv_both",
          "kv_connector_module_path": "pegaflow.connector",
          "kv_connector_extra_config": {
            "pegaflow.mode": "save_only"
          }
        }
      ]
    }
  }'

Valid values are read_write and save_only.

TP Shards Across Hosts

CUDA IPC is host-local. When one tensor-parallel replica spans multiple hosts, run one PegaFlow server on each host and configure the connector with every server endpoint in global TP-rank order:

{
  "kv_connector": "PegaKVConnector",
  "kv_role": "kv_both",
  "kv_connector_module_path": "pegaflow.connector",
  "kv_connector_extra_config": {
    "pegaflow.tp_shard_endpoints": [
      "http://host-a:50055",
      "http://host-b:50055"
    ]
  }
}

For TP8 and two endpoints, global ranks 0-3 register with the first server and ranks 4-7 register with the second. Each server sees a local TP4 topology and must manage the four GPUs on its own host. Every vLLM process must receive the same ordered endpoint list.

The scheduler queries every shard and only reuses the prefix available from all of them. Each worker loads with the lease issued by its local server. The connector gives every shard a distinct namespace, so deployments with a different host split cannot reuse an incompatible cache layout.

TP sharding currently requires equal contiguous shards and TP-only parallelism. Pipeline, decode-context, and prefill-context parallelism are rejected when more than one endpoint is configured.

P/D Partial Tail Blocks

vLLM normally exposes hashes only for complete KV blocks. In a P/D deployment, enable pegaflow.pd_tail_save on prefill and pegaflow.pd_tail_load on decode to reuse the final partial prompt block as well. Start both vLLM processes with the same explicit PYTHONHASHSEED and --prefix-caching-hash-algo xxhash_cbor.

Prefill: {"pegaflow.pd_tail_save": true}

Decode: {"pegaflow.pd_tail_load": true, "pegaflow.wait_for_full_prefix": true}

pegaflow.wait_for_full_prefix makes decode wait (up to 30s) until the full prompt prefix is fetchable from a remote node via MetaServer + RDMA. It only applies when prefill and decode run separate engines; it does not observe saves landing in a shared/local engine and has no effect when RDMA is not configured.

Development

See the examples directory for more usage examples.

Testing

Running Unit Tests

The test suite includes integration tests that verify the EngineRpcClient can correctly communicate with a running pegaflow-server instance.

Prerequisites

  1. Build the Rust extension:

    cd python
    maturin develop --release
    
  2. Build the server binary:

    cd ..
    cargo build --release --bin pegaflow-server
    
  3. Ensure CUDA is available (tests require GPU):

    python -c "import torch; assert torch.cuda.is_available()"
    

Running Tests

cd python

# Run all tests
pytest tests/ -v

# Run specific test file
pytest tests/test_engine_client.py -v

# Run with coverage
pytest tests/ --cov=pegaflow --cov-report=html

Test Structure

  • tests/conftest.py: Contains pytest fixtures for:

    • pega_server: Automatically starts/stops pegaflow-server for integration tests
    • engine_client: Creates an EngineRpcClient connected to the test server
    • client_context: Provides a ClientContext representing a vLLM instance with GPU KV cache tensors
    • registered_instance: Provides a registered instance ID for query tests
  • tests/test_engine_client.py: Integration tests for:

    • Server connectivity
    • Query operations with various inputs

Test Fixtures

The ClientContext class abstracts a vLLM instance and provides:

  • register_kv_caches(): Register GPU KV cache tensors with the server
  • query(block_hashes): Query available blocks
  • unregister_context(): Unregister context from server

Example test usage:

def test_query(client_context):
    """Test query operation."""
    result = client_context.query([])
    assert result is not None

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pegaflow_llm_cu13-0.23.11-cp314-cp314-manylinux_2_34_x86_64.whl (8.0 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.34+ x86-64

pegaflow_llm_cu13-0.23.11-cp314-cp314-manylinux_2_34_aarch64.whl (7.9 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.34+ ARM64

pegaflow_llm_cu13-0.23.11-cp313-cp313-manylinux_2_34_x86_64.whl (8.0 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ x86-64

pegaflow_llm_cu13-0.23.11-cp313-cp313-manylinux_2_34_aarch64.whl (7.9 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ ARM64

pegaflow_llm_cu13-0.23.11-cp312-cp312-manylinux_2_34_x86_64.whl (8.0 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

pegaflow_llm_cu13-0.23.11-cp312-cp312-manylinux_2_34_aarch64.whl (7.9 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ ARM64

pegaflow_llm_cu13-0.23.11-cp311-cp311-manylinux_2_34_x86_64.whl (8.0 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.34+ x86-64

pegaflow_llm_cu13-0.23.11-cp311-cp311-manylinux_2_34_aarch64.whl (7.9 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.34+ ARM64

pegaflow_llm_cu13-0.23.11-cp310-cp310-manylinux_2_34_x86_64.whl (8.0 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.34+ x86-64

pegaflow_llm_cu13-0.23.11-cp310-cp310-manylinux_2_34_aarch64.whl (7.9 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.34+ ARM64

File details

Details for the file pegaflow_llm_cu13-0.23.11-cp314-cp314-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for pegaflow_llm_cu13-0.23.11-cp314-cp314-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 927741ac86c2f0952a7dcb5ca01db9e521ff4e2652a9f061fd3c72920df26b7c
MD5 d97cbacb02c2f965c4a63b54409348a2
BLAKE2b-256 056ce11d9873f01e1416d7dd8159eb43ee73a94e037a294cc238444d6afa7197

See more details on using hashes here.

File details

Details for the file pegaflow_llm_cu13-0.23.11-cp314-cp314-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for pegaflow_llm_cu13-0.23.11-cp314-cp314-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 cf421d03405f095fa5ead2d4e238d31ff1f29bd1f1797fa701a1989dd8b57d7f
MD5 9d4703336521cb17f6dcf4728b8fb79f
BLAKE2b-256 3d035ce8773770444757b3aaca79bf3f0deafe02409f95d52d7050d2961de559

See more details on using hashes here.

File details

Details for the file pegaflow_llm_cu13-0.23.11-cp313-cp313-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for pegaflow_llm_cu13-0.23.11-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 993b10dfbd5e53a4ea106f1eef06835ca8b928c52b2b3e21e0a97942cd3efcc6
MD5 44758c5c62f2bd1e56534f0f37d04611
BLAKE2b-256 ab48106528b43bff3a5884daf5e2d8cb0a03a252d07ebe99dceb1a98b7253b34

See more details on using hashes here.

File details

Details for the file pegaflow_llm_cu13-0.23.11-cp313-cp313-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for pegaflow_llm_cu13-0.23.11-cp313-cp313-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 24e6d3a7209c49fefe6abc86b5abf55c9f36afba0fab61f973e0012635472bec
MD5 6c0c41b124ac168cf1daab67a17f45ea
BLAKE2b-256 6680bffca134c057398ecadd2f4cbe11f96f5fc5efad34e0a6802c649d0d63e4

See more details on using hashes here.

File details

Details for the file pegaflow_llm_cu13-0.23.11-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for pegaflow_llm_cu13-0.23.11-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 9f74ba7f716e4a537d3466e40fa8160a439982b98c302425229052d258305cb8
MD5 97bd9298360b50767f608ab5118c85ef
BLAKE2b-256 dcbc073e86dad0d6cb9994f003fbf53d2af5bf61e3b59cd30b5197f3b89a278b

See more details on using hashes here.

File details

Details for the file pegaflow_llm_cu13-0.23.11-cp312-cp312-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for pegaflow_llm_cu13-0.23.11-cp312-cp312-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 fa36e09dce565eba45ffefe5bcff4ee2af3bf63e1f2f13864b809dc8b1da6d97
MD5 f35e869874a223a94ee77f246c9c573c
BLAKE2b-256 176d043a0960649a14bbbf5bc5b084df509817af82e609f9828cb89b61c1e2b2

See more details on using hashes here.

File details

Details for the file pegaflow_llm_cu13-0.23.11-cp311-cp311-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for pegaflow_llm_cu13-0.23.11-cp311-cp311-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 2f3b80101725b1d914b1c95215a0244b69d96354820d79fac12f7e868a96e9ee
MD5 5f7eae0e20b5cc295a56b15f91cee08f
BLAKE2b-256 30f4469cd4825c454a9536572f6107283aaa2dff46976c2fd79c8043e0913bcb

See more details on using hashes here.

File details

Details for the file pegaflow_llm_cu13-0.23.11-cp311-cp311-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for pegaflow_llm_cu13-0.23.11-cp311-cp311-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 1713281c51b85978981227966f1105c3815231b356ef78619da4392f90c63764
MD5 8b93c3aa4b329cb8218bd141e0dc9613
BLAKE2b-256 3d3aef1ad5a90911b333b93c536e49fbead6b32444775c52f0b0ec72ed4acc18

See more details on using hashes here.

File details

Details for the file pegaflow_llm_cu13-0.23.11-cp310-cp310-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for pegaflow_llm_cu13-0.23.11-cp310-cp310-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 8ffd9dadd7b9b8e527a357783c7bcf180d7eb8d6f72ad86762e7b5c6fc04a203
MD5 8e0d7081f21c94736af1c59e52685f34
BLAKE2b-256 90016fc25fc7b7209e04b1fa322e3bc66e4a376efb9d2c845717be4e28320390

See more details on using hashes here.

File details

Details for the file pegaflow_llm_cu13-0.23.11-cp310-cp310-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for pegaflow_llm_cu13-0.23.11-cp310-cp310-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 f9f301f4f5b838e2f96d5d0b24bd0267cab3343f5a19eaef101c9c6b7f27587c
MD5 f1ba223abac7fb39c691dff6d4bf628b
BLAKE2b-256 f8d2b8ef3270b14fe14826130a559f68c0620a058b4fb4247dc0c761f45dae7d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page