#machine-learning #convolution #image #pytorch #compatible #neural #neural-network


Fast, minimal dependency, completely Rust implementation of convolutions for machine learning

10 releases

0.3.4 Apr 18, 2022
0.3.3 Apr 18, 2022
0.3.1 Mar 22, 2022
0.2.3 Mar 21, 2022
0.1.0 Oct 13, 2021

#319 in Machine learning

Download history 35/week @ 2024-03-31 9/week @ 2024-04-07 9/week @ 2024-04-14 14/week @ 2024-04-21 13/week @ 2024-04-28 14/week @ 2024-05-05 14/week @ 2024-05-12 18/week @ 2024-05-19 13/week @ 2024-05-26 22/week @ 2024-06-02 30/week @ 2024-06-09 29/week @ 2024-06-16 17/week @ 2024-06-23 1/week @ 2024-06-30 7/week @ 2024-07-07

57 downloads per month
Used in 3 crates

MIT license

584 lines


convolutions-rs is a crate that provides a fast, well-tested convolutions library for machine learning written entirely in Rust with minimal dependencies. In particular, this is the first convolutions crate that has absolutely no dependencies on native C libraries. We provide both transposed convolutions (also called deconvolutions), as well as normal convolutions.

This crate has been developed in the course of the ZipNet project (https://github.com/Conzel/zipnet), where we required a C-free implementation of convolutions in order to compile our code to WebASM.


  • Minimal dependencies, especially no C-dependencies
  • Extensively tested through randomly generated unit tests
  • 100% compatible with Pytorch implementation
  • Generics to ensure smooth usage
  • Speed verified by benchmarking

As of now, this crate is as fast as Pytorch on small images, but has a noticeable slowdown on large and medium images (takes ~20-50x as much time). We are still reaonsably fast enough for research/sample applications, but we aim to improve the speed to get close to PyTorch. Benchmarks can be found at https://github.com/Conzel/convolutions-rs-benchmarks/.

Additionally, Transposed Convolution is currently relatively slow. We are working to alleviate the issue.

Compatibility with implementations

As mentioned in the bullet points above, we are 100% compatible with the Pytorch implementation. This doesn't mean that we support all operations that Pytorch has, but all those which we support give the same output as in Pytorch. This is verified by random array tests.

The implementations of Pytorch and Tensorflow mostly agree. The only case we found where they sometimes don't agree is same padding, as there might be different strategies to do this. This is not a problem for strides of size 1, but it results in different array values for strides > 1.

See here for further discussion: https://stackoverflow.com/questions/52975843/comparing-conv2d-with-padding-between-tensorflow-and-pytorch


As mentioned, this package provides normal convolutions as well as transposed convolutions. We provide both in the form of free functions as well as something resembling a neural network layer. This crate also requires ndarray to use the functions, as input and output are in the form of ndarrays.


use convolutions_rs::convolutions::*;
use ndarray::*;
use convolutions_rs::Padding;

// Input has shape (channels, height, width)
let input = Array::from_shape_vec(
    (1, 4, 4),

// Kernel has shape (channels out, channels in, height, width)
let kernel: Array4<f32> = Array::from_shape_vec(

let conv_layer = ConvolutionLayer::new(kernel.clone(), 1, Padding::Valid);
let output_layer: Array3<f32> = conv_layer.convolve(&input);
let output_free = conv2d(&kernel, &input, Padding::Valid, 1);

println!("Layer: {:?}", output_layer);
println!("Free: {:?}", output_free);


~94K SLoC