openg2g.controller¶
openg2g.controller.base
¶
Abstract base class for controllers.
Controller
¶
Bases: Generic[DCBackendT, GridBackendT], ABC
Interface for a control component in the G2G framework.
Controllers receive datacenter and grid state and produce control actions. Multiple controllers compose in order within the coordinator.
Source code in openg2g/controller/base.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
dt_s
abstractmethod
property
¶
Control interval as a Fraction (seconds).
reset()
abstractmethod
¶
Reset simulation state to initial conditions.
Called by the coordinator before each start. Must
clear all simulation state: dual variables, counters, cached
matrices. Configuration (dt_s, fits, step sizes) is not
affected.
Abstract so every implementation explicitly enumerates its state. A forgotten field is a bug -- not clearing it silently corrupts the second run.
Source code in openg2g/controller/base.py
start()
¶
Acquire per-run resources.
Called after reset, before the simulation loop.
No-op by default because most controllers have no resources to
acquire.
stop()
¶
Release per-run resources. Simulation state is preserved.
Called after the simulation loop in LIFO order. No-op by default.
step(clock, datacenter, grid, events)
abstractmethod
¶
Compute control commands for this step. Return an empty list for no-op.
Source code in openg2g/controller/base.py
openg2g.controller.batch_size_schedule
¶
Batch size schedule controller: applies pre-defined batch size changes at specified times.
BatchSizeChange
dataclass
¶
A batch size change event, optionally with gradual ramp-up.
Attributes:
| Name | Type | Description |
|---|---|---|
batch_size |
int
|
Target batch size (max_num_seqs). |
ramp_up_rate |
float
|
Requests/second ramp-up rate. 0 means immediate. |
Source code in openg2g/controller/batch_size_schedule.py
at(t)
¶
Schedule this change at time t seconds.
Returns:
| Type | Description |
|---|---|
BatchSizeSchedule
|
A single-entry |
BatchSizeSchedule
¶
Ordered sequence of batch size changes, built with | operator.
Example:
schedule = (
BatchSizeChange(48).at(40)
| BatchSizeChange(32).at(60)
| BatchSizeChange(48, ramp_up_rate=4).at(280)
)
Raises:
| Type | Description |
|---|---|
ValueError
|
If two entries share the same timestamp. |
Source code in openg2g/controller/batch_size_schedule.py
BatchSizeScheduleController
¶
Bases: Controller[DatacenterBackend, GridBackend]
Applies pre-defined batch size changes at scheduled times.
Walks each model's schedule and emits
SetBatchSize commands when the
simulation clock reaches the scheduled time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schedules
|
dict[str, BatchSizeSchedule]
|
Per-model batch size schedules, keyed by model label. |
required |
dt_s
|
Fraction
|
How often the controller checks the schedule (seconds). |
Fraction(1)
|
Source code in openg2g/controller/batch_size_schedule.py
openg2g.controller.load_shift
¶
Cross-site LLM load shifting controller.
Shifts replicas between datacenters when batch-size control (OFO) is exhausted and voltage violations persist. Runs after all per-site OFO controllers in the coordinator loop.
LoadShiftConfig
¶
Bases: BaseModel
Configuration for cross-site load shifting.
Source code in openg2g/controller/load_shift.py
headroom = 0.3
class-attribute
instance-attribute
¶
Fraction of extra server capacity to pre-allocate at each DC so incoming replicas have room (e.g. 0.3 = 30% headroom).
LoadShiftController
¶
Bases: Controller[LLMBatchSizeControlledDatacenter, OpenDSSGrid]
Shift LLM replicas between datacenters to resolve voltage violations.
Rules:
1. Only shift models already running at both source and destination.
2. Only act when batch sizes are saturated AND violation persists.
3. For undervoltage: shift load OUT of violated site → highest-voltage site.
For overvoltage: shift load INTO violated site ← lowest-voltage site.
4. Shift gpus_per_shift GPUs worth of replicas per time step.
5. Repeat until violation resolves or no candidates remain.
Source code in openg2g/controller/load_shift.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
openg2g.controller.noop
¶
No-op controller that does nothing.
NoopController
¶
Bases: Controller[DatacenterBackend, GridBackend]
Controller that always returns an empty action.
Source code in openg2g/controller/noop.py
openg2g.controller.ofo
¶
Online Feedback Optimization (OFO) batch-size controller.
Implements the primal-dual algorithm for joint voltage regulation and latency management via GPU batch size control.
OFOConfig
¶
Bases: BaseModel
Online Feedback Optimization tuning parameters.
Attributes:
| Name | Type | Description |
|---|---|---|
primal_step_size |
float
|
Primal descent step size ρ_x (Eq. 8). |
w_throughput |
float
|
Throughput weight in primal gradient. |
w_switch |
float
|
Switching cost regularizer weight γ (Eq. 4a). |
voltage_gradient_scale |
float
|
Scaling factor k_v for voltage dual term in the primal gradient. |
v_min |
float
|
Lower voltage bound (pu). |
v_max |
float
|
Upper voltage bound (pu). |
voltage_dual_step_size |
float
|
Voltage dual ascent step size ρ_v (Eqs. 5-6). |
latency_dual_step_size |
float
|
Latency dual ascent step size ρ_l (Eq. 7). |
sensitivity_update_interval |
int
|
Steps between H-matrix re-estimation (0 = only once at init). |
sensitivity_perturbation_kw |
float
|
Perturbation magnitude (kW) for finite-difference sensitivity estimation. |
Source code in openg2g/controller/ofo.py
LogisticModelStore
¶
Per-model logistic models for power, latency, and throughput.
Used by
OFOBatchSizeController
to compute gradients of the Lagrangian with respect to batch size.
Attributes:
| Name | Type | Description |
|---|---|---|
COL_MODEL_LABEL |
Column name for model label in the CSV. |
|
COL_METRIC |
Column name for metric type in the CSV. |
Source code in openg2g/controller/ofo.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 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 | |
power(model)
¶
latency(model)
¶
throughput(model)
¶
generate(models, data_sources, *, runs=None, mlenergy_data_dir=None)
classmethod
¶
Generate logistic fits from ML.ENERGY benchmark data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
models
|
tuple[InferenceModelSpec, ...]
|
Model specifications. |
required |
data_sources
|
dict[str, Any]
|
Per-model |
required |
runs
|
Any
|
Pre-loaded |
None
|
mlenergy_data_dir
|
Path | None
|
Path to compiled mlenergy-data directory.
Ignored if |
None
|
Returns:
| Type | Description |
|---|---|
LogisticModelStore
|
A new |
Source code in openg2g/controller/ofo.py
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 | |
save(csv_path, *, plot=False)
¶
Save logistic fits to a CSV.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
csv_path
|
Path
|
Output CSV path. |
required |
plot
|
bool
|
If |
False
|
Source code in openg2g/controller/ofo.py
load(csv_path)
classmethod
¶
Load power, latency, and throughput fits from a merged CSV.
Expected columns: model_label, metric, plus the logistic
model parameter columns (L, x0, k, b0).
The metric column must contain power, latency, or
throughput (case-insensitive).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
csv_path
|
Path | str
|
Path to the logistic fits CSV. |
required |
Source code in openg2g/controller/ofo.py
ensure(csv_path, models=None, data_sources=None, *, mlenergy_data_dir=None, plot=False)
classmethod
¶
Load from csv_path, generating first if needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
csv_path
|
Path
|
Path to the logistic fits CSV. |
required |
models
|
tuple[InferenceModelSpec, ...] | None
|
Model specifications. Required when no cached file exists. |
None
|
data_sources
|
dict[str, Any] | None
|
Per-model |
None
|
mlenergy_data_dir
|
Path | None
|
Path to compiled mlenergy-data directory. |
None
|
plot
|
bool
|
If |
False
|
Source code in openg2g/controller/ofo.py
VoltageDualVariables
¶
Full-network duals for voltage box constraints.
Maintains per-bus dual variables for under- and overvoltage and updates them via projected gradient ascent:
dual_undervoltage <- [dual_undervoltage + ρ_v * (v_min - v̂)]+
dual_overvoltage <- [dual_overvoltage + ρ_v * (v̂ - v_max)]+
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_bus_phases
|
int
|
Number of bus-phase pairs in the voltage vector (3M). |
required |
config
|
OFOConfig
|
OFO configuration (voltage bounds and dual step size). |
required |
Source code in openg2g/controller/ofo.py
update(observed_voltages)
¶
Update duals given observed voltage vector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observed_voltages
|
ndarray
|
Observed voltage magnitudes (pu), shape
|
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in openg2g/controller/ofo.py
PrimalBatchOptimizer
¶
Primal batch-size optimizer operating in log2 space.
Maintains continuous state x_i = log2(batch_i) per model and applies
a gradient descent step using voltage duals, latency duals, and fitted
power/latency/throughput curves.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
models
|
list[InferenceModelSpec]
|
Model specifications for each served model. |
required |
feasible_batch_sizes
|
list[int]
|
Allowed batch sizes (union across all models). |
required |
power_fits
|
dict[str, LogisticModel]
|
Per-model logistic fit for power vs log2(batch_size). |
required |
latency_fits
|
dict[str, LogisticModel]
|
Per-model logistic fit for latency vs log2(batch_size). |
required |
throughput_fits
|
dict[str, LogisticModel]
|
Per-model logistic fit for throughput vs log2(batch_size). |
required |
config
|
OFOConfig
|
OFO configuration (step size, throughput/switch weights, voltage gradient scale). |
required |
Source code in openg2g/controller/ofo.py
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 | |
init_from_batches(batch_init)
¶
Initialize log-batch-size state from discrete batch sizes.
Source code in openg2g/controller/ofo.py
step(*, voltage_dual_diff, sensitivity_matrix, phase_share_by_model, latency_dual_by_model=None, replica_count_by_model=None)
¶
Primal gradient descent step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
voltage_dual_diff
|
ndarray
|
Voltage dual difference vector
(η = λ̄ − λ), shape |
required |
sensitivity_matrix
|
ndarray
|
Voltage sensitivity matrix (H = dv/dp),
shape |
required |
phase_share_by_model
|
dict[str, ndarray]
|
Per-model normalized phase share vectors,
shape |
required |
latency_dual_by_model
|
dict[str, float] | None
|
Per-model latency dual variables (μ_i). |
None
|
replica_count_by_model
|
dict[str, float] | None
|
Per-model active replica counts (w_i). |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, int]
|
Next batch sizes per model. |
Source code in openg2g/controller/ofo.py
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 | |
OFOBatchSizeController
¶
Bases: Controller[LLMBatchSizeControlledDatacenter[LLMDatacenterState], OpenDSSGrid]
Online Feedback Optimization controller for batch-size regulation.
Reads grid voltage and datacenter state, updates voltage and latency
duals, runs the primal batch-size optimizer, and returns new batch
sizes. Latency dual updates use dc_state.observed_itl_s_by_model
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inference_models
|
tuple[InferenceModelSpec, ...]
|
Model specifications served in the datacenter. |
required |
models
|
LogisticModelStore
|
Per-model logistic models for power, latency, and throughput used in gradient computation. |
required |
config
|
OFOConfig | None
|
Unified OFO tuning parameters. |
None
|
dt_s
|
Fraction
|
Control interval (seconds). |
Fraction(1)
|
Source code in openg2g/controller/ofo.py
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 | |
openg2g.controller.rule_based
¶
Rule-based batch-size controller for voltage regulation.
A proportional controller that adjusts LLM batch sizes based on observed voltage violations. Unlike the OFO controller, it requires no sensitivity matrix, no logistic curve fits, and no dual variables — making it a natural "simple baseline" for comparison.
Algorithm (each control step): 1. Read all bus-phase voltages from the grid. 2. Find worst voltage violation magnitude. 3. Compute a signed "pressure" signal: - positive (undervoltage) → reduce batch (less power draw, less voltage drop) - negative (overvoltage) → increase batch (more power draw, more voltage drop) - zero → no action (all voltages within bounds) 4. Adjust each model's batch size proportionally in log2-space. 5. Snap to the nearest feasible batch size.
RuleBasedConfig
¶
Bases: BaseModel
Configuration for the rule-based batch-size controller.
Source code in openg2g/controller/rule_based.py
step_size = 10.0
class-attribute
instance-attribute
¶
Proportional gain: log2(batch) change per pu of voltage violation. With feasible batches spaced ~1 log2 unit apart, a violation of 0.01 pu needs step_size ~10 to produce a 0.1 log2 shift, enough to eventually change the discrete batch level.
v_min = 0.95
class-attribute
instance-attribute
¶
Lower voltage limit (pu).
v_max = 1.05
class-attribute
instance-attribute
¶
Upper voltage limit (pu).
deadband = 0.001
class-attribute
instance-attribute
¶
Ignore violations smaller than this (pu). Prevents chattering.
latency_guard = True
class-attribute
instance-attribute
¶
If True, prevent batch size increase when ITL exceeds deadline.
RuleBasedBatchSizeController
¶
Bases: Controller[LLMBatchSizeControlledDatacenter[LLMDatacenterState], OpenDSSGrid]
Proportional rule-based controller for LLM batch-size regulation.
Reads grid voltages, computes a signed pressure signal from the worst violation, and adjusts batch sizes proportionally. No model fits or sensitivity matrices required.
Source code in openg2g/controller/rule_based.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | |
openg2g.controller.tap_schedule
¶
Tap schedule controller: applies pre-defined regulator tap changes at specified times.
TapScheduleController
¶
Bases: Controller[DatacenterBackend, GridBackend]
Applies pre-defined tap changes at scheduled times.
When multiple schedule entries fire in the same step, their tap values are merged (later entries win).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schedule
|
TapSchedule
|
Tap schedule built via
|
required |
dt_s
|
Fraction
|
How often the controller checks the schedule (seconds). |
Fraction(1)
|