| BatchedInvertibleMatrix |
Ran ran on a synthesized input |
ljc-fvnr/structural-aligned-mixture-of-var/models/AutoregressiveAlignment.py code served (permissive licence) · get_code("cc613e8946fad5b9") How it ranRan in Syntology's sandbox on this synthesized input (drafted by a language model, class construct-then-call prompt, model not recorded) and returned tuple. The image it ran in was not recorded. If your copy returns the same type on these inputs, it matched what we observed; nothing here checks that. It does NOT mean the output is correct: nothing has compared it to the paper.
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
class BatchedInvertibleMatrix(nn.Module):
def __init__(self, h, d, scale=0.02):
super(BatchedInvertibleMatrix, self).__init__()
self.h = h
self.d = d
scaling_factor = np.sqrt(scale / np.sqrt(d))
self.LU = nn.Parameter(scaling_factor * torch.randn(h, d, d))
def forward(self, get_inverse=True, get_LU=False):
L = torch.tril(self.LU, -1) + torch.eye(self.d, device=self.LU.device).unsqueeze(0).expand(self.h, -1, -1)
U = torch.triu(self.LU, 1)
diag_elements = F.softplus(torch.diagonal(self.LU, dim1=-2, dim2=-1))
U = U + torch.diag_embed(diag_elements)
if get_LU:
return (L, U)
if not get_inverse:
return torch.matmul(L, U)
identity = torch.eye(self.d, device=self.LU.device).unsqueeze(0).expand(self.h, -1, -1)
L_inv = torch.linalg.solve_triangular(L, identity, upper=False)
U_inv = torch.linalg.solve_triangular(U, identity, upper=True)
LU = torch.matmul(L, U)
LU_inv = torch.matmul(U_inv, L_inv)
return (LU, LU_inv)
INIT_ARGS = {
'h': 2,
'd': 4,
'scale': 0.02
}
ARGS = {
'get_inverse': True,
'get_LU': False
}
if __name__ == '__main__':
model = BatchedInvertibleMatrix(**INIT_ARGS)
result = model(**ARGS)
print("Forward pass successful")
print(f"Result type: {type(result)}")
if isinstance(result, tuple):
print(f"Tuple length: {len(result)}")
print(f"LU shape: {result[0].shape}")
print(f"LU_inv shape: {result[1].shape}")
- Setup block drafted by
- a language model, class construct-then-call prompt, model not recorded
- Imports
- numpy, torch — import names as written, not package names to install; a name the repository itself provides is not a package
- Returned
- type tuple — a tuple output has no array shape of its own; none is stated
- Environment
- image not recorded; re-verify against syntology-harvest-sandbox:v5-deps; not recorded for this sample: the harness installs a cuda-to-cpu shim (cuda_shim.py) before every sample and no run records whether a sample needed it; seed: torch and numpy seeded to 0 by the harness immediately before the class was constructed from INIT_ARGS (fixture_check.py)
|
| FixedVAR |
Ran ran on a synthesized input |
ljc-fvnr/structural-aligned-mixture-of-var/models/AutoregressiveAlignment.py code served (permissive licence) · get_code("6ef77d317ed5c58d") How it ranRan in Syntology's sandbox on this synthesized input (drafted by a language model, class construct-then-call prompt, model not recorded) and returned Tensor, ndim 3, dtype family float, shape (4, 8, 16) as a later run recorded it. The image it ran in was not recorded. If your copy returns the same type, ndim and shape on these inputs, it matched what we observed; nothing here checks that. It does NOT mean the output is correct: nothing has compared it to the paper.
import torch
import torch.nn as nn
class Config:
def __init__(self):
self.n_embd = 16
self.n_head = 4
self.enc_in = 2
self.dropout = 0.1
INIT_ARGS = {
'config': Config(),
'max_len': 128
}
ARGS = {
'X': torch.randn(4, 8, 16)
}
if __name__ == '__main__':
from torch import nn
class FixedVAR(nn.Module):
def __init__(self, config, max_len=128):
super(FixedVAR, self).__init__()
self.D = config.n_embd
self.n_head = config.n_head
assert self.D % self.n_head == 0
self.d = self.D // self.n_head
self.C = config.enc_in
self.fixed_q = nn.Parameter(0.02 * torch.randn(1, self.C, max_len, self.n_head, self.d))
self.fixed_v = nn.Parameter(0.02 * torch.randn(1, self.C, max_len, self.n_head, self.d))
self.dropout = nn.Dropout(config.dropout)
def forward(self, X, *args, **kwargs):
B, L, D = X.shape
K = X.reshape(B, L, self.n_head, -1)
b = B // self.C
Q = self.fixed_q[:, :, 0:L].expand(b, -1, -1, -1, -1).reshape(B, L, self.n_head, self.d)
V = self.fixed_v[:, :, 0:L].expand(b, -1, -1, -1, -1).reshape(B, L, self.n_head, self.d)
W = torch.einsum('blhd,blhe->blhde', K, V)
W = torch.cumsum(W, dim=1)
X = torch.einsum('blhd,blhde->blhe', Q, W)
X = X.reshape(B, L, D)
X = self.dropout(X)
return X
model = FixedVAR(**INIT_ARGS)
output = model(**ARGS)
print(f"Output shape: {output.shape}")
print(f"Expected shape: {ARGS['X'].shape}")
assert output.shape == ARGS['X'].shape, f"Shape mismatch: {output.shape} vs {ARGS['X'].shape}"
print("Success!")
- Setup block drafted by
- a language model, class construct-then-call prompt, model not recorded
- Imports
- torch — import names as written, not package names to install; a name the repository itself provides is not a package
- Returned
- type Tensor, ndim 3, dtype family float, shape (4, 8, 16) — shape (4, 8, 16) as a later run recorded it
- dtype family
- int means an integer dtype; float means any other dtype the instrument saw, bool and complex included (fixture_check.py records only these two families)
- Environment
- image not recorded; re-verify against syntology-harvest-sandbox:v5-deps; not recorded for this sample: the harness installs a cuda-to-cpu shim (cuda_shim.py) before every sample and no run records whether a sample needed it; seed: torch and numpy seeded to 0 by the harness immediately before the class was constructed from INIT_ARGS (fixture_check.py)
|
| LinearAttention |
Ran ran on a synthesized input |
ljc-fvnr/structural-aligned-mixture-of-var/models/AutoregressiveAlignment.py code served (permissive licence) · get_code("fa3be66911755893") How it ranRan in Syntology's sandbox on this synthesized input (drafted by a language model, class construct-then-call prompt, model not recorded) and returned Tensor, ndim 3, dtype family float, shape (2, 3, 8) as a later run recorded it. The image it ran in was not recorded. If your copy returns the same type, ndim and shape on these inputs, it matched what we observed; nothing here checks that. It does NOT mean the output is correct: nothing has compared it to the paper.
import torch
import torch.nn as nn
class RMSNormMultiHead(nn.Module):
def __init__(self, n_head: int, dim: int, eps: float=1e-08):
super().__init__()
self.n_head = n_head
self.dim = dim
self.eps = eps
self.weight = nn.Parameter(torch.ones(n_head, dim))
@staticmethod
def rms_norm(x, eps=1e-05):
return x * torch.rsqrt(x.pow(2).mean(dim=-1, keepdim=True) + eps)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""
x: (..., n_head, dim)
"""
normed = self.rms_norm(x.float(), self.eps).type_as(x)
return normed * self.weight
class LinearAttention(nn.Module):
def __init__(self, config):
super(LinearAttention, self).__init__()
self.D = config.n_embd
self.n_head = config.n_head
assert self.D % self.n_head == 0
self.d = self.D // self.n_head
self.q_proj = nn.Linear(self.D, self.D, bias=config.bias)
self.v_proj = nn.Linear(self.D, self.D, bias=config.bias)
self.q_norm = RMSNormMultiHead(self.n_head, self.d)
self.v_norm = RMSNormMultiHead(self.n_head, self.d)
self.dropout = nn.Dropout(config.dropout)
def forward(self, X, qv_input=None):
B, L, D = X.shape
K = X.reshape(B, L, self.n_head, -1)
Q = self.q_proj(X if qv_input is None else qv_input).reshape(B, L, self.n_head, -1)
Q = self.q_norm(Q)
V = self.v_proj(X if qv_input is None else qv_input).reshape(B, L, self.n_head, -1)
V = self.v_norm(V)
W = torch.einsum('blhd,blhe->blhde', K, V)
W = torch.cumsum(W, dim=1)
X = torch.einsum('blhd,blhde->blhe', Q, W)
X = X.reshape(B, L, D)
X = self.dropout(X)
return X
def collect_qkv(self, X, qv_input=None):
B, L, D = X.shape
K = X.reshape(B, L, self.n_head, -1)
Q = self.q_proj(X if qv_input is None else qv_input).reshape(B, L, self.n_head, -1)
Q = self.q_norm(Q)
V = self.v_proj(X if qv_input is None else qv_input).reshape(B, L, self.n_head, -1)
V = self.v_norm(V)
return (Q, K, V)
class Config:
def __init__(self, n_embd, n_head, bias, dropout):
self.n_embd = n_embd
self.n_head = n_head
self.bias = bias
self.dropout = dropout
INIT_ARGS = {
'config': Config(n_embd=8, n_head=2, bias=False, dropout=0.1)
}
ARGS = {
'X': torch.randn(2, 3, 8),
'qv_input': None
}
- Setup block drafted by
- a language model, class construct-then-call prompt, model not recorded
- Imports
- torch — import names as written, not package names to install; a name the repository itself provides is not a package
- Returned
- type Tensor, ndim 3, dtype family float, shape (2, 3, 8) — shape (2, 3, 8) as a later run recorded it
- dtype family
- int means an integer dtype; float means any other dtype the instrument saw, bool and complex included (fixture_check.py records only these two families)
- Environment
- image not recorded; re-verify against syntology-harvest-sandbox:v5-deps; not recorded for this sample: the harness installs a cuda-to-cpu shim (cuda_shim.py) before every sample and no run records whether a sample needed it; seed: torch and numpy seeded to 0 by the harness immediately before the class was constructed from INIT_ARGS (fixture_check.py)
|
| MLP |
Ran ran on a synthesized input |
ljc-fvnr/structural-aligned-mixture-of-var/models/AutoregressiveAlignment.py code served (permissive licence) · get_code("4af1a565eefe778f") How it ranRan in Syntology's sandbox on this synthesized input (drafted by a language model, class construct-then-call prompt, model not recorded) and returned Tensor, ndim 2, dtype family float, shape (2, 4) as a later run recorded it. The image it ran in was not recorded. If your copy returns the same type, ndim and shape on these inputs, it matched what we observed; nothing here checks that. It does NOT mean the output is correct: nothing has compared it to the paper.
import torch
import torch.nn as nn
class Config:
def __init__(self, n_embd, bias, dropout):
self.n_embd = n_embd
self.bias = bias
self.dropout = dropout
class MLP(nn.Module):
def __init__(self, config):
super().__init__()
self.c_fc = nn.Linear(config.n_embd, 4 * config.n_embd, bias=config.bias)
self.gelu = nn.GELU()
self.c_proj = nn.Linear(4 * config.n_embd, config.n_embd)
self.dropout = nn.Dropout(config.dropout)
def forward(self, x):
x = self.c_fc(x)
x = self.gelu(x)
x = self.c_proj(x)
x = self.dropout(x)
return x
INIT_ARGS = {
'config': Config(n_embd=4, bias=True, dropout=0.1)
}
ARGS = {
'x': torch.randn(2, 4)
}
if __name__ == '__main__':
mlp = MLP(**INIT_ARGS)
output = mlp(**ARGS)
print(f"Output shape: {output.shape}")
print(f"Expected shape: torch.Size([2, 4])")
- Setup block drafted by
- a language model, class construct-then-call prompt, model not recorded
- Imports
- torch — import names as written, not package names to install; a name the repository itself provides is not a package
- Returned
- type Tensor, ndim 2, dtype family float, shape (2, 4) — shape (2, 4) as a later run recorded it
- dtype family
- int means an integer dtype; float means any other dtype the instrument saw, bool and complex included (fixture_check.py records only these two families)
- Environment
- image not recorded; re-verify against syntology-harvest-sandbox:v5-deps; not recorded for this sample: the harness installs a cuda-to-cpu shim (cuda_shim.py) before every sample and no run records whether a sample needed it; seed: torch and numpy seeded to 0 by the harness immediately before the class was constructed from INIT_ARGS (fixture_check.py)
|
| MultiSegmentTokenizer |
Ran ran on a synthesized input |
ljc-fvnr/structural-aligned-mixture-of-var/models/AutoregressiveAlignment.py code served (permissive licence) · get_code("60ee36d6fa8c1c67") How it ranRan in Syntology's sandbox on this synthesized input (drafted by a language model, class construct-then-call prompt, model not recorded) and returned Tensor, ndim 4, dtype family float, shape (2, 4, 2, 8) as a later run recorded it. The image it ran in was not recorded. If your copy returns the same type, ndim and shape on these inputs, it matched what we observed; nothing here checks that. It does NOT mean the output is correct: nothing has compared it to the paper.
import torch
import torch.nn as nn
import torch.nn.functional as F
class RMSNorm(torch.nn.Module):
def __init__(self, dim: int, eps: float=1e-05):
"""
From: https://github.com/meta-llama/llama/blob/main/llama/model.py
"""
super().__init__()
self.eps = eps
self.weight = nn.Parameter(torch.ones(dim))
@classmethod
def norm(self, x):
return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + 1e-05)
def forward(self, x):
output = self.norm(x.float()).type_as(x)
return output * self.weight
NORM = RMSNorm
class MultiSegmentTokenizer(nn.Module):
def __init__(self, s: int, n_channels: int, d_model: int, number_of_targets=0, univariate=False):
super(MultiSegmentTokenizer, self).__init__()
self.s = s
self.tokenizer = nn.Linear(s, d_model)
self.d_model = d_model
n_targets = number_of_targets if number_of_targets else n_channels
self.context_channel_projection = nn.Linear(n_channels, n_targets)
self.context_temporal_projection = nn.Linear(s, d_model)
self.tokenizer_output = nn.Linear(d_model, s)
self.C = n_channels
self.number_of_targets = number_of_targets
self.input_norm_main = NORM(d_model)
self.input_norm_ctx = NORM(d_model)
self.univariate = univariate
def forward(self, x: torch.Tensor) -> torch.Tensor:
B, C, L = x.shape
pad_len = (self.s - L % self.s) % self.s
x_padded = F.pad(x, (pad_len, 0), 'constant', 0)
L_padded = L + pad_len
N = L_padded // self.s
x_segmented = x_padded.view(B, C, N, self.s)
return x_segmented
def tokenize(self, x_segmented):
x_ctx = x_segmented.clone()
B, C, N, s = x_ctx.shape
x_ctx = x_ctx.permute(0, 2, 3, 1)
x_ctx = self.context_channel_projection(x_ctx)
x_ctx = x_ctx.permute(0, 3, 1, 2)
x_ctx = self.context_temporal_projection(x_ctx)
if self.univariate:
x_ctx = torch.zeros_like(x_ctx)
x_main = x_segmented[:, -self.number_of_targets:]
x_main = self.tokenizer(x_main)
x_main = torch.stack([self.input_norm_ctx(x_ctx), self.input_norm_main(x_main)], dim=3).reshape(B, -1, N * 2, self.d_model)
return x_main
def inverse_tokenize(self, x):
B, C, N, d = x.shape
x = self.tokenizer_output(x)
return x
def process_single_token(self, x):
x = self.tokenizer(x)
x = x.unsqueeze(-2)
return x
INIT_ARGS = {
's': 8,
'n_channels': 4,
'd_model': 16,
'number_of_targets': 3,
'univariate': False
}
ARGS = {
'x': torch.randn(2, 4, 12)
}
if __name__ == '__main__':
model = MultiSegmentTokenizer(**INIT_ARGS)
output = model(**ARGS)
print("Output shape:", output.shape)
print("Output:", output)
- Setup block drafted by
- a language model, class construct-then-call prompt, model not recorded
- Imports
- torch — import names as written, not package names to install; a name the repository itself provides is not a package
- Returned
- type Tensor, ndim 4, dtype family float, shape (2, 4, 2, 8) — shape (2, 4, 2, 8) as a later run recorded it
- dtype family
- int means an integer dtype; float means any other dtype the instrument saw, bool and complex included (fixture_check.py records only these two families)
- Environment
- image not recorded; re-verify against syntology-harvest-sandbox:v5-deps; not recorded for this sample: the harness installs a cuda-to-cpu shim (cuda_shim.py) before every sample and no run records whether a sample needed it; seed: torch and numpy seeded to 0 by the harness immediately before the class was constructed from INIT_ARGS (fixture_check.py)
|
| RMSNorm |
Ran ran on a synthesized input |
ljc-fvnr/structural-aligned-mixture-of-var/models/AutoregressiveAlignment.py code served (permissive licence) · get_code("45502bf8690c09af") How it ranRan in Syntology's sandbox on this synthesized input (drafted by a language model, class construct-then-call prompt, model not recorded) and returned Tensor, ndim 2, dtype family float, shape (2, 4) as a later run recorded it. The image it ran in was not recorded. If your copy returns the same type, ndim and shape on these inputs, it matched what we observed; nothing here checks that. It does NOT mean the output is correct: nothing has compared it to the paper.
import torch
import torch.nn as nn
class RMSNorm(torch.nn.Module):
def __init__(self, dim: int, eps: float=1e-05):
"""
From: https://github.com/meta-llama/llama/blob/main/llama/model.py
"""
super().__init__()
self.eps = eps
self.weight = nn.Parameter(torch.ones(dim))
@classmethod
def norm(self, x):
return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + 1e-05)
def forward(self, x):
output = self.norm(x.float()).type_as(x)
return output * self.weight
INIT_ARGS = {
'dim': 4,
'eps': 1e-05,
}
ARGS = {
'x': torch.randn(2, 4),
}
if __name__ == '__main__':
model = RMSNorm(**INIT_ARGS)
output = model(**ARGS)
print(f"Output shape: {output.shape}")
print(f"Output: {output}")
- Setup block drafted by
- a language model, class construct-then-call prompt, model not recorded
- Imports
- torch — import names as written, not package names to install; a name the repository itself provides is not a package
- Returned
- type Tensor, ndim 2, dtype family float, shape (2, 4) — shape (2, 4) as a later run recorded it
- dtype family
- int means an integer dtype; float means any other dtype the instrument saw, bool and complex included (fixture_check.py records only these two families)
- Environment
- image not recorded; re-verify against syntology-harvest-sandbox:v5-deps; not recorded for this sample: the harness installs a cuda-to-cpu shim (cuda_shim.py) before every sample and no run records whether a sample needed it; seed: torch and numpy seeded to 0 by the harness immediately before the class was constructed from INIT_ARGS (fixture_check.py)
|
| RMSNormMultiHead |
Ran ran on a synthesized input |
ljc-fvnr/structural-aligned-mixture-of-var/models/AutoregressiveAlignment.py code served (permissive licence) · get_code("cc0ecd2c3201fc4c") How it ranRan in Syntology's sandbox on this synthesized input (drafted by a language model, class construct-then-call prompt, model not recorded) and returned Tensor, ndim 3, dtype family float, shape (3, 2, 4) as a later run recorded it. The image it ran in was not recorded. If your copy returns the same type, ndim and shape on these inputs, it matched what we observed; nothing here checks that. It does NOT mean the output is correct: nothing has compared it to the paper.
import torch
import torch.nn as nn
class RMSNormMultiHead(nn.Module):
def __init__(self, n_head: int, dim: int, eps: float=1e-08):
super().__init__()
self.n_head = n_head
self.dim = dim
self.eps = eps
self.weight = nn.Parameter(torch.ones(n_head, dim))
@staticmethod
def rms_norm(x, eps=1e-05):
return x * torch.rsqrt(x.pow(2).mean(dim=-1, keepdim=True) + eps)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""
x: (..., n_head, dim)
"""
normed = self.rms_norm(x.float(), self.eps).type_as(x)
return normed * self.weight
INIT_ARGS = {
'n_head': 2,
'dim': 4,
'eps': 1e-08,
}
ARGS = {
'x': torch.randn(3, 2, 4),
}
if __name__ == '__main__':
model = RMSNormMultiHead(**INIT_ARGS)
output = model(**ARGS)
print(f"Input shape: {ARGS['x'].shape}")
print(f"Output shape: {output.shape}")
print(f"Output:\n{output}")
- Setup block drafted by
- a language model, class construct-then-call prompt, model not recorded
- Imports
- torch — import names as written, not package names to install; a name the repository itself provides is not a package
- Returned
- type Tensor, ndim 3, dtype family float, shape (3, 2, 4) — shape (3, 2, 4) as a later run recorded it
- dtype family
- int means an integer dtype; float means any other dtype the instrument saw, bool and complex included (fixture_check.py records only these two families)
- Environment
- image not recorded; re-verify against syntology-harvest-sandbox:v5-deps; not recorded for this sample: the harness installs a cuda-to-cpu shim (cuda_shim.py) before every sample and no run records whether a sample needed it; seed: torch and numpy seeded to 0 by the harness immediately before the class was constructed from INIT_ARGS (fixture_check.py)
|
| Model |
Not yet run unverified (harvested, no recorded run) |
ljc-fvnr/structural-aligned-mixture-of-var/models/AutoregressiveAlignment.py code served (permissive licence) · get_code("af4d19e1b7122a73") How it ran |
| Transformer |
Not yet run unverified (harvested, no recorded run) |
ljc-fvnr/structural-aligned-mixture-of-var/models/AutoregressiveAlignment.py code served (permissive licence) · get_code("ce6f9d53b28653be") How it ran |