Skip to content

added backward sampling in a layer-wise manner - #412

Open
gengala wants to merge 1 commit into
mainfrom
sampling-backward-layered
Open

added backward sampling in a layer-wise manner#412
gengala wants to merge 1 commit into
mainfrom
sampling-backward-layered

Conversation

@gengala

@gengala gengala commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator

as per title. related to #403.

use it as follows:

import torch
import matplotlib.pyplot as plt

from cirkit.backend.torch.queries import SamplingQuery

circuit = torch.load("circuit.pt", weights_only=False)

torch.manual_seed(131)
sq_backward = SamplingQuery(circuit, backward=True)
td_samples, _ = sq_backward(num_samples=16)

images = td_samples.cpu().reshape(16, 28, 28).numpy()
fig, axes = plt.subplots(4, 4, figsize=(8, 8))
for i, ax in enumerate(axes.flat):
    ax.imshow(images[i], cmap="gray", vmin=0, vmax=255)
    ax.axis("off")
fig.suptitle("Top-down sampling (backward=True)")
plt.show()

what I got from the MNIST circuit in the notebook:
image

@codecov

codecov Bot commented Jun 22, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 21.81818% with 86 lines in your changes missing coverage. Please review.
✅ Project coverage is 67.86%. Comparing base (eb99f6f) to head (ed07fe2).
⚠️ Report is 24 commits behind head on main.

Files with missing lines Patch % Lines
cirkit/backend/torch/queries.py 13.79% 49 Missing and 1 partial ⚠️
cirkit/backend/torch/layers/inner.py 34.48% 19 Missing ⚠️
cirkit/backend/torch/layers/input.py 15.38% 11 Missing ⚠️
cirkit/backend/torch/layers/optimized.py 25.00% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #412      +/-   ##
==========================================
- Coverage   68.71%   67.86%   -0.86%     
==========================================
  Files          58       58              
  Lines        6007     6114     +107     
  Branches      773      790      +17     
==========================================
+ Hits         4128     4149      +21     
- Misses       1590     1675      +85     
- Partials      289      290       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

torch.zeros(num_samples, dtype=torch.long, device=device),
)

samples = torch.zeros(num_samples, circuit.num_variables, dtype=torch.long, device=device)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to allow for heterogeneous inputs (as is the case in TabPC, where we have both categoricals and Gaussians), setting the dtype explicitly as int here causes errors later when we try to write floats into the samples tensor. Equally, if it is set to be float then we will have to cast the integer samples from the TorchCategoricalLayer before writing them.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On this note, would be nice if you or I could push the code for gaussian layers

# Sample which of (Ki * arity) inputs each path follows, then split by arity slot.
sample_ids, folds, units = selection
weight = self.weight()
# Shared-parameter layers replicate `weight` across folds — collapse fold index.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice to have it, and definitely it should not be lost, but I think that this logic should be in my custom layers (when I make the PR to add them).
Otherwise, it's the only place in these layers where we mention parameter sharing / fold of the parameter being different from the layers fold

@chbricout

Copy link
Copy Markdown
Contributor

In general, this looks quite good to me. Two point that I raised (1) remove reference to parameter sharing, (2) implement the logic for gaussian layers (I already have it and I believe Dylan has it also).

One thing that does strike me is the absence of tests, I think it should be quite easy to manually create a circuit to follow an objective distribution and compare the distribution of the samples to the objective distribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants