abetlen / abetlen/llama-cpp-python

Not selecting the tokens with the highest probabilities with temperature 0

Abierto
#1,322 7 comentarios 0 reacciones 0 asignados Ver en GitHub
bug
Lenguaje dominante
Python
Estrellas
10.6k
Forks
1.4k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

# Prerequisites

Please answer the following questions for yourself before submitting an issue.

- [x] I am running the latest code. Development is very rapid so there are no tagged versions as of now.
- [x] I carefully followed the [README.md](https://github.com/abetlen/llama-cpp-python/blob/main/README.md).
- [x] I [searched using keywords relevant to my issue](https://docs.github.com/en/issues/tracking-your-work-with-issues/filtering-and-searching-issues-and-pull-requests) to make sure that I am creating a new issue that is not already open (or closed).
- [x] I reviewed the [Discussions](https://github.com/abetlen/llama-cpp-python/discussions), and have a new bug or useful enhancement to share.

# Expected Behavior

When running Mistral LLM with a temperature of 0, I expected the model to choose tokens with the highest probabilities exclusively.

# Current Behavior

I observed that for some tokens (apparently randomly), it selected the ones with the second-highest probabilities, leading to different outputs.

# Environment and Context

I am using Google Colab, T4 GPU with 15 GB VRAM.

```
$ python3 --version
Python 3.10.12
$ make --version
GNU Make 4.3
$ g++ --version
g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
$ pip list | egrep "pandas|numpy"
geopandas 0.13.2
numpy 1.25.2
pandas 1.5.3
pandas-datareader 0.10.0
pandas-gbq 0.19.2
pandas-stubs 1.5.3.230304
sklearn-pandas 2.2.0
```

# Steps to Reproduce

1. Installing llama-cpp-python
```
!CMAKE_ARGS="-DLLAMA_CUBLAS=on" FORCE_CMAKE=1 pip install llama-cpp-python==0.2.57
```

2. Initializing the model
```
from llama_cpp import Llama
llm = Llama(
model_path="mistral-7b-instruct-v0.2.Q8_0.gguf",
n_ctx= 10000,
n_gpu_layers = -1,
verbose= True,
logits_all = True # true to return logprobs
)
```
3. Running the model and getting the outputs
```
prompt = """[INST] (((5 + 4)*2)/9)+2? [/INST] """

llm.reset() # to clear the cache
output = llm.create_completion(prompt,
max_tokens = 200,
echo = False,
temperature = 0,
logprobs = 5 ) # to return top 5 tokens

results = output['choices'][0]['text']
print('Results from the model:\n')
print(results)

log_probs = output['choices'][0]['logprobs']['top_logprobs']
print('Results by selecting tokens with the highest probabilities:\n')
for el in log_probs:
chosen = max(el, key=lambda k: el[k])
print(chosen, end = '')

```

The results of the above two prints are the following which are not exactly the same because of the selected tokens:
```
Results from the model:

To solve the expression step by step, follow these instructions:

1. First, perform the multiplication inside the parentheses: (5 + 4) * 2 = 9 * 2 = 18.
2. Next, do the division: 18 / 9 = 2.
3. Add 2 to the result: 2 + 2 = 4.

So, the solution to ((5 + 4)*2)/9+2 is 4.

Results by selecting tokens with the highest probabilities:

To solve the expression ((( by step, follow the instructions:

1. First, perform the multiplication inside the parentheses: (5 + 4) * 2 = 9 * 2 = 18.
2. Next, do the division: 18 / 9 = 2.
3. Add 2 to the result: 2 + 2 = 4.

So, the solution to the5 + 4)*2)/9 +2 is 4.
```

4. Coverting log_probs to probabilities

```
import numpy as np
# convert logits to probs
for dict in log_probs:
for key, value in dict.items():
dict[key] = np.exp(value)
```

The probabilities show that for example, the model selects the token `step` with probability 0.22 instead of the token `(((` with probability 0.54.
```
{' (((': 0.5391272,
' step': 0.22150521,
' ((': 0.15229335,
',': 0.06753055,
'(((': 0.010963313}
```
As another example, it selects `+` with probability 0.0002 instead of `""+` (with leading whitespace) with probability 0.99 in this expression : `((5 + 4)*2)/9+2 is 4`
```
{' +': 0.9997909,
'+': 0.00020875783,
' plus': 2.6573096e-07,
' ±': 2.159923e-08,
' +=': 1.6546656e-08},
```

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.