WorksComputer Science
torchtsetlin: Fast, Interpretable Tsetlin Machines for PyTorch
Abstract
The Tsetlin machine learns propositional rules with teams of Tsetlin automata that are driven by reward and penalty rather than gradients, so its models are interpretable by construction and cheap to evaluate. Its software, however, consists of C and CUDA kernels behind scikit-learn-style wrappers that sit outside the deep learning ecosystem. torchtsetlin implements the Tsetlin machine family as ordinary PyTorch modules: vanilla and weighted, coalesced, regression, convolutional over sequences, images and volumes, and dense per-pixel segmentation. The automata are a tensor buffer, so a model moves with .to(device), is saved with state_dict() and learns from a mini-batch with one update(x,y) call. Its core technique is a batched formulation of Type I and Type II feedback, in which automaton transitions become event counts realised as binomial draws, with one commit per mini-batch. On ten benchmarks taken from the existing libraries, it matches their accuracy, and on all but the two smallest it trains 2.4-10.9X faster than PyTsetlinMachineCUDA and 10.3-81.2X faster than tmu on the same GPU. The package is pure Python, MIT-licensed and on PyPI; every number in this paper is reproduced by one script. Code: https://github.com/vlbthambawita/torchtsetlin; package: https://pypi.org/project/torchtsetlin/.
Introduction
Multimedia systems increasingly run learned models where energy, latency and accountability are tight: keyword spotting on always-on audio front-ends, analysis of medical images and physiological signals, and classification of text and images at the edge. Deep networks deliver accuracy there, but their decisions are hard to audit and their floating-point inference is costly on small devices. The Tsetlin machine [TM; 10] offers a different trade-off. It represents each class by conjunctive clauses over Boolean features and their negations (Figure 1), so a trained model is a readable rule set [27], and inference reduces to bitwise AND, OR and counting, which has led to TM chips and edge accelerators down to microwatt keyword spotting [19, 22, 30]. Convolutional TMs [16] learn rules over image patches, and TMs have been applied to medical text [4], ECG beats [33] and image segmentation [20].

Figure 1. A Tsetlin machine at a glance: each row of automata defines a clause, matching clauses vote with their polarity, and feedback moves each automaton’s state across the include/exclude boundary between N and N+1 (inset).
Each (clause, literal) pair is a Tsetlin automaton [23, 29], a counter that decides whether the literal is included, and learning is reward and penalty instead of gradients. The family now includes weighted [26], regression [3] and coalesced [8] machines, with regularisers such as clause-size budgets [1] and drop clause [28].
The software has not kept pace with this reach. The reference libraries implement each variant as a C or CUDA kernel behind a fit/predict interface [7, 11, 13, 15], while multimedia pipelines (decoding, augmentation, feature extraction, GPU data loading and the neural baselines a TM is compared with) are built on PyTorch [25]. Bridging the two leaves four gaps: (i) the learned state sits in a private buffer that tensor tools cannot inspect, mask or save; (ii) the device is fixed by the imported package, so CPU and GPU runs follow different code paths; (iii) Booleanized data must leave the framework as NumPy arrays; and (iv) a new feedback rule, output layer or input structure means a new kernel. A fifth gap is evidence: each library reports results on its own settings and hardware, and the only cross-library timing we know of covers one dataset, MNIST, and omits both the accuracy and the hyperparameters [9].
This paper presents torchtsetlin,12 an open-source library that closes these gaps, and makes four contributions:
TMs as PyTorch modules. Vanilla, weighted, coalesced and regression machines share one learning loop and one tensor state, and each has convolutional variants over sequences (1-D), images (2-D) and volumes such as video clips or medical scans (3-D), plus dense per-pixel segmentation and a graph machine (§ 2–§ 4).
Batched feedback on the GPU. Automaton transitions of a mini-batch become event counts drawn as binomials and committed once, with fused GPU kernels for the commit and for bit-packed convolution (§ 3).
A pre-registered, reproducible comparison of six TM libraries on ten benchmarks that the existing libraries themselves publish, with no tuning and final-epoch accuracy (§ 6–§ 6.1); torchtsetlin matches their accuracy, and on all but the two smallest benchmarks trains 2.4–10.9× faster than the fastest existing GPU library on the same GPU.
An open artifact: MIT-licensed, on PyPI, documented, tested in CI, with one script that rebuilds every number in this paper (§ 7).
Design
State as tensors. A machine with clauses over features holds the automata of Figure 1 as an integer buffer ta_state of shape , with memory depth . The counters are buffers, not parameters, so to(), state_dict() and lazy shape inference (as in LazyLinear) work unchanged. Learning is update(x, y), the analogue of loss.backward(); optimizer.step(), which returns the pre-update vote sums for logging; the empty-clause convention (output 1 while learning, 0 while predicting) follows train()/eval().
One learning loop, many input structures. The package has four layers (Figure 2). functional holds all learning maths as stateless tensor operations, and TsetlinMachineBase runs the generic loop. A concrete model supplies only an output layer and a feedback policy through three hooks. A new input structure overrides encoding, clause evaluation and feedback counting: one mixin turns every flat model into a convolutional one, in which a clause fires if it matches any patch and positions are thermometer-encoded [16]; a 3-D subclass changes only the patch geometry; dense segmentation folds pixels into the batch axis so that each is a flat example; and the graph machine [14] treats nodes as patches and passes messages between clause layers along the edges.

