pub struct Convolution<T: ConvElement>(/* private fields */);
Expand description

Convolution without pinned memory.

Implementations§

source§

impl Convolution<f32>

source

pub fn f32(size: u32) -> Result<ConvolutionBuilder<f32>>

Creates a new floating-point convolution builder. size determines the filter size and must be odd (1, 3, 5, …).

Panics

Panics if the filter size is even.

source§

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 fieldValue
signal_bias-S.bias
filter_bias-F.bias
output_biasO.bias
scaleS.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:

  1. Unbias the signal: S := S + params.signal_bias.
  2. Unbias the filters: F := F + params.filter_bias.
  3. Compute “standard” convolution output O := S (*) F using i32 precision.
  4. Upscale each number in the output: O := O * params.scale.
  5. If there is filter bias B provided, apply bias to the output per each output channel: O[f, ..] := O[f, ..] + B[f].
  6. Downscale the output: O := round(O / 2**self.bit_shift), where round() works as floating-point rounding with the default mode (round to nearest, ties to even).
  7. Apply output bias: O := O + params.output_bias.
  8. Saturate output to i8 range.
source

pub fn i8(size: u32) -> Result<ConvolutionBuilder<i8>>

Creates a new i8 convolution builder. size determines the filter size and must be odd (1, 3, 5, …).

Panics

Panics if the filter size is even.

source§

impl<T: ConvElement> Convolution<T>

source

pub fn size(&self) -> u32

Spatial size of the convolution.

source

pub fn params(&self) -> T::Params

Returns general parameters of the convolution.

source

pub fn set_params(&mut self, params: T::Params) -> Result<()>

Sets convolution parameters.

source

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 have MxK_HxK_WxC layout, where M is the number of filters, K_H and K_W are spatial dimensions of a filter, C is the number of input channels.
source

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.

source

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 have MxK_HxK_WxC layout, where M is the number of filters, K_H and K_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.
source

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().

Trait Implementations§

source§

impl<T> Debug for Convolution<T>where T: ConvElement, T::Params: Debug,

source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Convolution<T>

§

impl<T> Send for Convolution<T>where <T as ConvElement>::Params: Send,

§

impl<T> !Sync for Convolution<T>

§

impl<T> Unpin for Convolution<T>where T: Unpin, <T as ConvElement>::Params: Unpin,

§

impl<T> UnwindSafe for Convolution<T>where T: UnwindSafe, <T as ConvElement>::Params: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.