AdriBat1 commited on
Commit
5f654f8
·
1 Parent(s): 979e977

Add and verify LLM training and inference examples

Browse files
remote-gpu-client/examples/inference_llm.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Remote LLM Inference
4
+ ====================
5
+ Usa il modello LLM salvato sul server (nella directory persistente)
6
+ per generare testo da un prompt.
7
+ """
8
+
9
+ import sys
10
+ from antigravity_sdk import RemoteGPU
11
+
12
+ # Default prompts
13
+ PROMPTS = [
14
+ "The future of artificial intelligence is",
15
+ "Once upon a time in a digital world,",
16
+ "The best way to learn Python is"
17
+ ]
18
+
19
+ INFERENCE_CODE = r'''
20
+ import os
21
+ import sys
22
+
23
+ print("🔧 Setting up Environment...")
24
+ os.system(f"{sys.executable} -m pip install transformers==4.37.2 accelerate==0.27.2 --quiet")
25
+
26
+
27
+
28
+
29
+ import torch
30
+ from transformers import AutoTokenizer, AutoModelForCausalLM
31
+
32
+ # 1. Configuration
33
+ STORAGE_DIR = "/home/user/app/storage/my_llm"
34
+ print(f"📂 Loading LLM from {STORAGE_DIR}...")
35
+
36
+ if not os.path.exists(STORAGE_DIR):
37
+ print("❌ Model not found! Run 'train_llm.py' first.")
38
+ sys.exit(1)
39
+
40
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
41
+ print(f" Using device: {device}")
42
+
43
+ # 2. Load Model & Tokenizer
44
+ try:
45
+ tokenizer = AutoTokenizer.from_pretrained(STORAGE_DIR)
46
+ model = AutoModelForCausalLM.from_pretrained(STORAGE_DIR).to(device)
47
+ print("✅ Model loaded successfully!")
48
+ except Exception as e:
49
+ print(f"❌ Error loading model: {e}")
50
+ sys.exit(1)
51
+
52
+ # 3. Generate
53
+ prompts = {PROMPTS_PLACEHOLDER}
54
+
55
+ print("\n🔮 Generating Text...")
56
+ print("=" * 60)
57
+
58
+ for prompt in prompts:
59
+ print(f"📝 Prompt: {prompt}")
60
+ inputs = tokenizer(prompt, return_tensors="pt").to(device)
61
+
62
+ with torch.no_grad():
63
+ output = model.generate(
64
+ **inputs,
65
+ max_length=100,
66
+ num_return_sequences=1,
67
+ temperature=0.7,
68
+ do_sample=True,
69
+ pad_token_id=tokenizer.eos_token_id
70
+ )
71
+
72
+ generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
73
+ print(f"🤖 Output:\n{generated_text}")
74
+ print("-" * 60)
75
+
76
+ print("✅ Generation Complete.")
77
+ '''
78
+
79
+ def main():
80
+ print("📡 Connecting to Remote GPU for LLM Inference...")
81
+ gpu = RemoteGPU()
82
+
83
+ # Inject prompts into code
84
+ code_to_run = INFERENCE_CODE.replace("{PROMPTS_PLACEHOLDER}", str(PROMPTS))
85
+
86
+ result = gpu.run(code_to_run)
87
+
88
+ if __name__ == "__main__":
89
+ main()
remote-gpu-client/examples/train_llm.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Remote LLM Training (DistilGPT2 on Wikitext)
4
+ ============================================
5
+ Allena un piccolo LLM (DistilGPT2) su un dataset di testo (Wikitext-2)
6
+ direttamente sulla GPU remota e salva il modello persistente.
7
+ """
8
+
9
+ from antigravity_sdk import RemoteGPU
10
+
11
+ TRAINING_CODE = r'''
12
+ import os
13
+ import sys
14
+
15
+ print("🔧 Setting up Environment...")
16
+ # Pin compatible versions for PyTorch 2.1.2
17
+ os.system(f"{sys.executable} -m pip install transformers==4.37.2 datasets==2.17.0 accelerate==0.27.2 --quiet")
18
+
19
+
20
+
21
+ import torch
22
+ from transformers import AutoTokenizer, AutoModelForCausalLM, Trainer, TrainingArguments, TextDataset, DataCollatorForLanguageModeling
23
+ from datasets import load_dataset
24
+
25
+ print("🚀 Starting LLM Training...")
26
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
27
+ print(f" Using device: {device}")
28
+
29
+ # 1. Configuration
30
+ MODEL_NAME = "distilgpt2"
31
+ STORAGE_DIR = "/home/user/app/storage/my_llm"
32
+ os.makedirs(STORAGE_DIR, exist_ok=True)
33
+
34
+ # 2. Load Tokenizer & Model
35
+ print(f" Loading {MODEL_NAME}...")
36
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
37
+ tokenizer.pad_token = tokenizer.eos_token # Fix for GPT-2 which has no pad token
38
+ model = AutoModelForCausalLM.from_pretrained(MODEL_NAME).to(device)
39
+
40
+
41
+ # 3. Prepare Dataset (Wikitext-2 small subset for speed)
42
+ print(" Loading dataset (wikitext-2)...")
43
+ # For simplicity/speed in this demo, accessing a small raw text subset or using 'wikitext' library
44
+ dataset = load_dataset("wikitext", "wikitext-2-raw-v1", split="train[:1%]") # 1% just for demo speed
45
+ print(f" Dataset loaded. Rows: {len(dataset)}")
46
+
47
+ # Helper to tokenize
48
+ def tokenize_function(examples):
49
+ return tokenizer(examples["text"], padding="max_length", truncation=True, max_length=128)
50
+
51
+ print(" Tokenizing...")
52
+ tokenized_datasets = dataset.map(tokenize_function, batched=True)
53
+ tokenized_datasets = tokenized_datasets.remove_columns(["text"])
54
+ tokenized_datasets.set_format("torch")
55
+
56
+ data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False)
57
+
58
+ # 4. Training Arguments
59
+ training_args = TrainingArguments(
60
+ output_dir="./results",
61
+ overwrite_output_dir=True,
62
+ num_train_epochs=1,
63
+ per_device_train_batch_size=4,
64
+ save_steps=500,
65
+ save_total_limit=1,
66
+ report_to="none",
67
+ disable_tqdm=True # Cleaner output logs
68
+ )
69
+
70
+ # 5. Trainer
71
+ trainer = Trainer(
72
+ model=model,
73
+ args=training_args,
74
+ train_dataset=tokenized_datasets,
75
+ data_collator=data_collator,
76
+ )
77
+
78
+ # 6. Train
79
+ print(" Starting Fine-Tuning...")
80
+ trainer.train()
81
+
82
+ # 7. Save Persistently
83
+ print(f" 💾 Saving model to {STORAGE_DIR}...")
84
+ model.save_pretrained(STORAGE_DIR)
85
+ tokenizer.save_pretrained(STORAGE_DIR)
86
+
87
+ # 8. Test Generation
88
+ print(" Testing generation...")
89
+ input_text = "The future of AI is"
90
+ inputs = tokenizer(input_text, return_tensors="pt").to(device)
91
+ output = model.generate(**inputs, max_length=50, num_return_sequences=1)
92
+ generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
93
+
94
+ print("-" * 40)
95
+ print(f"Input: {input_text}")
96
+ print(f"Output: {generated_text}")
97
+ print("-" * 40)
98
+
99
+ print("✅ LLM Training Complete & Model Saved.")
100
+ '''
101
+
102
+ def main():
103
+ print("📡 Connecting to Remote GPU for LLM Training...")
104
+ gpu = RemoteGPU()
105
+
106
+ # Run, getting logs mostly
107
+ result = gpu.run(TRAINING_CODE)
108
+
109
+ if "Training Complete" in result.output:
110
+ print("\n🏆 LLM Addestrato e Salvato sul Server!")
111
+ else:
112
+ print("\n⚠️ Qualcosa è andato storto (controlla i log sopra).")
113
+
114
+ if __name__ == "__main__":
115
+ main()