Use Flash-Attention 3 (prebuilt via HF kernels) as the attention backend
Browse filesLoad kernels-community/flash-attn3 (auto-resolves the prebuilt kernel for
ZeroGPU's Blackwell arch) and expose it as `flash_attn_interface`, which is what
DVLT's attention imports. Hardcode set_attn_backend("fa3") — dropped the
ATTN_BACKEND env + _select_attn_backend selection logic. Falls back to SDPA only
when the kernel is unavailable (e.g. local CPU).
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
- app.py +16 -16
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -4,6 +4,7 @@
|
|
| 4 |
# dependencies = [
|
| 5 |
# "gradio>=5.49,<6",
|
| 6 |
# "spaces",
|
|
|
|
| 7 |
# "trimesh>=4.4",
|
| 8 |
# "torch>=2.5.1",
|
| 9 |
# "torchvision>=0.20.1",
|
|
@@ -23,6 +24,7 @@ Hugging Face ZeroGPU Space (deps in requirements.txt; torch from the image).
|
|
| 23 |
from __future__ import annotations
|
| 24 |
|
| 25 |
import os
|
|
|
|
| 26 |
import tempfile
|
| 27 |
import time
|
| 28 |
from dataclasses import dataclass, field
|
|
@@ -35,10 +37,20 @@ import torch
|
|
| 35 |
from accelerate import Accelerator
|
| 36 |
from PIL import Image
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
from dvlt.common.constants import DataField, PredictionField
|
| 39 |
from dvlt.common.geometry import depth_to_world_coords_points
|
| 40 |
from dvlt.common.pose import to4x4
|
| 41 |
from dvlt.model.dvlt.model import DVLT, _slice_expand_flatten
|
|
|
|
| 42 |
from dvlt.util.preprocess import preprocess_images
|
| 43 |
from dvlt.viz.depth import overlay_depth_map
|
| 44 |
from dvlt.viz.glb import pointcloud_to_glb
|
|
@@ -52,7 +64,6 @@ PATCH_SIZE = 14
|
|
| 52 |
DEFAULT_STEPS = 12 # K — the model's default inference step count
|
| 53 |
MAX_STEPS = 24
|
| 54 |
MAX_FRAMES = 16 # cap views so global attention stays within the ZeroGPU budget
|
| 55 |
-
ATTN_BACKEND = os.environ.get("DVLT_ATTN", "auto") # auto | flash | fa3 (prefers fa3 if installed)
|
| 56 |
|
| 57 |
VIDEO_FPS_DEFAULT = 2.0
|
| 58 |
DECODE_EVERY_DEFAULT = 3
|
|
@@ -71,24 +82,13 @@ _ACCEL = Accelerator(mixed_precision="bf16" if torch.cuda.is_available() else "n
|
|
| 71 |
_MODEL: DVLT | None = None
|
| 72 |
|
| 73 |
|
| 74 |
-
def _select_attn_backend():
|
| 75 |
-
"""Pick the fastest available attention backend (fa3 > flash > auto)."""
|
| 76 |
-
from dvlt.model_components import set_attn_backend
|
| 77 |
-
|
| 78 |
-
order = {"fa3": ["fa3", "flash", "auto"], "flash": ["flash", "auto"]}.get(ATTN_BACKEND, ["auto"])
|
| 79 |
-
for backend in order:
|
| 80 |
-
try:
|
| 81 |
-
set_attn_backend(backend)
|
| 82 |
-
print(f"[dvlt] attention backend: {backend}", flush=True)
|
| 83 |
-
return
|
| 84 |
-
except Exception:
|
| 85 |
-
continue
|
| 86 |
-
|
| 87 |
-
|
| 88 |
def load_model() -> DVLT:
|
| 89 |
global _MODEL
|
| 90 |
if _MODEL is None:
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
| 92 |
model = DVLT(img_size=IMG_SIZE, depth_head_type="conv")
|
| 93 |
model.load_pretrained(CHECKPOINT, strict=True)
|
| 94 |
model.setup_test(_ACCEL)
|
|
|
|
| 4 |
# dependencies = [
|
| 5 |
# "gradio>=5.49,<6",
|
| 6 |
# "spaces",
|
| 7 |
+
# "kernels",
|
| 8 |
# "trimesh>=4.4",
|
| 9 |
# "torch>=2.5.1",
|
| 10 |
# "torchvision>=0.20.1",
|
|
|
|
| 24 |
from __future__ import annotations
|
| 25 |
|
| 26 |
import os
|
| 27 |
+
import sys
|
| 28 |
import tempfile
|
| 29 |
import time
|
| 30 |
from dataclasses import dataclass, field
|
|
|
|
| 37 |
from accelerate import Accelerator
|
| 38 |
from PIL import Image
|
| 39 |
|
| 40 |
+
# Flash-Attention 3, prebuilt for ZeroGPU's Blackwell GPUs and exposed under the
|
| 41 |
+
# module name DVLT imports (`from flash_attn_interface import flash_attn_func`).
|
| 42 |
+
try:
|
| 43 |
+
from kernels import get_kernel
|
| 44 |
+
|
| 45 |
+
sys.modules["flash_attn_interface"] = get_kernel("kernels-community/flash-attn3")
|
| 46 |
+
except Exception as exc: # local / no compatible kernel -> DVLT falls back to SDPA
|
| 47 |
+
print(f"[dvlt] FA3 kernel unavailable ({exc}); using default attention.")
|
| 48 |
+
|
| 49 |
from dvlt.common.constants import DataField, PredictionField
|
| 50 |
from dvlt.common.geometry import depth_to_world_coords_points
|
| 51 |
from dvlt.common.pose import to4x4
|
| 52 |
from dvlt.model.dvlt.model import DVLT, _slice_expand_flatten
|
| 53 |
+
from dvlt.model_components import set_attn_backend
|
| 54 |
from dvlt.util.preprocess import preprocess_images
|
| 55 |
from dvlt.viz.depth import overlay_depth_map
|
| 56 |
from dvlt.viz.glb import pointcloud_to_glb
|
|
|
|
| 64 |
DEFAULT_STEPS = 12 # K — the model's default inference step count
|
| 65 |
MAX_STEPS = 24
|
| 66 |
MAX_FRAMES = 16 # cap views so global attention stays within the ZeroGPU budget
|
|
|
|
| 67 |
|
| 68 |
VIDEO_FPS_DEFAULT = 2.0
|
| 69 |
DECODE_EVERY_DEFAULT = 3
|
|
|
|
| 82 |
_MODEL: DVLT | None = None
|
| 83 |
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
def load_model() -> DVLT:
|
| 86 |
global _MODEL
|
| 87 |
if _MODEL is None:
|
| 88 |
+
try:
|
| 89 |
+
set_attn_backend("fa3")
|
| 90 |
+
except Exception as exc: # noqa: BLE001
|
| 91 |
+
print(f"[dvlt] fa3 unavailable ({exc}); using default attention.")
|
| 92 |
model = DVLT(img_size=IMG_SIZE, depth_head_type="conv")
|
| 93 |
model.load_pretrained(CHECKPOINT, strict=True)
|
| 94 |
model.setup_test(_ACCEL)
|
requirements.txt
CHANGED
|
@@ -8,6 +8,7 @@
|
|
| 8 |
|
| 9 |
gradio>=5.49,<6
|
| 10 |
spaces
|
|
|
|
| 11 |
trimesh>=4.4
|
| 12 |
|
| 13 |
dvlt @ https://huggingface.co/spaces/blanchon/dvlt/resolve/main/packages/dvlt/wheels/dvlt-0.0.1-py3-none-any.whl
|
|
|
|
| 8 |
|
| 9 |
gradio>=5.49,<6
|
| 10 |
spaces
|
| 11 |
+
kernels
|
| 12 |
trimesh>=4.4
|
| 13 |
|
| 14 |
dvlt @ https://huggingface.co/spaces/blanchon/dvlt/resolve/main/packages/dvlt/wheels/dvlt-0.0.1-py3-none-any.whl
|