Skip to content
Snippets Groups Projects
Commit f79e7beb authored by Vlad-Andrei BĂDOIU (78692)'s avatar Vlad-Andrei BĂDOIU (78692)
Browse files

Basis for unit testing

parent e4592038
No related branches found
No related tags found
Loading
Pipeline #55029 failed
This commit is part of merge request !18. Comments created here will be created in the context of that merge request.
...@@ -6,4 +6,6 @@ useGPU: ...@@ -6,4 +6,6 @@ useGPU:
stage: test_gpu stage: test_gpu
script: script:
- echo "Check whether we have enabled our GPU or not." - echo "Check whether we have enabled our GPU or not."
- pip install -r requirements.txt
- nvidia-smi - nvidia-smi
- export PYTHONPATH=$PYTHONPATH:.; pytest -s
import unittest
import torch
from torch import nn
from optimus.datasets import WikiText103Dataset
from optimus.tokenizers import SentencePieceTokenizer
from optimus.dataloader import OptimusDataLoader
from optimus.models import OptimusTransformer
from optimus.trainer import Trainer
class BenchmarkTest(unittest.TestCase):
def test_ms_per_batch(self):
batch_size: int = 8
grad_acc_steps: int = 1
seq_len: int = 512
lr_max: float = 1e-4
grad_clip_norm: float = 1.0
epochs: int = 1
tokenizer_path: str = 'optimus.model'
checkpoints_path: str = 'best_model.pth'
dim: int = 512
n_layers: int = 6
n_heads: int = 8
dropout: float = 0.0
device: str = 'cuda'
tok = SentencePieceTokenizer(model_path=tokenizer_path)
# load dataset splits
train_ds = WikiText103Dataset(split='test')
test_ds = WikiText103Dataset(split='valid')
# create dataloader object and move to device
dl = OptimusDataLoader(train_ds, test_ds, tok,
bs=batch_size,
seq_len=seq_len,
device=device)
# create model and move to device
model = OptimusTransformer(len(tok),
n_layers=n_layers,
dim=dim,
n_heads=n_heads,
p_drop=dropout,
weight_tying=False)
model = model.to(device)
# define loss metric and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), betas=(0.9, 0.999), eps=1e-9)
print("Starting training...")
# create trainer and start fitting
trainer = Trainer(dl=dl,
model=model,
criterion=criterion,
optimizer=optimizer,
lr=lr_max,
grad_acc_steps=grad_acc_steps,
grad_clip_norm=grad_clip_norm,
model_save_path=checkpoints_path,
progress_bar=False)
trainer.fit(epochs)
print("--- Testing results for Optimus Transfoer --- ")
print(f"Est. ms/batch : {trainer.ms_per_batch:.2f}\n")
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment