Repository navigation
Expand file tree
/
Copy pathtest_llamacpp.py
More file actions
153 lines (125 loc) · 4.64 KB
/
Copy pathtest_llamacpp.py
File metadata and controls
153 lines (125 loc) · 4.64 KB
1
2
3
4
5
6
7
8
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"""
Test llama-cpp-python installation and GPU support
"""
import sys
from pathlib import Path
print("=" * 60)
print("Testing llama-cpp-python installation...")
print("=" * 60)
# Test 1: Import check
try:
from llama_cpp import Llama
print("✅ llama-cpp-python imported successfully")
except ImportError as e:
print(f"❌ Failed to import llama-cpp-python: {e}")
sys.exit(1)
# Test 2: Check for CUDA support
try:
from llama_cpp import llama_supports_gpu_offload
if llama_supports_gpu_offload():
print("✅ GPU offload is supported (CUDA enabled)")
else:
print("⚠️ GPU offload not available (CPU only)")
except Exception as e:
print(f"⚠️ Could not check GPU support: {e}")
print("\n" + "=" * 60)
print("Downloading test model (Qwen 3 32B 4-bit GGUF)...")
print("This will take a few minutes (~20GB download)")
print("=" * 60)
# Test 3: Download Qwen 3 32B model
model_dir = Path("./models")
model_dir.mkdir(exist_ok=True)
# Using huggingface_hub to download
try:
from huggingface_hub import hf_hub_download
print("✅ huggingface_hub available")
except ImportError:
print("⚠️ Installing huggingface_hub...")
import subprocess
subprocess.check_call([sys.executable, "-m", "pip", "install", "-q", "huggingface_hub"])
from huggingface_hub import hf_hub_download
print("✅ huggingface_hub installed")
# Download model
model_path = model_dir / "qwen3-32b-instruct-q4_k_m.gguf"
if model_path.exists():
print(f"✅ Model already downloaded: {model_path}")
else:
print("\n📥 Downloading Qwen 3 32B (Q4_K_M, ~20GB)...")
print("This may take 10-20 minutes depending on your internet speed...")
try:
downloaded_path = hf_hub_download(
repo_id="Qwen/Qwen2.5-32B-Instruct-GGUF",
filename="qwen2.5-32b-instruct-q4_k_m.gguf",
local_dir=str(model_dir),
local_dir_use_symlinks=False
)
# Rename to our expected name
Path(downloaded_path).rename(model_path)
print(f"✅ Model downloaded to: {model_path}")
except Exception as e:
print(f"❌ Failed to download model: {e}")
print("\nAlternative: Manually download from:")
print("https://huggingface.co/Qwen/Qwen2.5-32B-Instruct-GGUF")
sys.exit(1)
print("\n" + "=" * 60)
print("Loading model and testing inference...")
print("=" * 60)
# Test 4: Load model and run inference
try:
print(f"\n🔄 Loading {model_path.name}...")
print("This will use ~20GB of your VRAM...")
llm = Llama(
model_path=str(model_path),
n_gpu_layers=-1, # Offload all layers to GPU
n_ctx=8192, # Context window
n_threads=8, # CPU threads for prompt processing
verbose=False
)
print("✅ Model loaded successfully!")
print(f" Context size: {llm.n_ctx()}")
print(f" GPU layers: All (-1)")
# Test inference
print("\n🧪 Testing inference with a medical extraction task...")
test_prompt = """Extract medical entities from this text:
"The patient presented with hypertension and was prescribed lisinopril 10mg daily."
Return JSON with entities array containing entity_name and entity_type."""
print(f"\n📝 Prompt: {test_prompt[:100]}...")
import time
start = time.time()
response = llm(
test_prompt,
max_tokens=300,
temperature=0.1,
stop=["</s>", "\n\n\n"]
)
elapsed = time.time() - start
print(f"\n✅ Inference completed in {elapsed:.2f}s")
print(f"\n📤 Response:")
print("-" * 60)
print(response['choices'][0]['text'])
print("-" * 60)
# Calculate tokens per second
tokens_generated = response['usage']['completion_tokens']
tokens_per_sec = tokens_generated / elapsed
print(f"\n📊 Performance:")
print(f" Tokens generated: {tokens_generated}")
print(f" Speed: {tokens_per_sec:.1f} tokens/sec")
print(f" Total time: {elapsed:.2f}s")
if tokens_per_sec > 50:
print(" ✅ Excellent speed (GPU acceleration working!)")
elif tokens_per_sec > 20:
print(" ✅ Good speed")
else:
print(" ⚠️ Slow - check GPU usage")
print("\n" + "=" * 60)
print("🎉 All tests passed! llama-cpp-python is ready to use.")
print("=" * 60)
except Exception as e:
print(f"❌ Error during inference test: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
print("\n💡 Next steps:")
print(" 1. Run: python run_pipeline.py --use-local-llm --test-mode")
print(" 2. Check GPU usage in another terminal: nvidia-smi -l 1")
print(" 3. Compare speed vs Claude Bedrock")