pub struct Convolution<T: ConvElement>(/* private fields */);Expand description
Convolution without pinned memory.
Implementations§
Source§impl Convolution<f32>
impl Convolution<f32>
Source§impl Convolution<i8>
Quantized convolution over signed 8-bit integers.
impl Convolution<i8>
Quantized convolution over signed 8-bit integers.
Due to use of i8 inputs, computations are performed much faster than on f32 inputs
(the difference manifests most on the specialized hardware, but it is seen in this
OpenCL-powered implementation as well).
§Connection to real-value convolution
Quantized convolution mirrors real-valued convolution in which i8 elements
of the signal, filter and output tensors represent real-valued numbers with the
following mapping:
let scale: f32 = // ...
let bias: i32 = // ...
|x: i8| -> f32 { scale * (i32::from(x) - bias) as f32 }scale and bias may differ for different tensors; these params are usually determined
by profiling the corresponding convolutional neural network (see e.g. this paper).
Denote these quantiation params for tensor T as T.scale and T.bias. Denote S
the signal, F the filter, O the output. Convolution parameters must be set as follows:
I8Params field | Value |
|---|---|
signal_bias | -S.bias |
filter_bias | -F.bias |
output_bias | O.bias |
scale | S.scale * F.scale / O.scale |
scale is represented as a fixed-point number with bit_shift binary digits after
the point. Note that filter biases B are not transformed during the computation.
§Computing convolution
Suppose S is the signal and F is the filter tensor; both contain i8 values.
The computation is performed as follows:
- Unbias the signal:
S := S + params.signal_bias. - Unbias the filters:
F := F + params.filter_bias. - Compute “standard” convolution output
O := S (*) Fusingi32precision. - Upscale each number in the output:
O := O * params.scale. - If there is filter bias
Bprovided, apply bias to the output per each output channel:O[f, ..] := O[f, ..] + B[f]. - Downscale the output:
O := round(O / 2**self.bit_shift), whereround()works as floating-point rounding with the default mode (round to nearest, ties to even). - Apply output bias:
O := O + params.output_bias. - Saturate output to
i8range.
Source§impl<T: ConvElement> Convolution<T>
impl<T: ConvElement> Convolution<T>
Sourcepub fn set_params(&mut self, params: T::Params) -> Result<()>
pub fn set_params(&mut self, params: T::Params) -> Result<()>
Sets convolution parameters.
Sourcepub fn with_filters<'a>(
self,
filters: impl Into<ArrayView4<'a, T>>,
) -> Result<FiltersConvolution<T>>
pub fn with_filters<'a>( self, filters: impl Into<ArrayView4<'a, T>>, ) -> Result<FiltersConvolution<T>>
Returns the convolution with pinned filter memory.
§Parameters
filtersmust haveMxK_HxK_WxClayout, whereMis the number of filters,K_HandK_Ware spatial dimensions of a filter,Cis the number of input channels.
Sourcepub fn with_biased_filters<'a>(
self,
filters: impl Into<ArrayView4<'a, T>>,
filter_biases: &[T::Acc],
) -> Result<FiltersConvolution<T>>
pub fn with_biased_filters<'a>( self, filters: impl Into<ArrayView4<'a, T>>, filter_biases: &[T::Acc], ) -> Result<FiltersConvolution<T>>
Returns the convolution with pinned filter / filter bias memory.
Sourcepub fn compute<'a>(
&self,
signal: FeatureMap<'_, T>,
filters: impl Into<ArrayView4<'a, T>>,
) -> Result<Array4<T>>
pub fn compute<'a>( &self, signal: FeatureMap<'_, T>, filters: impl Into<ArrayView4<'a, T>>, ) -> Result<Array4<T>>
Performs convolution on the provided signal and filters.
§Parameters
filtersmust haveMxK_HxK_WxClayout, whereMis the number of filters,K_HandK_Ware spatial dimensions of a filter,Cis the number of input channels.
§Return value
The output will have the same layout as signal. An error means something wrong
with OpenCL.
§Panics
- Panics if
filtersdo not have expected spatial dimensions, i.e.,self.size() x self.size(). - Panics if the number of input channels differs from number of channels in
filters.
Sourcepub fn compute_with_biases<'a>(
&self,
signal: FeatureMap<'_, T>,
filters: impl Into<ArrayView4<'a, T>>,
filter_biases: &[T::Acc],
) -> Result<Array4<T>>
pub fn compute_with_biases<'a>( &self, signal: FeatureMap<'_, T>, filters: impl Into<ArrayView4<'a, T>>, filter_biases: &[T::Acc], ) -> Result<Array4<T>>
Performs convolution on the provided signal and filters, with the output offset
by the provided per-filter biases.
Parameters, return value and panics are the same as for Self::compute().