immanuelpeter commited on
Commit
977d6fd
·
verified ·
1 Parent(s): c0ea94b

Match pinned Hy4 vision forward numerics

Browse files
Files changed (1) hide show
  1. 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
- tokens = embedded.reshape(pixels.shape[0], rows * cols, -1)
50
- positions = self.position_embedding.weight[1:].T.reshape(
51
- 1, -1, self.edge, self.edge
 
 
 
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
- return tokens + positions.flatten(2).transpose(1, 2).to(tokens.dtype)
 
 
 
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
- q, k, v = [
75
- proj(tokens).view(batch, length, self.heads, -1).transpose(1, 2)
 
 
 
76
  for proj in (self.q_proj, self.k_proj, self.v_proj)
77
  ]
78
- attended = F.scaled_dot_product_attention(q, k, v)
79
- return self.o_proj(attended.transpose(1, 2).reshape(batch, length, width))
 
 
 
 
 
 
 
 
 
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 = self.proj(grid.permute(0, 3, 1, 2)).flatten(2).transpose(1, 2)
 
 
 
 
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