Skip to content
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

[Feature] how to get the token score(logprob) of greedy decoder? #2652

Open
Wondersui opened this issue Oct 24, 2024 · 1 comment
Open
Assignees

Comments

@Wondersui
Copy link

Motivation

I want to get the token score(logprob) of output tokens when use greedy search decode

Related resources

If you want to get the score(logprob) of the output token, you can only use tok sampling. If you set k=1, the output score will be 1. If it is greater than 1, topk sampling will output different results, which is not in line with expectations. Is there any way to get the score of the output token by greedy decode?

Additional context

none

@irexyc
Copy link
Collaborator

irexyc commented Oct 25, 2024

Without considering efficiency, you can inference it twice and compute it manually with existing apis.

import torch
from lmdeploy import pipeline, GenerationConfig
pipe = pipeline('/nvme/shared/vicuna-7b-v1.5/', log_level='INFO')
messages = 'hello' # or openai format

output = pipe(messages, gen_config=GenerationConfig(top_k=1))

decorated = pipe.chat_template.messages2prompt(messages)
prompt_tokens = pipe.tokenizer.encode(decorated)
all_tokens = prompt_tokens + output.token_ids
all_logits = pipe.get_logits(all_tokens)
all_scores = torch.softmax(all_logits, dim=-1)
gen_scores = all_scores[0, len(prompt_tokens) - 1:-1]
for i, tk in enumerate(output.token_ids):
    print(i, tk, gen_scores[i, tk])

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

No branches or pull requests

2 participants