File size: 12,973 Bytes
b010003 |
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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
#!/usr/bin/env python3
"""
Helion-2.5-Rnd Batch Inference
Efficient batch processing for large-scale inference tasks
"""
import argparse
import json
import logging
import time
from pathlib import Path
from typing import Dict, List, Optional, Union
import pandas as pd
from tqdm import tqdm
from inference.client import HelionClient
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
class BatchProcessor:
"""Process large batches of inference requests"""
def __init__(
self,
client: HelionClient,
batch_size: int = 10,
max_retries: int = 3,
retry_delay: float = 1.0
):
"""
Initialize batch processor
Args:
client: HelionClient instance
batch_size: Number of requests to process concurrently
max_retries: Maximum retry attempts for failed requests
retry_delay: Delay between retries in seconds
"""
self.client = client
self.batch_size = batch_size
self.max_retries = max_retries
self.retry_delay = retry_delay
self.stats = {
'total': 0,
'successful': 0,
'failed': 0,
'total_time': 0.0,
'avg_time_per_request': 0.0
}
def process_prompts(
self,
prompts: List[str],
temperature: float = 0.7,
max_tokens: int = 1024,
**kwargs
) -> List[Dict]:
"""
Process a list of prompts
Args:
prompts: List of input prompts
temperature: Sampling temperature
max_tokens: Maximum tokens per response
**kwargs: Additional generation parameters
Returns:
List of results with prompt, response, and metadata
"""
results = []
start_time = time.time()
logger.info(f"Processing {len(prompts)} prompts...")
for i in tqdm(range(0, len(prompts), self.batch_size)):
batch = prompts[i:i + self.batch_size]
for prompt in batch:
result = self._process_single_with_retry(
prompt,
temperature,
max_tokens,
**kwargs
)
results.append(result)
# Update statistics
self.stats['total'] = len(prompts)
self.stats['successful'] = sum(1 for r in results if r['success'])
self.stats['failed'] = len(prompts) - self.stats['successful']
self.stats['total_time'] = time.time() - start_time
self.stats['avg_time_per_request'] = self.stats['total_time'] / len(prompts)
logger.info(f"Batch processing complete. Success rate: {self.stats['successful']}/{self.stats['total']}")
return results
def _process_single_with_retry(
self,
prompt: str,
temperature: float,
max_tokens: int,
**kwargs
) -> Dict:
"""Process single prompt with retry logic"""
for attempt in range(self.max_retries):
try:
start = time.time()
response = self.client.complete(
prompt=prompt,
temperature=temperature,
max_tokens=max_tokens,
**kwargs
)
duration = time.time() - start
return {
'prompt': prompt,
'response': response,
'success': True,
'duration': duration,
'attempts': attempt + 1
}
except Exception as e:
logger.warning(f"Attempt {attempt + 1} failed: {str(e)}")
if attempt < self.max_retries - 1:
time.sleep(self.retry_delay)
else:
return {
'prompt': prompt,
'response': None,
'success': False,
'error': str(e),
'attempts': attempt + 1
}
def process_chat_conversations(
self,
conversations: List[List[Dict]],
temperature: float = 0.7,
max_tokens: int = 1024,
**kwargs
) -> List[Dict]:
"""
Process chat conversations in batch
Args:
conversations: List of message lists
temperature: Sampling temperature
max_tokens: Maximum tokens per response
**kwargs: Additional generation parameters
Returns:
List of conversation results
"""
results = []
start_time = time.time()
logger.info(f"Processing {len(conversations)} conversations...")
for conv in tqdm(conversations):
try:
start = time.time()
response = self.client.chat(
messages=conv,
temperature=temperature,
max_tokens=max_tokens,
**kwargs
)
duration = time.time() - start
results.append({
'conversation': conv,
'response': response,
'success': True,
'duration': duration
})
except Exception as e:
logger.error(f"Conversation processing failed: {str(e)}")
results.append({
'conversation': conv,
'response': None,
'success': False,
'error': str(e)
})
total_time = time.time() - start_time
successful = sum(1 for r in results if r['success'])
logger.info(f"Processed {successful}/{len(conversations)} conversations in {total_time:.2f}s")
return results
def process_file(
self,
input_file: str,
output_file: str,
prompt_column: str = "prompt",
temperature: float = 0.7,
max_tokens: int = 1024,
**kwargs
) -> pd.DataFrame:
"""
Process prompts from file
Args:
input_file: Input CSV/JSON file path
output_file: Output file path
prompt_column: Column name containing prompts
temperature: Sampling temperature
max_tokens: Maximum tokens per response
**kwargs: Additional generation parameters
Returns:
DataFrame with results
"""
# Load input file
input_path = Path(input_file)
if input_path.suffix == '.csv':
df = pd.read_csv(input_path)
elif input_path.suffix == '.json':
df = pd.read_json(input_path)
else:
raise ValueError(f"Unsupported file format: {input_path.suffix}")
if prompt_column not in df.columns:
raise ValueError(f"Column '{prompt_column}' not found in input file")
# Process prompts
prompts = df[prompt_column].tolist()
results = self.process_prompts(
prompts,
temperature=temperature,
max_tokens=max_tokens,
**kwargs
)
# Add results to dataframe
df['response'] = [r['response'] for r in results]
df['success'] = [r['success'] for r in results]
df['duration'] = [r.get('duration', None) for r in results]
df['error'] = [r.get('error', None) for r in results]
# Save results
output_path = Path(output_file)
output_path.parent.mkdir(parents=True, exist_ok=True)
if output_path.suffix == '.csv':
df.to_csv(output_path, index=False)
elif output_path.suffix == '.json':
df.to_json(output_path, orient='records', indent=2)
else:
raise ValueError(f"Unsupported output format: {output_path.suffix}")
logger.info(f"Results saved to {output_path}")
return df
def get_statistics(self) -> Dict:
"""Get processing statistics"""
return self.stats.copy()
class DatasetProcessor:
"""Process specific dataset formats"""
def __init__(self, client: HelionClient):
self.client = client
self.processor = BatchProcessor(client)
def process_qa_dataset(
self,
questions: List[str],
contexts: Optional[List[str]] = None,
temperature: float = 0.3,
max_tokens: int = 512
) -> List[Dict]:
"""Process question-answering dataset"""
prompts = []
for i, question in enumerate(questions):
if contexts and i < len(contexts):
prompt = f"Context: {contexts[i]}\n\nQuestion: {question}\n\nAnswer:"
else:
prompt = f"Question: {question}\n\nAnswer:"
prompts.append(prompt)
return self.processor.process_prompts(
prompts,
temperature=temperature,
max_tokens=max_tokens
)
def process_code_dataset(
self,
tasks: List[str],
languages: Optional[List[str]] = None,
temperature: float = 0.2,
max_tokens: int = 1024
) -> List[Dict]:
"""Process code generation tasks"""
prompts = []
for i, task in enumerate(tasks):
lang = languages[i] if languages and i < len(languages) else "python"
prompt = f"Write a {lang} function to: {task}\n\n```{lang}\n"
prompts.append(prompt)
return self.processor.process_prompts(
prompts,
temperature=temperature,
max_tokens=max_tokens
)
def process_translation_dataset(
self,
texts: List[str],
source_lang: str,
target_lang: str,
temperature: float = 0.3,
max_tokens: int = 1024
) -> List[Dict]:
"""Process translation tasks"""
prompts = []
for text in texts:
prompt = f"Translate the following text from {source_lang} to {target_lang}:\n\n{text}\n\nTranslation:"
prompts.append(prompt)
return self.processor.process_prompts(
prompts,
temperature=temperature,
max_tokens=max_tokens
)
def process_summarization_dataset(
self,
documents: List[str],
max_summary_length: int = 150,
temperature: float = 0.5,
max_tokens: int = 512
) -> List[Dict]:
"""Process document summarization"""
prompts = []
for doc in documents:
prompt = f"Summarize the following document in {max_summary_length} words or less:\n\n{doc}\n\nSummary:"
prompts.append(prompt)
return self.processor.process_prompts(
prompts,
temperature=temperature,
max_tokens=max_tokens
)
def main():
"""Main batch processing entry point"""
parser = argparse.ArgumentParser(description="Batch inference with Helion")
parser.add_argument("--base-url", type=str, default="http://localhost:8000")
parser.add_argument("--input", type=str, required=True, help="Input file (CSV/JSON)")
parser.add_argument("--output", type=str, required=True, help="Output file (CSV/JSON)")
parser.add_argument("--prompt-column", type=str, default="prompt")
parser.add_argument("--temperature", type=float, default=0.7)
parser.add_argument("--max-tokens", type=int, default=1024)
parser.add_argument("--batch-size", type=int, default=10)
args = parser.parse_args()
# Initialize client and processor
client = HelionClient(base_url=args.base_url)
processor = BatchProcessor(client, batch_size=args.batch_size)
# Process file
df = processor.process_file(
input_file=args.input,
output_file=args.output,
prompt_column=args.prompt_column,
temperature=args.temperature,
max_tokens=args.max_tokens
)
# Print statistics
stats = processor.get_statistics()
logger.info("\nProcessing Statistics:")
logger.info(f"Total requests: {stats['total']}")
logger.info(f"Successful: {stats['successful']}")
logger.info(f"Failed: {stats['failed']}")
logger.info(f"Total time: {stats['total_time']:.2f}s")
logger.info(f"Avg time per request: {stats['avg_time_per_request']:.2f}s")
if __name__ == "__main__":
main() |