sgn.base
¶
Base classes for building a graph of elements and pads.
ElementLike
dataclass
¶
Bases: UniqueID
flowchart TD
sgn.base.ElementLike[ElementLike]
sgn.base.UniqueID[UniqueID]
sgn.base.UniqueID --> sgn.base.ElementLike
click sgn.base.ElementLike href "" "sgn.base.ElementLike"
click sgn.base.UniqueID href "" "sgn.base.UniqueID"
A basic container to hold source and sink pads. The assumption is that this will be a base class for code that actually does something. It should never be subclassed directly, instead subclass SourceElement, SinkElement or TransformElement.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_pads
|
list[SourcePad]
|
list, optional, The list of SourcePad objects. This must be given for SourceElements or TransformElements |
list()
|
sink_pads
|
list[SinkPad]
|
list, optional, The list of SinkPad objects. This must be given for SinkElements or TransformElements |
list()
|
Note
Subclasses can customize pad configuration by setting class-level attributes: - static_sink_pads/static_source_pads: Pads that are always present on this element type. - allow_dynamic_sink_pads/allow_dynamic_source_pads: Boolean flags controlling whether users can provide additional pad names at instantiation. If False, only static_pads are allowed (fully fixed). If True, user-provided pads are combined with static_pads.
Source code in src/sgn/base.py
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 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 | |
logger
property
¶
Return the logger scoped to this element, e.g. sgn.{name}.
pad_list
property
¶
Return a list of all pads.
sink_pad_dict
property
¶
Return a dictionary of sink pads with the pad name as the key.
source_pad_dict
property
¶
Return a dictionary of source pads with the pad name as the key.
__init_subclass__(**kwargs)
¶
Reject dataclass fields redeclared as bare class attributes.
use_threads and executor_pads are dataclass fields, so a
subclass must redeclare them with an annotation to change the
class default; a bare executor_pads = frozenset({"src"}) in the
class body is silently shadowed by the inherited field's instance
default and the element then dispatches the pads the author did not
choose. thread_safe next to them is a ClassVar, for which a bare
assignment is the correct idiom -- which is exactly what makes the
mistake easy to make, so catch it at class definition time.
Source code in src/sgn/base.py
__post_init__()
¶
Establish the graph attribute as an empty dictionary.
Source code in src/sgn/base.py
internal()
¶
on_startup()
¶
Called after the pipeline is fully constructed and validated, but before the first frame is processed. Override for setup that requires the full pipeline topology to be in place.
InternalPad
dataclass
¶
flowchart TD
sgn.base.InternalPad[InternalPad]
sgn.base.UniqueID[UniqueID]
sgn.base.PadLike[PadLike]
sgn.base.UniqueID --> sgn.base.InternalPad
sgn.base.PadLike --> sgn.base.InternalPad
click sgn.base.InternalPad href "" "sgn.base.InternalPad"
click sgn.base.UniqueID href "" "sgn.base.UniqueID"
click sgn.base.PadLike href "" "sgn.base.PadLike"
A pad that sits inside an element and is called between sink and source pads. Internal pads are connected in the elements internal graph according to the below (data flows top to bottom)
snk1 ... snkN (if exist) \ ... // internal (always exists) // ... \ src1 ... srcM (if exist)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
element
|
Element
|
Element, The Element instance associated with this pad |
required |
call
|
Callable
|
Callable, The function that will be called during graph execution for this pad |
required |
name
|
str
|
str, optional, The unique name for this object |
''
|
Source code in src/sgn/base.py
__call__()
async
¶
PadLike
dataclass
¶
Bases: ABC
flowchart TD
sgn.base.PadLike[PadLike]
click sgn.base.PadLike href "" "sgn.base.PadLike"
Pads are 1:1 with graph nodes but source and sink pads must be grouped into elements in order to exchange data from sink->source. source->sink exchanges happen between elements.
A pad must belong to an element and that element must be provided as a keyword argument called "element". The element must also provide a call function that will be executed when the pad is called. The call function must take a pad as an argument, e.g., def call(pad):
Developers should not subclass or use Pad directly. Instead use SourcePad or SinkPad.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
element
|
Element
|
Element, The Element instance associated with this pad |
required |
call
|
Callable
|
Callable, The function that will be called during graph execution for this pad |
required |
Source code in src/sgn/base.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 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 | |
pad_type
abstractmethod
property
¶
The pad's type string representation.
uses_executor
property
¶
True when an active pipeline executor would run this pad's callback.
Requires the element author's safety certificate (thread_safe),
the instance-level benefit opt-in (use_threads), and this pad's
type being in the element's executor_pads policy; does not check
whether an executor is actually installed. Snapshotted at pad
construction (the inputs are construction-time policy).
__call__()
abstractmethod
async
¶
The call method for a pad must be implemented by the element that the pad belongs to.
This method will be called when the pad is called in the graph.
Implementations must not await anything that can suspend except via
_dispatch. Pipeline's execution loops rely on that invariant to
await non-dispatching pads inline instead of wrapping each in a task;
a pad that suspends elsewhere still executes in DAG order, but
serializes its whole batch (and, in threaded mode, stops the inline
half of every generation from overlapping with the workers).
Source code in src/sgn/base.py
SinkElement
dataclass
¶
Bases: ABC, ElementLike, Generic[FrameLike]
flowchart TD
sgn.base.SinkElement[SinkElement]
sgn.base.ElementLike[ElementLike]
sgn.base.UniqueID[UniqueID]
sgn.base.ElementLike --> sgn.base.SinkElement
sgn.base.UniqueID --> sgn.base.ElementLike
click sgn.base.SinkElement href "" "sgn.base.SinkElement"
click sgn.base.ElementLike href "" "sgn.base.ElementLike"
click sgn.base.UniqueID href "" "sgn.base.UniqueID"
Sink element represents a terminal node in a pipeline, that typically writes data to disk, etc. Sink_pads must exist but not source_pads.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
str, optional, The unique name for this object |
''
|
sink_pad_names
|
Sequence[str]
|
list, optional, Set the list of sink pad names. These need to be unique for
an element but not for an application. The resulting full names will be
made with " |
list()
|
Source code in src/sgn/base.py
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 | |
at_eos
property
¶
If frames on any sink pads are End of Stream (EOS), then mark this whole element as EOS.
Returns:
| Type | Description |
|---|---|
bool
|
bool, True if any sink pad is at EOS, False otherwise |
__init_subclass__(**kwargs)
¶
__post_init__()
¶
Establish the sink pads and graph attributes.
Source code in src/sgn/base.py
mark_eos(pad)
¶
Marks a sink pad as receiving the End of Stream (EOS). The EOS marker signals that no more frames will be received on this pad.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pad
|
SinkPad
|
SinkPad, The sink pad that is receiving the EOS signal |
required |
Source code in src/sgn/base.py
pull(pad, frame)
abstractmethod
¶
Pull for a SinkElement represents the action of associating a frame with a particular input source pad a frame. This function must be provided by the subclass, and is where any "final" behavior must occur, e.g. writing to disk, etc.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pad
|
SinkPad
|
SinkPad, The sink pad that is receiving the frame |
required |
frame
|
FrameLike
|
Frame, The frame that is being received |
required |
Source code in src/sgn/base.py
SinkPad
dataclass
¶
flowchart TD
sgn.base.SinkPad[SinkPad]
sgn.base.UniqueID[UniqueID]
sgn.base.PadLike[PadLike]
sgn.base.UniqueID --> sgn.base.SinkPad
sgn.base.PadLike --> sgn.base.SinkPad
click sgn.base.SinkPad href "" "sgn.base.SinkPad"
click sgn.base.UniqueID href "" "sgn.base.UniqueID"
click sgn.base.PadLike href "" "sgn.base.PadLike"
A pad that receives a data Frame when called. When linked, it returns a dictionary suitable for building a graph in graphlib.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
element
|
Element
|
Element, The Element instance associated with this pad |
required |
call
|
Callable
|
Callable, The function that will be called during graph execution for this pad, takes two arguments, the pad and the frame |
required |
name
|
str
|
str, optional, The unique name for this object |
''
|
other
|
SourcePad | None
|
SourcePad, optional, This holds the source pad that is linked to this sink pad, default None |
None
|
input
|
Frame | None
|
Frame, optional, This holds the Frame provided by the linked source pad. Generally it gets set when this SinkPad is called, default None |
None
|
data_spec
|
DataSpec | None
|
DataSpec, optional, This holds a specification for the data stored in frames, and is expected to be consistent across frames passing through this pad. This is set when this sink pad is first called |
None
|
Source code in src/sgn/base.py
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 | |
__call__()
async
¶
When called, a sink pad gets a Frame from the linked source pad and then calls the element's provided call function.
Notes
Pad Call Order: pads must be called in the correct order such that the upstream sources have new information by the time call is invoked. This should be done within a directed acyclic graph such as those provided by the apps.Pipeline class.
Source code in src/sgn/base.py
link(other)
¶
Returns a dictionary of dependencies suitable for adding to a graphlib graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
SourcePad
|
SourcePad, The source pad to link to this sink pad |
required |
Notes
Many-to-one (source, sink) Not Supported: Only sink pads can be linked. A sink pad can be linked to only one source pad, but multiple sink pads may link to the same source pad. Calling link() on an already-linked sink pad raises ValueError.
Returns:
| Type | Description |
|---|---|
dict[Pad, set[Pad]]
|
dict[SinkPad, set[SourcePad]], a dictionary of dependencies suitable for |
dict[Pad, set[Pad]]
|
adding to a graphlib graph |
Source code in src/sgn/base.py
SourceElement
dataclass
¶
Bases: ABC, ElementLike
flowchart TD
sgn.base.SourceElement[SourceElement]
sgn.base.ElementLike[ElementLike]
sgn.base.UniqueID[UniqueID]
sgn.base.ElementLike --> sgn.base.SourceElement
sgn.base.UniqueID --> sgn.base.ElementLike
click sgn.base.SourceElement href "" "sgn.base.SourceElement"
click sgn.base.ElementLike href "" "sgn.base.ElementLike"
click sgn.base.UniqueID href "" "sgn.base.UniqueID"
Initialize with a list of source pads. Every source pad is added to the graph with no dependencies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
str, optional, The unique name for this object |
''
|
source_pad_names
|
Sequence[str]
|
list, optional, Set the list of source pad names. These need to be unique
for an element but not for an application. The resulting full names will be
made with " |
list()
|
Source code in src/sgn/base.py
__init_subclass__(**kwargs)
¶
__post_init__()
¶
Establish the source pads and graph attributes.
Source code in src/sgn/base.py
new(pad)
abstractmethod
¶
New frames are created on "pad". Must be provided by subclass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pad
|
SourcePad
|
SourcePad, The source pad through which the frame is passed |
required |
Returns:
| Type | Description |
|---|---|
Frame
|
Frame, The new frame to be passed through the source pad |
Source code in src/sgn/base.py
SourcePad
dataclass
¶
flowchart TD
sgn.base.SourcePad[SourcePad]
sgn.base.UniqueID[UniqueID]
sgn.base.PadLike[PadLike]
sgn.base.UniqueID --> sgn.base.SourcePad
sgn.base.PadLike --> sgn.base.SourcePad
click sgn.base.SourcePad href "" "sgn.base.SourcePad"
click sgn.base.UniqueID href "" "sgn.base.UniqueID"
click sgn.base.PadLike href "" "sgn.base.PadLike"
A pad that provides a data Frame when called.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
element
|
Element
|
Element, The Element instance associated with this pad |
required |
call
|
Callable
|
Callable, The function that will be called during graph execution for this pad |
required |
name
|
str
|
str, optional, The unique name for this object |
''
|
output
|
Frame | None
|
Frame, optional, This attribute is set to be the output Frame when the pad is called. |
None
|
Source code in src/sgn/base.py
__call__()
async
¶
When called, a source pad receives a Frame from the element that the pad belongs to.
Source code in src/sgn/base.py
TransformElement
dataclass
¶
Bases: ABC, ElementLike, Generic[FrameLike]
flowchart TD
sgn.base.TransformElement[TransformElement]
sgn.base.ElementLike[ElementLike]
sgn.base.UniqueID[UniqueID]
sgn.base.ElementLike --> sgn.base.TransformElement
sgn.base.UniqueID --> sgn.base.ElementLike
click sgn.base.TransformElement href "" "sgn.base.TransformElement"
click sgn.base.ElementLike href "" "sgn.base.ElementLike"
click sgn.base.UniqueID href "" "sgn.base.UniqueID"
Both "source_pads" and "sink_pads" must exist. The execution scheduling flow of the logic within a TransformElement is as follows: 1.) all sink pads, 2.) the internal pad, 3.) all source pads. The execution of all downstream logic will be blocked until logic in all upstream pads within the same TransformElement has exited.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
str, optional, The unique name for this object |
''
|
source_pad_names
|
Sequence[str]
|
list, optional, Set the list of source pad names. These need to be unique
for an element but not for an application. The resulting full names will
be made with " |
list()
|
sink_pad_names
|
Sequence[str]
|
list, optional, Set the list of sink pad names. These need to be unique
for an element but not for an application. The resulting full names will
be made with " |
list()
|
Source code in src/sgn/base.py
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 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 743 744 745 746 747 748 749 750 751 752 753 754 755 756 | |
__init_subclass__(**kwargs)
¶
Validate pad configuration at class definition time.
__post_init__()
¶
Establish the source pads and sink pads and graph attributes.
Source code in src/sgn/base.py
new(pad)
abstractmethod
¶
New frames are created on "pad". Must be provided by subclass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pad
|
SourcePad
|
SourcePad, The source pad through which the frame is passed |
required |
Returns:
| Type | Description |
|---|---|
FrameLike
|
Frame, The new frame to be passed through the source pad |
Source code in src/sgn/base.py
pull(pad, frame)
abstractmethod
¶
Pull data from the input pads (source pads of upstream elements), must be implemented by subclasses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pad
|
SinkPad
|
SinkPad, The sink pad that is receiving the frame |
required |
frame
|
FrameLike
|
Frame, The frame that is pulled from the source pad |
required |
Source code in src/sgn/base.py
UniqueID
dataclass
¶
Generic class from which all classes that participate in an execution graph should be derived. Enforces a unique name and hashes based on that name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
str, optional, The unique name for this object, defaults to the objects unique uuid4 hex string if not specified |
''
|
Source code in src/sgn/base.py
__eq__(other)
¶
__hash__()
¶
Compute the hash of the object based on the unique id.
Notes
Motivation: we need the Base class to be hashable, so that it can be used as a key in a dictionary, but mutable dataclasses are not hashable by default, so we have to define our own hash function here. Stability: As currently implemented, the hash of a UniqueID object will not be stable across python sessions, and should therefore not be used for checksum purposes.
Returns:
| Type | Description |
|---|---|
int
|
int, hash of the object |
Source code in src/sgn/base.py
__post_init__()
¶
Handle setup of the UniqueID class, including the ._id attribute.