Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Difference in embedding values for Alibaba-NLP/gte-Qwen2-1.5B-instruct between vLLM and huggingface methods #11801

Closed
1 task done
venki-lfc opened this issue Jan 7, 2025 · 6 comments · Fixed by #11808
Labels
bug Something isn't working

Comments

@venki-lfc
Copy link

Your current environment

The output of `python collect_env.py`
PyTorch version: 2.5.1+cu124
Is debug build: False
CUDA used to build PyTorch: 12.4
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.4 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: Could not collect
CMake version: Could not collect
Libc version: glibc-2.35

Python version: 3.10.12 (main, Nov  6 2024, 20:22:13) [GCC 11.4.0] (64-bit runtime)
Python platform: Linux-5.15.0-130-generic-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: 12.6.68
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: 
GPU 0: NVIDIA L40S
GPU 1: NVIDIA L40S
GPU 2: NVIDIA L40S
GPU 3: NVIDIA L40S
GPU 4: NVIDIA L40S
GPU 5: NVIDIA L40S
GPU 6: NVIDIA L40S
GPU 7: NVIDIA L40S

Nvidia driver version: 535.183.01
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.9.3.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv.so.9.3.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn.so.9.3.0
/usr/lib/x86_64-linux-gnu/libcudnn_engines_precompiled.so.9.3.0
/usr/lib/x86_64-linux-gnu/libcudnn_engines_runtime_compiled.so.9.3.0
/usr/lib/x86_64-linux-gnu/libcudnn_graph.so.9.3.0
/usr/lib/x86_64-linux-gnu/libcudnn_heuristic.so.9.3.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops.so.9.3.0
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture:                         x86_64
CPU op-mode(s):                       32-bit, 64-bit
Address sizes:                        52 bits physical, 57 bits virtual
Byte Order:                           Little Endian
CPU(s):                               128
On-line CPU(s) list:                  0-127
Vendor ID:                            AuthenticAMD
Model name:                           AMD EPYC 9354 32-Core Processor
CPU family:                           25
Model:                                17
Thread(s) per core:                   2
Core(s) per socket:                   32
Socket(s):                            2
Stepping:                             1
Frequency boost:                      enabled
CPU max MHz:                          3799.0720
CPU min MHz:                          1500.0000
BogoMIPS:                             6500.11
Flags:                                fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 invpcid_single hw_pstate ssbd mba ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local avx512_bf16 clzero irperf xsaveerptr rdpru wbnoinvd amd_ppin cppc arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif v_spec_ctrl avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid overflow_recov succor smca fsrm flush_l1d
Virtualization:                       AMD-V
L1d cache:                            2 MiB (64 instances)
L1i cache:                            2 MiB (64 instances)
L2 cache:                             64 MiB (64 instances)
L3 cache:                             512 MiB (16 instances)
NUMA node(s):                         2
NUMA node0 CPU(s):                    0-31,64-95
NUMA node1 CPU(s):                    32-63,96-127
Vulnerability Gather data sampling:   Not affected
Vulnerability Itlb multihit:          Not affected
Vulnerability L1tf:                   Not affected
Vulnerability Mds:                    Not affected
Vulnerability Meltdown:               Not affected
Vulnerability Mmio stale data:        Not affected
Vulnerability Reg file data sampling: Not affected
Vulnerability Retbleed:               Not affected
Vulnerability Spec rstack overflow:   Mitigation; safe RET
Vulnerability Spec store bypass:      Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:             Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:             Mitigation; Enhanced / Automatic IBRS; IBPB conditional; STIBP always-on; RSB filling; PBRSB-eIBRS Not affected; BHI Not affected
Vulnerability Srbds:                  Not affected
Vulnerability Tsx async abort:        Not affected

