LightningIRModel

class lightning_ir.base.model.LightningIRModel(config: LightningIRConfig, *args, **kwargs)[source]

Bases: PreTrainedModel

Base class for Lightning IR models. Derived classes implement the forward method for handling query and document embeddings. It acts as mixin for a transformers.PreTrainedModel backbone model.

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

Initializes the model.

Parameters:

config (LightningIRConfig) – Configuration class for the model

Methods

__init__(config, *args, **kwargs)

Initializes the model.

forward(*args, **kwargs)

Forward method of the model.

from_pretrained(model_name_or_path, *args, ...)

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

pooling(embeddings, attention_mask, ...)

Helper method to apply pooling to the embeddings.

sparsification(embeddings[, ...])

Helper method to apply sparsification to the embeddings.

Attributes

ALLOW_SUB_BATCHING

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

training

ALLOW_SUB_BATCHING = True

Flag to allow mini batches of documents for a single query. Set to false for listwise models to ensure correctness.

config_class

Configuration class for the model.

alias of LightningIRConfig

forward(*args, **kwargs) LightningIROutput[source]

Forward method of the model. Must be implemented by the derived class.

classmethod from_pretrained(model_name_or_path: str | Path, *args, **kwargs) Self[source]
Loads a pretrained model. Wraps the transformers.PreTrainedModel.from_pretrained method 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'>
pooling(embeddings: Tensor, attention_mask: Tensor | None, pooling_strategy: Literal['first', 'mean', 'max', 'sum'] | None) Tensor[source]

Helper method to apply pooling to the embeddings.

Parameters:
  • embeddings (torch.Tensor) – Query or document embeddings

  • attention_mask (torch.Tensor | None) – Query or document attention mask

  • pooling_strategy (Literal['first', 'mean', 'max', 'sum'] | None) – The pooling strategy. No pooling is applied if None.

Raises:

ValueError – If an unknown pooling strategy is passed

Returns:

(Optionally) pooled embeddings

Return type:

torch.Tensor

sparsification(embeddings: Tensor, sparsification_strategy: Literal['relu', 'relu_log'] | None = None) Tensor[source]

Helper method to apply sparsification to the embeddings.

Parameters:
  • embeddings (torch.Tensor) – Query or document embeddings

  • sparsification_strategy (Literal['relu', 'relu_log'] | None, optional) – The sparsification strategy. No sparsification is applied if None, defaults to None

Raises:

ValueError – If an unknown sparsification strategy is passed

Returns:

(Optionally) sparsified embeddings

Return type:

torch.Tensor