Skip to main content

Faster and more memory efficient implementation of the Partial Convolution 2D layer in PyTorch equivalent to the standard NVidia implem.

Project description

Torch Pconv

PyPI version

Faster and more memory efficient implementation of the Partial Convolution 2D layer in PyTorch equivalent to the standard Nvidia implementation.

This implementation has numerous advantages:

  1. It is strictly equivalent in computation to the reference implementation by Nvidia . I made unit tests to assess that all throughout development.
  2. It's commented and more readable
  3. It's faster and more memory efficient, which means you can use more layers on smaller GPUs. It's a good thing considering today's GPU prices.
  4. It's a PyPI-published library. You can pip install it instead of copy/pasting source code, and get the benefit of ( free) bugfixes when someone notice a bug in the implementation.

Total memory cost (in bytes)

Getting started

pip3 install torch_pconv

Usage

import torch
from torch_pconv import PConv2d

images = torch.rand(32, 3, 256, 256)
masks = (torch.rand(32, 256, 256) > 0.5).to(torch.float32)

pconv = PConv2d(
    in_channels=3,
    out_channels=64,
    kernel_size=7,
    stride=1,
    padding=2,
    dilation=2,
    bias=True
)

output, shrunk_masks = pconv(images, masks)

Performance improvement

Test

You can find the reference implementation by Nvidia here .

I tested their implementation vs mine one the following configuration:

ParameterValue
in_channels64
out_channels128
kernel_size9
stride1
padding3
biasTrue
input height/width256

The goal here was to produce the most computationally expensive partial convolution operator so that the performance difference is displayed better.

I compute both the forward and the backward pass, in case one consumes more memory than the other.

Results

Total memory cost (in bytes)

torch_pconv
Nvidia® (Guilin)
Forward only813 466 6244 228 120 576
Backward only1 588 201 4801 588 201 480
Forward + Backward2 405 797 6406 084 757 512

Development

To install the latest version from Github, run:

git clone git@github.com:DesignStripe/torch_pconv.git torch_pconv
cd torch_pconv
pip3 install -U .

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torch_pconv-0.1.1.tar.gz (7.0 kB view hashes)

Uploaded Source

Built Distribution

torch_pconv-0.1.1-py3-none-any.whl (7.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page