Versions of relevant libraries:
[pip3] numpy==1.26.4
[pip3] nvidia-cublas-cu12==12.4.5.8
[pip3] nvidia-cuda-cupti-cu12==12.4.127
[pip3] nvidia-cuda-nvrtc-cu12==12.4.127
[pip3] nvidia-cuda-runtime-cu12==12.4.127
[pip3] nvidia-cudnn-cu12==9.1.0.70
[pip3] nvidia-cufft-cu12==11.2.1.3
[pip3] nvidia-curand-cu12==10.3.5.147
[pip3] nvidia-cusolver-cu12==11.6.1.9
[pip3] nvidia-cusparse-cu12==12.3.1.170
[pip3] nvidia-ml-py==12.560.30
[pip3] nvidia-nccl-cu12==2.21.5
[pip3] nvidia-nvjitlink-cu12==12.4.127
[pip3] nvidia-nvtx-cu12==12.4.127
[pip3] pynvml==11.5.3
[pip3] pyzmq==26.2.0
[pip3] sentence-transformers==3.3.1
[pip3] torch==2.5.1
[pip3] torchvision==0.20.1
[pip3] transformers==4.47.1
[pip3] triton==3.1.0
[conda] Could not collect
ROCM Version: Could not collect
Neuron SDK Version: N/A
vLLM Version: 0.6.6.post1
vLLM Build Flags:
CUDA Archs: Not Set; ROCm: Disabled; Neuron: Disabled
GPU Topology:
GPU0    GPU1    GPU2    GPU3    GPU4    GPU5    GPU6    GPU7    CPU Affinity    NUMA Affinity   GPU NUMA ID
GPU0     X      NODE    NODE    NODE    SYS     SYS     SYS     SYS     0-31,64-95      0               N/A
GPU1    NODE     X      NODE    NODE    SYS     SYS     SYS     SYS     0-31,64-95      0               N/A
GPU2    NODE    NODE     X      NODE    SYS     SYS     SYS     SYS     0-31,64-95      0               N/A
GPU3    NODE    NODE    NODE     X      SYS     SYS     SYS     SYS     0-31,64-95      0               N/A
GPU4    SYS     SYS     SYS     SYS      X      NODE    NODE    NODE    32-63,96-127    1               N/A
GPU5    SYS     SYS     SYS     SYS     NODE     X      NODE    NODE    32-63,96-127    1               N/A
GPU6    SYS     SYS     SYS     SYS     NODE    NODE     X      NODE    32-63,96-127    1               N/A
GPU7    SYS     SYS     SYS     SYS     NODE    NODE    NODE     X      32-63,96-127    1               N/A

Legend:

  X    = Self
  SYS  = Connection traversing PCIe as well as the SMP interconnect between NUMA nodes (e.g., QPI/UPI)
  NODE = Connection traversing PCIe as well as the interconnect between PCIe Host Bridges within a NUMA node
  PHB  = Connection traversing PCIe as well as a PCIe Host Bridge (typically the CPU)
  PXB  = Connection traversing multiple PCIe bridges (without traversing the PCIe Host Bridge)
  PIX  = Connection traversing at most a single PCIe bridge
  NV#  = Connection traversing a bonded set of # NVLinks

