Skip to content

Commit

Permalink
fix: do not print perf stat when NaN (#1375)
Browse files Browse the repository at this point in the history
If the chat is exited or interrupted it will still print the stats with
NaN values which is unnecessary.

Signed-off-by: Sébastien Han <[email protected]>
  • Loading branch information
leseb authored Nov 15, 2024
1 parent 4697764 commit d7b681a
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions torchchat/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -1189,12 +1189,27 @@ def callback(x, *, done_generating=False):
f"Mean Accepted: {sum([idx * i for idx, i in enumerate(counts_aggregated)])/sum(counts_aggregated)}"
)

print(
f"\n Average tokens/sec (total): {torch.mean(torch.tensor(aggregate_metrics['tokens_per_sec'])).item():.2f} \
\nAverage tokens/sec (first token): {torch.mean(torch.tensor(aggregate_metrics['first_token_per_sec'])).item():.2f} \
\nAverage tokens/sec (next tokens): {torch.mean(torch.tensor(aggregate_metrics['next_tokens_per_sec'])).item():.2f} \n\
avg_tokens_sec = torch.mean(
torch.tensor(aggregate_metrics["tokens_per_sec"])
).item()
avg_first_token_sec = torch.mean(
torch.tensor(aggregate_metrics["first_token_per_sec"])
).item()
avg_next_tokens_sec = torch.mean(
torch.tensor(aggregate_metrics["next_tokens_per_sec"])
).item()

if not (
torch.isnan(torch.tensor(avg_tokens_sec))
or torch.isnan(torch.tensor(avg_first_token_sec))
or torch.isnan(torch.tensor(avg_next_tokens_sec))
):
print(
f"\n Average tokens/sec (total): {avg_tokens_sec:.2f} \
\nAverage tokens/sec (first token): {avg_first_token_sec:.2f} \
\nAverage tokens/sec (next tokens): {avg_next_tokens_sec:.2f} \n\
"
)
)
if torch.cuda.is_available():
print(f"Memory used: {torch.cuda.max_memory_reserved() / 1e9:.02f} GB")

Expand Down

0 comments on commit d7b681a

Please sign in to comment.