Utilities API¶
Graph Utilities¶
OPFHomoWrapper
¶
Wrapper using PyG's to_homogeneous() for OPF hetero-to-homo conversion.
Converts HeteroData objects into Data objects suitable for
homogeneous GNN training, preserving node/edge type indicators and
optionally storing padded edge attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
add_node_type
|
bool
|
Whether to add |
True
|
add_edge_type
|
bool
|
Whether to add |
True
|
dummy_values
|
bool
|
Whether to fill missing attributes with zeros. |
True
|
attach_full_edge_attr
|
bool
|
Whether to compute and store
|
False
|
Source code in lumina/utils/graph_utils.py
25 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 | |
convert(hetero_data: HeteroData) -> Data
¶
Convert heterogeneous OPF data to homogeneous format via PyG.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hetero_data
|
HeteroData
|
Input heterogeneous graph. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Data |
Data
|
Homogeneous graph with |
Source code in lumina/utils/graph_utils.py
OPFHeteroWrapper
¶
Wrapper that runs a homogeneous GNN on heterogeneous OPF inputs.
Instead of using to_hetero() (which can cause torch.fx issues),
this wrapper converts heterogeneous inputs to a simplified homogeneous
format, runs the underlying model, and returns outputs keyed by node
type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
Homogeneous GNN model instance. |
required |
metadata
|
tuple
|
Graph metadata |
required |
aggr
|
str
|
Aggregation method for combining embeddings from different relations (currently unused; reserved for future use). |
'sum'
|
Source code in lumina/utils/graph_utils.py
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 | |
__call__(x_dict, edge_index_dict, edge_attr_dict=None)
¶
Forward pass: converts hetero inputs to homo, runs model, returns per-type dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_dict
|
dict[str, Tensor]
|
Per-node-type feature tensors. |
required |
edge_index_dict
|
dict[tuple, Tensor]
|
Per-edge-type connectivity tensors. |
required |
edge_attr_dict
|
dict[tuple, Tensor]
|
Per-edge-type attribute tensors. |
None
|
Returns:
| Type | Description |
|---|---|
|
dict[str, torch.Tensor]: Model output keyed by the primary node type. |
Source code in lumina/utils/graph_utils.py
parameters()
¶
train()
¶
eval()
¶
HomoOPFDataset
¶
Dataset wrapper that converts OPF hetero graphs to homogeneous on-the-fly.
Wraps an existing OPF dataset and applies OPFHomoWrapper.convert()
to each sample at access time. Non-finite targets are optionally
sanitized (replaced with zeros) and flagged via a y_mask attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
opf_dataset
|
Original OPFDataset or |
required | |
add_node_type
|
bool
|
Whether to include node type information. |
True
|
add_edge_type
|
bool
|
Whether to include edge type information. |
True
|
sanitize_targets
|
bool
|
If |
True
|
log_bad_targets
|
bool
|
If |
True
|
max_bad_target_logs
|
int
|
Maximum number of bad-target warnings to emit per dataset instance. |
1
|
Source code in lumina/utils/graph_utils.py
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 | |
HeteroToHomoConverter
¶
Converts heterogeneous OPF graphs to homogeneous format with feature projection.
Unlike OPFHomoWrapper (which delegates to PyG's to_homogeneous),
this class manually projects node and edge features to fixed-width
unified feature spaces, builds global node/edge indices, and preserves
type indicators.
Conversion strategy
- Project or pad/truncate all node features to
node_dim. - Add integer
node_typeindicators. - Convert all edge types to a single type with
edge_typeindicators. - Preserve targets for appropriate node/edge types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_dim
|
int
|
Target dimension for unified node features. |
64
|
edge_dim
|
int
|
Target dimension for unified edge features. |
32
|
use_node_type_embedding
|
bool
|
Whether to add learnable node type embeddings (reserved for future use). |
True
|
use_edge_type_embedding
|
bool
|
Whether to add learnable edge type embeddings (reserved for future use). |
True
|
Source code in lumina/utils/graph_utils.py
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 | |
convert(hetero_data: HeteroData) -> Data
¶
Convert a heterogeneous graph to a homogeneous graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hetero_data
|
HeteroData
|
Input heterogeneous graph with per-type node/edge features and targets. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Data |
Data
|
Homogeneous graph with unified |
Source code in lumina/utils/graph_utils.py
prepare_opf_training_data(dataset, train_ratio: float = 0.8, val_ratio: float = 0.1, test_ratio: float = 0.1, use_homogeneous: bool = True)
¶
Split an OPF dataset into train/val/test subsets.
Creates random index-based subsets and optionally wraps each in
HomoOPFDataset for homogeneous model training.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
OPFDataset instance with |
required | |
train_ratio
|
float
|
Fraction of data for training. |
0.8
|
val_ratio
|
float
|
Fraction of data for validation. |
0.1
|
test_ratio
|
float
|
Fraction of data for testing. |
0.1
|
use_homogeneous
|
bool
|
If |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
If ratios do not sum to 1.0. |
Source code in lumina/utils/graph_utils.py
get_opf_metadata(dataset)
¶
Extract graph metadata from an OPF dataset.
Retrieves the (node_types, edge_types) tuple needed by
to_hetero() and other heterogeneous model utilities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
OPFDataset instance supporting indexing. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
|
Source code in lumina/utils/graph_utils.py
convert_opf_to_homo(hetero_data: HeteroData, node_dim: int = 64, edge_dim: int = 32) -> Data
¶
Convert OPF heterogeneous data to homogeneous format.
Convenience function that creates a HeteroToHomoConverter and calls
its convert method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hetero_data
|
HeteroData
|
Input heterogeneous OPF graph. |
required |
node_dim
|
int
|
Target unified node feature dimension. |
64
|
edge_dim
|
int
|
Target unified edge feature dimension. |
32
|
Returns:
| Name | Type | Description |
|---|---|---|
Data |
Data
|
Homogeneous graph data object. |
Source code in lumina/utils/graph_utils.py
Throughput Tracking¶
ThroughputTracker
¶
Measures training throughput (samples/sec) over a configurable window.
After a configurable warmup period, records per-step timing for a fixed number of measurement steps, then computes and logs the mean throughput across all DDP ranks. Results are optionally logged to W&B and written as JSON metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict
|
Full training configuration. Throughput settings are
read from |
required |
world_size
|
int
|
Total number of DDP processes. |
required |
global_rank
|
int
|
Rank of the current process. |
required |
get_global_step
|
callable
|
Zero-argument callable returning the current global step count (used as W&B x-axis). |
required |
wandb_enabled
|
bool
|
Whether to log metrics to W&B. |
False
|
Source code in lumina/utils/throughput.py
19 20 21 22 23 24 25 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 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 | |
set_wandb_enabled(enabled)
¶
Enable or disable W&B logging for throughput metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enabled
|
bool
|
|
required |
Source code in lumina/utils/throughput.py
write_metadata()
¶
Write environment and configuration metadata to a JSON file (rank-0 only).
Source code in lumina/utils/throughput.py
maybe_start_measurement()
¶
Begin measurement if warmup is complete and measurement has not yet run.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
|
Source code in lumina/utils/throughput.py
measure_active()
¶
Return whether throughput measurement is currently in progress.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
|
accelerator_synchronize()
¶
Synchronize the current accelerator device for accurate timing.
get_batch_samples(batch)
¶
Determine the number of samples in a batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
PyG batch object or plain tensor. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
Number of samples in the batch. |
Source code in lumina/utils/throughput.py
record_step(step_metrics)
¶
Record a single step's throughput metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
step_metrics
|
dict
|
Metrics for the step, must include
|
required |
Source code in lumina/utils/throughput.py
on_step_end(step_metrics=None)
¶
Called at the end of each training step to record metrics and check completion.
Automatically calls finalize once the measurement window is filled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
step_metrics
|
dict
|
Throughput metrics for this step. |
None
|
Source code in lumina/utils/throughput.py
finalize(partial=False)
¶
Compute and log the final throughput summary across all ranks.
Gathers per-step samples/sec from all DDP ranks, computes the global mean, and logs to W&B if enabled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
partial
|
bool
|
If |
False
|
Source code in lumina/utils/throughput.py
Model Utilities¶
set_seed(seed)
¶
Set random seeds for reproducibility across all backends.
Configures Python random, NumPy, PyTorch CPU, and (if available)
PyTorch CUDA random number generators. Also sets cuDNN to deterministic
mode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
int
|
Random seed value. |
required |
Source code in lumina/utils/model.py
dict_agg(stats, key, value, op='concat')
¶
Aggregate a value into a dictionary entry by summing or concatenating.
If key already exists in stats, the value is combined with the existing entry using the specified operation. Otherwise, the value is stored directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stats
|
dict
|
Dictionary to update in place. |
required |
key
|
str
|
Key in the dictionary. |
required |
value
|
ndarray
|
Value to add or concatenate. |
required |
op
|
str
|
Operation type -- |
'concat'
|
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If op is not |
Source code in lumina/utils/model.py
I/O¶
check_dir(dir_path)
¶
Ensure a directory exists, creating it (and parents) if necessary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dir_path
|
str
|
Path to the directory to verify or create. |
required |