llmcompressor.utils.pytorch.module
实用/辅助函数
函数
-
get_layer_by_name–按名称获取模块的层。
-
get_layers–根据目标获取模块的层(也称为子模块)。
-
get_matching_layer–给定一个目标正则表达式,找到模块中最接近匹配的层名。
-
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。