File size: 18,721 Bytes
01b0ba7 bbad13f 01b0ba7 bbad13f 01b0ba7 bbad13f 01b0ba7 bbad13f 01b0ba7 bbad13f 01b0ba7 bbad13f 01b0ba7 bbad13f 01b0ba7 bbad13f 01b0ba7 bbad13f 01b0ba7 bbad13f 01b0ba7 bbad13f 01b0ba7 bbad13f 01b0ba7 bbad13f 01b0ba7 bbad13f 01b0ba7 bbad13f 01b0ba7 bbad13f 01b0ba7 bbad13f 01b0ba7 bbad13f 01b0ba7 bbad13f 01b0ba7 bbad13f 01b0ba7 bbad13f 01b0ba7 bbad13f 01b0ba7 bbad13f 01b0ba7 bbad13f 01b0ba7 bbad13f 01b0ba7 bbad13f 01b0ba7 bbad13f 01b0ba7 bbad13f 01b0ba7 bbad13f 01b0ba7 bbad13f 01b0ba7 bbad13f 01b0ba7 |
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 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 |
"""
Helion-OSC Inference Script
DeepXR/Helion-OSC - Mathematical Coding Language Model
This module provides comprehensive inference capabilities for the Helion-OSC model,
including specialized methods for different programming and mathematical tasks.
"""
import torch
import json
import logging
from typing import Optional, Dict, Any, List, Union
from transformers import (
AutoTokenizer,
AutoModelForCausalLM,
GenerationConfig,
StoppingCriteria,
StoppingCriteriaList
)
from dataclasses import dataclass
import warnings
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
@dataclass
class GenerationParameters:
"""Parameters for text generation"""
max_length: int = 2048
temperature: float = 0.7
top_p: float = 0.95
top_k: int = 50
repetition_penalty: float = 1.05
length_penalty: float = 1.0
do_sample: bool = True
num_return_sequences: int = 1
early_stopping: bool = False
class CodeStoppingCriteria(StoppingCriteria):
"""Custom stopping criteria for code generation"""
def __init__(self, stop_sequences: List[str], tokenizer):
self.stop_sequences = stop_sequences
self.tokenizer = tokenizer
def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> bool:
decoded = self.tokenizer.decode(input_ids[0], skip_special_tokens=True)
return any(seq in decoded for seq in self.stop_sequences)
class HelionOSCInference:
"""
Comprehensive inference wrapper for Helion-OSC model
Supports multiple generation modes:
- Code generation
- Mathematical reasoning
- Algorithm design
- Code debugging
- Documentation generation
"""
def __init__(
self,
model_name: str = "DeepXR/Helion-OSC",
device: Optional[str] = None,
load_in_8bit: bool = False,
load_in_4bit: bool = False,
use_flash_attention: bool = True,
trust_remote_code: bool = True
):
"""
Initialize the Helion-OSC model
Args:
model_name: HuggingFace model identifier
device: Device to load model on (cuda/cpu/mps)
load_in_8bit: Load model in 8-bit precision
load_in_4bit: Load model in 4-bit precision
use_flash_attention: Use flash attention for faster inference
trust_remote_code: Trust remote code from model repository
"""
self.model_name = model_name
self.device = self._get_device(device)
self.load_in_8bit = load_in_8bit
self.load_in_4bit = load_in_4bit
logger.info(f"Initializing Helion-OSC on {self.device}...")
# Load tokenizer
self.tokenizer = self._load_tokenizer(trust_remote_code)
# Load model
self.model = self._load_model(
use_flash_attention=use_flash_attention,
trust_remote_code=trust_remote_code
)
# Load generation configs
self.generation_configs = self._load_generation_configs()
logger.info("Model loaded successfully!")
self._print_model_info()
def _get_device(self, device: Optional[str]) -> str:
"""Determine the best available device"""
if device:
return device
if torch.cuda.is_available():
return "cuda"
elif torch.backends.mps.is_available():
return "mps"
return "cpu"
def _load_tokenizer(self, trust_remote_code: bool):
"""Load and configure tokenizer"""
logger.info("Loading tokenizer...")
tokenizer = AutoTokenizer.from_pretrained(
self.model_name,
trust_remote_code=trust_remote_code,
padding_side="left"
)
# Ensure pad token is set
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
return tokenizer
def _load_model(self, use_flash_attention: bool, trust_remote_code: bool):
"""Load and configure model"""
logger.info("Loading model...")
model_kwargs = {
"trust_remote_code": trust_remote_code,
"low_cpu_mem_usage": True
}
# Configure precision and quantization
if self.load_in_8bit:
model_kwargs["load_in_8bit"] = True
logger.info("Loading in 8-bit precision")
elif self.load_in_4bit:
model_kwargs["load_in_4bit"] = True
model_kwargs["bnb_4bit_compute_dtype"] = torch.bfloat16
model_kwargs["bnb_4bit_use_double_quant"] = True
model_kwargs["bnb_4bit_quant_type"] = "nf4"
logger.info("Loading in 4-bit precision")
else:
if self.device == "cuda":
model_kwargs["torch_dtype"] = torch.bfloat16
else:
model_kwargs["torch_dtype"] = torch.float32
# Configure device mapping
if self.device == "cuda" and not (self.load_in_8bit or self.load_in_4bit):
model_kwargs["device_map"] = "auto"
# Load model
model = AutoModelForCausalLM.from_pretrained(
self.model_name,
**model_kwargs
)
# Move to device if needed
if self.device != "cuda" or (self.load_in_8bit or self.load_in_4bit):
if not (self.load_in_8bit or self.load_in_4bit):
model = model.to(self.device)
model.eval()
# Enable gradient checkpointing for memory efficiency if needed
if hasattr(model, 'gradient_checkpointing_enable'):
model.gradient_checkpointing_enable()
return model
def _load_generation_configs(self) -> Dict[str, GenerationParameters]:
"""Load task-specific generation configurations"""
return {
"code_generation": GenerationParameters(
max_length=4096,
temperature=0.7,
top_p=0.95,
top_k=50,
repetition_penalty=1.05,
do_sample=True
),
"mathematical_reasoning": GenerationParameters(
max_length=2048,
temperature=0.3,
top_p=0.9,
top_k=40,
repetition_penalty=1.0,
do_sample=False
),
"code_completion": GenerationParameters(
max_length=1024,
temperature=0.6,
top_p=0.92,
top_k=45,
repetition_penalty=1.03,
do_sample=True
),
"algorithm_design": GenerationParameters(
max_length=3072,
temperature=0.5,
top_p=0.93,
top_k=50,
repetition_penalty=1.08,
do_sample=True
),
"debugging": GenerationParameters(
max_length=2048,
temperature=0.4,
top_p=0.88,
repetition_penalty=1.0,
do_sample=False
)
}
def _print_model_info(self):
"""Print model information"""
try:
num_params = sum(p.numel() for p in self.model.parameters())
logger.info(f"Model parameters: {num_params:,}")
logger.info(f"Model dtype: {next(self.model.parameters()).dtype}")
logger.info(f"Device: {self.device}")
except Exception as e:
logger.warning(f"Could not get model info: {e}")
def generate(
self,
prompt: Union[str, List[str]],
task_type: str = "code_generation",
custom_params: Optional[GenerationParameters] = None,
stop_sequences: Optional[List[str]] = None,
return_full_text: bool = False,
**kwargs
) -> Union[str, List[str]]:
"""
Generate text based on prompt
Args:
prompt: Input prompt or list of prompts
task_type: Type of task (code_generation, mathematical_reasoning, etc.)
custom_params: Custom generation parameters
stop_sequences: List of sequences to stop generation
return_full_text: Whether to return full text including prompt
**kwargs: Additional generation parameters
Returns:
Generated text or list of generated texts
"""
# Get generation parameters
if custom_params:
params = custom_params
elif task_type in self.generation_configs:
params = self.generation_configs[task_type]
else:
logger.warning(f"Unknown task type '{task_type}', using default parameters")
params = GenerationParameters()
# Override with kwargs
for key, value in kwargs.items():
if hasattr(params, key):
setattr(params, key, value)
# Tokenize input
is_batch = isinstance(prompt, list)
inputs = self.tokenizer(
prompt,
return_tensors="pt",
padding=True,
truncation=True,
max_length=self.model.config.max_position_embeddings
).to(self.device)
# Setup stopping criteria
stopping_criteria = None
if stop_sequences:
stopping_criteria = StoppingCriteriaList([
CodeStoppingCriteria(stop_sequences, self.tokenizer)
])
# Generate
with torch.no_grad():
outputs = self.model.generate(
**inputs,
max_length=params.max_length,
temperature=params.temperature,
top_p=params.top_p,
top_k=params.top_k,
repetition_penalty=params.repetition_penalty,
length_penalty=params.length_penalty,
do_sample=params.do_sample,
num_return_sequences=params.num_return_sequences,
early_stopping=params.early_stopping,
pad_token_id=self.tokenizer.pad_token_id,
eos_token_id=self.tokenizer.eos_token_id,
stopping_criteria=stopping_criteria
)
# Decode outputs
generated_texts = []
for output in outputs:
text = self.tokenizer.decode(output, skip_special_tokens=True)
if not return_full_text and not is_batch:
# Remove prompt from single generation
if isinstance(prompt, str):
text = text[len(prompt):].strip()
generated_texts.append(text)
return generated_texts if is_batch or params.num_return_sequences > 1 else generated_texts[0]
def code_generation(
self,
prompt: str,
language: Optional[str] = None,
max_length: int = 4096,
**kwargs
) -> str:
"""
Generate code for a given prompt
Args:
prompt: Code generation prompt
language: Programming language (optional)
max_length: Maximum length of generated code
**kwargs: Additional generation parameters
Returns:
Generated code
"""
if language:
prompt = f"Language: {language}\n{prompt}"
return self.generate(
prompt,
task_type="code_generation",
max_length=max_length,
**kwargs
)
def mathematical_reasoning(
self,
prompt: str,
max_length: int = 2048,
**kwargs
) -> str:
"""
Solve mathematical problems with step-by-step reasoning
Args:
prompt: Mathematical problem
max_length: Maximum length of solution
**kwargs: Additional generation parameters
Returns:
Mathematical solution with reasoning
"""
return self.generate(
prompt,
task_type="mathematical_reasoning",
max_length=max_length,
**kwargs
)
def algorithm_design(
self,
prompt: str,
include_complexity: bool = True,
max_length: int = 3072,
**kwargs
) -> str:
"""
Design algorithms with complexity analysis
Args:
prompt: Algorithm design prompt
include_complexity: Whether to include complexity analysis
max_length: Maximum length of output
**kwargs: Additional generation parameters
Returns:
Algorithm design with analysis
"""
if include_complexity:
prompt += "\n\nPlease include time and space complexity analysis."
return self.generate(
prompt,
task_type="algorithm_design",
max_length=max_length,
**kwargs
)
def debug_code(
self,
code: str,
error_message: Optional[str] = None,
max_length: int = 2048,
**kwargs
) -> str:
"""
Debug code and provide fixes
Args:
code: Code to debug
error_message: Optional error message
max_length: Maximum length of output
**kwargs: Additional generation parameters
Returns:
Debugging analysis and fixes
"""
prompt = f"Debug the following code:\n\n```\n{code}\n```"
if error_message:
prompt += f"\n\nError message: {error_message}"
prompt += "\n\nProvide a detailed explanation and fixed code."
return self.generate(
prompt,
task_type="debugging",
max_length=max_length,
**kwargs
)
def complete_code(
self,
code_context: str,
max_length: int = 1024,
**kwargs
) -> str:
"""
Complete partial code
Args:
code_context: Partial code to complete
max_length: Maximum length of completion
**kwargs: Additional generation parameters
Returns:
Code completion
"""
return self.generate(
code_context,
task_type="code_completion",
max_length=max_length,
stop_sequences=["\n\n", "```", "###"],
**kwargs
)
def batch_generate(
self,
prompts: List[str],
task_type: str = "code_generation",
batch_size: int = 4,
**kwargs
) -> List[str]:
"""
Generate responses for multiple prompts in batches
Args:
prompts: List of prompts
task_type: Type of task
batch_size: Batch size for processing
**kwargs: Additional generation parameters
Returns:
List of generated responses
"""
results = []
for i in range(0, len(prompts), batch_size):
batch = prompts[i:i + batch_size]
batch_results = self.generate(batch, task_type=task_type, **kwargs)
if isinstance(batch_results, str):
batch_results = [batch_results]
results.extend(batch_results)
return results
def main():
"""Example usage and demonstrations"""
print("=" * 80)
print("Helion-OSC Inference Examples")
print("=" * 80)
# Initialize model
helion = HelionOSCInference(
load_in_8bit=False, # Set to True for lower memory usage
load_in_4bit=False # Set to True for even lower memory usage
)
# Example 1: Code Generation
print("\n" + "=" * 80)
print("Example 1: Code Generation")
print("=" * 80)
code_prompt = """Write a Python function to implement a binary search tree with the following methods:
- insert(value): Insert a new value
- search(value): Search for a value
- delete(value): Delete a value
- inorder_traversal(): Return inorder traversal
Include proper documentation and type hints."""
print(f"\nPrompt:\n{code_prompt}")
print("\nGenerating...")
result = helion.code_generation(code_prompt, language="python")
print(f"\nGenerated Code:\n{result}")
# Example 2: Mathematical Reasoning
print("\n" + "=" * 80)
print("Example 2: Mathematical Reasoning")
print("=" * 80)
math_prompt = """Prove that the sum of the first n natural numbers equals n(n+1)/2 using mathematical induction."""
print(f"\nPrompt:\n{math_prompt}")
print("\nGenerating...")
result = helion.mathematical_reasoning(math_prompt)
print(f"\nSolution:\n{result}")
# Example 3: Algorithm Design
print("\n" + "=" * 80)
print("Example 3: Algorithm Design")
print("=" * 80)
algo_prompt = """Design an efficient algorithm to find the longest palindromic substring in a given string."""
print(f"\nPrompt:\n{algo_prompt}")
print("\nGenerating...")
result = helion.algorithm_design(algo_prompt, include_complexity=True)
print(f"\nAlgorithm:\n{result}")
# Example 4: Code Debugging
print("\n" + "=" * 80)
print("Example 4: Code Debugging")
print("=" * 80)
buggy_code = """
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
# This is too slow for large n
result = fibonacci(100)
"""
print(f"\nBuggy Code:\n{buggy_code}")
print("\nGenerating debugging analysis...")
result = helion.debug_code(buggy_code, error_message="Takes too long to compute")
print(f"\nDebug Analysis:\n{result}")
# Example 5: Batch Processing
print("\n" + "=" * 80)
print("Example 5: Batch Code Generation")
print("=" * 80)
batch_prompts = [
"Write a Python function to reverse a linked list",
"Write a JavaScript function to debounce API calls",
"Write a Rust function to parse JSON safely"
]
print("\nProcessing batch prompts...")
results = helion.batch_generate(batch_prompts, batch_size=2)
for i, (prompt, result) in enumerate(zip(batch_prompts, results), 1):
print(f"\nPrompt {i}: {prompt}")
print(f"Result {i}:\n{result}\n")
print("=" * 80)
print("Examples completed!")
print("=" * 80)
if __name__ == "__main__":
main() |