Optimum 文档

优化

您正在查看的是需要从源码安装。如果您想通过常规的 pip 安装,请查看最新的稳定版本 (v1.27.0)。
Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

优化

转换

class optimum.fx.optimization.Transformation

< >

( )

参数

  • preserves_computation (bool, 默认为 False) — 转换是否保留图计算。如果为 True,则原始图和转换后的图应产生相同的输出。

一个 torch.fx 图转换。

它必须实现 transform() 方法,并用作可调用对象。

__call__

< >

( graph_module: GraphModule lint_and_recompile: bool = True ) torch.fx.GraphModule

参数

  • graph_module (torch.fx.GraphModule) — 要转换的模块。
  • lint_and_recompile (bool, 默认为 True) — 是否应该对转换后的模块进行 linting 和重新编译。当链式转换以仅执行一次此操作时,可以将其设置为 False

返回

torch.fx.GraphModule

转换后的模块。

get_transformed_nodes

< >

( graph_module: GraphModule ) List[torch.fx.Node]

参数

  • graph_module (torch.fx.GraphModule) — 要获取节点的 graph_module。

返回

List[torch.fx.Node]

给出由该转换转换的节点列表。

mark_as_transformed

< >

( node: Node )

参数

  • node (torch.fx.Node) — 要标记为已转换的节点。

将一个节点标记为已通过此转换进行转换。

transform

< >

( graph_module: GraphModule ) torch.fx.GraphModule

参数

  • graph_module (torch.fx.GraphModule) — 要转换的模块。

返回

torch.fx.GraphModule

转换后的模块。

transformed

< >

( node: Node ) bool

参数

  • node (torch.fx.Node) — 要检查的节点。

返回

布尔值

指定节点是否已通过此转换进行转换。

可逆转换

class optimum.fx.optimization.ReversibleTransformation

< >

( )

参数

  • preserves_computation (bool, 默认为 False) — 转换是否保留图计算。如果为 True,则原始图和转换后的图应产生相同的输出。

一个可逆的 torch.fx 图转换。

它必须实现 transform()reverse() 方法,并用作可调用对象。

__call__

< >

( graph_module: GraphModule lint_and_recompile: bool = True reverse: bool = False ) torch.fx.GraphModule

参数

  • graph_module (torch.fx.GraphModule) — 要转换的模块。
  • lint_and_recompile (bool, 默认为 True) — 是否应该对转换后的模块进行 linting 和重新编译。当链式转换以仅执行一次此操作时,可以将其设置为 False
  • reverse (bool, 默认为 False) — 如果为 True,则执行反向转换。

返回

torch.fx.GraphModule

转换后的模块。

mark_as_restored

< >

( node: Node )

参数

  • node (torch.fx.Node) — 要标记为已恢复的节点。

将节点标记为已恢复到原始状态。

reverse

< >

( graph_module: GraphModule ) torch.fx.GraphModule

参数

  • graph_module (torch.fx.GraphModule) — 要转换的模块。

返回

torch.fx.GraphModule

反向转换的模块。

optimum.fx.optimization.compose

< >

( *args: Transformation inplace: bool = True )

参数

  • args (Transformation) — 要组合的转换。
  • inplace (bool, 默认为 True) — 结果转换是否应该就地执行,或者创建一个新的图模块。

组合一系列转换。

示例

>>> from transformers import BertModel
>>> from transformers.utils.fx import symbolic_trace
>>> from optimum.fx.optimization import ChangeTrueDivToMulByInverse, MergeLinears, compose

>>> model = BertModel.from_pretrained("bert-base-uncased")
>>> traced = symbolic_trace(
...     model,
...     input_names=["input_ids", "attention_mask", "token_type_ids"],
... )
>>> composition = compose(ChangeTrueDivToMulByInverse(), MergeLinears())
>>> transformed_model = composition(traced)

转换

class optimum.fx.optimization.MergeLinears

< >

( )

参数

  • preserves_computation (bool, 默认为 False) — 转换是否保留图计算。如果为 True,则原始图和转换后的图应产生相同的输出。

将相同输入的线性层合并为一个大线性层的转换。

示例

>>> from transformers import BertModel
>>> from transformers.utils.fx import symbolic_trace
>>> from optimum.fx.optimization import MergeLinears

