Repository navigation
Description
I have noticed that my fine-tuned versions of the phi-3.5-mini model generate incoherent content when exceeding an output length of 4096 tokens. I could reproduce this behaviour with the base-model as well.
Here is a simplified demonstration:
from unsloth import FastLanguageModel
import os
model_path = "unsloth/Phi-3.5-mini-instruct"
model, tokenizer = FastLanguageModel.from_pretrained(model_name=model_path)
FastLanguageModel.for_inference(model)
def make_inference(prompt: str):
# prepare input sequence
input_ids = tokenizer(prompt, return_tensors="pt")
input_len = input_ids['input_ids'].shape[1]
print(f"Input length: {input_len}\n")
# Inference
output_ids = model.generate(**input_ids.to("cuda"), max_new_tokens=50)[0, input_len:]
output = tokenizer.decode(output_ids, skip_special_tokens=True)
print(f"Model output:\n{output}")
# Prompting with < 4096 tokens
prompt = "The capital of France is Paris\n" * 585 # continued generation of a simple sentence
make_inference(prompt)
Output:
Input length: 4081
Model output:
The capital of France is Paris
The capital of France is Paris
The
,
,
,
Note
This simplified example demonstrates that the model starts to continue generating the input sentence until the sequence length is 4096 tokens. After that, the model outputs "gibberish" text containing only whitespace and random characters like ",".
I did not observe this behaviour when the sequence stays below 4096 tokens or when the initial input sequence is already above 4096 tokens:
# Prompting with >4096 tokens
prompt = "The capital of France is Paris\n" * 586
make_inference(prompt)
Output:
Input length: 4102
Model output:
The capital of France is Paris
The capital of France is Paris
The capital of France is Paris
The capital of France is Paris
The capital of France is Paris
The capital of France is Paris
The capital of France is Paris
The
# Generation with total output sequence <4096 tokens
prompt = "The capital of France is Paris\n" * 400
make_inference(prompt)
Output:
Input length: 2800
Model output:
The capital of France is Paris
The capital of France is Paris
The capital of France is Paris
The capital of France is Paris
The capital of France is Paris
The capital of France is Paris
The capital of France is Paris
The
Overall, my impression is that the output becomes instable when exceeding 4096 tokens (the model outputs gibberish). I think it is somehow related to how the embeddings for the LongRopeRotaryEmbedding module are cached and used during inference. My understanding on the implementation here is very limited though...
This issue was noticed in relation to a bug in #1189 and also mentioned in #1193.
Edit: formatting
Activity
danielhanchen commentedon Oct 31, 2024
@Erland366 Oh another generation issue - I think you investigated this a bit right?