File size: 9,319 Bytes
edeca31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
"""Base class for Membership Inference Attacks."""

from abc import ABC, abstractmethod
from typing import Dict, Optional, Literal

import numpy as np

from mia.experiment import MIAExperiment
from mia.predictions import PredictionCache

AccessLevel = Literal["blackbox", "greybox", "whitebox"]


class BaseAttack(ABC):
    """
    Abstract base class for MIA attacks.

    Defines the interface for all attack implementations and provides
    common functionality for loading data.
    """

    def __init__(
        self,
        experiment: MIAExperiment,
        prediction_cache: PredictionCache,
        access_level: AccessLevel = "blackbox",
    ):
        """
        Initialize attack.

        Args:
            experiment: MIA experiment with membership matrices
            prediction_cache: Cache with model predictions
            access_level: Access level for the attack
        """
        self.experiment = experiment
        self.prediction_cache = prediction_cache
        self.access_level = access_level

    @property
    @abstractmethod
    def name(self) -> str:
        """Attack name for logging/display."""
        pass

    @abstractmethod
    def run(
        self,
        target_split: str = "target_test",
        num_target_models: Optional[int] = None,
        verbose: bool = True,
    ) -> Dict[str, np.ndarray]:
        """
        Run the attack on a target split.

        Args:
            target_split: Split to attack (target_train, target_val, target_test)
            num_target_models: Number of target models (None = all)
            verbose: Print progress

        Returns:
            Dictionary with:
                'scores': (num_samples,) aggregated membership scores
                'labels': (num_samples,) true membership labels
                'per_model_scores': (num_samples, num_models) individual scores
        """
        pass

    def get_shadow_data(self) -> Dict[str, np.ndarray]:
        """
        Get shadow model data for attack calibration.

        Returns:
            Dictionary with shadow model losses and membership matrix.
        """
        shadow_losses = self.prediction_cache.store["shadow"]["losses"][:, :]
        shadow_membership = self.experiment.get_membership_matrix("shadow").matrix
        return {
            "losses": shadow_losses,
            "membership": shadow_membership,
        }

    def get_target_data(
        self, split: str, num_models: Optional[int] = None
    ) -> Dict[str, np.ndarray]:
        """
        Get target model data for the attack.

        Args:
            split: Target split name
            num_models: Number of models (None = all)

        Returns:
            Dictionary with target losses, logits, and membership.
        """
        membership_matrix = self.experiment.get_membership_matrix(split)

        if num_models is None:
            num_models = membership_matrix.num_models

        target_losses = self.prediction_cache.store[split]["losses"][:, :num_models]
        target_logits = self.prediction_cache.store[split]["logits"][:, :num_models, :]

        return {
            "losses": target_losses,
            "logits": target_logits,
            "membership": membership_matrix,
            "num_models": num_models,
        }

    def compute_in_out_statistics(
        self,
        shadow_losses: np.ndarray,
        shadow_membership: np.ndarray,
    ) -> Dict[str, np.ndarray]:
        """
        Compute IN/OUT loss statistics from shadow models.

        Args:
            shadow_losses: (num_samples, num_shadow_models)
            shadow_membership: (num_samples, num_shadow_models) boolean

        Returns:
            Dictionary with in_mean, in_std, out_mean, out_std
        """
        in_mask = shadow_membership
        out_mask = ~shadow_membership

        in_losses_masked = np.where(in_mask, shadow_losses, np.nan)
        out_losses_masked = np.where(out_mask, shadow_losses, np.nan)

        return {
            "in_mean": np.nanmean(in_losses_masked, axis=1),
            "in_std": np.nanstd(in_losses_masked, axis=1) + 1e-10,
            "out_mean": np.nanmean(out_losses_masked, axis=1),
            "out_std": np.nanstd(out_losses_masked, axis=1) + 1e-10,
        }


class GreyBoxAttack(BaseAttack):
    """
    Base class for grey-box attacks with activation access.

    Extends BaseAttack with methods to load and use activation features.
    """

    def __init__(
        self,
        experiment: MIAExperiment,
        prediction_cache: PredictionCache,
        activation_cache=None,
    ):
        super().__init__(experiment, prediction_cache, access_level="greybox")
        self.activation_cache = activation_cache

    def get_shadow_activations(self) -> np.ndarray:
        """Get shadow model activations."""
        if self.activation_cache is None:
            raise ValueError("Activation cache not provided")
        return self.activation_cache.store["shadow"]["features"][:, :]

    def get_target_activations(
        self, split: str, num_models: Optional[int] = None
    ) -> np.ndarray:
        """Get target model activations."""
        if self.activation_cache is None:
            raise ValueError("Activation cache not provided")
        if num_models is None:
            return self.activation_cache.store[split]["features"][:, :]
        return self.activation_cache.store[split]["features"][:, :num_models, :]

    def compute_activation_statistics(
        self,
        shadow_activations: np.ndarray,
        shadow_membership: np.ndarray,
    ) -> Dict[str, np.ndarray]:
        """
        Compute IN/OUT activation statistics from shadow models.

        Args:
            shadow_activations: (num_samples, num_shadow_models, feature_dim)
            shadow_membership: (num_samples, num_shadow_models) boolean

        Returns:
            Dictionary with in_act_mean, in_act_std, out_act_mean, out_act_std
        """
        in_mask = shadow_membership[:, :, np.newaxis]  # (N, M, 1)
        out_mask = ~shadow_membership[:, :, np.newaxis]

        in_act_masked = np.where(in_mask, shadow_activations, np.nan)
        out_act_masked = np.where(out_mask, shadow_activations, np.nan)

        return {
            "in_act_mean": np.nanmean(in_act_masked, axis=1),  # (N, D)
            "in_act_std": np.nanstd(in_act_masked, axis=1) + 1e-10,
            "out_act_mean": np.nanmean(out_act_masked, axis=1),
            "out_act_std": np.nanstd(out_act_masked, axis=1) + 1e-10,
        }


class WhiteBoxAttack(GreyBoxAttack):
    """
    Base class for white-box attacks with gradient access.

    Extends GreyBoxAttack with methods to load and use gradient features.
    """

    def __init__(
        self,
        experiment: MIAExperiment,
        prediction_cache: PredictionCache,
        activation_cache=None,
        gradient_cache=None,
    ):
        super().__init__(experiment, prediction_cache, activation_cache)
        self.access_level = "whitebox"
        self.gradient_cache = gradient_cache

    def get_shadow_gradients(self) -> Dict[str, np.ndarray]:
        """Get shadow model gradient features."""
        if self.gradient_cache is None:
            raise ValueError("Gradient cache not provided")
        grp = self.gradient_cache.store["shadow"]
        return {
            "grad_norms": grp["grad_norms"][:, :],
            "grad_dot_weight": grp["grad_dot_weight"][:, :],
            "grad_features": grp["grad_features"][:, :, :],
        }

    def get_target_gradients(
        self, split: str, num_models: Optional[int] = None
    ) -> Dict[str, np.ndarray]:
        """Get target model gradient features."""
        if self.gradient_cache is None:
            raise ValueError("Gradient cache not provided")
        grp = self.gradient_cache.store[split]
        if num_models is None:
            return {
                "grad_norms": grp["grad_norms"][:, :],
                "grad_dot_weight": grp["grad_dot_weight"][:, :],
                "grad_features": grp["grad_features"][:, :, :],
            }
        return {
            "grad_norms": grp["grad_norms"][:, :num_models],
            "grad_dot_weight": grp["grad_dot_weight"][:, :num_models],
            "grad_features": grp["grad_features"][:, :num_models, :],
        }

    def compute_gradient_statistics(
        self,
        shadow_grad_norms: np.ndarray,
        shadow_membership: np.ndarray,
    ) -> Dict[str, np.ndarray]:
        """
        Compute IN/OUT gradient norm statistics from shadow models.

        Args:
            shadow_grad_norms: (num_samples, num_shadow_models)
            shadow_membership: (num_samples, num_shadow_models) boolean

        Returns:
            Dictionary with in_grad_mean, in_grad_std, out_grad_mean, out_grad_std
        """
        in_mask = shadow_membership
        out_mask = ~shadow_membership

        in_grad_masked = np.where(in_mask, shadow_grad_norms, np.nan)
        out_grad_masked = np.where(out_mask, shadow_grad_norms, np.nan)

        return {
            "in_grad_mean": np.nanmean(in_grad_masked, axis=1),
            "in_grad_std": np.nanstd(in_grad_masked, axis=1) + 1e-10,
            "out_grad_mean": np.nanmean(out_grad_masked, axis=1),
            "out_grad_std": np.nanstd(out_grad_masked, axis=1) + 1e-10,
        }