extropic-ai/thrml
Thermodynamic Hypergraphical Model Library in JAX
THRML – 基于 JAX 的概率图模型采样
它是什么
- 一个 Python 库(可通过
pip install thrml安装),构建于 JAX 之上。 - 为离散概率图模型(PGM)和基于能量的模型(EBM)提供快速、GPU 加速的 块吉布斯采样。
- 旨在为 Extropic 计划在定制低功耗硬件上运行的采样类型进行原型设计,但今天可在任何兼容 JAX 的设备上运行。
关键能力
- 块吉布斯采样 – 同时更新变量组(块),这比一次更新一个节点在现代硬件上高效得多。
- 任意 PyTree 节点状态 – 您可以将任何兼容 JAX 的数据结构附加到节点,而不仅仅是标量。
- 异构图 – 支持不同节点具有不同类型或交互模式的模型。
- 离散 EBM 工具 – 用于构建和训练基于能量的模型(如 Ising 模型)的辅助工具。
- 后端无关执行 – 核心 API 适用于默认的
JaxBackend,但您可以插入自定义后端,与外部硬件或其他加速器通信。
典型工作流程
- 定义节点(例如
SpinNode)和描述因子图的边。 - 创建模型(例如
IsingEBM),提供偏置、权重、温度等。 - 将节点分组为块(
Block),这些块将一起采样。 - 构建采样程序(
IsingSamplingProgram),将模型与块布局关联。 - 选择调度(
SamplingSchedule),指定预热步数、样本数量以及每个样本的吉布斯步数。 - 使用
sample_states运行采样,可选地传递自定义后端。
快速示例(对 5 节点 Ising 链进行采样)
import jax, jax.numpy as jnp
from thrml import SpinNode, Block, SamplingSchedule, sample_states
from thrml.models import IsingEBM, IsingSamplingProgram, hinton_init
# 1. 构建图
nodes = [SpinNode() for _ in range(5)]
edges = [(nodes[i], nodes[i+1]) for i in range(4)]
# 2. 模型参数
biases = jnp.zeros(5)
weights = jnp.full(4, 0.5)
beta = jnp.array(1.0)
model = IsingEBM(nodes, edges, biases, weights, beta)
# 3. 块布局(交替颜色)
free_blocks = [Block(nodes[::2]), Block(nodes[1::2])]
program = IsingSamplingProgram(model, free_blocks, clamped_blocks=[])
# 4. 初始化状态
key = jax.random.key(0)
init_state = hinton_init(jax.random.split(key)[0], model, free_blocks, ())
# 5. 采样调度
schedule = SamplingSchedule(n_warmup=100, n_samples=1000, steps_per_sample=2)
# 6. 运行采样器
samples = sample_states(jax.random.split(key)[1], program, schedule, init_state, [], [Block(nodes)])
该调用返回一个 JAX 数组,包含采样的自旋配置。
安装
pip install thrml # 或使用 "uv pip install thrml" 用于 uv 安装器
需要 Python 3.10 或更高版本,以及可用的 JAX 安装(CPU、GPU 或 TPU)。
文档与学习资源
- 完整 API 参考:https://docs.thrml.ai/en/latest/
- 概念指南(后端集成、观察器、训练):https://docs.thrml.ai/concepts.html
- 更详细的示例(复现 DTM 论文)在配套仓库中:https://github.com/pschilliOrange/dtm-replication
何时使用 THRML
- 您需要对离散 PGM(例如 Ising、Potts、二元因子图)进行高吞吐量采样。
- 您正在尝试块状吉布斯更新以加速收敛。
- 您想要一个 JAX 原生工作流,以后可以切换到自定义硬件后端。
- 您正在研究基于能量的模型,需要一个将模型视为因子图而非神经网络的库。
引用 如果您发布依赖 THRML 的工作,请引用随附的 arXiv 论文:
@misc{jelinčič2025efficientprobabilistichardwarearchitecture,
title={An efficient probabilistic hardware architecture for diffusion-like models},
author={Andraž Jelinčič and Owen Lockwood and Akhil Garlapati and Guillaume Verdon and Trevor McCourt},
year={2025},
eprint={2510.23972},
archivePrefix={arXiv},
primaryClass={cs.LG},
url={https://arxiv.org/abs/2510.23972}
}
谁应该关注
- 从事离散概率模型、EBM 或采样算法的研究人员和工程师。
- 对原型设计最终将在专用低能耗采样芯片上运行的算法的开发人员。
- 任何已经使用 JAX 并希望使用完全保持 JIT 兼容且避免 Python 级循环的库的人。
相关
- 项目
- 项目
- 项目
- 项目
- 项目