BiEncoderModel

class lightning_ir.bi_encoder.model.BiEncoderModel(config: BiEncoderConfig, *args, **kwargs)[source]

Bases: LightningIRModel

__init__(config: BiEncoderConfig, *args, **kwargs) None[source]

A bi-encoder model that encodes queries and documents separately and computes a relevance score between them using a ScoringFunction. See BiEncoderConfig for configuration options.

Parameters:

config (BiEncoderConfig) – Configuration for the bi-encoder model

Raises:

ValueError – If a projection is used but the hidden size of the backbone encoder and embedding dim of the bi-encoder model do not match

Methods

__init__(config, *args, **kwargs)

A bi-encoder model that encodes queries and documents separately and computes a relevance score between them using a ScoringFunction.

encode(encoding[, expansion, ...])

Encodes a batched tokenized text sequences and returns the embeddings and scoring mask.

encode_doc(encoding)

Encodes tokenized documents.

encode_query(encoding)

Encodes tokenized queries.

forward(query_encoding, doc_encoding[, num_docs])

Embeds queries and/or documents and computes relevance scores between them if both are provided.

from_pretrained(model_name_or_path, *args, ...)

Loads a pretrained model. Wraps the transformers.PreTrainedModel.from_pretrained method and to return a

get_output_embeddings()

Returns the output embeddings of the model for tieing the input and output embeddings.

score(query_embeddings, doc_embeddings[, ...])

Compute relevance scores between queries and documents.

scoring_mask(encoding[, expansion, ...])

Computes a scoring for batched tokenized text sequences which is used in the scoring function to mask out vectors during scoring.

Attributes

ALLOW_SUB_BATCHING

Flag to allow mini batches of documents for a single query.

config_class

Configuration class for the bi-encoder model.

alias of BiEncoderConfig

encode(encoding: BatchEncoding, expansion: bool = False, pooling_strategy: 'first' | 'mean' | 'max' | 'sum' | None = None, mask_scoring_input_ids: Tensor | None = None) BiEncoderEmbedding[source]

Encodes a batched tokenized text sequences and returns the embeddings and scoring mask.

Parameters:
  • encoding (BatchEncoding) – Tokenizer encodings for the text sequence

  • expansion (bool, optional) – Whether mask expansion was applied to the text sequence, defaults to False

  • pooling_strategy (Literal['first', 'mean', 'max', 'sum'] | None, optional) – Strategy to pool token embeddings into a single embedding. If None no pooling is applied, defaults to None

  • mask_scoring_input_ids (torch.Tensor | None, optional) – Which token_ids to mask out during scoring, defaults to None

Returns:

Embeddings and scoring mask

Return type:

BiEncoderEmbedding

encode_doc(encoding: BatchEncoding) BiEncoderEmbedding[source]

Encodes tokenized documents.

Parameters:

encoding (BatchEncoding) – Tokenizer encodings for the documents

Returns:

Query embeddings and scoring mask

Return type:

BiEncoderEmbedding

encode_query(encoding: BatchEncoding) BiEncoderEmbedding[source]

Encodes tokenized queries.

Parameters:

encoding (BatchEncoding) – Tokenizer encodings for the queries

Returns:

Query embeddings and scoring mask

Return type:

BiEncoderEmbedding

forward(query_encoding: BatchEncoding | None, doc_encoding: BatchEncoding | None, num_docs: Sequence[int] | int | None = None) BiEncoderOutput[source]

Embeds queries and/or documents and computes relevance scores between them if both are provided.

Parameters:
  • query_encoding (BatchEncoding | None) – Tokenizer encodings for the queries

  • doc_encoding (BatchEncoding | None) – Tokenizer encodings for the documents

  • num_docs (Sequence[int] | int | None, optional) – Specifies how many documents are passed per query. If a sequence of integers, len(num_doc) should be equal to the number of queries and sum(num_docs) equal to the number of documents, i.e., the sequence contains one value per query specifying the number of documents for that query. If an integer, assumes an equal number of documents per query. If None, tries to infer the number of documents by dividing the number of documents by the number of queries, defaults to None

Returns:

Output of the model

Return type:

BiEncoderOutput

classmethod from_pretrained(model_name_or_path: str | Path, *args, **kwargs) LightningIRModel
Loads a pretrained model. Wraps the transformers.PreTrainedModel.from_pretrained method and to return a

derived LightningIRModel. See LightningIRModelClassFactory for more details.

param model_name_or_path:

Name or path of the pretrained model

type model_name_or_path:

str | Path

raises ValueError:

If called on the abstract class LightningIRModel and no config is passed

return:

A derived LightningIRModel consisting of a backbone model and a LightningIRModel mixin

rtype:

LightningIRModel

>>> # Loading using model class and backbone checkpoint
>>> type(CrossEncoderModel.from_pretrained("bert-base-uncased"))
<class 'lightning_ir.base.class_factory.CrossEncoderBertModel'>
>>> # Loading using base class and backbone checkpoint
>>> type(LightningIRModel.from_pretrained("bert-base-uncased", config=CrossEncoderConfig()))
<class 'lightning_ir.base.class_factory.CrossEncoderBertModel'>
get_output_embeddings() Module | None[source]

Returns the output embeddings of the model for tieing the input and output embeddings. Returns None if no MLM head is used for projection.

Returns:

Output embeddings of the model

Return type:

torch.nn.Module | None

score(query_embeddings: BiEncoderEmbedding, doc_embeddings: BiEncoderEmbedding, num_docs: Sequence[int] | int | None = None) Tensor[source]

Compute relevance scores between queries and documents.

Parameters:
  • query_embeddings (BiEncoderEmbedding) – Embeddings and scoring mask for the queries

  • doc_embeddings (BiEncoderEmbedding) – Embeddings and scoring mask for the documents

  • num_docs (Sequence[int] | int | None, optional) – Specifies how many documents are passed per query. If a sequence of integers, len(num_doc) should be equal to the number of queries and sum(num_docs) equal to the number of documents, i.e., the sequence contains one value per query specifying the number of documents for that query. If an integer, assumes an equal number of documents per query. If None, tries to infer the number of documents by dividing the number of documents by the number of queries, defaults to None

Returns:

Relevance scores

Return type:

torch.Tensor

scoring_mask(encoding: BatchEncoding, expansion: bool = False, pooling_strategy: 'first' | 'mean' | 'max' | 'sum' | None = None, mask_scoring_input_ids: Tensor | None = None) Tensor[source]

Computes a scoring for batched tokenized text sequences which is used in the scoring function to mask out vectors during scoring.

Parameters:
  • encoding (BatchEncoding) – Tokenizer encodings for the text sequence

  • expansion (bool, optional) – Whether or not mask expansion was applied to the tokenized sequence, defaults to False

  • pooling_strategy (Literal['first', 'mean', 'max', 'sum'] | None, optional) – Which pooling strategy is pool the embeddings, defaults to None

  • mask_scoring_input_ids (torch.Tensor | None, optional) – Sequence of token_ids which should be masked during scoring, defaults to None

Returns:

Scoring mask

Return type:

torch.Tensor