Layers
Approximate Layer implementations
ApproxConv2d
Bases: ApproxLayer
, Conv2d
Approximate 2D Convolution layer implementation
Source code in src/torchapprox/layers/approx_conv2d.py
17 18 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
|
conv_args: Conv2dArgs
property
Wrap layer configuration in dataclass for more convenient passing around
fan_in: int
property
Number of incoming connection for a single neuron
from_conv2d(conv2d)
staticmethod
Construct ApproxConv2d from torch.nn.Conv2d layer
Source code in src/torchapprox/layers/approx_conv2d.py
from_super(cls_instance)
staticmethod
output_dims(x)
Output width and height
Source code in src/torchapprox/layers/approx_conv2d.py
ApproxLayer
Bases: ABC
Derivable Abstract Base Class for implementing Approximate Neural Network layers
Source code in src/torchapprox/layers/approx_layer.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 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 274 275 276 277 278 279 280 |
|
fan_in: int
abstractmethod
property
Number of incoming connections for a neuron in this layer
lut: torch.Tensor
property
writable
The Lookup table to use for approximate multiplication. LUT can be:
- None
: An accurate product is used internall. This is much faster than passing
operands through LUT kernels. Functionally equivalent to running the layer in
quant
mode, but useful when the unfolded inputs/outputs need to be traced at runtime.
- torch.Tensor
or numpy.array
:
- 2D array of size 256x256 is required. Unused entries will be ignored when simulating
multiplication where the operand width is less than 8 Bit
- When supplying a torch.Tensor
the datatype needs to be signed 16-Bit.
mean: float
property
writable
Perturbation Error mean
Returns:
Type | Description |
---|---|
float
|
Currently configured perturbation mean |
opcount: int
abstractmethod
property
Number of multiplications for a single forward pass of this layer
stdev: float
property
writable
Perturbation Error Relative Standard Deviation
Returns:
Type | Description |
---|---|
float
|
Currently configured perturbation standard deviation |
approx_fwd(x, w, quant_params)
abstractmethod
Approximate Product Forward Pass Performs the layer operation using the currently configured approximate product Lookup Table.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
CharTensor
|
Layer input |
required |
Returns:
Type | Description |
---|---|
Layer output |
Source code in src/torchapprox/layers/approx_layer.py
forward(x, x_scale=None, x_zero_point=None, bias=None)
Forward pass with currently selected mode applied
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Tensor
|
Layer input |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Layer output |
Source code in src/torchapprox/layers/approx_layer.py
noise_fwd(x_q, w_q)
Quantized Forward Pass that is perturbed with Gaussian Noise
The standard deviation of the additive noise
is derived from the stdev
parameter and scaled
with the standard deviation of the current batch
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Layer input |
required |
Returns:
Type | Description |
---|---|
FloatTensor
|
Layer output |
Source code in src/torchapprox/layers/approx_layer.py
quant_fwd(x, w)
abstractmethod
Quantized Forward Pass Performs the layer operation with an additional pass through the currently configured quantizer.
`x_q and w_q are expected to be fake-quantized tensors, i.e. floats that are discretized to a set of values, but not converted to actual their integer representation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_q |
Fake-quantized activations |
required | |
w_q |
Fake-quantized weights |
required |
Returns:
Type | Description |
---|---|
FloatTensor
|
Layer output |
Source code in src/torchapprox/layers/approx_layer.py
ApproxLinear
Bases: ApproxLayer
, Linear
Approximate Linear Layer implementation
Source code in src/torchapprox/layers/approx_linear.py
ApproxWrapper
Bases: Module
Wrapper for adding quant/dequant stubs to a linear layer in a model.
PyTorch provides the option to wrap modules in quantizers automatically, however a custom module is necessary so that we can forward the activation quantization scale and zero point to the approximate layer in the forward function.
The wrapped instance of torch.nn.Module
is meant to be replaced with an instance of
torchapprox.layers.ApproxLayer
in a separate call to
torch.ao.quantization.prepare()
after it has been wrapped here.
Source code in src/torchapprox/layers/approx_wrapper.py
__init__(wrapped, qconfig=None)
Wrap a torch.nn.linear layer with quantization stubs
Parameters:
Name | Type | Description | Default |
---|---|---|---|
wrapped |
Union[Linear, Conv2d]
|
the layer to be wrapped |
required |
qconfig |
Optional[QConfig]
|
Quantization configuration. Defaults to None. |
None
|
Source code in src/torchapprox/layers/approx_wrapper.py
InferenceMode
Bases: Enum
Layer inference mode. Can be any of:
- quant
: Run inference using the layer's quantizer
- approx
: Run inference using approximate product LUT
- noise
: Run inference that is perturbed with additive Gaussian noise