Closed salokr closed 1 month ago
I'm assuming a FastLanguageModel.for_inference(model)
call was missing maybe
Hi, thank you for responding. I added the call and still there was no difference
I have tried to write a minimal script to reproduce the errors (since I had to provide custom Trainer and custom evaluator the code is still large; sorry for that).
The code is available at: https://drive.google.com/file/d/1mkNf8zWSr82KIpcP-VaD75z5rM8fsJ16/view?usp=sharing
If you want to skip over the notebook, please only look at LLaMATrainer and compute_metrics_wrapper The rest of the code is the same and there's no major alteration (the same input/output template).
I also printed the logits and due to large output space, I have kept them hidden, click on "Show Output" to take a look at them
I minimized the code even more. Here's the minimal script: (also attached a screenshot)
from unsloth import FastLanguageModel
import torch
print(torch.cuda.device_count())
max_seq_length = 2048 # Choose any! We auto support RoPE Scaling internally!
dtype = None # None for auto detection. Float16 for Tesla T4, V100, Bfloat16 for Ampere+
load_in_4bit = True # Use 4bit quantization to reduce memory usage. Can be False.
model, tokenizer = FastLanguageModel.from_pretrained(
model_name = "unsloth/Meta-Llama-3.1-8B",
max_seq_length = max_seq_length,
dtype = dtype,
load_in_4bit = load_in_4bit,
# token = "hf_...", # use one if using gated models like meta-llama/Llama-2-7b-hf
)
model = FastLanguageModel.get_peft_model(
model,
r = 16, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128
target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
"gate_proj", "up_proj", "down_proj",],
lora_alpha = 16,
lora_dropout = 0, # Supports any, but = 0 is optimized
bias = "none", # Supports any, but = "none" is optimized
# [NEW] "unsloth" uses 30% less VRAM, fits 2x larger batch sizes!
use_gradient_checkpointing = "unsloth", # True or "unsloth" for very long context
random_state = 3407,
use_rslora = False, # We support rank stabilized LoRA
loftq_config = None, # And LoftQ
)
from datasets import load_dataset
dataset_gsm8k = load_dataset("gsm8k", "main")
sample = dataset_gsm8k["train"][0]
# print(sample)
encoding = tokenizer(sample["question"], return_tensors="pt")
# print(encoding["input_ids"][0].shape)
logits = model(encoding["input_ids"]).logits
import torch
probabilities = torch.nn.functional.softmax(logits, dim=-1)
predictions = torch.argmax(probabilities, axis=-1)
print("Predictions:", predictions)
print("Tokenized Input:", tokenizer.batch_decode(predictions))
FastLanguageModel.for_inference(model)
op_gen = model.generate(encoding["input_ids"], return_dict_in_generate=True,output_scores=True)
tokenizer.batch_decode(op_gen["sequences"])
NVM. I believe I was using the forward pass only once without using the BoS token.
Hello I am having the same issue as you, I do not know how you solved it.
Hi,
Thank you for availing the library.
I am using LLaMA-3.1 (
unsloth/llama-3-8b-Instruct-bnb-4bit
version) to instruct-tune a model on GSM8K. First, I used theSFTTrainer
to train a model and then defined a custom metric usingcompute_metrics
. Next, I pass the logits to an argmax function and then dobatch_decode
to get the output. Unfortunately, this produces gibberish output such as:INPUT SENTENCE
OUTPUT SENTENCE
As the next step, I used the model.generate function and then model started generating not only sensible but correct answers too: INPUT SENTENCE
OUTPUT SENTENCE
To reproduce this, I am posting the code below:
def map_dataset_to_template(dataset, tokenizer, key = "conversations"): def apply_template(examples, tokenizer, key): messages = examples[key] text = [tokenizer.apply_chat_template(message, tokenize = False, add_generation_prompt = False) for message in messages] return {"text":text}
Log the number of examples processed