Source code for mllm_shap.runtime.dedup

"""Coalition deduplication keys and registry.

@domain runtime
@see [[knowledge/architecture/runtime]]
@tested-by [[tests/runtime/test_dedup.py]]
"""

import hashlib
import json
import struct
from collections.abc import Mapping
from dataclasses import dataclass

from ..domain.config import DedupConfig, GenerationConfig
from ..masking.codec import MaskCodec, PackedMask


[docs] def dedup_enabled(config: DedupConfig, generation: GenerationConfig) -> bool: """Return whether coalition deduplication should be active. ``auto`` is on at any temperature: a coalition is generated once per run and reused, so repeated masks share one payoff and Shapley efficiency holds under sampled decoding. """ match config.enabled: case "on": return True case "off": return False case "auto": _ = generation return True case _ as unreachable: msg = f"unsupported dedup mode: {unreachable!r}" raise AssertionError(msg)
def _hash_packed_mask(packed: PackedMask) -> bytes: """Return stable bytes for a packed coalition mask.""" return struct.pack("<I", packed.n_bits) + packed.words
[docs] def build_coalition_key( snapshot_id: str, player_ids: tuple[str, ...], mask_present: tuple[bool, ...], absence_policy: str, model_id: str, generation: GenerationConfig, draw: int = 0, policy_options: Mapping[str, object] | None = None, processor_kwargs: Mapping[str, object] | None = None, ) -> str: """Build a stable SHA256 coalition key for deduplication. ``draw`` numbers the independent samples of one coalition; ``policy_options`` are the absence policy's non-default options and ``processor_kwargs`` the backend's processor arguments. Draw 0 and no options keep the key of earlier runs, so their archives still replay. """ packed = MaskCodec.pack(mask_present) hasher = hashlib.sha256() hasher.update(snapshot_id.encode("utf-8")) hasher.update(b"\x1e") for player_id in player_ids: hasher.update(player_id.encode("utf-8")) hasher.update(b"\x1f") hasher.update(_hash_packed_mask(packed)) hasher.update(absence_policy.encode("utf-8")) hasher.update(model_id.encode("utf-8")) hasher.update( struct.pack( "<ifi", generation.max_new_tokens, generation.temperature, generation.top_k, ) ) if generation.content_order != "legacy": hasher.update(b"\x1b" + generation.content_order.encode("utf-8")) if policy_options: options = json.dumps(policy_options, sort_keys=True, separators=(",", ":")) hasher.update(b"\x1c" + options.encode("utf-8")) if processor_kwargs: kwargs = json.dumps(processor_kwargs, sort_keys=True, separators=(",", ":")) hasher.update(b"\x1a" + kwargs.encode("utf-8")) if draw: hasher.update(b"\x1d" + struct.pack("<i", draw)) return hasher.hexdigest()
[docs] @dataclass class CoalitionDedupRegistry: """Track coalition keys that already required backend execution.""" def __init__(self) -> None: """Start with no observed coalition keys.""" self._seen: set[str] = set()
[docs] def observe(self, coalition_key: str) -> bool: """Record a coalition key and return whether it is newly observed.""" is_new = coalition_key not in self._seen if is_new: self._seen.add(coalition_key) return is_new
[docs] def contains(self, coalition_key: str) -> bool: """Return whether a coalition key was already observed.""" return coalition_key in self._seen
def __len__(self) -> int: """Return the number of unique coalition keys observed.""" return len(self._seen)