CoolFace
Modelpublic

YuxuanGong/lean-RAG

sourceHugging Faceapache-2.0updated 3mo agoView on Hugging Face
1likes95downloads
Model Card

BGE-M3 Mathlib-RAG

READMEen:https://huggingface.co/YuxuanGong/lean-RAG/blob/main/READMEen.md

基于 BAAI/bge-m3 微调的 Mathlib4 API 检索模型,专为 Lean 4 形式化证明场景设计。该模型能够根据当前证明目标(goal type)和数学关键词,从 Mathlib4 的 32.6 万条声明中检索最相关的引理/定理/定义。

在我的github主页可访问完整skill:https://github.com/Tangtaizong-BUAA/Lean-formulizer

模型用途

该模型是 proof-formalizer 技能链路的检索核心。在形式化一个数学证明时:

  1. 1.从 Lean 环境中提取当前 目标类型(goal conclusion)
  2. 2.结合人类提供的数学关键词
  3. 3.用本模型编码为 1024 维向量
  4. 4.在预构建的 FAISS 索引中检索 top-k 最相关的 Mathlib4 声明

支持中英文混合查询,训练数据包含中文数学描述和 Lean 代码。

使用方式

python
from sentence_transformers import SentenceTransformer

model = SentenceTransformer("YuxuanGong/lean-RAG")

# 查询:Lean goal + 关键词
query = "x % y < y  nat mod lt"
# 候选:Mathlib4 声明(从 declarations.jsonl 构建的 FAISS 索引)
passages = [
    "Keywords: nat mod lt theorem | Name: Nat.mod_lt | Kind: theorem | ...",
    "Keywords: nat mod add theorem | Name: Nat.ModEq.add | Kind: theorem | ...",
    # ... 32.6 万条
]

query_emb = model.encode(query)
passage_embs = model.encode(passages)
similarities = model.similarity(query_emb, passage_embs)

与 proof-formalizer Skill 集成

bash
# Skill 中的 mathlib_lookup.sh (Route 4) 使用本模型:
DENSE_QUERY="${GOAL_TYPE} ${KEYWORDS_TEXT}"
# 然后调用 search.py 用本模型编码查询 -> FAISS 搜索

模型架构

基于 BGE-M3 (XLM-RoBERTa) 架构:

参数
基座模型BAAI/bge-m3
隐藏层维度1024
Transformer 层数24
注意力头数16
中间层维度4096
词表大小250,002
最大位置编码8,194
输出维度1024
池化方式CLS token
相似度函数Cosine Similarity
SentenceTransformer(
  (0): Transformer(XLMRobertaModel, task='feature-extraction')
  (1): Pooling(cls pooling, 1024d)
  (2): Normalize()
)

训练数据

从 Mathlib4 的 325,959 条声明中构造三元组训练数据:

  • 训练集: 323,977 条 (query, positive, negatives)
  • 验证集: 500 条
  • 格式: BGE 三元组格式 — 每条样本包含 1 个 query、1 个 positive passage、7 个 negative passages
  • Query: 定理名 + 类型签名(中英混合)
  • Positive: 对应引理的完整结构化描述(Keywords | Name | Kind | Namespace | Doc | Type)
  • Negatives: 随机采样 + 困难负样本

数据示例

角色内容
Query`theorem lengthtakeoflelength \(s.take n).length = n`
Positive`Keywords: le find iffName: Nat lefindiffKind: lemmaNamespace: NatType: ...`
Negative`Keywords: length take le' listName: List lengthtakele'Kind: theoremNamespace: List`

训练细节

超参数
损失函数MultipleNegativesRankingLoss (cosine, scale=20.0)
训练轮数2 epochs
训练步数6,750
Per-device Batch Size24
梯度累积4 (有效 batch size = 96)
学习率1e-5
LR 调度cosine
Warmup5%
优化器AdamW (fused)
精度FP16
随机种子42
GPURTX PRO 6000 Blackwell 98GB
训练时间~3.7 小时

训练 Loss 曲线

EpochStepLoss
0.612,0500.146
0.893,0000.167
1.204,0500.145
1.485,0000.131
1.786,0000.136
2.006,7500.140

评测结果

直接检索(Direct Retrieval)

给定声明名 + docstring,检索对应的 Mathlib 声明:

指标Baseline (BGE-M3)Fine-tuned (本模型)
Recall@1-29.4%
Recall@5-58.6%
Recall@10-66.2%
MRR-41.2%

Skill Mode F:引理检索(Lemma Conclusion → Find Lemma)

模拟真实使用场景 — "我有这个 goal,找到能证它的引理":

指标
查询数626
Recall@126.7%
Recall@561.5%
Recall@1071.9%
Recall@2080.5%
MRR42.1%

Skill Mode E:证明初始检索(Initial Goal → Proof Lemmas)

模拟 "开始证这个定理,需要哪些引理"(更难任务):

指标
查询数493
Recall@1013.8%
Recall@2018.3%
MRR4.7%

已知限制

  • Mode E 召回率较低:从零开始的证明检索仍具有挑战性,MRR 仅 4.7%
  • 跨版本 Mathlib 兼容性:模型训练于特定 Mathlib4 版本,API 可能随版本变化
  • 最大序列长度:虽然 BGE-M3 支持 8192 tokens,训练时截断至 400 tokens(适配 declaration 长度)
  • 中英文偏好:英文查询效果通常优于中文

环境依赖

bash
pip install sentence-transformers>=5.4.1

框架版本(训练时)

  • Python: 3.12.3
  • Sentence Transformers: 5.4.1
  • Transformers: 5.5.4
  • PyTorch: 2.8.0+cu128
  • Accelerate: 1.13.0

引用

如果你使用了这个模型,请引用:

bibtex
@misc{gong2025mathlib-rag,
  title={Mathlib-RAG: Fine-tuned BGE-M3 for Lean 4 Mathlib API Retrieval},
  author={Gong, Yuxuan},
  year={2025},
  publisher={Hugging Face},
  howpublished={\url{https://huggingface.co/YuxuanGong/lean-RAG}}
}

基座模型引用:

bibtex
@article{chen2024bge,
  title={BGE M3-Embedding: Multi-Lingual, Multi-Functionality, Multi-Granularity Text Embeddings Through Self-Knowledge Distillation},
  author={Chen, Jianlv and Xiao, Shitao and Zhang, Peitian and others},
  journal={arXiv preprint arXiv:2402.03216},
  year={2024}
}

许可

Apache 2.0