unsloth: [FIXED] Llama 3 Finetuned model is not generating EOS token.
I’ve fine-tuned the llama 3 8 billion model. I followed the notebook and only changed the dataset. The dataset is similar to the alpaca dataset but for the Bangla language. I’ve trained the model for 1 epoch (36hrs) on a single T4 GPU. But, when I’m trying to generate a response it is not generating any eos token. It will go on till hitting the max_new_token length and stop.
Here is a sample of the code that is creating the dataset. (The same as the colab notebook. Just change the dataset name and system prompt)
code:
alpaca_prompt = """Below is an instruction in bangla that describes a task, paired with an input also in bangla that provides further context. Write a response in bangla that appropriately completes the request.
### Instruction:
{}
### Input:
{}
### Response:
{}"""
EOS_TOKEN = tokenizer.eos_token # Must add EOS_TOKEN
def formatting_prompts_func(examples):
instructions = examples["instruction"]
inputs = examples["input"]
outputs = examples["output"]
texts = []
for instruction, input, output in zip(instructions, inputs, outputs):
# Must add EOS_TOKEN, otherwise your generation will go on forever!
text = alpaca_prompt.format(instruction, input, output) + EOS_TOKEN
texts.append(text)
return { "text" : texts, }
pass
from datasets import load_dataset
dataset = load_dataset("iamshnoo/alpaca-cleaned-bengali", split = "train")
dataset = dataset.map(formatting_prompts_func, batched = True,)
One single example of the dataset['text'] looks like this:
'Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\n\n### Instruction:\nপদার্থের পরিবর্তনশীলতা এর বৈজ্ঞানিক সংজ্ঞা কি?\n\n### Input:\n\n\n### Response:\nবিপাক একটি জীবের মধ্যে ঘটে যাওয়া সমস্ত জৈব রাসায়নিক বিক্রিয়াকে বোঝায়, যার মধ্যে এমন প্রতিক্রিয়া রয়েছে যা শক্তি উত্পাদন করতে অণু ভাঙ্গতে পারে (ক্যাটাবলিজম) এবং নতুন অণু তৈরি করে (অ্যানাবলিজম) । এই প্রতিক্রিয়াগুলি এনজাইম দ্বারা সহজতর হয় এবং বৃদ্ধি, প্রজনন এবং পরিবেশের প্রতিক্রিয়া হিসাবে প্রয়োজনীয় প্রক্রিয়াগুলির মাধ্যমে জীবন বজায় রাখার জন্য প্রয়োজনীয়। বিপাক বিশেষত খাদ্যের ভাঙ্গন এবং এটি শক্তিতে রূপান্তরিত হতে পারে।<|end_of_text|>'
The EOS token has been added to the text in the end
Here is the generation code (same as the notebook):
# alpaca_prompt = Copied from above
alpaca_prompt = """Below is an instruction in bangla that describes a task, paired with an input also in bangla that provides further context. Write a response in bangla that appropriately completes the request.
### Instruction:
{}
### Input:
{}
### Response:
{}"""
FastLanguageModel.for_inference(model) # Enable native 2x faster inference
inputs = tokenizer(
[
alpaca_prompt.format(
"সুস্থ থাকার তিনটি উপায় বলুন", # instruction
"", # input
"", # output - leave this blank for generation!
)
], return_tensors = "pt").to("cuda")
outputs = model.generate(**inputs, max_new_tokens = 2048, use_cache = True)
tokenizer.batch_decode(outputs)
Here is the response output :
['<|begin_of_text|>Below is an instruction in bangla that describes a task, paired with an input also in bangla that provides further context. Write a response in bangla that appropriately completes the request.\n\n### Instruction:\nসুস্থ থাকার তিনটি উপায় বলুন\n\n### Input:\n\n\n### Response:\n১. নিয়মিত ব্যায়াম করুন: নিয়মিত শারীরিক ক্রিয়াকলাপ করা আপনার শরীরের স্বাস্থ্য এবং সুস্থতা বজায় রাখতে সহায়তা করতে পারে। এটি হার্ট রোগ, ডায়াবেটিস এবং স্থূলতার মতো দীর্ঘস্থায়ী রোগের ঝুঁকি হ্রাস করতে পারে। ২. স্বাস্থ্যকর খাদ্য খানঃ একটি সুষম এবং পুষ্টিকর ডায়েট খাওয়া আপনার শরীরের স্বাস্থ্য এবং সুস্থতা বজায় রাখতে সহায়তা করতে পারে। ফল, সবজি, পূর্ণ শস্য, চর্বিযুক্ত প্রোটিন এবং স্বাস্থ্যকর ফ্যাট সহ একটি ভারসাম্যপূর্ণ ডায়েট খাওয়া আপনার শরীরকে সঠিকভাবে কাজ করতে সহায়তা করতে পারে। ৩. পর্যাপ্ত ঘুম পানঃ পর্যাপ্ত ঘুম পাওয়া আপনার শরীরের স্বাস্থ্য এবং সুস্থতা বজায় রাখতে গুরুত্বপূর্ণ। প্রতি রাতে কমপক্ষে 7-8 ঘন্টা ঘুম পাওয়া আপনার শরীরের স্বাস্থ্যের জন্য অপরিহার্য। ঘুমের অভাব আপনার ইমিউন সিস্টেমকে দুর্বল করতে পারে, রোগের ঝুঁকি বাড়িয়ে তুলতে পারে এবং আপনার মানসিক স্বাস্থ্যের উপর নেতিবাচক প্রভাব ফেলতে পারে। সুতরাং পর্যাপ্ত ঘুম পাওয়া আপনার সামগ্রিক স্বাস্থ্য এবং সুস্থতা বজায় রাখতে গুরুত্বপূর্ণ। এটি আপনার শরীরের স্বাস্থ্যের জন্য অপরিহার্য। এটি আপনার শরীরের স্বাস্থ্যের জন্য অপরিহার্য। এটি আপনার শরীরের স্বাস্থ্যের জন্য অপরিহার্য। এটি আপনার শরীরের স্বাস্থ্যের জন্য অপরিহার্য। এটি আপনার শরীরের স্বাস্থ্যের জন্য অপরিহার্য। এটি আপনার শরীরের স্বাস্থ্যের জন্য অপরিহার্য। এটি আপনার শরীরের স্বাস্থ্যের জন্য অপরিহার্য। এটি আপনার শরীরের স্বাস্থ্যের জন্য অপরিহার্য। এটি আপনার শরীরের স্বাস্থ্যের জন্য অপরিহার্য। এটি আপনার শরীরের স্বাস্থ্যের জন্য অপরিহার্য। এটি আপনার শরীরের স্বাস্থ্যের জন্য অপরিহার্য। এটি আপনার শরীরের স্বাস্থ্যের জন্য অপরিহার্য। এটি আপনার শরীরের স্বাস্থ্যের জন্য অপরিহার্য। এটি আপনার শরীরের স্বাস্থ্যের জন্য অপরিহার্য। এটি আপনার শরীরের স্বাস্থ্যের জন্য অপরিহার্য। এটি আপনার শরীরের স্বাস্থ্যের জন্য অপরিহার্য। এটি আপনার শরীরের স্বাস্থ্যের জন্য অপরিহার্য। এটি আপনার শরীরের স্বাস্থ্যের জন্য অপরিহার্য। এটি আপনার শরীরের স্বাস্থ্যের জন্য অপরিহার্য। এটি আপনার শরী']
I asked the model in Bangla “Tell me 3 ways I can be healthy” and the model generated a coherent response. But after finishing the response it starts spamming “এটি আপনার শরীরের স্বাস্থ্যের জন্য অপরিহার্য” (eng-translation: It is necessary for your body). And it goes till it hits the max_new_token length. I’ve tried different questions, but the result is always the same. I couldn’t find a single time where the model generated the eos token.
The EOS token has been added to the data['text']. So in theory, If I fine-tune the model then it should learn to predict the EOS token. I’ve a total 51k samples and finetuned the model for 1 epoch.
One thing I’ve noticed is that in the original colab notebook, when the model was trained for 60 iterations and used to generate a response none of the responses generated EOS token.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 18 (8 by maintainers)
I’ve figured out the solution. Below is the code for those who just want the solution, not the details:
Solution Code:
Now, pass the
modelandtokenizertoSFTTrainer.Details of the solution
pad_token_id&eos_token_idis the same. Therefore when the model is fine-tuning the loss function ignores bothpad_tokenandeos_token. Thus, the model is not learning to predict theeos_token.pad_token_idandeos_token_idfor the unsloth-llama3. I found both thepad_token_idand theeos_token_idare the same.pad_token_id. I’ve found this stack overflow question where it shows how to change thepad_token_idfor falcon model.pad_token_idyou can not add any random value. It’ll throwCUDAerror. (I’m not sure but I’m assuming the reason for that error is the mismatch between tokenizer vocab size and model vocab size.)<|reserved_special_token_0|>to<|reserved_special_token_250|>. You can use any of the reserved special token value as thepad_tokenvalue. I’ve used the first one<|reserved_special_token_0|>pad_token_idandeos_token_idvalues.pad_token_id. This time I ask the model same question as before and the model was able to generateeos_tokenand stopped before hitting themax_new_tokenslength. Below I’ve shown 2 pictures showcasing the model’s response for the same and differenteos_tokenandpad_token.I’m hopping UnslothAI is going to see this bug and solve it in their colab notebook. Lots of people are facing this issue .
@KillerShoaib WHOOPS you are entirely correct!!! I immediately updated all
pad_tokens Unsloth has to<|reserved_special_token_250|>Thanks for the keen eye!!OMG Thank you for the solution here, was driving me nuts why llama3 was getting more rambling the more i trained it.
I encountered the same problem. I added EOS to the training data, but during prediction, the output always continues to the maximum number of tokens.
See https://huggingface.co/meta-llama/Meta-Llama-3-8B-Instruct/commit/4d6c61da057c45bfc4dc4d3bfa5a691ecb9ce0cf
Yes the pad token is in fact a bug fix
@davedgd Oh so Unsloth is fine (the models or just finetuning with Unsloth?) but the Meta ones still don’t work as expected?