Skip to main content

EngramDB 的 Python 分发包

Disk-first storage engine for Engram / PLE n-gram memory tables (Rust).

分发名 engramdb-python(PyPI 相似名规避);import 名仍为 engramdb。

当前 v0.2.x 同时包含两条 Python 接入路径:

  1. PyO3 原生扩展(优先):crates/engramdb-pyo3,构建后以 python/engramdb/_engramdb.so 提供 Store / View / PageReader / Linux IoUringPageReader。
  2. ctypes C-ABI 回退:crates/engramdb-python,无 PyO3 构建产物时也能用。

python/engramdb/__init__.py 会自动优先加载 PyO3,失败则回退 ctypes。

快速使用

# 方式 1:maturin 构建 wheel(推荐,直接产生可安装的 native wheel)
cd python
CARGO_HOME=/tmp/cargo-home RUSTFLAGS="-C link-arg=-undefined -C link-arg=dynamic_lookup" \
  maturin build --release --interpreter python3

# 方式 2:开发期内直接构建并复制到包目录
CARGO_HOME=/tmp/cargo-home RUSTFLAGS="-C link-arg=-undefined -C link-arg=dynamic_lookup" \
  cargo build -p engramdb-pyo3 --release
cp target/release/lib_engramdb.dylib python/engramdb/_engramdb.so

# 在 engram-peft 等项目中用 uv 添加本地开发依赖
cd ~/code/engram-peft
uv add --editable ../EngramDB/python

# 发布到 PyPI 后可直接:
# uv add engramdb-python
import engramdb

# 打开 Store-I:目录内为 shard_000.bin 等定长行文件
store = engramdb.Store("path/to/rows", shards=1, rows_per_shard=100, width=256)
row_bytes = store.fetch([0, 1, 2])   # bytes, 每条 256B
store.close()

# 打开 Store-P 视图
view = engramdb.View("path/to/view.bin")
rec = view.read_record(0)            # 一条 e_t 记录

# SGLang 兼容:从多个 fd/offset 读页(Unix 有 PageReader,Linux 另有 IoUringPageReader)
reader = engramdb.PageReader(page_size=4096)
pages = reader.read_pages([fd0, fd1], [offset0, offset1])

引擎适配层

目标是 不改 vLLM / SGLang 源码,启动前执行一小段 hook 即可把 PLE 表切到 EngramDB。

vLLM

from engramdb import Store
from engramdb.vllm_plugin import install_vllm_ple

store = Store("/path/to/engram-rows", shards=..., rows_per_shard=..., width=...)

install_vllm_ple(
    Qwen3_8FlashNextNGramEmbedding,   # 实际运行的 vLLM 模型类
    store=store,
    attr_name="embed_tokens_per_layer",
    embedding_dim=hidden_size_per_layer_input,
)

from vllm import LLM
llm = LLM(model="...", ...)

如果已经构造好模型实例,可以用:

from engramdb.vllm_plugin import patch_named_embedding
patch_named_embedding(model, "embed_tokens_per_layer", store=store, embedding_dim=...)

SGLang

from engramdb.sglang import install_sglang_ple

install_sglang_ple(
    Gemma4Model,                      # 实际运行的 SGLang 模型类
    store=store,
    attr_name="embed_tokens_per_layer",
    embedding_dim=hidden_size_per_layer_input,
)

低层 reader 替换:

from engramdb.sglang import install_sglang_io_uring_reader
install_sglang_io_uring_reader()

engram-peft 集成

安装 engramdb-python 后,可以直接使用内置的磁盘版 MultiHeadEmbedding:

import engramdb
from engramdb.integrations import install_disk_multi_head_embedding

store = engramdb.Store("path/to/embedding-store", shards=1, rows_per_shard=100, width=256)
install_disk_multi_head_embedding(store)

# 之后再调用 engram-peft 的 get_engram_model(...) 即可让 Engram 层从磁盘读取 embedding

多表 / Arrow / 最小服务原型

多表按目录组织:

from engramdb import Database

db = Database("path/to/tables-root")
print(db.list_tables())  # ["alpha", "beta"]

raw = db.fetch("alpha", [1, 3], shards=1, rows_per_shard=100, width=256)

可选 Arrow 读取(需要 pyarrow):

from engramdb.arrow_utils import store_fetch_arrow, table_to_ipc_bytes

table = store_fetch_arrow(store, [0, 1, 2])
ipc = table_to_ipc_bytes(table)   # Arrow IPC stream bytes

最小服务(当前为原型,提供两种 wire 模式):

JSON 模式:

from engramdb import Database
from engramdb.server import EngramDBServer

server = EngramDBServer(Database("path/to/tables-root"), host="127.0.0.1", port=8765)
server.serve_forever()

二进制模式(长度前缀 + 1-byte kind,fetch_raw 直接返回原始字节, fetch_arrow 直接返回 Arrow IPC stream,不需要 base64 包装):

from engramdb import Database, EngramDBBinaryServer, EngramDBClient

server = EngramDBBinaryServer(
    Database("path/to/tables-root"),
    host="127.0.0.1",
    port=8765,
)
server.serve_forever()

