kd13 commited on
Commit
84e591d
·
verified ·
1 Parent(s): d0ce4da

Update modeling_vit.py

Browse files
Files changed (1) hide show
  1. modeling_vit.py +35 -26
modeling_vit.py CHANGED
@@ -25,51 +25,60 @@ class SwiGLU(nn.Module):
25
 
26
  def forward(self, x):
27
  return self.w_down(F.silu(self.w_gate(x)) * self.w_up(x))
28
-
29
  class RotaryEmbedding2D(nn.Module):
30
  def __init__(self, head_dim: int, grid_size: int, base: float = 10000.0):
31
  super().__init__()
32
  self.head_dim = head_dim
33
- self.axis_dim = head_dim // 2
34
  self.grid_size = grid_size
35
- self.num_patches = grid_size * grid_size
 
 
 
36
 
 
 
 
 
37
  inv_freq = 1.0 / (
38
- base ** (torch.arange(0, self.axis_dim, 2, dtype=torch.float32) / self.axis_dim)
39
  )
40
- coords = torch.arange(grid_size, dtype=torch.float32)
 
41
  yy, xx = torch.meshgrid(coords, coords, indexing="ij")
42
  x_freqs = torch.outer(xx.reshape(-1), inv_freq)
43
  y_freqs = torch.outer(yy.reshape(-1), inv_freq)
 
 
 
 
 
 
 
 
 
 
 
44
 
45
- self.register_buffer("cos_x", x_freqs.cos()[None, None, :, :], persistent=False)
46
- self.register_buffer("sin_x", x_freqs.sin()[None, None, :, :], persistent=False)
47
- self.register_buffer("cos_y", y_freqs.cos()[None, None, :, :], persistent=False)
48
- self.register_buffer("sin_y", y_freqs.sin()[None, None, :, :], persistent=False)
49
-
50
- @staticmethod
51
- def _rotate_axis(x, cos, sin):
52
  x_even = x[..., 0::2]
53
  x_odd = x[..., 1::2]
54
  out_even = x_even * cos - x_odd * sin
55
  out_odd = x_even * sin + x_odd * cos
56
  return torch.stack((out_even, out_odd), dim=-1).flatten(-2)
57
 
58
- def _apply_rope(self, x):
59
- cls_token = x[:, :, :1, :]
60
- patches = x[:, :, 1:, :]
61
- x_axis, y_axis = patches.split(self.axis_dim, dim=-1)
62
- cos_x = self.cos_x.to(device=x.device, dtype=x.dtype)
63
- sin_x = self.sin_x.to(device=x.device, dtype=x.dtype)
64
- cos_y = self.cos_y.to(device=x.device, dtype=x.dtype)
65
- sin_y = self.sin_y.to(device=x.device, dtype=x.dtype)
66
- x_axis = self._rotate_axis(x_axis, cos_x, sin_x)
67
- y_axis = self._rotate_axis(y_axis, cos_y, sin_y)
68
- patches = torch.cat((x_axis, y_axis), dim=-1)
69
- return torch.cat((cls_token, patches), dim=2)
70
-
71
  def forward(self, q, k):
72
- return self._apply_rope(q), self._apply_rope(k)
 
 
 
 
 
 
 
 
 
 
73
 
74
  class ConvStem(nn.Module):
75
  def __init__(self, in_chans: int, embed_dim: int, channels: tuple[int, int, int]):
 
25
 
26
  def forward(self, x):
27
  return self.w_down(F.silu(self.w_gate(x)) * self.w_up(x))
28
+
29
  class RotaryEmbedding2D(nn.Module):
30
  def __init__(self, head_dim: int, grid_size: int, base: float = 10000.0):
31
  super().__init__()
32
  self.head_dim = head_dim
 
33
  self.grid_size = grid_size
34
+ self.base = base
35
+ self.axis_dim = head_dim // 2
36
+
37
+ self.cos_sin_cache = None
38
 
39
+ def get_cos_sin(self, device, dtype):
40
+ if self.cos_sin_cache is not None and self.cos_sin_cache[0].device == device:
41
+ return self.cos_sin_cache
42
+
43
  inv_freq = 1.0 / (
44
+ self.base ** (torch.arange(0, self.axis_dim, 2, dtype=torch.float32, device=device) / self.axis_dim)
45
  )
46
+
47
+ coords = torch.arange(self.grid_size, dtype=torch.float32, device=device)
48
  yy, xx = torch.meshgrid(coords, coords, indexing="ij")
49
  x_freqs = torch.outer(xx.reshape(-1), inv_freq)
50
  y_freqs = torch.outer(yy.reshape(-1), inv_freq)
51
+
52
+ cos_x = x_freqs.cos()[None, None, :, :].to(dtype)
53
+ sin_x = x_freqs.sin()[None, None, :, :].to(dtype)
54
+ cos_y = y_freqs.cos()[None, None, :, :].to(dtype)
55
+ sin_y = y_freqs.sin()[None, None, :, :].to(dtype)
56
+
57
+ cos = torch.cat((cos_x, cos_y), dim=-1)
58
+ sin = torch.cat((sin_x, sin_y), dim=-1)
59
+
60
+ self.cos_sin_cache = (cos, sin)
61
+ return cos, sin
62
 
63
+ def apply_rotary_emb(self, x, cos, sin):
 
 
 
 
 
 
64
  x_even = x[..., 0::2]
65
  x_odd = x[..., 1::2]
66
  out_even = x_even * cos - x_odd * sin
67
  out_odd = x_even * sin + x_odd * cos
68
  return torch.stack((out_even, out_odd), dim=-1).flatten(-2)
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  def forward(self, q, k):
71
+ cos, sin = self.get_cos_sin(q.device, q.dtype)
72
+
73
+ if q.shape[-2] == cos.shape[-2] + 1:
74
+ cls_cos = torch.ones(1, 1, 1, cos.shape[-1], device=q.device, dtype=q.dtype)
75
+ cls_sin = torch.zeros(1, 1, 1, sin.shape[-1], device=q.device, dtype=q.dtype)
76
+ cos = torch.cat((cls_cos, cos), dim=-2)
77
+ sin = torch.cat((cls_sin, sin), dim=-2)
78
+
79
+ q_pos = self.apply_rotary_emb(q, cos, sin)
80
+ k_pos = self.apply_rotary_emb(k, cos, sin)
81
+ return q_pos, k_pos
82
 
83
  class ConvStem(nn.Module):
84
  def __init__(self, in_chans: int, embed_dim: int, channels: tuple[int, int, int]):