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

Expose sample from generation utils #1559

Merged
merged 1 commit into from
Sep 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
1 change: 1 addition & 0 deletions docs/source/api_ref_generation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ torchtune.generation
:nosignatures:

generate
sample
4 changes: 2 additions & 2 deletions torchtune/generation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

from ._generation import generate, generate_next_token
from ._generation import generate, generate_next_token, sample

__all__ = ["generate", "generate_next_token"]
__all__ = ["generate", "generate_next_token", "sample"]
13 changes: 11 additions & 2 deletions torchtune/generation/_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,18 @@ def multinomial_sample_one(probs: torch.Tensor) -> torch.Tensor:


def sample(
logits: torch.Tensor, temperature: float = 1.0, top_k: int = None
logits: torch.Tensor, temperature: float = 1.0, top_k: Optional[int] = None
) -> torch.Tensor:
"""Generic sample from a probability distribution."""
"""Generic sample from a probability distribution.

Args:
logits (torch.Tensor): logits from which to sample
temperature (float): value to scale the predicted logits by, default 1.0.
top_k (Optional[int]): If specified, we prune the sampling to only token ids within the top_k probabilities

Returns:
torch.Tensor: sampled token id
"""
# scale the logits based on temperature
logits = logits / max(temperature, 1e-5)
if top_k is not None:
Expand Down
Loading