Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added some tests for minGPT pipeline #5

Merged
merged 2 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Run Python Tests

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Check out the code
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.8' # Adjust the Python version if needed

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt # Ensure you have a `requirements.txt` file for dependencies

- name: Run tests
run: |
python -m unittest discover -s minGPT/tests -p "*.py"

Binary file added minGPT/ckpts/sample_checkpoint.pt
Binary file not shown.
16 changes: 16 additions & 0 deletions minGPT/tests/test_data/test_data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
mol_smiles conductivity
NC(=O)CSCC(CO[Cu])OC(=O)[Au] 1
CCC(F)C(=O)NC(CO[Cu])COC(=O)[Au] 0
CCSCCN(CCN[Cu])CCOC(=O)[Au] 1
C#CCN(CCOCCO[Cu])CCOC(=O)[Au] 1
CCC(COC(=O)[Au])C(=O)NCCO[Cu] 0
CC(CCOC(=O)[Au])SCCO[Cu] 1
CN(CCNC(=O)COC(=O)[Au])CCO[Cu] 1
CN(CCO[Cu])C(=O)CCOC(=O)[Au] 1
CCC(C)C(NC(=O)[Au])C(=O)NC(C)CN[Cu] 0
CCS(C)(=O)=NC(=O)C(CN[Cu])OC(=O)[Au] 0
C=CC(CNCCCOCCOC(=O)[Au])O[Cu] 1
CCC(=O)CNC(=O)C(CN[Cu])OC(=O)[Au] 0
CC(CCO[Cu])N(C)CCCCCOC(=O)[Au] 1
CC(C)CNC(CO[Cu])COC(=O)[Au] 0
NC(=O)CCC(=O)NCC(CO[Cu])OC(=O)[Au] 1
89 changes: 89 additions & 0 deletions minGPT/tests/test_pipeline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import unittest
import sys
import os
import pandas as pd
from minGPT.pipeline import minGPT

class TestMinGPTPipeline(unittest.TestCase):

def setUp(self):
self.pipeline = minGPT()
self.data_file_path = os.path.join("minGPT", "tests", "test_data", "test_data.csv")
self.model_config = self.pipeline.get_default_model_config()
self.data_config = self.pipeline.get_default_data_config()
self.data_config.file_path = self.data_file_path

def load_model_with_config(self, train_dataset):
"""Helper method to load model with vocab and block sizes."""
self.model_config.vocab_size = train_dataset.get_vocab_size()
self.model_config.block_size = train_dataset.get_block_size()
self.pipeline.load_model(self.model_config)

def test_data_preprocessing(self):
data_config = self.pipeline.get_default_data_config()
data_config.file_path = self.data_file_path
data_config.block_size = 64

raw_data = pd.read_csv(self.data_file_path, sep="\t")
print("Unprocessed Data (First 5 Rows):")
print(raw_data.head())

train_dataset, test_dataset = self.pipeline.data_preprocessing(data_config)

self.assertIsNotNone(train_dataset, "Train dataset should not be None")
self.assertIsNotNone(test_dataset, "Test dataset should not be None")
self.assertGreater(len(train_dataset), 0, "Train dataset should contain data")
self.assertGreater(len(test_dataset), 0, "Test dataset should contain data")

def test_model_loading(self):
train_dataset, _ = self.pipeline.data_preprocessing(self.data_config)
self.load_model_with_config(train_dataset)
self.assertIsNotNone(self.pipeline.model, "Model should be loaded")

def test_training(self):
train_config = self.pipeline.get_default_train_config()
train_config.max_iters = 10
train_config.device = 'cpu'

train_dataset, _ = self.pipeline.data_preprocessing(self.data_config)
self.load_model_with_config(train_dataset)

def batch_end_callback(trainer):
print(f"Batch {trainer.iter_num} ended with loss: {trainer.loss.item() if hasattr(trainer, 'loss') else 'N/A'}")

train_config.call_back = batch_end_callback
loss = self.pipeline.train(train_config)
self.assertIsNotNone(loss, "Training loss should not be None")

def test_generation(self):
generate_config = self.pipeline.get_default_generate_config()
generate_config.num_samples = 5
generate_config.ckpt_path = os.path.join("minGPT", "ckpts", "sample_checkpoint.pt")

train_dataset, _ = self.pipeline.data_preprocessing(self.data_config)
self.load_model_with_config(train_dataset)

generated_out = self.pipeline.generate(generate_config)
self.assertIsInstance(generated_out, list, "Generation output should be a list")
self.assertGreater(len(generated_out), 0, "Generated output should contain elements")
print("Generated Output:", generated_out)

def test_evaluation(self):
train_dataset, _ = self.pipeline.data_preprocessing(self.data_config)
self.pipeline.df = pd.DataFrame({"mol_smiles": ["C(=O)CSCC(CO[Cu])OC(=O)[Au]"]})

generate_config = self.pipeline.get_default_generate_config()
generate_config.num_samples = 5
generate_config.ckpt_path = os.path.join("minGPT", "ckpts", "sample_checkpoint.pt")

