Update new_review_code.py
Browse files- new_review_code.py +67 -58
new_review_code.py
CHANGED
@@ -1,74 +1,83 @@
|
|
1 |
-
import os
|
2 |
import torch
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
5 |
from datasets import load_dataset
|
6 |
-
|
7 |
-
from peft import AutoPeftModelForCausalLM, LoraConfig, get_peft_model, prepare_model_for_kbit_training
|
8 |
-
from utils import find_all_linear_names, print_trainable_parameters
|
9 |
|
10 |
-
|
11 |
-
|
|
|
|
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
20 |
)
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
|
30 |
-
#
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
38 |
)
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
#
|
44 |
-
training_args = TrainingArguments(
|
45 |
-
per_device_train_batch_size=1,
|
46 |
-
gradient_accumulation_steps=1,
|
47 |
-
gradient_checkpointing =True,
|
48 |
-
max_grad_norm= 0.3,
|
49 |
-
num_train_epochs=3,
|
50 |
-
learning_rate=1e-4,
|
51 |
-
bf16=True,
|
52 |
-
save_total_limit=3,
|
53 |
-
logging_steps=300,
|
54 |
-
output_dir=output_dir,
|
55 |
-
optim="paged_adamw_32bit",
|
56 |
-
lr_scheduler_type="constant",
|
57 |
-
warmup_ratio=0.05,
|
58 |
)
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
args=training_args
|
67 |
)
|
68 |
|
69 |
-
|
70 |
-
trainer.
|
|
|
|
|
|
|
|
|
71 |
|
72 |
-
|
73 |
-
trainer.model.save_pretrained(output_dir)
|
74 |
-
tokenizer.save_pretrained(output_dir)
|
|
|
|
|
1 |
import torch
|
2 |
+
from transformers import (
|
3 |
+
AutoModelForCausalLM,
|
4 |
+
AutoTokenizer,
|
5 |
+
TrainingArguments,
|
6 |
+
Trainer,
|
7 |
+
DataCollatorForLanguageModeling
|
8 |
+
)
|
9 |
from datasets import load_dataset
|
10 |
+
import pandas as pd
|
|
|
|
|
11 |
|
12 |
+
# Check GPU availability
|
13 |
+
print("CUDA Available:", torch.cuda.is_available())
|
14 |
+
print("Current Device:", torch.cuda.current_device())
|
15 |
+
print("Device Name:", torch.cuda.get_device_name(0))
|
16 |
|
17 |
+
# Load and prepare dataset
|
18 |
+
def load_custom_dataset(file_path):
|
19 |
+
# Read CSV
|
20 |
+
df = pd.read_csv(file_path)
|
21 |
+
|
22 |
+
# Ensure 'text' column exists
|
23 |
+
if 'text' not in df.columns:
|
24 |
+
raise ValueError("CSV must have a 'text' column")
|
25 |
+
|
26 |
+
# Convert to Hugging Face dataset
|
27 |
+
dataset = load_dataset('csv', data_files=file_path, split='train')
|
28 |
+
return dataset
|
29 |
|
30 |
+
# Model and Tokenizer Setup
|
31 |
+
model_name = "codellama/CodeLlama-7b-hf"
|
32 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
33 |
+
model = AutoModelForCausalLM.from_pretrained(
|
34 |
+
model_name,
|
35 |
+
torch_dtype=torch.float16, # Use float16 for memory efficiency
|
36 |
+
device_map="auto" # Automatic device mapping
|
37 |
)
|
38 |
|
39 |
+
# Tokenization function
|
40 |
+
def tokenize_function(examples):
|
41 |
+
return tokenizer(examples['text'], truncation=True, max_length=1024)
|
42 |
|
43 |
+
# Prepare dataset
|
44 |
+
dataset = load_custom_dataset('instructions.csv')
|
45 |
+
tokenized_dataset = dataset.map(tokenize_function, batched=True)
|
46 |
|
47 |
+
# Training Arguments
|
48 |
+
training_args = TrainingArguments(
|
49 |
+
output_dir="./ansible-review-model",
|
50 |
+
overwrite_output_dir=True,
|
51 |
+
num_train_epochs=4,
|
52 |
+
per_device_train_batch_size=2,
|
53 |
+
save_steps=10_000,
|
54 |
+
save_total_limit=2,
|
55 |
+
prediction_loss_only=True,
|
56 |
+
learning_rate=2e-4,
|
57 |
+
warmup_ratio=0.1,
|
58 |
+
fp16=True, # Use mixed precision
|
59 |
+
logging_dir='./logs',
|
60 |
)
|
61 |
|
62 |
+
# Data Collator
|
63 |
+
data_collator = DataCollatorForLanguageModeling(
|
64 |
+
tokenizer=tokenizer,
|
65 |
+
mlm=False # For causal language modeling
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
)
|
67 |
|
68 |
+
# Trainer
|
69 |
+
trainer = Trainer(
|
70 |
+
model=model,
|
71 |
+
args=training_args,
|
72 |
+
train_dataset=tokenized_dataset,
|
73 |
+
data_collator=data_collator,
|
|
|
74 |
)
|
75 |
|
76 |
+
# Start Training
|
77 |
+
trainer.train()
|
78 |
+
|
79 |
+
# Save Model and Tokenizer
|
80 |
+
trainer.save_model("./ansible-review-model")
|
81 |
+
tokenizer.save_pretrained("./ansible-review-model")
|
82 |
|
83 |
+
print("Training Complete!")
|
|
|
|