【MIIT program】support matterchat - #305
Open
learncat163 wants to merge 1 commit into
Open
Conversation
Author
MatterChat 推理对话示例1. 单次推理1.1 一行命令推理from ppmat.models import build_model_from_name
from pymatgen.io.cif import CifParser
model, _ = build_model_from_name('matterchat_full')
model.to('gpu').eval()
struct = CifParser('Si.cif').get_structures()[0]
print(model.chat(struct, 'what is the chemical formula of this material?'))
# -> The chemical formula of this material is Si.1.2 指定 config + 本地权重from omegaconf import OmegaConf
from pymatgen.io.cif import CifParser
from ppmat.models import build_model
from ppmat.utils import save_load
config = OmegaConf.to_container(OmegaConf.load('structure_generation/configs/matterchat/matterchat_full.yaml'), resolve=True)
model = build_model(config['Model'])
save_load.load_pretrain(model, './matterchat_full/')
model.to('gpu').eval()
struct = CifParser('Si.cif').get_structures()[0]
print(model.chat(struct, 'what is the chemical formula of this material?'))2. 多轮对话示例对同一晶体连续提问多个问题: from ppmat.models import build_model_from_name
from pymatgen.io.cif import CifParser
model, _ = build_model_from_name("matterchat_full")
model.to("gpu").eval()
struct = CifParser("GaN.cif").get_structures()[0]
# 4 个标准问题
prompts = [
"what is the chemical formula of this material?",
"what is the space group of this material?",
"Is this material stable or not?",
"What is the bandgap of this material?",
]
for prompt in prompts:
answer = model.chat(struct, prompt, max_new_tokens=64)
print(f"Q: {prompt}")
print(f"A: {answer}")
print()输出: 3. 批量推理 (多个 CIF)import os
from ppmat.models import build_model_from_name
from pymatgen.io.cif import CifParser
model, _ = build_model_from_name("matterchat_full")
model.to("gpu").eval()
cif_dir = "path/to/cif_files"
prompt = "what is the chemical formula of this material?"
for fname in sorted(os.listdir(cif_dir)):
if not fname.endswith(".cif"):
continue
struct = CifParser(os.path.join(cif_dir, fname)).get_structures()[0]
answer = model.chat(struct, prompt, max_new_tokens=64)
print(f"{fname}: {answer}") |
|
Thanks for your contribution! |
leeleolay
reviewed
Jul 16, 2026
| from ppmat.datasets.oc20_s2ef_dataset import OC20S2EFDataset # noqa | ||
| from ppmat.datasets.qm9_dataset import QM9Dataset # noqa | ||
| from ppmat.datasets.omol25_dataset import OMol25Dataset | ||
| from ppmat.models.matterchat.trainer import MTDataset # noqa |
Collaborator
There was a problem hiding this comment.
ppmatSim已经支持相关的功能,复用已有的
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
MatterChat diff (PyTorch and PaddlePaddle)
AiStudio在线推理案例
MatterChat 由三个模块串联组成: CHGNet (晶体编码器) → Q-Former (跨模态桥接) → Mistral-7B (大语言模型)。本文档分别验证每个模块从 PyTorch 迁移到 PaddlePaddle 后的数值对齐情况。
1. CHGNet Diff
CHGNet 负责将晶体结构 (CIF) 编码为逐原子 embedding。验证用
GaN.cif(4 原子) 作为输入, 对比 Paddle 输出与 PyTorch 参考的[N_atom, 64]material embedding。结果
代码样例
PyTorch 参考数据生成
PaddlePaddle 对比
2. Q-Former Diff
Q-Former 是 BLIP-2 风格的 BERT 变体, 用 32 个 query token 对 CHGNet 输出做 cross-attention, 产生
[1, 32, 768]的查询表示。验证 Q-Former 前向输出与基线一致。结果
Q-Former 输出与基线完全一致 (diff=0)。
代码样例
PyTorch 参考数据生成
PaddlePaddle 对比
3. Mistral LLM Diff
Mistral-7B 是 32 层 decoder-only Transformer, 含 RoPE、GQA、SwiGLU。由于模型规模大 (7.3B 参数), float16 跨框架累积误差不可避免, 因此阈值放宽至 1e-3。验证逐层 hidden state、最终 hidden_last 及 Top-20 token 匹配。
结果汇总
逐层误差
代码样例
PyTorch 参考数据生成
PaddlePaddle 对比