self.load_model_with_config(train_dataset)
generated_out = self.pipeline.generate(generate_config)
self.pipeline.gen_df = pd.DataFrame({"mol_smiles": generated_out})

results = self.pipeline.evaluate()
self.assertIsInstance(results, tuple, "Evaluation should return a tuple")
self.assertEqual(len(results), 6, "Evaluation should return six metrics")
print("Evaluation Results:", results)

if __name__ == '__main__':
unittest.main()
80 changes: 46 additions & 34 deletions minGPT_pipeline.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,27 @@
"cells": [
{
"cell_type": "code",
"execution_count": 46,
"execution_count": 1,
"id": "5593892c",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"No normalization for SPS. Feature removed!\n",
"No normalization for AvgIpc. Feature removed!\n",
"/Users/arashkajeh/Documents/GitHub/PolyGen/venv38/lib/python3.8/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n",
"Skipped loading some Tensorflow models, missing a dependency. No module named 'tensorflow'\n",
"Skipped loading modules with pytorch-geometric dependency, missing a dependency. No module named 'torch_geometric'\n",
"Skipped loading modules with pytorch-geometric dependency, missing a dependency. cannot import name 'DMPNN' from 'deepchem.models.torch_models' (/Users/arashkajeh/Documents/GitHub/PolyGen/venv38/lib/python3.8/site-packages/deepchem/models/torch_models/__init__.py)\n",
"Skipped loading modules with pytorch-lightning dependency, missing a dependency. No module named 'lightning'\n",
"Skipped loading some Jax models, missing a dependency. No module named 'jax'\n",
"Skipped loading some PyTorch models, missing a dependency. No module named 'tensorflow'\n"
]
}
],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
Expand All @@ -29,7 +46,7 @@
},
{
"cell_type": "code",
"execution_count": 47,
"execution_count": 2,
"id": "0b36bb88-5c31-47ab-bbfc-9f283898c5e8",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -59,7 +76,7 @@
},
{
"cell_type": "code",
"execution_count": 48,
"execution_count": 3,
"id": "66681958-a1f1-4fcd-8216-e2a15fc54c7c",
"metadata": {},
"outputs": [],
Expand All @@ -69,7 +86,7 @@
},
{
"cell_type": "code",
"execution_count": 49,
"execution_count": 4,
"id": "9bdf2a24-8cd4-41c7-bedd-07525412f532",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -100,7 +117,7 @@
},
{
"cell_type": "code",
"execution_count": 50,
"execution_count": null,
"id": "29b97415-56ac-4b8d-bda6-214d93ab8e15",
"metadata": {},
"outputs": [
Expand All @@ -121,7 +138,15 @@
"call_back: None\n",
"pretrain: None\n",
"\n",
"auto\n"
"auto\n",
"running on device cpu\n",
"iter_dt 0.00ms; iter 0: train loss 6.40999, val loss 6.41125\n",
"iter_dt 1052.81ms; iter 100: train loss 2.05009, val loss 2.04999\n",
"iter_dt 1192.84ms; iter 200: train loss 0.56169, val loss 0.56216\n",
"iter_dt 446.42ms; iter 300: train loss 0.34879, val loss 0.35418\n",
"iter_dt 1401.88ms; iter 400: train loss 0.30944, val loss 0.30891\n",
"iter_dt 786.11ms; iter 500: train loss 0.28507, val loss 0.28915\n",
"iter_dt 445.25ms; iter 600: train loss 0.26450, val loss 0.26419\n"
]
}
],
Expand All @@ -145,7 +170,7 @@
"\n",
"train_config.call_back = batch_end_callback\n",
"# Uncomment the following line to start training\n",
"# loss = pipeline.train(train_config)"
"loss = pipeline.train(train_config)"
]
},
{
Expand All @@ -158,23 +183,10 @@
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": null,
"id": "f72b13fe-9653-4f6b-9d9b-cc4a00f51eea",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ckpts_path: None\n",
"num_samples: 100\n",
"temperature: 1.0\n",
"task: conditional\n",
"ckpt_path: ./minGPT/ckpts/10000.pt\n",
"\n"
]
}
],
"outputs": [],
"source": [
"generate_config = pipeline.get_default_generate_config()\n",
"generate_config.ckpt_path = \"./minGPT/ckpts/10000.pt\"\n",
Expand All @@ -196,18 +208,10 @@
},
{
"cell_type": "code",
"execution_count": 52,
"execution_count": null,
"id": "5c824f5e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(0.97, 0.5800000000000001, 0.87, 0.9772727272727273, 0.2633707205965721, 0.6905273247947646)\n"
]
}
],
"outputs": [],
"source": [
"print(pipeline.evaluate())"
]
Expand All @@ -219,6 +223,14 @@
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "7540c31b-4701-490c-856c-bdf85cffc99b",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -242,4 +254,4 @@
},
"nbformat": 4,
"nbformat_minor": 5
}
}
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ transformers==4.46.0
ipykernel==6.29.5
torch==2.0.1
notebook==7.2.2
pandas==2.0.3
Loading