llmcompressor.recipe
用于定义和管理压缩工作流的 Recipe 系统。
提供 Recipe 框架,用于指定压缩配置,包括元数据跟踪、Recipe 解析和工作流编排。支持基于阶段的执行和灵活的参数管理,以实现复杂的压缩管道。
模块
类
-
Recipe–用于表示模型 Recipe 的类。
Recipe
基类:BaseModel
用于表示模型 Recipe 的类。Recipe 编码了修改模型和/或训练过程所需的指令,作为一系列修饰符(modifiers)。
Recipe 可以从文件、字符串或 HuggingFace stub 创建。可接受的文件格式包括 json 和 yaml,但序列化 Recipe 时,默认将使用 yaml。
方法
-
create_instance–从文件、字符串或 RecipeModifier 对象创建 Recipe 实例
-
dict–:return: Recipe 的字典表示
-
from_dict–解析表示 Recipe 的字典并返回 Recipe 实例。
-
from_modifiers–从修饰符列表创建 Recipe 实例
-
yaml–返回 Recipe 的 YAML 字符串表示,
create_instance classmethod
create_instance(
path_or_modifiers: Union[
str, Modifier, List[Modifier], Recipe
],
modifier_group_name: Optional[str] = None,
target_stage: Optional[str] = None,
) -> Recipe
从文件、字符串或 RecipeModifier 对象创建 Recipe 实例
支持使用 Recipe 字符串或文件
recipe_str = ''' ... test_stage: ... pruning_modifiers: ... ConstantPruningModifier: ... start: 0.0 ... end: 2.0 ... targets: ['re:.*weight'] ... ''' recipe = Recipe.create_instance(recipe_str, target_stage="test_stage")
参数
-
(path_or_modifiersUnion[str, Modifier, List[Modifier], Recipe]) –Recipe 文件的路径或 Recipe 字符串(必须是有效的 json/yaml 文件或有效的 json/yaml 字符串)。还可以接受 RecipeModifier 实例或 RecipeModifiers 列表
-
(modifier_group_nameOptional[str], 默认值:None) –Recipe 的 stage_name。如果为
oneshot或train,则 run_type 将根据 modifier_group_name 推断;如果为 None,则会分配一个默认的虚拟 group_name。此参数仅在从 Modifier/Modifier 列表实例创建 Recipe 时使用,否则将被忽略。
返回
-
Recipe–从路径或修饰符创建的 Recipe 实例,或有效的 yaml/json 格式的 Recipe 字符串
源代码位于 llmcompressor/recipe/recipe.py
dict
from_dict classmethod
解析表示 Recipe 的字典并返回 Recipe 实例。确保所有修饰符条目都被实例化为 Modifier 对象。
参数
-
(recipe_dictDict[str, Any]) –包含 Recipe 结构的字典。
返回
-
Recipe–实例化了 Modifier 对象的 Recipe 实例。
源代码位于 llmcompressor/recipe/recipe.py
from_modifiers classmethod
from_modifiers(
modifiers: Union[Modifier, List[Modifier]],
modifier_group_name: Optional[str] = None,
) -> Recipe
从修饰符列表创建 Recipe 实例
(注意:所有修饰符都被包装到一个阶段中,阶段名称为 modifier_group_name。如果 modifier_group_name 为 None,则默认 run type 为 oneshot)
生命周期: | - 验证修饰符 | - 从修饰符创建 Recipe 字符串 | - 从 Recipe 字符串创建 Recipe 实例
参数
-
(修饰符Union[Modifier, List[Modifier]]) –RecipeModifier 实例列表
-
(modifier_group_nameOptional[str], 默认值:None) –Recipe 的 stage_name。如果为
oneshot或train,则 run_type 将根据 modifier_group_name 推断;如果为 None,则会分配一个默认的虚拟 group_name。
返回
-
Recipe–从修饰符创建的 Recipe 实例
源代码位于 llmcompressor/recipe/recipe.py
yaml
返回 Recipe 的 YAML 字符串表示,可以选择与另一个 YAML 文件合并。
参数
-
(文件路径Optional[str], 默认值:None) –保存 YAML 的可选路径
-
(existing_recipe_pathOptional[str], 默认值:None) –另一个 recipe.yaml 文件的可选路径
返回
-
str–合并后的 YAML 字符串