-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup_inference.py
More file actions
166 lines (138 loc) · 6.11 KB
/
Copy pathsetup_inference.py
File metadata and controls
166 lines (138 loc) · 6.11 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python3
"""
Setup script for Math-Eval inference environment.
Handles both API-based and open-source model requirements.
"""
import subprocess
import sys
import argparse
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
def run_command(cmd, description):
"""Run a command and handle errors."""
logger.info(f"Running: {description}")
try:
result = subprocess.run(cmd, shell=True, check=True, capture_output=True, text=True)
if result.stdout:
logger.info(result.stdout)
return True
except subprocess.CalledProcessError as e:
logger.error(f"Failed: {description}")
if e.stderr:
logger.error(e.stderr)
return False
def install_base_requirements():
"""Install base requirements for inference."""
logger.info("Installing base inference requirements...")
cmd = f"{sys.executable} -m pip install pandas pillow tqdm numpy"
return run_command(cmd, "Installing base packages")
def install_api_requirements():
"""Install requirements for API-based models."""
logger.info("Installing API model requirements...")
cmd = f"{sys.executable} -m pip install openai google-generativeai"
return run_command(cmd, "Installing API model packages")
def install_opensource_requirements():
"""Install requirements for open-source models."""
logger.info("Installing open-source model requirements...")
# Check if CUDA is available
try:
import torch
cuda_available = torch.cuda.is_available()
logger.info(f"CUDA available: {cuda_available}")
except ImportError:
cuda_available = False
logger.info("PyTorch not installed yet, assuming CUDA setup")
# Install PyTorch with appropriate CUDA support
if cuda_available or True: # Assume CUDA for now
torch_cmd = f"{sys.executable} -m pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118"
else:
torch_cmd = f"{sys.executable} -m pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu"
if not run_command(torch_cmd, "Installing PyTorch"):
return False
# Install transformers and related packages
transformers_cmd = f"{sys.executable} -m pip install transformers>=4.30.0 accelerate>=0.20.0"
if not run_command(transformers_cmd, "Installing Transformers"):
return False
# Install model-specific requirements
model_specific_cmd = f"{sys.executable} -m pip install qwen-vl-utils einops bitsandbytes"
if not run_command(model_specific_cmd, "Installing model-specific packages"):
return False
# Optional: Install flash attention for better performance
flash_attn_cmd = f"{sys.executable} -m pip install flash-attn --no-build-isolation"
run_command(flash_attn_cmd, "Installing flash attention (optional)")
return True
def setup_inference_environment():
"""Setup the complete inference environment."""
logger.info("Setting up Math-Eval inference environment...")
# Always install base requirements
if not install_base_requirements():
logger.error("Failed to install base requirements")
return False
logger.info("✅ Base requirements installed successfully")
return True
def validate_installation(model_type):
"""Validate that the installation was successful."""
logger.info(f"Validating {model_type} installation...")
try:
import pandas
import PIL
import tqdm
import numpy
logger.info("✅ Base packages validated")
except ImportError as e:
logger.error(f"❌ Base package validation failed: {e}")
return False
if model_type in ['api', 'all']:
try:
import openai
import google.generativeai
logger.info("✅ API packages validated")
except ImportError as e:
logger.error(f"❌ API package validation failed: {e}")
return False
if model_type in ['opensource', 'all']:
try:
import torch
import transformers
logger.info(f"✅ Open-source packages validated (CUDA: {torch.cuda.is_available()})")
except ImportError as e:
logger.error(f"❌ Open-source package validation failed: {e}")
return False
return True
def main():
parser = argparse.ArgumentParser(description="Setup Math-Eval inference environment")
parser.add_argument('--model_type', type=str, choices=['api', 'opensource', 'all'],
default='all', help='Type of models to setup for')
parser.add_argument('--validate_only', action='store_true',
help='Only validate existing installation')
args = parser.parse_args()
if args.validate_only:
success = validate_installation(args.model_type)
sys.exit(0 if success else 1)
# Setup base environment
if not setup_inference_environment():
logger.error("Failed to setup inference environment")
sys.exit(1)
# Install specific model requirements
if args.model_type in ['api', 'all']:
if install_api_requirements():
logger.info("✅ API model requirements installed")
else:
logger.error("❌ Failed to install API model requirements")
if args.model_type in ['opensource', 'all']:
if install_opensource_requirements():
logger.info("✅ Open-source model requirements installed")
else:
logger.error("❌ Failed to install open-source model requirements")
# Validate installation
if validate_installation(args.model_type):
logger.info("🎉 Inference environment setup completed successfully!")
logger.info("Next steps:")
logger.info("1. Configure your API keys in inference_config.json")
logger.info("2. Run inference with: python run_inference.py --help")
else:
logger.error("❌ Installation validation failed")
sys.exit(1)
if __name__ == "__main__":
main()