Quickstart
Installation
pip install dag-kv
Verify your GPU / Apple Silicon / CPU backend:
dag-kv info
3-Line Permuted RAG Example
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from dag_kv import patch_hf_model
model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-3B-Instruct", dtype=torch.float16, device_map="cuda:0")
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-3B-Instruct")
dag = patch_hf_model(model, tokenizer, quantization="k8v4", bridge_strategy="submodular", bridge_top_k=0.04)
# Enable CUDA Graphs for 2.17x faster B=1 decode
dag.engine.enable_cuda_graph_decode(batch_sizes=(1,), max_blocks_cap=256)
chunk_a = dag.prefill_chunk("### FILE: auth.py\nRS256 JWT tokens signed by kms-prod-09.\n")
chunk_b = dag.prefill_chunk("### FILE: db.py\nPostgreSQL 16 on pg-primary.internal:5432.\n")
# Reorder arbitrarily with 100% KV reuse:
print(dag.generate([chunk_b, chunk_a], prompt_suffix="\nQuestion: What DB port is used?\nAnswer:"))
Live IDE AST-Delta Caching
from dag_kv import ASTDeltaCache
ast_cache = ASTDeltaCache(auto_invalidate_stale=True)
chunks_v1, st1 = ast_cache.sync_file("service.py", source_v1, dag.engine)
chunks_v2, st2 = ast_cache.sync_file("service.py", source_v2, dag.engine)
print(f"Reused {st2.reused_nodes}/{st2.total_nodes} AST nodes ({st2.token_reuse_pct:.1f}% tokens)")