Skip to content

Commit cfd4fc7

Browse files
Update registry.py
feat(tasks): Enhance task discovery and display This commit improves the task registry to ensure all available task suites, including community, multilingual, custom, and test suites, are correctly discovered and displayed in the `lighteval tasks list` command.
1 parent 0d41a87 commit cfd4fc7

File tree

1 file changed

+64
-4
lines changed

1 file changed

+64
-4
lines changed

src/lighteval/tasks/registry.py

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,36 @@
3737
from lighteval.tasks.lighteval_task import LightevalTask, LightevalTaskConfig
3838
from lighteval.utils.imports import CANNOT_USE_EXTENDED_TASKS_MSG, can_load_extended_tasks
3939

40+
# Import community tasks
41+
AVAILABLE_COMMUNITY_TASKS_MODULES = []
42+
def load_community_tasks():
43+
"""Dynamically load community tasks, handling errors gracefully."""
44+
modules = []
45+
try:
46+
# Community tasks are in the lighteval directory, not under src
47+
import sys
48+
import os
49+
community_path = os.path.join(os.path.dirname(__file__), "..", "..", "..", "community_tasks")
50+
if os.path.exists(community_path):
51+
sys.path.insert(0, os.path.dirname(community_path))
52+
53+
# List all python files in community_tasks
54+
community_files = [f[:-3] for f in os.listdir(community_path)
55+
if f.endswith('.py') and not f.startswith('_')]
56+
57+
for module_name in community_files:
58+
try:
59+
module = importlib.import_module(f"community_tasks.{module_name}")
60+
if hasattr(module, 'TASKS_TABLE'):
61+
modules.append(module)
62+
logger.info(f"Successfully loaded community tasks from {module_name}")
63+
except Exception as e:
64+
logger.warning(f"Failed to load community tasks from {module_name}: {e}")
65+
except Exception as e:
66+
logger.warning(f"Error loading community tasks directory: {e}")
67+
68+
return modules
69+
4070

4171
logger = logging.getLogger(__name__)
4272

@@ -137,6 +167,23 @@ def task_registry(self) -> dict[str, LightevalTaskConfig]:
137167
else:
138168
logger.warning(CANNOT_USE_EXTENDED_TASKS_MSG)
139169

170+
# Load community tasks
171+
community_modules = load_community_tasks()
172+
for community_task_module in community_modules:
173+
custom_tasks_module.append(community_task_module)
174+
175+
# Load multilingual tasks
176+
MULTILINGUAL_TASKS_AVAILABLE = False
177+
multilingual_tasks = None
178+
try:
179+
import lighteval.tasks.multilingual.tasks as multilingual_tasks
180+
MULTILINGUAL_TASKS_AVAILABLE = True
181+
except ImportError as e:
182+
logger.warning(f"Could not load multilingual tasks: {e}. You may need to install additional dependencies.")
183+
184+
if MULTILINGUAL_TASKS_AVAILABLE and multilingual_tasks is not None:
185+
custom_tasks_module.append(multilingual_tasks)
186+
140187
for module in custom_tasks_module:
141188
custom_task_configs.extend(module.TASKS_TABLE)
142189
logger.info(f"Found {len(module.TASKS_TABLE)} custom tasks in {module.__file__}")
@@ -285,13 +332,26 @@ def print_all_tasks(self):
285332
Print all the tasks in the task registry.
286333
"""
287334
tasks_names = list(self.task_registry.keys())
335+
336+
# Ensure all default suites are present
337+
suites_in_registry = {name.split("|")[0] for name in tasks_names}
338+
for suite in DEFAULT_SUITES:
339+
if suite not in suites_in_registry:
340+
# We add a dummy task to make sure the suite is printed
341+
tasks_names.append(f"{suite}|")
342+
288343
tasks_names.sort()
344+
289345
for suite, g in groupby(tasks_names, lambda x: x.split("|")[0]):
290-
tasks_names = list(g)
291-
tasks_names.sort()
346+
tasks_in_suite = [name for name in g if name.split("|")[1]] # Filter out dummy tasks
347+
tasks_in_suite.sort()
348+
292349
print(f"\n- {suite}:")
293-
for task_name in tasks_names:
294-
print(f" - {task_name}")
350+
if not tasks_in_suite:
351+
print(" (no tasks in this suite)")
352+
else:
353+
for task_name in tasks_in_suite:
354+
print(f" - {task_name}")
295355

296356
@staticmethod
297357
def create_custom_tasks_module(custom_tasks: str | Path | ModuleType) -> ModuleType:

0 commit comments

Comments
 (0)