NVIDIA_VISIBLE_DEVICES=all
NCCL_P2P_DISABLE=1
NVIDIA_REQUIRE_CUDA=cuda>=12.6 brand=unknown,driver>=470,driver<471 brand=grid,driver>=470,driver<471 brand=tesla,driver>=470,driver<471 brand=nvidia,driver>=470,driver<471 brand=quadro,driver>=470,driver<471 brand=quadrortx,driver>=470,driver<471 brand=nvidiartx,driver>=470,driver<471 brand=vapps,driver>=470,driver<471 brand=vpc,driver>=470,driver<471 brand=vcs,driver>=470,driver<471 brand=vws,driver>=470,driver<471 brand=cloudgaming,driver>=470,driver<471 brand=unknown,driver>=535,driver<536 brand=grid,driver>=535,driver<536 brand=tesla,driver>=535,driver<536 brand=nvidia,driver>=535,driver<536 brand=quadro,driver>=535,driver<536 brand=quadrortx,driver>=535,driver<536 brand=nvidiartx,driver>=535,driver<536 brand=vapps,driver>=535,driver<536 brand=vpc,driver>=535,driver<536 brand=vcs,driver>=535,driver<536 brand=vws,driver>=535,driver<536 brand=cloudgaming,driver>=535,driver<536 brand=unknown,driver>=550,driver<551 brand=grid,driver>=550,driver<551 brand=tesla,driver>=550,driver<551 brand=nvidia,driver>=550,driver<551 brand=quadro,driver>=550,driver<551 brand=quadrortx,driver>=550,driver<551 brand=nvidiartx,driver>=550,driver<551 brand=vapps,driver>=550,driver<551 brand=vpc,driver>=550,driver<551 brand=vcs,driver>=550,driver<551 brand=vws,driver>=550,driver<551 brand=cloudgaming,driver>=550,driver<551
NCCL_VERSION=2.22.3-1
NVIDIA_DRIVER_CAPABILITIES=compute,utility
NVIDIA_PRODUCT_NAME=CUDA
CUDA_VERSION=12.6.1
CUDA_VISIBLE_DEVICES=6,7
CUDA_VISIBLE_DEVICES=6,7
LD_LIBRARY_PATH=/usr/local/lib/python3.10/dist-packages/cv2/../../lib64:/usr/local/nvidia/lib:/usr/local/nvidia/lib64
CUDA_MODULE_LOADING=LAZY

Model Input Dumps

No response

🐛 Describe the bug

When I serve the Alibaba-NLP/gte-Qwen2-1.5B-instruct model using vllm and compare the embedding values with HuggingFace, the resulting similarity scores seem to be very different.

First I serve the model using:
python -m vllm.entrypoints.openai.api_server --dtype auto --tensor-parallel-size 1 --enforce-eager --model gte-Qwen2-1.5B-instruct/snapshots/c6c1b92f4a3e1b92b326ad29dd3c8433457df8dd --gpu-memory-utilization 0.85 --task embed

Then on the client side, after initializing, I do the following:

responses = client.embeddings.create(
    input=[
        "how much protein should a female eat",
        "summit define",
    ],
    model=model,
)

query_embeddings = np.array([data.embedding for data in responses.data])  # QUERY EMBEDDINGS

responses = client.embeddings.create(
    input=[
        "As a general guideline, the CDC's average requirement of protein for women ages 19 to 70 is 46 grams per day. But, as you can see from this chart, you'll need to increase that if you're expecting or training for a marathon. Check out the chart below to see how much protein you should be eating each day.",
        "Definition of summit for English Language Learners. : 1  the highest point of a mountain : the top of a mountain. : 2  the highest level. : 3  a meeting or series of meetings between the leaders of two or more governments.",
    ],
    model=model,
)

doc_embeddings = np.array([data.embedding for data in responses.data])  # DOCUMENT EMBEDDINGS

scores = (query_embeddings @ doc_embeddings.T) * 100
print(scores.tolist())
[[52.59680514979408, 21.655208775528223], [14.08504534593078, 50.74565606361716]]

When I follow the HuggingFace method using Sentence Transformers, it looks like this:

from sentence_transformers import SentenceTransformer

model = SentenceTransformer("gte-qwen2-1.5b-instruct/snapshots/ca1f7d7fda484a8024027bbc1769db97789034ff", trust_remote_code=True)
# In case you want to reduce the maximum length:
#model.max_seq_length = 8192

queries = [
    "how much protein should a female eat",
    "summit define",
]
documents = [
    "As a general guideline, the CDC's average requirement of protein for women ages 19 to 70 is 46 grams per day. But, as you can see from this chart, you'll need to increase that if you're expecting or training for a marathon. Check out the chart below to see how much protein you should be eating each day.",
    "Definition of summit for English Language Learners. : 1  the highest point of a mountain : the top of a mountain. : 2  the highest level. : 3  a meeting or series of meetings between the leaders of two or more governments.",
]

query_embeddings = model.encode(queries, prompt_name="query")
document_embeddings = model.encode(documents)

