[USearch 源码解读] Python 生态

USearch Python 生态解读——pybind11 原生层的 GIL 释放模式与 per-index mutex、index.py 的 Pythonic 封装、numba cfunc JIT 度量、Indexes 分片 facade 与 ucall RPC 服务器

src/pages/articles/_md/Database/VectorSearch/USearch/CodeWiki/2.26.2/06-python-ecosystem.md

← 返回概览


模块定位

python/ 是 USearch 引用量最大的绑定(PyPI usearch),也是唯一发展出完整生态层的绑定:原生 pybind11 扩展(python/lib.cpp,1,548 行)+ 纯 Python 包(index.py 1,764 行、numba.py、server.py、io.py、client.py、eval.py)。构建入口在仓库根:setup.py 用 Pybind11Extension("usearch.compiled", sources=["python/lib.cpp"]) 编译,pyproject.toml 的 cibuildwheel 出 7 平台 × 6 Python 版本 = 42 个 wheel(含 free-threaded 3.14t,模块声明 py::mod_gil_not_used(),python/lib.cpp:1124)。

模块架构

Python 生态分层
Python 生态分层

双层结构的分工写在 index.py:5-8 头注释:usearch.compiled(snake_case、buffer 导向)只做性能与分发;usearch/index.py 做 API 美化——keyword-only 签名、rich docstring、__contains__/__getitem__ 协议,并”embeds JIT into the primary Index class”。compiled.pyi 为原生层补类型桩(py.typed 标记 PEP 561)。构建上单头 C++ 库 + NumKong 动态分发:macOS 用 -undefined dynamic_lookup 让 NumKong 符号运行时由 __init__.py:9-33 的 ctypes.CDLL(_numkong_path, mode=ctypes.RTLD_GLOBAL) 预加载解析——避免静态链接整个内核库。

调用链路

批量插入的完整并发时序(GIL 与锁的协作是这条链路的看点):

Index.add(keys, vectors, copy, threads, log)                    python/usearch/index.py:689
└─ _add_to_compiled                                                index.py:263
   ├─ keys=None → start_id = len(compiled),keys = np.arange(start_id, start_id+n, dtype=Key)
   │                                                  自动编号并把 keys 返回给调用方   index.py:283-289
   └─ compiled.add_many(keys, vectors, copy, threads, progress, dtype)   pybind11
      └─ add_many_to_index                                         python/lib.cpp:252
         ├─ keys/vectors.request():buffer 协议零拷贝取裸指针        lib.cpp:256
         ├─ 校验:keys itemsize=8、C-contiguous、维度匹配            lib.cpp:259-283
         └─ add_typed_to_index<scalar_at>                          lib.cpp:196
            ├─ { py::gil_scoped_release release;                  lib.cpp:213 ← GIL 释放
            │    unique_lock lock(*index.mutex_ptr_);             lib.cpp:214 ← per-index mutex
            │    index.try_reserve(ceil2(size+n), threads);       lib.cpp:215 ← 2 的幂避免反复 realloc
            │    executor.dynamic(count, λ(thread_idx, task_idx):
            │        index.add(key, vector, thread_idx, force_copy))  ← thread_idx 定位 cast 槽
            │  }
            ├─ 仅 thread_idx==0 的 worker acquire GIL 查信号/推进度   lib.cpp:227-235
            └─ atomic_error → PyErr_SetString → py::error_already_set  lib.cpp:244-248

时序纪律有三条:GIL 在 buffer 校验完成后、拿 mutex 之前释放;先弃 GIL 再拿锁(防锁序反转死锁,见下);错误只由主线程单点抛——工作线程经 std::atomic<char const*> 上报(lib.cpp:194)。

读路径的返回契约:compiled.search_many 返回 5 元组——(keys 2D, distances 2D, counts 1D, visited_members, computed_distances)(lib.cpp:539-544)——Python 侧包成 BatchMatches;单条查询经 distill_batch(index.py:224)蒸馏为 Matches,多查询保持 BatchMatches。一个如实记录的 API 瑕疵:Index.search 签名里有 radius: float = math.inf 参数(index.py:744),但调用 _search_in_compiled 时没有透传——是被静默忽略的死参数(index.py:779-790 的调用清单里无 radius)。

核心实现

dense_index_py_t:为什么继承还要加一把锁

python/lib.cpp:79-88 的原注释解释了全模块最重要的一个设计:原生 index_dense_t 假定单一拥有线程——它携带 per-worker 的 cast_buffer_ 槽位(index_dense.hpp:461-464),绑定层为每次调用挑选 executor-local 的 thread_idx;多个 Python 线程对同一索引做重操作会在这些槽位上冲突,所以在每个”释放 GIL 的绑定入口点”都持 per-index mutex。用 unique_ptr 持有而非直接成员:std::mutex 不可拷贝/移动,而 pybind11 按值返回的工厂要求 wrapper 可移动构造(lib.cpp:86-88)。

merge_paths 的锁序注释(防死锁教科书)

dense_indexes_py_t::merge_paths(python/lib.cpp:103-127)并行 view 多个分片文件,注释原文(lib.cpp:109-112):

“Release the GIL before taking the per-index mutex so a Python thread waiting on the mutex doesn’t hold the GIL - otherwise a worker thread in the current owner would block forever in gil_scoped_acquire.”

即等锁线程若攥着 GIL,持锁方的 worker 线程在 progress 回调里 acquire GIL 会永久阻塞——经典锁序反转。worker 内还周期性 gil_scoped_acquire + PyErr_CheckSignals 响应 Ctrl-C(lib.cpp:122-124)。

