8000 Don't convert logprobs arrays to lists by kddubey · Pull Request #1021 · abetlen/llama-cpp-python · GitHub
[go: up one dir, main page]

Skip to content

Don't convert logprobs arrays to lists #1021

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
merged 1 commit into from
Dec 18, 2023
Merged
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
13 changes: 6 additions & 7 deletions llama_cpp/llama.py
Original file line number Diff line number Diff line change
Expand Up @@ -1555,7 +1555,7 @@ def logit_bias_processor(
self.detokenize(completion_tokens[:returned_tokens])
)
token_offset = len(prompt_tokens) + returned_tokens
logits = self._scores[token_offset - 1, :].tolist()
logits = self._scores[token_offset - 1, :]
current_logprobs = Llama.logits_to_logprobs(logits)
sorted_logprobs = list(
sorted(
Expand Down Expand Up @@ -1674,7 +1674,7 @@ def logit_bias_processor(
self.detokenize(completion_tokens[:returned_tokens])
)
token_offset = len(prompt_tokens) + returned_tokens - 1
logits = self._scores[token_offset, :].tolist()
logits = self._scores[token_offset, :]
current_logprobs = Llama.logits_to_logprobs(logits)
sorted_logprobs = list(
sorted(
Expand Down Expand Up @@ -1788,9 +1788,8 @@ def logit_bias_processor(
self.detokenize([token]).decode("utf-8", errors="ignore")
for token in all_tokens
]
all_logprobs = [
Llama.logits_to_logprobs(row.tolist()) for row in self._scores
][token_offset:]
all_logprobs = Llama.logits_to_logprobs(self._scores)[token_offset:]
Copy link
Contributor Author
@kddubey kddubey Dec 17, 2023

Choose a reason for hiding this comment

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

all_logprobs is now a 2-D array instead of a list of 1-D arrays. But this change should be ok b/c it gets zipped, which will iterate over the first axis

# TODO: may be able to change this loop to use np.take_along_dim
for token, token_str, logprobs_token in zip(
all_tokens, all_token_strs, all_logprobs
):
Expand Down Expand Up @@ -2287,7 +2286,7 @@ def token_nl(self) -> int:

@staticmethod
def logits_to_logprobs(
logits: Union[List, npt.NDArray[np.single]], axis: int = -1
logits: Union[npt.NDArray[np.single], List], axis: int = -1
) -> npt.NDArray[np.single]:
# https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.log_softmax.html
logits_maxs: np.ndarray = np.amax(logits, axis=axis, keepdims=True)
Expand All @@ -2298,7 +2297,7 @@ def logits_to_logprobs(
subtract_maxs = np.subtract(logits, logits_maxs, dtype=np.single)
exp = np.exp(subtract_maxs)
# Suppress warnings about log of zero
with np.errstate(divide='ignore'):
with np.errstate(divide="ignore"):
summed = np.sum(exp, axis=axis, keepdims=True)
out = np.log(summed)
return subtract_maxs - out
Expand Down
0