hackergeek commited on
Commit
92992ea
Β·
verified Β·
1 Parent(s): 78dadb4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -21
app.py CHANGED
@@ -3,7 +3,6 @@ import gradio as gr
3
  from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments, Trainer
4
  from peft import get_peft_model, LoraConfig, TaskType
5
  from datasets import load_dataset
6
- from bitsandbytes import BitsAndBytesConfig
7
 
8
  # βœ… Check if a GPU is available, otherwise use CPU
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
@@ -11,24 +10,11 @@ device = "cuda" if torch.cuda.is_available() else "cpu"
11
  # βœ… Function to start training
12
  def train_model(dataset_url, model_url, epochs):
13
  try:
14
- # Load the tokenizer
15
  tokenizer = AutoTokenizer.from_pretrained(model_url)
 
16
 
17
- # βœ… Load model with 4-bit quantization for CPU efficiency
18
- bnb_config = BitsAndBytesConfig(
19
- load_in_4bit=True if device == "cuda" else False,
20
- bnb_4bit_quant_type="nf4",
21
- bnb_4bit_compute_dtype=torch.bfloat16,
22
- bnb_4bit_use_double_quant=True
23
- )
24
-
25
- model = AutoModelForCausalLM.from_pretrained(
26
- model_url,
27
- quantization_config=bnb_config if device == "cuda" else None,
28
- device_map=device
29
- )
30
-
31
- # βœ… Apply LoRA for efficient training
32
  lora_config = LoraConfig(
33
  task_type=TaskType.CAUSAL_LM,
34
  r=8,
@@ -50,20 +36,20 @@ def train_model(dataset_url, model_url, epochs):
50
  tokenized_datasets = dataset.map(tokenize_function, batched=True)
51
  train_dataset = tokenized_datasets["train"]
52
 
53
- # βœ… Training Arguments
54
  training_args = TrainingArguments(
55
  output_dir="./deepseek_lora_cpu",
56
  evaluation_strategy="epoch",
57
  learning_rate=5e-4,
58
- per_device_train_batch_size=1,
59
  per_device_eval_batch_size=1,
60
  num_train_epochs=int(epochs),
61
  save_strategy="epoch",
62
  save_total_limit=2,
63
  logging_dir="./logs",
64
  logging_steps=10,
65
- fp16=False,
66
- gradient_checkpointing=True,
67
  optim="adamw_torch",
68
  report_to="none"
69
  )
 
3
  from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments, Trainer
4
  from peft import get_peft_model, LoraConfig, TaskType
5
  from datasets import load_dataset
 
6
 
7
  # βœ… Check if a GPU is available, otherwise use CPU
8
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
10
  # βœ… Function to start training
11
  def train_model(dataset_url, model_url, epochs):
12
  try:
13
+ # Load tokenizer and model
14
  tokenizer = AutoTokenizer.from_pretrained(model_url)
15
+ model = AutoModelForCausalLM.from_pretrained(model_url).to(device)
16
 
17
+ # βœ… Apply LoRA (Reduces trainable parameters)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  lora_config = LoraConfig(
19
  task_type=TaskType.CAUSAL_LM,
20
  r=8,
 
36
  tokenized_datasets = dataset.map(tokenize_function, batched=True)
37
  train_dataset = tokenized_datasets["train"]
38
 
39
+ # βœ… Training Arguments (Optimized for CPU)
40
  training_args = TrainingArguments(
41
  output_dir="./deepseek_lora_cpu",
42
  evaluation_strategy="epoch",
43
  learning_rate=5e-4,
44
+ per_device_train_batch_size=1, # Keeps memory low
45
  per_device_eval_batch_size=1,
46
  num_train_epochs=int(epochs),
47
  save_strategy="epoch",
48
  save_total_limit=2,
49
  logging_dir="./logs",
50
  logging_steps=10,
51
+ fp16=False, # Disable FP16 for CPU
52
+ gradient_checkpointing=True, # Saves memory
53
  optim="adamw_torch",
54
  report_to="none"
55
  )