ND Convolution for PyTorch
Project description
torchnd
N-dimensional convolution for PyTorch that works with any number of spatial dimensions, supporting groups, dilation, transposed convolution, complex numbers, and arbitrary dimension layouts.
Installation
pip install torchnd
Usage
2D convolution:
import torch
from torchnd import conv_nd
x = torch.randn(2, 4, 16, 16)
weight = torch.randn(8, 4, 3, 3)
out = conv_nd(x, weight, dim=(-2, -1), padding=1, stride=2)
4D convolution (batch × time × height × width):
x = torch.randn(2, 4, 10, 16, 16)
weight = torch.randn(8, 4, 3, 3, 3)
out = conv_nd(x, weight, dim=(-3, -2, -1), padding=1)
Channel-last layout:
x = torch.randn(2, 16, 16, 4)
weight = torch.randn(8, 4, 3, 3)
out = conv_nd(x, weight, dim=(1, 2), channel_dim=-1, padding=1)
Transposed convolution:
x = torch.randn(2, 4, 8, 8)
weight = torch.randn(4, 8, 3, 3)
out = conv_nd(x, weight, dim=(-2, -1), padding=1, stride=2, transposed=True)
Complex numbers:
x = torch.randn(2, 4, 16, 16, dtype=torch.complex64)
weight = torch.randn(8, 4, 3, 3, dtype=torch.complex64)
out = conv_nd(x, weight, dim=(-2, -1), padding=1)
Asymmetric parameters per dimension:
x = torch.randn(2, 4, 16, 16)
weight = torch.randn(8, 4, 3, 3)
out = conv_nd(
x, weight,
dim=(-2, -1),
stride=(2, 1),
padding=(1, 2),
dilation=(1, 2)
)
Modules:
from torchnd import ConvNd, ConvTransposeNd
conv = ConvNd(4, 8, 3, dim=(-2, -1), padding=1)
out = conv(x)
Padding:
from torchnd import pad_nd, adjoint_pad_nd
# Pad or crop arbitrary dimensions
padded = pad_nd(x, pad=(1, 1, 2, 2), dims=(-2, -1), mode="reflect")
cropped = pad_nd(x, pad=(-1, -1), dims=(0,)) # Negative values crop
# Adjoint of padding (unpads)
unpadded = adjoint_pad_nd(padded, pad=(1, 1, 2, 2), dims=(-2, -1))
Adjoint operators:
from torchnd import ConvNd
conv = ConvNd(4, 8, 3, dim=(-2, -1), padding=1, bias=False)
x = torch.randn(2, 4, 16, 16)
y = torch.randn_like(conv(x))
# Adjoint satisfies: <conv(x), y> = <x, conv.adjoint(y)>
# For ConvNd: adjoint is transposed convolution with conjugated weights
adj_y = conv.adjoint(y, input_shape=(16, 16))
Functions
conv_nd
N-dimensional convolution with flexible dimension specification. Supports arbitrary spatial dimensions via recursive decomposition when native PyTorch implementations (1D, 2D, 3D) are unavailable. Handles complex numbers, grouped convolution, dilation, stride, and transposed convolution.
The dim parameter specifies which dimensions are spatial. The channel_dim parameter specifies the channel dimension. This allows for arbitrary tensor layouts (e.g., channel-last).
pad_nd
N-dimensional padding and cropping with flexible dimension specification. Supports arbitrary dimension layouts, not limited to the last N dimensions. Negative padding values crop the tensor.
Modes: constant, reflect, replicate, circular. Each dimension can use a different mode. For constant mode, negative padding crops symmetrically. For non-constant modes, negative padding is not supported.
The dims parameter specifies which dimensions to pad. If None, pads the last N dimensions where N = len(pad) // 2. Padding is specified as pairs (left, right) per dimension.
pad_or_crop_to_size
Pad or crop a tensor to a target size, centering the original content. Combines padding and cropping in a single operation. Useful for resizing tensors to a fixed shape while keeping the content centered.
adjoint_pad_nd
Computes the adjoint of pad_nd via autograd. For a padding operator P, the adjoint P* satisfies the inner product identity: <Px, y> = <x, P*y> for all x, y. The adjoint unpads the tensor, summing contributions from padded regions back into the original shape. For constant/zeros mode, the adjoint is equivalent to cropping. For other modes, it correctly handles the adjoint of the boundary extension.
Adjoint convolution
The ConvNd and ConvTransposeNd modules provide adjoint() methods. For ConvNd, the adjoint is the transposed convolution with conjugated weights (for complex) or identical weights (for real). For ConvTransposeNd, the adjoint is the forward convolution with conjugated weights.
The adjoint satisfies the inner product identity: <Ax, y> = <x, A*y> where A is the convolution operator and A* is its adjoint. This is verified via dot product tests. The adjoint is undefined when bias is present.
Implementation
For dimensions beyond 3D, conv_nd recursively decomposes the convolution into lower-dimensional operations. A 4D convolution becomes a sum of 3D convolutions applied to strided slices of the input.
Complex convolution is handled by decomposing into real operations: (a+bi)*(c+di) = (ac-bd) + (ad+bc)i.
The implementation uses native PyTorch operations when possible (1D, 2D, 3D) and falls back to recursion only for higher dimensions, minimizing memory overhead with strided views.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file torchnd-0.1.1.tar.gz.
File metadata
- Download URL: torchnd-0.1.1.tar.gz
- Upload date:
- Size: 21.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f8caf64f8aa3cfc9e586cc9c62da8717c6a093001131f08b582e1912fcc40401
|
|
| MD5 |
963de5494e746fdfc2ce5ac8c48d37dd
|
|
| BLAKE2b-256 |
85c56bfeff08dd5882517d40056eb4d85632347e1b1328413ab73a96c3837879
|
Provenance
The following attestation bundles were made for torchnd-0.1.1.tar.gz:
Publisher:
release.yml on fzimmermann89/torchnd
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torchnd-0.1.1.tar.gz -
Subject digest:
f8caf64f8aa3cfc9e586cc9c62da8717c6a093001131f08b582e1912fcc40401 - Sigstore transparency entry: 1328818589
- Sigstore integration time:
-
Permalink:
fzimmermann89/torchnd@d3af7822c4a30186685f4f103cfeb8c67d2c9bc7 -
Branch / Tag:
refs/tags/0.1.1 - Owner: https://github.com/fzimmermann89
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@d3af7822c4a30186685f4f103cfeb8c67d2c9bc7 -
Trigger Event:
release
-
Statement type:
File details
Details for the file torchnd-0.1.1-py3-none-any.whl.
File metadata
- Download URL: torchnd-0.1.1-py3-none-any.whl
- Upload date:
- Size: 12.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9bb76e44c62496dd664da7dcdc2805740c0fe15a81e094ddad271a1a5943f2fb
|
|
| MD5 |
1009b3f7b48c8f870cdc3607330abd16
|
|
| BLAKE2b-256 |
d4bab135c4cfa89e1cc32669f70adcf41fea213798d96833556e499281fade21
|
Provenance
The following attestation bundles were made for torchnd-0.1.1-py3-none-any.whl:
Publisher:
release.yml on fzimmermann89/torchnd
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torchnd-0.1.1-py3-none-any.whl -
Subject digest:
9bb76e44c62496dd664da7dcdc2805740c0fe15a81e094ddad271a1a5943f2fb - Sigstore transparency entry: 1328818615
- Sigstore integration time:
-
Permalink:
fzimmermann89/torchnd@d3af7822c4a30186685f4f103cfeb8c67d2c9bc7 -
Branch / Tag:
refs/tags/0.1.1 - Owner: https://github.com/fzimmermann89
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@d3af7822c4a30186685f4f103cfeb8c67d2c9bc7 -
Trigger Event:
release
-
Statement type: