Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRY sampler improvements #6053

Merged
merged 7 commits into from
Jun 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions modules/sampler_hijack.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,21 +204,25 @@ def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> to
input_ids = input_ids[:, -self._range:]

for input_ids_row, scores_row in zip(input_ids, scores):
# Raw integer must be extracted here to check for set membership.
last_token = input_ids_row[-1].item()
# Use normal Python data types for improved performance
input_ids = input_ids_row.tolist()

last_token = input_ids[-1]
if last_token in self.sequence_breakers:
continue

# Exclude the last token as it always matches.
match_indices = (input_ids_row[:-1] == last_token).nonzero()
match_indices = []
for idx, val in enumerate(input_ids[:-1]):
if val == last_token:
match_indices.append(idx)
belladoreai marked this conversation as resolved.
Show resolved Hide resolved

# Stores the maximum matching sequence length
# for each token immediately following the sequence in the input.
match_lengths = {}

for i in match_indices:
next_token = input_ids_row[i + 1].item()
next_token = input_ids[i + 1]

if next_token in self.sequence_breakers:
continue
Expand All @@ -227,15 +231,15 @@ def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> to
# so the match is at least of length 1.
match_length = 1

# Extend the match backwards as far as possible.
while True:
# Extend the match backwards (at most to 50 to prevent exponent overflow at penalty calculation) (this cap also improves performance on worst case)
while match_length < 50:
j = i - match_length
if j < 0:
# Start of input reached.
break

previous_token = input_ids_row[-(match_length + 1)].item()
if input_ids_row[j] != previous_token:
previous_token = input_ids[-(match_length + 1)]
if input_ids[j] != previous_token:
# Start of match reached.
break

Expand Down