Skip to main content

torchlogix_logo

PyPI version Build Status Documentation MIT License DOI DOI

torchlogix is a PyTorch-based library for training and inference of logic neural networks. These solve machine learning tasks by learning combinations of boolean logic expressions. As the choice of boolean expressions is conventionally non-differentiable, relaxations are applied to allow training with gradient-based methods. The final model can be discretized again, resulting in a fully boolean expression with extremely efficient inference, e.g., beyond a million images of MNIST per second on CPU.

Note: torchlogix is based on the difflogic package (https://github.com/Felix-Petersen/difflogic/), and extends it by new concepts such as additional layer types, compact parametrizations, higher-dimensional logic blocks, learnable connections and binarization as described in "WARP Logic Neural Networks" (Paper @ ArXiv). It also implements a graph-based intermediate representation (IR) for efficient compilation to different targets (currently FPGA & CPU).

Installation

pip install torchlogix                 # basic
pip install "torchlogix[dev]"          # with dev tools

The following software stacks have validated performance: python3.12 / python3.13, cuda12.4 / cuda13.0, torch2.6 / torch2.9.

Quickstart

torchlogix provides learnable logic layers with torch.nn-like API. For example, a very simple convolutional model for MNIST can be defined like so:

import torch
from torchlogix.layers import LogicDense, LogicConv2d, OrPooling2d, GroupSum, FixedBinarization

model = torch.nn.Sequential(
    # Every pixel is False (=0) or True (>0). Standard practice on MNIST
    FixedBinarization(thresholds=[0.0]),
    # Convolution w/ 16 kernels - 4 inputs each, randomly drawn from a 3x3 receptive field
    LogicConv2d(in_dim=28, channels=1, num_kernels=16, tree_depth=2, receptive_field_size=3),
    # Reduce dimensionality with pooling operation
    OrPooling2d(kernel_size=2, stride=2, padding=0),
    torch.nn.Flatten(),
    # Two randomly connected dense layers w/ 4000 neurons
    LogicDense(16*13*13, 4_000),
    LogicDense(4_000, 4_000),
    # Output 10 logits via group sum (scaled by 1/8 for smoothness)
    GroupSum(k=10, tau=8)
)

Like ordinary PyTorch neural networks, this model may be trained, e.g., with torch.nn.CrossEntropyLoss. The Adam optimizer with a learning rate of 0.01 works well. Every layer and hence the entire model can be switched between the relaxed trainable and discrete, fully boolean version with the standard model.train() / model.eval() commands. Furthermore, there is a dedicated model.set_export_mode(), which expresses the forward path as pure boolean- and indexing operations. This can be represented as a fully unrolled combinational Circuit, which can be compiled for fast inference:

from torchlogix import Circuit
circuit = Circuit.from_model(model, input_shape=(1, 28, 28))
circuit.compile()
preds = circuit(X_np, use_compiled=True)  # ~6 ms for 100k images on my laptop

The graph-based IR of a Circuit can be simplified and emit C and Verilog code directly:

circuit.simplify()  # removes dead code, folds constants, does dedup...
circuit.get_c_code()
circuit.get_verilog_code()

The full training- and evaluation of the model above is demonstrated in the example notebook examples/mnist_example.ipynb.

torchlogix is integrated with the 3rd party tool alkaid for more advanced FPGA compiling. For more details, see docs/guides/hardware_deployment.md.

Documentation

More thorough documentation is available here, including an API Reference. Some quick links:

Experiments

Various experiments can be run using the script experiments/train.py. For example, the medium-sized convolutional model on CIFAR-10 from the paper "Convolutional Differentiable Logic Gate Networks" (Paper @ ArXiv), can be trained like so:

python train.py --dataset cifar-10 -a ClgnCifar10Medium --connections-init-method random-unique -lr 0.02 -wd 0.002 --device cuda --compile-model

This achieves 70% discrete test accurcay within 30 minutes on an A100, which can be increased further with data augmentation, and knowledge distillation but details of the training procedure are beyond the scope of this package.

Citation

If you use torchlogix in your research, please cite:

@software{torchlogix2026,
  author = {Gerlach, Lino and Gerlach, Thore and Kauffman, Elliott and Våge, Liv},
  title = {torchlogix},
  year = {2026},
  doi = {10.5281/zenodo.18800427}
}

License

torchlogix is released under the MIT license. See LICENSE for additional details about it.

Download files

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

Source Distribution

torchlogix-0.2.1.tar.gz (358.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

torchlogix-0.2.1-py3-none-any.whl (68.0 kB view details)

Uploaded Python 3

File details

Details for the file torchlogix-0.2.1.tar.gz.

File metadata

  • Download URL: torchlogix-0.2.1.tar.gz
  • Upload date:
  • Size: 358.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for torchlogix-0.2.1.tar.gz
Algorithm Hash digest
SHA256 f03a6e6e6da1d2548dd8a4ebe678e2403554e48e46e34924a143f4c70a596506
MD5 b5dd7fe71ef7cece357a8fad44012be3
BLAKE2b-256 358ef25e6d8302a33b36521e2423b341f45c2f9e1d7ea809d7424a31c2504df1

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchlogix-0.2.1.tar.gz:

Publisher: publish.yml on ligerlac/torchlogix

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file torchlogix-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: torchlogix-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 68.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for torchlogix-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f5cef442473ca0ce158e4f5d1ba449c4d2af7a2d34089fe5d9725806adc39d33
MD5 58d4e66bf22d65f4af187dcbef37001a
BLAKE2b-256 86e9dbb7da58b50defdb4813b7c432f59152b91639dc9de57dc4633ff5cb2871

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchlogix-0.2.1-py3-none-any.whl:

Publisher: publish.yml on ligerlac/torchlogix

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page