Skip to main content

Pretense

Pretraining sentence transformers with retrieval-oriented objectives.

Pretense supports:

  • RetroMAE
  • DupMAE
  • Condenser
  • coCondenser
  • Contriever
  • supervised pairwise contrastive training
  • Multiple Negatives Ranking Loss (MNRL)
  • cached MNRL (CMNRL)
  • supervised and unsupervised SimCSE

Training uses Hugging Face Transformers and Accelerate, accepts Hugging Face Datasets or ordinary PyTorch datasets, and produces encoders that load in Transformers and Sentence Transformers 5.x or 6.x.

Installation

uv add pretense

Python 3.10 or newer is required. CUDA-enabled PyTorch should be selected using the appropriate uv/PyTorch index for the target system.

Pretense supports PyTorch 2.2 and newer without choosing or replacing a particular CUDA build. Restoring a complete Trainer checkpoint, including optimizer and scheduler state, requires PyTorch 2.6 or newer because Transformers blocks torch.load checkpoint restoration on earlier releases. Weights-only Pretense checkpoints use safetensors and remain loadable on every supported version.

For development from a source checkout, use uv sync --extra dev instead. Install the optional Hugging Face Datasets dependency with uv add "pretense[examples]" when using the bundled examples.

Train with the Python SDK

PretenseTrainer follows the Hugging Face Trainer interface:

from transformers import AutoTokenizer

from pretense import (
    MAECollator,
    MethodConfig,
    PretenseTrainer,
    PretenseTrainingArguments,
    load_pretraining_model,
)

model_name = "google-bert/bert-base-uncased"
method = MethodConfig(name="retromae")
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = load_pretraining_model(method, model_name)

trainer = PretenseTrainer(
    model=model,
    args=PretenseTrainingArguments(
        output_dir="outputs/retromae",
        per_device_train_batch_size=16,
        learning_rate=5e-5,
        num_train_epochs=1,
    ),
    train_dataset=dataset,
    data_collator=MAECollator(tokenizer=tokenizer, text_column="text"),
    processing_class=tokenizer,
)
trainer.train()
trainer.save_model("outputs/retromae/final")

The trainer accepts the same callbacks, optimizers, schedulers, evaluation datasets, and metrics as transformers.Trainer. Ordinary transformers.TrainingArguments are also supported. When the dataset uses the standard columns shown below, the trainer can select the collator automatically; pass a collator explicitly for custom column names or explicit MNRL negatives.

For complete Python workflows, including programmatic models and Sentence Transformers fine-tuning, see the examples.

Training produces regular console and training_log.jsonl metrics and resumable checkpoint-* directories. See training and checkpointing for evaluation, retention, callbacks, experiment trackers, recovery after interruption, and exporting.

Export a trained model explicitly when it is ready for downstream use:

from pretense import export_sentence_transformer
from transformers import AutoModel
from sentence_transformers import SentenceTransformer

export_dir = export_sentence_transformer(
    trainer.model,
    tokenizer,
    "outputs/retromae/sentence-transformers",
)
sentence_model = SentenceTransformer(str(export_dir))
encoder = AutoModel.from_pretrained(export_dir / "0_Transformer")

The Sentence Transformers directory is the canonical export. It includes the complete Hugging Face backbone under 0_Transformer/, so separate copies of the model weights are unnecessary. For a Hub export, load that backbone with AutoModel.from_pretrained(repo_id, subfolder="0_Transformer").

Supported methods

Method Objectives Required input
RetroMAE encoder MLM + CLS-conditioned reconstruction text
DupMAE RetroMAE + ordinary-token bag-of-words prediction text
Condenser skip-connected head MLM + late MLM text
coCondenser Condenser + paired-span, cross-device contrastive loss documents or paired spans
Contriever augmented-view MoCo contrastive learning text
Contrastive supervised pairwise margin loss two text columns + binary label
MNRL paired retrieval ranking with in-batch and optional explicit negatives query + positive, optionally negative columns
CMNRL memory-efficient GradCache MNRL query + positive, optionally negative columns
SimCSE dropout-view or supervised NLI contrastive learning text, or premise + entailment + optional contradiction

See the method notes for input formats, configuration, pooling behavior, distributed-training caveats, and architecture support. Separate guides cover custom models and FlashAttention.

Development

uv run ruff check .
uv run mypy src/pretense
uv run pytest
uv build --no-sources

Pretense targets objective and architecture parity, not guaranteed reproduction of paper benchmark scores. See the method notes and release documentation.

Attribution

The implementation draws on the following papers and reference projects:

Cite the corresponding paper when publishing results. Pretense is licensed under Apache-2.0.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pretense-0.1.0.tar.gz (287.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pretense-0.1.0-py3-none-any.whl (32.2 kB view details)

Uploaded Python 3

File details

Details for the file pretense-0.1.0.tar.gz.

File metadata

  • Download URL: pretense-0.1.0.tar.gz
  • Upload date:
  • Size: 287.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pretense-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f21b1b44df55a5378b3afeee035d859c46e81f57d62c5916aeed3ffb20c5e217
MD5 330a97ab4284d29853de788fd6dc1d4e
BLAKE2b-256 944ae501ea63966fcb4d1fff8c84ea5acbb482c84cd5fc53014dee091eb84959

See more details on using hashes here.

File details

Details for the file pretense-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pretense-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 32.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pretense-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4f2c4e6b6152ff2f7bc9118abfc1e53b7cd665bafca6860faff76716333ff0f4
MD5 f0fda919c97914e0d6698f4063dfa0ef
BLAKE2b-256 4a92d9e1ac0066aa5afd98d4e5c5e2a8df9666528486a0417a315c994ae6baf6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page