>>> model = BertModel.from_pretrained("bert-base-uncased")
>>> traced = symbolic_trace(
...     model,
...     input_names=["input_ids", "attention_mask", "token_type_ids"],
... )
>>> transformation = MergeLinears()
>>> transformed_model = transformation(traced)
>>> restored_model = transformation(transformed_model, reverse=True)

class optimum.fx.optimization.FuseBiasInLinear

< >

( )

参数

  • preserves_computation (bool, 默认为 False) — 转换是否保留图计算。如果为 True,则原始图和转换后的图应产生相同的输出。

将偏差融合到 torch.nn.Linear 中的权重的转换。

示例

>>> from transformers import BertModel
>>> from transformers.utils.fx import symbolic_trace
>>> from optimum.fx.optimization import FuseBiasInLinear

>>> model = BertModel.from_pretrained("bert-base-uncased")
>>> traced = symbolic_trace(
...     model,
...     input_names=["input_ids", "attention_mask", "token_type_ids"],
... )
>>> transformation = FuseBiasInLinear()
>>> transformed_model = transformation(traced)
>>> restored_model = transformation(transformed_model, reverse=True)

class optimum.fx.optimization.ChangeTrueDivToMulByInverse

< >

( )

参数

  • preserves_computation (bool, 默认为 False) — 转换是否保留图计算。如果为 True,则原始图和转换后的图应产生相同的输出。

当分母为静态时,将 true除法节点更改为乘以逆节点的转换。例如,在注意力层中,缩放因子有时就是这种情况。

示例

>>> from transformers import BertModel
>>> from transformers.utils.fx import symbolic_trace
>>> from optimum.fx.optimization import ChangeTrueDivToMulByInverse

>>> model = BertModel.from_pretrained("bert-base-uncased")
>>> traced = symbolic_trace(
...     model,
...     input_names=["input_ids", "attention_mask", "token_type_ids"],
... )
>>> transformation = ChangeTrueDivToMulByInverse()
>>> transformed_model = transformation(traced)
>>> restored_model = transformation(transformed_model, reverse=True)

class optimum.fx.optimization.FuseBatchNorm2dInConv2d

< >

( )

参数

  • preserves_computation (bool, 默认为 False) — 转换是否保留图计算。如果为 True,则原始图和转换后的图应产生相同的输出。

将 `nn.Conv2d` 后面的 `nn.BatchNorm2d` 融合到一个 `nn.Conv2d` 中的转换。只有当卷积的唯一后续节点是批归一化时,才会进行融合。

例如,在以下情况下将不会进行融合:

     Conv2d
     /   \
    /     \
ReLU   BatchNorm2d

示例

>>> from transformers.utils.fx import symbolic_trace
>>> from transformers import AutoModelForImageClassification

>>> from optimum.fx.optimization import FuseBatchNorm2dInConv2d

>>> model = AutoModelForImageClassification.from_pretrained("microsoft/resnet-50")
>>> model.eval()
>>> traced_model = symbolic_trace(
...     model,
...     input_names=["pixel_values"],
...     disable_check=True
... )

>>> transformation = FuseBatchNorm2dInConv2d()
>>> transformed_model = transformation(traced_model)

class optimum.fx.optimization.FuseBatchNorm1dInLinear

< >

( )

参数

  • preserves_computation (bool, 默认为 False) — 转换是否保留图计算。如果为 True,则原始图和转换后的图应产生相同的输出。

将 `nn.Linear` 之后或之前的 `nn.BatchNorm1d` 融合到一个 `nn.Linear` 中的转换。只有当线性层的唯一后续节点是批归一化,或者批归一化的唯一后续节点是线性层时,才会进行融合。

例如,在以下情况下将不会进行融合:

     Linear
     /   \
    /     \
ReLU   BatchNorm1d

示例

>>> from transformers.utils.fx import symbolic_trace
>>> from transformers import AutoModel

>>> from optimum.fx.optimization import FuseBatchNorm1dInLinear

>>> model = AutoModel.from_pretrained("nvidia/groupvit-gcc-yfcc")
>>> model.eval()
>>> traced_model = symbolic_trace(
...     model,
...     input_names=["input_ids", "attention_mask", "pixel_values"],
...     disable_check=True
... )

>>> transformation = FuseBatchNorm1dInLinear()
>>> transformed_model = transformation(traced_model)
< > 在 GitHub 上更新