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

Tokenisers not round-tripping a string #1531

Open
4 tasks done
riedgar-ms opened this issue Jun 14, 2024 · 1 comment
Open
4 tasks done

Tokenisers not round-tripping a string #1531

riedgar-ms opened this issue Jun 14, 2024 · 1 comment

Comments

@riedgar-ms
Copy link

Prerequisites

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

  • I am running the latest code. Development is very rapid so there are no tagged versions as of now.
  • I carefully followed the README.md.
  • I searched using keywords relevant to my issue to make sure that I am creating a new issue that is not already open (or closed).
  • I reviewed the Discussions, and have a new bug or useful enhancement to share.

Expected Behavior

I am trying to send a string through a string->tokens->string cycle, but I'm not getting the same string back as when I started.

Current Behavior

The round trip is putting an extra space on the start of the final string.

Environment and Context

Please provide detailed information about your computer setup. This is important in case the issue is not reproducible except for under certain specific conditions.

  • Physical (or virtual) hardware you are using, e.g. for Linux:

Core-i7 13800

  • Operating System, e.g. for Linux:

Win 11

  • SDK version, e.g. for Linux:

Python 3.12.3
Latest llama-cpp-python installed via pip

Failure Information (for bugs)

Please help provide information about the failure if this is a bug. If it is not a bug, please remove the rest of this template.

Steps to Reproduce

Please provide detailed steps for reproducing the issue. We are not sitting in front of your screen, so the more detail the better.

import pytest

from huggingface_hub import hf_hub_download
from typing import Any

class TestLlamaCppTokenizers:
    LLAMACPP_MODELS = [
        dict(
            gguf="TheBloke/Llama-2-7B-GGUF:llama-2-7b.Q5_K_M.gguf",
            kwargs={"verbose": True, "n_ctx": 4096},
        ),
        dict(
            gguf="microsoft/Phi-3-mini-4k-instruct-gguf:Phi-3-mini-4k-instruct-q4.gguf",
            kwargs={"verbose": True, "n_ctx": 4096},
        ),
    ]

    def get_tokenizer(self, model_info: dict[str, Any]):
        import llama_cpp

        repo_id, gguf_file = model_info["gguf"].split(":")
        downloaded_file = hf_hub_download(repo_id=repo_id, filename=gguf_file)
        lm = llama_cpp.Llama(model_path=downloaded_file, logits_all=True, **model_info["kwargs"])
        my_tok = lm.tokenizer()
        return my_tok

    @pytest.mark.parametrize("model_info", LLAMACPP_MODELS)
    def test_smoke(self, model_info: dict[str, Any]):
        my_tok = self.get_tokenizer(model_info)
        assert my_tok is not None

    @pytest.mark.parametrize("model_info", LLAMACPP_MODELS)
    @pytest.mark.parametrize("target_string", ["hello", "’"])
    def test_string_roundtrip(self, model_info: dict[str, Any], target_string: str):
        my_tok = my_tok = self.get_tokenizer(model_info)

        encoded = my_tok.tokenize(target_string.encode(), add_bos=False, special=False)
        decoded = my_tok.detokenize(encoded)
        final_string = decoded.decode()

        assert final_string == target_string

Failure Logs

Please include any relevant log snippets or files. If it works under one configuration but not under another, please provide logs for both configurations and their corresponding outputs so it is easy to see where behavior changes.

From pytest I'm seeing failures from the final assert:

FAILED tests/test_tokenizers.py::TestLlamaCppTokenizers::test_string_roundtrip[hello-model_info0] - AssertionError: assert ' hello' == 'hello'
FAILED tests/test_tokenizers.py::TestLlamaCppTokenizers::test_string_roundtrip[hello-model_info1] - AssertionError: assert ' hello' == 'hello'
FAILED tests/test_tokenizers.py::TestLlamaCppTokenizers::test_string_roundtrip[\u2019-model_info0] - AssertionError: assert ' ’' == '’'
FAILED tests/test_tokenizers.py::TestLlamaCppTokenizers::test_string_roundtrip[\u2019-model_info1] - AssertionError: assert ' ’' == '’'
@CISC
Copy link
Contributor

CISC commented Jun 14, 2024

This is normal for Llama tokenizers, I don't know about Phi-3, it depends on the add_prefix_space metadata.

riedgar-ms added a commit to guidance-ai/guidance that referenced this issue Jun 17, 2024
There seems to be a bug in the `LlamaCpp` tokenisers, where they [prepend spaces](abetlen/llama-cpp-python#1531). Fix this following @mmoskal , by prepending a byte which is extremely unlikely to occur in a real string, and using it to figure out the offending prefix.
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