搜索器
实现搜索算法标准接口。
当我们需要在模型的一组超参数中进行搜索/优化时,搜索器非常有用。搜索器通常与模式结合使用,模式可以通过其入口点定义搜索空间,即将模型转换为搜索空间。然后,搜索器在这个搜索空间上进行优化。
类
一个基本的搜索界面,可用于搜索/优化模型。 |
- class BaseSearcher
基础类:
ABC一个基本的搜索界面,可用于搜索/优化模型。
基础接口支持设置搜索、检查点和加载逻辑等基本功能,并定义了要遵循的最小工作流程。
- final __init__()
我们不允许重写 __init__ 方法。
- Return type:
无
- after_search()
搜索后可选的后续处理步骤。
- Return type:
无
- before_search()
搜索前的可选预处理步骤。
- Return type:
无
- config: Dict[str, Any]
- constraints: Dict[str, str | float | Dict | None]
- construct_forward_loop(silent=True, progress_bar_msg=None, max_iter_data_loader=None, post_process_fn=False)
使用提供的配置在模型上获取可运行的前向循环。
- Return type:
Callable[[Module], None] | None
- property default_search_config: Dict[str, Any]
获取搜索器的默认配置。
- abstract property default_state_dict: Dict[str, Any]
返回默认状态字典。
- deployment: Dict[str, str] | None
- dummy_input: Any | Tuple
- eval_score(silent=True)
可选地静默评估得分函数。
- Return type:
浮点数
- forward_loop: Callable[[Module], None] | None
- property has_score: bool
检查模型是否具有评分函数。
- load_search_checkpoint()
用于搜索检查点的加载函数,返回指示是否加载了检查点的标志。
- Return type:
bool
- model: Module
- reset_search()
在开始时重置搜索。
- Return type:
无
- abstract run_search()
运行实际搜索。
- Return type:
无
- sanitize_search_config(config)
清理搜索配置字典。
- Parameters:
config (Dict[str, Any] | None) –
- Return type:
Dict[str, Any]
- save_search_checkpoint()
保存搜索检查点的功能。
- Return type:
无
- final search(model, constraints, dummy_input=None, config=None)
搜索给定的可修剪模型以找到最佳子网并返回搜索模型。
最佳子网络在满足
constraints的同时,最大化由score_func给出的分数。- Parameters:
model (Module) – 要搜索的转换后的模型。
约束 (字典[字符串, 字符串 | 浮点数 | 字典 | 无]) – 从约束名称到搜索模型必须满足的上限的字典。
dummy_input (Any | Tuple | None) –
model.forward()的参数。这用于导出和计算基于推理的指标,例如延迟/FLOPs。dummy_inputs的格式遵循torch.onnx.export中args参数的约定。config (Dict[str, Any] | None) – 用于配置搜索的额外可选参数。
- Return type:
Dict[str, Any]
- Returns: A tuple (subnet, state_dict) where
subnet 是搜索到的子网 (nn.Module),可以用于后续任务,如微调,state_dict 包含搜索过程的历史和详细统计信息。
- final state_dict()
可以存储/加载的状态字典。
- Return type:
Dict[str, Any]