semiring
semiring
¤
Semiring = type[SemiringImpl]
module-attribute
¤
SemiringT = TypeVar('SemiringT', bound=type['SemiringImpl'])
module-attribute
¤
Ts = TypeVarTuple('Ts')
module-attribute
¤
ComplexLSESumSemiring
¤
Bases: SemiringImpl
The complex log space computation.
Source code in cirkit/backend/torch/semiring.py
417 418 419 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 | |
add(*xs)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
435 436 437 | |
apply_reduce(func, *xs, dim, keepdim)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
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 | |
cast(x)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
422 423 424 425 426 427 428 429 | |
mul(*xs)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
443 444 445 | |
prod(x, /, *, dim=None, keepdim=False)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
439 440 441 | |
sum(x, /, *, dim=None, keepdim=False)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
431 432 433 | |
LSESumSemiring
¤
Bases: SemiringImpl
The log space computation.
Source code in cirkit/backend/torch/semiring.py
353 354 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 411 412 413 414 | |
add(*xs)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
371 372 373 | |
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 411 412 413 414 | |
cast(x)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
358 359 360 361 362 363 364 365 | |
mul(*xs)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
380 381 382 | |
prod(x, /, *, dim=None, keepdim=False)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
375 376 377 378 | |
sum(x, /, *, dim=None, keepdim=False)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
367 368 369 | |
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
17 18 19 20 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 | |
_registry = {}
class-attribute
¤
_registry_morphisms = {}
class-attribute
¤
__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
142 143 144 145 146 147 148 149 150 151 152 | |
add(*xs)
abstractmethod
classmethod
¤
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*xs
|
Tensor
|
|
()
|
Returns:
Source code in cirkit/backend/torch/semiring.py
221 222 223 224 225 226 227 228 229 230 231 | |
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
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 | |
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
195 196 197 198 199 200 201 202 203 204 205 | |
einsum(equation, *, inputs=None, operands=None, dim, keepdim)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
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 | |
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 |
SemiringT
|
The retrieved concrete Semiring. |
Source code in cirkit/backend/torch/semiring.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | |
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
92 93 94 95 96 97 98 99 100 | |
map_from(x, semiring)
classmethod
¤
Map a tensor from the given semiring to this semiring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
|
required |
semiring
|
SemiringT
|
|
required |
Returns:
Source code in cirkit/backend/torch/semiring.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | |
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
250 251 252 253 254 255 256 257 258 259 260 | |
prod(x, /, *, dim=None, 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
|
Optional[Union[int, Sequence[int]]]
|
The dimension(s) to reduce along, None for all dims. Defaults to None. |
None
|
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
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | |
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[[SemiringT], SemiringT]
|
Callable[[Semiring], Semiring]: The class decorator to register a subclass. |
Source code in cirkit/backend/torch/semiring.py
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 | |
register_map_from(other)
classmethod
¤
Register a concrete semiring morphism implementation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
SemiringT
|
The source semiring. |
required |
Returns:
| Type | Description |
|---|---|
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
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 | |
sum(x, /, *, dim=None, keepdim=False)
abstractmethod
classmethod
¤
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
|
required |
dim
|
int | None
|
|
None
|
keepdim
|
bool
|
|
False
|
Returns:
Source code in cirkit/backend/torch/semiring.py
207 208 209 210 211 212 213 214 215 216 217 218 219 | |
SumProductSemiring
¤
Bases: SemiringImpl
The linear space computation.
Source code in cirkit/backend/torch/semiring.py
302 303 304 305 306 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 | |
add(*xs)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
328 329 330 | |
apply_reduce(func, *xs, dim, keepdim)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
342 343 344 345 346 347 348 349 350 | |
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
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | |
mul(*xs)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
338 339 340 | |
prod(x, /, *, dim=None, keepdim=False)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
332 333 334 335 336 | |
sum(x, /, *, dim=None, keepdim=False)
classmethod
¤
Source code in cirkit/backend/torch/semiring.py
324 325 326 | |
_(x)
¤
Source code in cirkit/backend/torch/semiring.py
526 527 528 | |