Getting started¶
Install, aggregate a toy game, explain a short prompt and check that the values add up.
Installation¶
Python 3.12 or newer:
pip install mllm-shap
From a clone (development uses uv):
git clone https://github.com/Pawlo77/MLLM-SHAP
cd MLLM-SHAP
make install
Your first aggregation¶
A coalition mask says which players are present; the payoff \(v(S)\) scores that coalition. Estimand aggregators turn masks and payoffs into attributions. On a three-player majority game Shapley and Banzhaf disagree:
from itertools import product
from mllm_shap import BanzhafAggregator, ShapleyAggregator
num_players = 3
masks = [tuple(bits) for bits in product([False, True], repeat=num_players)]
def majority_payoff(mask: tuple[bool, ...]) -> float:
return 1.0 if sum(mask) >= 2 else 0.0
payoffs = [majority_payoff(mask) for mask in masks]
shapley = ShapleyAggregator().aggregate(masks, payoffs)
banzhaf = BanzhafAggregator().aggregate(masks, payoffs)
print("Shapley:", shapley)
print("Banzhaf:", banzhaf)
Output
Shapley: [0.3333333333333333, 0.3333333333333333, 0.3333333333333333]
Banzhaf: [0.5, 0.5, 0.5]
Why they differ: Using estimand aggregators.
Explain a short prompt¶
Build a ConversationSnapshot and an ExplainConfig,
then call ExplainRunner. The mock backend needs no model, GPU or
extra:
import math
from mllm_shap import (
ConversationSnapshot,
ExplainConfig,
ExplainRunner,
Message,
Role,
Turn,
)
snapshot = ConversationSnapshot.from_turns((
Turn(messages=(Message(role=Role.USER, text="Cancel my old phone plan today"),)),
))
config = ExplainConfig.model_validate({
"backend": {"kind": "mock", "model_id": "stub"},
"explanation": {
"estimator": "exact",
"value_fn": "tfidf_cosine",
"absence_policy": "pad",
"archive": {"path": ""}, # keep this demo off disk
},
})
output = ExplainRunner(config).explain_sync(snapshot)
result = output.result
print("values:", tuple(round(value, 4) for value in result.values))
print("base_value v(empty):", round(result.base_value, 4))
print("grand_value v(N):", round(result.grand_value, 4))
# Efficiency: Shapley values sum to v(N) - v(empty).
print(
"efficient:",
math.isclose(sum(result.values), result.grand_value - result.base_value),
)
Output
values: (0.1667, 0.1667, 0.1667, 0.1667, 0.1667, 0.1667)
base_value v(empty): 0.0
grand_value v(N): 1.0
efficient: True
Each of the six words is a player. base_value scores the fully masked prompt,
grand_value the full prompt; Shapley values split the difference. With the default
identity normalizer this holds for every exact run; check it first when an
attribution looks wrong.
Without archive.path: "" the run is archived under ./runs/{run_id}; a re-run
raises ArchiveExistsError unless on_exists is overwrite
(Configuration).
Label results for archives¶
Each run carries a RunManifest with estimand, estimator, value
function and players. It round-trips through JSON:
from mllm_shap import parse_manifest
restored = parse_manifest(output.manifest.to_dict())
print("schema_version:", restored.schema_version)
print("estimand:", restored.estimand.value)
print("estimator:", restored.estimator)
print("players:", restored.player_ids)
Output
schema_version: 5
estimand: shapley
estimator: exact
players: ('c917d8ae9bbeef10:0:0:0', 'c917d8ae9bbeef10:0:0:1', 'c917d8ae9bbeef10:0:0:2', 'c917d8ae9bbeef10:0:0:3', 'c917d8ae9bbeef10:0:0:4', 'c917d8ae9bbeef10:0:0:5')
Which estimator?¶
exact scores all \(2^n\) coalitions: short prompts only. For longer inputs pick
leverage, proxy (extra proxy), kernel_banzhaf, mc, complementary
or neyman_cc with budget.per_player a few times the player count. Decision table:
Choosing an estimator; code: Using approximate estimators.
Next: How a run works (inside a run), Command line, Configuration, Generative backends, logging (Runtime core) and the notebooks in Examples.