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

[Other]TopPProcess add bfloat16 input #7337

Merged
merged 2 commits into from
Oct 31, 2023
Merged
Changes from 1 commit
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
12 changes: 9 additions & 3 deletions paddlenlp/generation/logits_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,16 @@ def TopKProcess(probs: paddle.Tensor, top_k: int, min_tokens_to_keep: int):


def TopPProcess(probs: paddle.Tensor, top_p: float, min_tokens_to_keep: int):
sorted_indices = paddle.argsort(probs, descending=True)
if isinstance(sorted_indices, tuple):
sorted_probs, sorted_indices = sorted_indices
if probs.dtype == paddle.bfloat16:
probs = paddle.cast(probs, paddle.float32)

sorted_indices = paddle.argsort(probs, descending=True)
sorted_probs = paddle.sort(probs, descending=True)

sorted_probs = paddle.cast(sorted_probs, paddle.bfloat16)
sorted_indices = paddle.cast(sorted_indices, paddle.int64)
Copy link
Contributor

Choose a reason for hiding this comment

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

这里的 sorted_indices 可以不用cast,其返回值本身就是 int64.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

else:
sorted_indices = paddle.argsort(probs, descending=True)
sorted_probs = paddle.sort(probs, descending=True)

cumulative_probs = paddle.cumsum(sorted_probs, axis=-1)
Expand Down
Loading