inference.py: native video preprocessing (no qwen-vl-utils)
Browse files- inference.py +359 -224
inference.py
CHANGED
|
@@ -1,224 +1,359 @@
|
|
| 1 |
-
"""fusion-embedding-1 inference — one embedding space for text, images, and audio.
|
| 2 |
-
|
| 3 |
-
Loads the frozen Qwen3-VL-Embedding base (native paths for text and images), the frozen
|
| 4 |
-
Qwen2.5-Omni audio tower, and this repository's trained connector checkpoint. All inputs
|
| 5 |
-
use the base model's official chat-template format; embedding quality is sensitive to
|
| 6 |
-
this formatting, so use the templates provided here rather than constructing your own.
|
| 7 |
-
|
| 8 |
-
from inference import FusionEmbedder
|
| 9 |
-
fe = FusionEmbedder.from_pretrained("EximiusLabs/fusion-embedding-1-2b-preview")
|
| 10 |
-
a, t, i = fe.embed_audio("dog.wav"), fe.embed_text("a dog barks"), fe.embed_image("dog.jpg")
|
| 11 |
-
|
| 12 |
-
Requires: fusion_embedding (pip install git+https://github.com/Eximius-Labs/fusion-embedding-1),
|
| 13 |
-
transformers>=4.46, torchvision, pillow, soundfile, librosa.
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
from __future__ import annotations
|
| 17 |
-
|
| 18 |
-
import dataclasses
|
| 19 |
-
import
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
f"<|im_start|>
|
| 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 |
-
raise
|
| 174 |
-
"video
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""fusion-embedding-1 inference — one embedding space for text, images, and audio.
|
| 2 |
+
|
| 3 |
+
Loads the frozen Qwen3-VL-Embedding base (native paths for text and images), the frozen
|
| 4 |
+
Qwen2.5-Omni audio tower, and this repository's trained connector checkpoint. All inputs
|
| 5 |
+
use the base model's official chat-template format; embedding quality is sensitive to
|
| 6 |
+
this formatting, so use the templates provided here rather than constructing your own.
|
| 7 |
+
|
| 8 |
+
from inference import FusionEmbedder
|
| 9 |
+
fe = FusionEmbedder.from_pretrained("EximiusLabs/fusion-embedding-1-2b-preview")
|
| 10 |
+
a, t, i = fe.embed_audio("dog.wav"), fe.embed_text("a dog barks"), fe.embed_image("dog.jpg")
|
| 11 |
+
|
| 12 |
+
Requires: fusion_embedding (pip install git+https://github.com/Eximius-Labs/fusion-embedding-1),
|
| 13 |
+
transformers>=4.46, torchvision, pillow, soundfile, librosa.
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
from __future__ import annotations
|
| 17 |
+
|
| 18 |
+
import dataclasses
|
| 19 |
+
import math
|
| 20 |
+
import os
|
| 21 |
+
from typing import TYPE_CHECKING, Optional, Union
|
| 22 |
+
|
| 23 |
+
if TYPE_CHECKING:
|
| 24 |
+
import numpy as np
|
| 25 |
+
|
| 26 |
+
import torch
|
| 27 |
+
|
| 28 |
+
BASE_MODEL = "Qwen/Qwen3-VL-Embedding-2B"
|
| 29 |
+
AUDIO_MODEL = "Qwen/Qwen2.5-Omni-7B"
|
| 30 |
+
DEFAULT_QUERY_INSTRUCTION = "Retrieve images or text relevant to the user's query."
|
| 31 |
+
DOC_INSTRUCTION = "Represent the user's input."
|
| 32 |
+
CKPT_FILE = "fusion-embedding-1-2b-preview.pt"
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def _chat(instruction: str, user_content: str) -> str:
|
| 36 |
+
"""The base's official embedding format: system-turn instruction, assistant opener."""
|
| 37 |
+
return (f"<|im_start|>system\n{instruction}<|im_end|>\n"
|
| 38 |
+
f"<|im_start|>user\n{user_content}<|im_end|>\n"
|
| 39 |
+
f"<|im_start|>assistant\n")
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# --------------------------------------------------------------------------- #
|
| 43 |
+
# video preprocessing (native)
|
| 44 |
+
#
|
| 45 |
+
# Faithful reimplementation of the base model's reference video preprocessing
|
| 46 |
+
# (the Qwen3-VL-Embedding scripts' vision pipeline at image_patch_size=16), so
|
| 47 |
+
# no extra vision package is needed: frame selection, the per-frame image
|
| 48 |
+
# resize applied to frame-sequence inputs, the per-video smart resize under the
|
| 49 |
+
# total-pixel budget, and the processor kwargs (do_resize=False,
|
| 50 |
+
# do_sample_frames=False, video_metadata) match the reference exactly; outputs
|
| 51 |
+
# are verified bitwise-equal against the reference implementation on identical
|
| 52 |
+
# inputs. Decoded-video inputs (frame tensors, file paths via torchcodec) take
|
| 53 |
+
# the reference path-input treatment: a single per-video resize, no per-frame
|
| 54 |
+
# image resize.
|
| 55 |
+
# --------------------------------------------------------------------------- #
|
| 56 |
+
_V_PATCH_FACTOR = 32 # image_patch_size 16 x spatial merge 2
|
| 57 |
+
_V_FRAME_FACTOR = 2
|
| 58 |
+
_V_DEFAULT_FPS = 1.0
|
| 59 |
+
_V_DEFAULT_MAX_FRAMES = 64
|
| 60 |
+
_V_MIN_PIXELS = 128 * _V_PATCH_FACTOR ** 2 # per-frame floor
|
| 61 |
+
_V_MAX_PIXELS = 768 * _V_PATCH_FACTOR ** 2 # per-frame ceiling
|
| 62 |
+
_V_TOTAL_PIXELS = 10 * _V_MAX_PIXELS # per-video budget
|
| 63 |
+
_V_IMG_MIN_PIXELS = 4 * _V_PATCH_FACTOR ** 2 # per-frame image defaults
|
| 64 |
+
_V_IMG_MAX_PIXELS = 16384 * _V_PATCH_FACTOR ** 2
|
| 65 |
+
_V_FPS_MIN_FRAMES = 4
|
| 66 |
+
_V_MAX_RATIO = 200
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def _v_round(n: float, f: int) -> int:
|
| 70 |
+
return round(n / f) * f
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def _v_ceil(n: float, f: int) -> int:
|
| 74 |
+
return math.ceil(n / f) * f
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def _v_floor(n: float, f: int) -> int:
|
| 78 |
+
return math.floor(n / f) * f
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def _v_smart_resize(height: int, width: int, factor: int,
|
| 82 |
+
min_pixels: int, max_pixels: int):
|
| 83 |
+
if max(height, width) / min(height, width) > _V_MAX_RATIO:
|
| 84 |
+
raise ValueError(
|
| 85 |
+
f"absolute aspect ratio must be smaller than {_V_MAX_RATIO}, "
|
| 86 |
+
f"got {max(height, width) / min(height, width)}")
|
| 87 |
+
h_bar = max(factor, _v_round(height, factor))
|
| 88 |
+
w_bar = max(factor, _v_round(width, factor))
|
| 89 |
+
if h_bar * w_bar > max_pixels:
|
| 90 |
+
beta = math.sqrt((height * width) / max_pixels)
|
| 91 |
+
h_bar = _v_floor(height / beta, factor)
|
| 92 |
+
w_bar = _v_floor(width / beta, factor)
|
| 93 |
+
elif h_bar * w_bar < min_pixels:
|
| 94 |
+
beta = math.sqrt(min_pixels / (height * width))
|
| 95 |
+
h_bar = _v_ceil(height * beta, factor)
|
| 96 |
+
w_bar = _v_ceil(width * beta, factor)
|
| 97 |
+
return h_bar, w_bar
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def _v_frame_to_image(frame):
|
| 101 |
+
"""Frame-sequence element -> resized RGB PIL image (reference fetch_image)."""
|
| 102 |
+
from PIL import Image
|
| 103 |
+
|
| 104 |
+
if isinstance(frame, (str, os.PathLike)):
|
| 105 |
+
image = Image.open(str(frame))
|
| 106 |
+
else:
|
| 107 |
+
image = frame
|
| 108 |
+
if image.mode == "RGBA":
|
| 109 |
+
white = Image.new("RGB", image.size, (255, 255, 255))
|
| 110 |
+
white.paste(image, mask=image.split()[3])
|
| 111 |
+
image = white
|
| 112 |
+
else:
|
| 113 |
+
image = image.convert("RGB")
|
| 114 |
+
width, height = image.size
|
| 115 |
+
rh, rw = _v_smart_resize(height, width, _V_PATCH_FACTOR,
|
| 116 |
+
_V_IMG_MIN_PIXELS, _V_IMG_MAX_PIXELS)
|
| 117 |
+
return image.resize((rw, rh))
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
def _v_prepare(video, fps, max_frames):
|
| 121 |
+
"""Normalize any supported video input to (uint8 tensor [T,C,H,W], metadata).
|
| 122 |
+
|
| 123 |
+
Frame sequences follow the reference list-input treatment (per-frame image
|
| 124 |
+
resize, pad to an even count by repeating the last frame, synthetic
|
| 125 |
+
metadata at 2 fps). Frame tensors and file paths follow the reference
|
| 126 |
+
decoded-video treatment (frame selection only; single per-video resize).
|
| 127 |
+
"""
|
| 128 |
+
import numpy as np
|
| 129 |
+
|
| 130 |
+
if isinstance(video, torch.Tensor):
|
| 131 |
+
if video.ndim != 4 or video.shape[1] not in (1, 3):
|
| 132 |
+
raise ValueError(
|
| 133 |
+
f"expected a [T, C, H, W] frame tensor, got {list(video.shape)}")
|
| 134 |
+
frames = video
|
| 135 |
+
if frames.shape[1] == 1:
|
| 136 |
+
frames = frames.expand(-1, 3, -1, -1)
|
| 137 |
+
if frames.dtype != torch.uint8:
|
| 138 |
+
frames = frames.clamp(0, 255).to(torch.uint8)
|
| 139 |
+
t = frames.shape[0]
|
| 140 |
+
mf = max_frames or _V_DEFAULT_MAX_FRAMES
|
| 141 |
+
if t > mf:
|
| 142 |
+
idx = np.linspace(0, t - 1, mf, dtype=int)
|
| 143 |
+
frames = frames[torch.as_tensor(idx.copy())]
|
| 144 |
+
t = mf
|
| 145 |
+
n = _v_ceil(t, _V_FRAME_FACTOR)
|
| 146 |
+
if t < n:
|
| 147 |
+
frames = torch.cat([frames, frames[-1:].expand(n - t, -1, -1, -1)])
|
| 148 |
+
metadata = dict(fps=2.0, frames_indices=list(range(n)),
|
| 149 |
+
total_num_frames=float(n))
|
| 150 |
+
return frames, metadata
|
| 151 |
+
|
| 152 |
+
if isinstance(video, (str, os.PathLike)):
|
| 153 |
+
v = str(video)
|
| 154 |
+
if v.startswith("file://"):
|
| 155 |
+
v = v[7:]
|
| 156 |
+
try:
|
| 157 |
+
from torchcodec.decoders import VideoDecoder
|
| 158 |
+
except ImportError as e:
|
| 159 |
+
raise ImportError(
|
| 160 |
+
"embedding a video by file path requires torchcodec "
|
| 161 |
+
"(pip install torchcodec); alternatively pass decoded frames "
|
| 162 |
+
"(a [T, C, H, W] tensor or a list of PIL images)") from e
|
| 163 |
+
decoder = VideoDecoder(v)
|
| 164 |
+
video_fps = decoder.metadata.average_fps
|
| 165 |
+
total = decoder.metadata.num_frames
|
| 166 |
+
want_fps = fps or _V_DEFAULT_FPS
|
| 167 |
+
min_frames = _v_ceil(_V_FPS_MIN_FRAMES, _V_FRAME_FACTOR)
|
| 168 |
+
max_f = _v_floor(max_frames or _V_DEFAULT_MAX_FRAMES, _V_FRAME_FACTOR)
|
| 169 |
+
n = total / video_fps * want_fps
|
| 170 |
+
n = min(min(max(n, min_frames), max_f), total)
|
| 171 |
+
n = _v_floor(n, _V_FRAME_FACTOR)
|
| 172 |
+
if not (_V_FRAME_FACTOR <= n <= total):
|
| 173 |
+
raise ValueError(
|
| 174 |
+
f"video too short: {total} frames; need >= {_V_FRAME_FACTOR}")
|
| 175 |
+
idx = torch.linspace(0, total - 1, n).round().long().tolist()
|
| 176 |
+
frames = decoder.get_frames_at(indices=idx).data
|
| 177 |
+
metadata = dict(fps=video_fps, frames_indices=idx,
|
| 178 |
+
total_num_frames=total, video_backend="torchcodec")
|
| 179 |
+
return frames, metadata
|
| 180 |
+
|
| 181 |
+
# frame sequence (PIL images and/or paths)
|
| 182 |
+
frames = list(video)
|
| 183 |
+
if not frames:
|
| 184 |
+
raise ValueError("empty frame sequence")
|
| 185 |
+
mf = max_frames or _V_DEFAULT_MAX_FRAMES
|
| 186 |
+
if len(frames) > mf:
|
| 187 |
+
idx = np.linspace(0, len(frames) - 1, mf, dtype=int)
|
| 188 |
+
frames = [frames[i] for i in idx]
|
| 189 |
+
images = [_v_frame_to_image(f) for f in frames]
|
| 190 |
+
n = _v_ceil(len(images), _V_FRAME_FACTOR)
|
| 191 |
+
if len(images) < n:
|
| 192 |
+
images.extend([images[-1]] * (n - len(images)))
|
| 193 |
+
tensor = torch.stack([
|
| 194 |
+
torch.from_numpy(np.array(image).transpose(2, 0, 1)) for image in images
|
| 195 |
+
])
|
| 196 |
+
metadata = dict(fps=2.0, frames_indices=list(range(n)),
|
| 197 |
+
total_num_frames=float(n))
|
| 198 |
+
return tensor, metadata
|
| 199 |
+
|
| 200 |
+
|
| 201 |
+
def _v_resize_video(frames: torch.Tensor) -> torch.Tensor:
|
| 202 |
+
"""Per-video smart resize under the total-pixel budget (reference exact)."""
|
| 203 |
+
from torchvision.transforms import InterpolationMode
|
| 204 |
+
from torchvision.transforms import functional as TF
|
| 205 |
+
|
| 206 |
+
n, _, height, width = frames.shape
|
| 207 |
+
max_pixels = max(min(_V_MAX_PIXELS, _V_TOTAL_PIXELS / n * _V_FRAME_FACTOR),
|
| 208 |
+
int(_V_MIN_PIXELS * 1.05))
|
| 209 |
+
rh, rw = _v_smart_resize(height, width, _V_PATCH_FACTOR,
|
| 210 |
+
_V_MIN_PIXELS, max_pixels)
|
| 211 |
+
return TF.resize(frames, [rh, rw],
|
| 212 |
+
interpolation=InterpolationMode.BICUBIC,
|
| 213 |
+
antialias=True).float()
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
class FusionEmbedder:
|
| 217 |
+
def __init__(self, ckpt_path: str, device: str = "cuda", dtype=torch.bfloat16):
|
| 218 |
+
from transformers import AutoFeatureExtractor, AutoModel, AutoProcessor
|
| 219 |
+
|
| 220 |
+
from fusion_embedding.config import FusionConfig
|
| 221 |
+
from fusion_embedding.hf_components import BaseLMAdapter, load_audio_tower
|
| 222 |
+
from fusion_embedding.model import FusionEmbeddingModel, last_token_pool
|
| 223 |
+
|
| 224 |
+
self.device = device
|
| 225 |
+
self._pool = last_token_pool
|
| 226 |
+
ck = torch.load(ckpt_path, map_location="cpu", weights_only=False)
|
| 227 |
+
flds = {f.name for f in dataclasses.fields(FusionConfig)}
|
| 228 |
+
self.cfg = FusionConfig(**{k: v for k, v in ck["config"].items() if k in flds})
|
| 229 |
+
|
| 230 |
+
self.full = AutoModel.from_pretrained(BASE_MODEL, trust_remote_code=True, dtype=dtype)
|
| 231 |
+
self.full = self.full.to(device).eval()
|
| 232 |
+
for p in self.full.parameters():
|
| 233 |
+
p.requires_grad_(False)
|
| 234 |
+
self.proc = AutoProcessor.from_pretrained(BASE_MODEL, trust_remote_code=True)
|
| 235 |
+
self.tok = self.proc.tokenizer
|
| 236 |
+
|
| 237 |
+
tower, _, _ = load_audio_tower(AUDIO_MODEL, device=device, dtype=dtype)
|
| 238 |
+
self.fe_audio = AutoFeatureExtractor.from_pretrained(AUDIO_MODEL, trust_remote_code=True)
|
| 239 |
+
|
| 240 |
+
self.model = FusionEmbeddingModel(self.cfg, self.full.get_input_embeddings(),
|
| 241 |
+
BaseLMAdapter(self.full.language_model),
|
| 242 |
+
audio_encoder=tower)
|
| 243 |
+
self.model.resampler.to(device).float()
|
| 244 |
+
self.model.resampler.load_state_dict(ck["resampler"])
|
| 245 |
+
self.model.text_whitening.load_state_dict(ck["text_whitening"]) # identity if unfitted
|
| 246 |
+
self.model.eval()
|
| 247 |
+
|
| 248 |
+
# ------------------------------------------------------------------ loading
|
| 249 |
+
@classmethod
|
| 250 |
+
def from_pretrained(cls, repo_or_path: str, device: str = "cuda",
|
| 251 |
+
revision: Optional[str] = None, **kw) -> "FusionEmbedder":
|
| 252 |
+
"""Load from a local checkpoint path or an HF repo. ``revision`` pins a repo
|
| 253 |
+
tag/commit (e.g. ``"v0.1-preview"``, ``"v0.2-preview"``); default is latest."""
|
| 254 |
+
if os.path.exists(repo_or_path):
|
| 255 |
+
path = repo_or_path
|
| 256 |
+
else:
|
| 257 |
+
from huggingface_hub import hf_hub_download
|
| 258 |
+
path = hf_hub_download(repo_or_path, CKPT_FILE, revision=revision)
|
| 259 |
+
return cls(path, device=device, **kw)
|
| 260 |
+
|
| 261 |
+
# ------------------------------------------------------------------ helpers
|
| 262 |
+
def _finish(self, pooled: torch.Tensor, dim: Optional[int]) -> torch.Tensor:
|
| 263 |
+
from fusion_embedding.model import mrl_truncate_normalize
|
| 264 |
+
return mrl_truncate_normalize(pooled.float(), dim or self.cfg.mrl_default).squeeze(0).cpu()
|
| 265 |
+
|
| 266 |
+
# ------------------------------------------------------------------ audio
|
| 267 |
+
@torch.no_grad()
|
| 268 |
+
def embed_audio(self, audio: Union[str, "np.ndarray"], sr: Optional[int] = None,
|
| 269 |
+
dim: Optional[int] = None) -> torch.Tensor:
|
| 270 |
+
import librosa
|
| 271 |
+
import soundfile as sf
|
| 272 |
+
if isinstance(audio, (str, os.PathLike)):
|
| 273 |
+
wav, sr = sf.read(str(audio), dtype="float32")
|
| 274 |
+
else:
|
| 275 |
+
wav = audio
|
| 276 |
+
assert sr is not None, "pass sr= when embedding a raw array"
|
| 277 |
+
if getattr(wav, "ndim", 1) > 1:
|
| 278 |
+
wav = wav.mean(axis=1)
|
| 279 |
+
target_sr = self.fe_audio.sampling_rate
|
| 280 |
+
if sr != target_sr:
|
| 281 |
+
wav = librosa.resample(wav, orig_sr=sr, target_sr=target_sr)
|
| 282 |
+
feats = self.fe_audio(wav, sampling_rate=target_sr, return_tensors="pt",
|
| 283 |
+
return_attention_mask=True, padding="max_length", truncation=True)
|
| 284 |
+
mel = feats["input_features"][0]
|
| 285 |
+
am = feats.get("attention_mask")
|
| 286 |
+
if am is not None:
|
| 287 |
+
mel = mel[:, : int(am[0].sum().item())]
|
| 288 |
+
audio_tok = self.model.audio_tokens(
|
| 289 |
+
mel.unsqueeze(0).to(self.device),
|
| 290 |
+
torch.ones(1, mel.shape[1], dtype=torch.bool, device=self.device))
|
| 291 |
+
ids = torch.tensor([[self.cfg.audio_pad_id] * self.cfg.n_query + [self.cfg.eos_id]],
|
| 292 |
+
device=self.device)
|
| 293 |
+
pooled = self.model.encode_audio(ids, torch.ones_like(ids), audio_tok)
|
| 294 |
+
return self._finish(pooled, dim)
|
| 295 |
+
|
| 296 |
+
# ------------------------------------------------------------------ text
|
| 297 |
+
@torch.no_grad()
|
| 298 |
+
def embed_text(self, text: str, instruction: str = DEFAULT_QUERY_INSTRUCTION,
|
| 299 |
+
dim: Optional[int] = None) -> torch.Tensor:
|
| 300 |
+
ids = self.tok.encode(_chat(instruction, text), add_special_tokens=False)[:512]
|
| 301 |
+
ids_t = torch.tensor([ids], device=self.device)
|
| 302 |
+
pooled = self.model.encode_text(ids_t, torch.ones_like(ids_t))
|
| 303 |
+
return self._finish(self.model.text_whitening(pooled), dim)
|
| 304 |
+
|
| 305 |
+
# ------------------------------------------------------------------ image
|
| 306 |
+
@torch.no_grad()
|
| 307 |
+
def embed_image(self, image, dim: Optional[int] = None) -> torch.Tensor:
|
| 308 |
+
from PIL import Image
|
| 309 |
+
if isinstance(image, (str, os.PathLike)):
|
| 310 |
+
image = Image.open(str(image))
|
| 311 |
+
image = image.convert("RGB")
|
| 312 |
+
text = _chat(DOC_INSTRUCTION, "<|vision_start|><|image_pad|><|vision_end|>")
|
| 313 |
+
inputs = self.proc(text=[text], images=[image], return_tensors="pt").to(self.device)
|
| 314 |
+
h = self.full(**inputs).last_hidden_state
|
| 315 |
+
pooled = self._pool(h, inputs["attention_mask"])
|
| 316 |
+
return self._finish(pooled, dim)
|
| 317 |
+
|
| 318 |
+
# ------------------------------------------------------------------ video
|
| 319 |
+
@torch.no_grad()
|
| 320 |
+
def embed_video(self, video, fps: Optional[float] = None,
|
| 321 |
+
max_frames: Optional[int] = None,
|
| 322 |
+
dim: Optional[int] = None) -> torch.Tensor:
|
| 323 |
+
"""Embed a video through the frozen base model's own video path.
|
| 324 |
+
|
| 325 |
+
``video`` is a decoded frame tensor ([T, C, H, W], e.g. straight from
|
| 326 |
+
a torchcodec ``VideoDecoder``), a file path/URL (decoded with
|
| 327 |
+
torchcodec, 1 fps up to 64 frames), or a pre-extracted frame sequence
|
| 328 |
+
(PIL images and/or frame paths, sampled uniformly to 64).
|
| 329 |
+
Preprocessing natively reimplements the base model's reference
|
| 330 |
+
scripts (see the module-level helpers above); no extra vision package
|
| 331 |
+
is required. Like images, video is a non-audio input: it takes the
|
| 332 |
+
frozen path (no whitening, no adapters).
|
| 333 |
+
"""
|
| 334 |
+
gate = getattr(self.model, "_adapter_gate", None)
|
| 335 |
+
if gate is not None and gate.active:
|
| 336 |
+
# The video path runs through the same (hook-carrying) decoder layers;
|
| 337 |
+
# non-audio inputs must run with the gate closed so the adapter branch
|
| 338 |
+
# never runs. Mirrors the encode_text/embed_image guards.
|
| 339 |
+
raise RuntimeError("adapter gate is open during a video embed — "
|
| 340 |
+
"non-audio inputs must run with the gate closed")
|
| 341 |
+
frames, metadata = _v_prepare(video, fps, max_frames)
|
| 342 |
+
frames = _v_resize_video(frames)
|
| 343 |
+
text = _chat(DOC_INSTRUCTION, "<|vision_start|><|video_pad|><|vision_end|>")
|
| 344 |
+
inputs = self.proc(text=[text], videos=[frames],
|
| 345 |
+
video_metadata=[metadata],
|
| 346 |
+
do_resize=False, do_sample_frames=False,
|
| 347 |
+
return_tensors="pt").to(self.device)
|
| 348 |
+
h = self.full(**inputs).last_hidden_state
|
| 349 |
+
pooled = self._pool(h, inputs["attention_mask"])
|
| 350 |
+
return self._finish(pooled, dim)
|
| 351 |
+
|
| 352 |
+
# ------------------------------------------------------------------ cross-modal readout
|
| 353 |
+
@staticmethod
|
| 354 |
+
def center(embs: torch.Tensor) -> torch.Tensor:
|
| 355 |
+
"""Per-modality mean-centering followed by renormalization. Recommended when ranking
|
| 356 |
+
a gallery of one modality against queries of another; improves cross-modal R@1 by
|
| 357 |
+
roughly two points across modality pairs in our evaluation."""
|
| 358 |
+
c = embs - embs.mean(dim=0, keepdim=True)
|
| 359 |
+
return torch.nn.functional.normalize(c, dim=-1)
|