"""
A module that implements transformations between different shapes.
"""
# Copyright (c) 2024-2026. KU Leuven
import numpy as np
import torch
from ..shape import Shape
from ..shape import SymTensor
from ..util import as_tuple
from .deeplog_module import DeepLogModule
from .sequential import Sequential
[docs]
def reshape(
module: DeepLogModule,
input: tuple[SymTensor, ...] | SymTensor | None = None,
output: tuple[SymTensor, ...] | SymTensor | None = None,
) -> DeepLogModule:
"""Wrap ``module`` so its input and/or output shape matches the requested shapes.
:param module: The module whose input and/or output should be reshaped.
:param input: If provided, the new input shape for ``module``.
:param output: If provided, the new output shape for ``module``.
:return: A module that performs identically to the given module under the requested shapes.
"""
if input is not None and input != module.get_input_shape():
module = Sequential(
construct_transformation(input, module.get_input_shape()), module
)
if output is not None and output != module.get_output_shape():
module = Sequential(
module, construct_transformation(module.get_output_shape(), output)
)
return module
def _per_source(
sources: list[int], offsets: list[int]
) -> tuple[list[tuple[int, list[int]]], list[int] | None]:
"""The columns an output reads of each source, and where they go.
``sources`` and ``offsets`` give each output column's source and its column
there. The reads are each source's columns, in order of first use, and the
order is the position, among the columns read, of each output column:
``None`` where they are already in output order.
"""
by_source: dict[int, list[int]] = {}
placed: dict[int, list[int]] = {}
for position, (source, offset) in enumerate(zip(sources, offsets, strict=True)):
by_source.setdefault(source, []).append(offset)
placed.setdefault(source, []).append(position)
positions = [position for source in by_source for position in placed[source]]
if positions == sorted(positions):
return list(by_source.items()), None
order = [0] * len(positions)
for column, position in enumerate(positions):
order[position] = column
return list(by_source.items()), order
def _gather_indices(labels: dict, output: SymTensor, *, raise_on: tuple) -> np.ndarray:
"""Build the flat gather indices for a single output SymTensor over ``labels``."""
if output.array.size == 0:
return np.array([], dtype=np.int64)
try:
return np.vectorize(lambda x: labels[x])(output.array).astype(np.int64)
except KeyError as exc:
raise TransformationNotPossible(*raise_on) from exc
class _IndexingTransform(DeepLogModule):
"""Flatten a SymTensor input and gather one or more output SymTensors from it."""
_input_shape: SymTensor
_output_shape: Shape
def __init__(self, input_shape: SymTensor, output_shape: Shape):
"""Precompute one gather buffer per output entry over the flattened input."""
super().__init__(input_shape, output_shape)
labels = {label: index for index, label in enumerate(input_shape.array.flat)}
outputs = as_tuple(output_shape)
for i, out in enumerate(outputs):
arr = _gather_indices(labels, out, raise_on=(input_shape, output_shape))
self.register_buffer(f"_indices_{i}", torch.from_numpy(arr))
self._empty_outputs = tuple(out.is_empty() for out in outputs)
self._n_outputs = len(outputs)
self._is_tuple_output = isinstance(output_shape, tuple)
self._input_ndim = input_shape.array.ndim
def forward(self, input_tensor):
"""Flatten the input once, then gather each output's positions."""
if self._input_ndim == 0:
flat = input_tensor.unsqueeze(1)
else:
flat = torch.flatten(input_tensor, start_dim=1, end_dim=self._input_ndim)
gathered = tuple(
# An empty output channel is canonically (batch, 0); a gather with
# zero indices would instead drag the input's subtensor dims along.
flat.new_zeros(flat.shape[0], 0)
if self._empty_outputs[i]
else flat[:, getattr(self, f"_indices_{i}")]
for i in range(self._n_outputs)
)
return gathered if self._is_tuple_output else gathered[0]
class _TupleIndexingTransform(DeepLogModule):
"""Select+flatten+concat a tuple input once, then gather one or more output SymTensors.
The concat packs every selected input into one ``(batch, n_symbols, *features)``
tensor, which requires the inputs to agree on their trailing feature shape and
their dtype. They do not always: a module may mix a network predicate's data
argument (one feature *vector* per symbol, e.g. an image) with plain scalar atom
probabilities, or a variable's positions, integers, with probabilities. When the
selected inputs disagree, the packing is skipped and each output is gathered
straight from its source inputs instead, one read per source, so that every
value keeps its dtype.
"""
_input_shape: tuple[SymTensor, ...]
_output_shape: Shape
def __init__(self, input_shapes: tuple[SymTensor, ...], output_shape: Shape):
"""Precompute the shared input flatten/concat and a gather buffer per output."""
super().__init__(input_shapes, output_shape)
outputs = as_tuple(output_shape)
# Union of input-tuple entries that any output reads from.
# When an output is empty we still keep one entry around to preserve the batch dim.
selected_set: set[int] = set()
for out in outputs:
if out.array.size == 0:
if input_shapes:
selected_set.add(0)
continue
for i, shape in enumerate(input_shapes):
if any(sym in out for sym in shape):
selected_set.add(i)
selected = sorted(selected_set)
relevant = [input_shapes[i] for i in selected]
flat_symbols = (
np.concatenate([s.array.flatten() for s in relevant])
if relevant
else np.array([])
)
labels = {label: index for index, label in enumerate(flat_symbols)}
# Column count each selected input contributes to the packed tensor; the
# running total turns a packed index into a (source, offset) pair for the
# unpacked path below.
bounds = np.cumsum([0] + [int(s.array.size) for s in relevant])
unpacked: list[tuple[list[tuple[int, list[int]]], list[int] | None]] = []
for i, out in enumerate(outputs):
arr = _gather_indices(labels, out, raise_on=(input_shapes, output_shape))
self.register_buffer(f"_indices_{i}", torch.from_numpy(arr))
source = np.atleast_1d(np.searchsorted(bounds, arr, side="right") - 1)
offset = np.atleast_1d(arr) - bounds[source]
unpacked.append(
_per_source(source.ravel().tolist(), offset.ravel().tolist())
)
#: For each output, the columns it reads of each selected input, and
#: where the columns read go, where packing is skipped.
self._unpacked = tuple(unpacked)
self._symbol_shapes = tuple(out.array.shape for out in outputs)
self._empty_outputs = tuple(out.is_empty() for out in outputs)
self._selected = tuple(selected)
self._end_dims = tuple(s.array.ndim for s in relevant)
self._n_outputs = len(outputs)
self._is_tuple_output = isinstance(output_shape, tuple)
def forward(self, *x):
"""Flatten+concat the selected inputs once, then gather each output's positions."""
if not self._selected:
empty = torch.zeros(1, 0)
return (
tuple(empty for _ in range(self._n_outputs))
if self._is_tuple_output
else empty
)
flat = [
torch.flatten(x[i], start_dim=1, end_dim=end_dim)
if end_dim > 0
else x[i].unsqueeze(1)
for i, end_dim in zip(self._selected, self._end_dims, strict=True)
]
if any(
t.shape[2:] != flat[0].shape[2:] or t.dtype != flat[0].dtype
for t in flat[1:]
):
return self._gather_unpacked(flat)
concat = flat[0] if len(flat) == 1 else torch.cat(flat, dim=1)
gathered = tuple(
# An empty output channel is canonically (batch, 0); a gather with
# zero indices would instead drag the input's subtensor dims along.
concat.new_zeros(concat.shape[0], 0)
if self._empty_outputs[i]
else concat[:, getattr(self, f"_indices_{i}")]
for i in range(self._n_outputs)
)
return gathered if self._is_tuple_output else gathered[0]
def _gather_unpacked(self, flat):
"""Gather each output straight from its sources, without packing them first.
The path for inputs whose feature shapes or dtypes disagree, so
``torch.cat`` cannot pack them, or would change a dtype. Each output reads
its columns of each input in one operation; an output must therefore
still be internally homogeneous, which it is whenever it feeds a single
consumer (a predicate argument, a circuit's atom channel).
"""
batch = flat[0].shape[0]
gathered = []
for i in range(self._n_outputs):
if self._empty_outputs[i]:
gathered.append(flat[0].new_zeros(batch, 0))
continue
sources, order = self._unpacked[i]
read = [flat[source][:, columns] for source, columns in sources]
stacked = read[0] if len(read) == 1 else torch.cat(read, dim=1)
if order is not None:
stacked = stacked[:, order]
# Restore the output's symbol dimensions, which ravelling flattened.
gathered.append(
stacked.reshape(batch, *self._symbol_shapes[i], *stacked.shape[2:])
)
return tuple(gathered) if self._is_tuple_output else gathered[0]