Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Value Zeroing for non-eager attention types #267

Merged
merged 1 commit into from
Apr 25, 2024
Merged
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
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
Loading