[−][src]Struct ocl_convolution::Convolution
Convolution without pinned memory.
Methods
impl Convolution<f32>
[src]
pub fn f32(size: usize) -> Result<ConvolutionBuilder<f32>>
[src]
Creates a new floating-point convolution builder. size
determines the filter size
and should be odd (1, 3, 5, ...).
Panics
Panics if the filter size
is even.
impl Convolution<i8>
[src]
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 should 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.
pub fn i8(size: usize) -> Result<ConvolutionBuilder<i8>>
[src]
Creates a new i8
convolution builder. size
determines the filter size
and should be odd (1, 3, 5, ...).
Panics
Panics if the filter size
is even.
impl<T: ConvElement> Convolution<T>
[src]
pub fn size(&self) -> usize
[src]
Spatial size of the convolution.
pub fn params(&self) -> &T::Params
[src]
Returns general parameters of the convolution.
pub fn set_params(&mut self, params: T::Params) -> Result<()>
[src]
Sets convolution parameters.
pub fn with_filters<'a>(
self,
filters: impl Into<ArrayView4<'a, T>>
) -> Result<FiltersConvolution<T>>
[src]
self,
filters: impl Into<ArrayView4<'a, T>>
) -> Result<FiltersConvolution<T>>
Returns the convolution with pinned filter memory.
Parameters
filters
should 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.
pub fn with_biased_filters<'a>(
self,
filters: impl Into<ArrayView4<'a, T>>,
filter_biases: &[T::Acc]
) -> Result<FiltersConvolution<T>>
[src]
self,
filters: impl Into<ArrayView4<'a, T>>,
filter_biases: &[T::Acc]
) -> Result<FiltersConvolution<T>>
Returns the convolution with pinned filter / filter bias memory.
pub fn compute<'a>(
&self,
signal: FeatureMap<T>,
filters: impl Into<ArrayView4<'a, T>>
) -> Result<Array4<T>>
[src]
&self,
signal: FeatureMap<T>,
filters: impl Into<ArrayView4<'a, T>>
) -> Result<Array4<T>>
Performs convolution on the provided signal
and filters
.
Parameters
filters
should 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
- The method will panic if
filters
do not have expected spatial dimensions, i.e.,self.size() x self.size()
. - Likewise, the method will panic if the number of input channels differs from number of
channels in
filters
.
pub fn compute_with_biases<'a>(
&self,
signal: FeatureMap<T>,
filters: impl Into<ArrayView4<'a, T>>,
filter_biases: &[T::Acc]
) -> Result<Array4<T>>
[src]
&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 generally the same as for
compute()
.
Trait Implementations
impl<T: Debug + ConvElement> Debug for Convolution<T>
[src]
Auto Trait Implementations
impl<T> !RefUnwindSafe for Convolution<T>
impl<T> Send for Convolution<T> where
<T as WithParams>::Params: Send,
<T as WithParams>::Params: Send,
impl<T> !Sync for Convolution<T>
impl<T> Unpin for Convolution<T> where
T: Unpin,
<T as WithParams>::Params: Unpin,
T: Unpin,
<T as WithParams>::Params: Unpin,
impl<T> UnwindSafe for Convolution<T> where
T: UnwindSafe,
<T as WithParams>::Params: UnwindSafe,
T: UnwindSafe,
<T as WithParams>::Params: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,