Skip to content

Don't convert logprobs arrays to lists#1021

Merged
abetlen merged 1 commit intoabetlen:mainfrom
kddubey:lists-to-arrays
Dec 18, 2023
Merged

Don't convert logprobs arrays to lists#1021
abetlen merged 1 commit intoabetlen:mainfrom
kddubey:lists-to-arrays

Conversation

@kddubey
Copy link
Copy Markdown
Contributor

@kddubey kddubey commented Dec 17, 2023

Follow-up to #1002

Speedup

Run this in an ipython shell (python -m pip install jupyter if you don't have it already)

import numpy as np
from llama_cpp import Llama

_scores: np.ndarray = -np.random.default_rng(123).uniform(
    low=0, high=60, size=(20, 32_000)
)
token_offset = 1

print("time old")
%timeit [Llama.logits_to_logprobs(row.tolist()) for row in _scores][token_offset:]
print()
print("time new")
%timeit Llama.logits_to_logprobs(_scores)[token_offset:]

prints

time old
72.6 ms ± 862 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

time new
24.5 ms ± 142 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

How has this been tested?

import numpy as np
from llama_cpp import Llama

_scores: np.ndarray = -np.random.default_rng(123).uniform(
    low=0, high=60, size=(20, 32_000)
)
token_offset = 1

# 1-D case
logits = _scores[token_offset, :]
current_logprobs_old = Llama.logits_to_logprobs(logits.tolist())
current_logprobs_new = Llama.logits_to_logprobs(logits)
assert np.allclose(current_logprobs_old, current_logprobs_new)

# 2-D case
all_logprobs_old = [Llama.logits_to_logprobs(row.tolist()) for row in _scores][
    token_offset:
]
all_logprobs_new = Llama.logits_to_logprobs(_scores)[token_offset:]
assert np.allclose(all_logprobs_old, all_logprobs_new)

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
Copy Markdown
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

@kddubey kddubey changed the title Don't convert logprobs lists to arrays Don't convert logprobs arrays to lists Dec 17, 2023
@abetlen abetlen merged commit 6b2e0e0 into abetlen:main Dec 18, 2023
@abetlen
Copy link
Copy Markdown
Owner

abetlen commented Dec 18, 2023

@kddubey thank you!

@kddubey kddubey deleted the lists-to-arrays branch December 19, 2023 03:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants