At1uri's picture
Release Mamba S6 Selective State Space weights (Safetensors) and inference engine
653a154 verified
Raw History Blame Contribute Delete
1.93 kB
"""
Inference & Verification Script for Mamba-S6 Selective State Space Model
Author: Ramprasad Atluri (At1uri)
Repository: At1uri/mamba-s6-selective-state-space-engine
"""
import os
import json
import time
import numpy as np
from safetensors.numpy import load_file
from mamba_s6 import MambaS6Block
def run_benchmark():
curr_dir = os.path.dirname(os.path.abspath(__file__))
weights_path = os.path.join(curr_dir, "model.safetensors")
config_path = os.path.join(curr_dir, "config.json")
with open(config_path, "r") as f:
config = json.load(f)
print("Loading Mamba S6 weights from safetensors...")
tensors = load_file(weights_path)
model = MambaS6Block(tensors, config)
# 1. Linear Scaling Benchmark across sequence lengths
seq_lengths = [64, 128, 256, 512]
d_model = config["d_model"]
print("\n--- Linear Time O(L) Scaling Benchmark ---")
for L in seq_lengths:
u = np.random.randn(L, d_model).astype(np.float32)
t0 = time.perf_counter()
y, h_final = model.forward_sequence(u)
elapsed_ms = (time.perf_counter() - t0) * 1000
print(f"Sequence Length {L:4d}: Output Shape {y.shape}, Latency = {elapsed_ms:6.2f} ms")
# 2. Autoregressive Step-by-Step Test O(1)
print("\n--- O(1) Constant-Time Autoregressive Step Test ---")
h_state = np.zeros((config["d_inner"], config["d_state"]), dtype=np.float32)
step_times = []
for step in range(10):
u_t = np.random.randn(d_model).astype(np.float32)
t0 = time.perf_counter()
y_t, h_state = model.step_autoregressive(u_t, h_state)
step_times.append((time.perf_counter() - t0) * 1000)
avg_step_ms = np.mean(step_times)
print(f"Mean Autoregressive Step Latency: {avg_step_ms:.3f} ms / token (Zero KV-Cache)")
print("\nVerification SUCCESS: Mamba S6 Selective Recurrence Verified.")
if __name__ == "__main__":
run_benchmark()