Module mimir.attacks.all_attacks

Enum class for attacks. Also contains the base attack class.

Classes

class AllAttacks (value, names=None, *, module=None, qualname=None, type=None, start=1)
Expand source code
class AllAttacks(str, Enum):
    LOSS = "loss"
    REFERENCE_BASED = "ref"
    ZLIB = "zlib"
    MIN_K = "min_k"
    MIN_K_PLUS_PLUS = "min_k++"
    NEIGHBOR = "ne"
    GRADNORM = "gradnorm"
    RECALL = "recall"
    DC_PDD = "dc_pdd" 
    # QUANTILE = "quantile" # Uncomment when tested implementation is available

An enumeration.

Ancestors

  • builtins.str
  • enum.Enum

Class variables

var DC_PDD
var GRADNORM
var LOSS
var MIN_K
var MIN_K_PLUS_PLUS
var NEIGHBOR
var RECALL
var REFERENCE_BASED
var ZLIB
class Attack (config,
target_model: Model,
ref_model: Model = None,
is_blackbox: bool = True)
Expand source code
class Attack:
    def __init__(self, config, target_model: Model, ref_model: Model = None, is_blackbox: bool = True):
        self.config = config
        self.target_model = target_model
        self.ref_model = ref_model
        self.is_loaded = False
        self.is_blackbox = is_blackbox

    def load(self):
        """
        Any attack-specific steps (one-time) preparation
        """
        if self.ref_model is not None:
            self.ref_model.load()
            self.is_loaded = True

    def unload(self):
        if self.ref_model is not None:
            self.ref_model.unload()
            self.is_loaded = False

    def _attack(self, document, probs, tokens=None, **kwargs):
        """
        Actual logic for attack. 
        """
        raise NotImplementedError("Attack must implement attack()")

    def attack(self, document, probs, **kwargs):
        """
        Score a document using the attack's scoring function. Calls self._attack
        """
        # Load attack if not loaded yet
        if not self.is_loaded:
            self.load()
            self.is_loaded = True

        detokenized_sample = kwargs.get("detokenized_sample", None)
        if self.config.pretokenized and detokenized_sample is None:
            raise ValueError("detokenized_sample must be provided")

        score = (
            self._attack(document, probs=probs, **kwargs)
            if not self.config.pretokenized
            else self._attack(
                detokenized_sample, tokens=document, probs=probs, **kwargs
            )
        )

        return score

Subclasses

Methods

def attack(self, document, probs, **kwargs)
Expand source code
def attack(self, document, probs, **kwargs):
    """
    Score a document using the attack's scoring function. Calls self._attack
    """
    # Load attack if not loaded yet
    if not self.is_loaded:
        self.load()
        self.is_loaded = True

    detokenized_sample = kwargs.get("detokenized_sample", None)
    if self.config.pretokenized and detokenized_sample is None:
        raise ValueError("detokenized_sample must be provided")

    score = (
        self._attack(document, probs=probs, **kwargs)
        if not self.config.pretokenized
        else self._attack(
            detokenized_sample, tokens=document, probs=probs, **kwargs
        )
    )

    return score

Score a document using the attack's scoring function. Calls self._attack

def load(self)
Expand source code
def load(self):
    """
    Any attack-specific steps (one-time) preparation
    """
    if self.ref_model is not None:
        self.ref_model.load()
        self.is_loaded = True

Any attack-specific steps (one-time) preparation

def unload(self)
Expand source code
def unload(self):
    if self.ref_model is not None:
        self.ref_model.unload()
        self.is_loaded = False