llmcompressor.utils.pytorch
模块
-
module–实用/辅助函数
函数
-
get_layer_by_name–按名称获取模块的层。
-
get_layers–根据目标获取模块的层(也称为子模块)。
-
get_matching_layer–给定一个目标正则表达式,查找模块中与 name_to_match 字符串最匹配的层名称。
-
get_no_split_params–获取不应在分片时拆分的模块类的列表。对于
-
qat_active–通过检查来确定模型中是否有任何层启用了量化
get_layer_by_name
按名称获取模块的层。
参数
-
(layer_namestr) –要查找的层的名称。空字符串返回模块本身。
-
(moduleModule) –在其中搜索 layer_name 的模块。
返回
-
模块–模块,名称为 layer_name 的层。
llmcompressor/utils/pytorch/module.py 中的源代码
get_layers
get_layers(
targets: Union[str, List[str]],
module: Module,
exclude_internal_modules: bool = False,
) -> Dict[str, Module]
根据目标获取模块的层(也称为子模块)。
参数
-
(targetsUnion[str, List[str]]) –要搜索的名称或正则表达式。可以是正则表达式,例如“re:.*input_layernorm$”来查找模块中所有名称以字符串“input_layernorm”结尾的层。
-
(moduleModule) –要从中搜索目标的父模块。
-
(exclude_internal_modulesbool, 默认值:False) –如果为 True,则不包括 llm-compressor 添加的内部模块,例如 Observers 和 Transforms。默认为 False,以保持向后兼容性。
返回
-
Dict[str, Module]–包含模块中匹配目标的 {层名称 -> 模块} 的字典。
llmcompressor/utils/pytorch/module.py 中的源代码
get_matching_layer
get_matching_layer(
target: str, name_to_match: str, module: Module
) -> Optional[Tuple[str, Module]]
给定一个目标正则表达式,查找模块中与 name_to_match 字符串最匹配的层名称。这用于匹配同一层中的子模块,例如,通过“re.*k_proj”匹配“model.decoder.layer.0.q_proj”来查找存在于第 0 层的 k_proj。
参数
-
(targetstr) –要搜索的正则表达式。
-
(name_to_matchstr) –要匹配的完整层名称,应存在于模块中。
-
(moduleModule) –要从中搜索目标的模块。
返回
-
Optional[Tuple[str, Module]]–包含符合目标正则表达式并最佳匹配 name_to_match 的层名称和模块的元组,如果没有找到匹配项则为 None。
llmcompressor/utils/pytorch/module.py 中的源代码
get_no_split_params
获取不应在分片时拆分的模块类的列表。对于 Hugging Face Transformer 模型,这是 decoder 层类型。对于其他类型的模型,它仅返回所有模块名称。
返回
-
Union[str, List[str]]–不应拆分的类名列表。
llmcompressor/utils/pytorch/module.py 中的源代码
qat_active
通过检查 weight_fake_quant 属性来确定模型中是否有任何层启用了量化。
参数
-
(moduleModule) –要检查量化的 PyTorch 模型。
返回
-
bool–如果模型中任何地方的量化都处于活动状态,则为 True,否则为 False。