![]()
![]()
![]()
![]()
![]()
從64到768維度的多模式嵌入•1B參數聊天
短文•圖像•視頻剪輯•長文檔
ONNX•Coreml•Pytorch
Python•JavaScript•Swift

歡迎來到Uform,這是一個多式聯運庫,它具有高效的用途。 Uform Tiny嵌入模型將幫助您跨各種語言理解和搜索視覺和文本內容。另一方面,Uform小型生成模型不僅支持對話和聊天用例,而且非常適合快速圖像字幕和視覺詢問回答(VQA)。借助緊湊的自定義預訓練的變壓器型號,它可以從您的服務器農場到智能手機運行。
f32到i8下降嵌入而不會失去太多召回。為了準確性和速度基準,請參閱評估頁面。
| 模型 | 參數 | 語言 | 建築學 |
|---|---|---|---|
uform3-image-text-english-large ? | 365 m | 1 | 12層BERT,VIT-L/14 |
uform3-image-text-english-base | 143 m | 1 | 4層BERT,VIT-B/16 |
uform3-image-text-english-small ? | 79 m | 1 | 4層BERT,VIT-S/16 |
uform3-image-text-multilingual-base | 206m | 21 | 12層BERT,VIT-B/16 |
| 模型 | 參數 | 目的 | 建築學 |
|---|---|---|---|
uform-gen2-dpo ? | 1.2 b | 聊天,圖像字幕,VQA | QWEN1.5-0.5B,VIT-H/14 |
uform-gen2-qwen-500m | 1.2 b | 聊天,圖像字幕,VQA | QWEN1.5-0.5B,VIT-H/14 |
uform-gen | 1.5 b | 圖像字幕,VQA | Llama-1.3b,VIT-B/16 |
首先, pip install uform 。然後,加載模型:
from uform import get_model , Modality
processors , models = get_model ( 'unum-cloud/uform3-image-text-english-small' )
model_text = models [ Modality . TEXT_ENCODER ]
model_image = models [ Modality . IMAGE_ENCODER ]
processor_text = processors [ Modality . TEXT_ENCODER ]
processor_image = processors [ Modality . IMAGE_ENCODER ]嵌入圖像:
import requests
from io import BytesIO
from PIL import Image
image_url = 'https://media-cdn.tripadvisor.com/media/photo-s/1b/28/6b/53/lovely-armenia.jpg'
image = Image . open ( BytesIO ( requests . get ( image_url ). content ))
image_data = processor_image ( image )
image_features , image_embedding = model_image . encode ( image_data , return_features = True )嵌入查詢:
text = 'a cityscape bathed in the warm glow of the sun, with varied architecture and a towering, snow-capped mountain rising majestically in the background'
text_data = processor_text ( text )
text_features , text_embedding = model_text . encode ( text_data , return_features = True )有關更多詳細信息,請查看:
生成模型與
from transformers import AutoModel , AutoProcessor
model = AutoModel . from_pretrained ( 'unum-cloud/uform-gen2-dpo' , trust_remote_code = True )
processor = AutoProcessor . from_pretrained ( 'unum-cloud/uform-gen2-dpo' , trust_remote_code = True )
prompt = 'Question or Instruction'
image = Image . open ( 'image.jpg' )
inputs = processor ( text = [ prompt ], images = [ image ], return_tensors = 'pt' )
with torch . inference_mode ():
output = model . generate (
** inputs ,
do_sample = False ,
use_cache = True ,
max_new_tokens = 256 ,
eos_token_id = 151645 ,
pad_token_id = processor . tokenizer . pad_token_id
)
prompt_len = inputs [ 'input_ids' ]. shape [ 1 ]
decoded_text = processor . batch_decode ( output [:, prompt_len :])[ 0 ]有關更多詳細信息,請查看:
根據應用程序,可以將嵌入到較小的數字表示形式下,而不會失去太多召回。在幾乎所有情況下,建議您從f32轉換為f16 ,除非您在沒有半精確支持的情況下運行非常舊的硬件。也可以通過線性縮放切換到i8 ,但在較大的收藏集中,將在數百萬可搜索條目的較大集合中召回。同樣,對於高維嵌入(512或768),一種常見的策略是將它們量化為單位表示形式以進行更快的搜索。
import numpy as np
f32_embedding : np . ndarray = model . encode_text ( text_data , return_features = False )
f16_embedding : np . ndarray = f32_embedding . astype ( np . float16 )
i8_embedding : np . ndarray = ( f32_embedding * 127 ). astype ( np . int8 )
b1_embedding : np . ndarray = np . packbits (( f32_embedding > 0 ). astype ( np . uint8 ))量化的替代方法是使用Matryoshka嵌入,其中嵌入將嵌入切成較小的部分,並以層次結構方式進行搜索。
import numpy as np
large_embedding : np . ndarray = model . encode_text ( text_data , return_features = False )
small_embedding : np . ndarray = large_embedding [:, : 256 ]
tiny_embedding : np . ndarray = large_embedding [:, : 64 ]這兩種方法都由Usearch Vector-Search引擎和SIMSIMD Numerics庫在本地支持。在處理小型集合(最多數百萬個條目)並尋找低延遲的餘弦距離計算時,您可以使用SIMSIMD實現5 x-2500x的性能改善,numpy,scipy和Vanilla Python。
from simsimd import cosine , hamming
distance : float = cosine ( f32_embedding , f32_embedding ) # 32x SciPy performance on Apple M2 CPU
distance : float = cosine ( f16_embedding , f16_embedding ) # 79x SciPy performance on Apple M2 CPU
distance : float = cosine ( i8_embedding , i8_embedding ) # 133x SciPy performance on Apple M2 CPU
distance : float = hamming ( b1_embedding , b1_embedding ) # 17x SciPy performance on Apple M2 CPU同樣,當處理大型收藏(每台服務器多達數十億個條目)並尋找高通量搜索時,您可以使用USEarch對FAISS和其他矢量搜索解決方案實現100倍的性能改善。這裡有幾個例子:
from usearch . index import Index
f32_index = Index ( ndim = 64 , metric = 'cos' , dtype = 'f32' ) # for Matryoshka embeddings
f16_index = Index ( ndim = 64 , metric = 'cos' , dtype = 'f16' ) # for Matryoshka embeddings
i8_index = Index ( ndim = 256 , metric = 'cos' , dtype = 'i8' ) # for quantized embeddings
b1_index = Index ( ndim = 768 , metric = 'hamming' , dtype = 'b1' ) # for binary embeddingsPytorch是一個重度依賴,特別是如果您在邊緣或物聯網設備上運行時。使用Vanilla ONNX運行時,可以顯著減少內存消耗和部署延遲。
$ conda create -n uform_torch python=3.10 -y
$ conda create -n uform_onnx python=3.10 -y
$ conda activate uform_torch && pip install -e " .[torch] " && conda deactivate
$ conda activate uform_onnx && pip install -e " .[onnx] " && conda deactivate
$ du -sh $( conda info --envs | grep ' uform_torch ' | awk ' {print $2} ' )
> 5.2G ~ /conda/envs/uform_torch
$ du -sh $( conda info --envs | grep ' uform_onnx ' | awk ' {print $2} ' )
> 461M ~ /conda/envs/uform_onnx對於模型和運行時,大部分重量都可以進一步降低至100 MB。您可以選擇眾多支持的ONX執行提供商之一,其中包括XNNPACK,CUDA和TENSORRT用於NVIDIA GPU,INTEL上的OpenVino,Windows上的DirectML,AMD上的ROCM,AMD上的ROCM,Apple設備上的Coreml等等。
生成模型可用於命令行中的類似聊天經驗。為此,您可以使用uform-chat CLI工具,該工具可在Uform軟件包中使用。
$ pip install uform
$ uform-chat --model unum-cloud/uform-gen2-dpo --image=zebra.jpg
$ uform-chat --model unum-cloud/uform-gen2-dpo
> --image= " https://bit.ly/3tIVg9M "
> --device= " cuda:0 "
> --fp16