Upload scripts/train_7b_cloud.py with huggingface_hub
Browse files- scripts/train_7b_cloud.py +76 -0
scripts/train_7b_cloud.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""AutoTrain cloud script - trains Qwen 7B LoRA and pushes to HuggingFace Hub."""
|
| 2 |
+
import os
|
| 3 |
+
import torch
|
| 4 |
+
from datasets import load_dataset, concatenate_datasets
|
| 5 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments, BitsAndBytesConfig
|
| 6 |
+
from peft import LoraConfig, get_peft_model
|
| 7 |
+
from trl import SFTTrainer
|
| 8 |
+
from huggingface_hub import login
|
| 9 |
+
|
| 10 |
+
HF_TOKEN = os.environ["HF_TOKEN"]
|
| 11 |
+
MODEL_ID = "Qwen/Qwen2.5-7B-Instruct"
|
| 12 |
+
REPO_ID = "devsomosahub/agent-os-adapter-7b"
|
| 13 |
+
OUTPUT_DIR = "./output"
|
| 14 |
+
|
| 15 |
+
login(token=HF_TOKEN)
|
| 16 |
+
|
| 17 |
+
print("Loading dataset from Hub...")
|
| 18 |
+
ds = load_dataset("devsomosahub/agent-os-dataset", data_files="train.jsonl", split="train")
|
| 19 |
+
|
| 20 |
+
def format_example(example):
|
| 21 |
+
text = f"<|im_start|>system\nYou are a command adapter. Output ONLY valid JSON. No explanation.<|im_end|>\n<|im_start|>user\n{example['input']}<|im_end|>\n<|im_start|>assistant\n{example['output']}<|im_end|>"
|
| 22 |
+
return {"text": text}
|
| 23 |
+
|
| 24 |
+
ds = ds.map(format_example)
|
| 25 |
+
ds = concatenate_datasets([ds, ds, ds, ds])
|
| 26 |
+
print(f"Dataset: {len(ds)} examples")
|
| 27 |
+
|
| 28 |
+
print("Loading model (Q4)...")
|
| 29 |
+
bnb_config = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.float16)
|
| 30 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 31 |
+
if tokenizer.pad_token is None:
|
| 32 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 33 |
+
|
| 34 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, quantization_config=bnb_config, device_map="auto", trust_remote_code=True)
|
| 35 |
+
|
| 36 |
+
lora_config = LoraConfig(
|
| 37 |
+
r=32, lora_alpha=64,
|
| 38 |
+
target_modules=["q_proj", "v_proj", "k_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
|
| 39 |
+
lora_dropout=0.05, bias="none", task_type="CAUSAL_LM",
|
| 40 |
+
)
|
| 41 |
+
model = get_peft_model(model, lora_config)
|
| 42 |
+
model.print_trainable_parameters()
|
| 43 |
+
|
| 44 |
+
print("Starting training...")
|
| 45 |
+
training_args = TrainingArguments(
|
| 46 |
+
output_dir=OUTPUT_DIR,
|
| 47 |
+
num_train_epochs=7,
|
| 48 |
+
per_device_train_batch_size=4,
|
| 49 |
+
gradient_accumulation_steps=2,
|
| 50 |
+
learning_rate=2e-4,
|
| 51 |
+
fp16=True,
|
| 52 |
+
logging_steps=10,
|
| 53 |
+
save_strategy="steps",
|
| 54 |
+
save_steps=500,
|
| 55 |
+
warmup_ratio=0.1,
|
| 56 |
+
lr_scheduler_type="cosine",
|
| 57 |
+
report_to="none",
|
| 58 |
+
push_to_hub=True,
|
| 59 |
+
hub_model_id=REPO_ID,
|
| 60 |
+
hub_token=HF_TOKEN,
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
trainer = SFTTrainer(
|
| 64 |
+
model=model,
|
| 65 |
+
train_dataset=ds,
|
| 66 |
+
args=training_args,
|
| 67 |
+
processing_class=tokenizer,
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
trainer.train()
|
| 71 |
+
|
| 72 |
+
print("Pushing to Hub...")
|
| 73 |
+
trainer.push_to_hub()
|
| 74 |
+
model.push_to_hub(REPO_ID, token=HF_TOKEN)
|
| 75 |
+
tokenizer.push_to_hub(REPO_ID, token=HF_TOKEN)
|
| 76 |
+
print(f"DONE! Model at https://huggingface.co/{REPO_ID}")
|