diff --git a/docs/attacks/loss.html b/docs/attacks/loss.html index 81718cc..f9007d2 100644 --- a/docs/attacks/loss.html +++ b/docs/attacks/loss.html @@ -46,7 +46,7 @@
mimir.attacks.loss
mimir.attacks.min_k
mimir.attacks.min_k
Base class (for LLMs).
-Initializes internal Module state, shared by both nn.Module and ScriptModule.
Initialize internal Module state, shared by both nn.Module and ScriptModule.
Base class (for LLMs).
-Initializes internal Module state, shared by both nn.Module and ScriptModule.
Initialize internal Module state, shared by both nn.Module and ScriptModule.
Base class (for LLMs).
-Initializes internal Module state, shared by both nn.Module and ScriptModule.
Initialize internal Module state, shared by both nn.Module and ScriptModule.
mimir.attacks.quantile
mimir.attacks.quantile
mimir.attacks.reference
mimir.attacks.zlib
mimir.models
mimir.models
Generic LM- used most often for target model
-Initializes internal Module state, shared by both nn.Module and ScriptModule.
Initialize internal Module state, shared by both nn.Module and ScriptModule.
Base class (for LLMs).
-Initializes internal Module state, shared by both nn.Module and ScriptModule.
Initialize internal Module state, shared by both nn.Module and ScriptModule.
Defines the computation performed at every call.
+Define the computation performed at every call.
Should be overridden by all subclasses.
Note
@@ -1426,7 +1436,7 @@def _forward_unimplemented(self, *input: Any) -> None:
- r"""Defines the computation performed at every call.
+ r"""Define the computation performed at every call.
Should be overridden by all subclasses.
@@ -1479,7 +1489,7 @@ Args
-def get_probabilities(self, text: str, tokens: numpy.ndarray = None)
+def get_probabilities(self, text: str, tokens: numpy.ndarray = None, no_grads: bool = True)
-
Get the probabilities or log-softmaxed logits for a text under the current model.
@@ -1505,10 +1515,10 @@ Returns
Expand source code
-@torch.no_grad()
-def get_probabilities(self,
+def get_probabilities(self,
text: str,
- tokens: np.ndarray = None):
+ tokens: np.ndarray = None,
+ no_grads: bool = True):
"""
Get the probabilities or log-softmaxed logits for a text under the current model.
Args:
@@ -1522,45 +1532,50 @@ Returns
Returns:
list: A list of probabilities.
"""
- if self.device is None or self.name is None:
- raise ValueError("Please set self.device and self.name in child class")
+ with torch.set_grad_enabled(not no_grads):
+ if self.device is None or self.name is None:
+ raise ValueError("Please set self.device and self.name in child class")
- if tokens is not None:
- labels = torch.from_numpy(tokens.astype(np.int64)).type(torch.LongTensor)
- if labels.shape[0] != 1:
- # expand first dimension
- labels = labels.unsqueeze(0)
- else:
- tokenized = self.tokenizer(
- text, return_tensors="pt")
- labels = tokenized.input_ids
+ if tokens is not None:
+ labels = torch.from_numpy(tokens.astype(np.int64)).type(torch.LongTensor)
+ if labels.shape[0] != 1:
+ # expand first dimension
+ labels = labels.unsqueeze(0)
+ else:
+ tokenized = self.tokenizer(
+ text, return_tensors="pt")
+ labels = tokenized.input_ids
- all_prob = []
- for i in range(0, labels.size(1), self.stride):
- begin_loc = max(i + self.stride - self.max_length, 0)
- end_loc = min(i + self.stride, labels.size(1))
- trg_len = end_loc - i # may be different from stride on last loop
- input_ids = labels[:, begin_loc:end_loc].to(self.device)
- target_ids = input_ids.clone()
- target_ids[:, :-trg_len] = -100
+ all_prob = []
+ for i in range(0, labels.size(1), self.stride):
+ begin_loc = max(i + self.stride - self.max_length, 0)
+ end_loc = min(i + self.stride, labels.size(1))
+ trg_len = end_loc - i # may be different from stride on last loop
+ input_ids = labels[:, begin_loc:end_loc].to(self.device)
+ target_ids = input_ids.clone()
+ target_ids[:, :-trg_len] = -100
- logits = self.model(input_ids, labels=target_ids).logits.cpu()
- shift_logits = logits[..., :-1, :].contiguous()
- probabilities = torch.nn.functional.log_softmax(shift_logits, dim=-1)
- shift_labels = target_ids[..., 1:].cpu().contiguous()
- labels_processed = shift_labels[0]
+ logits = self.model(input_ids, labels=target_ids).logits.cpu()
+ shift_logits = logits[..., :-1, :].contiguous()
+ probabilities = torch.nn.functional.log_softmax(shift_logits, dim=-1)
+ shift_labels = target_ids[..., 1:].cpu().contiguous()
+ labels_processed = shift_labels[0]
- del input_ids
- del target_ids
+ del input_ids
+ del target_ids
- for i, token_id in enumerate(labels_processed):
- if token_id != -100:
- probability = probabilities[0, i, token_id].item()
- all_prob.append(probability)
- # Should be equal to # of tokens - 1 to account for shift
- assert len(all_prob) == labels.size(1) - 1
+ for i, token_id in enumerate(labels_processed):
+ if token_id != -100:
+ probability = probabilities[0, i, token_id]
+ if no_grads:
+ probability = probability.item()
+ all_prob.append(probability)
+ # Should be equal to # of tokens - 1 to account for shift
+ assert len(all_prob) == labels.size(1) - 1
- return all_prob
+ if no_grads:
+ return all_prob
+ return torch.tensor(all_prob)
@@ -1729,7 +1744,7 @@ Returns
-
Wrapper for OpenAI API calls
-Initializes internal Module state, shared by both nn.Module and ScriptModule.
+Initialize internal Module state, shared by both nn.Module and ScriptModule.
Expand source code
@@ -1982,7 +1997,7 @@ Inherited members
Wrapper for referenc model, specifically used for quantile regression
-Initializes internal Module state, shared by both nn.Module and ScriptModule.
Initialize internal Module state, shared by both nn.Module and ScriptModule.
Wrapper for reference model
-Initializes internal Module state, shared by both nn.Module and ScriptModule.
Initialize internal Module state, shared by both nn.Module and ScriptModule.