xberg-gliner 1.3.10

GLiNER inference used by xberg NER: span-mode ONNX runtime plus an optional Candle GLiNER2 backend.
Documentation
//! `schema_gather`; non-parametric utility head.

use candle_core::{Result, Tensor};

/// Stateless schema-gather head. Holds no parameters.
pub struct SchemaGather;

/// Result of [`SchemaGather::forward`].
pub struct SchemaGatherOutput {
    /// `[1, H]`; the `[P]` token's hidden state (prompt context).
    pub pc_emb: Tensor,
    /// `[F, H]`; per-field / per-label embeddings.
    pub field_embs: Tensor,
}

impl SchemaGather {
    /// `schema_indices` includes the `[P]` index first, followed by all
    /// per-field `[E]` indices; matches `schema_positions` order from
    /// `crate::encode_v2`.
    pub fn forward(&self, hidden_states: &Tensor, schema_indices: &Tensor) -> Result<SchemaGatherOutput> {
        let h = hidden_states.squeeze(0)?;
        let all = h.index_select(schema_indices, 0)?;

        let pc_emb = all.narrow(0, 0, 1)?;
        let n = all.dim(0)?;
        let hidden_dim = all.dim(1)?;
        let field_embs = if n > 1 {
            all.narrow(0, 1, n - 1)?
        } else {
            Tensor::zeros((0, hidden_dim), all.dtype(), all.device())?
        };

        Ok(SchemaGatherOutput { pc_emb, field_embs })
    }
}