|
25 | 25 | import importlib
|
26 | 26 | import logging
|
27 | 27 | import os
|
| 28 | +import sys |
28 | 29 | from functools import lru_cache
|
29 | 30 | from itertools import groupby
|
30 | 31 | from pathlib import Path
|
|
38 | 39 | from lighteval.utils.imports import CANNOT_USE_EXTENDED_TASKS_MSG, can_load_extended_tasks
|
39 | 40 |
|
40 | 41 |
|
| 42 | +# Import community tasks |
| 43 | +AVAILABLE_COMMUNITY_TASKS_MODULES = [] |
| 44 | + |
| 45 | + |
| 46 | +def load_community_tasks(): |
| 47 | + """Dynamically load community tasks, handling errors gracefully.""" |
| 48 | + modules = [] |
| 49 | + try: |
| 50 | + # Community tasks are in the lighteval directory, not under src |
| 51 | + community_path = Path(__file__).parent.parent.parent / "community_tasks" |
| 52 | + if not community_path.exists(): |
| 53 | + return modules |
| 54 | + |
| 55 | + # Ensure the parent directory is on sys.path so we can import `community_tasks.*` |
| 56 | + parent_dir = str(community_path.parent) |
| 57 | + if parent_dir not in sys.path: |
| 58 | + sys.path.insert(0, parent_dir) |
| 59 | + |
| 60 | + # List all python files in community_tasks |
| 61 | + community_files = [p.stem for p in community_path.glob("*.py") if not p.name.startswith("_")] |
| 62 | + |
| 63 | + for module_name in community_files: |
| 64 | + try: |
| 65 | + module = importlib.import_module(f"community_tasks.{module_name}") |
| 66 | + if hasattr(module, "TASKS_TABLE"): |
| 67 | + modules.append(module) |
| 68 | + logger.info(f"Successfully loaded community tasks from {module_name}") |
| 69 | + except Exception as e: |
| 70 | + logger.warning(f"Failed to load community tasks from {module_name}: {e}") |
| 71 | + except Exception as e: |
| 72 | + logger.warning(f"Error loading community tasks directory: {e}") |
| 73 | + |
| 74 | + return modules |
| 75 | + |
| 76 | + |
41 | 77 | logger = logging.getLogger(__name__)
|
42 | 78 |
|
43 | 79 | # Helm, Bigbench, Harness are implementations following an evaluation suite setup
|
@@ -137,6 +173,24 @@ def task_registry(self) -> dict[str, LightevalTaskConfig]:
|
137 | 173 | else:
|
138 | 174 | logger.warning(CANNOT_USE_EXTENDED_TASKS_MSG)
|
139 | 175 |
|
| 176 | + # Load community tasks |
| 177 | + community_modules = load_community_tasks() |
| 178 | + for community_task_module in community_modules: |
| 179 | + custom_tasks_module.append(community_task_module) |
| 180 | + |
| 181 | + # Load multilingual tasks |
| 182 | + MULTILINGUAL_TASKS_AVAILABLE = False |
| 183 | + multilingual_tasks = None |
| 184 | + try: |
| 185 | + import lighteval.tasks.multilingual.tasks as multilingual_tasks |
| 186 | + |
| 187 | + MULTILINGUAL_TASKS_AVAILABLE = True |
| 188 | + except ImportError as e: |
| 189 | + logger.warning(f"Could not load multilingual tasks: {e}. You may need to install additional dependencies.") |
| 190 | + |
| 191 | + if MULTILINGUAL_TASKS_AVAILABLE and multilingual_tasks is not None: |
| 192 | + custom_tasks_module.append(multilingual_tasks) |
| 193 | + |
140 | 194 | for module in custom_tasks_module:
|
141 | 195 | custom_task_configs.extend(module.TASKS_TABLE)
|
142 | 196 | logger.info(f"Found {len(module.TASKS_TABLE)} custom tasks in {module.__file__}")
|
@@ -285,13 +339,26 @@ def print_all_tasks(self):
|
285 | 339 | Print all the tasks in the task registry.
|
286 | 340 | """
|
287 | 341 | tasks_names = list(self.task_registry.keys())
|
| 342 | + |
| 343 | + # Ensure all default suites are present |
| 344 | + suites_in_registry = {name.split("|")[0] for name in tasks_names} |
| 345 | + for suite in DEFAULT_SUITES: |
| 346 | + if suite not in suites_in_registry: |
| 347 | + # We add a dummy task to make sure the suite is printed |
| 348 | + tasks_names.append(f"{suite}|") |
| 349 | + |
288 | 350 | tasks_names.sort()
|
| 351 | + |
289 | 352 | for suite, g in groupby(tasks_names, lambda x: x.split("|")[0]):
|
290 |
| - tasks_names = list(g) |
291 |
| - tasks_names.sort() |
| 353 | + tasks_in_suite = [name for name in g if name.split("|")[1]] # Filter out dummy tasks |
| 354 | + tasks_in_suite.sort() |
| 355 | + |
292 | 356 | print(f"\n- {suite}:")
|
293 |
| - for task_name in tasks_names: |
294 |
| - print(f" - {task_name}") |
| 357 | + if not tasks_in_suite: |
| 358 | + print(" (no tasks in this suite)") |
| 359 | + else: |
| 360 | + for task_name in tasks_in_suite: |
| 361 | + print(f" - {task_name}") |
295 | 362 |
|
296 | 363 | @staticmethod
|
297 | 364 | def create_custom_tasks_module(custom_tasks: str | Path | ModuleType) -> ModuleType:
|
|
0 commit comments