sparse
MagnitudePruningCallback (Module)
¶
__init__(self, mask_refresh_interval=-1, stop_mask_refresh=inf, use_gradient=False, running_average=True, l0=False, forward_hook=None)
special
¶
Magnitude-based pruning function as the callback of prune.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mask_refresh_interval |
int |
number of steps to refresh mask. Defaults to 1. |
-1 |
stop_mask_refresh |
int |
when to stop refreshing mask. Defaults to float('inf'). |
inf |
use_gradient |
bool |
whether use the magnitude of gradients |
False |
running_average |
bool |
whether use the running average of magnitude. Defaults to True. |
True |
l0 |
bool |
whether to use l0 magnitude instead of l0 |
False |
forward_hook |
Callable |
callback function that gets executed at each forward. Defaults to None. |
None |
Source code in qsparse/sparse.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
forward(self, x, sparsity, mask, name='')
¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
.. note::
Although the recipe for forward pass needs to be defined within
this function, one should call the :class:Module
instance afterwards
instead of this since the former takes care of running the
registered hooks while the latter silently ignores them.
Source code in qsparse/sparse.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|
PruneLayer (Module)
¶
Applies pruning over input tensor. Please look for detailed description in prune
initted: bool
property
readonly
¶
whether the parameters of the prune layer are initialized.
forward(self, x)
¶
Prunes input tensor according to given sparsification schedule.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
torch.Tensor |
tensor to be pruned |
required |
Exceptions:
Type | Description |
---|---|
RuntimeError |
when the shape of input tensors mismatch with the shape of binary mask |
Returns:
Type | Description |
---|---|
torch.Tensor |
pruned tensor |
Source code in qsparse/sparse.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
|
UniformPruningCallback (MagnitudePruningCallback)
¶
unstructured uniform pruning function.
This function will prune uniformly without considering magnitude of the input tensors. If a init mask is provided, it will not reactivate those already pruned locations in init mask.
prune(inp=None, sparsity=0.5, dimensions={1}, callback=None, start=1000, interval=1000, repetition=4, rampup=False, name='')
¶
Creates a PruneLayer which is usually used for feature pruning if no input module is provided, or creates a weight- pruned version of the input module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inp |
nn.Module |
input module whose weight is to be pruned. Defaults to None. |
None |
sparsity |
float |
target sparsity. Defaults to 0.5. |
0.5 |
dimensions |
Iterable[int] |
which dimensions to prune. Defaults to {1}, pruning the channel dimension of conv feature map. |
{1} |
callback |
MagnitudePruningCallback |
callback for actual operation of calculating binary mask and prune inputs. Defaults to MagnitudePruningCallback. |
None |
start |
int |
starting step to apply pruning. Defaults to 1000. |
1000 |
interval |
int |
interval of iterations between each sparsity increasing steps. Defaults to 1000. |
1000 |
repetition |
int |
number of sparsity increasing steps. Defaults to 4. |
4 |
rampup |
bool |
whether to wait another interval before starting to prune. Defaults to False. |
False |
name |
str |
name of the prune layer created, used for better logging. Defaults to "". |
'' |
Returns:
Type | Description |
---|---|
nn.Module |
input module with its weight pruned or a instance of PruneLayer for feature pruning |
Source code in qsparse/sparse.py
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
|