Spaces:
Running on Zero

File size: 9,407 Bytes
cebb76b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import torch
import torch.nn.functional as F
import torch.utils.checkpoint

# flash-attn is a hard dependency in the training / NVIDIA-container environment,
# but it is unavailable (or ABI/arch-incompatible) on some inference targets such
# as HF Spaces ZeroGPU (torch 2.8+, Blackwell sm_120). Soft-import it and fall
# back to torch SDPA, which is numerically equivalent for our usage (non-causal,
# no dropout, default scale = 1/sqrt(head_dim)).
import os

try:
    import flash_attn
    _HAS_FLASH = True
except Exception:  # ImportError or runtime/arch incompatibility on import
    flash_attn = None
    _HAS_FLASH = False

# Allow forcing the SDPA path even when flash_attn is importable, so the
# ZeroGPU code path can be validated on a flash-attn-equipped GPU node.
if os.environ.get("DVSM_DISABLE_FLASH") == "1":
    _HAS_FLASH = False


def _attn_func(q, k, v):
    """Drop-in replacement for ``flash_attn.flash_attn_func``.

    flash_attn layout: q/k/v are ``[B, L, num_head, head_dim]``, non-causal,
    softmax_scale = 1/sqrt(head_dim). torch SDPA expects ``[B, num_head, L, head_dim]``
    and uses the same default scale + non-causal semantics, so we just transpose
    in and out. Output dtype matches the inputs (bf16/fp16), same as flash_attn.
    """
    if _HAS_FLASH:
        return flash_attn.flash_attn_func(q, k, v)
    qh = q.transpose(1, 2)  # [B, H, Lq, D]
    kh = k.transpose(1, 2)  # [B, H, Lk, D]
    vh = v.transpose(1, 2)  # [B, H, Lk, D]
    out = F.scaled_dot_product_attention(qh, kh, vh)  # default scale, non-causal
    return out.transpose(1, 2).contiguous()  # back to [B, Lq, H, D]


def checkpoint_wrapper(module):
    # Wrap model forward function to automatically use activation checkpointing
    ori_forward = module.forward
    def new_forward(*args, **kwargs):
        if module.training:
            output = torch.utils.checkpoint.checkpoint(
                ori_forward, *args, **kwargs, use_reentrant=False)
        else:
            output = ori_forward(*args, **kwargs)
        return output
    return new_forward


class Contiguous(torch.nn.Module):
    def forward(self, x):
        return x.contiguous()


class MLP(torch.nn.Module):
    def __init__(self, dim, expand_ratio=4, bias=False, activation=torch.nn.GELU):
        """
        Args:
            dim: Input dimension
            expand_ratio: Multiplier for hidden dimension
            bias: Whether to use bias in linear layers
            activation: Activation function
        """
        super().__init__()
        hidden_dim = round(dim * expand_ratio)
        self.mlp = torch.nn.Sequential(
            torch.nn.Linear(dim, hidden_dim, bias=bias),
            activation(),
            torch.nn.Linear(hidden_dim, dim, bias=bias))

    def forward(self, x):
        return self.mlp(x)


class SwiGluMLP(torch.nn.Module):
    def __init__(self, dim, expand_ratio=4, bias=False):
        """
        Args:
            dim: Input dimension
            expand_ratio: Multiplier for hidden dimension
            bias: Whether to use bias in linear layers
            activation: Activation function
        """
        super().__init__()
        hidden_dim = round(dim * expand_ratio)
        self.mlp = torch.nn.Sequential(
            torch.nn.Linear(dim, hidden_dim, bias=bias),
            torch.nn.Linear(dim, hidden_dim, bias=bias),
            torch.nn.Linear(hidden_dim, dim, bias=bias))

    def forward(self, x):
        gate = torch.nn.functional.silu(self.mlp[0](x), inplace=True)
        return self.mlp[2](gate * self.mlp[1](x))


