Repository navigation
【Feature】 Adapt util/worker config typing and defaults for custom cfg - #239
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors configuration management and worker initialization, introducing recursive type conversion for configurations and automating default values for missing dataset fields. The Infer and Eval workers were updated to handle existing configurations more robustly. Additionally, the extract_non_reasoning_content postprocessor was refactored to support list inputs natively, and new error codes for SWEBENCH were added. Review feedback highlights a redundant variable assignment in workers.py, suggests updating return type hints to reflect list support in postprocessors, and recommends hoisting regex compilation out of internal functions to improve performance when processing lists.
| runner_cfg = new_cfg['eval']['runner'] | ||
| runner_cfg['max_num_workers'] = self.args.max_num_workers | ||
| runner_cfg['max_workers_per_gpu'] = self.args.max_workers_per_gpu | ||
| runner_cfg['debug'] = runner_cfg['debug'] = self.args.debug or cfg.cli_args.debug |
| text: str | list, | ||
| think_start_token: str = '<think>', | ||
| think_end_token: str = '</think>', | ||
| ) -> str: |
| def _process_single(item: str): | ||
| # Keep historical behavior for non-string input. | ||
| if not isinstance(item, str): | ||
| return item | ||
| if think_start_token not in item and think_end_token in item: | ||
| result = item.split(think_end_token)[-1].strip() | ||
| logger.debug( | ||
| f"extract_non_reasoning_content: only end token present -> length={len(result)}" | ||
| ) | ||
| return result | ||
|
|
||
| reasoning_regex = re.compile(rf'{think_start_token}(.*?){think_end_token}', | ||
| re.DOTALL) | ||
| non_reasoning_content = reasoning_regex.sub('', item).strip() | ||
| logger.debug( | ||
| f"extract_non_reasoning_content: removed reasoning sections -> length={len(non_reasoning_content)}" | ||
| ) | ||
| return non_reasoning_content |
There was a problem hiding this comment.
Compiling the regular expression inside _process_single is inefficient when processing a list of strings, as it will be recompiled for every item. It should be compiled once outside the inner function.
| def _process_single(item: str): | |
| # Keep historical behavior for non-string input. | |
| if not isinstance(item, str): | |
| return item | |
| if think_start_token not in item and think_end_token in item: | |
| result = item.split(think_end_token)[-1].strip() | |
| logger.debug( | |
| f"extract_non_reasoning_content: only end token present -> length={len(result)}" | |
| ) | |
| return result | |
| reasoning_regex = re.compile(rf'{think_start_token}(.*?){think_end_token}', | |
| re.DOTALL) | |
| non_reasoning_content = reasoning_regex.sub('', item).strip() | |
| logger.debug( | |
| f"extract_non_reasoning_content: removed reasoning sections -> length={len(non_reasoning_content)}" | |
| ) | |
| return non_reasoning_content | |
| reasoning_regex = re.compile(rf'{think_start_token}(.*?){think_end_token}', | |
| re.DOTALL) | |
| def _process_single(item: str): | |
| # Keep historical behavior for non-string input. | |
| if not isinstance(item, str): | |
| return item | |
| if think_start_token not in item and think_end_token in item: | |
| result = item.split(think_end_token)[-1].strip() | |
| logger.debug( | |
| f"extract_non_reasoning_content: only end token present -> length={len(result)}" | |
| ) | |
| return result | |
| non_reasoning_content = reasoning_regex.sub('', item).strip() | |
| logger.debug( | |
| f"extract_non_reasoning_content: removed reasoning sections -> length={len(non_reasoning_content)}" | |
| ) | |
| return non_reasoning_content |
| text: str | list, | ||
| think_start_token: str = '<think>', | ||
| think_end_token: str = '</think>', | ||
| ) -> str: |
| def _process_single(item: str): | ||
| # Keep historical behavior for non-string input. | ||
| if not isinstance(item, str): | ||
| return item | ||
| if think_start_token not in item and think_end_token in item: | ||
| result = item.split(think_end_token)[-1].strip() | ||
| logger.debug( | ||
| f"extract_non_reasoning_content: only end token present -> length={len(result)}" | ||
| ) | ||
| return result | ||
|
|
||
| reasoning_regex = re.compile(rf'{think_start_token}(.*?){think_end_token}', | ||
| re.DOTALL) | ||
| non_reasoning_content = reasoning_regex.sub('', item).strip() | ||
| logger.debug( | ||
| f"extract_non_reasoning_content: only end token present -> length={len(result)}" | ||
| f"extract_non_reasoning_content: removed reasoning sections -> length={len(non_reasoning_content)}" | ||
| ) | ||
| return result | ||
| return non_reasoning_content |
There was a problem hiding this comment.
Compiling the regular expression inside _process_single is inefficient when processing a list of strings. It should be compiled once outside the inner function.
| def _process_single(item: str): | |
| # Keep historical behavior for non-string input. | |
| if not isinstance(item, str): | |
| return item | |
| if think_start_token not in item and think_end_token in item: | |
| result = item.split(think_end_token)[-1].strip() | |
| logger.debug( | |
| f"extract_non_reasoning_content: only end token present -> length={len(result)}" | |
| ) | |
| return result | |
| reasoning_regex = re.compile(rf'{think_start_token}(.*?){think_end_token}', | |
| re.DOTALL) | |
| non_reasoning_content = reasoning_regex.sub('', item).strip() | |
| logger.debug( | |
| f"extract_non_reasoning_content: only end token present -> length={len(result)}" | |
| f"extract_non_reasoning_content: removed reasoning sections -> length={len(non_reasoning_content)}" | |
| ) | |
| return result | |
| return non_reasoning_content | |
| reasoning_regex = re.compile(rf'{think_start_token}(.*?){think_end_token}', | |
| re.DOTALL) | |
| def _process_single(item: str): | |
| # Keep historical behavior for non-string input. | |
| if not isinstance(item, str): | |
| return item | |
| if think_start_token not in item and think_end_token in item: | |
| result = item.split(think_end_token)[-1].strip() | |
| logger.debug( | |
| f"extract_non_reasoning_content: only end token present -> length={len(result)}" | |
| ) | |
| return result | |
| non_reasoning_content = reasoning_regex.sub('', item).strip() | |
| logger.debug( | |
| f"extract_non_reasoning_content: removed reasoning sections -> length={len(non_reasoning_content)}" | |
| ) | |
| return non_reasoning_content |
PR Type / PR类型
Related Issue | 关联 Issue
Relates to #N/A
🔍 Motivation / 变更动机
本次改动主要解决自定义配置与运行时配置适配不稳定的问题:
reader_cfg/infer_cfg/eval_cfg时容易报错,不利于快速接入。infer/evalworker 在覆盖运行参数时会重建配置,可能丢失用户已有配置字段。type在运行态使用类对象更方便,但导出到配置文件时需要统一转换为字符串。📝 Modification / 修改内容
CustomConfigChecker中放宽datasets必填项,仅要求type和abbr。try_fill_in_custom_cfgs中为缺失的reader_cfg/infer_cfg/eval_cfg自动补齐默认配置。Infer/Evalworker 改为优先复用已有infer/eval配置,仅更新运行参数(并发、debug、dump 相关开关等),避免覆盖用户配置。task/runner/partitioner等type使用类对象,新增recur_convert_config_type,在导出配置前递归转换成字符串类型,保证落盘配置可读且兼容。extract_non_reasoning_content改为原生支持str | list输入,减少对装饰器依赖;同步更新 deprecated 模块相关单测。📐 Associated Test Results / 关联测试结果
tests/UT/cli/test_config_manager.pytests/UT/cli/test_workers.pytests/UT/utils/test_model_postprocessors.py无预期向后不兼容变更。
主要行为变化是自定义配置校验更宽松,并在缺省字段时自动补全默认值,属于兼容性增强。
无明显性能退化。新增的递归类型转换只在配置导出前执行一次,开销较小。
🌟 Use cases (Optional) / 使用案例(可选)
datasets配置(type + abbr)即可完成运行,缺失字段自动补齐。infer/eval详细参数,CLI 运行时仅更新必要运行参数,不再覆盖其个性化配置。✅ Checklist / 检查列表
Before PR:
After PR:
👥 Collaboration Info / 协作信息
worker.update_cfg的兼容性行为与config dump类型转换逻辑。