Skip to content

{% if add_generation_prompt %} [FIXED] #1284

Closed

Description

@giuliabaldini

Hi there,

if I run my usual code after the Qwen 2.5 commit, I get multiple errors. The first one is the following

jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'endfor'. Jinja was looking for the following tags: 'elif' or 'else' or 'endif'. The innermost block that needs to be closed is 'if'.

which is probably because of the change in this line. Once I fix that, I still get

RuntimeError: Unsloth: The tokenizer `OpenMeditron/Meditron3-8B`
does not have a {% if add_generation_prompt %} for generation purposes.
Please file a bug report immediately - thanks!

Any ideas?

Best,
Giulia

Activity

  1. changed the title [-]Cannot run old code after Qwen 2.5 update[/-] [+]Jinja error after Qwen 2.5 update[/+] on Nov 13, 2024
  2. xizhangmable commented on Nov 14, 2024

    @xizhangmable

    I have a similar issue when running

    train_ds = train_ds.map(lambda x: {"training_prompt": tokenizer.apply_chat_template(x["chat"], tokenize=False, add_generation_prompt=False)})
    

    TemplateSyntaxError: Encountered unknown tag 'endfor'. Jinja was looking for the following tags: 'elif' or 'else' or 'endif'. The innermost block that needs to be closed is 'if'.

  3. changed the title [-]Jinja error after Qwen 2.5 update[/-] [+][FIXED] `{% if add_generation_prompt %}` Error[/+] on Nov 14, 2024
  4. changed the title [-][FIXED] `{% if add_generation_prompt %}` Error[/-] [+][FIXED] `{% if add_generation_prompt %}`[/+] on Nov 14, 2024
  5. danielhanchen commented on Nov 14, 2024

    @danielhanchen
    Member

    Apologies just fixed @giuliabaldini @xizhangmable - thanks for reporting! Please update Unsloth on local machines via pip install --upgrade --no-cache-dir --no-deps unsloth

    For Colab, Kaggle just refresh!

  6. added
    fixed - pending confirmation
    on Nov 14, 2024
  7. changed the title [-][FIXED] `{% if add_generation_prompt %}`[/-] [+]`{% if add_generation_prompt %}` [FIXED][/+] on Nov 14, 2024
  8. GreenBogDes commented on Nov 16, 2024

    @GreenBogDes

    I am still getting this error in google colab in a new session. I was helped by rolling back to a2f8db3

    RuntimeError Traceback (most recent call last)
    in <cell line: 19>()
    17 # ] # More models at https://huggingface.co/unsloth
    18
    ---> 19 model, tokenizer = FastLanguageModel.from_pretrained(
    20 model_name = "mlabonne/Meta-Llama-3.1-8B-Instruct-abliterated",
    21 max_seq_length = max_seq_length,

    3 frames
    /usr/local/lib/python3.10/dist-packages/unsloth/tokenizer_utils.py in fix_chat_template(tokenizer)
    656 if "{% if add_generation_prompt %}" not in new_chat_template and
    657 "{%- if add_generation_prompt %}" not in new_chat_template:
    --> 658 raise RuntimeError(
    659 f"Unsloth: The tokenizer {tokenizer.name_or_path}\n"
    660 "does not have a {% if add_generation_prompt %} for generation purposes.\n"\

    RuntimeError: Unsloth: The tokenizer mlabonne/Meta-Llama-3.1-8B-Instruct-abliterated
    does not have a {% if add_generation_prompt %} for generation purposes.
    Please file a bug report immediately - thanks!

  9. jamessucla commented on Nov 19, 2024

    @jamessucla

    Getting this issue on kaggle with the same Meta-Llama-3.1-8B-Instruct-abliterated model.

    @GreenBogDes mind sharing how you installed? I've tried pip install git+https://github.com/unslothai/unsloth.git@a2f8db3e7341f983af5814a2c56f54fa29ee548d (and several variations) but then I get errors when trying to import unsloth

    ---------------------------------------------------------------------------
    ImportError                               Traceback (most recent call last)
    File /opt/conda/lib/python3.10/site-packages/unsloth/__init__.py:32
         31 try:
    ---> 32     import unsloth_zoo
         33 except:
    
    File /opt/conda/lib/python3.10/site-packages/unsloth_zoo/__init__.py:27
         26 if not ("UNSLOTH_IS_PRESENT" in os.environ):
    ---> 27     raise ImportError("Please install Unsloth via `pip install unsloth`!")
         28 pass
    
    ImportError: Please install Unsloth via `pip install unsloth`!
    
    During handling of the above exception, another exception occurred:
    
    ImportError                               Traceback (most recent call last)
    Cell In[3], line 1
    ----> 1 from unsloth import FastLanguageModel
          2 import torch
          3 max_seq_length = 2048 # Choose any! We auto support RoPE Scaling internally!
    
    File /opt/conda/lib/python3.10/site-packages/unsloth/__init__.py:34
         32     import unsloth_zoo
         33 except:
    ---> 34     raise ImportError("Unsloth: Please install unsloth_zoo via `pip install unsloth-zoo`")
         35 pass
         37 # Unsloth currently does not work on multi GPU setups - sadly we are a 2 brother team so
         38 # enabling it will require much more work, so we have to prioritize. Please understand!
         39 # We do have a beta version, which you can contact us about!
         40 # Thank you for your understanding and we appreciate it immensely!
    
    ImportError: Unsloth: Please install unsloth_zoo via `pip install unsloth-zoo`
    
  10. GreenBogDes commented on Nov 19, 2024

    @GreenBogDes

    Getting this issue on kaggle with the same Meta-Llama-3.1-8B-Instruct-abliterated model.

    @GreenBogDes mind sharing how you installed? I've tried pip install git+https://github.com/unslothai/unsloth.git@a2f8db3e7341f983af5814a2c56f54fa29ee548d (and several variations) but then I get errors when trying to import unsloth

    ---------------------------------------------------------------------------
    ImportError                               Traceback (most recent call last)
    File /opt/conda/lib/python3.10/site-packages/unsloth/__init__.py:32
         31 try:
    ---> 32     import unsloth_zoo
         33 except:
    
    File /opt/conda/lib/python3.10/site-packages/unsloth_zoo/__init__.py:27
         26 if not ("UNSLOTH_IS_PRESENT" in os.environ):
    ---> 27     raise ImportError("Please install Unsloth via `pip install unsloth`!")
         28 pass
    
    ImportError: Please install Unsloth via `pip install unsloth`!
    
    During handling of the above exception, another exception occurred:
    
    ImportError                               Traceback (most recent call last)
    Cell In[3], line 1
    ----> 1 from unsloth import FastLanguageModel
          2 import torch
          3 max_seq_length = 2048 # Choose any! We auto support RoPE Scaling internally!
    
    File /opt/conda/lib/python3.10/site-packages/unsloth/__init__.py:34
         32     import unsloth_zoo
         33 except:
    ---> 34     raise ImportError("Unsloth: Please install unsloth_zoo via `pip install unsloth-zoo`")
         35 pass
         37 # Unsloth currently does not work on multi GPU setups - sadly we are a 2 brother team so
         38 # enabling it will require much more work, so we have to prioritize. Please understand!
         39 # We do have a beta version, which you can contact us about!
         40 # Thank you for your understanding and we appreciate it immensely!
    
    ImportError: Unsloth: Please install unsloth_zoo via `pip install unsloth-zoo`
    

    What helped me was adding those lines:

    !pip install git+https://github.com/unslothai/unsloth-zoo.git
    import os
    os.environ["UNSLOTH_IS_PRESENT"] = "1"
    

    But it still doesn't work in the new version. Here's my notebook: https://colab.research.google.com/drive/1MGwKUq1O46IFkR5R6MmPn1ZtonaDhSN9?usp=sharing

  11. jamessucla commented on Nov 19, 2024

    @jamessucla

    This actually worked for me. Very hacky, but here's what I have so far, if it helps anyone else. Some commands taken from the Colab starter notebook.

    %%capture
    !pip install pip3-autoremove
    !pip-autoremove torch torchvision torchaudio -y
    !pip install torch torchvision torchaudio xformers --index-url https://download.pytorch.org/whl/cu121
    !pip install unsloth[kaggle-new]
    !pip uninstall unsloth -y && pip install git+https://github.com/unslothai/unsloth.git@a2f8db3e7341f983af5814a2c56f54fa29ee548d
    !pip install git+https://github.com/unslothai/unsloth-zoo.git
    import os
    os.environ["UNSLOTH_IS_PRESENT"] = "1"
    

    Then, after loading the model:

    Unsloth: We successfully patched the tokenizer to add a {% if add_generation_prompt %} to the chat_template.
    This is not a bug, but please notify the Unsloth maintainers - thanks!
    mlabonne/Meta-Llama-3.1-8B-Instruct-abliterated does not have a padding token! Will use pad_token = <|finetune_right_pad_id|>.

    It appears to be training now.

  12. giuliabaldini commented on Dec 5, 2024

    @giuliabaldini
    ContributorAuthor

    Sorry, took me some time to retry this with a newer version of unsloth, it is working, thanks!

  13. giuliabaldini commented on Dec 5, 2024

    @giuliabaldini
    ContributorAuthor

    Hey, I was too quick to close it, I found out what is the problem in my case, I will submit a PR for it soon!

  14. unpinned this issue on Jan 20, 2025
  15. danielhanchen commented on Jun 18, 2026

    @danielhanchen
    Member

    This should be fixed now - sorry on the delay - please update with uv pip install --upgrade unsloth unsloth_zoo --force-reinstall --no-cache-dir - if it's not fixed yet, it'll be in the next pip / PyPI release - if it's still not working after that, please open another issue - appreciate it again!

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