• 教學 >
  • (beta) 在 FX 中建立卷積/批次正規化融合器
捷徑

(beta) 在 FX 中建立 Convolution/Batch Norm fuser

建立時間:2021 年 3 月 4 日 | 最後更新:2024 年 1 月 16 日 | 最後驗證:2024 年 11 月 05 日

作者: Horace He

在本教學中,我們將使用 FX(一個用於 PyTorch 可組合函數轉換的工具包)來執行以下操作:

  1. 在資料依賴關係中尋找 conv/batch norm 的模式。

  2. 對於 1) 中找到的模式,將 batch norm 統計資料折疊到卷積權重中。

請注意,此最佳化僅適用於推論模式下的模型 (即 mode.eval())

我們將建立此處存在的 fuser:https://github.com/pytorch/pytorch/blob/orig/release/1.8/torch/fx/experimental/fuser.py

首先,讓我們導入一些必要的模組(我們將在稍後的程式碼中使用所有這些模組)。

from typing import Type, Dict, Any, Tuple, Iterable
import copy
import torch.fx as fx
import torch
import torch.nn as nn

在本教學中,我們將建立一個由卷積和批次正規化組成的模型。 請注意,此模型有一些棘手的組件 - 某些 conv/batch norm 模式隱藏在 Sequentials 中,並且其中一個 BatchNorms 包裝在另一個 Module 中。

class WrappedBatchNorm(nn.Module):
    def __init__(self):
        super().__init__()
        self.mod = nn.BatchNorm2d(1)
    def forward(self, x):
        return self.mod(x)

