Open
Description
Describe the bug
Running the following benchmark code:
from time import time
import numpy as np
import torch
from sentence_transformers import SentenceTransformer
import intel_extension_for_pytorch as ipex
def get_embed_function(model_name):
model = SentenceTransformer(model_name)
model.eval()
def embed_function(sentences):
with torch.inference_mode():
x = model.encode(sentences)
return x
return embed_function
def get_embed_function_optimized(model_name):
model = SentenceTransformer(model_name)
model.eval()
model = ipex.optimize(model, dtype=torch.bfloat16)
def embed_function(sentences):
with torch.inference_mode(), torch.cpu.amp.autocast():
x = model.encode(sentences)
return x
return embed_function
@torch.inference_mode()
def commpute_embeddings(func, sentences, warmup=True, runs=1):
latency_warmup = None
if warmup:
start = time()
func(sentences)
latency_warmup = time() - start
latency = 0
for _ in range(runs):
start = time()
embeddings = func(sentences)
latency += time() - start
return embeddings, latency / runs, latency_warmup
def benchmark():
sentences = ["Un avion est en train de décoller.",
"Un homme joue d'une grande flûte.",
"Un homme étale du fromage râpé sur une pizza.",
"Une personne jette un chat au plafond.",
"Une personne est en train de plier un morceau de papier.",
]
runs = 5
# model_name = "all-MiniLM-L6-v2"
model_name = "dangvantuan/sentence-camembert-large"
e_sbert, t, tw = commpute_embeddings(get_embed_function(model_name), sentences, warmup=True, runs=runs)
print(f'sbert took: {t}, warmup: {tw}')
e_sbert_opt, t, tw = commpute_embeddings(get_embed_function_optimized(model_name), sentences, warmup=True, runs=runs)
print(f'optimized sbert took: {t}, warmup: {tw}')
print(f'diff = {np.linalg.norm(np.array(e_sbert) - np.array(e_sbert_opt), axis=1)}')
if __name__ == "__main__":
benchmark()
I obtained (warnings excluded):
sbert took: 0.27114005088806153, warmup: 0.275803804397583
optimized sbert took: 0.2937626838684082, warmup: 0.2977313995361328
diff = [0.0784898 0.09684175 0.10466592 0.10803085 0.0929962 ]
Is this a bug or did I do something wrong?
The full output is below for your information:
2024-05-25 13:54:27,311 - SentenceTransformer.py - sentence_transformers.SentenceTransformer - WARNING - No sentence-transformers model found with name dangvantuan/sentence-camembert-large. Creating a new one with MEAN pooling.
/home/all/miniconda3/envs/env2/lib/python3.10/site-packages/huggingface_hub/file_download.py:1132: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.
warnings.warn(
sbert took: 0.27114005088806153, warmup: 0.275803804397583
2024-05-25 13:54:29,823 - SentenceTransformer.py - sentence_transformers.SentenceTransformer - WARNING - No sentence-transformers model found with name dangvantuan/sentence-camembert-large. Creating a new one with MEAN pooling.
2024-05-25 13:54:31,237 - _logger.py - IPEX - WARNING - calling in ipex numpy which is not share memory with torch tensor for bfloat16 input.
2024-05-25 13:54:31,237 - _logger.py - IPEX - WARNING - calling in ipex numpy which is not share memory with torch tensor for bfloat16 input.
2024-05-25 13:54:31,237 - _logger.py - IPEX - WARNING - calling in ipex numpy which is not share memory with torch tensor for bfloat16 input.
2024-05-25 13:54:31,237 - _logger.py - IPEX - WARNING - calling in ipex numpy which is not share memory with torch tensor for bfloat16 input.
2024-05-25 13:54:31,237 - _logger.py - IPEX - WARNING - calling in ipex numpy which is not share memory with torch tensor for bfloat16 input.
2024-05-25 13:54:31,531 - _logger.py - IPEX - WARNING - calling in ipex numpy which is not share memory with torch tensor for bfloat16 input.
2024-05-25 13:54:31,531 - _logger.py - IPEX - WARNING - calling in ipex numpy which is not share memory with torch tensor for bfloat16 input.
2024-05-25 13:54:31,531 - _logger.py - IPEX - WARNING - calling in ipex numpy which is not share memory with torch tensor for bfloat16 input.
2024-05-25 13:54:31,531 - _logger.py - IPEX - WARNING - calling in ipex numpy which is not share memory with torch tensor for bfloat16 input.
2024-05-25 13:54:31,531 - _logger.py - IPEX - WARNING - calling in ipex numpy which is not share memory with torch tensor for bfloat16 input.
2024-05-25 13:54:31,824 - _logger.py - IPEX - WARNING - calling in ipex numpy which is not share memory with torch tensor for bfloat16 input.
2024-05-25 13:54:31,825 - _logger.py - IPEX - WARNING - calling in ipex numpy which is not share memory with torch tensor for bfloat16 input.
2024-05-25 13:54:31,825 - _logger.py - IPEX - WARNING - calling in ipex numpy which is not share memory with torch tensor for bfloat16 input.
2024-05-25 13:54:31,825 - _logger.py - IPEX - WARNING - calling in ipex numpy which is not share memory with torch tensor for bfloat16 input.
2024-05-25 13:54:31,825 - _logger.py - IPEX - WARNING - calling in ipex numpy which is not share memory with torch tensor for bfloat16 input.
2024-05-25 13:54:32,118 - _logger.py - IPEX - WARNING - calling in ipex numpy which is not share memory with torch tensor for bfloat16 input.
2024-05-25 13:54:32,118 - _logger.py - IPEX - WARNING - calling in ipex numpy which is not share memory with torch tensor for bfloat16 input.
2024-05-25 13:54:32,118 - _logger.py - IPEX - WARNING - calling in ipex numpy which is not share memory with torch tensor for bfloat16 input.
2024-05-25 13:54:32,118 - _logger.py - IPEX - WARNING - calling in ipex numpy which is not share memory with torch tensor for bfloat16 input.
2024-05-25 13:54:32,118 - _logger.py - IPEX - WARNING - calling in ipex numpy which is not share memory with torch tensor for bfloat16 input.
2024-05-25 13:54:32,412 - _logger.py - IPEX - WARNING - calling in ipex numpy which is not share memory with torch tensor for bfloat16 input.
2024-05-25 13:54:32,412 - _logger.py - IPEX - WARNING - calling in ipex numpy which is not share memory with torch tensor for bfloat16 input.
2024-05-25 13:54:32,412 - _logger.py - IPEX - WARNING - calling in ipex numpy which is not share memory with torch tensor for bfloat16 input.
2024-05-25 13:54:32,412 - _logger.py - IPEX - WARNING - calling in ipex numpy which is not share memory with torch tensor for bfloat16 input.
2024-05-25 13:54:32,412 - _logger.py - IPEX - WARNING - calling in ipex numpy which is not share memory with torch tensor for bfloat16 input.
2024-05-25 13:54:32,706 - _logger.py - IPEX - WARNING - calling in ipex numpy which is not share memory with torch tensor for bfloat16 input.
2024-05-25 13:54:32,706 - _logger.py - IPEX - WARNING - calling in ipex numpy which is not share memory with torch tensor for bfloat16 input.
2024-05-25 13:54:32,706 - _logger.py - IPEX - WARNING - calling in ipex numpy which is not share memory with torch tensor for bfloat16 input.
2024-05-25 13:54:32,706 - _logger.py - IPEX - WARNING - calling in ipex numpy which is not share memory with torch tensor for bfloat16 input.
2024-05-25 13:54:32,706 - _logger.py - IPEX - WARNING - calling in ipex numpy which is not share memory with torch tensor for bfloat16 input.
optimized sbert took: 0.2937626838684082, warmup: 0.2977313995361328
diff = [0.0784898 0.09684175 0.10466592 0.10803085 0.0929962 ]
Versions
Collecting environment information...
PyTorch version: 2.3.0+cpu
PyTorch CXX11 ABI: No
IPEX version: 2.3.0+cpu
IPEX commit: 3ac92c8
Build type: Release
OS: Debian GNU/Linux 11 (bullseye) (x86_64)
GCC version: N/A
Clang version: N/A
IGC version: N/A
CMake version: N/A
Libc version: glibc-2.31
Python version: 3.10.13 (main, Sep 11 2023, 13:44:35) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.10.0-29-cloud-amd64-x86_64-with-glibc2.31
Is XPU available: False
DPCPP runtime version: N/A
MKL version: N/A
GPU models and configuration:
Intel OpenCL ICD version: N/A
Level Zero version: N/A
CPU:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
Address sizes: 52 bits physical, 57 bits virtual
CPU(s): 4
On-line CPU(s) list: 0-3
Thread(s) per core: 2
Core(s) per socket: 2
Socket(s): 1
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 143
Model name: Intel(R) Xeon(R) Platinum 8481C CPU @ 2.70GHz
Stepping: 8
CPU MHz: 2699.998
BogoMIPS: 5399.99
Hypervisor vendor: KVM
Virtualization type: full
L1d cache: 96 KiB
L1i cache: 64 KiB
L2 cache: 4 MiB
L3 cache: 105 MiB
NUMA node0 CPU(s): 0-3
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: Not affected
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, RSB filling, PBRSB-eIBRS SW sequence
Vulnerability Srbds: Not affected
Vulnerability Tsx async abort: Not affected
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc cpuid tsc_known_freq pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rtm avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves avx512_bf16 arat avx512vbmi umip avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq rdpid cldemote movdiri movdir64b fsrm md_clear serialize arch_capabilities
Versions of relevant libraries:
[pip3] intel-extension-for-pytorch==2.3.0
[pip3] mypy==1.7.0
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.26.2
[pip3] torch==2.3.0+cpu
[pip3] torchaudio==2.3.0+cpu
[pip3] torchlibrosa==0.1.0
[pip3] torchvision==0.18.0+cpu
[conda] intel-extension-for-pytorch 2.3.0 pypi_0 pypi
[conda] numpy 1.26.2 pypi_0 pypi
[conda] torch 2.3.0+cpu pypi_0 pypi
[conda] torchaudio 2.3.0+cpu pypi_0 pypi
[conda] torchlibrosa 0.1.0 pypi_0 pypi
[conda] torchvision 0.16.2 pypi_0 pypi