reset_excluded_layers¶
- paddle.incubate.asp. reset_excluded_layers ( main_program=None ) [source]
-
Reset excluded layers setting corresponding to
main_program
. Ifmain_program
is None, then all configurations of excluded_layers would be cleaned.- Parameters
-
main_program (Program, optional) – Program with model definition and its parameters. If None is given, then this function would reset all excluded_layers. Default is None.
Examples
>>> # Example1: Usage of Dynamic Graph >>> import paddle >>> 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, 100) ... ... def forward(self, img): ... hidden = self.conv1(img) ... hidden = paddle.flatten(hidden, start_axis=1) ... prediction = self.linear1(hidden) ... return prediction >>> my_layer = MyLayer() >>> optimizer = paddle.optimizer.SGD( ... learning_rate=0.01, parameters=my_layer.parameters()) >>> # Need to set excluded layers before calling decorate >>> paddle.incubate.asp.set_excluded_layers([my_layer.linear1.full_name()]) >>> # Reset excluded_layers, all supported layers would be included into Automatic SParsity's workflow. >>> # Please note, reset_excluded_layers also must be called before calling asp.decorate(). >>> paddle.incubate.asp.reset_excluded_layers() >>> optimizer = paddle.incubate.asp.decorate(optimizer)
>>> # Example2: Usage of Static Graph >>> import paddle >>> 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, 100) ... ... def forward(self, img): ... hidden = self.conv1(img) ... hidden = paddle.flatten(hidden, start_axis=1) ... prediction = self.linear1(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, 224, 224]) ... label = paddle.static.data(name='label', shape=[None, 100]) ... my_layer = MyLayer() ... prob = my_layer(input_data) ... loss = paddle.mean(paddle.nn.functional.square_error_cost(prob, label)) ... ... # Setup excluded layers out from ASP workflow. ... # Please note, excluded_layers must be set before calling optimizer.minimize(). ... paddle.incubate.asp.set_excluded_layers([my_layer.linear1.full_name()], main_program) ... # Reset excluded_layers, all supported layers would be included into Automatic SParsity's workflow. ... # Please note, reset_excluded_layers also must be called before calling optimizer.minimize(). ... paddle.incubate.asp.reset_excluded_layers(main_program) ... ... optimizer = paddle.optimizer.SGD(learning_rate=0.1) ... optimizer = paddle.static.amp.decorate(optimizer ) ... # Calling paddle.incubate.asp.decorate() to wrap minimize() in optimizer, which ... # will insert necessary masking operations for ASP workflow. ... optimizer = paddle.incubate.asp.decorate(optimizer) ... optimizer.minimize(loss, startup_program)