finch
AssemblyContext
Bases: Context
Compiles Finch Notation to Finch Assembly. Holds the state of the compilation process.
Source code in src/finch/compile/lower.py
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 415 416 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 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 | |
__call__(prgm)
__call__(prgm: ntn.Module) -> asm.Module
__call__(prgm: ntn.Literal) -> asm.Literal
__call__(prgm: ntn.Variable) -> asm.Variable
__call__(
prgm: ntn.NotationExpression,
) -> asm.AssemblyExpression
__call__(prgm: ntn.NotationStatement) -> None
Lower Finch Notation to Finch Assembly. First we check for early simplifications, then we call the normal lowering for the outermost node.
Source code in src/finch/compile/lower.py
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 | |
block()
Create a new block. Preambles and epilogues will stay within this block. This is used to create a new context for compiling a block of code.
Source code in src/finch/compile/lower.py
445 446 447 448 449 450 451 452 453 454 455 456 | |
scope()
Create a new scoped context that inherits from this one.
Source code in src/finch/compile/lower.py
458 459 460 461 462 463 464 465 466 467 | |
should_halt()
Check if the current function should halt. This is used to determine if the function has returned.
Source code in src/finch/compile/lower.py
469 470 471 472 473 474 475 | |
BufferizedNDArray
Bases: OverrideTensor
Source code in src/finch/tensor/bufferized_ndarray.py
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 | |
element_type
property
Data type of the tensor elements.
fill_value
property
Default value to fill the tensor.
ftype
property
Returns the ftype of the buffer, which is a BufferizedNDArrayFType.
shape_type
property
Shape type of the tensor.
__getitem__(index)
Get an item from the bufferized NDArray. This allows for indexing into the bufferized array.
Source code in src/finch/tensor/bufferized_ndarray.py
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 | |
__setitem__(index, value)
Set an item in the bufferized NDArray. This allows for indexing into the bufferized array.
Source code in src/finch/tensor/bufferized_ndarray.py
251 252 253 254 255 256 257 258 259 | |
declare(init, op, shape)
Declare a bufferized NDArray with the given initialization value, operation, and shape.
Source code in src/finch/tensor/bufferized_ndarray.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | |
to_numpy()
Convert the bufferized NDArray to a NumPy array. This is used to get the underlying NumPy array from the bufferized NDArray.
Source code in src/finch/tensor/bufferized_ndarray.py
78 79 80 81 82 83 | |
BufferizedNDArrayFType
Bases: FinchTensorFType, MutableStructFType
A ftype for bufferized NumPy arrays that provides metadata about the array. This includes the fill value, element type, and shape type.
Source code in src/finch/tensor/bufferized_ndarray.py
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 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 351 352 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 415 416 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 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 | |
__call__(val)
Convert a tensor to this bufferized ndarray type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
val
|
Any
|
A tensor to convert to this type. |
required |
Returns: A BufferizedNDArray instance of this type.
Source code in src/finch/tensor/bufferized_ndarray.py
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 | |
ChunkMaskTensor
Bases: PatternTensor
Map n indices to chunks of size b.
shape is (ceil(n / b), n), and row j holds the indices of chunk
j, so that the efficient row-major traversal visits each chunk and then
its indices. The last chunk may be shorter.
Source code in src/finch/tensor/patterns.py
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 | |
ConstantScalar
Bases: Scalar
A scalar whose value is treated as a compile-time constant. Kernels may
specialize on the value (and recompile per distinct value), unlike plain
Scalars, whose values are bound at call time.
Source code in src/finch/tensor/scalar.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
DC
dataclass
A degree constraint: value bounds ||deg(to_indices | from_indices)||_p.
p = inf (the default) is the classic max-degree constraint; p = 1 is
the distinct count; intermediate p capture skew between the two.
Source code in src/finch/autoschedule/tensor_stats/bound_stats.py
37 38 39 40 41 42 43 44 45 46 47 48 49 | |
DCStats
Bases: BoundStats
Classic degree constraints -- max degrees (p = inf) and distinct counts.
Estimates size with a best-first product search over the constraints,
clamped by the tensor's dense capacity.
Source code in src/finch/autoschedule/tensor_stats/bound_stats.py
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 | |
estimate_non_fill_values()
Smallest degree-constraint product covering all indices, clamped by dense capacity.
Source code in src/finch/autoschedule/tensor_stats/bound_stats.py
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 | |
DenseLevel
dataclass
Bases: SingleDimensionLevel
A class representing dense level.
Source code in src/finch/tensor/level/dense_level.py
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 | |
DenseLevelFType
dataclass
Bases: SingleDimensionLevelFType, MutableStructFType
Source code in src/finch/tensor/level/dense_level.py
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 | |
buffer_factory
property
Returns the ftype of the buffer used for the fibers.
element_type
property
Returns the type of elements stored in the fibers.
position_type
property
Returns the type of positions within the levels.
shape_type
property
Returns the type of the shape of the fibers.
__call__(val)
Convert a level to this dense level type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
val
|
Any
|
A value to convert to this type. |
required |
Returns: A DenseLevel instance of this type.
Source code in src/finch/tensor/level/dense_level.py
62 63 64 65 66 67 68 69 70 71 72 73 | |
construct(shape, *, pos)
Creates an instance of DenseLevel with the given ftype.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
tuple[Any, ...]
|
The shape to be used for the level. (mandatory) |
required |
Returns: An instance of DenseLevel.
Source code in src/finch/tensor/level/dense_level.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
ElementLevel
dataclass
Bases: Level
A class representing the leaf level of Finch tensors.
Source code in src/finch/tensor/level/element_level.py
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 353 354 | |
ElementLevelFType
dataclass
Bases: LevelFType, MutableStructFType
Source code in src/finch/tensor/level/element_level.py
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 | |
__call__(val)
Convert a level to this element level type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
val
|
Any
|
A value to convert to this type. |
required |
Returns: An ElementLevel instance of this type.
Source code in src/finch/tensor/level/element_level.py
106 107 108 109 110 111 112 113 114 115 116 117 | |
construct(shape, *, pos)
Creates an instance of ElementLevel with the given ftype.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
tuple[Any, ...]
|
Should be always |
required |
Returns: An instance of ElementLevel.
Source code in src/finch/tensor/level/element_level.py
92 93 94 95 96 97 98 99 100 101 102 103 104 | |
Extent
dataclass
Bases: FTyped[ExtentFType]
A class to represent the extent of a loop variable. This is used to define the start and end values of a loop.
The extent is start-inclusive and end-exclusive, meaning that the loop variable will take on values from start to end-1.
Source code in src/finch/compile/lower.py
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 | |
ExtentFType
dataclass
Bases: ImmutableStructFType
Source code in src/finch/compile/lower.py
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 | |
lower_loop(ctx, idx, ext, body)
Lower a loop with the given index and body. This is used to compile the loop into assembly.
Source code in src/finch/compile/lower.py
134 135 136 137 138 139 140 141 142 143 144 145 146 | |
FType
Bases: ABC
Source code in src/finch/algebra/ftypes.py
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 | |
__call__(val)
abstractmethod
Perform type conversion or construction with the given value.
For numeric/scalar types, this converts the value to this type. For composite types, this may construct an instance of this type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
val
|
Any
|
The value to convert or use for construction |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The converted/constructed value |
Source code in src/finch/algebra/ftypes.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | |
fisinstance(other)
Check if other is an instance of this ftype.
Source code in src/finch/algebra/ftypes.py
48 49 50 51 52 | |
FTyped
Bases: Generic[FT]
Abstract base class for objects that can be formatted.
Source code in src/finch/algebra/ftypes.py
708 709 710 711 712 713 714 715 716 717 718 719 | |
ftype
abstractmethod
property
The ftype of the object.
FiberTensor
dataclass
Bases: OverrideTensor
A class representing a tensor with fiber structure.
Attributes:
| Name | Type | Description |
|---|---|---|
lvl |
Level
|
a fiber allocator that manages the fibers in the tensor. |
Source code in src/finch/tensor/fiber_tensor.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 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 | |
ftype
property
Returns the ftype of the fiber tensor, which is a FiberTensorFType.
FiberTensorFType
dataclass
Bases: FinchTensorFType, ImmutableStructFType
An abstract base class representing the ftype of a fiber tensor.
Attributes:
| Name | Type | Description |
|---|---|---|
lvl_t |
LevelFType
|
The level ftype to be used for the tensor. |
Source code in src/finch/tensor/fiber_tensor.py
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 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 351 352 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 415 416 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 | |
__call__(val)
Convert a tensor to this fiber tensor type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
val
|
Any
|
A tensor to convert to this type. |
required |
Returns: A FiberTensor instance of this type.
Source code in src/finch/tensor/fiber_tensor.py
346 347 348 349 350 351 352 353 354 355 356 357 | |
construct(shape, fill_value=None)
Creates an instance of a FiberTensor with the given arguments.
Source code in src/finch/tensor/fiber_tensor.py
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | |
with_fill(fill_type)
Rebuild this ftype with the leaf fill replaced.
Source code in src/finch/tensor/fiber_tensor.py
317 318 319 | |
FillTensor
Bases: OverrideTensor
A tensor that has a specific shape but contains no actual data. Used primarily for broadcasting operations where a tensor of a specific shape is needed but the values are irrelevant.
Source code in src/finch/tensor/patterns.py
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 | |
element_type
property
Data type of the tensor's elements.
fill_value
property
Default fill value.
shape_type
property
Shape type of the tensor.
IndexTensor
Bases: OverrideTensor
A tensor that has a specific shape and returns the linear (flattened) index used to access it.
Source code in src/finch/tensor/patterns.py
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 | |
element_type
property
Data type of the tensor's elements.
fill_value
property
Default fill value.
shape_type
property
Shape type of the tensor.
LazyTensor
Bases: OverrideTensor
Source code in src/finch/interface/lazy.py
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 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 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 | |
element_type
property
Data type of the tensor elements.
fill_value
property
Default value to fill the tensor.
shape
property
Returns the shape of the LazyTensor as a tuple. The shape is determined by the data and is a static property.
shape_type
property
Shape type of the tensor.
__bool__()
Converts the LazyTensor to a bool. This is a no-op for LazyTensors, as they are symbolic representations.
Source code in src/finch/interface/lazy.py
362 363 364 365 366 367 368 369 | |
__complex__()
Converts the LazyTensor to a complex number. This is a no-op for LazyTensors, as they are symbolic representations.
Source code in src/finch/interface/lazy.py
335 336 337 338 339 340 341 342 | |
__float__()
Converts the LazyTensor to a float. This is a no-op for LazyTensors, as they are symbolic representations.
Source code in src/finch/interface/lazy.py
344 345 346 347 348 349 350 351 | |
__int__()
Converts the LazyTensor to an int. This is a no-op for LazyTensors, as they are symbolic representations.
Source code in src/finch/interface/lazy.py
353 354 355 356 357 358 359 360 | |
NumPyFType
Bases: TensorFType
Source code in src/finch/tensor/np_wrapper.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 | |
__call__(val)
Convert a tensor to this numpy tensor type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
val
|
NumPyWrapper
|
A tensor to convert to this type. |
required |
Returns: A NumPyWrapper instance of this type.
Source code in src/finch/tensor/np_wrapper.py
43 44 45 46 47 48 49 50 51 52 53 54 | |
NumPyWrapper
Bases: Tensor
Source code in src/finch/tensor/np_wrapper.py
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 | |
element_type
property
Data type of the tensor's elements.
fill_value
property
Default fill value.
shape_type
property
Shape type of the tensor.
NumpyBuffer
Bases: Buffer
A buffer that uses NumPy arrays to store data. This is a concrete implementation of the Buffer class.
Source code in src/finch/codegen/buffers/numpy_buffer.py
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 | |
ftype
property
Returns the ftype of the buffer, which is a NumpyBufferFType.
NumpyBufferFType
Bases: CBufferFType, NumbaBufferFType, MLIRBufferFType, CUnpackableFType
A ftype for buffers that uses NumPy arrays. This is a concrete implementation of the BufferFType class.
Source code in src/finch/codegen/buffers/numpy_buffer.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 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 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 351 352 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 415 416 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 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 | |
element_type
property
Returns the type of elements stored in the buffer. This is typically the same as the dtype used to create the buffer.
length_type
property
Returns the type used for the length of the buffer.
c_repack(ctx, lhs, obj)
Repack the buffer from C context.
Source code in src/finch/codegen/buffers/numpy_buffer.py
209 210 211 212 213 214 215 216 217 | |
c_unpack(ctx, var_n, val)
Unpack the buffer into C context.
Source code in src/finch/codegen/buffers/numpy_buffer.py
194 195 196 197 198 199 200 201 202 203 204 205 206 207 | |
construct_from_c(c_buffer)
Construct a NumpyBuffer from a C-compatible structure.
Source code in src/finch/codegen/buffers/numpy_buffer.py
236 237 238 239 240 | |
construct_from_numba(numba_buffer)
Construct a NumpyBuffer from a Numba-compatible object.
Source code in src/finch/codegen/buffers/numpy_buffer.py
308 309 310 311 312 | |
deserialize_from_c(obj, c_buffer)
Update this buffer based on how the C call modified the CNumpyBuffer structure.
Source code in src/finch/codegen/buffers/numpy_buffer.py
230 231 232 233 | |
numba_repack(ctx, lhs, obj)
Repack the buffer from Numba context.
Source code in src/finch/codegen/buffers/numpy_buffer.py
291 292 293 294 295 296 | |
numba_unpack(ctx, var_n, val)
Unpack the buffer into Numba context.
Source code in src/finch/codegen/buffers/numpy_buffer.py
282 283 284 285 286 287 288 289 | |
serialize_to_c(obj)
Serialize the NumPy buffer to a C-compatible structure.
Source code in src/finch/codegen/buffers/numpy_buffer.py
219 220 221 222 223 224 225 226 227 228 | |
serialize_to_numba(obj)
Serialize the NumPy buffer to a Numba-compatible object.
Source code in src/finch/codegen/buffers/numpy_buffer.py
298 299 300 301 302 | |
OverrideTensor
Bases: Tensor
Source code in src/finch/tensor/override_tensor.py
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 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 351 352 353 354 355 356 357 | |
__array_function__(func, types, args, kwargs)
Override NumPy functions using the array_function protocol.
Source code in src/finch/tensor/override_tensor.py
99 100 101 102 103 104 105 | |
__array_ufunc__(ufunc, method, *inputs, **kwargs)
Override NumPy ufuncs using the array_ufunc protocol.
Source code in src/finch/tensor/override_tensor.py
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 | |
__bool__()
Converts a zero-dimensional array to a Python bool object.
Source code in src/finch/tensor/override_tensor.py
300 301 302 303 304 305 306 307 | |
__complex__()
Converts a zero-dimensional array to a Python complex object.
Source code in src/finch/tensor/override_tensor.py
273 274 275 276 277 278 279 280 | |
__float__()
Converts a zero-dimensional array to a Python float object.
Source code in src/finch/tensor/override_tensor.py
282 283 284 285 286 287 288 289 | |
__int__()
Converts a zero-dimensional array to a Python int object.
Source code in src/finch/tensor/override_tensor.py
291 292 293 294 295 296 297 298 | |
RandomMaskTensor
Bases: PatternTensor
A reproducible random mask with true probability p and no stored entries.
shape may be an integer for a vector, or a tuple (including ()).
An omitted seed draws one random 64-bit seed from rng (a new NumPy
generator by default). Reads consume no randomness; a fixed seed and
coordinates give the same value even if the shape changes.
Like Finch.jl, indexed masks mix the seed, then each one-based coordinate
from the first axis to the last, which is Finch.jl's order once the axes
are reversed like other tensors. Scalar masks use the seed directly.
The high 53 bits give a value in [0, 1) that is compared with p.
Source code in src/finch/tensor/patterns.py
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 | |
RepeatTensor
Bases: PatternTensor
Map each of n indices to k repeated indices.
shape is (n, n * k), and row i holds the repeats j of i,
so that the efficient row-major traversal visits each index and then its
repeats. For k <= 0, all entries are false.
Source code in src/finch/tensor/patterns.py
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 | |
Scalar
Bases: OverrideTensor
Source code in src/finch/tensor/scalar.py
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 | |
element_type
property
Data type of the scalar.
fill_value
property
Default value to fill the scalar.
shape_type
property
Shape type of the scalar.
ScalarFType
Bases: TensorFType, ImmutableStructFType
Source code in src/finch/tensor/scalar.py
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 | |
__call__(val)
Convert a tensor to this scalar tensor type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
val
|
Any
|
A value to convert to this type. |
required |
Returns: A Scalar instance of this type.
Source code in src/finch/tensor/scalar.py
54 55 56 57 58 59 60 61 62 63 64 65 | |
SparseListLevel
dataclass
Bases: SingleDimensionLevel
A class representing sparse list level.
Source code in src/finch/tensor/level/sparse_list_level.py
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 | |
SparseListLevelFType
dataclass
Bases: SingleDimensionLevelFType, MutableStructFType
Source code in src/finch/tensor/level/sparse_list_level.py
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 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 351 352 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 415 416 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 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 | |
buffer_factory
property
Returns the ftype of the buffer used for the fibers.
element_type
property
Returns the type of elements stored in the fibers.
position_type
property
Returns the type of positions within the levels.
shape_type
property
Returns the type of the shape of the fibers.
__call__(val)
Convert a level to this sparse list level type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
val
|
Any
|
A value to convert to this type. |
required |
Returns: A SparseListLevel instance of this type.
Source code in src/finch/tensor/level/sparse_list_level.py
138 139 140 141 142 143 144 145 146 147 148 149 | |
construct(shape, *, pos)
Creates an instance of SparseListLevel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
tuple[Any, ...]
|
The shape to be used for the level. (mandatory) |
required |
Returns: An instance of DenseLevel.
Source code in src/finch/tensor/level/sparse_list_level.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | |
SplitMaskTensor
Bases: PatternTensor
Partition n indices into p contiguous regions of nearly equal size.
shape is (p, n), and row j covers n*j//p <= i < n*(j+1)//p,
so that the efficient row-major traversal visits each region and then its
indices.
Source code in src/finch/tensor/patterns.py
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 | |
Tensor
Bases: FTyped[TensorFType[FT]], ABC
Abstract base class for tensor-like data structures. Tensors are multi-dimensional arrays that can be used to represent data in various formats. They support operations such as indexing, slicing, and reshaping, and can be used in mathematical computations. This class provides the basic interface for tensors to be used with lazy ops in Finch, though more advanced interfaces may be required for different backends.
Source code in src/finch/algebra/tensor.py
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 | |
element_type
property
The element type of the tensor. The element type is the scalar type of the elements in a tensor, which may be different from the data type of the tensor.
fill
property
The fill of the tensor: its fill value, and whether kernels may specialize on it. Its ftype is the fill of the tensor's format.
fill_value
property
The fill value for the tensor. The fill value is the default value for a tensor when it is created with a given shape and dtype, as well as the background value for sparse tensors. Note: this is the raw value NOT the AbstractFill. A tensor whose fill is dynamic holds its own fill value.
ftype
abstractmethod
property
FType of the tensor, which may include metadata about the tensor.
ndim
property
Number of dimensions of the tensor.
shape
abstractmethod
property
Shape of the tensor.
shape_type
property
Shape type of the tensor. The shape type is a tuple of the index types in the tensor. It's the type of each element in tns.shape. It should be an actual tuple, rather than a tupleftype, so that it can hold e.g. dtypes, formats, or types, and so that we can easily index it.
item()
abstractmethod
Return the Python scalar stored in a zero-dimensional tensor.
Indexing tensor objects must keep returning tensor-like values, including for
zero-dimensional results. item() is the explicit escape hatch for code that
needs the underlying Python scalar value. Implementations should raise
ValueError when called on a non-scalar tensor.
Source code in src/finch/algebra/tensor.py
209 210 211 212 213 214 215 216 217 218 219 | |
to_numpy()
abstractmethod
Convert the tensor to a NumPy representation.
Source code in src/finch/algebra/tensor.py
221 222 223 224 | |
to_scipy()
abstractmethod
Convert the tensor to a SciPy sparse representation.
Source code in src/finch/algebra/tensor.py
226 227 228 229 | |
with_fill(fill_value)
An equivalent tensor carrying fill_value as its fill.
Source code in src/finch/algebra/tensor.py
159 160 161 162 163 164 165 | |
TensorFType
Bases: FType, ABC, Generic[FT]
Source code in src/finch/algebra/tensor.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 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 | |
element_type
abstractmethod
property
Data type of the tensor elements.
fill_type
abstractmethod
property
The fill of the format, either static or dynamic.
ndim
property
Number of dimensions of the tensor.
shape_type
abstractmethod
property
Shape type of the tensor. The shape type is a tuple of the index types in the tensor. It's the type of each element in tns.shape. It should be an actual tuple, rather than a tuple type, so that it can hold e.g. dtypes, formats, or types, and so that we can easily index it.
construct(shape)
abstractmethod
Create a tensor instance with the given shape.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
tuple
|
The shape of the tensor to create. |
required |
Returns: A tensor instance with the specified shape.
Source code in src/finch/algebra/tensor.py
89 90 91 92 93 94 95 96 97 98 99 | |
with_fill(fill_type)
This ftype carrying fill_type as its fill.
Source code in src/finch/algebra/tensor.py
54 55 56 57 58 59 60 | |
asarray(obj, /, *, dtype=None, device=None, copy=None, format=None)
asarray(
obj: np.ndarray,
/,
*,
dtype=None,
device=None,
copy=None,
format: None = None,
) -> BufferizedNDArray
asarray(
obj: sps.spmatrix | sps.sparray,
/,
*,
dtype=None,
device=None,
copy=None,
format: None = None,
) -> FiberTensor
asarray(
obj: Any,
/,
*,
dtype=None,
device=None,
copy=None,
format: FiberTensorFType,
) -> FiberTensor
asarray(
obj: np.generic | complex,
/,
*,
dtype=None,
device=None,
copy=None,
format: None = None,
) -> Scalar
asarray(
obj: Any,
/,
*,
dtype=None,
device=None,
copy=None,
format: TensorFType | None = None,
) -> Tensor
Convert given argument and return wrapper type instance. If input argument is already array type, return unchanged. https://data-apis.org/array-api/latest/API_specification/generated/array_api.asarray.html
Source code in src/finch/interface/lazy.py
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 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 | |
broadcast_arrays(*args)
Broadcasts one or more arrays against one another.
Parameters
*args: array an arbitrary number of to-be broadcasted arrays.
Returns
out: List[array] a list of broadcasted arrays. Each array has the same shape. Element types are preserved.
Source code in src/finch/interface/eager.py
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 | |
broadcast_to(x, /, shape)
Broadcasts an array to a new shape.
Parameters
x: array The input tensor to be broadcasted. shape: Sequence[int] The target shape to which the input tensor should be broadcasted.
Returns
out: array
A tensor with the same data as x, but with the specified shape.
Source code in src/finch/interface/eager.py
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 | |
compute(arg, ctx=None)
Executes a fused operation represented by LazyTensors. This function evaluates the entire operation in an optimized manner using the provided scheduler.
- arg: A lazy tensor or a tuple of lazy tensors representing the fused operation to be computed.
- ctx: The scheduler to use for computation. Defaults to the result of
get_default_scheduler().
Returns: - A tensor or a list of tensors computed by the fused operation.
Source code in src/finch/interface/fuse.py
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 | |
defer(arr)
defer(arr: tuple[Any, ...]) -> tuple[Any, ...]
defer(arr: Any) -> LazyTensor
- defer(arr) -> LazyTensor: Converts an array into a LazyTensor. If the input is already a LazyTensor, it is returned as-is. Otherwise, it creates a LazyTensor representation of the input array.
Parameters: - arr: The input array to be converted into a LazyTensor.
Returns: - LazyTensor: A lazy representation of the input array.
Source code in src/finch/interface/lazy.py
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 | |
einop(prgm, /, **kwargs)
Execute an einsum expression using the specified array framework.
This function parses and executes einsum-like expressions with extended syntax that supports various operations beyond traditional Einstein summation notation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prgm
|
str
|
Einsum program string specifying the computation. The syntax supports: - Assignment: "C[i,j] = A[i,j] + B[j,i]" - Increment: "C[i,j] += A[i,k] * B[k,j]" - Reductions: "C[i] += A[i,j]", "C[i] max= A[i,j]", "C[i] &= A[i,j]" - Arithmetic operations: +, -, *, /, //, %, ** - Comparison operations: ==, !=, <, <=, >, >= - Logical operations: and, or, not - Bitwise operations: &, |, ^, <<, >> - Function calls and complex expressions with parentheses - Mathematical functions: abs, sqrt, exp, log, sin, cos, tan, etc. - Literal values: integers, floats, booleans, and complex numbers - Python operator precedence and parentheses for grouping |
required |
**kwargs
|
Any
|
Named arrays referenced in the einsum expression. The keys should match the tensor names used in the program string. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
The result array from executing the einsum expression. |
Examples:
>>> rng = np.random.default_rng(42)
>>> A = rng.integers(0, 10, size=(3, 4))
>>> B = rng.integers(0, 10, size=(4, 3))
>>> # Matrix addition with transpose
>>> C = einop("C[i,j] = A[i,j] + B[j,i]", A=A, B=B)
>>> # Matrix multiplication
>>> D = einop("D[i,j] += A[i,k] * B[k,j]", A=A, B=B)
>>> # Min-Plus multiplication with shift
>>> E = einop("E[i] min= A[k,i] + D[k,j] << 1", A=A, D=D)
Source code in src/finch/interface/eager.py
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 | |
einsum(*args, **kwargs)
einsum(subscripts, *operands)
Evaluates the Einstein summation convention on the operands.
Using the Einstein summation convention, many common multi-dimensional,
linear algebraic array operations can be represented in a simple fashion.
In implicit mode einsum computes these values.
In explicit mode, einsum provides further flexibility to compute
other array operations that might not be considered classical Einstein
summation operations, by disabling, or forcing summation over specified
subscript labels.
See the notes and examples for clarification.
Parameters
subscripts : str Specifies the subscripts for summation as comma separated list of subscript labels. An implicit (classical Einstein summation) calculation is performed unless the explicit indicator '->' is included as well as subscript labels of the precise output form. operands : list of array_like These are the arrays for the operation.
Returns
output : ndarray The calculation based on the Einstein summation convention.
Notes
The Einstein summation convention can be used to compute
many multi-dimensional, linear algebraic array operations. einsum
provides a succinct way of representing these.
A non-exhaustive list of these operations,
which can be computed by einsum, is shown below along with examples:
- Trace of an array, :py:func:
numpy.trace. - Return a diagonal, :py:func:
numpy.diag. - Array axis summations, :py:func:
numpy.sum. - Transpositions and permutations, :py:func:
numpy.transpose. - Matrix multiplication and dot product, :py:func:
numpy.matmul:py:func:numpy.dot. - Vector inner and outer products, :py:func:
numpy.inner:py:func:numpy.outer. - Broadcasting, element-wise and scalar multiplication,
:py:func:
numpy.multiply. - Tensor contractions, :py:func:
numpy.tensordot. - Chained array operations, in efficient calculation order,
:py:func:
numpy.einsum_path.
The subscripts string is a comma-separated list of subscript labels,
where each label refers to a dimension of the corresponding operand.
Whenever a label is repeated it is summed, so np.einsum('i,i', a, b)
is equivalent to :py:func:np.inner(a,b) <numpy.inner>. If a label
appears only once, it is not summed, so np.einsum('i', a)
produces a view of a with no changes. A further example
np.einsum('ij,jk', a, b) describes traditional matrix multiplication
and is equivalent to :py:func:np.matmul(a,b) <numpy.matmul>.
Repeated subscript labels in one operand take the diagonal.
For example, np.einsum('ii', a) is equivalent to
:py:func:np.trace(a) <numpy.trace>.
In implicit mode, the chosen subscripts are important
since the axes of the output are reordered alphabetically. This
means that np.einsum('ij', a) doesn't affect a 2D array, while
np.einsum('ji', a) takes its transpose. Additionally,
np.einsum('ij,jk', a, b) returns a matrix multiplication, while,
np.einsum('ij,jh', a, b) returns the transpose of the
multiplication since subscript 'h' precedes subscript 'i'.
In explicit mode the output can be directly controlled by
specifying output subscript labels. This requires the
identifier '->' as well as the list of output subscript labels.
This feature increases the flexibility of the function since
summing can be disabled or forced when required. The call
np.einsum('i->', a) is like :py:func:np.sum(a) <numpy.sum>
if a is a 1-D array, and np.einsum('ii->i', a)
is like :py:func:np.diag(a) <numpy.diag> if a is a square 2-D array.
The difference is that einsum does not allow broadcasting by default.
Additionally np.einsum('ij,jh->ih', a, b) directly specifies the
order of the output subscript labels and therefore returns matrix
multiplication, unlike the example above in implicit mode.
To enable and control broadcasting, use an ellipsis. Default
NumPy-style broadcasting is done by adding an ellipsis
to the left of each term, like np.einsum('...ii->...i', a).
np.einsum('...i->...', a) is like
:py:func:np.sum(a, axis=-1) <numpy.sum> for array a of any shape.
To take the trace along the first and last axes,
you can do np.einsum('i...i', a), or to do a matrix-matrix
product with the left-most indices instead of rightmost, one can do
np.einsum('ij...,jk...->ik...', a, b).
einsum also provides an alternative way to provide the subscripts and
operands as einsum(op0, sublist0, op1, sublist1, ..., [sublistout]).
If the output shape is not provided in this format einsum will be
calculated in implicit mode, otherwise it will be performed explicitly.
The examples below have corresponding einsum calls with the two
parameter methods.
Examples
a = np.arange(25).reshape(5, 5) b = np.arange(5) c = np.arange(6).reshape(2, 3)
Trace of a matrix:
np.einsum("ii", a) np.int64(60) np.einsum(a, [0, 0]) np.int64(60) np.trace(a) np.int64(60)
Extract the diagonal (requires explicit form):
np.einsum("ii->i", a) array([ 0, 6, 12, 18, 24]) np.einsum(a, [0, 0], [0]) array([ 0, 6, 12, 18, 24]) np.diag(a) array([ 0, 6, 12, 18, 24])
Sum over an axis (requires explicit form):
np.einsum("ij->i", a) array([ 10, 35, 60, 85, 110]) np.einsum(a, [0, 1], [0]) array([ 10, 35, 60, 85, 110]) np.sum(a, axis=1) array([ 10, 35, 60, 85, 110])
For higher dimensional arrays summing a single axis can be done with ellipsis:
np.einsum("...j->...", a) array([ 10, 35, 60, 85, 110]) np.einsum(a, [Ellipsis, 1], [Ellipsis]) array([ 10, 35, 60, 85, 110])
Compute a matrix transpose, or reorder any number of axes:
np.einsum("ji", c) array([[0, 3], [1, 4], [2, 5]]) np.einsum("ij->ji", c) array([[0, 3], [1, 4], [2, 5]]) np.einsum(c, [1, 0]) array([[0, 3], [1, 4], [2, 5]]) np.transpose(c) array([[0, 3], [1, 4], [2, 5]])
Vector inner products:
np.einsum("i,i", b, b) np.int64(30) np.einsum(b, [0], b, [0]) np.int64(30) np.inner(b, b) np.int64(30)
Matrix vector multiplication:
np.einsum("ij,j", a, b) array([ 30, 80, 130, 180, 230]) np.einsum(a, [0, 1], b, [1]) array([ 30, 80, 130, 180, 230]) np.dot(a, b) array([ 30, 80, 130, 180, 230]) np.einsum("...j,j", a, b) array([ 30, 80, 130, 180, 230])
Broadcasting and scalar multiplication:
np.einsum("..., ...", 3, c) array([[ 0, 3, 6], [ 9, 12, 15]]) np.einsum(",ij", 3, c) array([[ 0, 3, 6], [ 9, 12, 15]]) np.einsum(3, [Ellipsis], c, [Ellipsis]) array([[ 0, 3, 6], [ 9, 12, 15]]) np.multiply(3, c) array([[ 0, 3, 6], [ 9, 12, 15]])
Vector outer product:
np.einsum("i,j", np.arange(2) + 1, b) array([[0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]) np.einsum(np.arange(2) + 1, [0], b, [1]) array([[0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]) np.outer(np.arange(2) + 1, b) array([[0, 1, 2, 3, 4], [0, 2, 4, 6, 8]])
Tensor contraction:
a = np.arange(60.0).reshape(3, 4, 5) b = np.arange(24.0).reshape(4, 3, 2) np.einsum("ijk,jil->kl", a, b) array([[4400., 4730.], [4532., 4874.], [4664., 5018.], [4796., 5162.], [4928., 5306.]]) np.einsum(a, [0, 1, 2], b, [1, 0, 3], [2, 3]) array([[4400., 4730.], [4532., 4874.], [4664., 5018.], [4796., 5162.], [4928., 5306.]]) np.tensordot(a, b, axes=([1, 0], [0, 1])) array([[4400., 4730.], [4532., 4874.], [4664., 5018.], [4796., 5162.], [4928., 5306.]])
Example of ellipsis use:
a = np.arange(6).reshape((3, 2)) b = np.arange(12).reshape((4, 3)) np.einsum("ki,jk->ij", a, b) array([[10, 28, 46, 64], [13, 40, 67, 94]]) np.einsum("ki,...k->i...", a, b) array([[10, 28, 46, 64], [13, 40, 67, 94]]) np.einsum("k...,jk", a, b) array([[10, 28, 46, 64], [13, 40, 67, 94]])
Source code in src/finch/interface/eager.py
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 | |
element(fill_type=None, element_type=None, position_type=None, buffer_factory=NumpyBufferFType, buffer_type=None)
Creates an ElementLevelFType with the given parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fill_type
|
Any
|
The fill of the level's format, or the value of a static fill. |
None
|
element_type
|
FType | None
|
The type of elements stored in the level. |
None
|
position_type
|
FType | None
|
The type of positions within the level. |
None
|
buffer_factory
|
Any
|
The factory used to create buffers for the level. |
NumpyBufferFType
|
buffer_type
|
Any
|
Format of the value stored in the level. |
None
|
Returns:
| Type | Description |
|---|---|
ElementLevelFType
|
An instance of ElementLevelFType. |
Source code in src/finch/tensor/level/element_level.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 305 306 307 | |
fisinstance(x, f)
Check if x is an instance of f.
Source code in src/finch/algebra/ftypes.py
945 946 947 948 949 | |
from_binsparse(array)
Convert a Binsparse tensor from the reference binsparse package into a
Finch tensor which shares its buffers wherever possible.
Dense levels become one Finch DenseLevel per dimension, rank-1 sparse
levels become SparseListLevel, and higher-rank sparse levels become
SparseCOOLevel.
Source code in src/finch/fileio/binsparse.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 | |
ftype(x)
ftype(x: FinchOperator) -> FinchOperatorFType
ftype(x: Tensor) -> TensorFType
ftype(x: tuple) -> TupleFType
ftype(x: NumpyBuffer) -> NumpyBufferFType
ftype(x: np.integer) -> FDTypeInteger
ftype(x: FT) -> FT
ftype(x: FTyped[FT]) -> FT
ftype(x: Any) -> FType
Return the corresponding FType for a given object. Recognizes numpy, Python builtins, and Python tuples. Sometimes recognizes types. Override .ftype to customize the ftype of an object.
Source code in src/finch/algebra/ftypes.py
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 | |
fused(f, /, ctx=None)
- fused(f):
A decorator that marks a function as fused. This allows the function to be used with
the
fusefunction for automatic fusion of operations.
Parameters: - f: The function to be marked as fused.
Returns: - A wrapper function that applies the fusion mechanism to the original function.
Source code in src/finch/interface/fuse.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |
isdtype(dtype, kind)
Check if a dtype belongs to an Array API dtype kind.
Source code in src/finch/algebra/ftypes.py
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 | |
jit(f, /, ctx=None)
A decorator that marks a function for just-in-time compilation. This allows the function to be compiled and optimized for performance when called.
- f: The function to be marked for JIT compilation. This function can use basic python control flow and operations (e.g. while, for, if). However, it shouldn't use more complex features like generators, classes, or recursion.
- ctx: The scheduler to use for computation. Defaults to the result of
get_default_scheduler().
- A transformed function that inserts defer and compute statements to do tracing and optimization.
Example usage: @jit def my_function(A, B, C): D = A @ B while some_condition(D): D = D + C return D
In this example, my_function will be transformed to include defer and compute
statements, allowing it to be optimized and executed efficiently when called.
def opt_my_function(A, B, C):
A, B = defer(A), defer(B)
D = A @ B
D = compute(D)
while some_condition(D):
C = defer(C)
D = D + C
D = compute(D)
D = compute(D)
return D
Source code in src/finch/finch_fused/jit.py
7 8 9 10 11 12 13 14 15 16 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 | |
matmul(x1, x2)
Computes the matrix product.
Returns a LazyTensor if either x1 or x2 is a LazyTensor. Otherwise, computes the result eagerly.
Source code in src/finch/interface/eager.py
431 432 433 434 435 436 437 438 439 440 441 | |
matrix_transpose(x)
Computes the transpose of a matrix or stack of matrices.
Source code in src/finch/interface/eager.py
444 445 446 447 448 449 450 | |
moveaxis(x, source, destination)
Moves array axes (dimensions) to new positions, while leaving other axes in their original positions.
Args
- x (array) - input array.
- source - Axes to move.
- destination - indices defining the desired positions for each respective source axis index.
Returns
- out (array) - an array containing reordered axes.
Source code in src/finch/interface/eager.py
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 | |
tensordot(x1, x2, /, *, axes=2)
Computes the tensordot operation.
Returns a LazyTensor if either x1 or x2 is a LazyTensor. Otherwise, computes the result eagerly.
Source code in src/finch/interface/eager.py
994 995 996 997 998 999 1000 1001 1002 1003 | |
to_binsparse(tns)
Convert a Finch tensor to a Binsparse tensor which shares its buffers.
Source code in src/finch/fileio/binsparse.py
52 53 54 55 56 57 58 59 60 61 | |
vecdot(x1, x2, /, *, axis=-1)
Computes the (vector) dot product of two arrays.
Parameters
x1: array The first input tensor. x2: array The second input tensor. axis: int, optional The axis along which to compute the dot product. Default is -1 (last axis).
Returns
out: array
A tensor containing the dot product of x1 and x2 along the specified axis.
Source code in src/finch/interface/eager.py
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 | |