视频_Embedding(Cosmos)
更新时间:2026-07-29
简介
CosmosVideoEmbedding 基于 NVIDIA Cosmos-Embed1-448p 模型,对输入视频进行语义 embedding,输出 L2 归一化的浮点向量,用于视频检索、相似度计算与去重。
功能描述
• 使用 transformers 原生推理加载 Cosmos-Embed1 模型,从视频中均匀抽帧(默认 8 帧)后计算视频语义向量。
• 支持三种视频输入源:video_url / video_base64 / video_binary。
• 输出投影后的视觉语义向量(visual_proj),与文本 embedding 处于同一空间,默认做 L2 归一化。
• 必须运行在 CUDA GPU 上,权重需预置于 {model_path}/{model_name}(不自动下载),出错时可选返回 None 或抛异常。
算子参数
输入
| 输入 | 含义 |
|---|---|
| videos | 视频输入列,取值形式由 video_src_type 决定(视频 URL / base64 / 二进制) |
输出
| 输出 | 含义 |
|---|---|
| result | 视频 embedding,类型 list<float32>;默认 L2 归一化。批次失败且 fail_on_error=False 时该行为 None |
参数
| 参数名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| video_src_type | str | "video_url" | 视频输入类型,可选 "video_url" / "video_base64" / "video_binary" |
| model_path | str | "/opt/aihc/models" | 权重根目录,实际模型目录为 {model_path}/{model_name} |
| model_name | str | "nvidia/Cosmos-Embed1-448p" | 模型名称/子目录,默认使用 Cosmos-Embed1-448p |
| batch_size | int | 4 | 单批处理的视频数量 |
| num_frames | int | 8 | 每段视频均匀抽取的帧数 |
| dtype | str | "auto" | 计算精度,可选 "auto" / "bfloat16" / "float16" / "float32";auto 时按 GPU 是否支持 bf16 自动选择 |
| rank | int | 0 | 多卡 rank,设备为 cuda:{rank % cuda_device_count} |
| normalize | bool | True | 是否对输出向量做 L2 归一化 |
| fail_on_error | bool | False | 批次出错时是否抛异常;False 时对应行返回 None(向后兼容旧调用方) |
调用示例
Python
1from __future__ import annotations
2
3import os
4
5import daft
6from daft import col
7
8from daft.aihc.common.udf import aihc_udf
9from daft.aihc.functions.multimodal.embedding.cosmos_video_embedding import CosmosVideoEmbedding
10
11os.environ.setdefault("BOS_ENDPOINT", "http://bj.bcebos.com")
12os.environ.setdefault("BOS_REGION", "bj")
13
14MODEL_PATH = "/opt/aihc/models" # MOCK
15
16if __name__ == "__main__":
17 if os.getenv("DAFT_RUNNER", "native") == "ray":
18 import ray
19
20 ray.init(ignore_reinit_error=True)
21 daft.set_runner_ray()
22 daft.set_execution_config(actor_udf_ready_timeout=6000, min_cpu_per_task=0)
23
24 samples = {
25 "video": ["bos://your-bucket/sample.mp4"], # MOCK
26 }
27 ds = daft.from_pydict(samples)
28 ds = ds.with_column(
29 "embedding",
30 aihc_udf(
31 CosmosVideoEmbedding,
32 construct_args={
33 "video_src_type": "video_url",
34 "model_path": MODEL_PATH,
35 "model_name": "nvidia/Cosmos-Embed1-448p",
36 "num_frames": 8,
37 "normalize": True,
38 },
39 num_cpus=1,
40 num_gpus=1,
41 concurrency=1,
42 batch_size=1,
43 )(col("video")),
44 )
45 ds.show()
评价此篇文章
