Using the base model unsloth/Qwen3-14B-Base-unsloth-bnb-4bit is a great choice. This model is already optimized for Unsloth and uses 4-bit quantization, which can significantly reduce memory usage and improve training speed.
Here's an updated version of the fine-tuning code that uses the base model:
from unsloth import FastLanguageModel
from transformers import TrainingArguments
from trl import SFTTrainer
import torch
# 1. Configuration
max_seq_length = 2048 # Adjust based on your average chunk size
dtype = None # None for auto detection (Float16 or BFloat16)
load_in_4bit = True # Use 4bit quantization
# 2. Load the Model and Tokenizer
model, tokenizer = FastLanguageModel.from_pretrained(
model_name = "unsloth/Qwen3-14B-Base-unsloth-bnb-4bit",
max_seq_length = max_seq_length,
dtype = dtype,
load_in_4bit = load_in_4bit,
)
# 3. Configure LoRA (PEFT)
model = FastLanguageModel.get_peft_model(
model,
r = 16, # Rank (LoRA attention dimension). 8, 16, 32 are common.
target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
"gate_proj", "up_proj", "down_proj"],
lora_alpha = 16,
lora_dropout = 0,
bias = "none",
use_gradient_checkpointing = "unsloth",
random_state = 3407,
use_rslora = False,
loftq_config = None,
)
# 4. Set up the Trainer
trainer = SFTTrainer(
model = model,
tokenizer = tokenizer,
train_dataset = dataset,
dataset_text_field = None, # We are NOT using a simple text field
dataset_num_proc = 2,
max_seq_length = max_seq_length,
# !!! CRITICAL STEP !!!
# Use the 'conversations' column and apply the ChatML template
formatting_func=lambda examples: tokenizer.apply_chat_template(
examples['conversations'],
tokenize=False,
add_generation_prompt=False, # We are training, not generating yet
),
args = TrainingArguments(
per_device_train_batch_size = 2,
gradient_accumulation_steps = 4,
warmup_steps = 5,
# max_steps = 60, # Set max_steps or num_train_epochs
num_train_epochs = 1,
learning_rate = 2e-4,
fp16 = not torch.cuda.is_bf16_supported(),
bf16 = torch.cuda.is_bf16_supported(),
logging_steps = 1,
optim = "adamw_8bit",
weight_decay = 0.01,
lr_scheduler_type = "linear",
seed = 3407,
output_dir = "outputs_qwen_temporal",
),
)
# 5. Train
trainer_stats = trainer.train()
The rest of the code remains the same. This should give you a good starting point for fine-tuning the unsloth/Qwen3-14B-Base-unsloth-bnb-4bit model on your temporal conditioning task.
Inference
After training, you can use the same inference code as before to test your model:
# Load for inference (example using Unsloth's FastLanguageModel)
from unsloth import FastLanguageModel
model, tokenizer = FastLanguageModel.from_pretrained(
model_name = "lora_model_temporal",
max_seq_length = max_seq_length,
dtype = dtype,
load_in_4bit = load_in_4bit,
)
FastLanguageModel.for_inference(model)
# Define the year you want to test
test_year = 1925
system_prompt = f"You are a helpful assistant. The current year is {test_year}. Adapt your knowledge, vocabulary, and style to this era."
# Create the input using the SAME ChatML format
chat = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": "What is the most exciting new technology?"},
]
inputs = tokenizer.apply_chat_template(
chat,
tokenize = True,
add_generation_prompt = True, # Tells the model it's the assistant's turn
return_tensors = "pt"
).to("cuda")
# Generate
outputs = model.generate(input_ids = inputs, max_new_tokens = 200, use_cache = True)
response = tokenizer.batch_decode(outputs)[0]
print(response)
This should give you a response that reflects the temporal conditioning you provided during training.