# 客户端
with EngramDBClient("127.0.0.1", 8765) as client:
    tables = client.list_tables()
    raw = client.fetch_raw("alpha", [0, 1, 2], shards=1, rows_per_shard=100, width=256)
    ipc = client.fetch_arrow("alpha", [0, 1, 2], shards=1, rows_per_shard=100, width=256)

服务命令包括:

  • ping
  • list_tables
  • fetch / fetch_raw
  • fetch_arrow(JSON 模式返回 base64 封装的 Arrow IPC;二进制模式返回裸 Arrow IPC stream)
  • view_read

注意:PyO3 Store 是不可跨线程共享的 unsendable 对象,因此服务端 Database.fetch 会在每个请求所在线程新开 Store;多线程共享连接的后端需要 Rust 侧安全句柄或线程池。

定位(一句话)

让"确定性哈希的 n-gram 记忆表"(Qwen PLE、DeepSeek Engram 等)像数据库一样落盘、建索引、预取、服务化——单机 CPU+NVMe 低延迟推理 / 高吞吐训练预处理。

详细文档见上游仓库 docs/(design.md / specifications / roadmap)以及 examples/interop_engram_peft.py。

Metadata

Release files for engramdb-python 0.2.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for engramdb-python 0.2.8
File Size Uploaded
engramdb_python-0.2.8.tar.gz 49.0 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for engramdb-python 0.2.8
File
engramdb_python-0.2.8-cp310-abi3-win_amd64.whl CPython 3.10 abi3 Windows x86-64 Details
engramdb_python-0.2.8-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.10 abi3 Linux glibc 2.17+ x86-64 Details
engramdb_python-0.2.8-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.10 abi3 Linux glibc 2.17+ ARM64 Details
engramdb_python-0.2.8-cp310-abi3-macosx_11_0_arm64.whl CPython 3.10 abi3 macOS 11.0+ ARM64 Details
engramdb_python-0.2.8-cp310-abi3-macosx_10_12_x86_64.whl CPython 3.10 abi3 macOS 10.12+ x86-64 Details

Total release size: 1.6 MB

Release files / engramdb_python-0.2.8.tar.gz

Download URL engramdb_python-0.2.8.tar.gz
Size 49.0 kB
Tags Source
SHA-256 checksum
How to use checksums
052f82344817db39ba6cb49cd6dbaefe57ac7a4c0968168ff853b91eb26f9994
BLAKE2b-256 checksum
How to use checksums
a94399d8b465528fac52e391014cf8395cf94f0fed462a7bea58fcfa9fea3131
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 30, 2026.

Transparency log

Release files / engramdb_python-0.2.8-cp310-abi3-win_amd64.whl

Download URL engramdb_python-0.2.8-cp310-abi3-win_amd64.whl
Size 229.0 kB
Tags CPython 3.10 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
ffe856c77e665a4c4755cc7c001ef658054705a82380e222f8130fc1c51180af
BLAKE2b-256 checksum
How to use checksums
1c7495c64a7630628ccc1b5e3f2dba00d3125b788f40e6fee6a7cb6652b9d00c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 30, 2026.

Transparency log

Release files / engramdb_python-0.2.8-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL engramdb_python-0.2.8-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 354.0 kB
Tags CPython 3.10 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
051452eeebf2143c58e3b6e5a30f0c96a02f75a00c0bb80239897a0903833a54
BLAKE2b-256 checksum
How to use checksums
5ea8a23eeb126ea56591bcc1212fb2c5e13b283798f22a61e20ac94243b488ac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 30, 2026.

Transparency log

Release files / engramdb_python-0.2.8-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL engramdb_python-0.2.8-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 337.1 kB
Tags CPython 3.10 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
fa1e850590fb2da8c0d2f396df98700274bd984b857dadbd9356546a670beafe
BLAKE2b-256 checksum
How to use checksums
b8585d9781e37a46039222d8caab19ec74d2da2482f0521a01ee6ecb0fc0655a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 30, 2026.

Transparency log

Release files / engramdb_python-0.2.8-cp310-abi3-macosx_11_0_arm64.whl

Download URL engramdb_python-0.2.8-cp310-abi3-macosx_11_0_arm64.whl
Size 334.5 kB
Tags CPython 3.10 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
05a767acf18a51fbf80ed0cbcbbd4773f0cded9a8b86e12ef084c5609b7bc9ff
BLAKE2b-256 checksum
How to use checksums
1701a7ad86804c7324b2e880aca4dac35f26254f91930d3d194e4617e74afea8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 30, 2026.

Transparency log

Release files / engramdb_python-0.2.8-cp310-abi3-macosx_10_12_x86_64.whl

Download URL engramdb_python-0.2.8-cp310-abi3-macosx_10_12_x86_64.whl
Size 343.5 kB
Tags CPython 3.10 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
fb034e97664c9966ec1b7d7cff4ef753b60ece0f372086f64b984d739bb38483
BLAKE2b-256 checksum
How to use checksums
162fc9ee912087a521cd43a9d81ef33495401e698b5b776fd518d97b862b9518
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 30, 2026.

Transparency log

Release history Release notifications | RSS feed

0.3.0

6 release files

0.2.10

6 release files

0.2.9

6 release files

This release

0.2.8 This release

6 release files

0.2.6

6 release files

0.2.5

6 release files

0.2.4

6 release files

0.2.3

6 release files

0.2.2

6 release files

0.2.1

6 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page