fix: Set attention mask in model inputs to avoid unexpected behavior
Browse files
app.py
CHANGED
@@ -16,11 +16,12 @@ def load_model_and_tokenizer(model_name):
|
|
16 |
def process_input_text(input_text, tokenizer, device):
|
17 |
inputs = tokenizer(input_text, return_tensors="pt").to(device)
|
18 |
input_ids = inputs["input_ids"]
|
|
|
19 |
return inputs, input_ids
|
20 |
|
21 |
def calculate_log_probabilities(model, tokenizer, inputs, input_ids):
|
22 |
with torch.no_grad():
|
23 |
-
outputs = model(
|
24 |
logits = outputs.logits[0, :-1, :]
|
25 |
log_probs = torch.log_softmax(logits, dim=-1)
|
26 |
token_log_probs = log_probs[range(log_probs.shape[0]), input_ids[0][1:]]
|
@@ -31,9 +32,11 @@ def calculate_log_probabilities(model, tokenizer, inputs, input_ids):
|
|
31 |
def generate_replacements(model: PreTrainedModel, tokenizer: PreTrainedTokenizer, prefix: str, device: torch.device, num_samples: int = 5) -> list[str]:
|
32 |
input_context = tokenizer(prefix, return_tensors="pt").to(device)
|
33 |
input_ids = input_context["input_ids"]
|
|
|
34 |
with torch.no_grad():
|
35 |
outputs = model.generate(
|
36 |
input_ids=input_ids,
|
|
|
37 |
max_length=input_ids.shape[-1] + 5,
|
38 |
num_return_sequences=num_samples,
|
39 |
temperature=1.0,
|
|
|
16 |
def process_input_text(input_text, tokenizer, device):
|
17 |
inputs = tokenizer(input_text, return_tensors="pt").to(device)
|
18 |
input_ids = inputs["input_ids"]
|
19 |
+
attention_mask = inputs["attention_mask"]
|
20 |
return inputs, input_ids
|
21 |
|
22 |
def calculate_log_probabilities(model, tokenizer, inputs, input_ids):
|
23 |
with torch.no_grad():
|
24 |
+
outputs = model(input_ids=input_ids, attention_mask=attention_mask, labels=input_ids)
|
25 |
logits = outputs.logits[0, :-1, :]
|
26 |
log_probs = torch.log_softmax(logits, dim=-1)
|
27 |
token_log_probs = log_probs[range(log_probs.shape[0]), input_ids[0][1:]]
|
|
|
32 |
def generate_replacements(model: PreTrainedModel, tokenizer: PreTrainedTokenizer, prefix: str, device: torch.device, num_samples: int = 5) -> list[str]:
|
33 |
input_context = tokenizer(prefix, return_tensors="pt").to(device)
|
34 |
input_ids = input_context["input_ids"]
|
35 |
+
attention_mask = input_context["attention_mask"]
|
36 |
with torch.no_grad():
|
37 |
outputs = model.generate(
|
38 |
input_ids=input_ids,
|
39 |
+
attention_mask=attention_mask,
|
40 |
max_length=input_ids.shape[-1] + 5,
|
41 |
num_return_sequences=num_samples,
|
42 |
temperature=1.0,
|