-
Notifications
You must be signed in to change notification settings - Fork 187
[TextGeneration] Samling arguments for generation #1225
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
Changes from 2 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ef21383
draft
horheynm c7c308f
draft
horheynm 65857c8
draft
horheynm 542bf27
impleentation
horheynm 5b7c97e
delete commented line
horheynm 23d35d4
tests, update sampling calculation
horheynm f5c2a31
comments and bug fixes
horheynm 3e4ff9e
commnets
horheynm 9b65ce1
conflicts
horheynm 57ab9dd
remove generted tokent est form nldecoder engine
horheynm 0a6a315
Merge branch 'main' into sampling
horheynm 816c5ea
Merge branch 'main' into sampling
bfineran bad29f4
update prompt seq len name
bfineran e0ab606
readd missing code
horheynm e166be1
Merge branch 'main' into sampling
horheynm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import numpy | ||
from deepsparse.utils.data import numpy_softmax | ||
|
||
class TokenGenerator: | ||
def __init__( | ||
self, | ||
logits: numpy.ndarray, | ||
deterministic: bool = True, | ||
sampling_temperature: float = 1.0, | ||
top_k: int=0, | ||
top_p: float=0.0, | ||
frequency_penalty: float=0.0, | ||
presence_penalty: float=0.0, | ||
): | ||
self.token_frequencies = numpy.zeros(logits.shape) | ||
|
||
self.deterministic = deterministic | ||
self.sampling_termperature = sampling_temperature | ||
self.top_k = top_k | ||
self.top_p = top_p | ||
self.frequency_penalty = frequency_penalty | ||
self.presence_penalty = presence_penalty | ||
|
||
|
||
def update_frequences(self, token: numpy.ndarray): | ||
for tk in token: | ||
self.token_frequencies[0][tk] += 1 | ||
|
||
|
||
def generate(self, logits: numpy.ndarray) -> numpy.ndarray: | ||
""" | ||
Samples a token from the logits using the sampling temperature. | ||
|
||
:param logits: the logits from the model with shape (vocab_size,) | ||
:return: the sampled token | ||
""" | ||
if self.deterministic: | ||
return numpy.argmax(logits) | ||
|
||
if self.sampling_temperature != 1.0: | ||
logits /= self.sampling_temperature | ||
|
||
if self.top_k: | ||
logits = self.apply_top_k(logits) | ||
if self.top_p: | ||
logits = self.apply_top_p(logits) | ||
|
||
# penalties here | ||
if self.frequency_penalty != 0.0: | ||
logits = self.apply_frequency_penalty(logits) | ||
if self.presence_penalty != 0.0: | ||
logits = self.apply_presence_penalty(logits) | ||
|
||
probs = self.numpy_softmax(logits) | ||
|
||
token = numpy.random.choice(len(probs), p=probs) | ||
self.update_frequencies(token) | ||
|
||
return token | ||
|
||
|
||
# from https://gist.github.com/thomwolf/1a5a29f6962089e871b94cbd09daf317 | ||
def apply_top_k( | ||
self, | ||
logits: numpy.ndarray, top_k: int, filter_value=-float("Inf") | ||
): | ||
indices_to_remove = ( | ||
logits < numpy.partition(logits, -top_k, axis=1)[:, -top_k][:, None] | ||
) | ||
logits[indices_to_remove] = filter_value | ||
return logits | ||
|
||
# from https://gist.github.com/thomwolf/1a5a29f6962089e871b94cbd09daf317 | ||
def apply_top_p( | ||
self, | ||
logits: numpy.ndarray, top_p: float, filter_value=-float("Inf") | ||
): | ||
sorted_indices = numpy.argsort(logits) | ||
sorted_logits = logits[sorted_indices] | ||
cumulative_probs = numpy_softmax(sorted_logits) | ||
sorted_indices_to_remove = cumulative_probs <= (1 - top_p) | ||
|
||
indices_to_remove = sorted_indices_to_remove.scatter( | ||
1, sorted_indices, sorted_indices_to_remove | ||
) | ||
logits = numpy.where(indices_to_remove, filter_value, logits) | ||
return logits |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.