Skip to content

Commit

Permalink
fix: hotflip with short context
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammed-shihebi committed May 1, 2024
1 parent 8eee810 commit 40d54c3
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions model-inference/model_inference/tasks/attacks/hotflip.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,9 @@ def attack_instance(
new_contexts = []
old_context = [" ".join([word for word in processed_context])]
tmp_context = processed_context
for value in range(self.top_k):
num_replacements = min(self.top_k, len(new_imp_token_idx)) # Use the smaller of self.top_k or length of new_imp_token_idx

for value in range(num_replacements):
token_to_replace = replacement_tokens[value]
token_idx = new_imp_token_idx[value]
tmp_context[token_idx] = token_to_replace
Expand All @@ -236,7 +238,7 @@ def attack_instance(
all_contexts = old_context
all_contexts.extend(new_contexts)

questions = [" ".join([w for w in processed_question])] * (self.top_k + 1)
questions = [" ".join([w for w in processed_question])] * (num_replacements + 1)
prepared_inputs = [[q, c] for q, c in zip(questions, all_contexts)]

batch_request = self.base_prediction_request
Expand Down

0 comments on commit 40d54c3

Please sign in to comment.