class IntraViewAttention(torch.nn.Module):
    def __init__(self,
        dim,
        head_dim,
        bias=False,
        use_qk_norm=True,
        use_gating=False,
        use_sandwich=False,
        out_dim=None,
    ):
        """
        dim: Input dimension
        head_dim: Dimension of each attention head
        bias: Whether to use bias in QKV and output projection
        use_qk_norm: Whether to use Q-K normalization
        use_gating: Whether to apply gating before output ffn
        """
        super().__init__()
        assert dim % head_dim == 0, f"dim={dim} should be divisible by head_dim={head_dim}"
        num_head = dim // head_dim

        self.dim = dim
        self.out_dim = dim if out_dim is None else out_dim
        self.head_dim = head_dim
        self.num_head = num_head
        self.use_qk_norm = use_qk_norm
        self.use_gating = use_gating
        self.use_sandwich = use_sandwich
        self.attn_dtype = torch.bfloat16 if torch.cuda.get_device_capability()[0] >= 8 else torch.float16

        self.to_qkv = torch.nn.Linear(dim, 3 * dim, bias=bias)
        self.fc = torch.nn.Linear(dim, self.out_dim, bias=bias)

        # Optional Q-K normalization
        if self.use_qk_norm:
            self.q_norm = torch.nn.RMSNorm(head_dim, eps=1e-5)
            self.k_norm = torch.nn.RMSNorm(head_dim, eps=1e-5)
        else:
            self.q_norm = torch.nn.Identity()
            self.k_norm = torch.nn.Identity()

        # Optional gating
        if self.use_gating:
            self.gate = torch.nn.Sequential(
                torch.nn.Linear(dim, dim, bias=False),
                torch.nn.Sigmoid())
        # Optional normalization
        if self.use_sandwich:
            self.sandwich = torch.nn.LayerNorm(dim, bias=False)

    def forward(self, x):
        """
        x: [B, L, D]
        """
        # Predict qkv packed as (batch, seq_len, 3, num_head, head_dim)
        qkv = self.to_qkv(x).unflatten(2, (3, self.num_head, self.head_dim))

        # Unpack (q, k, v) each as (batch, seq_len, num_head, head_dim)
        q, k, v = qkv.moveaxis(2,0).contiguous()

        # Apply qk normalization if enabled
        q = self.q_norm(q)
        k = self.k_norm(k)

        # Attention
        with torch.autocast(enabled=False, device_type="cuda"):
            q = q.to(self.attn_dtype)
            k = k.to(self.attn_dtype)
            v = v.to(self.attn_dtype)

        out = _attn_func(q, k, v)

        # Output layer to yield (batch, seq_len, dim)
        out = out.flatten(2,3)
        if self.use_gating:
            out = out * self.gate(x)
        out = self.fc(out)
        if self.use_sandwich:
            out = self.sandwich(out)

        return out


class CrossViewAttention(torch.nn.Module):
    def __init__(self,
        dim,
        head_dim,
        bias=False,
        use_qk_norm=True,
        use_gating=False,
        use_sandwich=False,
        out_dim=None,
    ):
        """
        Args:
            dim: Input dimension
            head_dim: Dimension of each attention head
            bias: Whether to use bias in QKV and output projection
            use_qk_norm: Whether to use Q-K normalization
            use_gating: Whether to apply gating before output ffn
        """
        super().__init__()
        assert dim % head_dim == 0, f"dim={dim} should be divisible by head_dim={head_dim}"
        num_head = dim // head_dim

        self.dim = dim
        self.out_dim = dim if out_dim is None else out_dim
        self.head_dim = head_dim
        self.num_head = num_head
        self.use_qk_norm = use_qk_norm
        self.use_gating = use_gating
        self.use_sandwich = use_sandwich
        self.attn_dtype = torch.bfloat16 if torch.cuda.get_device_capability()[0] >= 8 else torch.float16

        self.to_q = torch.nn.Linear(dim, dim, bias=bias)
        self.to_kv = torch.nn.Linear(dim, 2 * dim, bias=bias)
        self.fc = torch.nn.Linear(dim, self.out_dim, bias=bias)

        # Optional Q-K normalization
        if self.use_qk_norm:
            self.q_norm = torch.nn.RMSNorm(head_dim, eps=1e-5)
            self.k_norm = torch.nn.RMSNorm(head_dim, eps=1e-5)
        else:
            self.q_norm = torch.nn.Identity()
            self.k_norm = torch.nn.Identity()

        # Optional gating
        if self.use_gating:
            self.gate = torch.nn.Sequential(
                torch.nn.Linear(dim, dim, bias=False),
                torch.nn.Sigmoid())
        # Optional normalization
        if self.use_sandwich:
            self.sandwich = torch.nn.LayerNorm(dim, bias=False)

    def forward(self, x, kv=None):
        """
        x: [B, L, D]
        kv: ([B, M, D], [B, M, D]) If provided, x query on kv instead of doing self attention
        """
        q = self.to_q(x)

        # To shape of (batch, seq_len, num_head, head_dim)
        q = q.unflatten(2, (self.num_head, self.head_dim))
        q = self.q_norm(q)
        with torch.autocast(enabled=False, device_type="cuda"):
            q = q.to(self.attn_dtype)

        # Prepare (k, v)
        if kv is None:
            kv = self.to_kv(x)
            kv = kv.unflatten(2, (2, self.num_head, self.head_dim))
            k, v = kv.moveaxis(2, 0).contiguous()
            k = self.k_norm(k)
            with torch.autocast(enabled=False, device_type="cuda"):
                k = k.to(self.attn_dtype)
                v = v.to(self.attn_dtype)
        else:
            k, v = kv
            assert len(x) == len(k) and len(x) == len(v)

        # Attention
        out = _attn_func(q, k, v)

        # Output layer into (batch, seq_len, dim)
        out = out.flatten(2, 3)
        if self.use_gating:
            out = out * self.gate(x)
        out = self.fc(out)
        if self.use_sandwich:
            out = self.sandwich(out)

        return out, (k, v)