-
Notifications
You must be signed in to change notification settings - Fork 80
perf: default integer arrays to int32 for ~25% memory reduction #566
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
FBumann
wants to merge
11
commits into
PyPSA:master
Choose a base branch
from
fluxopt:perf/int32
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0331b34
Changes
FBumann b5df113
Add memory becnhmark
FBumann d0a8c74
bench: improve benchmark_lp_writer.py
FBumann f1746e9
- linopy/variables.py: ffill, bfill, sanitize — labels were cast ba…
FBumann 2f3e87e
Add dtype tests
FBumann dfd4ad5
All join="override" usages in tests have been replaced with assign_co…
FBumann 532126d
Delete dev-scripts/benchmark_lp_writer.py
FBumann 4abee58
Move label_dtype to options for runtime configurability
FBumann ee31b8c
Revert int32 for dimension coordinates to fix build regression
FBumann 52e3185
Merge remote-tracking branch 'origin/master' into perf/int32-resolve
FBumann f7bad31
perf(int32): respect label_dtype when reconstructing CSR constraints
FBumann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| """Tests for int32 default label dtype.""" | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
|
|
||
| from linopy import Model | ||
| from linopy.config import options | ||
|
|
||
|
|
||
| def test_default_label_dtype_is_int32() -> None: | ||
| assert options["label_dtype"] == np.int32 | ||
|
|
||
|
|
||
| def test_variable_labels_are_int32() -> None: | ||
| m = Model() | ||
| x = m.add_variables(lower=0, upper=10, coords=[range(5)], name="x") | ||
| assert x.labels.dtype == np.int32 | ||
|
|
||
|
|
||
| def test_constraint_labels_are_int32() -> None: | ||
| m = Model() | ||
| x = m.add_variables(lower=0, upper=10, coords=[range(5)], name="x") | ||
| m.add_constraints(x >= 1, name="c") | ||
| assert m.constraints["c"].labels.dtype == np.int32 | ||
|
|
||
|
|
||
| def test_expression_vars_are_int32() -> None: | ||
| m = Model() | ||
| x = m.add_variables(lower=0, upper=10, coords=[range(5)], name="x") | ||
| expr = 2 * x + 1 | ||
| assert expr.vars.dtype == np.int32 | ||
|
|
||
|
|
||
| @pytest.mark.skipif( | ||
| not pytest.importorskip("highspy", reason="highspy not installed"), | ||
| reason="highspy not installed", | ||
| ) | ||
| def test_solve_with_int32_labels() -> None: | ||
| m = Model() | ||
| x = m.add_variables(lower=0, upper=10, name="x") | ||
| y = m.add_variables(lower=0, upper=10, name="y") | ||
| m.add_constraints(x + y <= 15, name="c1") | ||
| m.add_objective(x + 2 * y, sense="max") | ||
| m.solve("highs") | ||
| assert m.objective.value == pytest.approx(25.0) | ||
|
|
||
|
|
||
| def test_overflow_guard_variables() -> None: | ||
| m = Model() | ||
| m._xCounter = np.iinfo(np.int32).max - 1 | ||
| with pytest.raises(ValueError, match="exceeds the maximum"): | ||
| m.add_variables(lower=0, upper=1, coords=[range(5)], name="x") | ||
|
|
||
|
|
||
| def test_overflow_guard_constraints() -> None: | ||
| m = Model() | ||
| x = m.add_variables(lower=0, upper=1, coords=[range(5)], name="x") | ||
| m._cCounter = np.iinfo(np.int32).max - 1 | ||
| with pytest.raises(ValueError, match="exceeds the maximum"): | ||
| m.add_constraints(x >= 0, name="c") | ||
|
|
||
|
|
||
| def test_label_dtype_option_int64() -> None: | ||
| with options: | ||
| options["label_dtype"] = np.int64 | ||
| m = Model() | ||
| x = m.add_variables(lower=0, upper=10, coords=[range(5)], name="x") | ||
| assert x.labels.dtype == np.int64 | ||
| expr = 2 * x + 1 | ||
| assert expr.vars.dtype == np.int64 | ||
|
|
||
|
|
||
| def test_label_dtype_rejects_invalid() -> None: | ||
| with pytest.raises(ValueError, match="label_dtype must be one of"): | ||
| options["label_dtype"] = np.float64 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was wondering about a auto-adjustment of the datatype here. but not sure if that would lead to problems. what do you think?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Im not sure if I understand your intend correctly. I assume you mean setting the dtype based on the model size, so a user never sees an error when the model is too large for int32?
If so: I evaluated it and decided against it because:
I chose reliability over convenience here. I think there wont be many users hitting the int32 limits. And the fix is very simple and documented in the error messag.