Skip to content

decode_n_tokens clean up #1532

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

Merged
merged 1 commit into from
Apr 22, 2025
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
10 changes: 2 additions & 8 deletions torchchat/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,6 @@ def decode_n_tokens(
attention_backend: SDPBackend = torch.nn.attention.SDPBackend.MATH,
**sampling_kwargs,
):
new_tokens, new_probs = [], []
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really used. This function yields individual tokens and probabilities, not the arrays.

encountered_eos = False
for _i in range(
num_new_tokens - 1
Expand All @@ -553,12 +552,10 @@ def decode_n_tokens(
**sampling_kwargs,
)
input_pos += 1
new_tokens.append(next_token.clone())
callback(new_tokens[-1], done_generating=_i == num_new_tokens - 2)
if need_probs or next_prob is None:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was backwards. It should be: if not need probs or next_prob is None:. Otherwise you are saying, if you need the probabilities you are getting None, and if you don't need them you are getting them.

callback(next_token.clone(), done_generating=_i == num_new_tokens - 2)
if not need_probs or next_prob is None:
Copy link
Contributor

@Jack-Khuu Jack-Khuu Apr 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything else is just cleaning up unused code

not need_probs is the only real change and in the non-speculative path is always false so the old check is effectively just if next_prob is None

need_probs=False,

I think we should drop the check instead of negating here, so it becomes easier to rip spec decoding out completely. The returned prob doesn't get used either way

for generated_token, _ in self.decode_n_tokens(

Suggested change
if not need_probs or next_prob is None:
if next_prob is None:

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly this is a nit we can just merge

yield out_token, None
else:
new_probs.append(next_prob.clone())
yield out_token, next_prob.clone()
cur_token = next_token

Expand All @@ -585,7 +582,6 @@ def decode_n_tokens(
dtype=cur_token.dtype,
device=cur_token.device,
)
new_tokens.append(eos_token.clone())
eos_token, next_prob = self.decode_one_token(
model,
eos_token.view(1, -1),
Expand Down Expand Up @@ -788,7 +784,6 @@ def generate(
input_pos = input_pos + num_added
next_token = next_tokens[-1]
else:
generated_tokens = []
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not used. Generated tokens are appended after the call to decode_n_tokens, but then nothing happens.

for generated_token, _ in self.decode_n_tokens(
model,
next_token,
Expand All @@ -806,7 +801,6 @@ def generate(
attention_backend=attention_backend,
**sampling_kwargs,
):
generated_tokens.append(generated_token.view(-1))
yield generated_token, None

generate_stats = {
Expand Down