Backends
steerability.backends
Backend implementations of the execution seam.
Each package implements the Backend and SteeringSession protocols from
steerability.algorithms.core.execution for one backend family. Specs resolve to these classes
through steerability.algorithms.core.execution.backend; nothing in steerability.algorithms
imports this package at module level.
ExclusiveSession
The in-process session: direct model access, hook scopes, and the default decode loop.
Exposes .model for components whose requirements include Capability.IN_PROCESS_TORCH.
The default decode delegates to model.generate. Items execute serially, each under its own
hook registrations, which preserves in-process semantics for every entry combination.
Source code in steerability/backends/huggingface/session.py
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 | |
closed
property
Whether the session has been closed.
layout
property
Structural facts derived from the loaded model, computed on every access so weight edits and model replacements are always reflected.
num_layers comes from the resolved decoder layer list; hidden_size,
num_attention_heads, and head_dim come from the text config (text_config(model),
the text sub-config on composite multimodal models), with a
hidden_size // num_attention_heads fallback for head_dim; dtype from the model;
model_fingerprint from the weight/config fingerprint; and model_type from the
composite config, so a multimodal checkpoint keeps its wrapper model_type.
model
property
The live model.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the session is closed or no model is available yet. |
tokenizer
property
The tokenizer, or None when the adopting caller has not resolved one yet.
capture(prompts, layers, mode, location='layer_output')
Capture residual-stream hidden states for prompts at layers.
Prompts resolve to token ids, right-pad into one batch, and run through the shared
layerwise extraction. In "all_tokens" mode each layer's tensor is [N, T, H] with rows
outside the attention mask zeroed; in "last_token" mode the last real (non-pad) position
of each row is selected, giving [N, H].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompts
|
list[PreparedPrompt]
|
The prompts to capture. |
required |
layers
|
list[int]
|
0-based layer ids to keep. |
required |
mode
|
Literal['all_tokens', 'last_token']
|
|
required |
location
|
Literal['layer_output', 'layer_input']
|
|
'layer_output'
|
Returns:
| Type | Description |
|---|---|
CaptureResult
|
The captured tensors and the batch attention mask, on CPU. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in steerability/backends/huggingface/session.py
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 | |
close()
Close the session; further use raises RuntimeError.
Source code in steerability/backends/huggingface/session.py
118 119 120 | |
entries_applied(state_entries)
Apply state entries to the live model for the duration of the context.
Used by the pipeline around a client-side decoding driver's decode, so every forward
the driver issues on the live model, including rollouts through this session and
auxiliary scoring passes, runs under the generation's hooks. The session owns
registration; hooks are removed when the context exits, even on error.
Source code in steerability/backends/huggingface/session.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | |
generate(items, params)
Generate one result per item, each under its own hook registrations.
Items sharing identical state entries, identical output entries, and identical-or-absent
effective seeds execute in one batched model.generate pass (right-padded to a common
prompt length, then left-packed together); otherwise items decode serially. Caller-supplied logits_processor and
stopping_criteria entries in params.extra append after the items' own contributions,
and the normalized stop fields compose as stop rules anchored at the prompt length. A
seeded item decodes inside a seeded RNG fork, so seeded runs are reproducible and the
covered generator state (CPU, plus the model device's) is restored afterwards; when
params.seed is set and an item carries no seed of its own, the item's seed derives per
index, so multi-item fan-outs sample distinct streams.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Sequence[GenerationItem]
|
The generation items. |
required |
params
|
GenerationParams
|
Normalized generation parameters shared by all items. |
required |
Returns:
| Type | Description |
|---|---|
list[ItemResult]
|
One |
list[ItemResult]
|
reason per candidate with the precedence stop, then eos, then length, then None. |
Source code in steerability/backends/huggingface/session.py
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 | |
score(items, params)
Teacher-forced log-probabilities of each item's reference tokens.
For each item, the prompt left-packs (pad positions move before the real tokens) so the
reference follows the prompt's last real token, then prompt and reference concatenate
into one causal forward pass under the item's hook registrations; the item's logits
processors replay position-by-position with the same (prefix_ids, scores) view they
receive during generation. Items sharing identical state and output entries score in one
batched forward pass (prompts right-padded to a common length, then left-packed
together); otherwise items score serially. Stopping criteria never apply. Decoder-only
models only; the pipeline's compute_logprobs serves encoder-decoder models in-process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Sequence[ScoringItem]
|
The scoring items. Every item must carry the same reference length. |
required |
params
|
GenerationParams
|
|
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Log probabilities of shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If items carry differing reference lengths. |
UnsupportedOperationError
|
If the model is an encoder-decoder model. |
Source code in steerability/backends/huggingface/session.py
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 | |
HFBackend
Bases: Backend
The in-process Hugging Face backend.
Owns a loaded model and tokenizer, either loaded from a spec or adopted from a caller that already holds them. At most one session may be open per backend at a time, so the backend runs one generation at a time.
Source code in steerability/backends/huggingface/backend.py
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 | |
capabilities
property
The advertised capability atoms.
capture_kinds
property
The advertised capture kinds, when Capability.HIDDEN_CAPTURE is present.
intervention_kinds
property
The advertised intervention kinds, when Capability.INTERVENTION_SPECS is present.
processor_kinds
property
The advertised processor kinds, when Capability.PER_STEP_LOGIT_SPECS is present.
spec = spec
instance-attribute
adopt(spec, model_provider, tokenizer_provider)
classmethod
Wrap an already-loaded model and tokenizer without loading anything.
Providers are read on every access, so a caller whose model is replaced mid-steer (a structural control returning a new model) always exposes the current one to sessions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
BackendSpec
|
The backend spec identifying this configuration. |
required |
model_provider
|
Callable[[], PreTrainedModel | None]
|
Callable returning the current model (may return None before one exists). |
required |
tokenizer_provider
|
Callable[[], object | None]
|
Callable returning the current tokenizer. |
required |
Returns:
| Type | Description |
|---|---|
HFBackend
|
The adopting backend. |
Source code in steerability/backends/huggingface/backend.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
capabilities_for_spec(spec)
classmethod
The static Hugging Face capability advertisement (spec-independent).
Source code in steerability/backends/huggingface/backend.py
113 114 115 116 | |
open_session()
Open the backend's one exclusive session.
Returns:
| Type | Description |
|---|---|
ExclusiveSession
|
The session, usable as a context manager. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If an exclusive session is already open on this backend. |
Source code in steerability/backends/huggingface/backend.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | |
release()
Free the resources this backend owns beyond what the caller owns.
The default is a no-op. Engine-owning backends override this to shut their engines down deterministically. Release is idempotent.
Source code in steerability/algorithms/core/execution/backend.py
204 205 206 207 208 209 210 | |
report_once(key)
Return True the first time key is seen on this backend, False afterwards.
Sessions are opened per pipeline call, so a per-session flag would fire on every call; this guard lives on the backend so a repeated informational readout fires once.
Source code in steerability/backends/huggingface/backend.py
135 136 137 138 139 140 141 142 143 144 | |
stage_artifacts(payloads)
Make each content-addressed artifact available to the execution side.
Called by the pipeline at the end of steer() with the tensor payloads of every
lowered intervention spec, keyed by content-addressed artifact id. Staging is
idempotent: an artifact that already exists at the destination is success. The
in-process backend keeps live tensors and needs no staging; engine-backed backends
write into the registry the serving engine reads.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payloads
|
dict[str, dict[str, Tensor]]
|
Mapping from artifact id to a name-to-tensor mapping. |
required |
Source code in steerability/algorithms/core/execution/backend.py
190 191 192 193 194 195 196 197 198 199 200 201 202 | |
huggingface
The in-process Hugging Face backend and its exclusive session.
backend.py holds HFBackend and its static capability advertisement; session.py holds
ExclusiveSession (direct model access, hook scopes, and the default decode loop) and the
generation-parameter rendering helpers. Constructing HFBackend loads a model and tokenizer in
the current process.
HF_CAPABILITIES = BackendCapabilities(atoms=frozenset({Capability.IN_PROCESS_TORCH, Capability.HIDDEN_CAPTURE, Capability.BEAM_PROPOSALS}), capture_kinds=CaptureKinds(kinds=frozenset({'residual'}), locations=frozenset({'layer_output', 'layer_input'}), modes=frozenset({'all_tokens', 'last_token'})))
module-attribute
ExclusiveSession
The in-process session: direct model access, hook scopes, and the default decode loop.
Exposes .model for components whose requirements include Capability.IN_PROCESS_TORCH.
The default decode delegates to model.generate. Items execute serially, each under its own
hook registrations, which preserves in-process semantics for every entry combination.
Source code in steerability/backends/huggingface/session.py
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 | |
closed
property
Whether the session has been closed.
layout
property
Structural facts derived from the loaded model, computed on every access so weight edits and model replacements are always reflected.
num_layers comes from the resolved decoder layer list; hidden_size,
num_attention_heads, and head_dim come from the text config (text_config(model),
the text sub-config on composite multimodal models), with a
hidden_size // num_attention_heads fallback for head_dim; dtype from the model;
model_fingerprint from the weight/config fingerprint; and model_type from the
composite config, so a multimodal checkpoint keeps its wrapper model_type.
model
property
The live model.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the session is closed or no model is available yet. |
tokenizer
property
The tokenizer, or None when the adopting caller has not resolved one yet.
capture(prompts, layers, mode, location='layer_output')
Capture residual-stream hidden states for prompts at layers.
Prompts resolve to token ids, right-pad into one batch, and run through the shared
layerwise extraction. In "all_tokens" mode each layer's tensor is [N, T, H] with rows
outside the attention mask zeroed; in "last_token" mode the last real (non-pad) position
of each row is selected, giving [N, H].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompts
|
list[PreparedPrompt]
|
The prompts to capture. |
required |
layers
|
list[int]
|
0-based layer ids to keep. |
required |
mode
|
Literal['all_tokens', 'last_token']
|
|
required |
location
|
Literal['layer_output', 'layer_input']
|
|
'layer_output'
|
Returns:
| Type | Description |
|---|---|
CaptureResult
|
The captured tensors and the batch attention mask, on CPU. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in steerability/backends/huggingface/session.py
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 | |
close()
Close the session; further use raises RuntimeError.
Source code in steerability/backends/huggingface/session.py
118 119 120 | |
entries_applied(state_entries)
Apply state entries to the live model for the duration of the context.
Used by the pipeline around a client-side decoding driver's decode, so every forward
the driver issues on the live model, including rollouts through this session and
auxiliary scoring passes, runs under the generation's hooks. The session owns
registration; hooks are removed when the context exits, even on error.
Source code in steerability/backends/huggingface/session.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | |
generate(items, params)
Generate one result per item, each under its own hook registrations.
Items sharing identical state entries, identical output entries, and identical-or-absent
effective seeds execute in one batched model.generate pass (right-padded to a common
prompt length, then left-packed together); otherwise items decode serially. Caller-supplied logits_processor and
stopping_criteria entries in params.extra append after the items' own contributions,
and the normalized stop fields compose as stop rules anchored at the prompt length. A
seeded item decodes inside a seeded RNG fork, so seeded runs are reproducible and the
covered generator state (CPU, plus the model device's) is restored afterwards; when
params.seed is set and an item carries no seed of its own, the item's seed derives per
index, so multi-item fan-outs sample distinct streams.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Sequence[GenerationItem]
|
The generation items. |
required |
params
|
GenerationParams
|
Normalized generation parameters shared by all items. |
required |
Returns:
| Type | Description |
|---|---|
list[ItemResult]
|
One |
list[ItemResult]
|
reason per candidate with the precedence stop, then eos, then length, then None. |
Source code in steerability/backends/huggingface/session.py
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 | |
score(items, params)
Teacher-forced log-probabilities of each item's reference tokens.
For each item, the prompt left-packs (pad positions move before the real tokens) so the
reference follows the prompt's last real token, then prompt and reference concatenate
into one causal forward pass under the item's hook registrations; the item's logits
processors replay position-by-position with the same (prefix_ids, scores) view they
receive during generation. Items sharing identical state and output entries score in one
batched forward pass (prompts right-padded to a common length, then left-packed
together); otherwise items score serially. Stopping criteria never apply. Decoder-only
models only; the pipeline's compute_logprobs serves encoder-decoder models in-process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Sequence[ScoringItem]
|
The scoring items. Every item must carry the same reference length. |
required |
params
|
GenerationParams
|
|
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Log probabilities of shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If items carry differing reference lengths. |
UnsupportedOperationError
|
If the model is an encoder-decoder model. |
Source code in steerability/backends/huggingface/session.py
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 | |
HFBackend
Bases: Backend
The in-process Hugging Face backend.
Owns a loaded model and tokenizer, either loaded from a spec or adopted from a caller that already holds them. At most one session may be open per backend at a time, so the backend runs one generation at a time.
Source code in steerability/backends/huggingface/backend.py
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 | |
capabilities
property
The advertised capability atoms.
capture_kinds
property
The advertised capture kinds, when Capability.HIDDEN_CAPTURE is present.
intervention_kinds
property
The advertised intervention kinds, when Capability.INTERVENTION_SPECS is present.
processor_kinds
property
The advertised processor kinds, when Capability.PER_STEP_LOGIT_SPECS is present.
spec = spec
instance-attribute
adopt(spec, model_provider, tokenizer_provider)
classmethod
Wrap an already-loaded model and tokenizer without loading anything.
Providers are read on every access, so a caller whose model is replaced mid-steer (a structural control returning a new model) always exposes the current one to sessions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
BackendSpec
|
The backend spec identifying this configuration. |
required |
model_provider
|
Callable[[], PreTrainedModel | None]
|
Callable returning the current model (may return None before one exists). |
required |
tokenizer_provider
|
Callable[[], object | None]
|
Callable returning the current tokenizer. |
required |
Returns:
| Type | Description |
|---|---|
HFBackend
|
The adopting backend. |
Source code in steerability/backends/huggingface/backend.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
capabilities_for_spec(spec)
classmethod
The static Hugging Face capability advertisement (spec-independent).
Source code in steerability/backends/huggingface/backend.py
113 114 115 116 | |
open_session()
Open the backend's one exclusive session.
Returns:
| Type | Description |
|---|---|
ExclusiveSession
|
The session, usable as a context manager. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If an exclusive session is already open on this backend. |
Source code in steerability/backends/huggingface/backend.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | |
release()
Free the resources this backend owns beyond what the caller owns.
The default is a no-op. Engine-owning backends override this to shut their engines down deterministically. Release is idempotent.
Source code in steerability/algorithms/core/execution/backend.py
204 205 206 207 208 209 210 | |
report_once(key)
Return True the first time key is seen on this backend, False afterwards.
Sessions are opened per pipeline call, so a per-session flag would fire on every call; this guard lives on the backend so a repeated informational readout fires once.
Source code in steerability/backends/huggingface/backend.py
135 136 137 138 139 140 141 142 143 144 | |
stage_artifacts(payloads)
Make each content-addressed artifact available to the execution side.
Called by the pipeline at the end of steer() with the tensor payloads of every
lowered intervention spec, keyed by content-addressed artifact id. Staging is
idempotent: an artifact that already exists at the destination is success. The
in-process backend keeps live tensors and needs no staging; engine-backed backends
write into the registry the serving engine reads.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payloads
|
dict[str, dict[str, Tensor]]
|
Mapping from artifact id to a name-to-tensor mapping. |
required |
Source code in steerability/algorithms/core/execution/backend.py
190 191 192 193 194 195 196 197 198 199 200 201 202 | |
compose_stop_criteria(params, prompt_len, tokenizer)
The stop criteria implied by the normalized stop fields, anchored at prompt_len.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
GenerationParams
|
The normalized parameters carrying |
required |
prompt_len
|
int
|
Prompt length the substring criteria decode past. |
required |
tokenizer
|
PreTrainedTokenizerBase | None
|
Tokenizer for substring decoding; required when stop strings are set. |
required |
Returns:
| Type | Description |
|---|---|
list
|
The composed criteria (possibly empty). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If stop strings are set and no tokenizer is available. |
Source code in steerability/backends/huggingface/session.py
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 | |
render_hf_gen_kwargs(params)
Render normalized generation parameters onto model.generate keyword arguments.
Keys in params.extra pass through untouched; a normalized field always takes precedence
over a same-named extra key. seed is not rendered here, since the session applies it as a
fork_rng-scoped manual_seed around the item's decode; the stop fields are not rendered
either, since the session composes them as prompt-anchored stop criteria
(compose_stop_criteria).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
GenerationParams
|
The normalized parameters. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Keyword arguments for |
Source code in steerability/backends/huggingface/session.py
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 | |
backend
The in-process Hugging Face backend and its capability advertisement.
HF_CAPABILITIES = BackendCapabilities(atoms=frozenset({Capability.IN_PROCESS_TORCH, Capability.HIDDEN_CAPTURE, Capability.BEAM_PROPOSALS}), capture_kinds=CaptureKinds(kinds=frozenset({'residual'}), locations=frozenset({'layer_output', 'layer_input'}), modes=frozenset({'all_tokens', 'last_token'})))
module-attribute
HFBackend
Bases: Backend
The in-process Hugging Face backend.
Owns a loaded model and tokenizer, either loaded from a spec or adopted from a caller that already holds them. At most one session may be open per backend at a time, so the backend runs one generation at a time.
Source code in steerability/backends/huggingface/backend.py
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 | |
capabilities
property
The advertised capability atoms.
capture_kinds
property
The advertised capture kinds, when Capability.HIDDEN_CAPTURE is present.
intervention_kinds
property
The advertised intervention kinds, when Capability.INTERVENTION_SPECS is present.
processor_kinds
property
The advertised processor kinds, when Capability.PER_STEP_LOGIT_SPECS is present.
spec = spec
instance-attribute
adopt(spec, model_provider, tokenizer_provider)
classmethod
Wrap an already-loaded model and tokenizer without loading anything.
Providers are read on every access, so a caller whose model is replaced mid-steer (a structural control returning a new model) always exposes the current one to sessions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
BackendSpec
|
The backend spec identifying this configuration. |
required |
model_provider
|
Callable[[], PreTrainedModel | None]
|
Callable returning the current model (may return None before one exists). |
required |
tokenizer_provider
|
Callable[[], object | None]
|
Callable returning the current tokenizer. |
required |
Returns:
| Type | Description |
|---|---|
HFBackend
|
The adopting backend. |
Source code in steerability/backends/huggingface/backend.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
capabilities_for_spec(spec)
classmethod
The static Hugging Face capability advertisement (spec-independent).
Source code in steerability/backends/huggingface/backend.py
113 114 115 116 | |
open_session()
Open the backend's one exclusive session.
Returns:
| Type | Description |
|---|---|
ExclusiveSession
|
The session, usable as a context manager. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If an exclusive session is already open on this backend. |
Source code in steerability/backends/huggingface/backend.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | |
release()
Free the resources this backend owns beyond what the caller owns.
The default is a no-op. Engine-owning backends override this to shut their engines down deterministically. Release is idempotent.
Source code in steerability/algorithms/core/execution/backend.py
204 205 206 207 208 209 210 | |
report_once(key)
Return True the first time key is seen on this backend, False afterwards.
Sessions are opened per pipeline call, so a per-session flag would fire on every call; this guard lives on the backend so a repeated informational readout fires once.
Source code in steerability/backends/huggingface/backend.py
135 136 137 138 139 140 141 142 143 144 | |
stage_artifacts(payloads)
Make each content-addressed artifact available to the execution side.
Called by the pipeline at the end of steer() with the tensor payloads of every
lowered intervention spec, keyed by content-addressed artifact id. Staging is
idempotent: an artifact that already exists at the destination is success. The
in-process backend keeps live tensors and needs no staging; engine-backed backends
write into the registry the serving engine reads.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payloads
|
dict[str, dict[str, Tensor]]
|
Mapping from artifact id to a name-to-tensor mapping. |
required |
Source code in steerability/algorithms/core/execution/backend.py
190 191 192 193 194 195 196 197 198 199 200 201 202 | |
session
The in-process exclusive session: direct model access, hook scopes, and the default decode loop.
logger = logging.getLogger(__name__)
module-attribute
ExclusiveSession
The in-process session: direct model access, hook scopes, and the default decode loop.
Exposes .model for components whose requirements include Capability.IN_PROCESS_TORCH.
The default decode delegates to model.generate. Items execute serially, each under its own
hook registrations, which preserves in-process semantics for every entry combination.
Source code in steerability/backends/huggingface/session.py
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 | |
closed
property
Whether the session has been closed.
layout
property
Structural facts derived from the loaded model, computed on every access so weight edits and model replacements are always reflected.
num_layers comes from the resolved decoder layer list; hidden_size,
num_attention_heads, and head_dim come from the text config (text_config(model),
the text sub-config on composite multimodal models), with a
hidden_size // num_attention_heads fallback for head_dim; dtype from the model;
model_fingerprint from the weight/config fingerprint; and model_type from the
composite config, so a multimodal checkpoint keeps its wrapper model_type.
model
property
The live model.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the session is closed or no model is available yet. |
tokenizer
property
The tokenizer, or None when the adopting caller has not resolved one yet.
capture(prompts, layers, mode, location='layer_output')
Capture residual-stream hidden states for prompts at layers.
Prompts resolve to token ids, right-pad into one batch, and run through the shared
layerwise extraction. In "all_tokens" mode each layer's tensor is [N, T, H] with rows
outside the attention mask zeroed; in "last_token" mode the last real (non-pad) position
of each row is selected, giving [N, H].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompts
|
list[PreparedPrompt]
|
The prompts to capture. |
required |
layers
|
list[int]
|
0-based layer ids to keep. |
required |
mode
|
Literal['all_tokens', 'last_token']
|
|
required |
location
|
Literal['layer_output', 'layer_input']
|
|
'layer_output'
|
Returns:
| Type | Description |
|---|---|
CaptureResult
|
The captured tensors and the batch attention mask, on CPU. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in steerability/backends/huggingface/session.py
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 | |
close()
Close the session; further use raises RuntimeError.
Source code in steerability/backends/huggingface/session.py
118 119 120 | |
entries_applied(state_entries)
Apply state entries to the live model for the duration of the context.
Used by the pipeline around a client-side decoding driver's decode, so every forward
the driver issues on the live model, including rollouts through this session and
auxiliary scoring passes, runs under the generation's hooks. The session owns
registration; hooks are removed when the context exits, even on error.
Source code in steerability/backends/huggingface/session.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | |
generate(items, params)
Generate one result per item, each under its own hook registrations.
Items sharing identical state entries, identical output entries, and identical-or-absent
effective seeds execute in one batched model.generate pass (right-padded to a common
prompt length, then left-packed together); otherwise items decode serially. Caller-supplied logits_processor and
stopping_criteria entries in params.extra append after the items' own contributions,
and the normalized stop fields compose as stop rules anchored at the prompt length. A
seeded item decodes inside a seeded RNG fork, so seeded runs are reproducible and the
covered generator state (CPU, plus the model device's) is restored afterwards; when
params.seed is set and an item carries no seed of its own, the item's seed derives per
index, so multi-item fan-outs sample distinct streams.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Sequence[GenerationItem]
|
The generation items. |
required |
params
|
GenerationParams
|
Normalized generation parameters shared by all items. |
required |
Returns:
| Type | Description |
|---|---|
list[ItemResult]
|
One |
list[ItemResult]
|
reason per candidate with the precedence stop, then eos, then length, then None. |
Source code in steerability/backends/huggingface/session.py
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 | |
score(items, params)
Teacher-forced log-probabilities of each item's reference tokens.
For each item, the prompt left-packs (pad positions move before the real tokens) so the
reference follows the prompt's last real token, then prompt and reference concatenate
into one causal forward pass under the item's hook registrations; the item's logits
processors replay position-by-position with the same (prefix_ids, scores) view they
receive during generation. Items sharing identical state and output entries score in one
batched forward pass (prompts right-padded to a common length, then left-packed
together); otherwise items score serially. Stopping criteria never apply. Decoder-only
models only; the pipeline's compute_logprobs serves encoder-decoder models in-process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Sequence[ScoringItem]
|
The scoring items. Every item must carry the same reference length. |
required |
params
|
GenerationParams
|
|
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Log probabilities of shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If items carry differing reference lengths. |
UnsupportedOperationError
|
If the model is an encoder-decoder model. |
Source code in steerability/backends/huggingface/session.py
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 | |
compose_stop_criteria(params, prompt_len, tokenizer)
The stop criteria implied by the normalized stop fields, anchored at prompt_len.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
GenerationParams
|
The normalized parameters carrying |
required |
prompt_len
|
int
|
Prompt length the substring criteria decode past. |
required |
tokenizer
|
PreTrainedTokenizerBase | None
|
Tokenizer for substring decoding; required when stop strings are set. |
required |
Returns:
| Type | Description |
|---|---|
list
|
The composed criteria (possibly empty). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If stop strings are set and no tokenizer is available. |
Source code in steerability/backends/huggingface/session.py
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 | |
render_hf_gen_kwargs(params)
Render normalized generation parameters onto model.generate keyword arguments.
Keys in params.extra pass through untouched; a normalized field always takes precedence
over a same-named extra key. seed is not rendered here, since the session applies it as a
fork_rng-scoped manual_seed around the item's decode; the stop fields are not rendered
either, since the session composes them as prompt-anchored stop criteria
(compose_stop_criteria).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
GenerationParams
|
The normalized parameters. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Keyword arguments for |
Source code in steerability/backends/huggingface/session.py
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 | |
vllm
The vLLM backends: the offline engine ("vllm") and the OpenAI-compatible server
("vllm-serve").
This package imports cleanly without vLLM installed. The capability tables (capabilities.py)
are static data used by check(); the strict parameter-rendering table and the request/response
mapping helpers (rendering.py) are plain functions; the two backend classes and their
backend-side helpers live in backend.py, and the request sessions in session.py. Constructing
VLLMBackend requires the vllm optional dependency (it boots an engine); VLLMServeBackend
needs only a reachable vLLM server.
VLLM_BASELINE_CAPABILITIES = BackendCapabilities(atoms=frozenset({Capability.SERVE_CHECKPOINT, Capability.SERVE_LORA, Capability.GUIDED_DECODING}), constraint_kinds=_VLLM_CONSTRAINT_KINDS)
module-attribute
VLLMBackend
Bases: Backend
The offline vLLM engine backend.
Boots one engine per backend instance from the spec (engine_kwargs option forwarded to
vllm.LLM); requires the vllm optional dependency. A CheckpointArtifact overrides the
served model reference and a LoRAArtifact attaches as a LoRA request on every generation.
When the spec declares hook_plugin, the unified worker is selected via
VLLM_HOOK_WORKER=unified and the discovery payload is fetched once and cached by spec
hash. Capability advertisement is available through capabilities_for_spec without
constructing the backend.
Source code in steerability/backends/vllm/backend.py
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 | |
capabilities
property
The advertised capability atoms.
capture_kinds
property
The advertised capture kinds, when Capability.HIDDEN_CAPTURE is present.
intervention_kinds
property
The advertised intervention kinds, when Capability.INTERVENTION_SPECS is present.
processor_kinds
property
The advertised processor kinds, when Capability.PER_STEP_LOGIT_SPECS is present.
spec = spec
instance-attribute
tokenizer = _client_tokenizer(tokenizer_source, trust_remote_code)
instance-attribute
capabilities_for_spec(spec)
classmethod
The capability advertisement implied by spec.
Source code in steerability/backends/vllm/backend.py
250 251 252 253 | |
open_session()
Open a request session over the shared engine.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the backend has been released. |
Source code in steerability/backends/vllm/backend.py
255 256 257 258 259 260 261 262 | |
release()
Shut the engine down explicitly and mark the backend unusable.
Release is idempotent; after it, a new backend must be constructed. The distributed-state teardown is process-global, so release assumes no other live vLLM engine in the process. Ray-based executors are out of scope. Engine-touching calls on any still-open session raise after release.
Source code in steerability/backends/vllm/backend.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 308 309 310 311 312 313 314 315 | |
stage_artifacts(payloads)
Write each content-addressed artifact into the plugin registry the engine reads.
The offline engine shares the process's filesystem, so staging is a registry write (idempotent, verified against the content address).
Source code in steerability/backends/vllm/backend.py
218 219 220 221 222 223 224 | |
VLLMOfflineSession
Bases: _RequestSessionBase
Request session over the offline engine.
Token-id prompts submit as TokensPrompts in one engine call with per-item sampling
parameters; the engine schedules the batch internally, so no client-side fan-out is needed.
Source code in steerability/backends/vllm/session.py
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 | |
closed
property
Whether the session has been closed.
layout
property
Structural facts from the model config (client-side).
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the model config could not be resolved. |
tokenizer
property
The backend's client-side tokenizer.
capture(prompts, layers, mode, location='layer_output')
Hidden-state capture over the plugin's capture surface.
One request per prompt carries a capture spec and a fresh random cache_salt
(a prefix-cache hit skips forward passes, so capture cannot tolerate reused salts) with
max_tokens=1; the surplus decode position is truncated by the plugin. Per-layer
tensors are stacked and right-padded to the batch's longest prompt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompts
|
list[PreparedPrompt]
|
The prompts to capture over. |
required |
layers
|
list[int]
|
0-based decoder-layer indices to capture. |
required |
mode
|
Literal['all_tokens', 'last_token']
|
|
required |
location
|
Literal['layer_output', 'layer_input']
|
The residual-stream boundary, |
'layer_output'
|
Returns:
| Type | Description |
|---|---|
CaptureResult
|
The capture result: |
CaptureResult
|
|
CaptureResult
|
attention mask. |
Raises:
| Type | Description |
|---|---|
UnsupportedOperationError
|
If the spec declares no |
ValueError
|
If |
Source code in steerability/backends/vllm/session.py
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 | |
close()
Close the session; further use raises RuntimeError.
Source code in steerability/backends/vllm/session.py
63 64 65 | |
generate(items, params)
Generate one result per item through the engine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Sequence[GenerationItem]
|
The generation items; state entries lower as intervention specs on plugin-active backends, and no client-side hooks or live processors execute here. |
required |
params
|
GenerationParams
|
Normalized generation parameters shared by all items; unmapped |
required |
Returns:
| Type | Description |
|---|---|
list[ItemResult]
|
One |
Source code in steerability/backends/vllm/session.py
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 | |
score(items, params)
Teacher-forced log-probabilities of each item's reference tokens via prompt logprobs.
Each item's prompt and reference concatenate into one token-id prompt submitted with
prompt_logprobs=0, and the reference positions' log-probabilities are read back.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Sequence[ScoringItem]
|
The scoring items. Every item must carry the same reference length. |
required |
params
|
GenerationParams
|
Must carry no |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Log probabilities of shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If items carry differing reference lengths or |
Source code in steerability/backends/vllm/session.py
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 | |
VLLMServeBackend
Bases: Backend
The vLLM OpenAI-compatible server backend.
Targets a vLLM server rather than an arbitrary OpenAI-compatible endpoint: construction
verifies the server's version surface (GET /version), fetches the plugin discovery payload
(GET /v1/hook/capabilities) when the spec declares hook_plugin, and checks the served
model id against the spec (or serves the pipeline's structural artifacts). Prompts submit as
token ids on the completions endpoint with the token-id return option; the chat endpoint is
not used. Requires no local vLLM installation.
Spec options: base_url (required, the server root), api_key, request_timeout,
max_concurrency, max_retries, retry_backoff, tokenizer_name_or_path,
trust_remote_code, hook_plugin.
Source code in steerability/backends/vllm/backend.py
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 | |
backoff_base = float(spec.get_option('retry_backoff', default=0.5))
instance-attribute
capabilities
property
The advertised capability atoms.
capture_kinds
property
The advertised capture kinds, when Capability.HIDDEN_CAPTURE is present.
intervention_kinds
property
The advertised intervention kinds, when Capability.INTERVENTION_SPECS is present.
max_attempts = int(spec.get_option('max_retries', default=_DEFAULT_MAX_ATTEMPTS))
instance-attribute
max_concurrency = int(spec.get_option('max_concurrency', default=_DEFAULT_MAX_CONCURRENCY))
instance-attribute
processor_kinds
property
The advertised processor kinds, when Capability.PER_STEP_LOGIT_SPECS is present.
spec = spec
instance-attribute
tokenizer = _client_tokenizer(tokenizer_source, trust_remote_code)
instance-attribute
capabilities_for_spec(spec)
classmethod
The capability advertisement implied by spec.
Source code in steerability/backends/vllm/backend.py
602 603 604 605 | |
open_session()
Open a request session over the shared connection.
Source code in steerability/backends/vllm/backend.py
607 608 609 | |
release()
Free the resources this backend owns beyond what the caller owns.
The default is a no-op. Engine-owning backends override this to shut their engines down deterministically. Release is idempotent.
Source code in steerability/algorithms/core/execution/backend.py
204 205 206 207 208 209 210 | |
stage_artifacts(payloads)
Make each content-addressed artifact available to the serving engine.
With an artifact_dir option the payloads are written into that registry root, which
must be the server's registry directory (its VLLM_HOOK_REGISTRY_DIR) on a shared
filesystem; visibility is verified through the server's artifact route when the
discovery payload advertises artifact_registry_root. Otherwise each payload is PUT
to the plugin's artifact route (/v1/hook/artifacts/{id}, body safetensors bytes, id
verified server-side); already-exists is success.
Source code in steerability/backends/vllm/backend.py
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 | |
VLLMServeSession
Bases: _RequestSessionBase
Request session over a vLLM server's completions endpoint.
Items fan out concurrently under the backend's max_concurrency; transport failures retry
with exponential backoff, and a batch whose items partially fail raises PartialBatchError
carrying the successes and the re-issuable failures.
Source code in steerability/backends/vllm/session.py
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 | |
closed
property
Whether the session has been closed.
layout
property
Structural facts from the model config (client-side).
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the model config could not be resolved. |
tokenizer
property
The backend's client-side tokenizer.
capture(prompts, layers, mode, location='layer_output')
Hidden-state capture over the plugin is not implemented in this toolkit version.
Source code in steerability/backends/vllm/session.py
170 171 172 173 174 175 176 177 178 179 180 | |
close()
Close the session; further use raises RuntimeError.
Source code in steerability/backends/vllm/session.py
63 64 65 | |
generate(items, params)
Generate one result per item through the completions endpoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Sequence[GenerationItem]
|
The generation items; entries must be empty. |
required |
params
|
GenerationParams
|
Normalized generation parameters shared by all items; unmapped |
required |
Returns:
| Type | Description |
|---|---|
list[ItemResult]
|
One |
Raises:
| Type | Description |
|---|---|
PartialBatchError
|
If some items failed after transport retries while others succeeded. |
Source code in steerability/backends/vllm/session.py
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 | |
score(items, params)
Teacher-forced log-probabilities of each item's reference tokens via prompt logprobs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Sequence[ScoringItem]
|
The scoring items. Every item must carry the same reference length. |
required |
params
|
GenerationParams
|
Must carry no |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Log probabilities of shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If items carry differing reference lengths or |
PartialBatchError
|
If some items failed after transport retries while others succeeded. |
Source code in steerability/backends/vllm/session.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 | |
extract_ref_logprobs(prompt_logprobs, ref_ids)
Pull the reference tokens' log-probabilities from a prompt-logprobs structure.
Accepts both the offline shape (per-position mappings from token id to an object with a
logprob attribute) and the serve JSON shape (string token-id keys mapping to dicts with a
"logprob" entry). The reference occupies the last len(ref_ids) prompt positions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt_logprobs
|
Sequence | None
|
The per-prompt-position logprob entries, aligned with the submitted prompt tokens (position 0 is None). |
required |
ref_ids
|
Sequence[int]
|
The reference token ids. |
required |
Returns:
| Type | Description |
|---|---|
list[float]
|
One log-probability per reference token. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the structure is missing or a reference position lacks its token's entry. |
Source code in steerability/backends/vllm/rendering.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 | |
map_vllm_finish_reason(finish_reason, stop_reason)
Map a vLLM candidate's finish reason onto the toolkit vocabulary.
vLLM reports "stop" for EOS, stop strings, and stop token ids alike, with stop_reason
None for EOS and the matched string or token id otherwise; "length" maps through
unchanged, and anything else (e.g. "abort") maps to None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
finish_reason
|
str | None
|
The vLLM candidate's finish reason. |
required |
stop_reason
|
Any
|
The vLLM candidate's stop reason. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
One of |
Source code in steerability/backends/vllm/rendering.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
merge_intervention_specs(specs)
One spec carrying every op of specs, in order, with tensor payloads unioned.
Source code in steerability/backends/vllm/rendering.py
243 244 245 246 247 248 249 250 251 252 | |
raise_for_spec_rejection(message)
Raise the toolkit error for a server-side spec rejection message carrying an E_* code.
Kind and constraint gaps (E_UNKNOWN_KIND, E_CONSTRAINT) are support facts a stale
client missed and raise UnsupportedOperationError; every other E_* rejection is a
malformed spec and raises ValueError. The code and JSON path are preserved verbatim.
A message without an E_* code returns without raising.
Source code in steerability/backends/vllm/rendering.py
286 287 288 289 290 291 292 293 294 295 296 297 298 | |
remap_spec_for_scoring(spec, prompt_len)
A scoring copy of spec with after_prompt scopes rewritten to from_position.
The teacher-forced reference is part of the server-side prompt, so the worker's "after the prompt" would select nothing; the rewrite anchors the scope at the original prompt length, the position of the first reference token in the submitted ids.
Source code in steerability/backends/vllm/rendering.py
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | |
render_constraint_sampling_args(field, value)
Constraint kwargs for SamplingParams, tolerant of the structured-outputs rename.
Newer vLLM removes GuidedDecodingParams in favor of StructuredOutputsParams passed as
structured_outputs=; older versions serve guided_decoding=. The declarative field names
(json, regex, grammar, choice) are shared by both surfaces.
Source code in steerability/backends/vllm/rendering.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 | |
render_guided_decoding_field(source)
The vLLM structured-output parameter name and payload for a constraint source.
Source code in steerability/backends/vllm/rendering.py
215 216 217 218 219 220 221 222 223 224 | |
render_vllm_sampling_args(params)
Render normalized generation parameters onto vLLM sampling-parameter names.
The table is exhaustive on this arm. Every normalized field maps to its vLLM name
(max_new_tokens to max_tokens, min_new_tokens to min_tokens, greedy=True to
temperature=0.0, n to n, stop strings to stop with
include_stop_str_in_output=True, extra stop ids to stop_token_ids), and any key left in
extra raises rather than being dropped. seed is not rendered here; sessions derive and
attach per-item seeds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
GenerationParams
|
The normalized parameters. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Keyword arguments for |
dict[str, Any]
|
fields). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
ValueError
|
If |
Source code in steerability/backends/vllm/rendering.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 | |
serve_environment(hook_plugin, base=None)
Return the environment for a vllm serve process under the engine boot policy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hook_plugin
|
bool
|
Whether the server loads the vLLM-Hook unified worker. |
required |
base
|
Mapping[str, str] | None
|
Environment to start from; |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
A copy of |
Source code in steerability/backends/vllm/environment.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
backend
The vLLM backends: the offline engine ("vllm") and the OpenAI-compatible server
("vllm-serve").
This module holds the backend-side helpers (the artifact uploader, layout and tokenizer
resolution, discovery reconciliation) and the two backend classes. It imports cleanly without
vLLM installed: constructing VLLMBackend requires the vllm optional dependency (it boots an
engine); VLLMServeBackend needs only a reachable vLLM server. Every from vllm ... and
from vllm_hook_plugins ... import stays inside a function or method body.
logger = logging.getLogger(__name__)
module-attribute
VLLMBackend
Bases: Backend
The offline vLLM engine backend.
Boots one engine per backend instance from the spec (engine_kwargs option forwarded to
vllm.LLM); requires the vllm optional dependency. A CheckpointArtifact overrides the
served model reference and a LoRAArtifact attaches as a LoRA request on every generation.
When the spec declares hook_plugin, the unified worker is selected via
VLLM_HOOK_WORKER=unified and the discovery payload is fetched once and cached by spec
hash. Capability advertisement is available through capabilities_for_spec without
constructing the backend.
Source code in steerability/backends/vllm/backend.py
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 | |
capabilities
property
The advertised capability atoms.
capture_kinds
property
The advertised capture kinds, when Capability.HIDDEN_CAPTURE is present.
intervention_kinds
property
The advertised intervention kinds, when Capability.INTERVENTION_SPECS is present.
processor_kinds
property
The advertised processor kinds, when Capability.PER_STEP_LOGIT_SPECS is present.
spec = spec
instance-attribute
tokenizer = _client_tokenizer(tokenizer_source, trust_remote_code)
instance-attribute
capabilities_for_spec(spec)
classmethod
The capability advertisement implied by spec.
Source code in steerability/backends/vllm/backend.py
250 251 252 253 | |
open_session()
Open a request session over the shared engine.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the backend has been released. |
Source code in steerability/backends/vllm/backend.py
255 256 257 258 259 260 261 262 | |
release()
Shut the engine down explicitly and mark the backend unusable.
Release is idempotent; after it, a new backend must be constructed. The distributed-state teardown is process-global, so release assumes no other live vLLM engine in the process. Ray-based executors are out of scope. Engine-touching calls on any still-open session raise after release.
Source code in steerability/backends/vllm/backend.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 308 309 310 311 312 313 314 315 | |
stage_artifacts(payloads)
Write each content-addressed artifact into the plugin registry the engine reads.
The offline engine shares the process's filesystem, so staging is a registry write (idempotent, verified against the content address).
Source code in steerability/backends/vllm/backend.py
218 219 220 221 222 223 224 | |
VLLMServeBackend
Bases: Backend
The vLLM OpenAI-compatible server backend.
Targets a vLLM server rather than an arbitrary OpenAI-compatible endpoint: construction
verifies the server's version surface (GET /version), fetches the plugin discovery payload
(GET /v1/hook/capabilities) when the spec declares hook_plugin, and checks the served
model id against the spec (or serves the pipeline's structural artifacts). Prompts submit as
token ids on the completions endpoint with the token-id return option; the chat endpoint is
not used. Requires no local vLLM installation.
Spec options: base_url (required, the server root), api_key, request_timeout,
max_concurrency, max_retries, retry_backoff, tokenizer_name_or_path,
trust_remote_code, hook_plugin.
Source code in steerability/backends/vllm/backend.py
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 | |
backoff_base = float(spec.get_option('retry_backoff', default=0.5))
instance-attribute
capabilities
property
The advertised capability atoms.
capture_kinds
property
The advertised capture kinds, when Capability.HIDDEN_CAPTURE is present.
intervention_kinds
property
The advertised intervention kinds, when Capability.INTERVENTION_SPECS is present.
max_attempts = int(spec.get_option('max_retries', default=_DEFAULT_MAX_ATTEMPTS))
instance-attribute
max_concurrency = int(spec.get_option('max_concurrency', default=_DEFAULT_MAX_CONCURRENCY))
instance-attribute
processor_kinds
property
The advertised processor kinds, when Capability.PER_STEP_LOGIT_SPECS is present.
spec = spec
instance-attribute
tokenizer = _client_tokenizer(tokenizer_source, trust_remote_code)
instance-attribute
capabilities_for_spec(spec)
classmethod
The capability advertisement implied by spec.
Source code in steerability/backends/vllm/backend.py
602 603 604 605 | |
open_session()
Open a request session over the shared connection.
Source code in steerability/backends/vllm/backend.py
607 608 609 | |
release()
Free the resources this backend owns beyond what the caller owns.
The default is a no-op. Engine-owning backends override this to shut their engines down deterministically. Release is idempotent.
Source code in steerability/algorithms/core/execution/backend.py
204 205 206 207 208 209 210 | |
stage_artifacts(payloads)
Make each content-addressed artifact available to the serving engine.
With an artifact_dir option the payloads are written into that registry root, which
must be the server's registry directory (its VLLM_HOOK_REGISTRY_DIR) on a shared
filesystem; visibility is verified through the server's artifact route when the
discovery payload advertises artifact_registry_root. Otherwise each payload is PUT
to the plugin's artifact route (/v1/hook/artifacts/{id}, body safetensors bytes, id
verified server-side); already-exists is success.
Source code in steerability/backends/vllm/backend.py
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 | |
capabilities
The vLLM capability tables, discovery cache and negotiation, and capability refusals.
The kind tables and baseline capabilities are static data used by check(); the discovery
cache and negotiation narrow them to what a live engine or server confirms. This module imports
cleanly without vLLM installed.
VLLM_BASELINE_CAPABILITIES = BackendCapabilities(atoms=frozenset({Capability.SERVE_CHECKPOINT, Capability.SERVE_LORA, Capability.GUIDED_DECODING}), constraint_kinds=_VLLM_CONSTRAINT_KINDS)
module-attribute
logger = logging.getLogger(__name__)
module-attribute
environment
Engine-boot environment policy for the offline vLLM backend and a launched vllm serve.
vLLM reads some settings from process environment variables only, and its engine core is a
spawned process that inherits the environment as it stands when LLM(...) builds its config.
This module computes one boot policy with two consumers: the offline backend applies it to
os.environ with save and restore semantics around LLM(...), so the settings are live while
the engine is constructed and are cleared again immediately afterwards, and serve_environment
returns the same policy as a fresh mapping for a vllm serve process.
Two variables are governed:
VLLM_USE_FLASHINFER_SAMPLERdefaults to"0"on every boot. vLLM selects the FlashInfer top-k/top-p sampler when it is available, andflashinfer-pythonships that kernel JIT-only; the JIT compile runs at startup warmup and invokesnvccat a path derived fromCUDA_HOME, so a node whose CUDA toolkit does not match the installed torch build fails at boot. The native PyTorch sampler decodes the same greedy tokens. This is a default: an explicit caller setting wins.VLLM_HOOK_WORKERis forced to"unified"for ahook_pluginboot to select the plugin's unified worker, and is restored to its prior value after the boot.
This module does not set VLLM_USE_V2_MODEL_RUNNER; the vLLM-Hook plugin owns the model-runner
constraint (it pins the legacy runner from inside the plugin, covering the vllm serve process
the toolkit does not launch).
FLASHINFER_SAMPLER_VARIABLE = 'VLLM_USE_FLASHINFER_SAMPLER'
module-attribute
HOOK_WORKER_VARIABLE = 'VLLM_HOOK_WORKER'
module-attribute
engine_boot_environment(hook_plugin)
Return the forced and default environment mappings for one offline engine boot.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hook_plugin
|
bool
|
Whether the boot selects the vLLM-Hook unified worker. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
A |
dict[str, str]
|
value and restored afterwards; |
tuple[dict[str, str], dict[str, str]]
|
so an explicit caller value wins. |
tuple[dict[str, str], dict[str, str]]
|
|
tuple[dict[str, str], dict[str, str]]
|
when |
Source code in steerability/backends/vllm/environment.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | |
engine_environment(forced, defaults)
Apply the boot environment for the span of the block and restore it on exit.
Forced variables are written regardless of any existing value; default variables are
written only when absent from os.environ. Every variable written is restored on exit,
including variables that were unset on entry (they are removed again). Restoration also
runs when the block raises.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
forced
|
Mapping[str, str]
|
Variables to set regardless of any existing value. |
required |
defaults
|
Mapping[str, str]
|
Variables to set only when they are absent from the environment. |
required |
Yields:
| Type | Description |
|---|---|
dict[str, str]
|
The variables actually written, as a name-to-value mapping. |
Source code in steerability/backends/vllm/environment.py
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 | |
serve_environment(hook_plugin, base=None)
Return the environment for a vllm serve process under the engine boot policy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hook_plugin
|
bool
|
Whether the server loads the vLLM-Hook unified worker. |
required |
base
|
Mapping[str, str] | None
|
Environment to start from; |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
A copy of |
Source code in steerability/backends/vllm/environment.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
rendering
Request and response rendering for the vLLM sessions.
Holds the strict sampling-argument table, the finish-reason and logprob mapping helpers, the
per-item entry split and intervention-spec merge/remap, and the server-side spec-rejection
parser. These are plain functions; this module imports cleanly without vLLM installed, and the
vllm imports it needs stay function-local.
extract_ref_logprobs(prompt_logprobs, ref_ids)
Pull the reference tokens' log-probabilities from a prompt-logprobs structure.
Accepts both the offline shape (per-position mappings from token id to an object with a
logprob attribute) and the serve JSON shape (string token-id keys mapping to dicts with a
"logprob" entry). The reference occupies the last len(ref_ids) prompt positions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt_logprobs
|
Sequence | None
|
The per-prompt-position logprob entries, aligned with the submitted prompt tokens (position 0 is None). |
required |
ref_ids
|
Sequence[int]
|
The reference token ids. |
required |
Returns:
| Type | Description |
|---|---|
list[float]
|
One log-probability per reference token. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the structure is missing or a reference position lacks its token's entry. |
Source code in steerability/backends/vllm/rendering.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 | |
map_vllm_finish_reason(finish_reason, stop_reason)
Map a vLLM candidate's finish reason onto the toolkit vocabulary.
vLLM reports "stop" for EOS, stop strings, and stop token ids alike, with stop_reason
None for EOS and the matched string or token id otherwise; "length" maps through
unchanged, and anything else (e.g. "abort") maps to None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
finish_reason
|
str | None
|
The vLLM candidate's finish reason. |
required |
stop_reason
|
Any
|
The vLLM candidate's stop reason. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
One of |
Source code in steerability/backends/vllm/rendering.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
merge_intervention_specs(specs)
One spec carrying every op of specs, in order, with tensor payloads unioned.
Source code in steerability/backends/vllm/rendering.py
243 244 245 246 247 248 249 250 251 252 | |
raise_for_spec_rejection(message)
Raise the toolkit error for a server-side spec rejection message carrying an E_* code.
Kind and constraint gaps (E_UNKNOWN_KIND, E_CONSTRAINT) are support facts a stale
client missed and raise UnsupportedOperationError; every other E_* rejection is a
malformed spec and raises ValueError. The code and JSON path are preserved verbatim.
A message without an E_* code returns without raising.
Source code in steerability/backends/vllm/rendering.py
286 287 288 289 290 291 292 293 294 295 296 297 298 | |
remap_spec_for_scoring(spec, prompt_len)
A scoring copy of spec with after_prompt scopes rewritten to from_position.
The teacher-forced reference is part of the server-side prompt, so the worker's "after the prompt" would select nothing; the rewrite anchors the scope at the original prompt length, the position of the first reference token in the submitted ids.
Source code in steerability/backends/vllm/rendering.py
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | |
render_constraint_sampling_args(field, value)
Constraint kwargs for SamplingParams, tolerant of the structured-outputs rename.
Newer vLLM removes GuidedDecodingParams in favor of StructuredOutputsParams passed as
structured_outputs=; older versions serve guided_decoding=. The declarative field names
(json, regex, grammar, choice) are shared by both surfaces.
Source code in steerability/backends/vllm/rendering.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 | |
render_guided_decoding_field(source)
The vLLM structured-output parameter name and payload for a constraint source.
Source code in steerability/backends/vllm/rendering.py
215 216 217 218 219 220 221 222 223 224 | |
render_vllm_sampling_args(params)
Render normalized generation parameters onto vLLM sampling-parameter names.
The table is exhaustive on this arm. Every normalized field maps to its vLLM name
(max_new_tokens to max_tokens, min_new_tokens to min_tokens, greedy=True to
temperature=0.0, n to n, stop strings to stop with
include_stop_str_in_output=True, extra stop ids to stop_token_ids), and any key left in
extra raises rather than being dropped. seed is not rendered here; sessions derive and
attach per-item seeds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
GenerationParams
|
The normalized parameters. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Keyword arguments for |
dict[str, Any]
|
fields). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
ValueError
|
If |
Source code in steerability/backends/vllm/rendering.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 | |
session
The vLLM request sessions: the offline-engine session and the server session.
_RequestSessionBase holds the lifecycle and layout shared by both; VLLMOfflineSession
submits batched engine calls and serves plugin capture, and VLLMServeSession fans requests out
over the completions endpoint. This module imports cleanly without vLLM installed; the vllm
imports the sessions need stay method-local.
VLLMOfflineSession
Bases: _RequestSessionBase
Request session over the offline engine.
Token-id prompts submit as TokensPrompts in one engine call with per-item sampling
parameters; the engine schedules the batch internally, so no client-side fan-out is needed.
Source code in steerability/backends/vllm/session.py
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 | |
closed
property
Whether the session has been closed.
layout
property
Structural facts from the model config (client-side).
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the model config could not be resolved. |
tokenizer
property
The backend's client-side tokenizer.
capture(prompts, layers, mode, location='layer_output')
Hidden-state capture over the plugin's capture surface.
One request per prompt carries a capture spec and a fresh random cache_salt
(a prefix-cache hit skips forward passes, so capture cannot tolerate reused salts) with
max_tokens=1; the surplus decode position is truncated by the plugin. Per-layer
tensors are stacked and right-padded to the batch's longest prompt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompts
|
list[PreparedPrompt]
|
The prompts to capture over. |
required |
layers
|
list[int]
|
0-based decoder-layer indices to capture. |
required |
mode
|
Literal['all_tokens', 'last_token']
|
|
required |
location
|
Literal['layer_output', 'layer_input']
|
The residual-stream boundary, |
'layer_output'
|
Returns:
| Type | Description |
|---|---|
CaptureResult
|
The capture result: |
CaptureResult
|
|
CaptureResult
|
attention mask. |
Raises:
| Type | Description |
|---|---|
UnsupportedOperationError
|
If the spec declares no |
ValueError
|
If |
Source code in steerability/backends/vllm/session.py
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 | |
close()
Close the session; further use raises RuntimeError.
Source code in steerability/backends/vllm/session.py
63 64 65 | |
generate(items, params)
Generate one result per item through the engine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Sequence[GenerationItem]
|
The generation items; state entries lower as intervention specs on plugin-active backends, and no client-side hooks or live processors execute here. |
required |
params
|
GenerationParams
|
Normalized generation parameters shared by all items; unmapped |
required |
Returns:
| Type | Description |
|---|---|
list[ItemResult]
|
One |
Source code in steerability/backends/vllm/session.py
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 | |
score(items, params)
Teacher-forced log-probabilities of each item's reference tokens via prompt logprobs.
Each item's prompt and reference concatenate into one token-id prompt submitted with
prompt_logprobs=0, and the reference positions' log-probabilities are read back.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Sequence[ScoringItem]
|
The scoring items. Every item must carry the same reference length. |
required |
params
|
GenerationParams
|
Must carry no |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Log probabilities of shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If items carry differing reference lengths or |
Source code in steerability/backends/vllm/session.py
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 | |
VLLMServeSession
Bases: _RequestSessionBase
Request session over a vLLM server's completions endpoint.
Items fan out concurrently under the backend's max_concurrency; transport failures retry
with exponential backoff, and a batch whose items partially fail raises PartialBatchError
carrying the successes and the re-issuable failures.
Source code in steerability/backends/vllm/session.py
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 | |
closed
property
Whether the session has been closed.
layout
property
Structural facts from the model config (client-side).
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the model config could not be resolved. |
tokenizer
property
The backend's client-side tokenizer.
capture(prompts, layers, mode, location='layer_output')
Hidden-state capture over the plugin is not implemented in this toolkit version.
Source code in steerability/backends/vllm/session.py
170 171 172 173 174 175 176 177 178 179 180 | |
close()
Close the session; further use raises RuntimeError.
Source code in steerability/backends/vllm/session.py
63 64 65 | |
generate(items, params)
Generate one result per item through the completions endpoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Sequence[GenerationItem]
|
The generation items; entries must be empty. |
required |
params
|
GenerationParams
|
Normalized generation parameters shared by all items; unmapped |
required |
Returns:
| Type | Description |
|---|---|
list[ItemResult]
|
One |
Raises:
| Type | Description |
|---|---|
PartialBatchError
|
If some items failed after transport retries while others succeeded. |
Source code in steerability/backends/vllm/session.py
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 | |
score(items, params)
Teacher-forced log-probabilities of each item's reference tokens via prompt logprobs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Sequence[ScoringItem]
|
The scoring items. Every item must carry the same reference length. |
required |
params
|
GenerationParams
|
Must carry no |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Log probabilities of shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If items carry differing reference lengths or |
PartialBatchError
|
If some items failed after transport retries while others succeeded. |
Source code in steerability/backends/vllm/session.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 | |