Commit a6b7103
perf: reduce memory usage in build_model (sparse coefficients + per-effect share constraints) (#595)
* fix: memory issues due to dense large coeficients
1. flixopt/features.py — Added sparse_multiply_sum() function that takes a sparse dict of (group_id, sum_id) -> coefficient instead of a dense DataArray. This avoids ever
allocating the massive dense array.
2. flixopt/elements.py — Replaced _coefficients (dense DataArray) and _flow_sign (dense DataArray) with a single _signed_coefficients cached property that returns
dict[tuple[str, str], float | xr.DataArray] containing only non-zero signed coefficients. Updated create_linear_constraints to use sparse_multiply_sum instead of
sparse_weighted_sum.
The dense allocation at line 2385 (np.zeros(n_conv, max_eq, n_flows, *time) ~14.5 GB) is completely eliminated. Memory usage is now proportional to the number of non-zero
entries (typically 2-3 flows per converter) rather than the full cartesian product.
* fix(effects): avoid massive memory allocation in share variable creation
Replace linopy.align(join='outer') with per-contributor accumulation
and linopy.merge(dim='contributor'). The old approach reindexed ALL
dimensions via xr.where(), allocating ~12.7 GB of dense arrays.
Now contributions are split by contributor at registration time and
accumulated via linopy addition (cheap for same-shape expressions),
then merged along the disjoint contributor dimension.
* Switch to per contributor constraints to solve memmory issues
* fix(effects): avoid massive memory allocation in share variable creation
Replace linopy.align(join='outer') with per-contributor accumulation
and individual constraints. The old approach reindexed ALL dimensions
via xr.where(), allocating ~12.7 GB of dense arrays.
Now contributions are split by contributor at registration time and
accumulated via linopy addition (cheap for same-shape expressions).
Each contributor gets its own constraint, avoiding any cross-contributor
alignment. Reduces effects expression memory from 1.2 GB to 5 MB.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* Switch to per contributor constraints to solve memmory issues
* perf: improve bus balance to be more memmory efficient
* Switch to per effect shares
* Firs succesfull drop to 10 GB
* Make more readable
* Go back to one variable for all shares
* ⏺ Instead of adding zero-constraints for uncovered combos, we should just set lower=0, upper=0 on those entries (fix the bounds), or better yet — use a mask on the per-effect
constraints and set the variable bounds to 0 for uncovered combos. The simplest fix: create the variable with lower=0, upper=0 by default, then only the covered entries need
constraints.
* Only create variables needed
* _create_share_var went from 1,674ms → 116ms — a 14x speedup! The reindex + + approach is much faster than per-contributor sel + merge
* Revert
* Revert
* 1. effects.py: add_temporal_contribution and add_periodic_contribution now raise ValueError if a DataArray has no effect dimension and no effect= argument is provided.
2. statistics_accessor.py: Early return with empty xr.Dataset() when no contributors are detected, preventing xr.concat from failing on an empty list.
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>1 parent 701f7af commit a6b7103
9 files changed
Lines changed: 367 additions & 214 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
917 | 917 | | |
918 | 918 | | |
919 | 919 | | |
920 | | - | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
921 | 925 | | |
922 | 926 | | |
923 | 927 | | |
924 | 928 | | |
925 | | - | |
926 | | - | |
927 | | - | |
928 | | - | |
929 | | - | |
930 | | - | |
931 | | - | |
932 | | - | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
933 | 949 | | |
934 | 950 | | |
935 | 951 | | |
936 | | - | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
| 961 | + | |
937 | 962 | | |
938 | | - | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
939 | 973 | | |
940 | 974 | | |
941 | 975 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
345 | 345 | | |
346 | 346 | | |
347 | 347 | | |
348 | | - | |
349 | | - | |
350 | | - | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
351 | 351 | | |
352 | 352 | | |
353 | 353 | | |
| |||
361 | 361 | | |
362 | 362 | | |
363 | 363 | | |
364 | | - | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
365 | 370 | | |
366 | 371 | | |
367 | 372 | | |
368 | | - | |
369 | | - | |
| 373 | + | |
370 | 374 | | |
| 375 | + | |
371 | 376 | | |
372 | 377 | | |
373 | 378 | | |
374 | 379 | | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
375 | 386 | | |
376 | 387 | | |
377 | | - | |
| 388 | + | |
378 | 389 | | |
379 | | - | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
380 | 396 | | |
381 | 397 | | |
382 | 398 | | |
383 | | - | |
384 | | - | |
| 399 | + | |
385 | 400 | | |
| 401 | + | |
386 | 402 | | |
387 | 403 | | |
388 | 404 | | |
389 | 405 | | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
390 | 412 | | |
391 | 413 | | |
392 | | - | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
393 | 434 | | |
394 | 435 | | |
395 | 436 | | |
| |||
542 | 583 | | |
543 | 584 | | |
544 | 585 | | |
545 | | - | |
546 | | - | |
547 | | - | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
548 | 589 | | |
549 | 590 | | |
550 | 591 | | |
551 | 592 | | |
552 | 593 | | |
553 | 594 | | |
554 | | - | |
555 | | - | |
556 | | - | |
557 | | - | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
558 | 599 | | |
559 | 600 | | |
560 | 601 | | |
| |||
573 | 614 | | |
574 | 615 | | |
575 | 616 | | |
576 | | - | |
| 617 | + | |
577 | 618 | | |
578 | 619 | | |
579 | 620 | | |
580 | | - | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
581 | 628 | | |
582 | | - | |
583 | | - | |
| 629 | + | |
| 630 | + | |
584 | 631 | | |
585 | 632 | | |
586 | 633 | | |
587 | | - | |
588 | | - | |
589 | | - | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
590 | 644 | | |
591 | | - | |
592 | | - | |
593 | | - | |
594 | | - | |
595 | | - | |
596 | | - | |
597 | | - | |
598 | | - | |
599 | | - | |
600 | | - | |
601 | | - | |
602 | | - | |
603 | | - | |
604 | | - | |
605 | 645 | | |
606 | | - | |
607 | 646 | | |
608 | | - | |
| 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 | + | |
609 | 678 | | |
610 | 679 | | |
611 | 680 | | |
| |||
0 commit comments