# Scaling Tractable Probabilistic Circuits: A Systems Perspective

PyJuice speeds up training of large probabilistic circuits on GPUs

Anji Liu, Kareem Ahmed, Guy Van den Broeck · arXiv · 2024-6-2

- **Problem** Existing GPU implementations of probabilistic circuits reload the same node values many times, so memory traffic slows training.
- **Method** PyJuice compiles a circuit into block-indexed tensors, so each loaded value serves a whole block of sum outputs instead of one.
- **Results** PyJuice trains large circuits 1 to 2 orders of magnitude faster than existing systems and uses 2 to 5x less GPU memory.

![Table 1: Average (± standard deviation of 5 runs) runtime (in seconds) per training epochof 60K samples for PyJuice and the baselines SPFlow (Molina et al., 20…](https://refivu.com/papers/e32b51bf-5311-4420-bc25-ec49bc5d82ec/figures/figure-97fc7495dd9413278ed2edad7597ace9.png?thumb=512)

**Table 1** . Average (± standard deviation of 5 runs) runtime (in seconds) per training epochof 60K samples for PyJuice and the baselines SPFlow (Molina et al., 2019), EiNet (Peharz et al., 2020a), Juice.jl (Dang et al., 2021), and Dynamax (Murphy et al., 2023). We adopted four PC structures: PD, RAT-SPN, HCLT, and HMM. All experiments were carried out on an RTX 4090 GPU with 24GB memory. To maximize parallelism, we always use the maximum possible batch size. “OOM” denotes out-of-memory with batch size2. The best numbers are in boldface.

[Original paper](https://arxiv.org/abs/2406.00766v2) · [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/) · Anji Liu, Kareem Ahmed, Guy Van den Broeck · Scaling Tractable Probabilistic Circuits: A Systems Perspective · Original paper figure · Extracted the figure area from page 2 of the original PDF.

## Why PC training stalls on GPUs

A probabilistic circuit computes its output by passing through a graph of sum and product nodes, so training and inference both reduce to passes over that graph. Neural network layers reduce to large matrix operations that GPUs handle well, but PC layers connect nodes sparsely, so those operators don't fit. The log-probabilities of different nodes can sit at drastically different scales, so the implementation has to handle numerical underflow as well.

Earlier GPU implementations process the graph node by node, which gives parallelism over a batch of samples. Parallelizing topologically independent nodes by layer adds speedup, but the authors note that it still leaves much unexploited. In a PD structure with about 150M edges, the IO and compute of sum layers both far exceed those of the product layers.

Sum layers have about 85x more edges than product layers, yet the runtime gap is over 50x.

The GPU's caching mechanism keeps this from causing 256x loads from High-Bandwidth Memory, but the excess IO still slows the algorithm down. The authors' design principle is to group sum edges across processors so that product node outputs are reloaded less.

The authors treat this IO-driven slowdown as the obstacle that limits how large a PC can be trained. Their system, PyJuice, targets this bottleneck to allow training of larger PCs within a fixed memory quota.

## Reusing loaded values across blocks

A fully connected sum layer shows the core idea most clearly, because every processor needs every input probability. Each processor computes the M sum nodes one at a time, so every input gets reloaded M times. One processor now computes all K\_M outputs of a sum node block, and it iterates over every product node block to accumulate partial results.

Each step touches K\_M times K\_N edges, so one loaded input serves K\_M outputs at once. With this strategy, the number of reloads of each input falls from M to M/K\_M.

Inside one step, the processor loads a parameter block of size K\_M times K\_N and a vector of K\_N input probabilities. The partial outputs come from a matrix-vector product between that parameter block and the input vector. Adding a batch dimension turns this into a matrix-matrix product, which GPU Tensor Cores compute efficiently. In the Figure 3 example with K\_M = K\_N = 2, the outputs of m0 and m1 come from two steps, first n0 and n1 and then n2 and n3.

The guiding principle is to allocate sum edges to processors so that product outputs are reloaded as rarely as possible. This allocation also lets part of the computation map onto Tensor Cores, which reduces the computational overhead of sum layers.

Most practical sum layers aren't fully connected, so the authors require block-sparsity. A sparse PC can be viewed as a block-sparse PC with block size 1. Zero parameters are padded onto edges that belong to the block-sparse PC but not to the sparse PC, so larger blocks can apply. The padded edges add cost, but the authors accept it when the sparsity pattern is fairly regular.

Even small block sizes such as 2 or 4 can give significant speedup despite the padding.

## Compiling PC layers into index tensors

The compiler stores index tensors so that one kernel can handle a whole group of sum blocks with the same child count. Before compiling, the PC is layered by topological depth, starting from the input nodes. Within a layer, nodes at the same depth can be computed in parallel because they don't depend on each other. A dynamic programming algorithm searches for the smallest number of groups whose fraction of added pseudo-node blocks stays below a predefined threshold.

For each group, the compiler records the starting index of each sum node block. The tensor sum\_ids holds the indices of all sum node blocks in the group. The tensors prod\_ids and param\_ids have one row per sum block, with the child indices and parameter indices in each row. Each parameter index points to the start of a KM×KN parameter block for one pair of sum and product node blocks.

In the worked example, the first group holds {m0, m1} and {m4, m5}, each with one child block, so sum\_ids stores m0 and m4. Grouping blocks by child count lets the kernel launch use hyperparameters tuned for each group's node count.

Each group's three index tensors feed one CUDA kernel that computes the log-probabilities of its sum nodes. The kernel launches CM×CB thread-blocks, and each block computes a KM×KB tile of outputs. In each loop step, a thread-block loads a KM×KN parameter block and a KN×KB input block into SRAM. It subtracts the per-sample maximum, exponentiates, and multiplies by the parameters with Tensor Cores.

The partial results go into a running accumulator in log space, which keeps the logsumexp computation stable.

The authors benchmarked the kernel on the largest PD sum layer, with 29K nodes and 30M edges. They varied the block sizes KM and KN exponentially from 1 to 64, and compared against a dedicated kernel for KM = KN = 1. Between L1 and L2 cache, the same kernel needs 25-50x fewer IO operations. Even with block sizes of 2 or 4, the speedup over the baseline is quite significant.

A sparse PC can be padded into a block-sparse PC with block size 1, which is how the design covers sparse PCs.

## Getting gradients from PC flows

The backward pass would need to store or recompute intermediate values such as lmax and pp. Thread blocks that read the same product-node log-probabilities would each have to write partial gradients for them. These writes create barriers between thread blocks, which slow down execution. The authors sidestep both costs by propagating PC flows instead.

A PC flow differs from the desired gradient only by a factor of θn,c, and it has a simple recursive definition.

The flow of the root node is set to 1, and every other node's flow is built from its parents' flows. A product-node parent contributes its flow weighted by the parameter θm,n times the ratio pn(x)/pm(x). The edge flow from node n to child c is Fn,c(x) = θn,c · pc(x)/pn(x) · Fn(x). The authors prove in Section B.2 that Fn(x) equals the derivative of log p\_nr(x) with respect to log pn(x).

They also prove that Fn,c(x) equals θn,c times the derivative of log p\_nr(x) with respect to θn,c.

The backward pass sets the root flow to 1 and sweeps the layers in reverse, so parent layers come before child layers. Each layer's child flows are computed from flows already produced by the preceding layers. Block-based parallelization then reduces reloads of parent log-probabilities, and Section A.2 gives the full algorithm. The authors also recompute product-node probabilities during the backward pass instead of storing them on the GPU.

All product layers write into one scratch space on GPU HBM sized for the largest product layer. A sum layer that needs a product-node probability re-computes it on request, which costs slightly more computation time.

## Runtime against existing PC systems

The authors timed four structures, PD, RAT-SPN, HCLT and HMM, with five sizes each, on an RTX 4090 with 24GB of memory. Each run covers 60K samples including the forward pass, the backward pass and mini-batch EM, averaged over 5 runs. The baselines are SPFlow, EiNet and Juice.jl, with Dynamax used only for HMMs.

SPFlow took over 25000 seconds on every PD size.

Juice.jl is the strongest baseline in these columns, so the comparison with it matters most.

Dynamax batch sizes of 128 or more caused internal errors, so its results were not reported at those sizes.

On memory, the authors measured an HCLT with 159M edges and an HMM with 130M edges, and PyJuice used less GPU memory than the baselines. The gap is larger at large batch sizes, because a constant-size space is always needed for the parameters. Compiling the HCLT with 159M edges took about 8.7 s, and the compiled structure is reused for training and inference.

## Block-sparse and pruned PCs

The block-sparse benchmark starts from a sum layer with 209M edges, which the authors partition into blocks of 32 nodes on each side. They randomly discard 32 by 32 edge blocks, so the remaining layer keeps a block-sparse structure. As the fraction of removed edge blocks grows, the runtime of both the forward and the backward pass falls significantly.

Each point in Figure 7 averages 5 runs, and its error bars show standard deviations. The authors read this trend as a reason for future PC modeling work to focus on designing block-sparse PCs.

For unstructured sparsity, the authors prune two HCLTs with 10M and 40M edges using the algorithm of Dang et al. (2022). They compare PyJuice only with Juice.jl, because the other implementations can't handle sparse PCs. Figure 8 plots the runtime per epoch on 60K samples for both HCLTs as the fraction of pruned edges rises. PyJuice stays faster than Juice.jl in every setting shown, but the gap narrows once over 90% of edges are pruned.

On sparse PCs, PyJuice can't draw the full benefit from the block-based parallelization of Section 4. Even so, PyJuice still outperforms the baseline on these pruned models. The two experiments together show whether the runtime gains hold when a PC is sparse or pruned.

## Fine-tuning an HMM language model with PyJuice

That HMM was then fine-tuned on the CommonGen dataset and used to control constrained generation in large language models. The original model reached a perplexity of 9.78 on CommonGen.

![Table 2: Perplexity of HMM language models trained on the Com- monGen benchmark (Lin et al., 2020)](https://refivu.com/papers/e32b51bf-5311-4420-bc25-ec49bc5d82ec/figures/figure-97d260f6ca52a649f1f7883af8a2fcdb.png?thumb=512)

**Table 2** . Perplexity of HMM language models trained on the Com- monGen benchmark (Lin et al., 2020).

[Original paper](https://arxiv.org/abs/2406.00766v2) · [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/)

The perplexity improvement is about 1.0 relative to the original model. The paper points to Section C.2 for more details on this fine-tuning.

The current draft's comparison mixes two changes, because the PyJuice rows also change the hidden size. The paper's table therefore doesn't isolate the effect of hidden size alone.

## Fitting a full image PC in memory

Liu et al. (2023c) train sparse patch-level PCs on 4×4 patches, plus a top-level PC that aggregates their outputs. The full PC had over 10M nodes, so the original paper fine-tuned only the top-level model. With PyJuice, the entire model fits in 24GB of GPU memory, so the authors can fine-tune all of it.

For the PC trained on ImageNet32, this fine-tuning step moves the result from 4.06 to 4.04 bits-per-dimension. The gain is small, but the setup now covers the whole model instead of one part of it. Appendix C.3 gives the further details of this fine-tuning run.

## Natural image density baselines for PD and HCLT

The authors fit PD and HCLT models to 16 by 16 patches sampled at random from three natural image datasets. Each patch yields 768 categorical variables, because 16×16×3 gives 768 and each variable takes one of 256 values. Before training, the patches are converted into the YCoCg color space, which the authors observed to improve density estimation.

The random top-left patch positions are sampled uniformly, which the authors note gives different results from aligned patches.

The authors adopt two PD structures, PD-mid with 107M edges and PD-large with 405M edges. They also adopt two HCLT structures, HCLT-mid with 40M edges and HCLT-large with 174M edges. They adopt full-batch EM, because it gives consistently better performance across models and datasets than other optimization strategies. The PC flows are accumulated across all training samples before one EM step is taken.

On ImageNet32, HCLT-large reaches 4.33 bits-per-dimension, compared to 5.20 for PD-large. Liu et al. (2023a) reports 4.82 bits-per-dimension for HCLT on ImageNet32, while the authors achieve 4.33. The authors credit more training epochs and more hyperparameter search, which the speedup made possible. Because the patches are in YCoCg rather than RGB, these numbers aren't directly comparable with RGB results.

The stated aim is to set reproducible baselines, not records, and WikiText results appear in Section C.4.

![Table 3: Density estimation performance of PCs on three natural image datasets. Reported numbers are test set bits-per-dimension](https://refivu.com/papers/e32b51bf-5311-4420-bc25-ec49bc5d82ec/figures/figure-4d9ad0fbff0e88d129aafeac5ba3c6b5.png?thumb=512)

**Table 3** . Density estimation performance of PCs on three natural image datasets. Reported numbers are test set bits-per-dimension.

[Original paper](https://arxiv.org/abs/2406.00766v2) · [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/)

## Limits of the evidence

Sparse PCs gain less from block-based parallelism in the reported runs than dense ones do. The authors state that PyJuice cannot fully benefit from block-based parallelization on sparse PCs. They compare only against Juice.jl, because the other implementations don't support sparse PCs. The sparse test uses two HCLTs with 10M and 40M edges, pruned with the algorithm of Dang et al.

(2022). The speed gap narrows when over 90% of edges are pruned. Padding adds pseudo-edges with zero parameters, and a relatively large number of them may need padding for irregular sparsity.

All runtime and memory measurements were taken on an RTX 4090 GPU with 24GB of memory. The paper reports no other GPU, so the speed numbers describe this one card. SPFlow and EiNet are left out of the HMM results because they can't share transition and emission parameters across time steps. Dynamax leads to internal errors at batch size 128 or more, so those HMM results are missing.

The memory comparison covers only one HCLT with 159M edges and one HMM with 130M edges.

The image gains depend on more training epochs and more hyperparameter search, which the speedup made possible. Because of the YCoCg preprocessing, the bits-per-dimension results aren't directly comparable with RGB numbers. Liu et al. (2023a) reports 4.82 bits-per-dimension for HCLT on ImageNet32, while the authors reached 4.33. The HMM perplexity comparison starts from the 4096-state model that Zhang et al.

(2023) trained.

They present the image results as reproducible baselines for tracking progress in PC modeling and learning. The block-sparse runtime study uses one synthetic sum layer with 209M edges, not a trained model.

## Paper info

- **Authors**: Anji Liu, Kareem Ahmed, Guy Van den Broeck
- **Venue**: arXiv · Preprint
- **First published**: 2024-6-2
- **Added**: 2026-10-29
- **Updated**: 2025-10-30
- **Version**: v2
- **arXiv**: 2406.00766
- **Category**: cs.LG
- **Original paper terms**: [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/)
- **What we show**: Paper info, commentary and permitted figures from the original paper

[Original paper ↗](https://arxiv.org/abs/2406.00766v2) · [PDF ↗](https://arxiv.org/pdf/2406.00766v2)

AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.

## Abstract

Probabilistic Circuits (PCs) are a general framework for tractable deep generative models, which support exact and efficient probabilistic inference on their learned distributions. Recent modeling and training advancements have enabled their application to complex real-world tasks. However, the time and memory inefficiency of existing PC implementations hinders further scaling up. This paper proposes PyJuice, a general GPU implementation design for PCs that improves prior art in several regards. Specifically, PyJuice is 1-2 orders of magnitude faster than existing systems (including very recent ones) at training large-scale PCs. Moreover, PyJuice consumes 2-5x less GPU memory, which enables us to train larger models. At the core of our system is a compilation process that converts a PC into a compact representation amenable to efficient block-based parallelization, which significantly reduces IO and makes it possible to leverage Tensor Cores available in modern GPUs. Empirically, PyJuice can be used to improve state-of-the-art PCs trained on image (e.g., ImageNet32) and language (e.g., WikiText, CommonGen) datasets. We further establish a new set of baselines on natural image and language datasets by benchmarking existing PC structures but with much larger sizes and more training epochs, with the hope of incentivizing future research. Code is available at https://github.com/Tractables/pyjuice.

## Quote from the paper

Anji Liu, Kareem Ahmed, Guy Van den Broeck (2024). Scaling Tractable Probabilistic Circuits: A Systems Perspective. arXiv:2406.00766v2. https://arxiv.org/abs/2406.00766v2

**BibTeX**

```
@misc{liu2024scaling,
  title = {{Scaling Tractable Probabilistic Circuits: A Systems Perspective}},
  author = {Anji Liu and Kareem Ahmed and Guy Van den Broeck},
  year = {2024},
  eprint = {2406.00766},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2406.00766v2}
}
```

## Other paper commentary

- [Dolphin: A Programmable Framework for Scalable Neurosymbolic Learning](https://refivu.com/en/papers/45dd94f7-4a58-4799-bc56-64bca4cda2ed-dolphin-a-programmable-framework-for-scalable-neurosymbolic.md)
- [The Look-Elsewhere Effect in Searches for New Physics: from Classical Methods to Machine Learning](https://refivu.com/en/papers/40749a33-958c-4352-8394-19db350aef80-the-look-elsewhere-effect-in-searches-for-new.md)
- [When to Trust a PFN: Detecting Harmful Shift in Tabular Foundation Models](https://refivu.com/en/papers/f72c16f8-edc3-4f30-bb5a-814b2eb7dc46-when-to-trust-a-pfn-detecting-harmful-shift.md)
- [TASK Memory: Beyond Text-Only Memory with Activation Steering and Knowledge Editing against Memory Hallucination](https://refivu.com/en/papers/b18e954a-3014-44f6-b927-a1312ec35082-task-memory-beyond-text-only-memory-with-activation.md)
- [The Reliability Function of Error-Correcting Output Codes with Noisy Binary Classifiers](https://refivu.com/en/papers/8c266525-b2cc-4dc5-bfd3-2f2927e30f5a-the-reliability-function-of-error-correcting-output-codes.md)
- [Typical Angina, Reversed: A Long-Standing Documentation Error in the UCI Heart Disease Data](https://refivu.com/en/papers/b1cd0f68-7d26-46cf-88cd-e57ef82a37ed-typical-angina-reversed-a-long-standing-documentation-error.md)
- [SpargeAttention: Accurate and Training-free Sparse Attention Accelerating Any Model Inference](https://refivu.com/en/papers/6a719301-c428-424d-9dc3-3159ff623aea-spargeattention-accurate-and-training-free-sparse-attention.md)
- [Adversarial Flow Models](https://refivu.com/en/papers/5de76f30-93e7-4a6f-b44f-f36b2096f9e2-adversarial-flow-models.md)
- [EmbodiedLab: Benchmarking Embodied Agents That Act to Know](https://refivu.com/en/papers/db9dafac-e675-4429-8d4f-1cea4451726d-embodiedlab-benchmarking-embodied-agents-that-act-to-know.md)
- [SABER: A SQL-Compatible Semantic Document Processing System Based on Extended Relational Algebra](https://refivu.com/en/papers/c1ac9468-9718-49de-bad2-4505803dee02-saber-a-sql-compatible-semantic-document-processing-system.md)
- [PaLM-E: An Embodied Multimodal Language Model](https://refivu.com/en/papers/c14507e3-43e7-434d-adc8-94f0d2c42b5f-palm-e-an-embodied-multimodal-language-model.md)
- [Flow Straight and Fast: Learning to Generate and Transfer Data with Rectified Flow](https://refivu.com/en/papers/a0c4439d-88d7-4d0f-be30-5509d158003e-flow-straight-and-fast-learning-to-generate-and.md)

## Terms in this paper

- [WikiText](https://refivu.com/en/terms/b2db4641e2db-wikitext)
- [density estimation](https://refivu.com/en/terms/3c3bb9a30918-density-estimation)
- [runtime](https://refivu.com/en/terms/de0efb0b9bc3-runtime)
- [Tensor Cores](https://refivu.com/en/terms/3c542399e985-tensor-cores)

---

AI-written text can contain mistakes. [Refivu](https://refivu.com/en/papers/e32b51bf-5311-4420-bc25-ec49bc5d82ec-scaling-tractable-probabilistic-circuits-a-systems)
