File size: 7,453 Bytes
5fed0fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

Model name utilities for Frontier-CS.



Provides consistent model prefix conversion used across:

- Solution generation (generate_solutions.py)

- Solution matrix checking (frontier-eval check)

- Batch evaluation (frontier-eval batch)

"""

import re
from typing import Dict, List, Optional, Tuple

# Model prefix aliases for backward compatibility
MODEL_PREFIX_ALIASES: Dict[str, str] = {
    "grokcodefast1_": "grok4fastreasoning_",
}


def get_model_prefix(model: str) -> str:
    """

    Convert model name to the prefix format used in solution folder names.



    This is the canonical function for model prefix conversion. All tools

    should use this to ensure consistent naming.



    Examples:

        >>> get_model_prefix("gpt-5")

        'gpt5'

        >>> get_model_prefix("gpt-5.1-preview")

        'gpt5.1'

        >>> get_model_prefix("gemini/gemini-2.5-pro")

        'gemini2.5pro'

        >>> get_model_prefix("claude-sonnet-4-5-20250929")

        'claude4.5sonnet'

        >>> get_model_prefix("grok-3-fast-reasoning")

        'grok3fastreasoning'



    Args:

        model: Model name (e.g., "gpt-5", "gemini/gemini-2.5-pro")



    Returns:

        Normalized prefix for solution directory names

    """
    original = model

    # Remove provider prefix if present (e.g., 'gemini/gemini-2.5-pro' -> 'gemini-2.5-pro')
    if "/" in model:
        model = model.split("/", 1)[1]

    model_lower = model.lower().strip()

    # Handle GPT-5 variants
    # Keep 'gpt-5.1' distinct so its artifacts prefix as 'gpt5.1'
    if model_lower.startswith("gpt-5.1") or model_lower.startswith("gpt5.1"):
        return "gpt5.1"
    if model_lower.startswith("gpt-5") or model_lower.startswith("gpt5"):
        return "gpt5"

    # Handle Gemini 2.5 Pro variants
    if "gemini-2.5-pro" in model_lower or "gemini2.5pro" in model_lower:
        return "gemini2.5pro"

    # Handle other Gemini variants (e.g., gemini-1.5-pro -> gemini1.5pro)
    gemini_match = re.match(r"gemini-?(\d+\.?\d*)-?pro", model_lower)
    if gemini_match:
        version = gemini_match.group(1)
        return f"gemini{version}pro"

    # Handle Claude variants (e.g., claude-sonnet-4-5-20250929 -> claude4.5sonnet)
    claude_match = re.match(r"claude-([a-z]+)-(\d+)-(\d+)", model_lower)
    if claude_match:
        family = claude_match.group(1)
        major = claude_match.group(2)
        minor = claude_match.group(3)
        return f"claude{major}.{minor}{family}"

    # Handle Grok variants - keep 'fast' and 'reasoning' in the prefix
    if "grok" in model_lower:
        sanitized = re.sub(r"[^a-zA-Z0-9]+", "", model_lower)
        if sanitized:
            return sanitized

    # Default: sanitize by removing all non-alphanumeric characters
    sanitized = re.sub(r"[^a-zA-Z0-9]+", "", model_lower)
    if not sanitized:
        raise ValueError(f"Unable to derive model prefix from '{original}'")
    return sanitized


def normalize_solution_name(name: str) -> str:
    """

    Normalize a solution directory name by applying prefix aliases.



    Args:

        name: Solution directory name



    Returns:

        Normalized name with aliases applied

    """
    for old_prefix, new_prefix in MODEL_PREFIX_ALIASES.items():
        if name.startswith(old_prefix):
            return new_prefix + name[len(old_prefix):]
    return name


def sanitize_problem_name(problem: str) -> str:
    """

    Convert problem path to solution name suffix.



    Examples:

        >>> sanitize_problem_name("flash_attn")

        'flash_attn'

        >>> sanitize_problem_name("gemm_optimization/squares")

        'gemm_optimization_squares'



    Args:

        problem: Problem ID (may contain slashes)



    Returns:

        Sanitized problem name for use in solution directory names

    """
    return problem.replace("/", "_")


def parse_solution_name(solution_name: str) -> Tuple[str, str, int]:
    """

    Parse a solution directory name into components.



    Examples:

        >>> parse_solution_name("gpt5_flash_attn")

        ('gpt5', 'flash_attn', 0)

        >>> parse_solution_name("gpt5_flash_attn_1")

        ('gpt5', 'flash_attn', 1)

        >>> parse_solution_name("claude4.5sonnet_gemm_optimization_squares_2")

        ('claude4.5sonnet', 'gemm_optimization_squares', 2)



    Args:

        solution_name: Solution directory name



    Returns:

        Tuple of (model_prefix, problem_slug, variant_index)

    """
    # Check for variant suffix
    parts = solution_name.rsplit("_", 1)
    variant_index = 0
    base_name = solution_name

    if len(parts) == 2 and parts[1].isdigit():
        variant_index = int(parts[1])
        base_name = parts[0]

    # Split into model prefix and problem slug
    # Model prefix is the first part before underscore
    first_underscore = base_name.find("_")
    if first_underscore == -1:
        return (base_name, "", variant_index)

    model_prefix = base_name[:first_underscore]
    problem_slug = base_name[first_underscore + 1:]

    return (model_prefix, problem_slug, variant_index)


def build_solution_name(model: str, problem: str, variant_index: int = 0) -> str:
    """

    Build a solution directory name from components.



    Args:

        model: Model name (will be converted to prefix)

        problem: Problem ID

        variant_index: Variant index (0 = no suffix)



    Returns:

        Solution directory name

    """
    prefix = get_model_prefix(model)
    slug = sanitize_problem_name(problem)
    suffix = "" if variant_index == 0 else f"_{variant_index}"
    return f"{prefix}_{slug}{suffix}"


def detect_provider(model: str) -> str:
    """

    Detect the LLM provider from model name.



    Args:

        model: Model name



    Returns:

        Provider name: 'openai', 'google', 'anthropic', 'xai', 'deepseek', 'openrouter'

    """
    normalized = model.strip()
    if "/" in normalized:
        provider_hint, actual_model = normalized.split("/", 1)
    else:
        provider_hint, actual_model = "", normalized

    provider_hint = provider_hint.lower()
    actual_lower = actual_model.lower()

    if (provider_hint in {"", "openai", "azure", "azure_openai"}) and actual_lower.startswith("gpt"):
        return "openai"
    if provider_hint in {"gemini", "google"} or "gemini" in actual_lower:
        return "google"
    if provider_hint == "anthropic" or "claude" in actual_lower:
        return "anthropic"
    if provider_hint == "xai" or "grok" in actual_lower:
        return "xai"
    if provider_hint == "deepseek" or "deepseek" in actual_lower:
        return "deepseek"

    return provider_hint or "openai"


def is_reasoning_model(model: str, override: Optional[bool] = None) -> bool:
    """

    Determine if a model is a reasoning model.



    Args:

        model: Model name

        override: If set, use this value instead of auto-detection



    Returns:

        True if the model is a reasoning model

    """
    if override is not None:
        return override

    prefixes = ("gpt-5", "o1", "o3", "deepseek-reasoner")
    if any(model.startswith(p) for p in prefixes):
        return True

    normalized = model.lower()
    if "reasoning" in normalized and normalized.startswith("grok-"):
        return True

    return False