| Attention |
Ran ran on a synthesized input |
jiawangbai/HAT/models/volo.py code served (permissive licence) · get_code("758bd44a54df5709") 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; no output shape is stated. The image it ran in was not recorded. If your copy returns the same type and ndim 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
INIT_ARGS = {
'dim': 4,
'num_heads': 2,
'qkv_bias': False,
'qk_scale': None,
'attn_drop': 0.0,
'proj_drop': 0.0,
}
ARGS = {
'x': torch.randn(2, 3, 4, 4),
}
if __name__ == '__main__':
attention = nn.Module()
attention.__class__ = type('Attention', (nn.Module,), {
'__init__': lambda self, dim, num_heads=8, qkv_bias=False, qk_scale=None, attn_drop=0.0, proj_drop=0.0: (
nn.Module.__init__(self),
setattr(self, 'num_heads', num_heads),
setattr(self, 'scale', qk_scale or (dim // num_heads) ** (-0.5)),
setattr(self, 'qkv', nn.Linear(dim, dim * 3, bias=qkv_bias)),
setattr(self, 'attn_drop', nn.Dropout(attn_drop)),
setattr(self, 'proj', nn.Linear(dim, dim)),
setattr(self, 'proj_drop', nn.Dropout(proj_drop)),
),
'forward': lambda self, x: (
lambda B, H, W, C: (
lambda qkv: (
lambda q, k, v: (
lambda attn: (
lambda attn: (
lambda x: self.proj_drop(self.proj(x))
)(self.attn_drop(attn) @ v).transpose(1, 2).reshape(B, H, W, C)
)(attn.softmax(dim=-1))
)(q @ k.transpose(-2, -1) * self.scale)
)(qkv[0], qkv[1], qkv[2])
)(self.qkv(x).reshape(B, H * W, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4))
)(*x.shape)
})
attn_module = attention.__class__(**INIT_ARGS)
output = attn_module(**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}"
- 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 — no output shape is stated: observe_signature.py observed this class with its constructor sizes canonicalised where it could, which the node does not record, so its shape is not stated as this setup block's
- 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)
|
| ClassAttention |
Ran ran on a synthesized input |
jiawangbai/HAT/models/volo.py code served (permissive licence) · get_code("d6aec4de1a988995") 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, 1, 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
INIT_ARGS = {
'dim': 4,
'num_heads': 2,
'head_dim': None,
'qkv_bias': False,
'qk_scale': None,
'attn_drop': 0.0,
'proj_drop': 0.0,
}
ARGS = {
'x': torch.randn(2, 5, 4),
}
class ClassAttention(nn.Module):
"""
Class attention layer from CaiT, see details in CaiT
Class attention is the post stage in our VOLO, which is optional.
"""
def __init__(self, dim, num_heads=8, head_dim=None, qkv_bias=False, qk_scale=None, attn_drop=0.0, proj_drop=0.0):
super().__init__()
self.num_heads = num_heads
if head_dim is not None:
self.head_dim = head_dim
else:
head_dim = dim // num_heads
self.head_dim = head_dim
self.scale = qk_scale or head_dim ** (-0.5)
self.kv = nn.Linear(dim, self.head_dim * self.num_heads * 2, bias=qkv_bias)
self.q = nn.Linear(dim, self.head_dim * self.num_heads, bias=qkv_bias)
self.attn_drop = nn.Dropout(attn_drop)
self.proj = nn.Linear(self.head_dim * self.num_heads, dim)
self.proj_drop = nn.Dropout(proj_drop)
def forward(self, x):
B, N, C = x.shape
kv = self.kv(x).reshape(B, N, 2, self.num_heads, self.head_dim).permute(2, 0, 3, 1, 4)
k, v = (kv[0], kv[1])
q = self.q(x[:, :1, :]).reshape(B, self.num_heads, 1, self.head_dim)
attn = q * self.scale @ k.transpose(-2, -1)
attn = attn.softmax(dim=-1)
attn = self.attn_drop(attn)
cls_embed = (attn @ v).transpose(1, 2).reshape(B, 1, self.head_dim * self.num_heads)
cls_embed = self.proj(cls_embed)
cls_embed = self.proj_drop(cls_embed)
return cls_embed
if __name__ == '__main__':
model = ClassAttention(**INIT_ARGS)
output = model(**ARGS)
print(f"Output shape: {output.shape}")
print(f"Expected shape: (2, 1, 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 3, dtype family float, shape (2, 1, 4) — shape (2, 1, 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)
|
| ClassBlock |
Ran ran on a synthesized input |
jiawangbai/HAT/models/volo.py code served (permissive licence) · get_code("b10f308ac343586f") 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; no output shape is stated. The image it ran in was not recorded. If your copy returns the same type and ndim 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
from timm.models.layers import DropPath
class ClassAttention(nn.Module):
def __init__(self, dim, num_heads=8, head_dim=None, qkv_bias=False, qk_scale=None, attn_drop=0.0, proj_drop=0.0):
super().__init__()
self.num_heads = num_heads
if head_dim is not None:
self.head_dim = head_dim
else:
head_dim = dim // num_heads
self.head_dim = head_dim
self.scale = qk_scale or head_dim ** (-0.5)
self.kv = nn.Linear(dim, self.head_dim * self.num_heads * 2, bias=qkv_bias)
self.q = nn.Linear(dim, self.head_dim * self.num_heads, bias=qkv_bias)
self.attn_drop = nn.Dropout(attn_drop)
self.proj = nn.Linear(self.head_dim * self.num_heads, dim)
self.proj_drop = nn.Dropout(proj_drop)
def forward(self, x):
B, N, C = x.shape
kv = self.kv(x).reshape(B, N, 2, self.num_heads, self.head_dim).permute(2, 0, 3, 1, 4)
k, v = (kv[0], kv[1])
q = self.q(x[:, :1, :]).reshape(B, self.num_heads, 1, self.head_dim)
attn = q * self.scale @ k.transpose(-2, -1)
attn = attn.softmax(dim=-1)
attn = self.attn_drop(attn)
cls_embed = (attn @ v).transpose(1, 2).reshape(B, 1, self.head_dim * self.num_heads)
cls_embed = self.proj(cls_embed)
cls_embed = self.proj_drop(cls_embed)
return cls_embed
class Mlp(nn.Module):
def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, drop=0.0):
super().__init__()
out_features = out_features or in_features
hidden_features = hidden_features or in_features
self.fc1 = nn.Linear(in_features, hidden_features)
self.act = act_layer()
self.fc2 = nn.Linear(hidden_features, out_features)
self.drop = nn.Dropout(drop)
def forward(self, x):
x = self.fc1(x)
x = self.act(x)
x = self.drop(x)
x = self.fc2(x)
x = self.drop(x)
return x
class ClassBlock(nn.Module):
def __init__(self, dim, num_heads, head_dim=None, mlp_ratio=4.0, qkv_bias=False, qk_scale=None, drop=0.0, attn_drop=0.0, drop_path=0.0, act_layer=nn.GELU, norm_layer=nn.LayerNorm):
super().__init__()
self.norm1 = norm_layer(dim)
self.attn = ClassAttention(dim, num_heads=num_heads, head_dim=head_dim, qkv_bias=qkv_bias, qk_scale=qk_scale, attn_drop=attn_drop, proj_drop=drop)
self.drop_path = DropPath(drop_path) if drop_path > 0.0 else nn.Identity()
self.norm2 = norm_layer(dim)
mlp_hidden_dim = int(dim * mlp_ratio)
self.mlp = Mlp(in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop)
def forward(self, x):
cls_embed = x[:, :1]
cls_embed = cls_embed + self.drop_path(self.attn(self.norm1(x)))
cls_embed = cls_embed + self.drop_path(self.mlp(self.norm2(cls_embed)))
return torch.cat([cls_embed, x[:, 1:]], dim=1)
INIT_ARGS = {
'dim': 8,
'num_heads': 2,
'head_dim': 4,
'mlp_ratio': 4.0,
'qkv_bias': False,
'qk_scale': None,
'drop': 0.0,
'attn_drop': 0.0,
'drop_path': 0.1,
'act_layer': nn.GELU,
'norm_layer': nn.LayerNorm
}
ARGS = {
'x': torch.randn(2, 5, 8)
}
- Setup block drafted by
- a language model, class construct-then-call prompt, model not recorded
- Imports
- timm, 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 — no output shape is stated: observe_signature.py observed this class with its constructor sizes canonicalised where it could, which the node does not record, so its shape is not stated as this setup block's
- 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)
|
| Downsample |
Ran ran on a synthesized input |
jiawangbai/HAT/models/volo.py code served (permissive licence) · get_code("33ecaec51f88c518") 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, 8, 8, 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
INIT_ARGS = {
'in_embed_dim': 4,
'out_embed_dim': 8,
'patch_size': 2,
}
ARGS = {
'x': torch.randn(2, 16, 16, 4),
}
if __name__ == '__main__':
downsample = nn.Module.__new__(type('Downsample', (nn.Module,), {
'__init__': lambda self, in_embed_dim, out_embed_dim, patch_size: (
super(type(self), self).__init__(),
setattr(self, 'proj', nn.Conv2d(in_embed_dim, out_embed_dim, kernel_size=patch_size, stride=patch_size))
),
'forward': lambda self, x: (
x.permute(0, 3, 1, 2),
self.proj(x.permute(0, 3, 1, 2)),
self.proj(x.permute(0, 3, 1, 2)).permute(0, 2, 3, 1)
)[-1]
}))(**INIT_ARGS)
output = downsample(**ARGS)
print(f"Output shape: {output.shape}")
- 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, 8, 8, 8) — shape (2, 8, 8, 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 |
jiawangbai/HAT/models/volo.py code served (permissive licence) · get_code("f3cd14e6c18a60d8") 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, 3) 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
INIT_ARGS = {
'in_features': 4,
'hidden_features': 8,
'out_features': 3,
'act_layer': nn.GELU,
'drop': 0.1,
}
ARGS = {
'x': torch.randn(2, 4),
}
mlp = nn.Module()
mlp.__class__ = type('Mlp', (nn.Module,), {
'__init__': lambda self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, drop=0.0: (
super(type(self), self).__init__(),
setattr(self, 'fc1', nn.Linear(in_features, hidden_features or in_features)),
setattr(self, 'act', act_layer()),
setattr(self, 'fc2', nn.Linear(hidden_features or in_features, out_features or in_features)),
setattr(self, 'drop', nn.Dropout(drop)),
)[-1],
'forward': lambda self, x: (
self.drop(self.fc2(self.drop(self.act(self.fc1(x)))))
),
})
mlp = type('Mlp', (nn.Module,), {
'__init__': lambda self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, drop=0.0: (
super(type(self), self).__init__(),
setattr(self, 'fc1', nn.Linear(in_features, hidden_features or in_features)),
setattr(self, 'act', act_layer()),
setattr(self, 'fc2', nn.Linear(hidden_features or in_features, out_features or in_features)),
setattr(self, 'drop', nn.Dropout(drop)),
)[-1],
'forward': lambda self, x: (
self.drop(self.fc2(self.drop(self.act(self.fc1(x)))))
),
})(**INIT_ARGS)
output = mlp(**ARGS)
- 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, 3) — shape (2, 3) 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)
|
| OutlookAttention |
Ran ran on a synthesized input |
jiawangbai/HAT/models/volo.py code served (permissive licence) · get_code("0b3e7de2ecb2c1e7") 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, 5, 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 math
class OutlookAttention(nn.Module):
"""
Implementation of outlook attention
--dim: hidden dim
--num_heads: number of heads
--kernel_size: kernel size in each window for outlook attention
return: token features after outlook attention
"""
def __init__(self, dim, num_heads, kernel_size=3, padding=1, stride=1, qkv_bias=False, qk_scale=None, attn_drop=0.0, proj_drop=0.0):
super().__init__()
head_dim = dim // num_heads
self.num_heads = num_heads
self.kernel_size = kernel_size
self.padding = padding
self.stride = stride
self.scale = qk_scale or head_dim ** (-0.5)
self.v = nn.Linear(dim, dim, bias=qkv_bias)
self.attn = nn.Linear(dim, kernel_size ** 4 * num_heads)
self.attn_drop = nn.Dropout(attn_drop)
self.proj = nn.Linear(dim, dim)
self.proj_drop = nn.Dropout(proj_drop)
self.unfold = nn.Unfold(kernel_size=kernel_size, padding=padding, stride=stride)
self.pool = nn.AvgPool2d(kernel_size=stride, stride=stride, ceil_mode=True)
def forward(self, x):
B, H, W, C = x.shape
v = self.v(x).permute(0, 3, 1, 2)
h, w = (math.ceil(H / self.stride), math.ceil(W / self.stride))
v = self.unfold(v).reshape(B, self.num_heads, C // self.num_heads, self.kernel_size * self.kernel_size, h * w).permute(0, 1, 4, 3, 2)
attn = self.pool(x.permute(0, 3, 1, 2)).permute(0, 2, 3, 1)
attn = self.attn(attn).reshape(B, h * w, self.num_heads, self.kernel_size * self.kernel_size, self.kernel_size * self.kernel_size).permute(0, 2, 1, 3, 4)
attn = attn * self.scale
attn = attn.softmax(dim=-1)
attn = self.attn_drop(attn)
x = (attn @ v).permute(0, 1, 4, 3, 2).reshape(B, C * self.kernel_size * self.kernel_size, h * w)
x = torch.nn.functional.fold(x, output_size=(H, W), kernel_size=self.kernel_size, padding=self.padding, stride=self.stride)
x = self.proj(x.permute(0, 2, 3, 1))
x = self.proj_drop(x)
return x
INIT_ARGS = {
'dim': 8,
'num_heads': 2,
'kernel_size': 3,
'padding': 1,
'stride': 1,
'qkv_bias': False,
'qk_scale': None,
'attn_drop': 0.0,
'proj_drop': 0.0,
}
ARGS = {
'x': torch.randn(2, 4, 5, 8),
}
if __name__ == '__main__':
model = OutlookAttention(**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 4, dtype family float, shape (2, 4, 5, 8) — shape (2, 4, 5, 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)
|
| Outlooker |
Ran ran on a synthesized input |
jiawangbai/HAT/models/volo.py code served (permissive licence) · get_code("a114b78025e6c55f") 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; no output shape is stated. The image it ran in was not recorded. If your copy returns the same type and ndim 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 numpy as np
from timm.models.layers import DropPath
INIT_ARGS = {
'dim': 4,
'kernel_size': 3,
'padding': 1,
'stride': 1,
'num_heads': 2,
'mlp_ratio': 2.0,
'attn_drop': 0.0,
'drop_path': 0.1,
'act_layer': torch.nn.GELU,
'norm_layer': torch.nn.LayerNorm,
'qkv_bias': False,
'qk_scale': None
}
# Create input tensor with shape (batch_size, height, width, channels)
batch_size = 2
height = 4
width = 4
channels = INIT_ARGS['dim']
input_tensor = torch.randn(batch_size, height, width, channels)
ARGS = {
'x': input_tensor
}
- Setup block drafted by
- a language model, class construct-then-call prompt, model not recorded
- Imports
- numpy, timm, 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 — no output shape is stated: observe_signature.py observed this class with its constructor sizes canonicalised where it could, which the node does not record, so its shape is not stated as this setup block's
- 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)
|
| PatchEmbed |
Ran ran on a synthesized input |
jiawangbai/HAT/models/volo.py code served (permissive licence) · get_code("53a133709900d494") 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, 48, 8, 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 PatchEmbed(nn.Module):
"""
Image to Patch Embedding.
Different with ViT use 1 conv layer, we use 4 conv layers to do patch embedding
"""
def __init__(self, img_size=224, stem_conv=False, stem_stride=1, patch_size=8, in_chans=3, hidden_dim=64, embed_dim=384):
super().__init__()
assert patch_size in [4, 8, 16]
self.stem_conv = stem_conv
if stem_conv:
self.conv = nn.Sequential(nn.Conv2d(in_chans, hidden_dim, kernel_size=7, stride=stem_stride, padding=3, bias=False), nn.BatchNorm2d(hidden_dim), nn.ReLU(inplace=True), nn.Conv2d(hidden_dim, hidden_dim, kernel_size=3, stride=1, padding=1, bias=False), nn.BatchNorm2d(hidden_dim), nn.ReLU(inplace=True), nn.Conv2d(hidden_dim, hidden_dim, kernel_size=3, stride=1, padding=1, bias=False), nn.BatchNorm2d(hidden_dim), nn.ReLU(inplace=True))
self.proj = nn.Conv2d(hidden_dim, embed_dim, kernel_size=patch_size // stem_stride, stride=patch_size // stem_stride)
self.num_patches = img_size // patch_size * (img_size // patch_size)
def forward(self, x):
if self.stem_conv:
x = self.conv(x)
x = self.proj(x)
return x
INIT_ARGS = {
'img_size': 64,
'stem_conv': True,
'stem_stride': 1,
'patch_size': 8,
'in_chans': 3,
'hidden_dim': 32,
'embed_dim': 48,
}
ARGS = {
'x': torch.randn(2, 3, 64, 64),
}
if __name__ == '__main__':
model = PatchEmbed(**INIT_ARGS)
output = model(**ARGS)
print(f"Output shape: {output.shape}")
print(f"Expected num_patches: {model.num_patches}")
- 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, 48, 8, 8) — shape (2, 48, 8, 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)
|
| Transformer |
Ran ran on a synthesized input |
jiawangbai/HAT/models/volo.py code served (permissive licence) · get_code("f1380508396f9eb9") 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, 3, 5, 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
from timm.models.layers import DropPath, to_2tuple, trunc_normal_
class Attention(nn.Module):
"""Implementation of self-attention"""
def __init__(self, dim, num_heads=8, qkv_bias=False, qk_scale=None, attn_drop=0.0, proj_drop=0.0):
super().__init__()
self.num_heads = num_heads
head_dim = dim // num_heads
self.scale = qk_scale or head_dim ** (-0.5)
self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)
self.attn_drop = nn.Dropout(attn_drop)
self.proj = nn.Linear(dim, dim)
self.proj_drop = nn.Dropout(proj_drop)
def forward(self, x):
B, H, W, C = x.shape
qkv = self.qkv(x).reshape(B, H * W, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4)
q, k, v = (qkv[0], qkv[1], qkv[2])
attn = q @ k.transpose(-2, -1) * self.scale
attn = attn.softmax(dim=-1)
attn = self.attn_drop(attn)
x = (attn @ v).transpose(1, 2).reshape(B, H, W, C)
x = self.proj(x)
x = self.proj_drop(x)
return x
class Mlp(nn.Module):
"""Implementation of MLP"""
def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, drop=0.0):
super().__init__()
out_features = out_features or in_features
hidden_features = hidden_features or in_features
self.fc1 = nn.Linear(in_features, hidden_features)
self.act = act_layer()
self.fc2 = nn.Linear(hidden_features, out_features)
self.drop = nn.Dropout(drop)
def forward(self, x):
x = self.fc1(x)
x = self.act(x)
x = self.drop(x)
x = self.fc2(x)
x = self.drop(x)
return x
class Transformer(nn.Module):
"""
Implementation of Transformer,
Transformer is the second stage in our VOLO
"""
def __init__(self, dim, num_heads, mlp_ratio=4.0, qkv_bias=False, qk_scale=None, attn_drop=0.0, drop_path=0.0, act_layer=nn.GELU, norm_layer=nn.LayerNorm):
super().__init__()
self.norm1 = norm_layer(dim)
self.attn = Attention(dim, num_heads=num_heads, qkv_bias=qkv_bias, qk_scale=qk_scale, attn_drop=attn_drop)
self.drop_path = DropPath(drop_path) if drop_path > 0.0 else nn.Identity()
self.norm2 = norm_layer(dim)
mlp_hidden_dim = int(dim * mlp_ratio)
self.mlp = Mlp(in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer)
def forward(self, x):
x = x + self.drop_path(self.attn(self.norm1(x)))
x = x + self.drop_path(self.mlp(self.norm2(x)))
return x
INIT_ARGS = {
'dim': 4,
'num_heads': 2,
'mlp_ratio': 4.0,
'qkv_bias': False,
'qk_scale': None,
'attn_drop': 0.0,
'drop_path': 0.0,
'act_layer': nn.GELU,
'norm_layer': nn.LayerNorm,
}
ARGS = {
'x': torch.randn(2, 3, 5, 4),
}
if __name__ == '__main__':
model = Transformer(**INIT_ARGS)
output = model(**ARGS)
print(f"Output shape: {output.shape}")
print(f"Expected shape: {ARGS['x'].shape}")
assert output.shape == ARGS['x'].shape, "Output shape mismatch"
print("Success!")
- Setup block drafted by
- a language model, class construct-then-call prompt, model not recorded
- Imports
- timm, 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, 3, 5, 4) — shape (2, 3, 5, 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)
|
| outlooker_blocks |
Ran ran; the contract check could not run (RAISES, DEP_MISSING or TIMEOUT) |
jiawangbai/HAT/models/volo.py code served (permissive licence) · get_code("ba7779c554538760") How it ranRan in Syntology's sandbox on this synthesized input (drafted by a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API) and returned Sequential. 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
torch.manual_seed(0)
# Define a minimal block_fn that the function will call
class MinimalOutlookerBlock(nn.Module):
def __init__(self, dim, kernel_size=3, padding=1, stride=1, num_heads=1, mlp_ratio=3.0, qkv_bias=False, qk_scale=None, attn_drop=0, drop_path=0.0):
super().__init__()
self.dim = dim
self.kernel_size = kernel_size
self.padding = padding
self.stride = stride
self.num_heads = num_heads
self.mlp_ratio = mlp_ratio
self.qkv_bias = qkv_bias
self.qk_scale = qk_scale
self.attn_drop = attn_drop
self.drop_path = drop_path
self.linear = nn.Linear(dim, dim)
def forward(self, x):
return self.linear(x)
# Define ARGS dictionary with all parameters
ARGS = {
'block_fn': MinimalOutlookerBlock,
'index': 0,
'dim': 64,
'layers': [2, 3, 4, 2],
'num_heads': 4,
'kernel_size': 3,
'padding': 1,
'stride': 1,
'mlp_ratio': 3.0,
'qkv_bias': False,
'qk_scale': None,
'attn_drop': 0.0,
'drop_path_rate': 0.1,
}
- Setup block drafted by
- a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API
- Imports
- torch — import names as written, not package names to install; a name the repository itself provides is not a package
- Returned
- type Sequential — a Sequential output has no array shape of its own; none is stated — the instrument coerces any output to an array; for a Sequential output the recorded ndim 1 and dtype family float describe that coercion, not the output, and are not 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: none by the harness; any seed is in the setup block
|
| transformer_blocks |
Ran ran; the contract check could not run (RAISES, DEP_MISSING or TIMEOUT) |
jiawangbai/HAT/models/volo.py code served (permissive licence) · get_code("bcb4ce1c1b25bc6c") How it ranRan in Syntology's sandbox on this synthesized input (drafted by a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API) and returned Sequential. 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
torch.manual_seed(0)
# Create a minimal block_fn that accepts the parameters passed by transformer_blocks
class MinimalTransformerBlock(nn.Module):
def __init__(self, dim, num_heads, mlp_ratio=3.0, qkv_bias=False, qk_scale=None, attn_drop=0, drop_path=0.0):
super().__init__()
self.dim = dim
self.num_heads = num_heads
self.mlp_ratio = mlp_ratio
self.qkv_bias = qkv_bias
self.qk_scale = qk_scale
self.attn_drop = attn_drop
self.drop_path = drop_path
self.linear = nn.Linear(dim, dim)
def forward(self, x):
return self.linear(x)
# Define ARGS dictionary with all parameters
ARGS = {
'block_fn': MinimalTransformerBlock,
'index': 0,
'dim': 64,
'layers': [2, 3, 4],
'num_heads': 4,
'mlp_ratio': 3.0,
'qkv_bias': False,
'qk_scale': None,
'attn_drop': 0,
'drop_path_rate': 0.1,
}
- Setup block drafted by
- a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API
- Imports
- torch — import names as written, not package names to install; a name the repository itself provides is not a package
- Returned
- type Sequential — a Sequential output has no array shape of its own; none is stated — the instrument coerces any output to an array; for a Sequential output the recorded ndim 1 and dtype family float describe that coercion, not the output, and are not 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: none by the harness; any seed is in the setup block
|
| VOLO |
Not yet run unverified (harvested, no recorded run) |
jiawangbai/HAT/models/volo.py code served (permissive licence) · get_code("2b66fd4ced7843b3") How it ran |
| get_block |
Not yet run unverified (harvested, no recorded run) |
jiawangbai/HAT/models/volo.py code served (permissive licence) · get_code("6974cb1ca3926a70") How it ran |
| rand_bbox |
Not yet run unverified (harvested, no recorded run) |
jiawangbai/HAT/models/volo.py code served (permissive licence) · get_code("b66088e90761117d") How it ran |