Figure 2. torchtsetlin layer by layer. Models combine an output layer with an input structure; all of them share the learning loop of TsetlinMachineBase, whose update runs on stateless tensor maths, compiled CPU kernels or fused Triton GPU kernels, on top of PyTorch. Each layer builds only on the one below it. The legend names the icons, in the order of the boxes.
Batched Learning
The classical TM learns online: an example is evaluated, its vote sums decide which clauses receive feedback, and the automata change before the next example is seen [10]. On a GPU this loop leaves the device idle, because the work per example is small and every step waits for the previous one. torchtsetlin learns from a mini-batch of examples in one call, update(x, y), in four stages that all read the automata as they were at the start of the call; only the last stage changes them.
1. Evaluate. The batch is Booleanised into literals , and all clauses are evaluated at once into clause outputs and class vote sums . While learning, a clause with no included literal outputs 1. update returns these pre-update vote sums, so training accuracy is logged without a second pass.
2. Select feedback. Each example is handled independently, as in the classical algorithm. For example with label , one negative class is drawn (uniformly, or focused by its vote sum). Each clause of the target class is selected with probability , each clause of the negative class with , and the clauses of the other classes get no feedback. Selected positive clauses of the target class and negative clauses of the negative class receive Type I feedback, the others Type II. Splitting by gives , the Type I and Type II selections of firing clauses, and , the Type I selections of non-firing clauses (Type II acts on firing clauses only).
3. Count events. Every elementary transition of the classical update is a Bernoulli trial on one automaton, so the batch reduces to the number of trials per automaton, which are matrix products:
Here and count Type Ia events on true and false literals of firing clauses, Type II events on false literals, and Type Ib events per clause, which act on all its literals alike. A convolutional clause fires if any patch matches; each of its events uses the literals of one matching patch, drawn uniformly at random per example and clause, separately for Type I and Type II, so holds the drawn patch for each (example, clause) pair. With weighted clauses, the weight change of a clause is its number of Type I minus Type II events while firing, .
4. Commit. Trials on one automaton share one probability, so their sum is binomial, and the whole batch is applied in one draw per automaton:
with specificity and , or 1 with boosted true-positive feedback. Type II stops at the include boundary because, online, a clause stops matching an example once it includes one of the example’s false literals. States are clamped to and weights to at least 1. The clause-size budget is soft, as in tmu: the Type Ia events of a firing clause above the budget are counted as Type Ib, so it is forgotten instead of reinforced. The include mask is then refreshed for the next call.
Memory. The largest intermediates have entries ( for patches). When they exceed max_chunk_elements, the batch is split into chunks that add into the same counters, so chunking bounds memory without changing the update, which is still committed once.
What batching changes. At the four stages are the classical update term by term. For , every example of a batch sees the automata and vote sums of its start. An example therefore does not see the inclusions caused by earlier examples of the same batch, and the margin , which online stops feedback to a class once its vote sum reaches , reacts only once per batch, so up to pushes can land on one automaton in one commit. This is the approximation of mini-batch SGD relative to SGD [6]: small batches follow the online algorithm, large ones overshoot. Batch size is therefore a hyperparameter, measured in § 6.4, and feedback_mode="sequential" keeps the exact algorithm: it commits each example in turn inside the same call, touching only the clauses that example selected.
GPU fast path. On CUDA, a batched update that fits in one chunk records its feedback events (the and of stage 3) instead of counts, and one Triton kernel runs stages 3 and 4 for a tile of (clause, literal) pairs: it computes the counts, samples the binomials, and writes the new state and a bit-packed include mask. Convolutional clauses are evaluated on bit-packed literals, with the OR over patches kept in registers. An update never synchronises with the host, so its shapes are captured as CUDA graphs on their third use and replayed; this removes the launch overhead of small models and of the sequential mode, which runs in blocks of 64 examples. Every fused path falls back to plain PyTorch operations, which are also the CPU path.
import torchtsetlin as tt x_train, y_train = tt.data.make_noisy_xor(5000, noise=0.4) # Boolean features, int labels x_test, y_test = tt.data.make_noisy_xor(2000, noise=0.0, seed=1) model = tt.TsetlinMachine(n_features=12, n_classes=2, n_clauses=20, T=15, s=3.9).to("cuda") trainer = tt.Trainer(model, batch_size=10, callbacks=[tt.EarlyStopping(patience=20)]) trainer.fit((x_train, y_train), epochs=100, val_data=(x_test, y_test)) print(trainer.evaluate((x_test, y_test))) # {'accuracy': ...} print(model.rules()[:2]) # ['IF x0 AND NOT x1 THEN 1', 'IF x1 AND NOT x0 THEN 1']
Functionality and Usage
Listing 1 shows the complete workflow on Noisy XOR [10]: with 40% label noise the machine recovers and prints the generating rule. Version 0.7.3 ships TsetlinMachine (optional clause weights [26]), CoalescedTsetlinMachine (multi-class or multi-label) and RegressionTsetlinMachine, convolutional 1-D, 2-D and 3-D variants of each, dense segmentation machines and a graph machine [14]. Booleanisation encoders (thermometer, hypervector [17] and others) are nn.Modules that run on the model’s device and carry feature names into the rules (worst_perimeter>=105.9 rather than x37). A Trainer with callbacks accepts tensors and DataLoaders, and the interpretation tools extract rules and global, local and per-pixel importance [5]. Figure 3 shows these tools on three small models trained with the library’s own loaders: per-class literal importance of a flat machine on MNIST, the clauses of a convolutional machine read as image patches, and a dense machine segmenting a synthetic street scene, with the per-pixel vote map of one class.
Figure 3. Output of torchtsetlin’s interpretation and plotting tools (interpret, viz). (a) Global importance of each pixel literal for each MNIST class in a flat machine. (b) Heavily weighted clauses of a convolutional MNIST machine, drawn as patch templates with their weights. (c) A dense segmentation machine on a synthetic street scene: input, ground truth, prediction and the vote sums for building (red: for, blue: against). One seed, five to eight epochs each (code/fig_viz.py).
Comparison with Existing Software
Table 1 compares torchtsetlin with the maintained TM packages. pyTsetlinMachine [13], its OpenMP version pyTsetlinMachineParallel [12] and PyTsetlinMachineCUDA [11] implement one class per variant in C and CUDA; tmu [15] unifies them over reusable clause and weight banks with CPU and CUDA backends; green-tsetlin [7, 9] targets efficient CPU training and deployment. Every GPU path among them is a hand-written PyCUDA kernel. All keep the automata in a native buffer; torchtsetlin alone builds on a general-purpose tensor library. Below we call them pyTM, pyTM-Par, pyTM-CUDA, tmu and GT.
| pyTM | pyTM-CUDA | tmu | GT | torchtsetlin | |
| Backend | C | PyCUDA | C / PyCUDA | C++ | PyTorch |
| CPU and GPU, one class | ✗ | ✗ | ✗ | ✗ | ✓ |
| Conv. 1-D / 2-D / 3-D | ✓/✗ | ✓/✗ | ✓/✗ | ✗/✗ | ✓✓ |
| Regression / coalesced | ✓/✗ | ✓/✗ | ✓/✗ | ✗/✗ | ✓ |
| Dense segmentation | ✗ | ✗ | ✗ | ✗ | ✓ |
| Graphs (message passing) | ✗ | ✗ | ✗ | ✗ | ✓ |
| Tensor state, state_dict | ✗ | ✗ | ✗ | ✗ | ✓ |
| New variant without C/CUDA | ✗ | ✗ | ○ | ✗ | ✓ |
Table 1. Features of TM software (✓ yes, ○ partial, ✗ no). GT’s 2-D convolution exists but fails at training through its public API. Graph machines otherwise ship in a separate CUDA library [14].
Benchmark Protocol
To avoid benchmarks chosen in torchtsetlin’s favour, the comparison uses only experiments that the existing libraries already publish, in their repositories or in the papers that introduced them (Table 2). Each dataset is Booleanised once and every library reads the same bits; hyperparameters are the published ones, with no tuning, and accuracy is read at the final epoch, never at the best one. These rules govern every comparison between libraries. Batched feedback is a learning parameter that only torchtsetlin has, so Section 7.4 adds two studies of the batch size in which no other library takes part; they are marked there and follow their own protocol. Metrics are those the libraries already report: accuracy, macro F1, precision and recall (RMSE and MAE for B10); seconds per training epoch (median of epochs 2–E; epoch 1 includes kernel compilation and is recorded separately); seconds to predict the test set; peak host and GPU memory; and model size.
| ID | Dataset (task) | Booleanisation; model (, , ) | Epochs × runs | Source of the experiment |
| B1 | Noisy XOR | 12 bits, 40% label noise; TM (10, 15, 3.9) | 200 × 5 | pyTM, pyTM-Par, pyTM-CUDA demos; Granmo [10 Tab. 15] |
| B2 | 2-D Noisy XOR | 4×4 images; conv. TM (40, 60, 3.9), patch 2×2 | 5000 × 5 | pyTM, pyTM-Par demos; Granmo et al. [16] |
| B3 | Breast cancer [31] | 10 bits/feature; TM (800, 40, 5.0) | 25 × 100 splits | pyTM, pyTM-Par demos |
| B4 | MNIST [18] | pixel > 75; TM (2000, 50, 10) | 250 × 5 | pyTM, pyTM-Par demos |
| B5 | MNIST | pixel > 75; w-TM (2000, 5000, 10) | 100 × 5 | pyTM, pyTM-Par, pyTM-CUDA demos; tmu example; Phoulady et al. [26] |
| B6 | MNIST, timing | pixel > 75; , , , budget 20 | 5 × 3 | Glimsdal et al. [9 Tab. I] (cross-library); Abeyrathna et al. [2] |
| B7 | MNIST | pixel ≥ 75; conv. w-TM (2000, 5000, 5.0), patch 10×10 | 30 × 5 | pyTM, pyTM-Par, pyTM-CUDA demos; tmu example |
| B8 | Fashion-MNIST [32] | adaptive Gaussian threshold; conv. w-TM (2000, 5000, 5.0), 10×10 | 30 × 5 | pyTM, pyTM-Par, pyTM-CUDA demos; Granmo et al. [16] |
| B9 | IMDb [21] | 5000 words, 1–2-grams, top 5000; w-TM (10000, 8000, 10) | 30 × 5 | pyTM, pyTM-Par, pyTM-CUDA demos; Abeyrathna et al. [2 Tab. 3] |
| B10 | California housing [24] (regression) | 10 bits/feature; w-RTM (1000, 5000, 2.75) | 30 × 25 splits | pyTM, pyTM-Par demos; Abeyrathna et al. [2 Tab. 5] |
Table 2. Benchmarks, all taken from existing TM libraries or their papers. = clauses per class, = threshold, = specificity, w = weighted clauses. Every library implementing the model runs on the same Booleanised data.
their published time; pyTM-Par is 2.60× slower than its README, on a different CPU.
Results
Accuracy
torchtsetlin learns what the reference libraries learn (Table 3). In batch mode it is within the spread of the other libraries on every benchmark but B4, where it is 97.90% against 97.97–98.21%. It reaches 99.11% on convolutional MNIST (B7), 90.06% on Fashion-MNIST (B8, the highest mean), 88.29% on IMDb (tmu: 88.39%), and the sequential mode has the lowest RMSE on California housing (0.612). Sequential and batched training agree on all four benchmarks where both run.
| Bench | pyTM | Par | CUDA | tmu | GT | seq | batch | b100 |
| B1 | 100.00 | 100.00 | 100.00±0.00 | 100.00±0.00 | 54.94 | 100.00±0.00 | 100.00±0.00 | 100.00±0.00 |
| B2 | 98.62 | 99.97 | 98.82±1.22 | 99.73±0.56 | – | 99.60±0.46 | 99.61±0.27 | 92.47±3.83 |
| B3 | 99.12 | 98.25 | 97.50±1.30 | 97.32±1.25 | 97.37 | 97.49±1.37 | 97.42±1.48 | 96.86±1.74 |
| B4 | 98.02 | 98.08 | 98.21±0.05 | 97.97±0.15 | 98.05 | – | 97.90±0.12 | 97.54±0.13 |
| B5 | 98.12 | 98.07 | 98.11±0.08 | 98.17±0.12 | 10.49 | – | 98.15±0.08 | 98.14±0.06 |
| B7 | 99.12 | 99.12 | 98.94±0.04 | 99.11±0.09 | – | – | 99.11±0.05 | 99.03±0.07 |
| B8 | – | – | 88.37±2.08 | 89.97±0.17 | – | – | 90.06±0.18 | 89.70±0.10 |
| B9 | – | – | 81.27±4.45 | 88.39±0.47 | – | – | 88.29±0.93 | 87.99±0.63 |
| B10 | – | – | 0.622±0.010 | 0.614±0.015 | – | – | 0.612±0.011 | 0.621±0.016 |
Table 3. Test accuracy (%) at the final epoch, mean ± sd over runs; B10: RMSE (lower is better). pyTM, pyTM-Par and GT ran once; – not run or not supported.
Three cells of the other libraries are far below their peers, and we report them as measured. GT ends B1 at 54.94% and collapses on B5 to 10.49% after reaching 97.5% by epoch 6; its random initial automaton states and its coalesced clause pool differ from the other libraries, and its own trainer restores the best epoch by default, which our protocol switches off. pyTM-CUDA oscillates between epochs on IMDb after peaking near 88%, in every seed, so its final-epoch accuracy is 81.27% ± 4.45%.
Training and inference time
Figure 4 summarises where torchtsetlin is faster. On the GPU it trains fastest on every benchmark with more than a few hundred automata (Table 4). Against pyTM-CUDA, b100 is 4.4× faster on MNIST (B4), 2.4× on convolutional MNIST (B7), 10.9× on IMDb (B9) and 4.0× on California housing (B10); batch mode, with batches of 16–32, is 2.0–5.2× faster on B4–B9. On B3–B10, tmu on the GPU is 10.3–81.2× slower than b100. Against the single-threaded pyTM, the usual reference of the TM literature, a convolutional MNIST epoch is 88.8× faster (3.99 s against 354 s). The two tiny models (B1, B2) are launch-bound: there b100 ties pyTM-CUDA and the 16-thread pyTM-Par is fastest overall.
| Bench | pyTM×1 | Par×16 | GTX×16 | tmu×1 | torchtsetlin×16 | CUDA | tmu | seq | batch | b100 |
| B1 | 0.00786 | 0.00657 | 0.417 | 0.445 | – | 0.0136 | 1.13 | 0.112 | 0.0421 | 0.0136 |
| B2 | 0.0118 | 0.00741 | – | 0.226 | – | 0.00884 | 0.579 | 0.102 | 0.0439 | 0.0902 |
| B3 | 0.0337 | 0.00643 | 0.0434 | 0.0766 | – | 0.0635 | 0.125 | 0.0163 | 0.00426 | 0.0153 |
| B4 | 9.32 | 2.99 | 46.3 | 32.8 | 69.6 | 3.44 | 19.3 | – | 1.59 | 0.788 |
| B5 | 7.44 | 3.34 | 75.5 | 32.8 | 73.0 | 3.62 | 20.8 | – | 1.63 | 0.802 |
| B7 | 354 | 30.4 | – | 276 | – | 9.53 | 41.2 | – | 4.65 | 3.99 |
| B8 | – | – | – | – | – | 9.58 | 47.8 | – | 4.66 | 3.97 |
| B9 | – | – | – | – | – | 21.2 | 50.9 | – | 4.07 | 1.94 |
| B10 | – | – | – | – | – | 0.124 | 1.45 | 0.429 | 0.0973 | 0.0311 |
Table 4. Training time, seconds per epoch (median of epochs 2–, one exclusive run). Bold: the fastest GPU entry. ×: threads. torchtsetlin on the CPU: three epochs, batch mode.
Inference shows the same pattern more strongly: torchtsetlin predicts the 10,000 MNIST test images in 0.0163 s (B4), 32.1× faster than pyTM-CUDA, and the 25,000 IMDb reviews 70.0× faster. Its convolutional prediction evaluates bit-packed literals and include masks in one kernel; on the tiny B2 images, however, pyTM-CUDA is faster (0.00499 s against 0.0444 s).
| Bench | pyTM×1 | Par×16 | GTX×16 | tmu×1 | torchtsetlin×16 | CUDA | tmu | seq | batch | b100 |
| B1 | 0.00264 | 0.00112 | 0.204 | 0.0871 | – | 0.000570 | 0.233 | 0.000991 | 0.000752 | 0.000660 |
| B2 | 0.0211 | 0.00468 | – | 0.205 | – | 0.00499 | 0.488 | 0.0043 | 0.0444 | 0.0441 |
| B3 | 0.00697 | 0.00316 | 0.00554 | 0.00451 | – | 0.00483 | 0.00666 | 0.000375 | 0.000372 | 0.000352 |
| B4 | 3.42 | 2.41 | 6.51 | 2.35 | 1.59 | 0.523 | 4.62 | – | 0.0163 | 0.0167 |
| B5 | 3.28 | 2.45 | 4.31 | 2.40 | 1.66 | 0.550 | 4.74 | – | 0.0164 | 0.0167 |
| B7 | 289 | 20.9 | – | 227 | – | 1.47 | 22.0 | – | 0.337 | 0.340 |
| B8 | – | – | – | – | – | 1.47 | 23.2 | – | 0.336 | 0.337 |
| B9 | – | – | – | – | – | 11.7 | 15.4 | – | 0.167 | 0.168 |
| B10 | – | – | – | – | – | 0.00220 | 0.161 | 0.000929 | 0.000746 | 0.000728 |
Table 7. Inference time, seconds to predict the whole test set (same runs and columns).
The Green Tsetlin cross-library protocol
B6 copies the only published cross-library TM timing, Table 1 of Glimsdal et al. [9]: build a model, train five epochs and score the test set after each, for 10 to 5000 clauses. Table 5 shows our seconds with the published ones in brackets. pyTM and single-threaded GT take about twice their published time on our older CPU: 2.14× for pyTM and 1.97× for single-threaded GT at 5000 clauses, so the published single-thread numbers reproduce up to the hardware. With four threads GT takes 3.13× its published time against 1.76× for pyTM-Par: its thread scaling is weaker on our machine. pyTM-CUDA reproduces closely (90.2 s against 88 s); tmu takes two to three
| Library | acc. (%) | |||||
| pyTM CPU×1 | 9.07 (4.5) | 20.1 (9) | 95.8 (47) | 174 (84) | 422 (197) | 96.6±0.0 |
| pyTM-Par CPU×4 | 2.25 (1.1) | 5.48 (2) | 37.1 (17) | 97.3 (47) | 262 (149) | 96.9±0.2 |
| pyTM-Par CPU×16 | 1.61 | 2.64 | 11.8 | 36.9 | 115 | 96.8±0.1 |
| GT CPU×1 | 1.04 (0.3) | 3.60 (2) | 35.0 (18) | 70.6 (36) | 177 (90) | 89.6±0.3 |
| GT CPU×4 | 12.8 (4) | 11.9 (5) | 26.0 (11) | 42.9 (18) | 97.1 (31) | 89.1±0.4 |
| GT CPU×16 | – | 29.4 | 36.4 | 45.7 | 79.2 | 89.0±0.6 |
| tmu CPU×1 | 84.6 (29.6) | 101 (33) | 262 (80) | 432 (128) | 916 (266) | 96.3±0.3 |
| tmu GPU | 177 (87.8) | 146 (62) | 150 (70) | 158 (75) | 186 (95) | 96.3±0.2 |
| pyTM-CUDA GPU | 16.3 (14.8) | 27.7 (26) | 31.3 (30) | 37.7 (36) | 90.2 (88) | 90.4±0.0 |
| torchtsetlin GPU [batch] | 3.61 | 3.60 | 3.63 | 3.74 | 5.39 | 96.2±0.4 |
| torchtsetlin GPU [b100] | 1.93 | 1.93 | 2.02 | 2.28 | 2.99 | 96.0±0.3 |
Table 5. B6, the protocol of Glimsdal et al. [9, Tab. I]: seconds to build a model and train 5 epochs with a test-set prediction after each (median of 3 repeats); in brackets the published value (Intel i9-12900K + RTX 3090). Accuracy at , mean ± sd over the repeats.
| ID | Library and check | Acc. (%) | Train s/epoch |
| R1 | pyTM, B5 (README) | 98.16 (98.19) | 8.38 (8.8) |
| R2 | pyTM-Par×10, B5 (README) | 98.16 (98.14) | 4.43 (1.7) |
| R3 | pyTM-CUDA, B8 8000 clauses (README) | 90.70 (90.94) | 42.2 (44.02) |
| R4 | tmu, B5 (example) | 98.22 | 33.1 |
| R6 | torchtsetlin seq, B1 [10 Tab. 15] | 100.00 (99.30) | – |
Table 6. Reproduction checks: each library on its own published configuration, seed 0. In brackets the published value.

