Struct ocl_convolution::Convolution
source · 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>
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 (*) F
usingi32
precision. - Upscale each number in the output:
O := O * params.scale
. - If there is filter bias
B
provided, 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
i8
range.
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
filters
must haveMxK_HxK_WxC
layout, whereM
is the number of filters,K_H
andK_W
are spatial dimensions of a filter,C
is 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
filters
must haveMxK_HxK_WxC
layout, whereM
is the number of filters,K_H
andK_W
are spatial dimensions of a filter,C
is 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
filters
do 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()
.