Skip to content

Commit c66e168

Browse files
42logosmmschlkAdvueu963
authored
fix(InteractionValues): skip dense lookup build for sparse-dict input (#532)
* fix(InteractionValues): skip dense lookup build for sparse-dict input * Add Changelog. Refactor to use shallow copy of dict instead of deep copy. Sufficient as dict contains only immutable data. --------- Co-authored-by: Maximilian <maximilian.muschalik@gmail.com> Co-authored-by: Advueu963 <S.Thies@campus.lmu.de>
1 parent 33de51e commit c66e168

3 files changed

Lines changed: 46 additions & 13 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ The conversion of the tree methods has been moved to C++ giving at least 2x up t
102102
- fixes `min_order` in `TreeExplainer` so that it now actually restricts the returned `InteractionValues` to interactions of order ``min_order..max_order`` (``min_order=0`` continues to include the empty interaction at the baseline value); invalid values now raise a clear `ValueError`. [#325](https://github.com/mmschlk/shapiq/issues/325)
103103
- fixes tree conversion breaking when the `LC_NUMERIC` locale is not set to the standard `"C"` value. [#515](https://github.com/mmschlk/shapiq/pull/515)
104104
- fixes a segfault in the `ProxySHAP` C++ extension code. [#506](https://github.com/mmschlk/shapiq/pull/506)
105-
105+
- fixes a Out Of Memory (OOM) when values is an dictionary and n_players is large. [#531](https://github.com/mmschlk/shapiq/issues/531)
106106

107107
## v1.4.1 (2025-11-10)
108108

‎src/shapiq/interaction_values.py‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,20 +1189,20 @@ def _validate_and_return_interactions(
11891189
Raises:
11901190
TypeError: If the values or interaction_lookup are not of the expected types.
11911191
"""
1192-
interactions: dict[tuple[int, ...], float] = {}
1193-
if interaction_lookup is None:
1194-
interaction_lookup = generate_interaction_lookup(
1195-
players=n_players,
1196-
min_order=min_order,
1197-
max_order=max_order,
1198-
)
1199-
if interaction_lookup is not None and not isinstance(interaction_lookup, dict):
1200-
msg = f"Interaction lookup must be a dictionary. Got {type(interaction_lookup)}."
1201-
raise TypeError(msg)
1202-
1192+
interactions: dict[tuple[int, ...], float]
12031193
if isinstance(values, dict):
1204-
interactions = copy.deepcopy(values) # type: ignore[assignment]
1194+
# Shallow copy is sufficient, as keys and values are immutable (tuples[float,...] and float).
1195+
interactions = dict(values)
12051196
else:
1197+
if interaction_lookup is None:
1198+
interaction_lookup = generate_interaction_lookup(
1199+
players=n_players,
1200+
min_order=min_order,
1201+
max_order=max_order,
1202+
)
1203+
if not isinstance(interaction_lookup, dict):
1204+
msg = f"Interaction lookup must be a dictionary. Got {type(interaction_lookup)}."
1205+
raise TypeError(msg)
12061206
interactions = {
12071207
interaction: values[index].item() for interaction, index in interaction_lookup.items()
12081208
}

‎tests/shapiq/tests_unit/test_interaction_values.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,3 +929,36 @@ def test_from_empty_array():
929929
assert len(iv.interaction_lookup) == 1
930930
assert iv[()] == baseline_value
931931
assert iv.baseline_value == baseline_value
932+
933+
934+
def test_init_with_sparse_dict():
935+
"""Regression: a sparse-dict ``values`` input must not allocate the dense
936+
interaction lookup of size Σ C(n_players, k).
937+
938+
Before the fix, ``_validate_and_return_interactions`` unconditionally called
939+
``generate_interaction_lookup(n_players, min_order, max_order)`` even when
940+
``values`` was a dict (where the built lookup is then unused — the dict
941+
branch only does ``copy.deepcopy(values)``). For n_players=30 and
942+
max_order=30 that lookup would have 2**30 ~ 1e9 entries and OOM.
943+
944+
The fix skips the build when ``values`` is a dict. Pre-fix, this test
945+
crashes with MemoryError; post-fix it completes instantly.
946+
"""
947+
sparse_values = {(0,): 1.0, (1,): 2.0, (0, 1): 3.0}
948+
949+
iv = InteractionValues(
950+
values=sparse_values,
951+
index="SV",
952+
max_order=30,
953+
n_players=30,
954+
min_order=1,
955+
baseline_value=0.0,
956+
)
957+
958+
assert iv.interactions == sparse_values
959+
assert iv.n_players == 30
960+
assert iv.max_order == 30
961+
# ``interaction_lookup`` is a property derived from ``self.interactions``;
962+
# it should only contain keys actually present in ``values``, not the
963+
# full enumeration of Σ C(n_players, k) interactions.
964+
assert set(iv.interaction_lookup.keys()) == set(sparse_values.keys())

0 commit comments

Comments
 (0)