This repository was archived by the owner on Mar 30, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Mar 30, 2026. It is now read-only.
bfloat16 is slower than float32 on Intel Xeon Platinum 8481C CPU #637
Copy link
Copy link
Open
Labels
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
Activity
jgong5 commentedon May 27, 2024
It is not expected that the "Linear" ops are slower but there could be something else going on in the model which slows things down. Is it possible to share the PyTorch profiles for fp32 and bf16 runs?
netw0rkf10w commentedon May 27, 2024
@jgong5 Thanks for your reply.
I have realized that bfloat16 is faster for
batchsize=1(tested with the first sentence in the above example).The profiling results (together with the full output logs just in case you need them) are the following for both cases:
Batch size 1:
Batch size 5:
jgong5 commentedon May 27, 2024
cc @zhuhaozhe
@netw0rkf10w Do you mind share the oneDNN verbose log with
ONEDNN_VERBOSE=1?netw0rkf10w commentedon May 27, 2024
@jgong5 Yes the full output can be found in the attached file.
ipex_bfloat16_bug.txt
jgong5 commentedon May 27, 2024
Weird, it seems kernels were running with avx512-bf16, not amx. @zhuhaozhe can you double check?
zhuhaozhe commentedon May 28, 2024
Sure, I will check it.
zhuhaozhe commentedon May 28, 2024
Hi, @jgong5.
From the Flags provided by reporter, there is no AMX included.
while on the SPR I can access
there are more flags about amx
amx_bf16 avx512_fp16 amx_tile amx_int8But 8481C should be Sapphire Rapids. Do you know where we can get such a hardware and try to re-produce on it?
netw0rkf10w commentedon May 28, 2024
@zhuhaozhe Thanks for your investigation. It's a
c3-standard-4VM instance from Google Cloud. Please let me know if you need further information.zhuhaozhe commentedon May 28, 2024
Thanks for clarification.
Hi, @jingxu10, do you know where we can access such a VM instance?
jingxu10 commentedon May 28, 2024
@zhuhaozhe SPR instances in CSP might not have AMX onboard. 8481C seems to be used in GCP.
https://cloud.google.com/compute/docs/cpu-platforms
WilliamTambellini commentedon May 28, 2024
Your linux kernel is also too old for amx detection
zhuhaozhe commentedon May 29, 2024
Thanks for the document @jingxu10.
This is correct, the document mentioned https://cloud.google.com/compute/docs/cpu-platforms#intel-amx we need linux kernel > 5.16 to enable amx.
@netw0rkf10w May you help to check your kernel version by
uname -r?From here looks like it is 5.10
netw0rkf10w commentedon May 29, 2024
@WilliamTambellini Good catch! Thanks!
@zhuhaozhe It's indeed 5.10. I'll upgrade the OS and get.
netw0rkf10w commentedon Jun 12, 2024
I confirm that after upgrading the Linux kernel to
6.1.0, I obtained improved performance:@zhuhaozhe Is the 2x improvement is as expected or could it be improved further? Thanks!
zhuhaozhe commentedon Jun 13, 2024
@netw0rkf10w, 2.6x improvement looks good to me, can you share profiling results as well?
netw0rkf10w commentedon Jun 13, 2024
@zhuhaozhe Yes, the full outputs are below (the first block is without ipex).
By the way, could you please tell me if the
WARNING - calling in ipex numpy which is not share memory with torch tensor for bfloat16 input.is normal? Thanks a lot!zhuhaozhe commentedon Jun 14, 2024
Thanks for your input, @netw0rkf10w.
Looks like your two profiling table are not apple to apple so I can not see the actually speed up.
Usually we expected
gemm,( likebmm,matmul,linearin your profiling benefits ) fromIntel AMX. And we expected memory ops benefits from lower memory footprint (16 bit for BF16 and 32 bit for FP32). https://www.intel.com/content/www/us/en/developer/articles/technical/accelerate-pytorch-training-inference-on-amx.html.The warning msg
intel-extension-for-pytorch/intel_extension_for_pytorch/nn/functional/_tensor_method.py
Line 6 in 8f40e45
t.numpy()to convertPytorch Tensor t (with dtype=torch.bfloat16)to a numpy tensor and since numpy dose not support bf16, we convert it to float32 first. This is a cost for using BF16, but we need more detail info to know whether it is a very large cost.netw0rkf10w commentedon Jun 18, 2024
@zhuhaozhe Thanks a lot for your reply!
Could you please tell me what details do you need to have an accurate assessment? As you can see from the code, the difference between the two version is just
model = ipex.optimize(model, dtype=torch.bfloat16)(andtorch.cpu.amp.autocast()), so it's not clear to me why it's not an apple-to-apple comparison.By the way, I would like to ask another question if you don't mind. I would like to serve this model with OpenVINO Model Server. However, the latter only supports model formats that don't seem to support bfloat16. Could you please tell me how to serve a model optimized with IPEX? Thank you very much in advance for your help!
zhuhaozhe commentedon Jun 25, 2024
Hi, @netw0rkf10w.
When we compare bf16/fp32 performance from profiling, we usually expect the num of calls should be the same. Then we can compare the "CPU time avg" to see the speed up. However, for your workload, it may have dynamic status so the number of calls are different. Then we actually do not have enough information to tell the exactly speed up here.
And for
OpenVino Model Server, I have not used it. Hi, @jingxu10. Do you have more infos here?