Skip to content

Tkn to piece special #2

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions common/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1024,12 +1024,12 @@ std::vector<llama_token> llama_tokenize(
return result;
}

std::string llama_token_to_piece(const struct llama_context * ctx, llama_token token) {
std::string llama_token_to_piece(const struct llama_context * ctx, llama_token token, bool special) {
std::vector<char> result(8, 0);
const int n_tokens = llama_token_to_piece(llama_get_model(ctx), token, result.data(), result.size());
const int n_tokens = llama_token_to_piece(llama_get_model(ctx), token, result.data(), result.size(), special);
if (n_tokens < 0) {
result.resize(-n_tokens);
int check = llama_token_to_piece(llama_get_model(ctx), token, result.data(), result.size());
int check = llama_token_to_piece(llama_get_model(ctx), token, result.data(), result.size(), special);
GGML_ASSERT(check == -n_tokens);
} else {
result.resize(n_tokens);
Expand Down
3 changes: 2 additions & 1 deletion common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,10 @@ std::vector<llama_token> llama_tokenize(

// tokenizes a token into a piece
// should work similar to Python's `tokenizer.id_to_piece`
// special = true includes control/eos pieces, default is omitting them
std::string llama_token_to_piece(
const struct llama_context * ctx,
llama_token token);
llama_token token, bool special = false);

// TODO: these should be moved in llama.h C-style API under single `llama_detokenize` function
// that takes into account the tokenizer type and decides how to handle the leading space
Expand Down
13 changes: 9 additions & 4 deletions llama.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1042,10 +1042,10 @@ static void ggml_offload_nop(struct ggml_tensor * tensor) {

static std::string llama_token_to_piece(const struct llama_context * ctx, llama_token token) {
std::vector<char> result(8, 0);
const int n_tokens = llama_token_to_piece(llama_get_model(ctx), token, result.data(), result.size());
const int n_tokens = llama_token_to_piece(llama_get_model(ctx), token, result.data(), result.size(), false);
if (n_tokens < 0) {
result.resize(-n_tokens);
int check = llama_token_to_piece(llama_get_model(ctx), token, result.data(), result.size());
int check = llama_token_to_piece(llama_get_model(ctx), token, result.data(), result.size(), false);
GGML_ASSERT(check == -n_tokens);
}
else {
Expand Down Expand Up @@ -9338,10 +9338,15 @@ static std::string llama_decode_text(const std::string & text) {

// does not write null-terminator to buf
int llama_token_to_piece(const struct llama_model * model, llama_token token, char * buf, int length) {
return llama_token_to_piece(model, token, buf, length, false);
}

// does not write null-terminator to buf
int llama_token_to_piece(const struct llama_model * model, llama_token token, char * buf, int length, bool print_all_types = false) {
if (0 <= token && token < llama_n_vocab(model)) {
switch (llama_vocab_get_type(model->vocab)) {
case LLAMA_VOCAB_TYPE_SPM: {
if (llama_is_normal_token(model->vocab, token)) {
if (print_all_types || llama_is_normal_token(model->vocab, token)) {
std::string result = model->vocab.id_to_token[token].text;
llama_unescape_whitespace(result);
if (length < (int) result.length()) {
Expand Down Expand Up @@ -9371,7 +9376,7 @@ int llama_token_to_piece(const struct llama_model * model, llama_token token, ch
break;
}
case LLAMA_VOCAB_TYPE_BPE: {
if (llama_is_normal_token(model->vocab, token)) {
if (print_all_types || llama_is_normal_token(model->vocab, token)) {
std::string result = model->vocab.id_to_token[token].text;
result = llama_decode_text(result);
if (length < (int) result.length()) {
Expand Down
3 changes: 2 additions & 1 deletion llama.h
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,8 @@ extern "C" {
const struct llama_model * model,
llama_token token,
char * buf,
int length);
int length,
bool print_all_types);

//
// Grammar
Expand Down