This release is a pre-release and may not be stable for production use.
TorchFlint: Advanced Tensor Operations and State Management for PyTorch
TorchFlint is a collection of advanced utility functions designed for tensor manipulation within the PyTorch ecosystem. It provides granular control over high-dimensional data processing, bridging the gap between high-level neural modules and low-level tensor operations.
✨ Key Features
1. Transparent Buffer Management
Simplifies the registration of non-parameter states in nn.Module using a Pythonic assignment syntax.
torchflint.buffer(tensor, persistent): Wraps a tensor so it can be assigned directly inside a module. It automatically handles device movement andstate_dictsaving without usingtorch.nn.Module.register_buffer.- Containers: Includes
BufferObject,BufferListandBufferDictfor organized state management.
2. Patchwork
A core module for handling N-dimensional patches, serving as the foundation for convolution and pooling operations.
- High Performance: Includes
torchflint.patchwork.unfold_space,torchflint.patchwork.fold_space, andtorchflint.patchwork.fold_stack. Notably, the speed oftorchflint.patchwork.fold_stackis close to the officialtorch.nn.functional.fold(only support images less than 2D) in many scenarios. - N-Dimensional Support: Works seamlessly on 1D, 2D, and 3D+ spatial data.
- Functional Convolution & Pooling: Built upon the
torchflint.patchworkmodule, many functions expose the intermediate steps of convolution and pooling, liketorchflint.patchwork.conv_patches,torchflint.patchwork.masked_conv,torchflint.patchwork.pool,torchflint.patchwork.max_pool, andtorchflint.patchwork.masked_avg_pool. However, although many of them works well in some tests, but limited (masked version was only tested for their forward process), they will be tested more rigorously in the future.
3. Convenient Tensor Utilities
A suite of tools in torchflint.functional.
🛠️ Installation
pip install torchflint
Requirement: Python 3.7+ and PyTorch >= 1.6.0.
📚 Documentation
Complete API documentation:
https://torchflint.readthedocs.io/
🚀 Usage Examples
Using Buffers
Register buffers via simple assignment:
import torch
import torchflint
class MyModule(torch.nn.Module):
def __init__(self):
super().__init__()
# Registers a persistent buffer named 'buffer'
self.buffer = torchflint.buffer(torch.zeros(10), persistent=True)
model = MyModule()
Patch-based Operations
Using the high-performance patch functions:
from torchflint import patchwork
tensor = torch.randn(32, 3, 32, 32)
# Extract patches
patches = patchwork.unfold_space(tensor, kernel_size=3, stride=2, padding=1)
# Reconstruct
cumulative_output = patchwork.fold_stack(patches, stride=2)
average_output = patchwork.fold_space(patches, stride=2)
Convolution (Standard & Masked)
Perform convolutions, optionally applying a mask to handle sparse data or boundary validity.
from torchflint import patchwork
tensor = torch.randn(1, 3, 32, 32)
weight = torch.randn(16, 3, 3, 3) # Standard weight shape: [Out, In, K, K]
# Standard Convolution
output = patchwork.conv(tensor, weight, stride=2, padding=1)
# Masked Convolution
# Apply convolution only where input_mask is valid (1)
input_mask = torch.randn(1, 1, 32, 32) > 0.5
masked_output = patchwork.masked_conv(
tensor, weight,
stride=2, padding=1,
input_mask=input_mask
)
Pooling (Standard & Masked)
Apply pooling operations. Masked pooling allows accurate statistical reduction by ignoring invalid regions instead of padding with zeros or infinity.
from torchflint import patchwork
tensor = torch.randn(1, 3, 32, 32)
# Standard Max Pooling
output = patchwork.max_pool(tensor, kernel_size=2, stride=2)
# Masked Max Pooling
# Regions marked as 0 in the mask are ignored during max calculation
# (Useful for non-rectangular images or padding handling)
mask = torch.ones_like(tensor)
mask[..., 0:5, 0:5] = 0 # Invalidate top-left corner
masked_output = patchwork.masked_max_pool(
tensor, kernel_size=2, stride=2,
mask=mask.bool()
)
📄 License
This project is licensed under the MIT License.
Citation
@misc{torchflint,
author = {Juanhua Zhang},
title = {TorchFlint: Advanced Tensor Operations and State Management for PyTorch},
year = {2024},
publisher = {GitHub},
journal = {GitHub repository},
howpublished = {\url{https://github.com/Caewinix/torchflint}}
}
Metadata
Release files for torchflint 0.0.1b16.post1.dev3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| torchflint-0.0.1b16.post1.dev3.tar.gz | 40.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| torchflint-0.0.1b16.post1.dev3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 83.9 kB
Release files / torchflint-0.0.1b16.post1.dev3.tar.gz
| Download URL | torchflint-0.0.1b16.post1.dev3.tar.gz |
|---|---|
| Size | 40.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
05366ef526331d3d33ff6207c70215d63d6d3335aa150b3e289e55936470bd2f
|
|
BLAKE2b-256 checksum How to use checksums |
583e3e8dfbc4bbed404d2498d4b074e9037f831e23e947762e2208676dde2d4d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.18
|
Release files / torchflint-0.0.1b16.post1.dev3-py3-none-any.whl
| Download URL | torchflint-0.0.1b16.post1.dev3-py3-none-any.whl |
|---|---|
| Size | 43.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a8f2c881c6163367d07ce176e02f71cea90712932e62793acc902533f9b1deb1
|
|
BLAKE2b-256 checksum How to use checksums |
2ec25126c8462c8e1a37083a699c8cfa86c960e6541c5d4b3bff5e37a37fff85
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.18
|