Yang Jeong Park, Mayank Kumaran, Chia-Wei Hsu, Elsa Olivetti, Ju Li
Massachusetts Institute of Technology and University of Illinois Urbana-Champaign
This is the official implementation of Contrastive Language–Crystal Pretraining (CLaC). In this work, we introduce a contrastive pretraining framework for multimodal representation learning between materials science knowledge and crystal structures. CLaC aligns 3D crystal graphs with natural language descriptions by jointly training a crystal graph neural network and a text encoder in a shared embedding space. To overcome the scarcity and bias of literature-derived text for crystalline materials, the model is pretrained on property-conditioned synthetic narratives generated by large language models and grounded in computed materials properties. Through CLIP-style contrastive learning with both inter-modal and intra-modal objectives, CLaC learns semantically structured representations that enable zero-shot multimodal understanding and language-guided crystal retrieval and inverse design, without task-specific fine-tuning.
CLaC-revision/
├── asset/ # Images and architecture diagrams
├── config/ # Configuration files (model, training, data)
├── data/ # Data loading and augmentation modules
├── model/ # Model definitions (CLaC, graph encoders, text encoders)
├── evaluation/ # Evaluation and analysis scripts
│ ├── compute_metrics.py # Main evaluation (zero-shot, inverse design)
│ ├── compute_metrics_baseline.py # LLM baseline evaluation
│ ├── analyze_text_embedding_similarity.py # Text embedding analysis
│ ├── visualize_embeddings.py # UMAP visualization
│ └── plot_*.py # Various plotting utilities
├── scripts/ # Executable bash scripts
│ ├── evaluation/ # Evaluation scripts
│ ├── visualization/ # Visualization scripts
│ └── training/ # Training scripts
├── tools/ # Utility scripts and data preparation
│ ├── run_manual_sweep.py
│ └── query_materials.py
├── docs/ # Documentation
│ ├── guides/ # Usage guides
│ └── evaluation/ # Evaluation methodology
├── outputs/ # All experimental results
│ ├── figures/ # Publication-ready figures
│ ├── component_comparison/
│ ├── inverse_design_dual/
│ ├── text_embedding_similarity/
│ └── umap_visualizations/
├── train.py # Main training script
├── sweep.yaml # Hyperparameter sweep configuration
└── readme.md # This file
- Operating System: Linux (tested on Ubuntu 20.04, 22.04), macOS (experimental support)
- Python: 3.10, 3.11 (tested on 3.10.12, 3.11.5)
- CUDA: 12.1 or 12.6 (tested on 12.1, 12.6)
- PyTorch: 2.8.0
- PyTorch Geometric: 2.6.1
- Core Dependencies:
orb-models==0.5.5(Crystal graph neural network encoder)jarvis-tools(Materials science utilities)transformers(Hugging Face transformers for text encoding)lightning(PyTorch Lightning for training)hydra-core(Configuration management)wandb(Experiment tracking)spacy(Text processing, requiresen_core_web_smmodel)umap-learn(Dimensionality reduction for visualization)matplotlib,scikit-learn,scipy,pandas,python-dotenv
- GPU: NVIDIA GPU with 16GB+ VRAM recommended (tested on NVIDIA A100 40GB, RTX 3090 24GB, V100 32GB)
- Minimum: NVIDIA GPU with 12GB VRAM (training with smaller batch sizes)
- For inference/evaluation only: 8GB VRAM sufficient
- RAM: 32GB+ system RAM recommended (minimum 16GB)
- Storage: 50GB+ free disk space (for dataset, model checkpoints, and outputs)
- CPU: Multi-core processor (8+ cores recommended for data loading)
The software has been successfully tested on:
- Ubuntu 22.04 LTS + CUDA 12.6 + PyTorch 2.8.0 + NVIDIA A6000 (48GB)
- Ubuntu 22.04 LTS + CUDA 12.1 + PyTorch 2.8.0 + NVIDIA RTX 3090 (24GB)
- Python 3.10+
- CUDA 12.6 (or adjust installation commands for your CUDA version)
- Conda or Miniconda
1. Create and activate conda environment:
conda create -n clac python=3.10
conda activate clac2. Install PyTorch with CUDA support:
# For CUDA 12.6 (recommended)
pip install torch==2.8.0 torchvision==0.23.0 --index-url https://download.pytorch.org/whl/cu126
# For CUDA 12.1
pip install torch==2.8.0 torchvision==0.23.0 --index-url https://download.pytorch.org/whl/cu1213. Install PyTorch Geometric and extensions:
# For CUDA 12.6
pip install torch-geometric==2.6.1
pip install pyg-lib torch-scatter torch-sparse torch-cluster torch-spline-conv \
-f https://data.pyg.org/whl/torch-2.8.0+cu126.html
# For CUDA 12.1, use: torch-2.8.0+cu121.html4. Install all remaining dependencies:
pip install -r requirements.txt5. Download spaCy language model:
python -m spacy download en_core_web_smconda env create -f environment.yml
conda activate clac
# Install PyTorch with CUDA support
pip install torch==2.8.0 torchvision==0.23.0 --index-url https://download.pytorch.org/whl/cu126
# Install PyTorch Geometric and extensions
pip install torch-geometric==2.6.1
pip install pyg-lib torch-scatter torch-sparse torch-cluster torch-spline-conv \
-f https://data.pyg.org/whl/torch-2.8.0+cu126.html
# Download spaCy language model
python -m spacy download en_core_web_smNote: Both PyTorch and PyTorch Geometric require CUDA-specific wheels, so they must be installed separately with the correct CUDA version. The requirements.txt and environment.yml include all other dependencies.
Test your installation:
python -c "import torch; import torch_geometric; import orb_models; print('✓ Installation successful')"CUDA version mismatch:
- Check your CUDA version:
nvcc --versionornvidia-smi - Install matching PyTorch version from PyTorch website
PyTorch Geometric installation issues:
- Ensure PyTorch is installed first
- Use the correct wheel URL for your CUDA version
- See PyG installation guide
Package conflicts:
- If you encounter dependency conflicts, create a fresh conda environment
- Ensure you follow the installation order: PyTorch → PyTorch Geometric → other packages
- Method 1 (Step-by-step): Approximately 15-30 minutes on a standard desktop computer with good internet connection
- Conda environment creation: 1-2 minutes
- PyTorch installation: 5-10 minutes (depending on internet speed)
- PyTorch Geometric and extensions: 5-10 minutes
- Remaining dependencies: 5-10 minutes
- Method 2 (environment.yml): Approximately 20-40 minutes (conda resolves all dependencies automatically)
Note: Installation time may vary depending on internet connection speed and system specifications.
We have proposed the following data configurations for training CLaC:
- Academic paper dataset: Text extracted from real research papers using materials as queries.
- Synthesized Text: Text generated by leveraging the inference capabilities of LLMs based on material property values extracted from open materials databases.
We appreciate your understanding that we are unable to fully share all data and the corresponding pretrained model weights due to potential copyright concerns regarding academic literature. However, the synthesized data is publicly available at GPT-Narratives-for-Materials.
Download the synthesized GPT narratives dataset:
# Create data directory
mkdir -p datafiles
# Download dataset from HuggingFace
wget https://huggingface.co/datasets/yjeong/GPT-Narratives-for-Materials/resolve/main/mp_3d_2020_gpt_narratives.parquet \
-O datafiles/mp_3d_2020_gpt_narratives.parquet
# Alternative: using curl
curl -L https://huggingface.co/datasets/yjeong/GPT-Narratives-for-Materials/resolve/main/mp_3d_2020_gpt_narratives.parquet \
-o datafiles/mp_3d_2020_gpt_narratives.parquetSplit into train/val/test sets:
python data/get_splits.py --input datafiles/mp_3d_2020_gpt_narratives.parquet --train-ratio 0.8 --val-ratio 0.1This creates:
datafiles/mp_3d_2020_gpt_narratives_train.parquet(80%)datafiles/mp_3d_2020_gpt_narratives_val.parquet(10%)datafiles/mp_3d_2020_gpt_narratives_test.parquet(10%)
To train the CLaC model, specify the base dataset path (e.g., datafiles/mp_3d_2020_gpt_narratives) in the configuration. The training code will automatically recognize and use the split files (_train.parquet, _val.parquet, _test.parquet) for training, validation, and testing.
See yaml files in config section. If you want to change training configuration, modify them before running.
Training code is based on pytorch lightning and hydra configuration.
To pretrain the CLaC model, run python train.py with appropriate configuration.
On 4x NVIDIA A6000 GPUs, training for 5 epochs takes approximately 24 hours.
To continue training from a saved checkpoint:
# config/config.yaml
resume_from_checkpoint: outputs/experiment/epoch=49-step=5000.ckptOr via command line:
python train.py resume_from_checkpoint=outputs/experiment/epoch=49-step=5000.ckptSee docs/guides/how_to_resume_training.md for details.
In this work, we measured zero-shot ability of our CLaC. For zero-shot retrieval, we measured the model's performance in finding corresponding crystals when given texts, and vice versa. For zero-shot multimodal understanding, we utilized multiple-choice questions (included in GPT-Narratives-for-Materials) regarding crystals, such as their composition and structure.
Zero-shot retrieval and QA:
python evaluation/compute_metrics.py \
--evaluation-method 'zero-shot retrieval' \
--label text \
--model-ckpt outputs/your-model/checkpoint.ckpt \
--device cuda:0Inverse design:
python evaluation/compute_metrics.py \
--evaluation-method inverse-design \
--property "band gap" \
--k-values 3 10 100 \
--model-ckpt outputs/your-model/checkpoint.ckpt \
--device cuda:0Batch evaluation scripts:
# Run all zero-shot evaluations
bash scripts/evaluation/zero_shot_qa_evaluation.sh
# Run inverse design evaluation
bash scripts/evaluation/inverse_design_evaluation.shFor detailed evaluation methodology and usage, see docs/evaluation/.
For comprehensive guides on various features and evaluations:
Usage Guides (docs/guides/):
- How to use formula replacement
- How to resume training
- How to run ablation studies
- How to run hyperparameter sweeps
- Inverse design usage guide
- Text-only inverse design guide
- Text embedding similarity analysis
- Threshold embedding analysis
- UMAP visualization guide
- Zero-shot QA with LLM baselines
- Query materials database
Evaluation Documentation (docs/evaluation/):
