Masking views¶
A partitioner splits a conversation into players; an absence policy renders the prompt a coalition sees. Both run on “Cancel my old phone plan today”, then message players and config. The snippets share one snapshot.
Partition tokens¶
from mllm_shap import ConversationSnapshot, Message, Role, Turn
from mllm_shap.masking import TokenPartitioner
turn = Turn(messages=(Message(role=Role.USER, text="Cancel my old phone plan today"),))
snapshot = ConversationSnapshot(turns=(turn,), snapshot_id="demo-snap")
players = TokenPartitioner().partition(snapshot)
print(players.num_players, "players:", players.player_ids[0], "...", players.player_ids[-1])
Output
6 players: demo-snap:0:0:0 ... demo-snap:0:0:5
Six words, six players, \(2^6 = 64\) coalitions. Ids come from snapshot id and token position: stable, in mask-bit order in the manifest.
Absence policies¶
A CoalitionMask marks present players;
MaskBuilder renders the prompt for the backend. Coalition
{Cancel, phone, plan} under three policies:
from mllm_shap import CoalitionMask
from mllm_shap.masking import DeletePolicy, MaskBuilder, NeutralPolicy, PadPolicy
mask = CoalitionMask.from_sequence((True, False, False, True, True, False))
for policy in (DeletePolicy(), PadPolicy(), NeutralPolicy()):
builder = MaskBuilder(policy)
rendered = builder.render(builder.view(snapshot, players, mask))
print(f"{policy.name:8s}{rendered.turns[0].messages[0].text}")
Output
delete Cancel phone plan
pad Cancel [MASK] [MASK] phone plan [MASK]
neutral Cancel __ ___ phone plan _____
infill asks a masked language model for each absent word (extra infill, default
roberta-base):
from mllm_shap.masking import InfillPolicy
builder = MaskBuilder(InfillPolicy())
print(builder.render(builder.view(snapshot, players, mask)).turns[0].messages[0].text)
Cancel your cell phone plan .
Recorded with mllm-shap 0.11.1 and transformers 5.24.1; the docs build has no model weights to re-run it.
Policy |
Absent word becomes |
Options |
|---|---|---|
|
Nothing; the separating whitespace goes too ( |
none |
|
One placeholder per word |
|
|
A filler of the same width |
|
|
The model’s top word; the same coalition always renders the same text, so removal stays on-manifold |
|
Policies edit only the partitioner’s spans (PlayerSet.spans) and never re-tokenise;
other text and whitespace stay. delete cuts audio players (Audio alignment), the
others silence them; image fills: Image input. Trade-offs:
Masking.
Message players¶
players: messages (MessagePartitioner) makes each
non-empty message a player labelled "<role>[/<speaker>] <n>: " plus its first 30
characters (n from 1). delete empties an absent message; pad and neutral
replace its text. Option roles (default: all) keeps other roles, such as the system
prompt, always present; players: tokens takes it too:
from mllm_shap import ExplainConfig
from mllm_shap.masking import MessagePartitioner
from mllm_shap.plugins import PluginContext
chat = ConversationSnapshot.from_turns((
Turn(
messages=(
Message(role=Role.SYSTEM, text="Answer briefly."),
Message(role=Role.USER, text="Is the sky blue?", speaker="ana"),
Message(role=Role.USER, text="Why?", speaker="ben"),
)
),
))
config = ExplainConfig.model_validate({
"backend": {"kind": "mock", "model_id": "stub"},
"explanation": {
"players": "messages",
"plugin_options": {"messages": {"roles": ["user"]}},
},
})
options = config.explanation.plugin_options["messages"]
chat_players = MessagePartitioner(
PluginContext(config=config, options=options)
).partition(chat)
for span in chat_players.spans:
print(span.display_label)
view = MaskBuilder(DeletePolicy()).view(
chat, chat_players, CoalitionMask.from_sequence((False, True))
)
print([m.text for m in MaskBuilder(DeletePolicy()).render(view).messages()])
Output
user/ana 2: Is the sky blue?
user/ben 3: Why?
['Answer briefly.', '', 'Why?']
Resolve plugins from config¶
Built-ins: the tokens, sentences and messages partitions (audio and image:
Audio alignment, Image input) and the four policies above.
ExplainRunner resolves them from explanation.players and
explanation.absence_policy:
from mllm_shap import ExplainConfig, PluginGroup, PluginRegistry
from mllm_shap.plugins import PluginContext, register_builtin_plugins
registry = PluginRegistry()
register_builtin_plugins(registry)
config = ExplainConfig.model_validate({
"backend": {"kind": "mock", "model_id": "stub"},
"explanation": {"players": "tokens", "absence_policy": "pad"},
})
context = PluginContext(config=config, registry=registry)
partitioner = registry.resolve(
PluginGroup.PARTITIONS, config.explanation.players, context
)
policy = registry.resolve(
PluginGroup.ABSENCE_POLICIES, config.explanation.absence_policy, context
)
print(type(partitioner).__name__)
print(type(policy).__name__)
Output
TokenPartitioner
PadPolicy
Schema: Configuration. Your own partitioner or policy: Extending mllm-shap. Notebook: Coalition masking and absence policies.