scores = (query_embeddings @ document_embeddings.T) * 100
print(scores.tolist())
[[78.49689483642578, 17.042871475219727], [14.924504280090332, 75.37963104248047]]

You can clearly see the similarity scores are different. What is the solution to address this problem?
Also notice that the query embeddings have prompt_name="query" enabled during the encoding. How to do the same for vLLM?

Thanks for your feedback in advance! :)

Before submitting a new issue...

  • Make sure you already searched for relevant issues, and asked the chatbot living at the bottom right corner of the documentation page, which can answer lots of frequently asked questions.
@venki-lfc venki-lfc added the bug Something isn't working label Jan 7, 2025
@DarkLight1337
Copy link
Member

Also notice that the query embeddings have prompt_name="query" enabled during the encoding. How to do the same for vLLM?

I'm pretty sure this is the reason for the discrepency, since vLLM doesn't apply prompt template by default. You have to figure out what is the template in Sentence-Transformers and apply that manually before passing the prompt to vLLM.

@venki-lfc
Copy link
Author

venki-lfc commented Jan 7, 2025

I'm pretty sure this is the reason for the discrepency, since vLLM doesn't apply prompt template by default. You have to figure out what is the template in Sentence-Transformers and apply that manually before passing the prompt to vLLM.

Thanks and I agree that it will have an impact on the user query. However I tried to encode the word Hello using two methods (without any additional paramters) and I get [-0.02854686 0.03622064 0.04167501 ... -0.01427057 -0.0341699 -0.01055191] in SentenceTransformers and [ 0.00823212, 0.04229736, 0.00614166, ..., -0.009552 , -0.01431274, 0.03182983] via vLLM

@DarkLight1337
Copy link
Member

I think there is something different in the way you use SentenceTransformers. This example script yields consistent results

import numpy as np
from sentence_transformers import SentenceTransformer
from vllm import LLM


if __name__ == "__main__":
    llm = LLM("Alibaba-NLP/gte-Qwen2-1.5B-instruct", task="embed")

    output, = llm.embed("Hello")
    vllm_embeds = np.array(output.outputs.embedding)
    print("vllm:", vllm_embeds.tolist()[:16])

    model = SentenceTransformer("Alibaba-NLP/gte-qwen2-1.5b-instruct")
    hf_embeds = model.encode("Hello")
    print("hf:", hf_embeds.tolist()[:16])

Output:

vllm: [0.00820159912109375, 0.04241943359375, 0.0061492919921875, 0.0241851806640625, 0.01459503173828125, -0.007015228271484375, -0.05352783203125, -0.01800537109375, -0.03082275390625, -0.010467529296875, 0.01552581787109375, -0.005859375, -0.018310546875, 0.0241241455078125, 0.005596160888671875, -0.00141143798828125]
hf: [0.008164888247847557, 0.04223780706524849, 0.006178292911499739, 0.024099230766296387, 0.014425956644117832, -0.006949191447347403, -0.053592704236507416, -0.01808161474764347, -0.030768735334277153, -0.010400176979601383, 0.015612752176821232, -0.005370304919779301, -0.0182710699737072, 0.024298779666423798, 0.005494194570928812, -0.001257302355952561]

@venki-lfc
Copy link
Author

venki-lfc commented Jan 7, 2025

I think there is something different in the way you use SentenceTransformers. This example script yields consistent results

Thank you for the valuable clarifications 😀
I loaded the model in SentenceTransformers using:
model = SentenceTransformer("gte-qwen2-1.5b-instruct/snapshots/ca1f7d7fda484a8024027bbc1769db97789034ff", trust_remote_code=True)

It seems that removing trust_remote_code=True has solved the issue and I am able to produce same embedding vectors in vLLM and SentenceTransformers.

Very strange though. What is trust_remote_code=True actually doing here?🤔

@DarkLight1337
Copy link
Member

Oh, I just remembered this issue: huggingface/transformers#34882

You should pass the same value of trust_remote_code in both vLLM and HF to get consistent output.

@venki-lfc
Copy link
Author

Thanks for the solution! It is now consistent across both methods. 😀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants