Pruning parameters of supported layers in :attr:`main_program` via
specified mask generation function given by :attr:`func_name`. This
function supports both training and inference controlled by :attr:`with_mask`.
If :attr:`with_mask` is True, it would also prune parameter related ASP mask Variables,
else only prunes parameters.
*Note*: If parameters are supported and in FP16, please set :attr:`n`=2, :attr:`m`=4,
if they in FP32, then :attr:`n`=1, :attr:`m`=2` to further enable Sparse Tensor Core acceleration.
*Note*: If calling this function with :attr:`with_mask`, it should call `OptimizerWithSparsityGuarantee.minimize`
and initialization (`exe.run(startup_program`)) before (For successfully obtain mask Variable).
Typically set `with_mask` as true for training (have called `OptimizerWithSparsityGuarantee.minimize`) and false for
inference only. To obtain OptimizerWithSparsityGuarantee, please see `sparsity.decoreate()`.
Args:
place (fluid.CPUPlace()|fluid.CUDAPlace(N)): Device place for pruned parameter and mask Variables, and N means the GPU's id. It should be the same as created instance of Executor.
main_program (Program, optional): Program with model definition and its parameters. Default is `paddle.static.default_main_program()
n (int): n of `n:m` sparse pattern.
m (int): m of `n:m` sparse pattern.
func_name (MaskAlgo, optional): The function name to generate spase mask. Default is `MaskAlgo.MASK_1D`. All options please refer to `MaskAlgo`.
with_mask (bool, optional): To prune mask Variables related to parameters or not. Ture is purning also, False is not. Defalut is True.
Returns:
dictionary: A dictionary with key: `parameter name` (string) and value: its corresponding mask Variable.
Examples:
.. code-block:: python
import paddle.fluid as fluid
from paddle.fluid.contrib import sparsity
main_program = fluid.Program()
startup_program = fluid.Program()
place = fluid.CUDAPlace(0)
with fluid.program_guard(main_program, startup_program):
ProgramASPInfo is a container to keep ASP relevant information of Pragrom. It contains three inner-variables:
1. __mask_vars (Dictionary): Key is parameter's name and vaule is its corresponding sparse mask Variable object, which is created by `ASPHelper.create_mask_variables`.
2. __masks (Dictionary): Key is parameter's name and vaule is its corressponding sparse mask Numpy array, which is created by `ASPHelper.prune_model`.
3. __excluded_layers (List): It stores name of layers which should not involve into ASP workflow.
This function is a decorator of `minimize` function in `Optimizer`.
There are three steps:
1. Call :attr:`optimizer`.minimize(:attr:`loss`)
2. Create sparse mask Tensors according to supported layers in :attr:`main_program`.
3. Insert masking ops in the end of parameters update.
*Note*: Please use `ASP.decorate` instead when applying distributed training with `Fleet`.
(Due to there is a invisiable graphs optimization in `Fleet.minimize()` which make training graph
cannot be modified anymore.)
Args:
optimizer (Optimizer): A Optimizer used for training.
loss (Variable): A Variable containing the value to minimize.
main_program (Program, optional): Program with model definition and its parameters. Default is `loss.block.program`.
startup_program (Program, optional): Program for initializing parameters in `parameter_list`. Default is `paddle.static.default_startup_program()`.
parameter_list (Iterable, optional): Iterable of `Variable` or `Variable.name` to update to minimize `loss`. The default value is None, at this time all parameters will be updated.
no_grad_set (set, optional): Set of `Variable or `Variable.name` that don't need to be updated. The default value is None.
Returns:
list: operators from :attr:`optimizer`.minimize(:attr:`loss`).
This function is to call `ASPHelper.minimize()` and return its return
Args:
loss (Variable): A Variable containing the value to minimize.
startup_program (Program, optional): Program for initializing parameters in `parameter_list`. Default is `paddle.static.default_startup_program()`.
parameter_list (Iterable, optional): Iterable of `Variable` or `Variable.name` to update to minimize `loss`. The default value is None, at this time all parameters will be updated.
no_grad_set (set, optional): Set of `Variable or `Variable.name` that don't need to be updated. The default value is None.
Returns:
list: operators from :attr:`optimizer`.minimize(:attr:`loss`).