MatterGen
Model Introduction
MatterGen is a generative model for inorganic materials developed by Microsoft Research. It jointly generates elemental compositions, unit cells, and periodic atomic coordinates, and supports candidate crystal generation conditioned on material properties.
Paper: MatterGen: a generative model for inorganic materials design
Reference implementation: https://github.com/microsoft/mattergen
Model Description
MatterGen uses a diffusion model to learn distributions over atomic species, fractional coordinates, and crystal lattices. It supports unconditional crystal generation and conditional generation based on properties such as chemical system, space group, magnetic density, band gap, and bulk modulus.
This repository contains the OneScience-adapted MatterGen model, diffusion and sampling code, and entry points for training, property fine-tuning, crystal generation, and data conversion. Model code is located in model/; data processing, general-purpose network layers, and property embeddings are provided by OneScience MatChem.
Use Cases
| Use case | Description |
|---|---|
| Unconditional crystal generation | Generate new candidate crystal structures with the base checkpoint |
| Property-conditioned generation | Generate structures for target properties such as magnetic density, band gap, and bulk modulus |
| Fixed-composition structure prediction | Search for possible crystal structures for a specified composition |
| Training from scratch | Train with MP-20 or data compatible with the MatterGen cache format |
| Property fine-tuning | Add a property adapter to a pretrained model and fine-tune it |
| Environment connectivity check | Verify OneScience, the checkpoint, and GPU/DCU availability by generating one sample |
Usage
1. Using OneCode
Try intelligent, one-click AI4S programming in the OneCode online environment:
Try intelligent, one-click AI4S programming
2. Manual Installation and Usage
Hardware requirements
- A GPU or DCU is recommended.
- A CPU can be used for import and configuration connectivity checks, but full generation and training will be slow.
- A DCU requires DTK to match the PyTorch build. Single-sample generation has been verified with DTK 25.04.2 and
torch 2.5.1+das.opt1.dtk25042.
Download the Model Package
hf download OneScience-Group/Mattergen
cd Mattergen
Install the Runtime Environment
DCU environment
# Activate DTK and conda first
module load compiler/dtk/25.04.2
conda create -n onescience311 python=3.11 -y
conda activate onescience311
# uv installation is also supported
pip install onescience[matchem-dcu] \
-i http://mirrors.onescience.ai:3141/pypi/simple/ \
--trusted-host mirrors.onescience.ai
GPU environment
# Activate conda first
conda create -n onescience311 python=3.11 -y \
libstdcxx-ng=12 libgcc-ng=12 gcc_linux-64=12 gxx_linux-64=12
conda activate onescience311
# uv installation is also supported
pip install onescience[matchem-gpu] \
-i http://mirrors.onescience.ai:3141/pypi/simple/ \
--trusted-host mirrors.onescience.ai
Training Data
MatterGen training uses a converted data cache. The MP-20 dataset is available on Hugging Face and can be downloaded directly:
hf download OneScience-Group/mp20 --repo-type dataset
The downloaded MatterGen cache is located at:
datasets/mp20/data/MP20/cache/mp_20/
├── train/
├── val/
└── test/
The default training YAML cannot use the downloaded data until you add its actual path under args. For example, update demo/configs/train_8dcu.yaml as follows:
args:
data_module: mp_20
data_module.root_dir: ./datasets/mp20/data/MP20/cache/mp_20
For property fine-tuning, also add data_module.root_dir under args in demo/configs/finetune_dft_mag_density_smoke.yaml. If you downloaded the data elsewhere, use the corresponding absolute path.
Convert custom CSV data first:
python csv_to_dataset.py \
--csv-folder /path/to/csv_folder \
--dataset-name my_dataset \
--cache-folder ./datasets/cache
Trained Weights
After downloading the model package with the Hugging Face command above, the pretrained checkpoints are located in weight/:
weight/
├── mattergen_base/
│ ├── config.yaml
│ └── checkpoints/
│ └── last.ckpt
└── dft_mag_density/
├── config.yaml
└── checkpoints/
└── last.ckpt
The repository also provides property-conditioned checkpoints for chemical_system, dft_band_gap, dft_mag_density, ml_bulk_modulus, and space_group. When using a demo YAML, set checkpoint in demo/configs/generate_base.yaml or demo/configs/generate_dft_mag_density.yaml to the corresponding ./weight/<model_name> path. For fine-tuning, update adapter.model_path in demo/configs/finetune_dft_mag_density_smoke.yaml.
Inference
Generate one crystal structure unconditionally:
python generate.py \
--checkpoint ./weight/mattergen_base \
--output outputs/generate/mattergen_base \
--batch-size 1 \
--num-batches 1
The generated outputs include:
outputs/generate/mattergen_base/
├── generated_crystals.extxyz
└── generated_crystals_cif.zip
Magnetic-density-conditioned generation requires the matching property checkpoint:
python generate.py \
--checkpoint ./weight/dft_mag_density \
--output outputs/generate/dft_mag_density \
--batch-size 1 \
--num-batches 1 \
--properties '{"dft_mag_density": 0.15}'
You can also use the demo YAML files:
cd demo
bash run.sh --config configs/generate_base.yaml
bash run.sh --config configs/generate_dft_mag_density.yaml
Training
Submit an eight-DCU training job with the MP-20 cache:
cd demo
bash run.sh --config configs/train_8dcu.yaml --submit
You can also launch training directly from the repository root with Hydra arguments:
python train.py \
data_module=mp_20 \
data_module.root_dir=./datasets/mp20/data/MP20/cache/mp_20 \
trainer.devices=1 \
data_module.batch_size.train=4
Fine-Tuning
The repository provides a smoke-test configuration for magnetic-density property fine-tuning. By default, it runs only one training batch and one validation batch:
cd demo
bash run.sh --config configs/finetune_dft_mag_density_smoke.yaml
For a full fine-tuning run, copy the YAML and update adapter.model_path, data_module.properties, the property embedding, batch size, and number of epochs. Remove trainer.limit_train_batches and trainer.limit_val_batches.
Official OneScience Resources
| Platform | OneScience Main Repository | Skills Repository |
|---|---|---|
| Gitee | https://gitee.com/onescience-ai/onescience | https://gitee.com/onescience-ai/oneskills |
| GitHub | https://github.com/onescience-ai/OneScience | https://github.com/onescience-ai/oneskills |
Citation and License
- The MatterGen-related code comes from the MatChem examples in the OneScience project and refers to the upstream MatterGen project (https://github.com/microsoft/mattergen). The upstream MatterGen code is released under the MIT License.
- If you use MatterGen training or generation results in research, please cite the original MatterGen paper, the relevant OneScience projects, and the datasets used.
中文说明
模型简介
MatterGen 是 Microsoft Research 开发的无机材料生成模型。它联合生成元素组成、晶胞和周期性原子坐标,支持以材料属性为条件生成候选晶体。
论文《MatterGen:用于无机材料设计的生成模型》
参考实现 https://github.com/microsoft/mattergen
模型描述
MatterGen 使用扩散模型学习原子种类、分数坐标和晶格的分布。它支持无条件生成晶体,也支持以化学体系、空间群、磁密度、带隙和体模量等属性为条件生成晶体。
本仓库包含经 OneScience 适配的 MatterGen 模型、扩散和采样代码,以及训练、属性微调(fine-tuning)、晶体生成和数据转换的入口。模型代码位于 model/。数据处理、通用网络层和属性嵌入由 OneScience MatChem 提供。
使用场景
| 使用场景 | 说明 |
|---|---|
| 无条件晶体生成 | 使用基础模型权重(检查点)(checkpoint)生成新的候选晶体结构 |
| 属性条件生成 | 为磁密度、带隙及体模量等目标属性生成结构 |
| 固定组成的结构预测 | 搜索指定组成对应的可能晶体结构 |
| 从头训练 | 使用 MP-20 或兼容 MatterGen 缓存格式的数据进行训练 |
| 属性微调 | 在预训练模型中加入属性适配器并微调 |
| 环境连通性检查 | 通过生成一个样本验证 OneScience、模型权重(检查点)及 GPU/DCU 是否可用 |
使用方法
1. 使用 OneCode
在 OneCode 在线环境中体验智能一键 AI4S 编程。
2. 手动安装与使用
硬件要求
- 建议使用 GPU 或 DCU。
- 可使用 CPU 进行导入和配置连通性检查,完整生成和训练会较慢。
- DCU 需要 DTK 与 PyTorch 构建版本匹配。已在 DTK 25.04.2 和
torch 2.5.1+das.opt1.dtk25042环境中验证单样本生成。
下载模型包
hf download OneScience-Group/Mattergen
cd Mattergen
安装运行环境
DCU 环境
# Activate DTK and conda first
module load compiler/dtk/25.04.2
conda create -n onescience311 python=3.11 -y
conda activate onescience311
# uv installation is also supported
pip install onescience[matchem-dcu] \
-i http://mirrors.onescience.ai:3141/pypi/simple/ \
--trusted-host mirrors.onescience.ai
GPU 环境
# Activate conda first
conda create -n onescience311 python=3.11 -y \
libstdcxx-ng=12 libgcc-ng=12 gcc_linux-64=12 gxx_linux-64=12
conda activate onescience311
# uv installation is also supported
pip install onescience[matchem-gpu] \
-i http://mirrors.onescience.ai:3141/pypi/simple/ \
--trusted-host mirrors.onescience.ai
训练数据
MatterGen 训练使用转换后的数据缓存。MP-20 数据集可直接从 Hugging Face 下载。
hf download OneScience-Group/mp20 --repo-type dataset
下载的 MatterGen 缓存位于以下路径。
datasets/mp20/data/MP20/cache/mp_20/
├── train/
├── val/
└── test/
默认训练 YAML 需在 args 下添加实际路径后,才能使用已下载的数据。例如,按以下方式更新 demo/configs/train_8dcu.yaml。
args:
data_module: mp_20
data_module.root_dir: ./datasets/mp20/data/MP20/cache/mp_20
进行属性微调时,还需添加 data_module.root_dir,位置是 args 下,配置文件为 demo/configs/finetune_dft_mag_density_smoke.yaml。如果数据下载在其他位置,请使用相应的绝对路径。
首先转换自定义 CSV 数据。
python csv_to_dataset.py \
--csv-folder /path/to/csv_folder \
--dataset-name my_dataset \
--cache-folder ./datasets/cache
训练权重
通过上述 Hugging Face 命令下载模型包后,预训练模型权重(检查点)位于 weight/。
weight/
├── mattergen_base/
│ ├── config.yaml
│ └── checkpoints/
│ └── last.ckpt
└── dft_mag_density/
├── config.yaml
└── checkpoints/
└── last.ckpt
本仓库还提供针对 chemical_system、dft_band_gap、dft_mag_density、ml_bulk_modulus 和 space_group 的属性条件模型权重(检查点)。使用示例 YAML 时,将 checkpoint(位于 demo/configs/generate_base.yaml 或 demo/configs/generate_dft_mag_density.yaml 中)设置为对应的 ./weight/<model_name> 路径。进行微调时,更新 adapter.model_path(位于 demo/configs/finetune_dft_mag_density_smoke.yaml 中)。
推理(inference)
无条件生成一个晶体结构。
python generate.py \
--checkpoint ./weight/mattergen_base \
--output outputs/generate/mattergen_base \
--batch-size 1 \
--num-batches 1
生成的输出包括
outputs/generate/mattergen_base/
├── generated_crystals.extxyz
└── generated_crystals_cif.zip
以磁密度为条件生成需要匹配的属性模型权重(检查点)。
python generate.py \
--checkpoint ./weight/dft_mag_density \
--output outputs/generate/dft_mag_density \
--batch-size 1 \
--num-batches 1 \
--properties '{"dft_mag_density": 0.15}'
也可使用示例 YAML 文件。
cd demo
bash run.sh --config configs/generate_base.yaml
bash run.sh --config configs/generate_dft_mag_density.yaml
训练
使用 MP-20 缓存提交八 DCU 训练作业。
cd demo
bash run.sh --config configs/train_8dcu.yaml --submit
也可在仓库根目录使用 Hydra 参数直接启动训练。
python train.py \
data_module=mp_20 \
data_module.root_dir=./datasets/mp20/data/MP20/cache/mp_20 \
trainer.devices=1 \
data_module.batch_size.train=4
微调
本仓库提供用于磁密度属性微调的冒烟测试配置。默认仅运行一个训练批次和一个验证批次。
cd demo
bash run.sh --config configs/finetune_dft_mag_density_smoke.yaml
进行完整微调时,复制该 YAML 文件,并更新 adapter.model_path、data_module.properties、属性嵌入、批大小和训练轮数。删除 trainer.limit_train_batches 和 trainer.limit_val_batches。
OneScience 官方资源
| 平台 | OneScience 主仓库 | Skills 仓库 |
|---|---|---|
| Gitee | https://gitee.com/onescience-ai/onescience | https://gitee.com/onescience-ai/oneskills |
| GitHub | https://github.com/onescience-ai/OneScience | https://github.com/onescience-ai/oneskills |
引用与许可证
- MatterGen 相关代码来自 OneScience 项目的 MatChem 示例,并参考上游 MatterGen 项目 (https://github.com/microsoft/mattergen)。上游 MatterGen 代码按 MIT 许可证发布。
- 如果在研究中使用 MatterGen 的训练或生成结果,请引用 MatterGen 原始论文、相关 OneScience 项目,以及所用数据集。
- Downloads last month
- 15