Implementation:Liu00222 Open Prompt Injection compute conditional probability
| Knowledge Sources | |
|---|---|
| Domains | NLP, Language_Modeling |
| Last Updated | 2026-02-14 15:00 GMT |
Overview
Concrete function for computing conditional log-probabilities of target text given a prefix using GPT-2, provided by the PromptLocate module.
Description
The compute_conditional_probability function concatenates the condition and target text, tokenizes them, runs a forward pass through the GPT-2 model, and extracts log-probabilities for only the target token positions. It handles truncation to 1024 tokens (GPT-2 maximum context) and returns both average and total log-probability.
Usage
Called by `causal_influence` to compute `P(suffix|prefix)` and `P(suffix|prefix+injected)`. Not typically called directly by users.
Code Reference
Source Location
- Repository: Open-Prompt-Injection
- File: OpenPromptInjection/apps/PromptLocate.py
- Lines: L123-151
Signature
def compute_conditional_probability(condition_text, target_text, tokenizer, model):
"""
Compute conditional log-probability of target_text given condition_text.
Args:
condition_text (str): Conditioning prefix text.
target_text (str): Target text to compute probability for.
tokenizer: GPT-2 tokenizer.
model: GPT-2 model on CUDA.
Returns:
tuple[float, float]: (avg_log_prob, total_log_prob).
avg_log_prob: Average log-probability per target token.
total_log_prob: Sum of log-probabilities for all target tokens.
Truncates combined input to 1024 tokens max.
"""
Import
from OpenPromptInjection.apps.PromptLocate import compute_conditional_probability
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| condition_text | str | Yes | Conditioning prefix text |
| target_text | str | Yes | Target text to compute probability for |
| tokenizer | PreTrainedTokenizer | Yes | GPT-2 tokenizer |
| model | PreTrainedModel | Yes | GPT-2 model on CUDA |
Outputs
| Name | Type | Description |
|---|---|---|
| avg_log_prob | float | Average log-probability per target token (more negative = less likely) |
| total_log_prob | float | Sum of log-probabilities for all target tokens |
Usage Examples
Computing Conditional Probability
from OpenPromptInjection.apps.PromptLocate import compute_conditional_probability
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("gpt2")
model = AutoModelForCausalLM.from_pretrained("gpt2").cuda()
condition = "The weather is sunny today."
target = " The temperature is 75 degrees."
avg_logp, total_logp = compute_conditional_probability(condition, target, tokenizer, model)
print(f"Avg log-prob: {avg_logp:.4f}")
print(f"Total log-prob: {total_logp:.4f}")