gliner.modeling.base moduleΒΆ
Base model classes for GLiNER neural network architectures.
This module provides abstract base classes and concrete implementations for various encoder-decoder architectures used in named entity recognition (NER) and relation extraction tasks.
- Classes:
BaseModel: Abstract base class for all models. BaseUniEncoderModel: Base class for uni-encoder architectures. UniEncoderSpanModel: Span-based NER model with uni-encoder. UniEncoderTokenModel: Token-based NER model with uni-encoder. BaseBiEncoderModel: Base class for bi-encoder architectures. BiEncoderSpanModel: Span-based NER model with bi-encoder. BiEncoderTokenModel: Token-based NER model with bi-encoder. UniEncoderSpanDecoderModel: Span model with decoder for label generation. UniEncoderSpanRelexModel: Span model with relation extraction capabilities.
- class gliner.modeling.base.BaseModel(config, from_pretrained=False, cache_dir=None)[source]ΒΆ
Bases:
ABC,ModuleAbstract base class for all GLiNER models.
This class defines the common interface and shared functionality for all model architectures. It includes methods for padding/truncating sequences and computing losses with various masking strategies.
- data_processorΒΆ
Data processor for handling input preprocessing.
- configΒΆ
Model configuration object.
- from_pretrainedΒΆ
Whether model was loaded from pretrained weights.
- Type:
bool
- cache_dirΒΆ
Directory for caching pretrained models.
- Type:
Optional[Path]
Initialize the base model.
- Parameters:
config (Any) β Configuration object containing model hyperparameters.
from_pretrained (bool) β Whether to load from pretrained weights.
cache_dir (str | Path | None) β Directory path for caching pretrained models.
- data_processor = NoneΒΆ
- __init__(config, from_pretrained=False, cache_dir=None)[source]ΒΆ
Initialize the base model.
- Parameters:
config (Any) β Configuration object containing model hyperparameters.
from_pretrained (bool) β Whether to load from pretrained weights.
cache_dir (str | Path | None) β Directory path for caching pretrained models.
- abstract get_representations()[source]ΒΆ
Get intermediate representations from the model.
- Returns:
Tuple of tensors representing intermediate model outputs.
- Return type:
Tuple[Tensor, β¦]
- class gliner.modeling.base.BaseUniEncoderModel(config, from_pretrained=False, cache_dir=None)[source]ΒΆ
Bases:
BaseModelBase class for uni-encoder model architectures.
Uni-encoder models use a single encoder for both text and entity labels, embedding them in the same semantic space.
- rnnΒΆ
Optional LSTM layer for sequence modeling.
- Type:
Optional[LstmSeq2SeqEncoder]
- cross_fuserΒΆ
Optional cross-attention fusion layer.
- Type:
Optional[CrossFuser]
Initialize the uni-encoder model.
- Parameters:
config (Any) β Model configuration object.
from_pretrained (bool) β Whether to load from pretrained weights.
cache_dir (str | Path | None) β Directory for caching pretrained models.
- __init__(config, from_pretrained=False, cache_dir=None)[source]ΒΆ
Initialize the uni-encoder model.
- Parameters:
config (Any) β Model configuration object.
from_pretrained (bool) β Whether to load from pretrained weights.
cache_dir (str | Path | None) β Directory for caching pretrained models.
- get_representations(input_ids=None, attention_mask=None, text_lengths=None, words_mask=None, **kwargs)[source]ΒΆ
Get entity label and word representations from input.
- Parameters:
input_ids (FloatTensor | None) β Input token IDs of shape (B, L).
attention_mask (LongTensor | None) β Attention mask of shape (B, L).
text_lengths (Tensor | None) β Length of each text in batch.
words_mask (LongTensor | None) β Word boundary mask.
**kwargs (Any) β Additional arguments for the encoder.
- Returns:
prompts_embedding: Entity label embeddings of shape (B, C, D).
prompts_embedding_mask: Mask for prompts of shape (B, C).
words_embedding: Word embeddings of shape (B, W, D).
mask: Mask for words of shape (B, W).
- Return type:
Tuple containing
- class gliner.modeling.base.UniEncoderSpanModel(config, from_pretrained=False, cache_dir=None)[source]ΒΆ
Bases:
BaseUniEncoderModelSpan-based NER model using uni-encoder architecture.
This model identifies entity spans by scoring all possible spans against entity type embeddings.
- span_rep_layerΒΆ
Layer for computing span representations.
- Type:
- prompt_rep_layerΒΆ
Projection layer for entity label embeddings.
- Type:
nn.Module
Initialize the span-based uni-encoder model.
- Parameters:
config (Any) β Model configuration object.
from_pretrained (bool) β Whether to load from pretrained weights.
cache_dir (str | Path | None) β Directory for caching pretrained models.
- __init__(config, from_pretrained=False, cache_dir=None)[source]ΒΆ
Initialize the span-based uni-encoder model.
- Parameters:
config (Any) β Model configuration object.
from_pretrained (bool) β Whether to load from pretrained weights.
cache_dir (str | Path | None) β Directory for caching pretrained models.
- forward(input_ids=None, attention_mask=None, words_embedding=None, mask=None, prompts_embedding=None, prompts_embedding_mask=None, words_mask=None, text_lengths=None, span_idx=None, span_mask=None, labels=None, **kwargs)[source]ΒΆ
Forward pass through the span-based model.
- Parameters:
input_ids (FloatTensor | None) β Input token IDs of shape (B, L).
attention_mask (LongTensor | None) β Attention mask of shape (B, L).
words_embedding (FloatTensor | None) β Pre-computed word embeddings of shape (B, W, D).
mask (LongTensor | None) β Mask for words of shape (B, W).
prompts_embedding (FloatTensor | None) β Pre-computed entity label embeddings of shape (B, C, D).
prompts_embedding_mask (LongTensor | None) β Mask for prompts of shape (B, C).
words_mask (LongTensor | None) β Word boundary mask.
text_lengths (Tensor | None) β Length of each text sequence.
span_idx (LongTensor | None) β Span indices of shape (B, L*K, 2).
span_mask (LongTensor | None) β Mask for valid spans of shape (B, L, K).
labels (FloatTensor | None) β Ground truth labels of shape (B, L, K, C).
**kwargs (Any) β Additional arguments.
- Returns:
GLiNERBaseOutput containing logits, loss, and intermediate representations.
- Return type:
- loss(scores, labels, prompts_embedding_mask, mask_label, alpha=-1.0, gamma=0.0, prob_margin=0.0, label_smoothing=0.0, reduction='sum', negatives=1.0, masking='none', **kwargs)[source]ΒΆ
Compute span classification loss.
- Parameters:
scores (Tensor) β Predicted scores of shape (B, L, K, C).
labels (Tensor) β Ground truth labels of shape (B, L, K, C).
prompts_embedding_mask (Tensor) β Mask for valid entity types of shape (B, C).
mask_label (Tensor) β Mask for valid spans of shape (B, L, K).
alpha (float) β Focal loss alpha parameter.
gamma (float) β Focal loss gamma parameter.
prob_margin (float) β Margin for probability adjustment.
label_smoothing (float) β Label smoothing factor.
reduction (str) β Loss reduction method (βsumβ or βmeanβ).
negatives (float) β Negative sampling probability.
masking (str) β Masking strategy for negative sampling.
**kwargs (Any) β Additional arguments.
- Returns:
Scalar loss tensor.
- Return type:
Tensor
- class gliner.modeling.base.UniEncoderTokenModel(config, from_pretrained=False, cache_dir=None)[source]ΒΆ
Bases:
BaseUniEncoderModelToken-based NER model using uni-encoder architecture.
This model classifies each word independently as entity type or non-entity.
Initialize the token-based uni-encoder model.
- Parameters:
config (Any) β Model configuration object.
from_pretrained (bool) β Whether to load from pretrained weights.
cache_dir (str | Path | None) β Directory for caching pretrained models.
- __init__(config, from_pretrained=False, cache_dir=None)[source]ΒΆ
Initialize the token-based uni-encoder model.
- Parameters:
config (Any) β Model configuration object.
from_pretrained (bool) β Whether to load from pretrained weights.
cache_dir (str | Path | None) β Directory for caching pretrained models.
- forward(input_ids=None, attention_mask=None, words_embedding=None, mask=None, prompts_embedding=None, prompts_embedding_mask=None, words_mask=None, text_lengths=None, labels=None, **kwargs)[source]ΒΆ
Forward pass through the token-based model.
- Parameters:
input_ids (FloatTensor | None) β Input token IDs of shape (B, L).
attention_mask (LongTensor | None) β Attention mask of shape (B, L).
words_embedding (FloatTensor | None) β Pre-computed word embeddings of shape (B, W, D).
mask (LongTensor | None) β Mask for words of shape (B, W).
prompts_embedding (FloatTensor | None) β Pre-computed entity label embeddings of shape (B, C, D).
prompts_embedding_mask (LongTensor | None) β Mask for prompts of shape (B, C).
words_mask (LongTensor | None) β Word boundary mask.
text_lengths (Tensor | None) β Length of each text sequence.
labels (FloatTensor | None) β Ground truth labels of shape (B, W, C).
**kwargs (Any) β Additional arguments.
- Returns:
GLiNERBaseOutput containing logits, loss, and intermediate representations.
- Return type:
- loss(scores, labels, prompts_embedding_mask, mask, alpha=-1.0, gamma=0.0, prob_margin=0.0, label_smoothing=0.0, reduction='sum', negatives=1.0, **kwargs)[source]ΒΆ
Compute token classification loss.
- Parameters:
scores (Tensor) β Predicted scores of shape (B, W, C).
labels (Tensor) β Ground truth labels of shape (B, W, C).
prompts_embedding_mask (Tensor) β Mask for valid entity types of shape (B, C).
mask (Tensor) β Mask for valid tokens of shape (B, W).
alpha (float) β Focal loss alpha parameter.
gamma (float) β Focal loss gamma parameter.
prob_margin (float) β Margin for probability adjustment.
label_smoothing (float) β Label smoothing factor.
reduction (str) β Loss reduction method (βsumβ or βmeanβ).
negatives (float) β Negative sampling probability.
**kwargs (Any) β Additional arguments.
- Returns:
Scalar loss tensor.
- Return type:
Tensor
- class gliner.modeling.base.BaseBiEncoderModel(config, from_pretrained=False, cache_dir=None)[source]ΒΆ
Bases:
BaseModelBase class for bi-encoder model architectures.
Bi-encoder models use separate encoders for text and entity labels, allowing independent encoding before fusion.
- rnnΒΆ
Optional LSTM layer.
- Type:
Optional[LstmSeq2SeqEncoder]
- cross_fuserΒΆ
Optional cross-attention fusion layer.
- Type:
Optional[CrossFuser]
Initialize the bi-encoder model.
- Parameters:
config (Any) β Model configuration object.
from_pretrained (bool) β Whether to load from pretrained weights.
cache_dir (str | Path | None) β Directory for caching pretrained models.
- __init__(config, from_pretrained=False, cache_dir=None)[source]ΒΆ
Initialize the bi-encoder model.
- Parameters:
config (Any) β Model configuration object.
from_pretrained (bool) β Whether to load from pretrained weights.
cache_dir (str | Path | None) β Directory for caching pretrained models.
- features_enhancement(text_embeds, labels_embeds, text_mask=None, labels_mask=None)[source]ΒΆ
Enhance features using cross-attention fusion.
- Parameters:
text_embeds (Tensor) β Text embeddings of shape (B, W, D).
labels_embeds (Tensor) β Label embeddings of shape (B, C, D).
text_mask (Tensor | None) β Mask for text of shape (B, W).
labels_mask (Tensor | None) β Mask for labels of shape (B, C).
- Returns:
Enhanced text embeddings of shape (B, W, D).
Enhanced label embeddings of shape (B, C, D).
- Return type:
Tuple containing
- get_representations(input_ids=None, attention_mask=None, labels_embeds=None, labels_input_ids=None, labels_attention_mask=None, text_lengths=None, words_mask=None, **kwargs)[source]ΒΆ
Get entity label and word representations using bi-encoder.
- Parameters:
input_ids (FloatTensor | None) β Input token IDs of shape (B, L).
attention_mask (LongTensor | None) β Attention mask of shape (B, L).
labels_embeds (FloatTensor | None) β Pre-computed label embeddings of shape (C, D).
labels_input_ids (FloatTensor | None) β Label token IDs for encoding.
labels_attention_mask (LongTensor | None) β Attention mask for labels.
text_lengths (Tensor | None) β Length of each text in batch.
words_mask (LongTensor | None) β Word boundary mask.
**kwargs (Any) β Additional arguments for the encoder.
- Returns:
labels_embeds: Label embeddings of shape (B, C, D).
labels_mask: Mask for labels of shape (B, C).
words_embedding: Word embeddings of shape (B, W, D).
mask: Mask for words of shape (B, W).
- Return type:
Tuple containing
- class gliner.modeling.base.BiEncoderSpanModel(config, from_pretrained=False, cache_dir=None)[source]ΒΆ
Bases:
BaseBiEncoderModelSpan-based NER model using bi-encoder architecture.
- span_rep_layerΒΆ
Layer for computing span representations.
- Type:
- prompt_rep_layerΒΆ
Projection layer for entity label embeddings.
- Type:
nn.Module
Initialize the span-based bi-encoder model.
- Parameters:
config (Any) β Model configuration object.
from_pretrained (bool) β Whether to load from pretrained weights.
cache_dir (str | Path | None) β Directory for caching pretrained models.
- __init__(config, from_pretrained=False, cache_dir=None)[source]ΒΆ
Initialize the span-based bi-encoder model.
- Parameters:
config (Any) β Model configuration object.
from_pretrained (bool) β Whether to load from pretrained weights.
cache_dir (str | Path | None) β Directory for caching pretrained models.
- forward(input_ids=None, attention_mask=None, labels_embeds=None, labels_input_ids=None, labels_attention_mask=None, words_embedding=None, mask=None, prompts_embedding=None, prompts_embedding_mask=None, words_mask=None, text_lengths=None, span_idx=None, span_mask=None, labels=None, **kwargs)[source]ΒΆ
Forward pass through the bi-encoder span model.
- Parameters:
input_ids (FloatTensor | None) β Input token IDs of shape (B, L).
attention_mask (LongTensor | None) β Attention mask of shape (B, L).
labels_embeds (FloatTensor | None) β Pre-computed label embeddings of shape (C, D).
labels_input_ids (FloatTensor | None) β Label token IDs for encoding.
labels_attention_mask (LongTensor | None) β Attention mask for labels.
words_embedding (FloatTensor | None) β Pre-computed word embeddings.
mask (LongTensor | None) β Mask for words.
prompts_embedding (FloatTensor | None) β Pre-computed entity label embeddings.
prompts_embedding_mask (LongTensor | None) β Mask for prompts.
words_mask (LongTensor | None) β Word boundary mask.
text_lengths (Tensor | None) β Length of each text sequence.
span_idx (LongTensor | None) β Span indices of shape (B, L*K, 2).
span_mask (LongTensor | None) β Mask for valid spans of shape (B, L, K).
labels (FloatTensor | None) β Ground truth labels of shape (B, L, K, C).
**kwargs (Any) β Additional arguments.
- Returns:
GLiNERBaseOutput containing logits, loss, and intermediate representations.
- Return type:
- loss(scores, labels, prompts_embedding_mask, mask_label, alpha=-1.0, gamma=0.0, label_smoothing=0.0, reduction='sum', negatives=1.0, masking='none', **kwargs)[source]ΒΆ
Compute span classification loss for bi-encoder.
- Parameters:
scores (Tensor) β Predicted scores of shape (B, L, K, C).
labels (Tensor) β Ground truth labels of shape (B, L, K, C).
prompts_embedding_mask (Tensor) β Mask for valid entity types of shape (B, C).
mask_label (Tensor) β Mask for valid spans of shape (B, L, K).
alpha (float) β Focal loss alpha parameter.
gamma (float) β Focal loss gamma parameter.
label_smoothing (float) β Label smoothing factor.
reduction (str) β Loss reduction method (βsumβ or βmeanβ).
negatives (float) β Negative sampling probability.
masking (str) β Masking strategy for negative sampling.
**kwargs (Any) β Additional arguments.
- Returns:
Scalar loss tensor.
- Return type:
Tensor
- class gliner.modeling.base.BiEncoderTokenModel(config, from_pretrained=False, cache_dir=None)[source]ΒΆ
Bases:
BaseBiEncoderModelToken-based NER model using bi-encoder architecture.
Initialize the token-based bi-encoder model.
- Parameters:
config (Any) β Model configuration object.
from_pretrained (bool) β Whether to load from pretrained weights.
cache_dir (str | Path | None) β Directory for caching pretrained models.
- __init__(config, from_pretrained=False, cache_dir=None)[source]ΒΆ
Initialize the token-based bi-encoder model.
- Parameters:
config (Any) β Model configuration object.
from_pretrained (bool) β Whether to load from pretrained weights.
cache_dir (str | Path | None) β Directory for caching pretrained models.
- forward(input_ids=None, attention_mask=None, labels_embeds=None, labels_input_ids=None, labels_attention_mask=None, words_embedding=None, mask=None, prompts_embedding=None, prompts_embedding_mask=None, words_mask=None, text_lengths=None, labels=None, **kwargs)[source]ΒΆ
Forward pass through the bi-encoder token model.
- Parameters:
input_ids (FloatTensor | None) β Input token IDs of shape (B, L).
attention_mask (LongTensor | None) β Attention mask of shape (B, L).
labels_embeds (FloatTensor | None) β Pre-computed label embeddings of shape (C, D).
labels_input_ids (FloatTensor | None) β Label token IDs for encoding.
labels_attention_mask (LongTensor | None) β Attention mask for labels.
words_embedding (FloatTensor | None) β Pre-computed word embeddings.
mask (LongTensor | None) β Mask for words.
prompts_embedding (FloatTensor | None) β Pre-computed entity label embeddings.
prompts_embedding_mask (LongTensor | None) β Mask for prompts.
words_mask (LongTensor | None) β Word boundary mask.
text_lengths (Tensor | None) β Length of each text sequence.
labels (FloatTensor | None) β Ground truth labels of shape (B, W, C).
**kwargs (Any) β Additional arguments.
- Returns:
GLiNERBaseOutput containing logits, loss, and intermediate representations.
- Return type:
- loss(scores, labels, prompts_embedding_mask, mask, alpha=-1.0, gamma=0.0, prob_margin=0.0, label_smoothing=0.0, reduction='sum', negatives=1.0, **kwargs)[source]ΒΆ
Compute token classification loss for bi-encoder.
- Parameters:
scores (Tensor) β Predicted scores of shape (B, W, C).
labels (Tensor) β Ground truth labels of shape (B, W, C).
prompts_embedding_mask (Tensor) β Mask for valid entity types of shape (B, C).
mask (Tensor) β Mask for valid tokens of shape (B, W).
alpha (float) β Focal loss alpha parameter.
gamma (float) β Focal loss gamma parameter.
prob_margin (float) β Margin for probability adjustment.
label_smoothing (float) β Label smoothing factor.
reduction (str) β Loss reduction method (βsumβ or βmeanβ).
negatives (float) β Negative sampling probability.
**kwargs (Any) β Additional arguments.
- Returns:
Scalar loss tensor.
- Return type:
Tensor
- class gliner.modeling.base.UniEncoderSpanDecoderModel(config, from_pretrained=False, cache_dir=None)[source]ΒΆ
Bases:
UniEncoderSpanModelSpan-based model with decoder for generating entity type labels.
This model extends the span-based approach by adding a decoder that can generate entity type labels as sequences, enabling more flexible entity typing.
- _enc2dec_projΒΆ
Projection layer if encoder and decoder dimensions differ.
- Type:
Optional[nn.Module]
Initialize the span-decoder model.
- Parameters:
config (Any) β Model configuration object.
from_pretrained (bool) β Whether to load from pretrained weights.
cache_dir (str | Path | None) β Directory for caching pretrained models.
- __init__(config, from_pretrained=False, cache_dir=None)[source]ΒΆ
Initialize the span-decoder model.
- Parameters:
config (Any) β Model configuration object.
from_pretrained (bool) β Whether to load from pretrained weights.
cache_dir (str | Path | None) β Directory for caching pretrained models.
- select_decoder_embedding(representations, rep_mask)[source]ΒΆ
Select and pack valid representations for decoder input.
Keeps only representations where mask == 1 and optionally projects them to the decoder hidden size.
- Parameters:
representations (FloatTensor) β Tensor of shape (B, N, D) containing embeddings.
rep_mask (LongTensor) β Tensor of shape (B, N) where 1 indicates valid position.
- Returns:
target_rep: FloatTensor of shape (B, M, D) with kept representations.
target_mask: LongTensor of shape (B, M) with 1 for valid, 0 for pad.
sel_idx: LongTensor of shape (B, M) with original column indices (-1 for padding positions).
- Return type:
Tuple containing
- get_raw_decoder_inputs(representations, rep_mask)[source]ΒΆ
Extract valid span tokens for decoder input.
- Parameters:
representations (Tensor) β Span representations of shape (B, S, T, D).
rep_mask (Tensor) β Mask of shape (B, S, T).
- Returns:
span_tokens: Valid span tokens of shape (M, T, D).
span_tokens_mask: Mask for span tokens of shape (M, T).
- Return type:
Tuple containing
- decode_labels(decoder_embedding=None, decoder_embedding_mask=None, decoder_labels_ids=None, decoder_labels_mask=None, decoder_labels=None, **kwargs)[source]ΒΆ
Decode labels using the decoder in teacher forcing mode.
- Parameters:
decoder_embedding (FloatTensor | None) β Span token embeddings of shape (B, N, T, D).
decoder_embedding_mask (LongTensor | None) β Mask for span tokens of shape (B, N, T).
decoder_labels_ids (FloatTensor | None) β Label token IDs of shape (M, L).
decoder_labels_mask (LongTensor | None) β Mask for labels of shape (M, L).
decoder_labels (FloatTensor | None) β Ground truth labels for loss computation of shape (M, L).
**kwargs (Any) β Additional arguments.
- Returns:
loss: Cross-entropy loss scalar.
decoder_outputs: Decoder logits of shape (M, S+L-1, V).
- Return type:
Tuple containing
- generate_labels(decoder_embedding=None, decoder_embedding_mask=None, max_new_tokens=32, eos_token_id=None, pad_token_id=None, temperature=1.0, do_sample=False, num_return_sequences=1, labels_trie=None, **kwargs)[source]ΒΆ
Generate entity type labels from decoder embeddings.
- Parameters:
decoder_embedding (FloatTensor | None) β Span token embeddings of shape (B, N, D).
decoder_embedding_mask (LongTensor | None) β Mask for span tokens of shape (B, N).
max_new_tokens (int) β Maximum number of tokens to generate.
eos_token_id (int | None) β End-of-sequence token ID.
pad_token_id (int | None) β Padding token ID.
temperature (float) β Sampling temperature.
do_sample (bool) β Whether to use sampling instead of greedy decoding.
num_return_sequences (int) β Number of sequences to generate per input.
labels_trie (Any | None) β Optional trie for constrained decoding.
**kwargs (Any) β Additional generation arguments.
- Returns:
Generated token IDs of shape (M, L).
- Return type:
Tensor
- select_span_decoder_embedding(prompts_embedding, prompts_embedding_mask, span_rep, span_scores, span_mask, decoder_text_embeds=None, decoder_words_mask=None, span_labels=None, threshold=0.5, top_k=None, decoder_input_ids=None, decoder_labels_ids=None)[source]ΒΆ
Select span embeddings for decoder input based on predictions or labels.
This method selects which spans to provide to the decoder, either based on ground truth labels (during training) or model predictions (during inference). It can operate in two modes: 1. βpromptβ mode: Uses entity type embeddings as decoder input. 2. βspanβ mode: Uses contextualized tokens within each span as decoder input.
- Parameters:
prompts_embedding (Tensor) β Entity type embeddings of shape (B, C, D).
prompts_embedding_mask (Tensor) β Mask for prompts of shape (B, C).
span_rep (Tensor) β Span representations of shape (B, L, K, D).
span_scores (Tensor) β Span classification scores of shape (B, L, K, C).
span_mask (Tensor) β Mask for valid spans of shape (B, L, K).
decoder_text_embeds (Tensor | None) β Text embeddings for span mode of shape (B, T, D).
decoder_words_mask (Tensor | None) β Word position mask of shape (B, T).
span_labels (Tensor | None) β Ground truth labels of shape (B, L, K, C).
threshold (float) β Confidence threshold for selecting spans.
top_k (int | None) β Optional limit on number of spans to select.
decoder_input_ids (Tensor | None) β Debugging parameter for input IDs.
decoder_labels_ids (Tensor | None) β Debugging parameter for label IDs.
- Returns:
span_rep_kept: Selected span embeddings for decoder of shape (B, S, T, D) or None if no valid spans.
span_msk: Mask for selected spans of shape (B, S, T) or None.
span_sel_idx: Original indices of selected spans of shape (B, S) or None.
- Return type:
Tuple containing
- forward(input_ids=None, attention_mask=None, decoder_input_ids=None, decoder_attention_mask=None, decoder_labels_ids=None, decoder_labels_mask=None, decoder_words_mask=None, words_embedding=None, mask=None, prompts_embedding=None, prompts_embedding_mask=None, words_mask=None, text_lengths=None, span_idx=None, span_mask=None, labels=None, decoder_labels=None, threshold=0.5, **kwargs)[source]ΒΆ
Forward pass through the span-decoder model.
- Parameters:
input_ids (FloatTensor | None) β Input token IDs of shape (B, L).
attention_mask (LongTensor | None) β Attention mask of shape (B, L).
decoder_input_ids (FloatTensor | None) β Decoder input IDs for span mode.
decoder_attention_mask (LongTensor | None) β Decoder attention mask.
decoder_labels_ids (FloatTensor | None) β Label token IDs for decoding of shape (M, L).
decoder_labels_mask (LongTensor | None) β Mask for decoder labels of shape (M, L).
decoder_words_mask (LongTensor | None) β Word position mask for span mode.
words_embedding (FloatTensor | None) β Pre-computed word embeddings.
mask (LongTensor | None) β Mask for words.
prompts_embedding (FloatTensor | None) β Pre-computed entity type embeddings.
prompts_embedding_mask (LongTensor | None) β Mask for prompts.
words_mask (LongTensor | None) β Word boundary mask.
text_lengths (Tensor | None) β Length of each text sequence.
span_idx (LongTensor | None) β Span indices of shape (B, L*K, 2).
span_mask (LongTensor | None) β Mask for valid spans of shape (B, L, K).
labels (FloatTensor | None) β Ground truth span labels of shape (B, L, K, C).
decoder_labels (FloatTensor | None) β Ground truth decoder labels of shape (M, L).
threshold (float | None) β Confidence threshold for span selection.
**kwargs (Any) β Additional arguments.
- Returns:
GLiNERDecoderOutput containing logits, losses, and decoder information.
- Return type:
- loss(scores, labels, prompts_embedding_mask, mask_label, alpha=-1.0, gamma=0.0, label_smoothing=0.0, reduction='sum', negatives=1.0, masking='none', decoder_loss=None, **kwargs)[source]ΒΆ
Compute combined loss for span classification and decoder.
- Parameters:
scores (Tensor) β Predicted span scores of shape (B, L, K, C).
labels (Tensor) β Ground truth span labels of shape (B, L, K, C).
prompts_embedding_mask (Tensor) β Mask for valid entity types of shape (B, C).
mask_label (Tensor) β Mask for valid spans of shape (B, L, K).
alpha (float) β Focal loss alpha parameter.
gamma (float) β Focal loss gamma parameter.
label_smoothing (float) β Label smoothing factor.
reduction (str) β Loss reduction method (βsumβ or βmeanβ).
negatives (float) β Negative sampling probability.
masking (str) β Masking strategy for negative sampling.
decoder_loss (Tensor | None) β Optional decoder loss to combine with span loss.
**kwargs (Any) β Additional arguments.
- Returns:
Scalar combined loss tensor.
- Return type:
Tensor
- class gliner.modeling.base.UniEncoderSpanRelexModel(config, from_pretrained=False, cache_dir=None)[source]ΒΆ
Bases:
UniEncoderSpanModelSpan-based NER model with relation extraction capabilities.
This model extends span-based NER to also extract relations between identified entities, predicting both entity types and relation types in a joint model.
- relations_rep_layerΒΆ
Layer for computing pairwise entity relations (adjacency matrix).
- Type:
Optional[RelationsRepLayer]
- triples_score_layerΒΆ
Layer for scoring (head, relation, tail) triples.
- Type:
Optional[TriplesScoreLayer]
- pair_rep_layerΒΆ
Alternative layer for relation scoring via concatenation.
- Type:
Optional[nn.Module]
Initialize the span-based relation extraction model.
- Parameters:
config (Any) β Model configuration object.
from_pretrained (bool) β Whether to load from pretrained weights.
cache_dir (str | Path | None) β Directory for caching pretrained models.
- __init__(config, from_pretrained=False, cache_dir=None)[source]ΒΆ
Initialize the span-based relation extraction model.
- Parameters:
config (Any) β Model configuration object.
from_pretrained (bool) β Whether to load from pretrained weights.
cache_dir (str | Path | None) β Directory for caching pretrained models.
- select_span_target_embedding(span_rep, span_scores, span_mask, span_labels=None, threshold=0.5, top_k=None)[source]ΒΆ
Select entity spans for relation extraction.
Filters spans based on entity classification scores or ground truth labels, keeping only high-confidence or positive entity spans for relation modeling.
- Parameters:
span_rep (FloatTensor) β Span representations of shape (B, L, K, D).
span_scores (FloatTensor) β Span classification scores of shape (B, L, K, C).
span_mask (LongTensor) β Mask for valid spans of shape (B, L, K).
span_labels (FloatTensor | None) β Optional ground truth labels of shape (B, L, K, C).
threshold (float) β Confidence threshold for selecting spans.
top_k (int | None) β Optional limit on number of spans to select.
- Returns:
target_rep: Selected span representations of shape (B, E, D).
target_mask: Mask for selected spans of shape (B, E).
- Return type:
Tuple containing
- select_target_embedding(representations=None, rep_mask=None)[source]ΒΆ
Pack valid representations by removing masked positions.
- Parameters:
representations (FloatTensor | None) β Tensor of shape (B, N, D).
rep_mask (LongTensor | None) β Mask of shape (B, N) where 1 indicates valid position.
- Returns:
target_rep: Packed representations of shape (B, M, D).
target_mask: Packed mask of shape (B, M).
- Return type:
Tuple containing
- forward(input_ids=None, attention_mask=None, words_embedding=None, mask=None, prompts_embedding=None, prompts_embedding_mask=None, words_mask=None, text_lengths=None, span_idx=None, span_mask=None, labels=None, adj_matrix=None, rel_matrix=None, threshold=0.5, adjacency_threshold=0.5, **kwargs)[source]ΒΆ
Forward pass through the relation extraction model.
- Parameters:
input_ids (FloatTensor | None) β Input token IDs of shape (B, L).
attention_mask (LongTensor | None) β Attention mask of shape (B, L).
words_embedding (FloatTensor | None) β Pre-computed word embeddings.
mask (LongTensor | None) β Mask for words.
prompts_embedding (FloatTensor | None) β Pre-computed entity type embeddings.
prompts_embedding_mask (LongTensor | None) β Mask for entity types.
words_mask (LongTensor | None) β Word boundary mask.
text_lengths (Tensor | None) β Length of each text sequence.
span_idx (LongTensor | None) β Span indices of shape (B, L*K, 2).
span_mask (LongTensor | None) β Mask for valid spans of shape (B, L, K).
labels (FloatTensor | None) β Ground truth entity labels of shape (B, L, K, C).
adj_matrix (FloatTensor | None) β Ground truth adjacency matrix of shape (B, E, E).
rel_matrix (FloatTensor | None) β Ground truth relation labels of shape (B, N, C_rel).
threshold (float | None) β Confidence threshold for entity selection.
adjacency_threshold (float | None) β Threshold for relation adjacency.
**kwargs (Any) β Additional arguments.
- Returns:
GLiNERRelexOutput containing entity and relation predictions.
- Return type:
- adj_loss(logits, labels, adj_mask, alpha=-1.0, gamma=0.0, prob_margin=0.0, label_smoothing=0.0, reduction='sum', masking='span', negatives=1.0, **kwargs)[source]ΒΆ
Compute adjacency matrix loss for entity pair relations.
- Parameters:
logits (Tensor) β Predicted adjacency scores of shape (B, E, E).
labels (Tensor) β Ground truth adjacency of shape (B, E, E).
adj_mask (Tensor) β Mask for valid entity pairs of shape (B, E, E).
alpha (float) β Focal loss alpha parameter.
gamma (float) β Focal loss gamma parameter.
prob_margin (float) β Margin for probability adjustment.
label_smoothing (float) β Label smoothing factor.
reduction (str) β Loss reduction method (βsumβ or βmeanβ).
masking (str) β Masking strategy for negative sampling.
negatives (float) β Negative sampling probability.
**kwargs (Any) β Additional arguments.
- Returns:
Scalar adjacency loss tensor.
- Return type:
Tensor
- rel_loss(logits, labels, pair_mask, class_mask, alpha=-1.0, gamma=0.0, prob_margin=0.0, label_smoothing=0.0, reduction='sum', negatives=1.0, masking='span', **kwargs)[source]ΒΆ
Compute relation classification loss for selected entity pairs.
- Parameters:
logits (Tensor) β Predicted relation scores of shape (B, N, C).
labels (Tensor) β Ground truth relation labels of shape (B, N, C).
pair_mask (Tensor) β Mask for valid pairs of shape (B, N, C).
class_mask (Tensor) β Mask for valid relation classes of shape (B, N, C).
alpha (float) β Focal loss alpha parameter.
gamma (float) β Focal loss gamma parameter.
prob_margin (float) β Margin for probability adjustment.
label_smoothing (float) β Label smoothing factor.
reduction (str) β Loss reduction method (βsumβ or βmeanβ).
negatives (float) β Negative sampling probability.
masking (str) β Masking strategy for negative sampling.
**kwargs (Any) β Additional arguments.
- Returns:
Scalar relation classification loss tensor.
- Return type:
Tensor