deeplog.circuit.lower.klay

Klay-direct lowering backend.

Used for a circuit whose structure a Klay semiring implements. An operator Klay has no node for does not disqualify the circuit: the graph is cut there (deeplog.circuit.split) and the rest still lowers here. Chains of same-type associative ops are flattened into n-ary Klay nodes and identity-constant children dropped, producing a shallow circuit.

Klay’s only constant primitives are true/false, so an arbitrary numeric constant is carried as an input slot pre-filled with its value rather than as a node — see lower_klay().

Classes

ConstantPrefillModule

Expose a subset of an inner module's input slots as runtime inputs.

Functions

lower_klay(circuit, roots[, frontier]) → deeplog.module.deeplog_module.DeepLogModule

Lower a circuit to a Klay-backed torch module.

Module Contents

class ConstantPrefillModule(
inner: Module,
n_total_slots: int,
free_slot_indices: tuple[int, ...],
constant_slot_values: dict[int, float],
)[source]

Bases: torch.nn.Module

Inheritance diagram of deeplog.circuit.lower.klay.ConstantPrefillModule

Expose a subset of an inner module’s input slots as runtime inputs.

The wrapped module expects n_total_slots inputs. The user-visible forward only takes a tensor whose last dim has size len(free_slot_indices) — those values are scattered into the free slots, and the remaining slots are filled from constant_slot_values on every call.

Useful whenever some inputs of an inner torch module are statically known constants (e.g. baked numeric labels, ablated features, pinned test inputs) and you don’t want to rebuild the inner module to drop them.

Wrap inner so only free_slot_indices are user inputs.

forward(x)[source]

Scatter x into the free slots, prefill the rest with constants.

lower_klay(
circuit: Circuit,
roots: dict[int, Symbol],
frontier: Mapping[int, Symbol] | None = None,
) → DeepLogModule[source]

Lower a circuit to a Klay-backed torch module.

A boolean circuit’s negations are pushed down to its atoms first, by De Morgan’s laws, since Klay negates only those. Every reachable leaf gets an input slot. So does every reachable arbitrary-value constant node (0.6 in times(p, 0.6)), because Klay has no constant primitive beyond true/false — but those slots are pre-filled with their values and dropped from the module’s input shape, so they never surface as runtime inputs. Klay evaluates the resulting circuit arithmetically, so a constant node shared by several parents (they are deduplicated by value) multiplies once per use, as written.