4moha commited on
Commit
de608a4
·
verified ·
1 Parent(s): 92e3646

fix: pre-render chat template via .map() instead of formatting_func

Browse files
Files changed (1) hide show
  1. train_lora.py +11 -12
train_lora.py CHANGED
@@ -58,30 +58,29 @@ def main() -> None:
58
  random_state=42,
59
  )
60
 
61
- # Load dataset and split off a tiny eval slice for live monitoring during training
 
 
62
  full = load_dataset(DATASET_REPO, data_files="train.jsonl", split="train")
63
- split = full.train_test_split(test_size=0.05, seed=42)
64
 
65
- # Unsloth's wrapper invokes formatting_func with a BATCH (dict of lists) and
66
- # expects a list-of-strings back — NOT TRL's per-example shape. Iterate the
67
- # batch and apply the chat template to each turn-list.
68
- def formatting_func(examples: dict) -> list[str]:
69
- return [
70
- tokenizer.apply_chat_template(
71
- msgs,
72
  tokenize=False,
73
  add_generation_prompt=False,
74
  )
75
- for msgs in examples["messages"]
76
- ]
 
77
 
78
  trainer = SFTTrainer(
79
  model=model,
80
  tokenizer=tokenizer,
81
  train_dataset=split["train"],
82
  eval_dataset=split["test"],
83
- formatting_func=formatting_func,
84
  args=SFTConfig(
 
85
  output_dir="raunch-stheno-v3.4-lora-v0",
86
  push_to_hub=True,
87
  hub_model_id=MODEL_REPO,
 
58
  random_state=42,
59
  )
60
 
61
+ # Load dataset, pre-render the chat template into a single "text" column,
62
+ # then split. Avoids version-skew on TRL/Unsloth's formatting_func contracts —
63
+ # SFTTrainer reads dataset_text_field="text" and tokenizes directly.
64
  full = load_dataset(DATASET_REPO, data_files="train.jsonl", split="train")
 
65
 
66
+ def render_chat(example: dict) -> dict:
67
+ return {
68
+ "text": tokenizer.apply_chat_template(
69
+ example["messages"],
 
 
 
70
  tokenize=False,
71
  add_generation_prompt=False,
72
  )
73
+ }
74
+ full = full.map(render_chat, remove_columns=["messages"])
75
+ split = full.train_test_split(test_size=0.05, seed=42)
76
 
77
  trainer = SFTTrainer(
78
  model=model,
79
  tokenizer=tokenizer,
80
  train_dataset=split["train"],
81
  eval_dataset=split["test"],
 
82
  args=SFTConfig(
83
+ dataset_text_field="text",
84
  output_dir="raunch-stheno-v3.4-lora-v0",
85
  push_to_hub=True,
86
  hub_model_id=MODEL_REPO,