Instructions to use immanuelpeter/Hy4-Vision with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use immanuelpeter/Hy4-Vision with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-feature-extraction", model="immanuelpeter/Hy4-Vision", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("immanuelpeter/Hy4-Vision", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Match pinned Hy4 vision forward numerics
Browse files- modeling_hy4.py +32 -9
modeling_hy4.py
CHANGED
|
@@ -46,9 +46,12 @@ class PatchEmbedding(nn.Module):
|
|
| 46 |
rows, cols = height // patch, width // patch
|
| 47 |
patches = F.unfold(pixels, patch, stride=patch).transpose(1, 2)
|
| 48 |
embedded = self.patch_embedding(patches.reshape(-1, 3, patch, patch))
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 52 |
)
|
| 53 |
positions = F.interpolate(
|
| 54 |
positions.float(),
|
|
@@ -56,7 +59,10 @@ class PatchEmbedding(nn.Module):
|
|
| 56 |
mode="bilinear",
|
| 57 |
align_corners=False,
|
| 58 |
)
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
|
| 62 |
class Attention(nn.Module):
|
|
@@ -64,6 +70,7 @@ class Attention(nn.Module):
|
|
| 64 |
super().__init__()
|
| 65 |
width = config.hidden_size
|
| 66 |
self.heads = config.num_attention_heads
|
|
|
|
| 67 |
self.q_proj = nn.Linear(width, width)
|
| 68 |
self.k_proj = nn.Linear(width, width)
|
| 69 |
self.v_proj = nn.Linear(width, width)
|
|
@@ -71,12 +78,24 @@ class Attention(nn.Module):
|
|
| 71 |
|
| 72 |
def forward(self, tokens):
|
| 73 |
batch, length, width = tokens.shape
|
| 74 |
-
|
| 75 |
-
proj(tokens)
|
|
|
|
|
|
|
|
|
|
| 76 |
for proj in (self.q_proj, self.k_proj, self.v_proj)
|
| 77 |
]
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
|
| 82 |
class MLP(nn.Module):
|
|
@@ -133,7 +152,11 @@ class Projector(nn.Module):
|
|
| 133 |
.permute(0, 1, 3, 2, 4, 5)
|
| 134 |
.reshape(batch, rows * cols // 4, 4 * width)
|
| 135 |
)
|
| 136 |
-
projected =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
return merged, projected
|
| 138 |
|
| 139 |
|
|
|
|
| 46 |
rows, cols = height // patch, width // patch
|
| 47 |
patches = F.unfold(pixels, patch, stride=patch).transpose(1, 2)
|
| 48 |
embedded = self.patch_embedding(patches.reshape(-1, 3, patch, patch))
|
| 49 |
+
batch = pixels.shape[0]
|
| 50 |
+
tokens = embedded.flatten(1)
|
| 51 |
+
positions = (
|
| 52 |
+
self.position_embedding.weight[1:]
|
| 53 |
+
.reshape(1, self.edge, self.edge, tokens.shape[-1])
|
| 54 |
+
.permute(0, 3, 1, 2)
|
| 55 |
)
|
| 56 |
positions = F.interpolate(
|
| 57 |
positions.float(),
|
|
|
|
| 59 |
mode="bilinear",
|
| 60 |
align_corners=False,
|
| 61 |
)
|
| 62 |
+
positions = positions.permute(0, 2, 3, 1).reshape(rows * cols, -1)
|
| 63 |
+
return (tokens + positions.to(tokens.dtype).repeat(batch, 1)).reshape(
|
| 64 |
+
batch, rows * cols, -1
|
| 65 |
+
)
|
| 66 |
|
| 67 |
|
| 68 |
class Attention(nn.Module):
|
|
|
|
| 70 |
super().__init__()
|
| 71 |
width = config.hidden_size
|
| 72 |
self.heads = config.num_attention_heads
|
| 73 |
+
self.scale = (width // self.heads) ** -0.5
|
| 74 |
self.q_proj = nn.Linear(width, width)
|
| 75 |
self.k_proj = nn.Linear(width, width)
|
| 76 |
self.v_proj = nn.Linear(width, width)
|
|
|
|
| 78 |
|
| 79 |
def forward(self, tokens):
|
| 80 |
batch, length, width = tokens.shape
|
| 81 |
+
chunks = [
|
| 82 |
+
proj(tokens)
|
| 83 |
+
.view(1, batch * length, self.heads, -1)
|
| 84 |
+
.transpose(1, 2)
|
| 85 |
+
.split(length, dim=2)
|
| 86 |
for proj in (self.q_proj, self.k_proj, self.v_proj)
|
| 87 |
]
|
| 88 |
+
# The upstream SDPA path dispatches a kernel per image.
|
| 89 |
+
attended = torch.cat(
|
| 90 |
+
[
|
| 91 |
+
F.scaled_dot_product_attention(q, k, v, scale=self.scale)
|
| 92 |
+
.transpose(1, 2)
|
| 93 |
+
.contiguous()
|
| 94 |
+
for q, k, v in zip(*chunks, strict=True)
|
| 95 |
+
],
|
| 96 |
+
dim=1,
|
| 97 |
+
)
|
| 98 |
+
return self.o_proj(attended.reshape(batch, length, width))
|
| 99 |
|
| 100 |
|
| 101 |
class MLP(nn.Module):
|
|
|
|
| 152 |
.permute(0, 1, 3, 2, 4, 5)
|
| 153 |
.reshape(batch, rows * cols // 4, 4 * width)
|
| 154 |
)
|
| 155 |
+
projected = (
|
| 156 |
+
torch.cat([self.proj(image) for image in grid.permute(0, 3, 1, 2).split(1)])
|
| 157 |
+
.flatten(2)
|
| 158 |
+
.transpose(1, 2)
|
| 159 |
+
)
|
| 160 |
return merged, projected
|
| 161 |
|
| 162 |
|