util
logging
¶
wrapper of logging module. use print
if logging module is not configured.
auto_name_prune_quantize_layers(net)
¶
Set name attribute of Prune/Quantize layers based on their torch module paths. This utility can be applied for better logging.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
net |
nn.Module |
network module with PruneLayer and QuantizeLayer. |
required |
Returns:
Type | Description |
---|---|
nn.Module |
modified module |
Source code in qsparse/util.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
|
calculate_mask_given_importance(importance, sparsity)
¶
return a binary torch tensor with sparsity equals to the given sparsity.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
importance |
torch.Tensor |
Floating-point tensor represents importance. |
required |
sparsity |
float |
sparsity level in |
required |
Returns:
Type | Description |
---|---|
torch.Tensor |
binary mask |
Source code in qsparse/util.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
|
get_option(key)
¶
return the requested option. The exported alias of this function is get_qsparse_option
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
str |
option name |
required |
Returns:
Type | Description |
---|---|
option value |
Source code in qsparse/util.py
29 30 31 32 33 34 35 36 37 38 39 40 |
|
nn_module(mod)
¶
Return actual module of a nn.Module
or nn.DataParallel
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mod |
nn.Module |
input pytorch module |
required |
Returns:
Type | Description |
---|---|
nn.Module |
actual module |
Source code in qsparse/util.py
64 65 66 67 68 69 70 71 72 73 74 75 76 |
|
preload_qsparse_state_dict(model, state_dict)
¶
calling before load_state_dict
to preload the state dict of QSPARSE layers, because load_state_dict
currently does not allow shape mismatch
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
torch.nn.Module |
model to be loaded |
required |
state_dict |
Dict[str, torch.Tensor] |
state dict |
required |
Returns:
Type | Description |
---|---|
torch.nn.Module |
loaded model |
Source code in qsparse/util.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
|
set_options(log_on_created=None, log_during_train=None)
¶
set QSPARSE options. Only the options given will be updated. The exported alias of this function is set_qsparse_options
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
log_on_created |
Optional[bool] |
If set to True, QSPARSE will log into console when every prune/quantize layer is created, the built-in value is True. Defaults to None. |
None |
log_during_train |
Optional[bool] |
If set to True, QSPARSE will log into console when pruning and quantization happen, the built-in value is True. Defaults to None. |
None |
Source code in qsparse/util.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
squeeze_tensor_to_shape(x, shape)
¶
squeeze a tensor to a given shape through averaging.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
torch.Tensor |
input tensor |
required |
shape |
List[int] |
target shape |
required |
Exceptions:
Type | Description |
---|---|
ValueError |
when the input tensor has different number of dimensions than the target shape, or the target shape provides a non-1 dimension to reduce |
Returns:
Type | Description |
---|---|
torch.Tensor |
aligned tensor |
Source code in qsparse/util.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
|