| # Spectra-Reason-GCD Implementation |
|
|
| This document details the implementation of the Spectra-Reason-GCD pipeline, which integrates a spectral encoder (DreaMS + Perceiver Resampler), an LLM reasoning engine (Llama-3-8B with LoRA), and grammar-constrained decoding (GCD) for valid SMILES generation. |
|
|
| ## 1. Environment Setup |
|
|
| Use the provided `specrag` conda environment. |
|
|
| ```bash |
| source /cluster/tufts/liulab/lib/anaconda3/etc/profile.d/conda.sh |
| conda activate specrag |
| ``` |
|
|
| ## 2. Directory Structure |
|
|
| ``` |
| Spec-RAG/ |
| ├── data/ # Dataset JSONL files |
| ├── grammars/ |
| │ └── smiles.ebnf # SMILES grammar for GCD |
| ├── scripts/ |
| │ ├── train_stage1_bridge.py |
| │ ├── train_stage2_cot_lora.py |
| │ ├── generate_gcd.py |
| │ ├── create_test_spectra.py |
| │ └── ... |
| ├── spec_rag/ # Python package |
| │ ├── cot_system2.py # Chain-of-Thought logic |
| │ ├── perceiver_resampler.py |
| │ ├── spectra_reason_encoder.py |
| │ ├── gcd_inference.py |
| │ └── ... |
| └── out/ # Checkpoints and logs |
| ``` |
|
|
| ## 3. Training Pipeline |
|
|
| ### Stage 1: Encoder Pre-training (Optional but Recommended) |
| Trains the Perceiver Resampler to bridge DreaMS embeddings to the LLM's embedding space. |
|
|
| ```bash |
| accelerate launch scripts/train_stage1_bridge.py \ |
| --train-jsonl data/train.jsonl \ |
| --output-dir out/stage1 \ |
| --dreams-ckpt /cluster/tufts/liulab/yiwan01/SpecBridge/data/ssl_model.ckpt \ |
| --llm-name meta-llama/Meta-Llama-3-8B-Instruct \ |
| --epochs 5 \ |
| --batch-size 8 \ |
| --mixed_precision bf16 |
| ``` |
|
|
| ### Stage 2: End-to-End CoT Fine-tuning |
| Fine-tunes the LLM (LoRA) and Perceiver Resampler jointly using Chain-of-Thought data. |
|
|
| ```bash |
| accelerate launch scripts/train_stage2_cot_lora.py \ |
| --train-jsonl data/train_cot.jsonl \ |
| --val-jsonl data/val_cot.jsonl \ |
| --output-dir out/stage2 \ |
| --dreams-ckpt /cluster/tufts/liulab/yiwan01/SpecBridge/data/ssl_model.ckpt \ |
| --stage1-perceiver out/stage1/perceiver.pt \ |
| --llm-name meta-llama/Meta-Llama-3-8B-Instruct \ |
| --epochs 3 \ |
| --batch-size 4 \ |
| --mixed_precision bf16 |
| ``` |
|
|
| **Note:** Monitor the training loss. It should decrease significantly. If loss remains high or constant, check that the `dreams-ckpt` is loading correctly and data is valid. |
|
|
| ## 4. Inference |
|
|
| Run inference on test spectra. We use Grammar-Constrained Decoding (GCD) to ensure valid SMILES output. |
|
|
| **Important:** |
| 1. Ensure `--encoder-dir` points to `out/stage2` to load the trained Perceiver. |
| 2. Use `--no-gcd` first to debug raw model output if you suspect issues. |
| 3. If output is garbage (e.g., repeating `0.0.0...`), check training loss and DreaMS loading. |
|
|
| ```bash |
| python scripts/generate_gcd.py \ |
| --input-jsonl data/test_spectra.jsonl \ |
| --output-jsonl out/predictions.jsonl \ |
| --model-dir out/stage2 \ |
| --encoder-dir out/stage2 \ |
| --dreams-ckpt /cluster/tufts/liulab/yiwan01/SpecBridge/data/ssl_model.ckpt \ |
| --grammar grammars/smiles.ebnf \ |
| --max-peaks 60 \ |
| --max-new-tokens 200 \ |
| --no-gcd # Remove this flag to enable Grammar Constraint |
| ``` |
|
|
| ## 5. Troubleshooting |
|
|
| * **Garbage Output ("0.0.0.0...", "** **..."):** |
| * This indicates the model is in a degenerate state. |
| * **Check Training:** Ensure training loss decreased. If loss was ~0.0 or constant, training failed. |
| * **Check Input:** Verify `test_spectra.jsonl` contains valid peaks (m/z, intensity). |
| * **Check DreaMS:** Ensure `dreams-ckpt` path is correct and accessible. |
| * **Empty Output:** |
| * Can happen if `max_new_tokens` is too small or EOS token is generated immediately. |
| * The inference script has been patched to correctly handle `inputs_embeds` generation output. |
| * **"No such file or directory":** |
| * Ensure you run scripts from the `Spec-RAG` root directory. |
| * Verify `data/` folder contains required JSONL files. |
|
|
| ## 6. Evaluation |
|
|
| (Use `chemprop` or `rdkit` based scripts to evaluate validity, novelty, and exact match accuracy of `out/predictions.jsonl`). |
|
|