generations_base
GenerationResultTypeVar
module-attribute
InputCollectionConstraint
Bases: StrEnum
Types of constraints for inputs collections.
Source code in horde_sdk/worker/generations_base.py
InputConstraintType
Bases: StrEnum
Types of constraints for inputs.
Source code in horde_sdk/worker/generations_base.py
HordeSingleGeneration
Bases: ABC
Base class for tracking a generation. Generations are specific instances of inference or computation.
This should not be confused with specific results, which are the output of a generation. For example, a generation could result in several images, but as the result of a single round of inference. The generation is the process of generating the images, while the images are the result of that generation.
See GENERATION_PROGRESS for the possible states a generation can be in.
Source code in horde_sdk/worker/generations_base.py
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 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 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 757 758 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 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 | |
result_type
property
The type of the result of the generation.
result_ids
property
The unique identifiers for the generations in the batch.
dispatch_result_ids
property
Identifiers supplied by the dispatch source, if any.
generation_parameters
instance-attribute
The parameters for the generation.
generation_failure_count
property
The number of times the generation has failed during any step of the generation process.
errored_states
property
Return a tuple of states which occurred just before an error state and the time they were set.
error_counts
property
Return a dictionary of states and the number of times they occurred before an error state.
requires_post_processing
property
Whether or not the generation requires post-processing.
requires_generation
property
Whether or not the generation requires generation.
requires_safety_check
property
Whether or not the generation requires a safety check.
generation_results
property
Get the result of the generation.
default_generate_progress_transitions
classmethod
default_generate_progress_transitions() -> (
Mapping[
GENERATION_PROGRESS, Iterable[GENERATION_PROGRESS]
]
)
Get the default generation progress transitions.
default_generate_progress_transitions_no_submit
classmethod
default_generate_progress_transitions_no_submit() -> (
Mapping[
GENERATION_PROGRESS, Iterable[GENERATION_PROGRESS]
]
)
Get the default generation progress transitions without submission.
Source code in horde_sdk/worker/generations_base.py
default_interrupt_states
classmethod
Get the default interrupt states.
does_class_require_generation
classmethod
does_class_require_safety_check
classmethod
register_callback
Register a callback to be called when the generation state is updated.
Parameters:
-
state(GENERATION_PROGRESS) –The state to register the callback for.
-
callback(Callable[[], None]) –The callback to call when the state is updated.
Source code in horde_sdk/worker/generations_base.py
__init__
__init__(
*,
generation_parameters: CompositeParametersBase,
result_type: (
type[GenerationResultTypeVar] | None
) = None,
generation_id: ID_TYPES | None = None,
dispatch_result_ids: Sequence[ID_TYPES] | None = None,
result_ids: list[ID_TYPES] | None = None,
requires_generation: bool = True,
requires_post_processing: bool = False,
requires_safety_check: bool = True,
requires_submit: bool = True,
safety_rules: SafetyRules = default_safety_rules,
state_error_limits: (
Mapping[GENERATION_PROGRESS, int] | None
) = HordeWorkerConfigDefaults.DEFAULT_STATE_ERROR_LIMITS,
generate_progress_transitions: Mapping[
GENERATION_PROGRESS, Iterable[GENERATION_PROGRESS]
] = base_generate_progress_transitions,
black_box_mode: bool = False,
strict_transition_mode: bool = True,
extra_logging: bool = True
) -> None
Initialize the generation.
Parameters:
-
generation_parameters(ComposedParameterSetBase) –The parameters for the generation.
-
result_type(type, default:None) –The type of the result of the generation.
-
generation_id(str | None, default:None) –The unique identifier for the generation. If None, a new UUID will be generated.
-
dispatch_result_ids(Sequence[str | UUID] | None, default:None) –Identifiers provided by the dispatch system for each result slot. Defaults to None when the generation has no remote counterpart yet.
-
result_ids(list[ID_TYPES] | None, default:None) –The unique identifiers for the results of the generation. If None, a new UUID will be generated for each generation in the batch.
-
requires_generation(bool, default:True) –Whether or not the generation requires generation (inference, etc). Defaults to True.
-
requires_post_processing(bool, default:False) –Whether or not the generation requires post-processing. Defaults to False.
-
requires_safety_check(bool, default:True) –Whether or not the generation requires a safety check. Defaults to True.
-
requires_submit(bool, default:True) –Whether or not the generation requires submission. Defaults to True.
-
safety_rules(SafetyRules, default:default_safety_rules) –The rules to use for safety checking. Defaults to
default_safety_rulesfromhorde_sdk.safety. -
state_error_limits(Mapping[GENERATION_PROGRESS, int], default:DEFAULT_STATE_ERROR_LIMITS) –The maximum number of times a generation can be in an error state before it is considered failed. Defaults to None.
-
generate_progress_transitions(dict[GenerationProgress, list[GenerationProgress]], default:base_generate_progress_transitions) –The transitions that are allowed between generation states. Defaults to
base_generate_progress_transitions(found in consts.py). -
black_box_mode(bool, default:False) –Whether the generation is in black box mode. This removes all of the intermediate states between starting and finished states. This should only be used when the backend has no observability into the generation process. Defaults to False.
-
strict_transition_mode(bool, default:True) –Whether or not to enforce strict transition checking. This prevents setting the same state multiple times in a row. Defaults to True.
-
extra_logging(bool, default:True) –Whether or not to enable extra debug-level logging, especially for state transitions. Defaults to True.
Raises:
-
ValueError–If result_type is None.
-
ValueError–If the result type is not iterable but the batch size is greater than 1.
-
ValueError–If result_ids is not None and its length does not match the batch size.
-
ValueError–If generate_progress_transitions is None.
-
ValueError–If the generation class requires generation but requires_generation is False.
-
ValueError–If the generation class requires a safety check but requires_safety_check is False.
Source code in horde_sdk/worker/generations_base.py
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 | |
set_dispatch_result_ids
Bind the generation to the result identifiers supplied by dispatch.
Source code in horde_sdk/worker/generations_base.py
get_generation_progress
get_progress_history
get_last_non_error_state
Get the last non-error state.
Source code in horde_sdk/worker/generations_base.py
get_last_non_error_state_and_time
Get the last non-error state and the time it was set.
Source code in horde_sdk/worker/generations_base.py
is_next_state_valid
Check if the next state is valid.
Source code in horde_sdk/worker/generations_base.py
on_error
on_error(
*,
failed_message: str,
failure_exception: Exception | None = None
) -> GENERATION_PROGRESS
Call when an error occurs at any point in the generation process, safety checks, or submission.
This should be reserved for errors which make the current step impossible to complete. For example, if the a sub-process is OOM killed.
Contrast with the add_metadata_entry method, which is used for non-critical errors. If there is no
METADATA_TYPE for the error you encountered, you can use failed_message and failure_exception instead.
Parameters:
-
failed_message(str) –The reason the generation failed.
-
failure_exception(Exception, default:None) –The exception that caused the generation to fail. Defaults to None.
Returns:
-
GENERATION_PROGRESS(GENERATION_PROGRESS) –The new state of the generation, which will be set to
GENERATION_PROGRESS.ERROR.
Raises:
-
RuntimeError–If the generation has exceeded the maximum number of errors for the current state.
-
RuntimeError–If the generation is in an error state and has no previous state to transition from.
Source code in horde_sdk/worker/generations_base.py
on_abort
on_abort(
*,
failed_message: str,
failure_exception: Exception | None = None
) -> GENERATION_PROGRESS
Call when the generation is aborted.
Parameters:
-
failed_message(str) –The reason the generation was aborted.
-
failure_exception(Exception, default:None) –The exception that caused the generation to be aborted. Defaults to None.
Returns:
-
GENERATION_PROGRESS(GENERATION_PROGRESS) –The new state of the generation, which will be set to
GENERATION_PROGRESS.ABORTED.
Raises:
-
RuntimeError–If the generation has exceeded the maximum number of errors for the current state.
-
RuntimeError–If the generation is in an error state and has no previous state to transition from.
Source code in horde_sdk/worker/generations_base.py
on_preloading
on_preloading_complete
on_generation_work_complete
on_generation_work_complete(
result: (
GenerationResultTypeVar
| Collection[GenerationResultTypeVar]
| None
) = None,
) -> GENERATION_PROGRESS
Call when the generation work is complete, such as when inference is done.
This does not mean the generation is finalized, as calling this function means that safety checks and submission may still be pending. Examples of when this function would be called are when comfyui has just returned an image, interrogating an image has just completed or when a text backend has just generated text.
Source code in horde_sdk/worker/generations_base.py
on_generating
on_post_processing
on_post_processing_complete
on_pending_safety_check
Call when the generation is pending safety check.
set_work_result
set_work_result(
result: (
GenerationResultTypeVar
| Collection[GenerationResultTypeVar]
),
) -> None
Set the result of the generation work.
Parameters:
-
result(Any) –The result of the generation work.
Source code in horde_sdk/worker/generations_base.py
on_complete
on_state
Call when the generation state is updated.
Parameters:
-
state(GENERATION_PROGRESS) –The new state of the generation.
Source code in horde_sdk/worker/generations_base.py
step
Call when the generation state is updated.
Parameters:
-
state(GENERATION_PROGRESS) –The new state of the generation.
is_safety_checking_done_on_all_batch
Check if the safety check is being done on a one-on-all basis.
Returns:
-
bool(bool) –True if the safety check is being done on a one-on-all basis, False otherwise.
Source code in horde_sdk/worker/generations_base.py
get_safety_check_results
Get the results of the safety checks.
Returns:
-
list[SafetyResult | None]–list[SafetyResult | None]: The results of the safety checks for each batch.
Source code in horde_sdk/worker/generations_base.py
on_safety_checking
on_safety_check_complete
Call when the safety check is complete.
Parameters:
-
batch_index(int) –The index of the batch to set the safety check result for. This is 0-indexed and must match the position of the result_ids provided during initialization.
-
safety_result(SafetyResult) –The result of the safety check.
Source code in horde_sdk/worker/generations_base.py
on_pending_submit
on_submitting
Call when an attempt is going to be made to submit the generation.
on_submit_complete
Call when the generation has been successfully submitted.
on_user_requested_abort
Call when the user requests to abort the generation.