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 | |
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 one or more control actions. Must complete synchronously.
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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_size
|
int
|
Target batch size (max_num_seqs). |
required |
ramp_up_rate
|
float
|
Requests/second ramp-up rate. 0 means immediate. |
0.0
|
Source code in openg2g/controller/batch_size_schedule.py
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 set_batch_size 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.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.
VoltageDualConfig
dataclass
¶
Configuration for the voltage dual variable update.
Attributes:
| Name | Type | Description |
|---|---|---|
v_min |
float
|
Lower voltage limit (pu). |
v_max |
float
|
Upper voltage limit (pu). |
ascent_step_size |
float
|
Step size for the voltage dual ascent (ρ_v in G2G paper Eqs. 5-6). |
Source code in openg2g/controller/ofo.py
PrimalConfig
dataclass
¶
Configuration for the primal batch-size optimizer.
Attributes:
| Name | Type | Description |
|---|---|---|
descent_step_size |
float
|
Primal gradient descent step size (ρ_x in G2G paper Eq. 8). |
w_throughput |
float
|
Weight on the (negative) throughput gradient. |
w_switch |
float
|
Weight on the switching cost regularizer
|
voltage_gradient_scale |
float
|
Scaling factor applied to the voltage gradient
term. Multiplies |
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
|
VoltageDualConfig
|
Voltage dual configuration (bounds and 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[LLMInferenceModelSpec]
|
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
|
PrimalConfig
|
Primal optimizer configuration. |
required |
Source code in openg2g/controller/ofo.py
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 | |
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
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 | |
OFOBatchController
¶
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 |
|---|---|---|---|
models
|
list[LLMInferenceModelSpec]
|
Model specifications. |
required |
power_fits
|
dict[str, LogisticModel]
|
Per-model logistic fit for power as a function of log2(batch_size). |
required |
latency_fits
|
dict[str, LogisticModel]
|
Per-model logistic fit for latency as a function of log2(batch_size). |
required |
throughput_fits
|
dict[str, LogisticModel]
|
Per-model logistic fit for throughput as a function of log2(batch_size). |
required |
itl_deadline_by_model
|
dict[str, float]
|
Per-model latency threshold (seconds). |
required |
primal_config
|
PrimalConfig
|
Primal optimizer configuration. |
required |
voltage_dual_config
|
VoltageDualConfig
|
Voltage dual configuration (v_min, v_max, ascent_step_size). |
required |
feasible_batch_sizes
|
list[int]
|
Allowed batch sizes. |
required |
latency_dual_step_size
|
float
|
Latency dual step size (ρ_l). |
1.0
|
dt_s
|
Fraction
|
Control interval (seconds). |
Fraction(1)
|
sensitivity_update_interval
|
int
|
Re-estimate sensitivity every N control steps (0 = only once at init). |
0
|
sensitivity_perturbation_kw
|
float
|
Perturbation size for sensitivity estimation. |
100.0
|
Source code in openg2g/controller/ofo.py
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 | |
from_workload(*, workload, power_fits, latency_fits, throughput_fits, primal_config, voltage_dual_config, latency_dual_step_size=1.0, dt_s=Fraction(1), sensitivity_update_interval=0, sensitivity_perturbation_kw=100.0)
classmethod
¶
Create an OFO controller from an LLMInferenceWorkload.
Derives feasible_batch_sizes and itl_deadline_by_model
from the workload's model specs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
workload
|
LLMInferenceWorkload
|
LLM workload specification. |
required |
power_fits
|
dict[str, LogisticModel]
|
Per-model logistic fit for power. |
required |
latency_fits
|
dict[str, LogisticModel]
|
Per-model logistic fit for latency. |
required |
throughput_fits
|
dict[str, LogisticModel]
|
Per-model logistic fit for throughput. |
required |
primal_config
|
PrimalConfig
|
Primal optimizer configuration. |
required |
voltage_dual_config
|
VoltageDualConfig
|
Voltage dual configuration. |
required |
latency_dual_step_size
|
float
|
Latency dual step size (ρ_l). |
1.0
|
dt_s
|
Fraction
|
Control interval (seconds). |
Fraction(1)
|
sensitivity_update_interval
|
int
|
Re-estimate sensitivity every N control steps. |
0
|
sensitivity_perturbation_kw
|
float
|
Perturbation size for sensitivity estimation. |
100.0
|
Source code in openg2g/controller/ofo.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.
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.
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)
|