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

Print the number of tokens generated #6771

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion examples/models/llama/runner/eager.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,11 @@ def main() -> None:
else runner.text_completion(
prompt=args.prompt,
temperature=args.temperature,
echo=True,
Copy link
Contributor

Choose a reason for hiding this comment

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

out of curiousity, what does echo=True do?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When it is true, the returned token list will include prompt tokens as well.

)
)
if args.show_tokens:
print(f"Tokens: {generated_tokens}")
print(f"Generated {len(generated_tokens)} tokens: {generated_tokens}")


if __name__ == "__main__":
Expand Down
5 changes: 4 additions & 1 deletion examples/models/llama/runner/generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def forward(
def generate( # noqa: C901
self,
prompt_tokens: List[int],
max_seq_len: int,
temperature: float = 0.8,
top_p: float = 0.9,
echo: bool = False,
Expand All @@ -83,7 +84,7 @@ def generate( # noqa: C901
print(f"{self.tokenizer.decode_token(current_token)}", end="", flush=True)
tokens = prompt_tokens + [current_token]

while len(tokens) < self.params.max_seq_len:
while len(tokens) < max_seq_len:
if self.params.use_kv_cache:
logits = self.forward(
tokens=torch.tensor(
Expand Down Expand Up @@ -135,6 +136,7 @@ def text_completion(
"""
return self.generate(
prompt_tokens=self.tokenizer.encode(prompt, bos=True, eos=False),
max_seq_len=self.params.max_seq_len,
temperature=temperature,
top_p=top_p,
echo=echo,
Expand Down Expand Up @@ -169,6 +171,7 @@ def chat_completion(
prompt_tokens=self.tokenizer.encode(
self._format_prompt(prompt), bos=True, eos=False
),
max_seq_len=self.params.max_seq_len,
temperature=temperature,
top_p=top_p,
echo=True,
Expand Down
Loading