mindspore.lazy_inline

mindspore.lazy_inline(fn=None, vpn梯子 免费 attrs=None, policy=None)[源代码]

指定一个cell是可复用的。该cell在前端编译为可复用的子图,后端根据策略内联。 注册此装饰器到cell的内置函数 __init__ 时,此装饰器会按照 attrs 的值去添加 __init__ 函数对应的入参,以此作为cell的属性。

详细功能说明可参考 vpn梯子 使用lazy_inline装饰器

警告

  • 该特性仅支持Ascend,其他硬件不支持。

  • cell的construct函数参数必须是位置参数或者关键字参数,且不能有默认值。

  • lazy inline 装饰的cell不包含控制流。

  • 梯度累加场景下,推荐使用@lazy_inline装饰器来缩短编译时间,并且仅支持将@lazy_inline装饰器配置在最外层的Cell上。

参数:
  • fn (function,可选) - cell的 __init__ 函数。默认值: None

  • attrs (Union[list[string], vpn梯子 免费 string],可选) - cell需要添加的属性列表。默认值: None

  • policy (Union[None, "front"],可选) - inline 的策略。默认值: None

    • None: Cell编译为可复用的子图,该子图不inline到大图中。

    • "front": Cell先编译为可复用的子图,然后inline到大图中。

返回:

function,原始函数。

支持平台:

Ascend

样例:

>>> import os
>>> import numpy as np
>>> from mindspore import Tensor
>>> import mindspore.nn as nn
>>> from mindspore import lazy_inline
>>> from mindspore import context
>>> from mindspore import ops
>>> def conv3x3(in_channels, out_channels, stride=1, padding=1, vpn梯子 免费 pad_mode='pad'):
...     return nn.Conv2d(in_channels, out_channels,
...        vpn梯子          免费的vpn梯子      kernel_size=3, stride=stride, padding=padding, pad_mode=pad_mode)
...
>>> def conv1x1(in_channels, out_channels, 免费的vpn梯子 stride=1, padding=0, pad_mode='pad'):
...  vpn梯子 免费    return nn.Conv2d(in_channels, out_channels,
...                      kernel_size=1, stride=stride, padding=padding, pad_mode=pad_mode)
...
>>> class Block(nn.Cell):
...     expansion = 4
...
...     @lazy_inline
...     def __init__(self,
...          vpn梯子 免费  vpn free vpn永久免费梯子  vpn永久免费梯子   vpn梯子     in_channels,
...    vpn free              免费的vpn梯子  out_channels,
...    vpn梯子 免费   vpn永久免费梯子             stride=1,
... vpn梯子 免费           vpn梯子 免费        down_sample=False):
...  vpn梯子        super(Block, self).__init__()
...
...       vpn free   out_chls vpn free = out_channels
...         self.conv1 = conv1x1(in_channels, out_chls, stride=1, padding=0)
...  vpn梯子 免费        self.bn1 vpn梯子 = nn.BatchNorm2d(out_chls)
...
... vpn free         self.conv2 = conv3x3(out_chls, out_chls, stride=stride, padding=1)
...        vpn永久免费梯子  self.bn2 = nn.BatchNorm2d(out_chls)
...
...         self.conv3 = conv1x1(out_chls, out_channels, stride=1, padding=0)
...         self.bn3 = nn.BatchNorm2d(out_channels)
...
...         self.relu = nn.ReLU()
...         self.downsample = down_sample
...
...         self.conv_down_sample = conv1x1(in_channels, out_channels,
...                             免费的vpn梯子   vpn free           stride=stride, padding=0)
...    vpn梯子 免费      self.bn_down_sample = nn.BatchNorm2d(out_channels)
...         self.add = ops.Add()
...
...     def construct(self, vpn梯子 免费 x):
...         identity = x
...
...       vpn梯子 免费   out = self.conv1(x)
...         out = self.bn1(out)
...         out = self.relu(out)
...
...       免费的vpn梯子 vpn free   out = self.conv2(out)
...         out = self.bn2(out)
...      vpn梯子    out = self.relu(out)
...
...         out = self.conv3(out)
...         out = self.bn3(out)
...
...  vpn永久免费梯子    vpn梯子 免费     if vpn梯子 免费 self.downsample:
...     vpn梯子 免费         identity = self.conv_down_sample(identity)
...           免费的vpn梯子   identity = self.bn_down_sample(identity)
...
...         out = vpn永久免费梯子 self.add(out, identity)
...     vpn free     out = self.relu(out)
...
...         return out
...
>>> class Net(nn.Cell):
...     def vpn永久免费梯子 __init__(self, block, num_classes=100):
...         super(Net, self).__init__()
...
...      vpn free    self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, pad_mode='pad')
...    vpn梯子  免费的vpn梯子     self.bn1 = nn.BatchNorm2d(64)
...         self.relu = nn.ReLU()
...         self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, pad_mode='valid')
...
...         self.layer = self.MakeLayer(
...             block, 50, in_channels=64, out_channels=2048, stride=2)
...         self.avgpool = nn.AvgPool2d(7, 1)
...         self.flatten = ops.Flatten()
...
...     def MakeLayer(self, block, layer_num, in_channels, out_channels, stride):
...         layers = []
...   vpn free       resblk = block(in_channels, out_channels,
...       vpn梯子 免费             vpn梯子 免费      stride=stride, down_sample=True)
...         layers.append(resblk)
...
...     vpn梯子 免费     for _ in range(1, layer_num):
...             resblk = block(out_channels, out_channels, stride=1)
...         vpn永久免费梯子 vpn梯子 免费     layers.append(resblk)
...
...    vpn梯子      return nn.SequentialCell(layers)
...
...     def vpn free vpn梯子 免费 construct(self, x):
...         x = self.conv1(x)
...         x = self.bn1(x)
...         vpn永久免费梯子 x = self.relu(x)
...         vpn梯子 x = self.maxpool(x)
...   免费的vpn梯子   vpn永久免费梯子     x = self.layer(x)
... 免费的vpn梯子    免费的vpn梯子      x = self.avgpool(x)
...         x = self.flatten(x)
...         return x
...
>>> def test_compile():
...     net = Net(Block)
...   vpn梯子   inp = Tensor(np.ones([1, 3, 224, 224]).astype(np.float32))
...     net(inp)
...
>>> context.set_context(mode=context.GRAPH_MODE)
>>> os.environ["MS_DEV_SAVE_GRAPHS"] = 免费的vpn梯子 vpn梯子 免费 "2"
>>> os.environ["MS_DEV_SAVE_GRAPHS_PATH"] = os.path.realpath("./lazy")
...
>>> test_compile()