Skip to content

[Bug] Llama_3.2_1B_Conversational: Seeing multiple trailing <|reserved_special_token_xxx|> at inference time #2360

Closed

Description

@smalhotra-spirent

Describe the bug
I'm following the steps in the https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Llama3.2_(1B_and_3B)-Conversational.ipynb notebook to fine tune Llama 3.2 1B model to generate custom Python functions from my codebase. During inference (Colab itself, and also when I convert to Ollama GGUF), the generated output always has a few reserved tokens in the end. For example: <|reserved_special_token_193|><|reserved_special_token_87|>.

Earlier, when I just followed the steps in the example Colab notebook, it resulted in continuous generation. But later when I added an explicit step to append EOS_TOKEN I'm not seeing continuous generation, but it still generates random reserved tokens. This is the only step that is different from the example Colab notebook:

tokenizer = get_chat_template(
    tokenizer,
    chat_template = "llama-3.1",
)

print(f"Using EOS token: {tokenizer.eos_token} with id: {tokenizer.eos_token_id}")
print(f"Using PAD token: {tokenizer.pad_token} with id: {tokenizer.pad_token_id}")

def formatting_prompts_func(examples):
    convos = examples["conversations"]
    texts = []
    for convo in convos:
        text = tokenizer.apply_chat_template(convo, tokenize=False, add_generation_prompt=False)
        # Manually ensure EOS token is present
        if not text.endswith(tokenizer.eos_token):
            text += tokenizer.eos_token
        texts.append(text)
    return {"text": texts}

dataset = standardize_sharegpt(dataset)
dataset = dataset.map(formatting_prompts_func, batched = True,)

I have also confirmed that EOS_TOKEN and PAD_TOKEN are not the same.

Here's what I get when I print the dataset after applying the formatting_prompts_func to the conversations (which appears to be in line with the Colab example):

<|begin_of_text|><|start_header_id|>system<|end_header_id|>

Cutting Knowledge Date: December 2023
Today Date: 26 July 2024

<|eot_id|><|start_header_id|>user<|end_header_id|>

--- Prompt example instruction ---

<|eot_id|><|start_header_id|>assistant<|end_header_id|>

```python
--- expected sample python code ---
```
<|eot_id|><|end_of_text|>

Any ideas what might be going on? As a last resort, I had to write a clean-up function that removes these tokens from the generated response.

  1. Environment Setup:

  2. Dataset Details:
    Custom private dataset that follows the following pattern:
    [{'content': '---sample prompt to generate a custom python function given some inputs---', 'role': 'user'}, {'content': '```python---sample code snippet for training---```\n', 'role': 'assistant'}]

  3. Model Details:
    unsloth/Llama-3.2-1B

  4. Training Configuration:

trainer = SFTTrainer(
    model = model,
    tokenizer = tokenizer,
    train_dataset = dataset,
    dataset_text_field = "text",
    max_seq_length = max_seq_length,
    data_collator = DataCollatorForSeq2Seq(tokenizer = tokenizer),
    dataset_num_proc = 2,
    packing = False, # Can make training 5x faster for short sequences.
    args = TrainingArguments(
        per_device_train_batch_size = 2,
        gradient_accumulation_steps = 4,
        warmup_steps = 5,
        #num_train_epochs = 1, # Set this for 1 full training run.
        max_steps = 500,
        learning_rate = 2e-4,
        fp16 = not is_bfloat16_supported(),
        bf16 = is_bfloat16_supported(),
        logging_steps = 10,
        optim = "adamw_8bit",
        weight_decay = 0.01,
        lr_scheduler_type = "linear",
        seed = 3407,
        output_dir = "outputs",
        report_to = "none", # Use this for WandB etc
    ),
)

# Start training
trainer_stats = trainer.train()
  1. Expected Behavior:
    I would expected the inference to just generate the desired Python output without any trailing reserved tokens.

  2. Actual Behavior:
    Unwanted trailing reserved tokens, leading to junk output after actual code snippet.

  3. Additional notes:
    None.

Activity

  1. changed the title [-][Bug] Llama_3.2_1B_Conversational: Seeing multiple <|reserved_special_token_xxx|> at inference time[/-] [+][Bug] Llama_3.2_1B_Conversational: Seeing multiple trailing <|reserved_special_token_xxx|> at inference time[/+] on Apr 16, 2025
  2. github-actions commented on Jul 1, 2025

    @github-actions
    Contributor

    Is this issue still important to you? Apologies in advance we might have missed this issue as well. For faster response times, please post on our Reddit server - https://www.reddit.com/r/unsloth or our Discord - https://discord.com/invite/unsloth

  3. rolandtannous commented on Oct 15, 2025

    @rolandtannous
    Contributor

    @smalhotra-spirent

    We just pushed a major fix for llamacpp integration.

    1- Please make sure to update your local install of unsloth

    pip install --force-reinstall --no-deps unsloth-zoo unsloth
    

    If you're using a notebook on kaggle or colab, it should pull the latest version automatically.

    2- You can now use save_pretrained_gguf to quantize and save to GGUF format:

    model.save_pretrained_gguf(
                save_directory,
                tokenizer=tokenizer,
                quantization_method=quantization_method,
            )
    

    For vision/multimodal models, use tokenizer=processor

    3- You can pass it one or several quant methods (as a list):

    quantization_method = "q8_0"
    

    or

    quantization_method = ["q8_0", "q4_k_m"]
    

    We support all the quants mentioned in this page except the iq variants.

    4- If the model is supported by Ollama, it will now also automatically create a Modelfile for you that you can use to create your model in ollama using:

    ollama create model_name -f ./Modelfile
    

    5- Use push_to_hub_gguf if you want to convert to gguf and push to Huggingface Hub instead.

    6- Should work for all models supported by unsloth and ollama.

    7- It now fully supports linux debian and RPM variants. We are still testing windows (if someone has a windows machine and is on our discord channel ping me to help debug)

    Just tried it with the llama-3.2-3B-instruct and it played nicely both with llama-cpp and ollama. Let me know how it goes for you

    Thanks!

  4. rolandtannous commented on Oct 23, 2025

    @rolandtannous
    Contributor

    Will be closing this for now as the issue has been resolved. Please make sure to follow the instructions given in my last response.

    Meanwhile , please feel free to respond if you're still having any issues.

    Thank you

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

    bugfixed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions