Skip to content

get_chat_template breaks when using add_prefix_space #305

Closed

Description

@oKatanaaa

When tokenizer is provided with add_prefix_space=True, get_chat_template throws an error. Having add_prefix_space=None works fine.

Code to reproduce:

from unsloth.chat_templates import get_chat_template
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.1", add_prefix_space=True)
tokenizer = get_chat_template(
    tokenizer,
    chat_template="chatml",
    map_eos_token=True
)

Error message:
TypeError: Couldn't build proto file into descriptor pool: duplicate file name sentencepiece_model.proto


Apart from this issue, there seems to be some quirk with loading a modified tokenizer from a folder with trained lora adapters.

In my case at the end of training I save tokenizer in the same folder as the adapters. When loading lora I give path to the folder with adapters (which also contains saved tokenizer) and it seems that unsloth tries to load the saved tokenizer instead of loading the one from the base model. The most confusing thing is that it thinks I set add_prefix_space=True, since I didn't
Moving tokenizer files into a separate folder fixes things, unsloth loads base tokenizer as expected. But the mystery still remains

Activity

  1. oKatanaaa commented on Apr 5, 2024

    @oKatanaaa
    ContributorAuthor

    Related issue #291
    This one possibly can be solved by upgrading sentencepiece library to the latest version.

  2. danielhanchen commented on Apr 5, 2024

    @danielhanchen
    Member

    @oKatanaaa Possibly fixed hopefully! Your code runs now - it seems like I'm dumb and using from transformers.utils import sentencepiece_model_pb2 solved the issue

  3. added
    fixed - pending confirmation
    on Apr 5, 2024
  4. oKatanaaa commented on Apr 5, 2024

    @oKatanaaa
    ContributorAuthor

    Sadly, the issue still persists for me. I tried both main and nightly branches. I also tried upgrading protobuf version to the latest. Nothing helped.

    In case it helps to identify the issue, here is a list of my python packages:

    absl-py                   2.1.0
    accelerate                0.27.2
    aiohttp                   3.9.1
    aiosignal                 1.3.1
    archspec                  0.2.1
    asttokens                 2.0.5
    astunparse                1.6.3
    async-timeout             4.0.3
    attrs                     23.1.0
    backcall                  0.2.0
    beautifulsoup4            4.12.2
    bitsandbytes              0.42.0
    boltons                   23.0.0
    Brotli                    1.0.9
    certifi                   2024.2.2
    cffi                      1.16.0
    chardet                   4.0.0
    charset-normalizer        2.0.4
    click                     8.1.7
    comm                      0.2.1
    conda                     23.9.0
    conda-build               3.28.1
    conda-content-trust       0.2.0
    conda_index               0.3.0
    conda-libmamba-solver     23.7.0
    conda-package-handling    2.2.0
    conda_package_streaming   0.9.0
    contourpy                 1.2.0
    cryptography              41.0.7
    cycler                    0.12.1
    datasets                  2.16.1
    DAWG-Python               0.7.2
    debugpy                   1.8.0
    decorator                 5.1.1
    dill                      0.3.7
    distro                    1.8.0
    dnspython                 2.4.2
    docopt                    0.6.2
    docstring-parser          0.15
    einops                    0.7.0
    exceptiongroup            1.0.4
    executing                 0.8.3
    expecttest                0.1.6
    fasttext                  0.9.2
    fasttext-langdetect       1.0.5
    filelock                  3.13.1
    fonttools                 4.49.0
    frozenlist                1.4.1
    fsspec                    2023.10.0
    gmpy2                     2.1.2
    huggingface-hub           0.20.3
    hypothesis                6.92.0
    idna                      3.4
    immutabledict             4.1.0
    ipykernel                 6.29.0
    ipython                   8.15.0
    jedi                      0.18.1
    Jinja2                    3.1.2
    joblib                    1.3.2
    jsonpatch                 1.32
    jsonpointer               2.1
    jsonschema                4.19.2
    jsonschema-specifications 2023.7.1
    jupyter_client            8.6.0
    jupyter_core              5.7.1
    kiwisolver                1.4.5
    kolibrify                 0.1.0              /workspace/kolibrify
    langdetect                1.0.9
    libarchive-c              2.9
    libmambapy                1.5.3
    markdown-it-py            3.0.0
    MarkupSafe                2.1.1
    matplotlib                3.8.3
    matplotlib-inline         0.1.6
    mdurl                     0.1.2
    menuinst                  2.0.1
    mkl-fft                   1.3.8
    mkl-random                1.2.4
    mkl-service               2.4.0
    more-itertools            10.1.0
    mpmath                    1.3.0
    multidict                 6.0.4
    multiprocess              0.70.15
    nest-asyncio              1.6.0
    networkx                  3.1
    ninja                     1.11.1.1
    nltk                      3.8.1
    numpy                     1.26.2
    packaging                 23.1
    pandas                    2.2.0
    parso                     0.8.3
    peft                      0.8.2
    pexpect                   4.8.0
    pickleshare               0.7.5
    Pillow                    10.0.1
    pip                       23.3.1
    pkginfo                   1.9.6
    platformdirs              3.10.0
    pluggy                    1.0.0
    prompt-toolkit            3.0.36
    protobuf                  5.26.1
    psutil                    5.9.0
    ptyprocess                0.7.0
    pure-eval                 0.2.2
    pyarrow                   15.0.0
    pyarrow-hotfix            0.6
    pybind11                  2.11.1
    pycosat                   0.6.6
    pycparser                 2.21
    Pygments                  2.15.1
    pymorphy2                 0.9.1
    pymorphy2-dicts-ru        2.4.417127.4579844
    pyOpenSSL                 23.2.0
    pyparsing                 3.1.1
    PySocks                   1.7.1
    python-dateutil           2.8.2
    python-etcd               0.4.5
    pytz                      2023.3.post1
    PyYAML                    6.0.1
    pyzmq                     25.1.2
    referencing               0.30.2
    regex                     2023.12.25
    requests                  2.31.0
    rich                      13.7.0
    rpds-py                   0.10.6
    ruamel.yaml               0.17.21
    ruamel.yaml.clib          0.2.6
    safetensors               0.4.2
    scikit-learn              1.4.0
    scipy                     1.12.0
    seaborn                   0.13.2
    sentence-transformers     2.3.1
    sentencepiece             0.2.0
    setuptools                68.2.2
    shtab                     1.6.5
    six                       1.16.0
    sortedcontainers          2.4.0
    soupsieve                 2.5
    stack-data                0.2.0
    sympy                     1.12
    tensorboardX              2.6.2.2
    threadpoolctl             3.3.0
    tokenizers                0.15.2
    tomli                     2.0.1
    toolz                     0.12.0
    torch                     2.1.2
    torchaudio                2.1.2
    torchelastic              0.2.2
    torchvision               0.16.2
    tornado                   6.4
    tqdm                      4.65.0
    traitlets                 5.7.1
    transformers              4.38.1
    triton                    2.1.0
    trl                       0.7.11
    truststore                0.8.0
    typer                     0.9.0
    types-dataclasses         0.6.6
    typing_extensions         4.7.1
    tyro                      0.7.3
    tzdata                    2023.4
    unsloth                   2024.4
    urllib3                   1.26.18
    wcwidth                   0.2.5
    wheel                     0.42.0
    xformers                  0.0.23.post1
    xxhash                    3.4.1
    yarl                      1.9.4
    zstandard                 0.19.0
    
  5. oKatanaaa commented on Apr 5, 2024

    @oKatanaaa
    ContributorAuthor

    Downgrading protobuf to 3.20.1 solved the issue. It should be mentioned in the patch notes in the next release since the issue still persists with newest protobuf versions.

  6. danielhanchen commented on Apr 6, 2024

    @danielhanchen
    Member

    @oKatanaaa You're correct! I updated the pyproject.toml to use < 4.0.0. I'm not sure why its erroring out - it seems like protobuf does not like people creating new sentencepiece tokenizers from scratch

  7. added a commit that references this issue on Mar 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    fixed - pending confirmation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions