hopwise.model.logits_processor¶
Common logits processor in recommender system
Classes¶
Abstract base class for all logit processors that can be applied during generation. |
|
This class can be used to create a list of [LogitsProcessor] to subsequently process a scores input tensor. |
|
Force the last token to be one of the force_tokens if the total length is reached, in the path generation stage |
|
Force the last token to be one of the force_tokens if the total length is reached, in the path generation stage |
|
Module Contents¶
- class hopwise.model.logits_processor.LogitsProcessor[source]¶
Abstract base class for all logit processors that can be applied during generation. Copy of HuggingFace’s LogitsProcessor.
- class hopwise.model.logits_processor.LogitsProcessorList[source]¶
Bases:
list
This class can be used to create a list of [LogitsProcessor] to subsequently process a scores input tensor. This class inherits from list and adds a specific __call__ method to apply each [LogitsProcessor] to the inputs. Copy of HuggingFace’s LogitsProcessorList.
- __call__(input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) torch.FloatTensor [source]¶
- Parameters:
input_ids (torch.LongTensor of shape (batch_size, sequence_length)) – Indices of input sequence tokens in the vocabulary. [What are input IDs?](../glossary#input-ids)
scores (torch.FloatTensor of shape (batch_size, config.vocab_size)) – Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search
kwargs (Dict[str, Any], optional) – Additional kwargs that are specific to a logits processor.
- Returns:
The processed prediction scores.
- Return type:
torch.FloatTensor of shape (batch_size, config.vocab_size)
- class hopwise.model.logits_processor.ConstrainedLogitsProcessorWordLevel(tokenized_ckg, tokenized_used_ids, max_sequence_length, tokenizer, mask_cache_size=3 * 10**4, pos_candidates_cache_size=1 * 10**5, task=KnowledgeEvaluationType.REC, **kwargs)[source]¶
Bases:
LogitsProcessor
Force the last token to be one of the force_tokens if the total length is reached, in the path generation stage this means to limit the hop size. This is a word-level constraint, does not work with piece tokenizers. If task is link prediction (LP) logit processor forces last token to reachable ones
- tokenized_ckg¶
- tokenized_used_ids¶
- max_sequence_length¶
- tokenizer¶
- bos_token_id¶
- pos_candidates_cache¶
- mask_cache¶
- task¶
- is_bos_token_in_input(input_ids)[source]¶
Check if the input contains a BOS token. Checking the first sequence is enough.
- process_scores_rec(input_ids, idx)[source]¶
Process each score based on input length and update mask list.
- class hopwise.model.logits_processor.PrefixConstrainedLogitsProcessorWordLevel(tokenized_ckg, tokenized_used_ids, max_sequence_length, tokenizer, **kwargs)[source]¶
Bases:
ConstrainedLogitsProcessorWordLevel
Force the last token to be one of the force_tokens if the total length is reached, in the path generation stage this means to limit the hop size. This is a word-level constraint, does not work with piece tokenizers. If task is link prediction (LP) logit processor forces last token to reachable ones
- mask_cache = None¶
- class hopwise.model.logits_processor.PLMLogitsProcessorWordLevel(tokenized_ckg, tokenized_used_ids, max_sequence_length, tokenizer, pos_candidates_cache_size=1 * 10**5, task=KnowledgeEvaluationType.REC, **kwargs)[source]¶
Bases:
LogitsProcessor
https://dl.acm.org/doi/pdf/10.1145/3485447.3511937 Constraint decoding strategy for PLM, it forces the model to generate alternatively entities and relations
- tokenized_ckg¶
- tokenized_used_ids¶
- max_sequence_length¶
- tokenizer¶
- bos_token_id¶
- pos_candidates_cache¶
- task¶
- entity_token_ids¶
- relation_token_ids¶