circuits
circuits
¤
AbstractTorchCircuit
¤
Bases: TorchDiAcyclicGraph[TorchLayer]
An abstract circuit implementation in torch. It is a (possibly folded) computational graph of torch layers implementations.
Source code in cirkit/backend/torch/circuits.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | |
_num_channels = num_channels
instance-attribute
¤
_properties = properties
instance-attribute
¤
_scope = scope
instance-attribute
¤
layers
property
¤
layers_inputs
property
¤
Retrieve the map from layers to their inputs.
Returns:
| Type | Description |
|---|---|
Mapping[TorchLayer, Sequence[TorchLayer]]
|
The layers inputs map. |
layers_outputs
property
¤
Retrieve the map from layers to their outputs.
Returns:
| Type | Description |
|---|---|
Mapping[TorchLayer, Sequence[TorchLayer]]
|
The layers outputs map. |
num_channels
property
¤
Retrieve the number of channels of each variable.
Returns:
| Type | Description |
|---|---|
int
|
The number of variables. |
num_variables
property
¤
Retrieve the number of variables the circuit is defined on.
Returns:
| Type | Description |
|---|---|
int
|
The number of variables. |
properties
property
¤
Retrieve the structural properties of the circuit.
Returns:
| Type | Description |
|---|---|
StructuralProperties
|
The structural properties. |
scope
property
¤
__init__(scope, num_channels, layers, in_layers, outputs, *, properties, fold_idx_info=None)
¤
Initializes a torch circuit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scope
|
Scope
|
The variables scope. |
required |
num_channels
|
int
|
The number of channels per variable. |
required |
layers
|
Sequence[TorchLayer]
|
The sequence of layers. |
required |
in_layers
|
dict[TorchLayer, Sequence[TorchLayer]]
|
A dictionary mapping layers to their inputs, if any. |
required |
outputs
|
Sequence[TorchLayer]
|
A list of output layers. |
required |
properties
|
StructuralProperties
|
The structural properties of the circuit. |
required |
fold_idx_info
|
FoldIndexInfo | None
|
The folding index information. It can be None if the circuit is not folded. |
None
|
Source code in cirkit/backend/torch/circuits.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | |
_build_address_book(fold_idx_info)
¤
Source code in cirkit/backend/torch/circuits.py
249 250 | |
_build_unfold_index_info()
¤
Source code in cirkit/backend/torch/circuits.py
244 245 246 247 | |
_evaluate_layers(x)
¤
Source code in cirkit/backend/torch/circuits.py
252 253 254 255 | |
layer_inputs(l)
¤
Given a layer, retrieve the layers that are input to it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
l
|
TorchLayer
|
The layer. |
required |
Returns:
| Type | Description |
|---|---|
Sequence[TorchLayer]
|
The inputs to the given layer. |
Source code in cirkit/backend/torch/circuits.py
197 198 199 200 201 202 203 204 205 206 | |
layer_outputs(l)
¤
Given a layer, retrieve the layers that receive input from it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
l
|
TorchLayer
|
The layer. |
required |
Returns:
| Type | Description |
|---|---|
Sequence[TorchLayer]
|
The outputs from the given layer. |
Source code in cirkit/backend/torch/circuits.py
208 209 210 211 212 213 214 215 216 217 | |
reset_parameters()
¤
Reset the parameters of the circuit in-place.
Source code in cirkit/backend/torch/circuits.py
237 238 239 240 241 242 | |
LayerAddressBook
¤
Bases: AddressBook
The address book data structure for the circuits. See AbstractTorchCircuit. The address book stores a list of AddressBookEntry, where each entry stores the information needed to gather the inputs to each (possibly folded) circuit layer.
Source code in cirkit/backend/torch/circuits.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
from_index_info(fold_idx_info, *, incomings_fn)
classmethod
¤
Constructs the layers address book using fold index information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fold_idx_info
|
FoldIndexInfo
|
The fold index information. |
required |
incomings_fn
|
Callable[[TorchLayer], Sequence[TorchLayer]]
|
A function mapping each circuit layer to the sequence of its inputs. |
required |
Returns:
| Type | Description |
|---|---|
LayerAddressBook
|
A layers address book. |
Source code in cirkit/backend/torch/circuits.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
lookup(module_outputs, *, in_graph=None)
¤
Source code in cirkit/backend/torch/circuits.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
TorchCircuit
¤
Bases: AbstractTorchCircuit
The torch circuit implementation. Differently from TorchConstantCircuit, this circuit expects some input tensor, i.e., the assignment to variables.
Source code in cirkit/backend/torch/circuits.py
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | |
__call__(x)
¤
Source code in cirkit/backend/torch/circuits.py
264 265 266 | |
forward(x)
¤
Evaluate the circuit layers in forward mode, i.e., by evaluating each layer by following the topological ordering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
The tensor input of the circuit, with shape \((B, C, D)\), where B is the batch size, \(C\) is the number of channels, and \(D\) is the number of variables. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
The tensor output of the circuit, with shape \((B, O, K)\), where \(O\) is the number of vectorized outputs (i.e., the number of output layers), and \(K\) is the number of scalars in each output (e.g., the number of classes). |
Source code in cirkit/backend/torch/circuits.py
268 269 270 271 272 273 274 275 276 277 278 279 280 281 | |
TorchConstantCircuit
¤
Bases: AbstractTorchCircuit
The constant torch circuit implementation.
Differently from TorchCircuit, this circuit does not expect an input tensor. For instance, this circuit class is instantiated when a circuit encoding a partition function is compiled.
Source code in cirkit/backend/torch/circuits.py
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | |
__call__()
¤
Source code in cirkit/backend/torch/circuits.py
292 293 294 | |
forward()
¤
Evaluate the circuit layers in forward mode, i.e., by evaluating each layer by following the topological ordering.
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
The tensor output of the circuit, with shape \((B, O, K)\), where \(O\) is the number of vectorized outputs (i.e., the number of output layers), and \(K\) is the number of scalars in each output (e.g., the number of classes). |
Source code in cirkit/backend/torch/circuits.py
296 297 298 299 300 301 302 303 304 305 306 | |