phrographica commited on
Commit
d7c809b
·
verified ·
1 Parent(s): d365a97

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -0
app.py CHANGED
@@ -18,6 +18,37 @@ import gc
18
  import numpy as np
19
  import os
20
  import torch
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  from video_depth_anything.video_depth import VideoDepthAnything
23
  from utils.dc_utils import read_video_frames, save_video
 
18
  import numpy as np
19
  import os
20
  import torch
21
+ import torch.nn.functional as F
22
+ import xformers
23
+ import xformers.ops
24
+
25
+ _xf_original = xformers.ops.memory_efficient_attention
26
+
27
+
28
+ def _sdpa_shim(query, key, value, attn_bias=None, p=0.0, scale=None, op=None, **kwargs):
29
+ """Route xformers memory-efficient attention through torch SDPA.
30
+
31
+ xformers uses [B, M, H, K]; torch SDPA uses [B, H, M, K]. A 3D input is
32
+ treated as a single head. Only tensor or None attn_bias is supported;
33
+ anything else falls through to the original implementation.
34
+ """
35
+ if attn_bias is not None and not isinstance(attn_bias, torch.Tensor):
36
+ return _xf_original(query, key, value, attn_bias, p, scale, op)
37
+
38
+ squeeze = query.dim() == 3
39
+ if squeeze:
40
+ q, k, v = (t.unsqueeze(1) for t in (query, key, value))
41
+ else:
42
+ q, k, v = (t.transpose(1, 2) for t in (query, key, value))
43
+
44
+ out = F.scaled_dot_product_attention(
45
+ q, k, v, attn_mask=attn_bias, dropout_p=p, scale=scale
46
+ )
47
+
48
+ return out.squeeze(1) if squeeze else out.transpose(1, 2)
49
+
50
+
51
+ xformers.ops.memory_efficient_attention = _sdpa_shim
52
 
53
  from video_depth_anything.video_depth import VideoDepthAnything
54
  from utils.dc_utils import read_video_frames, save_video