Modules — Sentence Transformers documentation (original) (raw)

sentence_transformers.sparse_encoder.modules defines different building blocks, that can be used to create SparseEncoder networks from scratch. For more details, see Training Overview.

See also the modules from sentence_transformers.base.modules in Base > Modules.

SPLADE Pooling

class sentence_transformers.sparse_encoder.modules.SpladePooling(pooling_strategy: Literal['max', 'sum'] = 'max', activation_function: Literal['relu', 'log1p_relu'] = 'relu', embedding_dimension: int | None = None, chunk_size: int | None = None)[source]

SPLADE Pooling module for creating the sparse embeddings.

This module implements the SPLADE pooling mechanism that:

  1. Takes token logits from a masked language model (MLM).
  2. Applies a sparse transformation using an activation function followed by log1p (i.e., log(1 + activation(MLM_logits))).
  3. Applies a pooling strategy max or sum to produce sparse embeddings.

The resulting embeddings are highly sparse and capture lexical information, making them suitable for efficient information retrieval.

Parameters:

SparseAutoEncoder

class sentence_transformers.sparse_encoder.modules.SparseAutoEncoder(input_dim: int, hidden_dim: int = 512, k: int = 8, k_aux: int = 512, normalize: bool = False, dead_threshold: int = 30)[source]

This module implements the Sparse AutoEncoder architecture based on the paper: Beyond Matryoshka: Revisiting Sparse Coding for Adaptive Representation, https://huggingface.co/papers/2503.01776

This module transforms dense embeddings into sparse representations by:

  1. Applying a multi-layer feed-forward network
  2. Applying top-k sparsification to keep only the largest values
  3. Supporting auxiliary losses for training stability (via k_aux parameter)

Parameters:

SparseStaticEmbedding

class sentence_transformers.sparse_encoder.modules.SparseStaticEmbedding(tokenizer: PreTrainedTokenizer, weight: torch.Tensor | None = None, frozen: bool = False)[source]

SparseStaticEmbedding module for efficient sparse representations.

This lightweight module computes sparse representations by mapping input tokens to static weights, such as IDF (Inverse Document Frequency) weights. It is designed to encode queries or documents into fixed-size embeddings based on the presence of tokens in the input.

A common scenario is to use this module for encoding queries, and using a heavier module like SPLADE (Transformer with “fill-mask” + SpladePooling) for document encoding.

Parameters: