Skip to content

Commit

Permalink
Support Value Zeroing for non-eager attention types (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
gsarti authored Apr 25, 2024
1 parent 343fc79 commit f2b9d92
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 11 deletions.
4 changes: 2 additions & 2 deletions inseq/attr/feat/ops/value_zeroing.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import logging
from enum import Enum
from types import FrameType
from typing import TYPE_CHECKING, Callable, Optional

import torch
Expand All @@ -22,7 +23,6 @@
from torch.utils.hooks import RemovableHandle

from ....utils import (
StackFrame,
find_block_stack,
get_post_variable_assignment_hook,
recursive_get_submodule,
Expand Down Expand Up @@ -100,7 +100,7 @@ def get_value_zeroing_hook(varname: str = "value") -> Callable[..., None]:
"""

def value_zeroing_forward_mid_hook(
frame: StackFrame,
frame: FrameType,
zeroed_token_index: Optional[int] = None,
zeroed_units_indices: Optional[OneOrMoreIndices] = None,
batch_size: int = 1,
Expand Down
3 changes: 0 additions & 3 deletions inseq/models/huggingface_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,6 @@ def __init__(
if isinstance(model, PreTrainedModel):
self.model = model
else:
if "output_attentions" not in model_kwargs:
model_kwargs["output_attentions"] = True

self.model = self._autoclass.from_pretrained(model, **model_kwargs)
self.model_name = self.model.config.name_or_path
self.tokenizer_name = tokenizer if isinstance(tokenizer, str) else None
Expand Down
3 changes: 1 addition & 2 deletions inseq/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
MissingAttributionMethodError,
UnknownAttributionMethodError,
)
from .hooks import StackFrame, get_post_variable_assignment_hook
from .hooks import get_post_variable_assignment_hook
from .import_utils import (
is_accelerate_available,
is_captum_available,
Expand Down Expand Up @@ -127,7 +127,6 @@
"filter_logits",
"cli_arg",
"get_post_variable_assignment_hook",
"StackFrame",
"validate_indices",
"pad_with_nan",
"recursive_get_submodule",
Expand Down
7 changes: 3 additions & 4 deletions inseq/utils/hooks.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import re
from inspect import getsourcelines
from sys import gettrace, settrace
from typing import Callable, Optional, TypeVar
from types import FrameType
from typing import Callable, Optional

from torch import nn

from .misc import get_left_padding

StackFrame = TypeVar("StackFrame")


def get_last_variable_assignment_position(
module: nn.Module,
Expand Down Expand Up @@ -57,7 +56,7 @@ def get_post_variable_assignment_hook(
module: nn.Module,
varname: str,
fname: str = "forward",
hook_fn: Callable[[StackFrame], None] = lambda **kwargs: None,
hook_fn: Callable[[FrameType], None] = lambda **kwargs: None,
**kwargs,
) -> Callable[[], None]:
"""Creates a hook that is called after the last variable assignment in the specified method of a `nn.Module`.
Expand Down

0 comments on commit f2b9d92

Please sign in to comment.