Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions pyrit/executor/promptgen/gcg/attack/base/attack_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,7 @@ def _loss_with_prefix_cache(
test_controls: Any,
loss_function: _SelectiveLossFunction,
logit_positions: torch.Tensor,
cache_parameter_name: str,
) -> torch.Tensor | None:
"""
Score candidates after evaluating their invariant prefix once.
Expand All @@ -799,7 +800,7 @@ def _loss_with_prefix_cache(
prefix_output = model(**prefix_kwargs)
try:
prefix_cache = self._expand_prefix_cache(
getattr(prefix_output, "past_key_values", None), batch_size=token_ids.shape[0]
getattr(prefix_output, cache_parameter_name, None), batch_size=token_ids.shape[0]
)
except (TypeError, ValueError):
del prefix_output, token_ids
Expand All @@ -808,7 +809,7 @@ def _loss_with_prefix_cache(

suffix_kwargs: dict[str, Any] = {
"input_ids": token_ids[:, prefix_length:],
"past_key_values": prefix_cache,
cache_parameter_name: prefix_cache,
"use_cache": False,
"return_dict": True,
"logits_to_keep": logit_positions - prefix_length,
Expand Down Expand Up @@ -851,20 +852,28 @@ def loss(
forward_parameters = {}

supports_selective_logits = "logits_to_keep" in forward_parameters
supports_prefix_cache = "past_key_values" in forward_parameters and "use_cache" in forward_parameters
# transformers 5.24 renamed the forward parameter `past_key_values` to `cache`.
cache_parameter_name = next((name for name in ("cache", "past_key_values") if name in forward_parameters), None)
supports_prefix_cache = "use_cache" in forward_parameters

if supports_selective_logits and isinstance(loss_function, _SelectiveLossFunction):
logit_positions = loss_function.get_required_logit_positions(
target_slice=self._target_slice,
control_slice=self._control_slice,
device=model.device,
)
if use_prefix_cache and supports_prefix_cache and self._control_slice.start > 1:
if (
use_prefix_cache
and cache_parameter_name is not None
and supports_prefix_cache
and self._control_slice.start > 1
):
cached_loss = self._loss_with_prefix_cache(
model,
test_controls=test_controls,
loss_function=loss_function,
logit_positions=logit_positions,
cache_parameter_name=cache_parameter_name,
)
if cached_loss is not None:
return cached_loss
Expand Down
Loading
Loading