搜索空间

DynamicSpace的扩展,用于处理可搜索单元及相关工具。

SearchSpace

搜索空间接口的存根。

函数

generate_search_space

使用动态单元修补模型并生成/返回搜索空间。

class SearchSpace

基础类: DynamicSpace

搜索空间接口的存根。

__init__(model, sym_map=None)

从模型及其关联的符号映射初始化搜索空间。

Parameters:
  • 模型 (模块) –

  • sym_map (SymMap | None) –

Return type:

export(dm_registry=<modelopt.torch.opt.dynamic._DMRegistryCls object>)

使用默认注册表导出。

Parameters:

dm_registry (_DMRegistryCls) –

Return type:

模块

generate(rules=None)

从模型 + 符号映射生成搜索空间。

Parameters:

规则 (字典[字符串, 字典[字符串, 任意类型] | | 字典[字符串, 字典[字符串, 任意类型] | ]] | ) –

Return type:

print_summary(skipped_hparams=['kernel_size'])

打印搜索空间的摘要。

Parameters:

skipped_hparams (List[str]) –

Return type:

sample(sample_func=<function choice>)

使用提供的sample_func并返回结果配置的示例可配置hparams。

Parameters:
  • model – 一个可配置的模型,包含一个或多个DynamicModule。

  • sample_func (Callable[[Sequence[T]], T] | Dict[str, Callable[[Sequence[T]], T]]) –

    用于超参数的采样函数。如果提供了字典,则键表示用于匹配超参数名称的Unix shell风格的通配符模式。默认是随机采样,对应于{"*": random.choice}

    注意

    当提供带有通配符的字典时,后面的匹配优先于前面的匹配。

Returns:

一个(parameter_name, choice)的字典,用于指定一个活动的子网。

Return type:

Dict[str, Any]

sort_parameters(hps_to_sort=None)

一种基于图传播的参数排序算法。

Parameters:

hps_to_sort (Set[str] | None) – 一组要排序的超参数名称。如果未提供或为空,则所有超参数都将被排序。

Return type:

generate_search_space(model, rules=None)

使用动态单元修补模型并生成/返回搜索空间。

Parameters:
  • model (Module) – 我们要为其生成搜索空间的修补模型。

  • rules (Dict[str, Dict[str, Any] | None | Dict[str, Dict[str, Any] | None]] | None) –

    An optional dict specifying the rules used during the search space generation. If not provided, the largest possible search space is generated. An example rule:

    rules = {
        "nn.Sequential": {"min_depth": 2},
        "nn.Conv2d": {
            "*": {
                "channels_ratio": [1],
                "channel_divisor": 16,
                "kernel_size": [],
            },
            "backbone.stages.[1-5]*.spatial_conv*": {
                "channels_ratio": [0.334, 0.5, 0.667, 1],
            },
            "det_blocks.*.spatial_conv*": {
                "channels_ratio": [0.667, 1],
            },
        },
        "nn.Linear": {},  # uses default arguments for Linear
        "nn.BatchNorm2d": {},
        "nn.SyncBatchNorm": {},
    }
    

    Note

    When rules are provided, the search space generation is restricted to the rules that are provided. Other module types will not be included in the search space.

    Note

    Rules with None, e.g., {"nn.Sequential": None}, will also cause the conversion to be skipped just like when no rules are provided for the layer type.

    Note

    Instead of specifying the module type as a string, the module type can also be specified as a key directly, e.g., {nn.Sequential: None}.

Returns:

修补后的模型具有描述结果搜索空间的动态单元。

Return type:

搜索空间

通常,搜索空间的生成遵循以下步骤概述:

  1. 分析模型以识别可以通过modelopt.torch.trace进行修补的动态单元。

  2. 相应地使用动态单元修补模型。

  3. 通过确保根据从模型分析中获得的符号映射在动态单元之间的一致性,从修补后的模型生成一致的搜索空间。

模型返回后,可以通过modelopt.torch.dynamic.SearchSpace中提供的实用程序来分析和修改搜索空间。