index.py:归一化与惰性结果

  • dtype 归一化(_normalize_dtype at index.py:100):dtype 为 None 时按 metric 位运算(MetricKindBitwise,Hamming/Tanimoto/Jaccard 等)→ 直接选 B1,否则经 _hardware_acceleration 依次探测 BF16/F16 硬件加速,都不行回落 F32——README 说”默认存储精度硬件相关”的出处。一个反直觉映射:np.uint8 被映射到 ScalarKind.B1(而不是 U8)——NumPy 的 uint8 更多被当”8 个打包布尔”用;要存无符号字节需显式传 ScalarKind.U8;
  • Matches/BatchMatches 惰性视图(index.py:330-434):C++ 预分配三个密集数组,batch[i](index.py:396)按 counts[i] 做 numpy 视图切片不复制;mean_recall 等评估直接在 2D 数组上向量化(index.py:412-431),to_list() 才物化 Python 对象——百万级查询 × top-k 若物化成 tuple 列表会产生巨额对象分配与 GIL 压力;
  • Clustering(index.py:437):subcluster 递归下钻、network 懒导入 networkx 建图、plot_centroids_popularity 懒导入 matplotlib;
  • Index.restore/metadata(index.py:654-684):先读 head 元数据再构造同构 Index 并 load/view。

numba.py:cfunc JIT 度量

jit(ndim, metric, dtype)(python/usearch/numba.py:10)返回 CompiledMetric(pointer, kind, signature):用 numba cfunc + types 签名(而非 ctypes 签名——docstring numba.py:21-23 说明是为了支持 half-precision),carray(a, ndim) 包装裸指针按 ndim 静态展开循环。为何最多 3x 提升(numba.py:17-19 原文):wheel 为兼容老 CPU 避开最新 SIMD 指令,Numba 可针对当前机器 + 固定 ndim 特化。限制:numba 不支持 float16(注释引用 numba#4402,numba.py:36-39),不支持的 dtype 直接原样返回内置度量(降级);i8 + IP 组合改写为 Cos。这个 CompiledMetric 经 make_index 的 metric_uintptr(lib.cpp:153-156)挂到 metric_t::stateless——Numba 产物与内置度量同一分发路径。

server.py:ucall RPC 与 i8 的 JSON 黑话

serve()(python/usearch/server.py:27)用 ucall.rich_posix.Server 注册 size/ndim/add/search 等 RPC,默认端口 8545。i8 向量传输的”dirty hack”(server.py:12-24 原注释)值得记录:假设 i8 标量在 [0,100] 区间,直接用 JSON 字符串传向量——ASCII 可打印字符恰好覆盖加 23 后的 [23,123],只需把 "(避开)和 |↔\(转义冲突)做两个映射(_ascii_to_vector:vector[vector==124]=60; vector-=23)。客户端 client.py 是逆操作。牺牲 0-22 区间换 JSON 编码零开销。服务生命周期:索引文件已存在时 immutable 模式走 index.view 否则 index.load;收到 KeyboardInterrupt 时仅 not immutable 才 index.save(path) 落盘后退出(server.py:88-95)。

io.py / eval.py

io.py 读写二进制矩阵格式(.fbin/.i8bin/.bbin 等):8 字节头(rows、cols 两个 int32)+ 原始数据,支持 np.memmap view 模式(io.py:92-99),校验文件大小防截断。eval.py 是基准评估:random_vectors 按 ScalarKind 动态范围生成不溢出量化的随机向量;Dataset → AddTask/SearchTask → Evaluation 链条聚合 search_per_second / recall_at_one / dcg/ndcg,__main__ 提供完整 CLI。

发现的两个本版缺陷(如实记录)

  1. Indexes.merge_path 必炸(python/usearch/index.py:1535-1536):调用 self._compiled.merge_path(...),但 pybind11 只注册了 merge_paths(python/lib.cpp:1537)——运行时 AttributeError。调用方需改用 merge_paths([path])。
  2. eval.py:83 引用了不存在的 index.numpy_dtype 属性(只有 dtype),传 index 参数时会 AttributeError。

设计模式

模式 位置 为什么用
原生/纯 Python 双层 compiled + index.py 性能与分发归原生;类型标注/lint/docstring 归纯 Python
GIL 释放统一范式 add_typed_to_index(lib.cpp:196) {release; mutex; parallel} + atomic 错误通道 + thread-0 信号检查
分片 facade dense_indexes_py_t(lib.cpp:94) N 个 mmap 只读分片当一个索引,merge_into 归并 top-k、bitset_t 按查询行加锁
度量即指针 `MetricLike = str MetricKind
进度回调 GIL 包装 progress_t(lib.cpp:53-69) gil_scoped_acquire 引用计数、已持有时 no-op,任何上下文安全

模块间交互

python/lib.cpp:32-33 直接 include index_dense.hpp 与 index_plugins.hpp(与 多语言绑定 的直连路线同源);__init__.py 的 NumKong RTLD_GLOBAL 预加载与 BinaryManager(定位/下载 usearch_sqlite 二进制)是 Python 特有的部署拼图。Indexes 分片搜索路径:每 shard 单线程内逐查询搜索 + result.merge_into 把各分片 top-k 归并进同一行(lib.cpp:376-463)。

扩展方式

  • 新增 Python API:纯 Python 层直接在 index.py 加方法;需新 C++ 能力则 lib.cpp 加 i.def(...) + compiled.pyi 补桩。
  • 新增 numba 支持的 dtype:numba_supported_types 加入选(F16 需等 numba#4402)、scalar_kind_to_accumulator_type/signature 各一行——C++ 侧 metric_t::stateless 已 dtype 无关,无需改动。
  • 新增内置度量:C++ 枚举 → lib.cpp:1151 native_enum 注册 → index.py _normalize_metric 加字符串别名 → eval.py 判断位运算类。