Connections API¶
Connections are the "which inputs feed each neuron" half of a
LogicDense layer. A connections module takes an
input of shape (batch, in_dim) and returns a gathered tensor of shape
(batch, lut_rank, out_dim) that the parametrization consumes.
Two variants:
fixed— buffered routing indices; a single gather per forward. No trainable parameters.learnable— softmax over a per-slot candidate pool (or all inputs withnum_candidates=-1); straight-through argmax on the forward pass.
Usage via LogicDense:
LogicDense(
in_dim=6272, out_dim=64000,
parametrization="light", lut_rank=4,
connections="learnable", num_candidates=8, init_method="random-unique",
)
Factory¶
setup_connections ¶
setup_connections(kind: str, in_dim: int, out_dim: int, lut_rank: int, device: device | str | None = None, **kwargs: Any) -> Connections
Build a dense connection module of the given kind.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kind
|
str
|
One of |
required |
in_dim
|
int
|
Number of input features. |
required |
out_dim
|
int
|
Number of output neurons. |
required |
lut_rank
|
int
|
Inputs per neuron. |
required |
device
|
device | str | None
|
Optional target device for buffers and parameters. |
None
|
**kwargs
|
Any
|
Extra keyword arguments forwarded to the selected class
constructor (e.g. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
Connections
|
class: |
Connections
|
class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Base class¶
Connections ¶
Connections(lut_rank: int = 2, device: device | str | None = None, init_method: str = 'random-unique', **_: Any)
Bases: Module, ABC
Abstract base for input-routing strategies.
A connections module maps an input tensor of shape (batch, in_dim)
to a gathered tensor of shape (batch, lut_rank, out_dim) that a
:class:~bitlogic.parametrizations.LUTParametrization can consume.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lut_rank
|
int
|
Number of inputs per output neuron. |
2
|
device
|
device | str | None
|
Optional target device for buffers and parameters. |
None
|
init_method
|
str
|
Index-initialization strategy — one of
|
'random-unique'
|
Source code in bitlogic/connections/base.py
forward
abstractmethod
¶
Gather lut_rank inputs per output neuron.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor of shape |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Tensor of shape |
Source code in bitlogic/connections/base.py
Concrete strategies¶
FixedDenseConnections ¶
FixedDenseConnections(in_dim: int, out_dim: int, lut_rank: int = 2, device: device | str | None = None, init_method: str = 'random-unique', num_groups: int | None = None, group_bias: float | None = None, **kwargs: Any)
Bases: Connections
Non-trainable dense routing — buffered indices, one gather per forward.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_dim
|
int
|
Number of input features. |
required |
out_dim
|
int
|
Number of output neurons. |
required |
lut_rank
|
int
|
Inputs per neuron. |
2
|
device
|
device | str | None
|
Optional target device for the index buffer. |
None
|
init_method
|
str
|
One of |
'random-unique'
|
num_groups
|
int | None
|
Required for |
None
|
group_bias
|
float | None
|
Required for |
None
|
**kwargs
|
Any
|
Forwarded to :class: |
{}
|
Source code in bitlogic/connections/fixed.py
LearnableDenseConnections ¶
LearnableDenseConnections(in_dim: int, out_dim: int, lut_rank: int = 2, temperature: float = 0.001, num_candidates: int = -1, forward_sampling: str = 'soft', device: device | str | None = None, init_method: str = 'random-unique', num_groups: int | None = None, group_bias: float | None = None, **kwargs: Any)
Bases: Connections
Learnable dense routing via softmax over candidate inputs.
Hard argmax in the forward (STE), softmax-weighted gradient in backward.
When num_candidates == -1 every input is a candidate for every
(slot, neuron) pair and the forward reduces to a matmul. Positive
num_candidates restricts the pool to a fixed subset per slot,
initialized from init_method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_dim
|
int
|
Number of input features. |
required |
out_dim
|
int
|
Number of output neurons. |
required |
lut_rank
|
int
|
Inputs per neuron. |
2
|
temperature
|
float
|
Softmax temperature for the backward weighting. |
0.001
|
num_candidates
|
int
|
Candidate inputs per slot. |
-1
|
forward_sampling
|
str
|
|
'soft'
|
device
|
device | str | None
|
Optional target device for buffers / parameters. |
None
|
init_method
|
str
|
|
'random-unique'
|
num_groups
|
int | None
|
Required for |
None
|
group_bias
|
float | None
|
Required for |
None
|
**kwargs
|
Any
|
Forwarded to :class: |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |