-
Notifications
You must be signed in to change notification settings - Fork 126
Add Example for training ColBERT using Pylate in terms of contrastive way #164
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
Open
sigridjineth
wants to merge
9
commits into
AnswerDotAI:main
Choose a base branch
from
sigridjineth:patch-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cadb7e0
add train_pylate_contrastive.py
sigridjineth d92e5e5
Update examples/train_pylate_contrastive.py
sigridjineth 88ea5f3
Update examples/train_pylate_contrastive.py
sigridjineth 9fba67f
Update examples/train_pylate_contrastive.py
sigridjineth 75d19df
Update examples/train_pylate_contrastive.py
sigridjineth 425df2f
Update examples/train_pylate_contrastive.py
sigridjineth c41cd6b
Update examples/train_pylate_contrastive.py
sigridjineth bf81243
Renaming and consistency with kd script
ea42756
Use bf16
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import torch | ||
from datasets import load_dataset | ||
from sentence_transformers import ( | ||
SentenceTransformerTrainer, | ||
SentenceTransformerTrainingArguments, | ||
) | ||
|
||
from pylate import evaluation, losses, models, utils | ||
|
||
# Add at the start of your train_pylate_contrastive.py | ||
import torch | ||
sigridjineth marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
torch._inductor.config.fallback_random = True | ||
sigridjineth marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
torch._inductor.config.triton.unique_kernel_names = True | ||
sigridjineth marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
# Or completely disable torch compile | ||
sigridjineth marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
# model.forward = torch.compile(model.forward, mode="max-autotune", fullgraph=True) | ||
sigridjineth marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
# Define model parameters for contrastive training | ||
model_name = "answerdotai/ModernBERT-large" # Choose the pre-trained model you want to use as base | ||
batch_size = 64 # Larger batch size often improves results, but requires more memory | ||
|
||
num_train_epochs = 5 # Adjust based on your requirements | ||
# Set the run name for logging and output directory | ||
run_name = "contrastive-sigrid-241226" | ||
output_dir = f"output/{run_name}" | ||
|
||
# 1. Here we define our ColBERT model. If not a ColBERT model, will add a linear layer to the base encoder. | ||
model = models.ColBERT(model_name_or_path=model_name) | ||
|
||
# Load dataset | ||
dataset = load_dataset("sentence-transformers/msmarco-bm25", "triplet", split="train") | ||
# Split the dataset (this dataset does not have a validation set, so we split the training set) | ||
splits = dataset.train_test_split(test_size=0.01) | ||
train_dataset = splits["train"] | ||
eval_dataset = splits["test"] | ||
|
||
# Define the loss function | ||
train_loss = losses.Contrastive(model=model) | ||
|
||
# Initialize the evaluator | ||
dev_evaluator = evaluation.ColBERTTripletEvaluator( | ||
anchors=eval_dataset["query"], | ||
positives=eval_dataset["positive"], | ||
negatives=eval_dataset["negative"], | ||
) | ||
|
||
# Configure the training arguments (e.g., batch size, evaluation strategy, logging steps) | ||
args = SentenceTransformerTrainingArguments( | ||
output_dir=output_dir, | ||
num_train_epochs=num_train_epochs, | ||
per_device_train_batch_size=batch_size, | ||
per_device_eval_batch_size=batch_size, | ||
fp16=True, # Set to False if you get an error that your GPU can't run on FP16 | ||
bf16=False, # Set to True if you have a GPU that supports BF16 | ||
run_name=run_name, # Will be used in W&B if `wandb` is installed | ||
learning_rate=3e-6, | ||
) | ||
|
||
# Initialize the trainer for the contrastive training | ||
trainer = SentenceTransformerTrainer( | ||
model=model, | ||
args=args, | ||
train_dataset=train_dataset, | ||
eval_dataset=eval_dataset, | ||
loss=train_loss, | ||
evaluator=dev_evaluator, | ||
data_collator=utils.ColBERTCollator(model.tokenize), | ||
) | ||
# Start the training process | ||
trainer.train() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.