prune_model

paddle.incubate.asp. prune_model ( model, n=2, m=4, mask_algo='mask_1d', with_mask=True ) [源代码]

使用 mask_algo 指定的掩码生成函数裁剪 model 中支持 ASP(Auto SParsity) 的子层参数。支持训练和推理,并由 with_mask 参数控制。如果 with_mask 是 True ,则还会裁剪与参数相关的 ASP 掩码变量,如果是 False,则仅裁剪参数本身。

注解

  • 在静态图模式下,使用 with_mask 调用函数时,需要先调用 OptimizerWithSparsityGuarantee.minimize 和 exe.run(startup_program) 来成功获取掩码变量。通常情况下训练时(已调用 OptimizerWithSparsityGuarantee.minimize)设置 with_mask 为 True。而仅进行推理时,设置 with_mask 为 False。 获取 OptimizerWithSparsityGuarantee 请参考 paddle.incubate.asp.decorate

  • 在动态图模式下,使用 with_mask 调用函数时,需要先调用 paddle.incubate.asp.decorate 来获取掩码变量。

参数

  • model (Program|nn.Layer) - 包含模型定义和参数的 Program ,或者 paddle.nn.Layer 对象

  • n (int,可选) - n:m 稀疏模式中的 n ,默认值为 2。

  • m (int,可选) - n:m 稀疏模式中的 m ,默认值为 4。

  • mask_algo (string,可选) - 生成稀疏掩码的函数名。默认值为 mask_1d。有效输入应为 mask_1d , mask_2d_greedy 和 mask_2d_best 中的一个。

  • with_mask (bool,可选) - 选择是否裁剪参数相关的 ASP 掩码变量,True 是要裁剪,False 就是不裁剪。默认是 True。

返回

dictionary - 一个字典,key 是参数名称,value 是对应的掩码变量。

代码示例

  1. 动态图模式

 >>> # Example1: Usage of Dynamic Graph
 >>> import paddle
 >>> import numpy as np

 >>> class MyLayer(paddle.nn.Layer):
 ...     def __init__(self):
 ...         super().__init__()
 ...         self.conv1 = paddle.nn.Conv2D(
 ...             in_channels=3, out_channels=4, kernel_size=3, padding=2)
 ...         self.linear1 = paddle.nn.Linear(4624, 32)
 ...         self.linear2 = paddle.nn.Linear(32, 32)
 ...         self.linear3 = paddle.nn.Linear(32, 10)
 ...
 ...     def forward(self, img):
 ...         hidden = self.conv1(img)
 ...         hidden = paddle.flatten(hidden, start_axis=1)
 ...         hidden = self.linear1(hidden)
 ...         hidden = self.linear2(hidden)
 ...         prediction = self.linear3(hidden)
 ...         return prediction

 >>> my_layer = MyLayer()
 >>> loss_fn = paddle.nn.MSELoss(reduction='mean')

 >>> optimizer = paddle.optimizer.SGD(
 ...     learning_rate=0.01, parameters=my_layer.parameters())

 >>> # Calling paddle.incubate.asp.decorate() to wrap step() in optimizer, which
 >>> # will apply necessary masking operations for ASP workflow.
 >>> # In dynamic graph mode, ASP would create related mask variables during decoration.
 >>> optimizer = paddle.incubate.asp.decorate(optimizer)

 >>> # Must call paddle.incubate.asp.decorate() first before calling paddle.incubate.asp.prune_model()
 >>> paddle.incubate.asp.prune_model(my_layer, mask_algo='mask_2d_best')

 >>> for i in range(10):
 ...     imgs = paddle.to_tensor(
 ...         np.random.randn(64, 3, 32, 32),
 ...         dtype='float32', stop_gradient=False)
 ...     labels = paddle.to_tensor(
 ...         np.random.randint(10, size=(64, 1)),
 ...         dtype='float32', stop_gradient=False)
 ...     output = my_layer(imgs)
 ...     loss = loss_fn(output, labels)
 ...     loss.backward()
 ...     optimizer.step()
 ...     optimizer.clear_grad()
  1. 静态图模式

 >>> # Example2: Usage of Static Graph
 >>> import paddle
 >>> import numpy as np

 >>> paddle.enable_static()

 >>> class MyLayer(paddle.nn.Layer):
 ...     def __init__(self):
 ...         super().__init__()
 ...         self.conv1 = paddle.nn.Conv2D(
 ...             in_channels=3, out_channels=4, kernel_size=3, padding=2)
 ...         self.linear1 = paddle.nn.Linear(4624, 32)
 ...         self.linear2 = paddle.nn.Linear(32, 32)
 ...         self.linear3 = paddle.nn.Linear(32, 10)
 ...
 ...     def forward(self, img):
 ...         hidden = self.conv1(img)
 ...         hidden = paddle.flatten(hidden, start_axis=1)
 ...         hidden = self.linear1(hidden)
 ...         hidden = self.linear2(hidden)
 ...         prediction = self.linear3(hidden)
 ...         return prediction

 >>> main_program = paddle.static.Program()
 >>> startup_program = paddle.static.Program()

 >>> with paddle.static.program_guard(main_program, startup_program):
 ...     input_data = paddle.static.data(name='data', shape=[None, 3, 32, 32])
 ...     label = paddle.static.data(name='label', shape=[None, 1])
 ...     my_layer = MyLayer()
 ...     prob = my_layer(input_data)
 ...     loss = paddle.mean(paddle.nn.functional.square_error_cost(prob, label))
 ...
 ...     optimizer = paddle.optimizer.SGD(learning_rate=0.1)
 ...     # Calling paddle.incubate.asp.decorate() to wrap minimize() in optimizer, which
 ...     # will insert necessary masking operations for ASP workflow.
 ...     # In static graph mode, ASP creates related mask variables
 ...     # during minimize().
 ...     optimizer = paddle.incubate.asp.decorate(optimizer)
 ...     optimizer.minimize(loss, startup_program)

 >>> device = paddle.device.get_device()
 >>> place = paddle.set_device(device)

 >>> exe = paddle.static.Executor(place)
 >>> exe.run(startup_program)

 >>> # Must call exe.run(startup_program) first before calling paddle.asp.prune_model()
 >>> paddle.incubate.asp.prune_model(my_layer, mask_algo='mask_2d_best')
 >>> # it also be accepted to call
 >>> # paddle.incubate.asp.prune_model(main_program, mask_algo='mask_2d_best')

 >>> for i in range(10):
 ...     imgs = np.random.randn(64, 3, 32, 32).astype('float32')
 ...     labels = np.random.randint(10, size=(64, 1)).astype('float32')
 ...     exe.run(main_program, feed={'data':imgs, 'label':labels})