class M(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(1, 1, 1)
        self.bn1 = nn.BatchNorm2d(1)
        self.conv2 = nn.Conv2d(1, 1, 1)
        self.nested = nn.Sequential(
            nn.BatchNorm2d(1),
            nn.Conv2d(1, 1, 1),
        )
        self.wrapped = WrappedBatchNorm()

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.conv2(x)
        x = self.nested(x)
        x = self.wrapped(x)
        return x

model = M()

model.eval()

將卷積與批次正規化融合

在 PyTorch 中嘗試自動融合卷積和批次正規化的主要挑戰之一是,PyTorch 沒有提供一種簡單的方式來存取計算圖。 FX 通過符號追蹤實際呼叫的操作來解決此問題,以便我們可以通過 forward 呼叫(嵌套在 Sequential 模組中或包裝在使用者定義的模組中)來追蹤計算。

traced_model = torch.fx.symbolic_trace(model)
print(traced_model.graph)

這為我們提供了模型的圖形表示。 請注意,隱藏在 sequential 中的模組以及包裝的 Module 都已內聯到圖中。 這是預設的抽象層級,但可以由 pass writer 配置。 更多資訊可以在 FX 概覽中找到 https://pytorch.dev.org.tw/docs/master/fx.html#module-torch.fx

將卷積與批次正規化融合

與其他一些融合不同,卷積與批次正規化的融合不需要任何新的運算子。 相反,由於推論期間的批次正規化包含逐點加法和乘法,因此這些運算可以“烘焙”到先前的卷積權重中。 這使我們可以完全從模型中移除批次正規化! 閱讀 https://nenadmarkus.com/p/fusing-batchnorm-and-conv/ 以瞭解更多詳細資訊。 此處的程式碼從 https://github.com/pytorch/pytorch/blob/orig/release/1.8/torch/nn/utils/fusion.py 複製,以提高清晰度。

def fuse_conv_bn_eval(conv, bn):
    """
    Given a conv Module `A` and an batch_norm module `B`, returns a conv
    module `C` such that C(x) == B(A(x)) in inference mode.
    """
    assert(not (conv.training or bn.training)), "Fusion only for eval!"
    fused_conv = copy.deepcopy(conv)

    fused_conv.weight, fused_conv.bias = \
        fuse_conv_bn_weights(fused_conv.weight, fused_conv.bias,
                             bn.running_mean, bn.running_var, bn.eps, bn.weight, bn.bias)

    return fused_conv

def fuse_conv_bn_weights(conv_w, conv_b, bn_rm, bn_rv, bn_eps, bn_w, bn_b):
    if conv_b is None:
        conv_b = torch.zeros_like(bn_rm)
    if bn_w is None:
        bn_w = torch.ones_like(bn_rm)
    if bn_b is None:
        bn_b = torch.zeros_like(bn_rm)
    bn_var_rsqrt = torch.rsqrt(bn_rv + bn_eps)

    conv_w = conv_w * (bn_w * bn_var_rsqrt).reshape([-1] + [1] * (len(conv_w.shape) - 1))
    conv_b = (conv_b - bn_rm) * bn_var_rsqrt * bn_w + bn_b

    return torch.nn.Parameter(conv_w), torch.nn.Parameter(conv_b)

FX 融合 Pass

現在我們有了計算圖以及融合卷積和批次正規化的方法,剩下的就是遍歷 FX 圖並應用所需的融合。

def _parent_name(target : str) -> Tuple[str, str]:
    """
    Splits a ``qualname`` into parent path and last atom.
    For example, `foo.bar.baz` -> (`foo.bar`, `baz`)
    """
    *parent, name = target.rsplit('.', 1)
    return parent[0] if parent else '', name

def replace_node_module(node: fx.Node, modules: Dict[str, Any], new_module: torch.nn.Module):
    assert(isinstance(node.target, str))
    parent_name, name = _parent_name(node.target)
    setattr(modules[parent_name], name, new_module)


def fuse(model: torch.nn.Module) -> torch.nn.Module:
    model = copy.deepcopy(model)
    # The first step of most FX passes is to symbolically trace our model to
    # obtain a `GraphModule`. This is a representation of our original model
    # that is functionally identical to our original model, except that we now
    # also have a graph representation of our forward pass.
    fx_model: fx.GraphModule = fx.symbolic_trace(model)
    modules = dict(fx_model.named_modules())

    # The primary representation for working with FX are the `Graph` and the
    # `Node`. Each `GraphModule` has a `Graph` associated with it - this
    # `Graph` is also what generates `GraphModule.code`.
    # The `Graph` itself is represented as a list of `Node` objects. Thus, to
    # iterate through all of the operations in our graph, we iterate over each
    # `Node` in our `Graph`.
    for node in fx_model.graph.nodes:
        # The FX IR contains several types of nodes, which generally represent
        # call sites to modules, functions, or methods. The type of node is
        # determined by `Node.op`.
        if node.op != 'call_module': # If our current node isn't calling a Module then we can ignore it.
            continue
        # For call sites, `Node.target` represents the module/function/method
        # that's being called. Here, we check `Node.target` to see if it's a
        # batch norm module, and then check `Node.args[0].target` to see if the
        # input `Node` is a convolution.
        if type(modules[node.target]) is nn.BatchNorm2d and type(modules[node.args[0].target]) is nn.Conv2d:
            if len(node.args[0].users) > 1:  # Output of conv is used by other nodes
                continue
            conv = modules[node.args[0].target]
            bn = modules[node.target]
            fused_conv = fuse_conv_bn_eval(conv, bn)
            replace_node_module(node.args[0], modules, fused_conv)
            # As we've folded the batch nor into the conv, we need to replace all uses
            # of the batch norm with the conv.
            node.replace_all_uses_with(node.args[0])
            # Now that all uses of the batch norm have been replaced, we can
            # safely remove the batch norm.
            fx_model.graph.erase_node(node)
    fx_model.graph.lint()
    # After we've modified our graph, we need to recompile our graph in order
    # to keep the generated code in sync.
    fx_model.recompile()
    return fx_model

注意

為了演示目的,我們在這裡進行了一些簡化,例如僅匹配 2D 卷積。 檢視 https://github.com/pytorch/pytorch/blob/master/torch/fx/experimental/fuser.py 以取得更可用的 pass。

測試我們的融合 Pass

我們現在可以在初始玩具模型上執行此融合 pass,並驗證我們的結果是否相同。 此外,我們可以印出融合模型的程式碼,並驗證不再有批次正規化。

fused_model = fuse(model)
print(fused_model.code)
inp = torch.randn(5, 1, 1, 1)
torch.testing.assert_allclose(fused_model(inp), model(inp))

在 ResNet18 上基準測試我們的融合

我們可以在更大的模型(如 ResNet18)上測試我們的融合 pass,並查看此 pass 在多大程度上提高了推論效能。

import torchvision.models as models
import time

rn18 = models.resnet18()
rn18.eval()

inp = torch.randn(10, 3, 224, 224)
output = rn18(inp)

def benchmark(model, iters=20):
    for _ in range(10):
        model(inp)
    begin = time.time()
    for _ in range(iters):
        model(inp)
    return str(time.time()-begin)

fused_rn18 = fuse(rn18)
print("Unfused time: ", benchmark(rn18))
print("Fused time: ", benchmark(fused_rn18))

正如我們之前看到的,我們的 FX 轉換的輸出是 (“torchscriptable”) PyTorch 程式碼,我們可以輕鬆地 jit.script 輸出以嘗試進一步提高我們的效能。 這樣,我們的 FX 模型轉換可以毫無問題地與 TorchScript 組合。

jit_rn18 = torch.jit.script(fused_rn18)
print("jit time: ", benchmark(jit_rn18))


############
# Conclusion
# ----------
# As we can see, using FX we can easily write static graph transformations on
# PyTorch code.
#
# Since FX is still in beta, we would be happy to hear any
# feedback you have about using it. Please feel free to use the
# PyTorch Forums (https://discuss.pytorch.org/) and the issue tracker
# (https://github.com/pytorch/pytorch/issues) to provide any feedback
# you might have.

腳本的總執行時間: ( 0 分鐘 0.000 秒)

Gallery 由 Sphinx-Gallery 產生

文件

取得 PyTorch 的完整開發者文件

檢視文件

教學

取得初學者和進階開發者的深入教學

檢視教學

資源

尋找開發資源並取得問題解答

檢視資源