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

Update deprecated Natten modules #27

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
18 changes: 9 additions & 9 deletions src/allin1/models/dinat.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import torch
from abc import ABC, abstractmethod
from typing import Optional, Tuple, Callable
from natten.functional import natten1dav, natten1dqkrpb, natten2dav, natten2dqkrpb
from natten.functional import na1d_av, na1d_qk, na2d_av, na2d_qk
from ..config import Config
from .utils import *

Expand Down Expand Up @@ -51,9 +51,9 @@ def extra_repr(self) -> str:
class _NeighborhoodAttentionNd(ABC, nn.Module):
# rpb is learnable relative positional biases; same concept is used Swin.
rpb: nn.Parameter
nattendqkrpb: Callable
na1d_qk: Callable
nattendav: Callable

def __init__(
self,
cfg: Config,
Expand Down Expand Up @@ -96,8 +96,8 @@ def forward(

# Compute NA between "query" and "key" to get the raw attention scores, and add relative positional biases.
# attention_scores = natten2dqkrpb(query_layer, key_layer, self.rpb, self.dilation)
attention_scores = self.nattendqkrpb(query_layer, key_layer, self.rpb, self.kernel_size, self.dilation)
attention_scores = self.na_qk(query_layer, key_layer, self.kernel_size, self.dilation, rpb=self.rpb)

# Normalize the attention scores to probabilities.
attention_probs = nn.functional.softmax(attention_scores, dim=-1)

Expand Down Expand Up @@ -141,8 +141,8 @@ def __init__(
torch.zeros(num_heads, (2 * self.kernel_size - 1)),
requires_grad=True,
)
self.nattendqkrpb = natten1dqkrpb
self.nattendav = natten1dav
self.na_qk = na1d_qk
self.nattendav = na1d_av


class NeighborhoodAttention2d(_NeighborhoodAttentionNd):
Expand All @@ -159,8 +159,8 @@ def __init__(
torch.zeros(num_heads, (2 * self.kernel_size - 1), (2 * self.kernel_size - 1)),
requires_grad=True,
)
self.nattendqkrpb = natten2dqkrpb
self.nattendav = natten2dav
self.na_qk = na2d_qk
self.nattendav = na2d_av


# Copied from transformers.models.nat.modeling_nat.NeighborhoodAttentionOutput
Expand Down