IronWolfAI commited on
Commit
b4f5699
·
verified ·
1 Parent(s): c59ec53

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -32
app.py CHANGED
@@ -1,45 +1,95 @@
1
  import os
 
2
  from datasets import load_dataset
 
3
  import torch
4
- from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments
5
  from trl import SFTTrainer
6
 
7
- # Load the model and tokenizer
8
- model_name = "microsoft/phi-4-multimodal-instruct"
9
- model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16)
10
- tokenizer = AutoTokenizer.from_pretrained(model_name)
11
-
12
- # Load the dataset
13
- dataset = load_dataset("openai/gsm8k", "main")["train"]
14
-
15
- # Preprocess the dataset
16
- def preprocess_function(examples):
17
- return tokenizer(examples["question"], padding="max_length", truncation=True)
18
-
19
- dataset = dataset.map(preprocess_function, batched=True)
20
-
21
- # Define the training arguments
22
- training_args = TrainingArguments(
23
- output_dir="./results",
24
- per_device_train_batch_size=4,
25
- gradient_accumulation_steps=4,
26
- learning_rate=2e-5,
27
- num_train_epochs=1,
28
- fp16=True,
29
- logging_dir="./logs",
30
- report_to="none",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  )
 
 
32
 
33
- # Create the SFT trainer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  trainer = SFTTrainer(
35
  model=model,
36
- train_dataset=dataset,
37
- args=training_args,
 
 
 
 
38
  tokenizer=tokenizer,
 
39
  )
40
 
41
- # Train the model
42
- trainer.train()
 
 
 
 
 
 
 
 
 
43
 
44
- # Save the model
45
- trainer.save_model("./results")
 
1
  import os
2
+ import logging
3
  from datasets import load_dataset
4
+ from peft import LoraConfig
5
  import torch
6
+ import transformers
7
  from trl import SFTTrainer
8
 
9
+ # Hyper-parameters and configurations
10
+ training_config = {
11
+ "output_dir": "./results",
12
+ "per_device_train_batch_size": 4,
13
+ "gradient_accumulation_steps": 4,
14
+ "learning_rate": 2e-5,
15
+ "num_train_epochs": 1,
16
+ "fp16": True,
17
+ "logging_dir": "./logs",
18
+ "report_to": "none",
19
+ }
20
+
21
+ peft_config = {
22
+ "r": 16, # LoRA rank
23
+ "lora_alpha": 64, # LoRA alpha
24
+ "target_modules": ["q_proj", "k_proj", "v_proj", "o_proj"], # Target modules for LoRA
25
+ "bias": "none",
26
+ "task_type": "CAUSAL_LM",
27
+ }
28
+
29
+ train_conf = training_config # Rename to match the original script's variable name
30
+ peft_conf = LoraConfig(**peft_config)
31
+
32
+ # Setup logging
33
+ logging.basicConfig(
34
+ format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
35
+ datefmt="%Y-%m-%d %H:%M:%S",
36
+ handlers=[logging.StreamHandler()],
37
+ )
38
+ log_level = logging.INFO # Set log level, you can adjust this based on your preference
39
+ logger = logging.getLogger(__name__)
40
+ logger.setLevel(log_level)
41
+
42
+ # Model Loading and Tokenizer Configuration
43
+ checkpoint_path = "microsoft/Phi-4-mini-instruct"
44
+ model_kwargs = dict(
45
+ use_cache=False,
46
+ trust_remote_code=True,
47
+ attn_implementation="flash_attention_2",
48
+ torch_dtype=torch.bfloat16,
49
+ device_map=None,
50
  )
51
+ model = transformers.AutoModelForCausalLM.from_pretrained(checkpoint_path, **model_kwargs)
52
+ tokenizer = transformers.AutoTokenizer.from_pretrained(checkpoint_path)
53
 
54
+ # Data Processing
55
+ def apply_chat_template(example):
56
+ messages = example["messages"]
57
+ # Assuming a function that converts chat messages into text for the model
58
+ example["text"] = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=False)
59
+ return example
60
+
61
+ train_dataset, test_dataset = load_dataset("HuggingFaceH4/ultrachat_200k", split=["train_sft", "test_sft"])
62
+ column_names = list(train_dataset.features)
63
+ processed_train_dataset = train_dataset.map(
64
+ apply_chat_template,
65
+ num_proc=10,
66
+ remove_columns=column_names,
67
+ )
68
+
69
+ # Training
70
  trainer = SFTTrainer(
71
  model=model,
72
+ args=train_conf,
73
+ peft_config=peft_conf,
74
+ train_dataset=processed_train_dataset,
75
+ eval_dataset=test_dataset, # Assuming you want to evaluate on the test set after training
76
+ max_seq_length=2048,
77
+ dataset_text_field="text",
78
  tokenizer=tokenizer,
79
+ packing=True,
80
  )
81
 
82
+ train_result = trainer.train()
83
+ metrics = train_result.metrics
84
+ trainer.log_metrics("train", metrics)
85
+ trainer.save_metrics("train", metrics)
86
+ trainer.save_state()
87
+
88
+ # Evaluation (assuming evaluation after training, otherwise comment out)
89
+ metrics = trainer.evaluate()
90
+ metrics["eval_samples"] = len(test_dataset)
91
+ trainer.log_metrics("eval", metrics)
92
+ trainer.save_metrics("eval", metrics)
93
 
94
+ # Save model
95
+ trainer.save_model(train_conf["output_dir"])