Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion optillm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
del _os.environ[_hf_token_var]

# Import from server module
from .server import (
from .server import ( # noqa: E402

main,
server_config,
app,
Expand Down
3 changes: 1 addition & 2 deletions optillm/autothink/classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""

import logging
from typing import Dict, Any, Tuple, Optional, List, Union
from typing import Tuple, List
import os
import sys

Expand Down Expand Up @@ -40,7 +40,6 @@ def _load_model(self):
except ImportError:
logger.info("Installing adaptive-classifier library...")
os.system(f"{sys.executable} -m pip install adaptive-classifier")
import adaptive_classifier

# Import the AdaptiveClassifier class
from adaptive_classifier import AdaptiveClassifier
Expand Down
2 changes: 1 addition & 1 deletion optillm/autothink/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import random
import logging
from transformers import PreTrainedModel, PreTrainedTokenizer, DynamicCache
from typing import Dict, List, Any, Optional, Union, Tuple
from typing import Dict, List, Any, Tuple

from .classifier import ComplexityClassifier
from .steering import SteeringVectorManager, install_steering_hooks, remove_steering_hooks
Expand Down
4 changes: 1 addition & 3 deletions optillm/autothink/steering.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
import random
import json
import datasets
from typing import Dict, List, Any, Tuple, Optional, Union
from collections import defaultdict
from typing import Dict, List, Any, Tuple, Optional

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -540,7 +539,6 @@ def update_token_history(self, new_tokens: List[int]):
if random.random() < 0.01:
logger.debug(f"STEERING: Token history updated, now has {len(self.token_history)} tokens")

def update_context(self, new_tokens: str):
"""
Update the context buffer with new tokens.

Expand Down
2 changes: 1 addition & 1 deletion optillm/batching.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import queue
import time
import logging
from typing import Dict, List, Any, Tuple, Optional
from typing import Dict, List, Any, Optional
from concurrent.futures import Future
from dataclasses import dataclass

Expand Down
1 change: 0 additions & 1 deletion optillm/bon.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
import optillm
from optillm import conversation_logger

logger = logging.getLogger(__name__)
Expand Down
14 changes: 6 additions & 8 deletions optillm/cepo/cepo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
import time
import math_verify

from optillm import conversation_logger
from collections import Counter
from concurrent.futures import ThreadPoolExecutor, as_completed
from dataclasses import dataclass
from typing import Literal, Any, Optional
from cerebras.cloud.sdk import BadRequestError as CerebrasBadRequestError
from openai import BadRequestError as OpenAIBadRequestError
from openai import InternalServerError as OpenAIInternalServerError

Expand Down Expand Up @@ -325,7 +323,7 @@ def llm_call_reason_effort_fallback(
if len(reasoning_effort_levels) == 1 and bre.message.startswith("Error code: 400 - {'error': {'message': 'think value"):
logger.info(f"The think level {effort} was not supported by the model; Disabling thinking")
cepo_config.use_reasoning = False
except (OpenAIBadRequestError, OpenAIInternalServerError) as e:
except (OpenAIBadRequestError, OpenAIInternalServerError):
# After 2 retries at this reasoning effort level it failed with error 400/500, lower level
logger.debug(f"400/500 persisted after retries at reasoning effort {effort}; degrading effort")
if logger.getEffectiveLevel() == logging.DEBUG:
Expand Down Expand Up @@ -491,9 +489,9 @@ def generate_single_plan(i):
messages.append({"role": "assistant", "content": response})

plans.append(response)
cb_log[f"messages_planning_fallback_used"] = messages
cb_log["messages_planning_fallback_used"] = messages
if cepo_config.print_output:
print(f"\nCePO: No plans generated successfully. Taking the fallback.\n")
print("\nCePO: No plans generated successfully. Taking the fallback.\n")

# Step 3 - Review and consolidate plans
plans_message = ""
Expand Down Expand Up @@ -575,7 +573,7 @@ def generate_single_plan(i):

cb_log["messages"] = messages
if cepo_config.print_output:
print(f"\nCePO: Answer generated for one bestofn_n attempt.")
print("\nCePO: Answer generated for one bestofn_n attempt.")

return final_output, completion_tokens, cb_log

Expand Down Expand Up @@ -692,7 +690,7 @@ def run_single_completion(i):
cb_log[f"completion_{i}_completion_tokens"] = tokens_i

if cepo_config.print_output or logger.getEffectiveLevel() == logging.DEBUG:
logger.debug(f"\nCePO: All Answers generated!")
logger.debug("\nCePO: All Answers generated!")

completions = [c if isinstance(c, str) else "" for c in completions]
return completions, completion_tokens, cb_log
Expand Down Expand Up @@ -882,7 +880,7 @@ def extract_answer_mathverify(response_str, last_n_chars=100):
try:
float(response_str)
return [float(response_str)]
except:
except Exception:
response_str = response_str.split("</think>", 1)[1] if "</think>" in response_str else response_str
if last_n_chars is not None:
response_str = response_str[-last_n_chars:]
Expand Down
3 changes: 1 addition & 2 deletions optillm/cot_decoding.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import torch
from transformers import PreTrainedModel, PreTrainedTokenizer
from typing import List, Tuple, Dict, Optional
import numpy as np
from typing import List, Tuple, Dict

def get_device():
if torch.backends.mps.is_available():
Expand Down
3 changes: 1 addition & 2 deletions optillm/cot_reflection.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import re
import logging
import optillm
from optillm import conversation_logger

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -68,7 +67,7 @@ def cot_reflection(system_prompt, initial_query, client, model: str, return_full
thinking_match = re.search(r'<thinking>(.*?)</thinking>', full_response, re.DOTALL)
output_match = re.search(r'<output>(.*?)(?:</output>|$)', full_response, re.DOTALL)

thinking = thinking_match.group(1).strip() if thinking_match else "No thinking process provided."
thinking_match.group(1).strip() if thinking_match else "No thinking process provided."
output = output_match.group(1).strip() if output_match else full_response

logger.info(f"Final output :\n{output}")
Expand Down
2 changes: 1 addition & 1 deletion optillm/deepconf/confidence.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import torch
import torch.nn.functional as F
import numpy as np
from typing import List, Dict, Tuple, Optional
from typing import Dict, Optional
import logging

logger = logging.getLogger(__name__)
Expand Down
6 changes: 2 additions & 4 deletions optillm/deepconf/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@

import torch
import logging
import random
from typing import List, Dict, Any, Optional, Tuple
from typing import List, Dict, Any, Tuple
from transformers import PreTrainedModel, PreTrainedTokenizer, DynamicCache
from collections import Counter, defaultdict
import numpy as np

from .confidence import ConfidenceCalculator, ConfidenceThresholdCalibrator

Expand Down Expand Up @@ -125,7 +123,7 @@ def generate_single_trace(self, messages: List[Dict[str, str]],
kv_cache = outputs.past_key_values

# Calculate confidence for current token
token_confidence = self.confidence_calculator.add_token_confidence(logits)
self.confidence_calculator.add_token_confidence(logits)

# Check for early termination (only after minimum trace length)
if (use_early_termination and
Expand Down
2 changes: 1 addition & 1 deletion optillm/entropy_decoding.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import torch
import torch.nn.functional as F
from transformers import PreTrainedModel, PreTrainedTokenizer
from typing import List, Tuple, Dict, Optional
from typing import List, Tuple, Dict
import logging

# Set up logging
Expand Down
10 changes: 3 additions & 7 deletions optillm/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from collections import OrderedDict, defaultdict
import torch.nn.functional as F
import torch.nn as nn
import math
from transformers import AutoModelForCausalLM, AutoTokenizer, PreTrainedModel
from peft import PeftModel, PeftConfig
import bitsandbytes as bnb
Expand All @@ -17,7 +16,6 @@
import threading
import traceback
import platform
import sys
import re

from optillm.cot_decoding import cot_decode
Expand Down Expand Up @@ -1069,11 +1067,9 @@ def _load_model():
# Check for flash attention availability
try:
import flash_attn
has_flash_attn = True
logger.info("Flash Attention 2 is available")
model_kwargs["attn_implementation"] = "flash_attention_2"
except ImportError:
has_flash_attn = False
logger.info("Flash Attention 2 is not installed - falling back to default attention")

elif 'mps' in device:
Expand Down Expand Up @@ -1155,7 +1151,7 @@ def _get_adapter_name(self, adapter_id: str) -> str:
def validate_adapter(self, adapter_id: str) -> bool:
"""Validate if adapter exists and is compatible"""
try:
config = PeftConfig.from_pretrained(
PeftConfig.from_pretrained(
adapter_id,
trust_remote_code=True,
token=os.getenv("HF_TOKEN")
Expand Down Expand Up @@ -1591,8 +1587,8 @@ def process_batch(

for i in range(0, len(formatted_prompts), self.optimal_batch_size):
batch_prompts = formatted_prompts[i:i + self.optimal_batch_size]
batch_system = system_prompts[i:i + self.optimal_batch_size]
batch_user = user_prompts[i:i + self.optimal_batch_size]
system_prompts[i:i + self.optimal_batch_size]
user_prompts[i:i + self.optimal_batch_size]

# Check cache first if enabled
if self.model_config.enable_prompt_caching:
Expand Down
1 change: 0 additions & 1 deletion optillm/leap.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from typing import List, Tuple
import json
import optillm
from optillm import conversation_logger

# Setup logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
Expand Down
3 changes: 1 addition & 2 deletions optillm/litellm_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import os
import time
import litellm
from litellm import completion
from litellm.utils import get_valid_models
from typing import List, Dict, Any, Optional
from typing import List, Dict, Optional

# Configure litellm to drop unsupported parameters
litellm.drop_params = True
Expand Down
1 change: 0 additions & 1 deletion optillm/mars/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import logging
from typing import Dict, Any, Tuple
from datetime import datetime
import random
from .prompts import (
MATHEMATICAL_SYSTEM_PROMPT,
AGENT_EXPLORATION_PROMPT,
Expand Down
Loading