gliner.modeling.cache module¶
Session cache support for decoder-backed span inference.
- class gliner.modeling.cache.ReusableDynamicLayer(initial_capacity=256, max_cache_length=None)[source]¶
Bases:
DynamicLayerAppend-only KV storage that grows geometrically instead of every turn.
- update(key_states, value_states, *args, **kwargs)[source]¶
Update the key and value caches in-place, and return the necessary keys and value states.
- Parameters:
key_states (torch.Tensor) – The new key states to cache.
value_states (torch.Tensor) – The new value states to cache.
cache_kwargs (dict[str, Any], optional) – Additional arguments for the cache.
- Returns:
The key and value states.
- Return type:
tuple[torch.Tensor, torch.Tensor]
- get_max_cache_shape()[source]¶
Returns the maximum sequence length of the cache object. DynamicLayer does not have a maximum length.
- property max_batch_size: int¶
- property max_cache_len: int¶
- class gliner.modeling.cache.ReusableDynamicCache(config=None, initial_capacity=256, max_cache_length=None)[source]¶
Bases:
DynamicCacheDynamicCache with stable, capacity-backed tensors when supported.
- gliner.modeling.cache.create_reusable_cache(config, *, initial_capacity=256, max_cache_length=None)[source]¶
Create a session KV cache whose storage is reused between appends.
- class gliner.modeling.cache.CacheState(past_key_values=None, input_ids=<factory>, attention_mask=<factory>, token_word_mask=<factory>, past_word_embeddings=None, past_word_mask=None, prompts_embedding=None, prompts_mask=None, cached_length=0, next_position_id=None, session_id=None, labels=(), text='', tokens=<factory>, char_starts=<factory>, char_ends=<factory>, span_logits=<factory>, metadata=<factory>)[source]¶
Bases:
objectAll reusable state belonging to one streaming-span session.
- past_key_values: Any = None¶
- input_ids: torch.Tensor¶
- attention_mask: torch.Tensor¶
- token_word_mask: torch.Tensor¶
- past_word_embeddings: torch.Tensor | None = None¶
- past_word_mask: torch.Tensor | None = None¶
- prompts_embedding: torch.Tensor | None = None¶
- prompts_mask: torch.Tensor | None = None¶
- cached_length: int = 0¶
- next_position_id: int | None = None¶
- session_id: str | None = None¶
- labels: tuple[str, ...] = ()¶
- text: str = ''¶
- tokens: list[str]¶
- char_starts: list[int]¶
- char_ends: list[int]¶
- span_logits: dict[tuple[int, int], torch.Tensor]¶
- metadata: dict[str, Any]¶
- property next_position: int¶
Return the absolute decoder position for the next token.
- property word_length: int¶
Return the authoritative cached word count without a device sync.
- __init__(past_key_values=None, input_ids=<factory>, attention_mask=<factory>, token_word_mask=<factory>, past_word_embeddings=None, past_word_mask=None, prompts_embedding=None, prompts_mask=None, cached_length=0, next_position_id=None, session_id=None, labels=(), text='', tokens=<factory>, char_starts=<factory>, char_ends=<factory>, span_logits=<factory>, metadata=<factory>)¶
- class gliner.modeling.cache.SessionCacheManager[source]¶
Bases:
objectThread-safe ownership and lifecycle management for session states.
- gliner.modeling.cache.copy_past_key_values(past_kv, *, config=None, max_cache_length=None)[source]¶
Return an independent copy of a transformers-style KV cache.
Streaming cache layers append in place. Tests, transactional helpers, and dynamic batching therefore need an explicit copy operation rather than a shallow
copy.copyof the cache container.