Figure 4. Where torchtsetlin is faster: the time of each comparison library divided by that of torchtsetlin (GPU, batch mode) on the same benchmark, for one training epoch and for predicting the test set. Right of 1×, torchtsetlin is faster. pyTM-Par is the fastest CPU library; B8–B10 were run on the GPU libraries only.
times its published time on both devices. torchtsetlin completes the protocol at 5000 clauses in 2.99 s, 30× faster than pyTM-CUDA, 62× faster than tmu on the GPU and 26× faster than GT on 16 threads, and its time grows only from 1.93 to 2.99 s across the sweep. The accuracy column is not a like-for-like comparison: the script gives GT and the coalesced models one pool of clauses but per class to the others, and its pyTM-CUDA model is unweighted and has no clause budget.
Batched versus sequential feedback
Batching trades fidelity to the sequential algorithm for speed. On the four benchmarks where both run, batch mode matches sequential mode, and b100 stays within one standard deviation of it on B1, B3 and B10. B2 is the exception: b100 reaches 92.47% against 99.60% for seq. A diagnostic sweep over batch sizes (five seeds and 5000 epochs each, the first study outside the comparison protocol) shows where fidelity breaks on this 10-clause convolutional task: batches of 16, 32 and 50 reach 99.6, 99.7 and 99.6%, the sequential result, while 100, 200 and 500 reach 92.5, 75.8 and 60.5%. Batch size is therefore a model hyperparameter in torchtsetlin, and the documentation recommends 16–64. Sequential mode stays available and costs 0.102 s per B2 epoch, against 0.0439 s in batch mode.
Figure 5 repeats the question at realistic model sizes, as the second study outside the comparison protocol of § 6. It extends two of the published benchmarks along the one axis that is new in torchtsetlin: the flat model is that of B4 and the convolutional model that of B7, their published setting is the row with 2000 clauses, and a batch of one updates after every example, as the reference libraries do. The 3-D convolutional model reads the eight bit planes of each pixel. The grid covers batch sizes 1 to 1024 and 1000 to 64,000 clauses per class (128 runs). Its protocol differs from the comparison in three ways: training uses 50,000 images and stops when the accuracy on the remaining 10,000 has not improved for 25 epochs, since a fixed number of epochs would give a large batch fewer updates; the reported value is the test accuracy at the best validation epoch; and each cell is one seed, so differences of a few tenths of a point between neighbouring cells are within the noise. Reading the final epoch instead, as the comparison does, changes no convolutional cell by more than 0.19 points and no 3-D cell by more than 0.30; the flat model is less stable at its last epoch (up to 1.72 points lower for batches up to 512, and 57.31–79.10% at 1024), which leaves the conclusions below unchanged.
Figure 5. Batch size against clauses per class on MNIST, for the flat model of B4, the convolutional model of B7 and a 3-D convolutional model over the eight bit planes of each pixel. Left: test accuracy at the best validation epoch. Right: GPU hours to reach that epoch (logarithmic colour scale). One seed per cell, scaled with the clause count, early stopping with a patience of 25 epochs; darker is higher. The accuracy colour scales are clipped at the stated value.
The convolutional model reaches 99.02–99.29% at every clause count for batches up to 64, 98.82–99.10% at 256 and 98.44–98.96% at 1024. The 3-D model degrades faster, from 98.83–99.12% at batches 1 and 32 to 97.65–98.46% at 256 and 96.75–97.94% at 1024. The flat model shows no clear trend up to a batch of 512 (96.02–98.11%) and fails at 1024 (70.95–81.10%).
A large batch does not pay for itself in time either, because it needs more epochs to converge. With 64,000 clauses the convolutional model takes 14.1 GPU hours to its best epoch at batch 1024, against 1.75 at batch 16. At the other end, single-example batches are the slowest way to the same accuracy: the flat model with 64,000 clauses needs 13.9 hours at batch 1 and 1.35 at batch 16. Batches of 16 to 64 are thus both the most accurate and the cheapest setting on all three models, which is the range the documentation recommends.
Memory and model size
The learned state takes the same space as in the C libraries, one byte per automaton (30.0 MiB for B4, against 30.0 MiB for pyTM). torchtsetlin needs 348–1,036 MiB of GPU memory on B1–B8 and 3,378 MiB on IMDb, whose 20,000 clauses over 10,000 literals make the largest model here; the PyCUDA libraries hold 2,104–2,344 MiB on every benchmark. The PyTorch and CUDA runtime costs host memory: torchtsetlin peaks at 1.1–3.6 GB on the GPU runs, against 0.1–2.1 GB for the other libraries.
Limitations
torchtsetlin on the CPU is slow to train: 69.6 s per B4 epoch on 16 threads, against 9.32 s for single-threaded pyTM, because dense tensor operations on small batches cannot use the bit-level tricks of the C kernels. Its CPU inference is the fastest of all CPU libraries (1.59 s on B4). The comparison uses one machine, and the CPU libraries ran once.
Software Quality and Availability
The suite has 257 tests, run on the CPU and on CUDA when present, and pins easily broken behaviour such as empty-clause semantics and the absence of host synchronisation in a CUDA update. CI tests Python 3.9, 3.11 and 3.12 on Linux; releases reach PyPI by Trusted Publishing. The documentation (https://vlbthambawita. github.io/torchtsetlin/) has guides, examples, an API reference, per-version benchmarks and a catalogue of 3,295 results from 55 TM papers. Development is open on GitHub under the MIT licence. experiments/paper_experiments holds the adapters for every library, the frozen configurations and reproduce.sh, which rebuilds every table of this paper from the JSON records of the runs.
Acknowledgments
This work is part of the European project SEARCH, which is supported by the Innovative Health Initiative Joint Undertaking (IHI JU) under grant agreement No. 101172997.
References
- [1]K. Darshana Abeyrathna, Ahmed A. O. Abouzeid, Bimal Bhattarai, Charul Giri, Sondre Glimsdal, Ole-Christoffer Granno, Lei Jiao, Rupsa Saha, Jivethesh Sharma, Svein A. Tunheim, and Xuan Zhang. 2023. Building Concise Logical Patterns by Constraining Tsetlin Machine Clause Size. In Proceedings of the Thirty-Second International Joint Conference on Artificial Intelligence (IJCAI), 3395–3403. doi:10.24963/ijcai.2023/378DOI
- [2]K. Darshana Abeyrathna, Bimal Bhattarai, Morten Goodwin, Saeed Rahimi Gorji, Ole-Christoffer Granno, Lei Jiao, Rupsa Saha, and Rohan K. Yadav. 2021. Massively Parallel and Asynchronous Tsetlin Machine Architecture Supporting Almost Constant-Time Scaling. In Proceedings of the 38th International Conference on Machine Learning (ICML) (Proceedings of Machine Learning Research, Vol. 139). PMLR, 10–20. https://proceedings.mlr.press/v139/abeyrathna21a.htmlarxiv.org/abs/2009.04861
- [3]K. Darshana Abeyrathna, Ole-Christoffer Granno, Xuan Zhang, Lei Jiao, and Morten Goodwin. 2020. The Regression Tsetlin Machine: A Novel Approach to Interpretable Nonlinear Regression. Philosophical Transactions of the Royal Society A: Mathematical, Physical and Engineering Sciences 378, 2164 (2020), 20190165. doi:10.1098/rsta.2019.0165DOI
- [4]Geir Thore Berge, Ole-Christoffer Granno, Tor Oddbjørn Tveit, Morten Goodwin, Lei Jiao, and Bernt Viggo Mathuessen. 2019. Using the Tsetlin Machine to Learn Human-Interpretable Rules for High-Accuracy Text Categorization With Medical Applications. IEEE Access 7 (2019), 115134–115146. doi:10.1109/ACCESS.2019.2935416DOI
- [5]Christian D. Blakely and Ole-Christoffer Granno. 2021. Closed-Form Expressions for Global and Local Interpretation of Tsetlin Machines. In Advances and Trends in Artificial Intelligence. Artificial Intelligence Practices (IEA/AIE 2021) (Lecture Notes in Computer Science, Vol. 12798). Springer, 158–172. doi:10.1007/978-3-030-79457-6_14DOI
- [6]Léon Bottou, Frank E. Curtis, and Jorge Nocedal. 2018. Optimization Methods for Large-Scale Machine Learning. SIAM Rev. 60, 2 (2018), 223–311. doi:10.1137/16M1080173DOI
- [7]Sondre Glimsdal. 2025. green-tsetlin: A Fast Tsetlin Machine Implementation, Intended for Production. https://github.com/ooki/green_tsetlin. Version 1.1.0 on PyPI (August 2025); C++ core with Python bindings; MIT licence.
- [8]Sondre Glimsdal and Ole-Christoffer Granno. 2021. Coalesced Multi-Output Tsetlin Machines with Clause Sharing. arXiv preprint arXiv:2108.07594. doi:10.48550/arXiv.2108.07594DOI
- [9]Sondre Glimsdal, Sebastian Østby, Tobias M. Brambo, and Eirik M. Vinje. 2024. Green Tsetlin: Redefining Efficiency in Tsetlin Machine Frameworks. arXiv preprint arXiv:2405.04212. arXiv:2405.04212 doi:10.48550/arXiv.2405.04212DOI
- [10]Ole-Christoffer Granno. 2018. The Tsetlin Machine – A Game Theoretic Bandit Driven Approach to Optimal Pattern Recognition with Propositional Logic. arXiv preprint arXiv:1804.01508. arXiv:1804.01508 doi:10.48550/arXiv.1804.01508
- [11]Ole-Christoffer Granno. 2021. PyTsetlinMachineCUDA: Massively Parallel and Asynchronous Architecture for Logic-Based AI. https://github.com/cair/PyTsetlinMachineCUDA. Version 0.1.9 on PyPI (June 2021); PyCUDA kernels; MIT licence.
- [12]Ole-Christoffer Granno. 2021. pyTsetlinMachineParallel: Multi-threaded Implementation of the Tsetlin Machine, Convolutional Tsetlin Machine, Regression Tsetlin Machine, and Weighted Tsetlin Machine. https://github.com/cair/pyTsetlinMachineParallel. Version 0.2.1 on PyPI (October 2021); C with OpenMP.
- [13]Ole-Christoffer Granno. 2024. pyTsetlinMachine: Tsetlin Machine, Convolutional, Regression, Weighted and Embedding Tsetlin Machines in C with a Python interface. https://github.com/cair/pyTsetlinMachine. Version 0.6.6 on PyPI (March 2024); MIT licence.
- [14]Ole-Christoffer Granno, Youmna Abdelwahab, Per-Arne Andersen, Karl Audun K. Borgersen, Paul F. A. Clarke, Kunal Dumbre, Ylva Grønningsæter, Vojtech Halenka, Runar Helin, Lei Jiao, Ahmed Khalid, Rebekka Omslandseter, Rupsa Saha, Mayur Shende, and Xuan Zhang. 2025. The Tsetlin Machine Goes Deep: Logical Learning and Reasoning With Graphs. arXiv preprint arXiv:2507.14874. arXiv:2507.14874 doi:10.48550/arXiv.2507.14874
- [15]Ole-Christoffer Granno and Per-Arne Andersen. 2024. TMU: Tsetlin Machine Unified. https://github.com/cair/tmu. Version 0.8.3 on PyPI (March 2024); MIT licence.
- [16]Ole-Christoffer Granno, Sondre Glimsdal, Lei Jiao, Morten Goodwin, Christian W. Omlin, and Geir Thore Berge. 2019. The Convolutional Tsetlin Machine. arXiv preprint arXiv:1905.09688. arXiv:1905.09688 doi:10.48550/arXiv.1905.09688
- [17]Vojtech Halenka, Ahmed K. Kadhim, Paul F. A. Clarke, Bimal Bhattarai, Rupsa Saha, Ole-Christoffer Granno, Lei Jiao, and Per-Arne Andersen. 2024. Exploring Effects of Hyperdimensional Vectors for Tsetlin Machines. In 2024 International Symposium on the Tsetlin Machine (ISTM). IEEE, 1–8. doi:10.1109/ISTM62799.2024.10931546DOI
- [18]Yann LeCun, Léon Bottou, Yoshua Bengio, and Patrick Haffner. 1998. Gradient-Based Learning Applied to Document Recognition. Proc. IEEE 86, 11 (1998), 2278–2324. doi:10.1109/5.726791DOI
- [19]Baizhou Lin, Yuetong Fang, Renjing Xu, Rishad Shafik, and Jagmohan Chauhan. 2025. TsetlinKWS: A 65nm 16.58µW, 0.63mm² State-Driven Convolutional Tsetlin Machine-Based Accelerator for Keyword Spotting. arXiv preprint arXiv:2510.24282. arXiv:2510.24282 doi:10.48550/arXiv.2510.24282
- [20]Yi Liu and K. Darshana Abeyrathna. 2025. CTM-UNet: Integrating Convolutional Tsetlin Machines with U-Net Architecture for Semantic Segmentation. In 2025 International Symposium on the Tsetlin Machine (ISTM). IEEE, 30–36. doi:10.1109/ISTM67926.2025.00013DOI
- [21]Andrew L. Maas, Raymond E. Daly, Peter T. Pham, Dan Huang, Andrew Y. Ng, and Christopher Potts. 2011. Learning Word Vectors for Sentiment Analysis. In Proceedings of the 49th Annual Meeting of the Association for Computational Linguistics: Human Language Technologies. 142–150.
- [22]Sidharth Maheshwari, Yousif Rahman, Rishad Shafik, Alex Yakovlev, Ashur Rafiev, Lei Jiao, and Ole-Christoffer Granno. 2023. REDRESS: Generating Compressed Models for Edge Inference Using Tsetlin Machines. IEEE Transactions on Pattern Analysis and Machine Intelligence 45, 9 (2023), 11152–11168. doi:10.1109/TPAMI.2023.3268415DOI
- [23]Kumpati S. Narendra and Mandayam A. L. Thathachar. 1989. Learning Automata: An Introduction. Prentice Hall, Englewood Cliffs, NJ.
- [24]R. Kelley Pace and Ronald Barry. 1997. Sparse Spatial Autoregressions. Statistics & Probability Letters 33, 3 (1997), 291–297. doi:10.1016/S0167-7152(96)00140-XDOI
- [25]Adam Paszke, Sam Gross, Francisco Massa, Adam Lerer, James Bradbury, Gregory Chanan, Trevor Killeen, Zeming Lin, Natalia Gimelshein, Luca Antiga, Alban Desmaison, Andreas Köpf, Edward Yang, Zachary DeVito, Martin Raison, Alykhun Tejani, Sasank Chintala, Benoit Steiner, Lu Fang, Junjie Bai, and Soumith Chintala. 2019. PyTorch: An Imperative Style, High-Performance Deep Learning Library. In Advances in Neural Information Processing Systems 32 (NeurIPS 2019), 8024–8035.arxiv.org/abs/1912.01703
- [26]Adrian Phoulady, Ole-Christoffer Granno, Saeed Rahimi Gorji, and Hady Ahmady Phoulady. 2019. The Weighted Tsetlin Machine: Compressed Representations with Weighted Clauses. arXiv preprint arXiv:1911.12607. arXiv:1911.12607 doi:10.48550/arXiv.1911.12607 Presented at the Ninth International Workshop on Statistical Relational AI (StarAI 2020).DOI
- [27]Cynthia Rudin. 2019. Stop Explaining Black Box Machine Learning Models for High Stakes Decisions and Use Interpretable Models Instead. Nature Machine Intelligence 1, 5 (2019), 206–215. doi:10.1038/s42256-019-0048-xDOI
- [28]Jivitesh Sharma, Rohan Yadav, Ole-Christoffer Granno, and Lei Jiao. 2023. Drop Clause: Enhancing Performance, Robustness and Pattern Recognition Capabilities of the Tsetlin Machine. In Proceedings of the AAAI Conference on Artificial Intelligence, Vol. 37. 13547–13555. doi:10.1609/aaai.v37i11.26588DOI
- [29]Michael L. Tsetlin. 1961. On Behaviour of Finite Automata in Random Medium. Avtomatika i Telemekhanika 22, 10 (1961), 1345–1354. In Russian. English translation in Automation and Remote Control, 22:1210–1219, 1961.
- [30]Adrian Wheeldon, Alex Yakovlev, Rishad Shafik, and Jordan Morris. 2021. Low-Latency Asynchronous Logic Design for Inference at the Edge. In 2021 Design, Automation & Test in Europe Conference & Exhibition (DATE). IEEE, 370–373. doi:10.23919/DATE51398.2021.9474126 arXiv:2012.03402 (2020).DOI
- [31]William Wolberg, Olvi Mangasarian, Nick Street, and W. Street. 1995. Breast Cancer Wisconsin (Diagnostic). UCI Machine Learning Repository. doi:10.24432/C5DW2B
- [32]Han Xiao, Kashif Rasul, and Roland Vollgraf. 2017. Fashion-MNIST: A Novel Image Dataset for Benchmarking Machine Learning Algorithms. arXiv preprint arXiv:1708.07747. arXiv:1708.07747 doi:10.48550/arXiv.1708.07747
- [33]Jinbao Zhang, Xuan Zhang, Lei Jiao, Ole-Christoffer Granno, Yongjun Qian, and Fan Pan. 2023. Interpretable Tsetlin Machine-based Premature Ventricular Contraction Identification. arXiv preprint arXiv:2301.10181. arXiv:2301.10181 doi:10.48550/arXiv.2301.10181DOI

