10000 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

Don't convert logprobs arrays to lists #1021

merged 1 commit into from
Dec 18, 2023

Conversation

kddubey
Copy link
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
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
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
0