Skip to content

Added missing activation to step function of s4block. #116

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions src/models/sequence/modules/s4block.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ def step(self, x, state):
if self.bottleneck is not None:
x = self.input_linear(x)
y, next_state = self.layer.step(x, state) # (B C H)
y = self.activation(y)
if self.gate is not None:
y = self.output_gate(y)
y = y * v
Expand Down
84 changes: 84 additions & 0 deletions src/utils/verify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
if __name__ == '__main__':
import sys
import pathlib
p = pathlib.Path().absolute()
print("Adding path: ", p)
sys.path.append(str(p))

import torch
from src.models.sequence.modules.s4block import S4Block

def test_state(random_init=False, **kwargs):
# B = 1
# H = 64
# N = 64
# L = 1024
B = 2
H = 3
N = 4
L = 8
s4 = S4Block(H, d_state=N, l_max=L, **kwargs)
s4.to(device)
s4.eval()
# for module in s4.modules():
# if hasattr(module, '_setup_step'): module._setup_step()
s4.setup_step()

u = torch.ones(B, H, L).to(device)
initial_state = s4.default_state(B)
if random_init:
if initial_state.size(-1) == N:
initial_state = initial_state[..., :N//2]
initial_state = torch.randn_like(initial_state)
initial_state = torch.cat([initial_state, initial_state.conj()], dim=-1)
else:
initial_state = torch.randn_like(initial_state)

state = initial_state.clone()
y, final_state = s4(u, state=state)
print("output:\n", y, y.shape)
print("final state:\n", final_state, final_state.shape)

# Use Stepping
# for module in s4.modules():
# if hasattr(module, '_setup_step'): module._setup_step()
s4.setup_step()
state = initial_state.clone()
ys = []
for u_ in torch.unbind(u, dim=-1):
y_, state = s4.step(u_, state=state)
ys.append(y_)
ys = torch.stack(ys, dim=-1)
print("step outputs:\n", ys)
print("step final state:\n", state)

# return

# Use Chunking

chunks = 4
state = initial_state.clone()
ys = []
for u_ in u.chunk(chunks, dim=-1):
y_, state = s4(u_, state=state)
ys.append(y_)
ys = torch.cat(ys, dim=-1)
print("chunk outputs:\n", ys)
print("chunk final state:\n", state)
# print("chunk output error:")
# utils.compare_outputs(y, ys)
# print("chunk final state error:")
# utils.compare_outputs(final_state, state)


if __name__ == '__main__':
# from benchmark import utils
torch.manual_seed(42)

device = 'cpu' # 'cuda'
device = torch.device(device)

# test_state(random_init=True, mode='dense', init='legt', rank=2, channels=2)
# test_state(random_init=True, mode='dplr', init='legt', rank=2, channels=2)
# test_state(random_init=False, mode='diag', init='legs', rank=1)
test_state(random_init=False, mode='diag', init='legs', rank=1, disc='zoh', channels=3, transposed=True)