semiring
semiring
¤
Semiring = type['SemiringImpl']
module-attribute
¤
ComplexLSESumSemiring
¤
Bases: SemiringImpl
The complex log space computation.
Source code in cirkit/backend/torch/semiring.py
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 | |
add(*xs)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
439 440 441 442 443 | |
apply_reduce(func, *xs, dim, keepdim)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 | |
cast(x)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
424 425 426 427 428 429 430 431 | |
mul(*xs)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
451 452 453 454 455 | |
prod(x, dim, *, keepdim=False)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
445 446 447 448 449 | |
sum(x, dim, *, keepdim=False)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
433 434 435 436 437 | |
EinsumFunc
¤
Bases: Protocol
Protocol for einsum-like tensor-contraction callables.
Source code in cirkit/backend/torch/semiring.py
15 16 17 18 | |
__call__(*xs)
¤
Source code in cirkit/backend/torch/semiring.py
18 | |
LSESumSemiring
¤
Bases: SemiringImpl
The log space computation.
Source code in cirkit/backend/torch/semiring.py
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | |
add(*xs)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
372 373 374 | |
apply_reduce(func, *xs, dim, keepdim)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | |
cast(x)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
359 360 361 362 363 364 365 366 | |
mul(*xs)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
380 381 382 | |
prod(x, dim, *, keepdim=False)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
376 377 378 | |
sum(x, dim, *, keepdim=False)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
368 369 370 | |
SemiringImpl
¤
Bases: ABC
The abstract base class for semiring implementations.
Due to numerical precision, the actual units in computational graph may hold values in, e.g., log space, instead of linear space. And therefore, this provides a unified interface for the computations so that computation can be done in a space suitable to the implementation regardless of the global setting.
Source code in cirkit/backend/torch/semiring.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 112 113 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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | |
__new__()
¤
Raise an error when this class is instantiated.
Raises:
| Type | Description |
|---|---|
TypeError
|
When this class is instantiated. |
Returns:
| Name | Type | Description |
|---|---|---|
SemiringImpl |
SemiringImpl
|
This method never returns. |
Source code in cirkit/backend/torch/semiring.py
137 138 139 140 141 142 143 144 145 146 | |
add(*xs)
abstractmethod
classmethod
¤
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*xs
|
Tensor
|
|
()
|
Returns:
Source code in cirkit/backend/torch/semiring.py
235 236 237 238 239 240 241 242 243 244 245 | |
apply_reduce(func, *xs, dim, keepdim)
abstractmethod
classmethod
¤
Apply a sum-like functions to the tensor(s).
The sum units may perform not just plain sum, but also weighted sum or even einsum. In fact, it can possibly be any function that is linear to the each input. All that kind of func can be used here.
It is expected that func always does computation in the linear space, as with numerical tricks, only relatively significant numbers contribute the final answer, and underflow will not affect much. However, the input/output values may still be in another space, and needs to be projected here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[[Unpack[Ts]], Tensor]
|
The sum-like function to be applied. |
required |
*xs
|
Unpack[Ts]
|
The input tensors. Type expected to be Tensor. |
()
|
dim
|
int
|
The dimension along which the values are correlated and must be scaled together, i.e., the dim to sum along. This should match the actual operation done by func. The same dim is shared among all inputs. |
required |
keepdim
|
bool
|
Whether the dim is kept as a size-1 dim, should match the actual operation done by func. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
The sum result. |
Source code in cirkit/backend/torch/semiring.py
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | |
cast(x)
abstractmethod
classmethod
¤
Cast a tensor to the data type required by this semiring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
The tensor. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
The tensor converted to the required data type. |
Source code in cirkit/backend/torch/semiring.py
209 210 211 212 213 214 215 216 217 218 219 | |
einsum(equation, *, inputs=None, operands=None, dim, keepdim)
classmethod
¤
Perform an einsum operation where sums and products are specified by the semiring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
equation
|
str | Sequence[Sequence[int]]
|
The einsum expression. |
required |
inputs
|
tuple[Tensor, ...] | None
|
The inputs of the einsum. |
None
|
operands
|
tuple[Tensor, ...] | None
|
Additional operands to pass to the einsum, after the inputs in the einsum expression. |
None
|
dim
|
int
|
The dimension of the inputs that get summed over in the einsum expression. This is useful to make the einsum computationally stable in some semirings, e.g., the log-sum-exp semiring. |
required |
keepdim
|
bool
|
Whether to keep the dimension that get summed over in the einsum expression. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
the result of the einsum operation over the semiring. |
Source code in cirkit/backend/torch/semiring.py
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 | |
from_name(name)
staticmethod
¤
Get a semiring by its registered name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name to probe. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Semiring |
Semiring
|
The retrieved concrete Semiring. |
Source code in cirkit/backend/torch/semiring.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
list()
staticmethod
¤
List all semiring names registered.
Returns:
| Type | Description |
|---|---|
Iterable[str]
|
Iterable[str]: An iterable over all names available. |
Source code in cirkit/backend/torch/semiring.py
89 90 91 92 93 94 95 96 | |
map_from(x, semiring)
classmethod
¤
Map a tensor from the given semiring to this semiring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
|
required |
semiring
|
Semiring
|
|
required |
Returns:
Source code in cirkit/backend/torch/semiring.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
mul(*xs)
abstractmethod
classmethod
¤
Multiply broadcastable tensors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*xs
|
Tensor
|
The input tensors, should have broadcastable shapes. |
()
|
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
The multiply result. |
Source code in cirkit/backend/torch/semiring.py
261 262 263 264 265 266 267 268 269 270 271 | |
prod(x, dim, *, keepdim=False)
abstractmethod
classmethod
¤
Do the product within a tensor on given dim(s).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
The input tensor. |
required |
dim
|
int
|
The dimension to reduce along. |
required |
keepdim
|
bool
|
Whether the dim is kept as a size-1 dim. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
The product result. |
Source code in cirkit/backend/torch/semiring.py
247 248 249 250 251 252 253 254 255 256 257 258 259 | |
register(name)
staticmethod
¤
Register a concrete semiring implementation by its name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name to register. |
required |
Returns:
| Type | Description |
|---|---|
Callable[[Semiring], Semiring]
|
Callable[[Semiring], Semiring]: The class decorator to register a subclass. |
Source code in cirkit/backend/torch/semiring.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | |
register_map_from(other)
classmethod
¤
Register a concrete semiring morphism implementation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Semiring
|
The source semiring. |
required |
Returns:
| Type | Description |
|---|---|
Callable[[Callable[[Tensor], Tensor]], Callable[[Tensor], Tensor]]
|
Callable[[Callable[[Tensor], Tensor]], Callable[[Tensor], Tensor]]: |
Callable[[Callable[[Tensor], Tensor]], Callable[[Tensor], Tensor]]
|
The function decorator to register the morphism. |
Source code in cirkit/backend/torch/semiring.py
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 | |
sum(x, dim, *, keepdim=False)
abstractmethod
classmethod
¤
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
|
required |
dim
|
int
|
|
required |
keepdim
|
bool
|
|
False
|
Returns:
Source code in cirkit/backend/torch/semiring.py
221 222 223 224 225 226 227 228 229 230 231 232 233 | |
SumProductSemiring
¤
Bases: SemiringImpl
The linear space computation.
Source code in cirkit/backend/torch/semiring.py
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | |
add(*xs)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
332 333 334 | |
apply_reduce(func, *xs, dim, keepdim)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
344 345 346 347 348 349 350 351 352 | |
cast(x)
classmethod
¤
Cast a tensor to the data type required by the semiring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
The tensor. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
The tensor converted to the required data type. |
Source code in cirkit/backend/torch/semiring.py
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | |
mul(*xs)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
340 341 342 | |
prod(x, dim, *, keepdim=False)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
336 337 338 | |
sum(x, dim, *, keepdim=False)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
328 329 330 | |
_(x)
¤
Source code in cirkit/backend/torch/semiring.py
529 530 531 | |