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

Simplify boilerplate for monoT5 and monoBERT #83

Merged
merged 3 commits into from
Sep 13, 2020
Merged
Changes from 1 commit
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
40 changes: 40 additions & 0 deletions pygaggle/rerank/pretrained.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import torch

from transformers import (AutoTokenizer,
AutoModelForSequenceClassification,
T5ForConditionalGeneration)

from .base import Reranker
from .transformer import (SequenceClassificationTransformerReranker,
T5Reranker)
from pygaggle.model import T5BatchTokenizer


__all__ = ['monoT5',
'monoBERT']


def monoT5(model_name: str = 'castorini/monot5-base-msmarco',
tokenizer_name: str = 't5-base',
batch_size: int = 8) -> Reranker:

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

model = T5ForConditionalGeneration.from_pretrained(model_name)
model = model.to(device).eval()

tokenizer = AutoTokenizer.from_pretrained(tokenizer_name)
tokenizer = T5BatchTokenizer(tokenizer, batch_size)
return T5Reranker(model, tokenizer)


def monoBERT(model_name: str = 'castorini/monobert-large-msmarco',
tokenizer_name: str = 'bert-large-uncased') -> Reranker:

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

model = AutoModelForSequenceClassification.from_pretrained(model_name)
model = model.to(device).eval()

tokenizer = AutoTokenizer.from_pretrained(tokenizer_name)
return SequenceClassificationTransformerReranker(model, tokenizer)