hljiang commited on
Commit
e68f79a
·
1 Parent(s): 4c4ca94

Speed up inference: SDPA attention (replaces removed xformers), fp16 autocast on GPU, preload default model at startup

Browse files
app.py CHANGED
@@ -386,6 +386,7 @@ def run_model(model, normalized_rgb):
386
  _device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
387
  if not _model_on_device:
388
  model.to(_device)
 
389
  _model_on_device = True
390
 
391
  equi_inputs = normalized_rgb.to(_device)
@@ -624,6 +625,9 @@ if __name__ == "__main__":
624
  if args.download or os.environ.get("HF_SPACE") == "1" or os.environ.get("SPACE_ID"):
625
  ensure_checkpoints()
626
 
 
 
 
627
  demo = build_demo(args.model_path)
628
  server_port = resolve_server_port(args.server_port)
629
  demo.queue(default_concurrency_limit=1)
 
386
  _device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
387
  if not _model_on_device:
388
  model.to(_device)
389
+ model.mixed_precision = _device.type == "cuda"
390
  _model_on_device = True
391
 
392
  equi_inputs = normalized_rgb.to(_device)
 
625
  if args.download or os.environ.get("HF_SPACE") == "1" or os.environ.get("SPACE_ID"):
626
  ensure_checkpoints()
627
 
628
+ if os.path.exists(args.model_path):
629
+ load_model(args.model_path)
630
+
631
  demo = build_demo(args.model_path)
632
  server_port = resolve_server_port(args.server_port)
633
  demo.queue(default_concurrency_limit=1)
depth_anything_v2/dinov2_layers/attention.py CHANGED
@@ -10,6 +10,7 @@
10
 
11
  import logging
12
 
 
13
  from torch import Tensor
14
  from torch import nn
15
 
@@ -50,13 +51,11 @@ class Attention(nn.Module):
50
  B, N, C = x.shape
51
  qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4)
52
 
53
- q, k, v = qkv[0] * self.scale, qkv[1], qkv[2]
54
- attn = q @ k.transpose(-2, -1)
55
-
56
- attn = attn.softmax(dim=-1)
57
- attn = self.attn_drop(attn)
58
-
59
- x = (attn @ v).transpose(1, 2).reshape(B, N, C)
60
  x = self.proj(x)
61
  x = self.proj_drop(x)
62
  return x
 
10
 
11
  import logging
12
 
13
+ import torch.nn.functional as F
14
  from torch import Tensor
15
  from torch import nn
16
 
 
51
  B, N, C = x.shape
52
  qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4)
53
 
54
+ q, k, v = qkv[0], qkv[1], qkv[2]
55
+ x = F.scaled_dot_product_attention(
56
+ q, k, v, dropout_p=self.attn_drop.p if self.training else 0.0
57
+ )
58
+ x = x.transpose(1, 2).reshape(B, N, C)
 
 
59
  x = self.proj(x)
60
  x = self.proj_drop(x)
61
  return x