You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Gemma3] NaN embeddings on GPU when batching sequences of mixed length (sliding window attention + all-padding windows) #45491
Serving runtime: pytorch/torchserve-kfs:0.12.0 (Python 3.9, Linux x86_64)
GPU: NVIDIA (CUDA, via KServe / TorchServe on Kubernetes)
CPU inference: not affected
Description
When encoding a batch of sentences of mixed lengths with google/EmbeddingGemma (300M) via SentenceTransformer.encode_query(), short sentences receive an all-NaN embedding vector on GPU. The same inputs encoded individually (batch_size=1) return correct, fully-normalized embeddings.
Trigger condition: the shortest sequence in the batch must be short enough that, when padded to the length of the longest sequence, one or more sliding-window attention windows fall entirely on padding tokens.
In the reported case:
Chunk 0: 142 tokens → padded to 728 tokens → NaN=768/768 in the batch
The model's sliding window size is 512. With 728 − 142 = 586 padding tokens appended, the first attention windows of Chunk 0 are 100% padding, causing softmax([-inf, -inf, …]) = 0/0 = NaN in the GPU kernel — a silent numerical error (no exception raised, no warning).
Actual behavior: Chunk 0 (shortest, ~142 tokens) returns a vector of 768 NaN values when batched with longer sequences. No exception or warning is raised.
Workaround: detect NaN after encoding and re-encode the affected inputs with batch_size=1.
Honestly, this is not a transformers issue itself but a padding side issue. Regardless of the size of the chunk, having only pad input is invalid in itself (i.e. what values would you expect on all padding input --> NaNs, 0s, ...). So this doesn't need a fix by us but you need to force left padding side instead - not sure why right is the default padding side here tbh
Many encoders use right padding, and it seems that https://huggingface.co/google/embeddinggemma-300m is not an exception. Left padding is a bit of a newer development from decoders. I'll try and reproduce this myself locally, but if right-padding with sliding window attention breaks if there's more than window_size padding tokens, then that might reasonably be a bug. I'll try and reproduce this in a bit.
...
KeyError: 'gemma3_text'
During handling of the above exception, another exception occurred:
...
ValueError: The checkpoint you are trying to load has model type `gemma3_text` but Transformers does not recognize this architecture. This could be because of an issue with the checkpoint, or because your version of Transformers is out of date.
Could you share a bit more details? Also, on the latest Transformers/Sentence Transformers, I'm unable to get any NaNs even with inputs of the same shape:
...
KeyError: 'gemma3_text'
During handling of the above exception, another exception occurred:
...
ValueError: The checkpoint you are trying to load has model type `gemma3_text` but Transformers does not recognize this architecture. This could be because of an issue with the checkpoint, or because your version of Transformers is out of date.
Could you share a bit more details? Also, on the latest Transformers/Sentence Transformers, I'm unable to get any NaNs even with inputs of the same shape:
from sentence_transformers import SentenceTransformer, version as st_version
from transformers import version as transformers_version
import numpy as np
embs = model.encode_query(chunks, batch_size=64)
for i, emb in enumerate(embs):
print(f"Chunk {i}: NaN={np.any(np.isnan(emb))}") # False for all
Individual — all correct
for chunk in chunks:
emb = model.encode_query([chunk], batch_size=1)
print(f"NaN={np.any(np.isnan(emb))}") # False for all
* Tom Aarsen
The pip list | grep transformers output showing 4.45.1 is from the base image before model dependencies are installed. At model load time, requirements.txt upgrades transformers to 4.50.0 into an isolated directory, so the actual runtime version is 4.50.0. The model loads fine on 4.50.0 — gemma3_text is already present there. The NaN occurs in its GPU attention kernel when running on that version
Thank you — this is a key clarification. Our config.json does include use_bidirectional_attention: true. If that parameter is ignored on 4.50.0, the model was running with causal attention, which would explain the abnormal behaviour. We will upgrade to transformers ≥ 4.56 and re-test. We may need to revisit this issue entirely after the upgrade — the NaN under bidirectional attention on a correct runtime may or may not reproduce.
Activity
Fix NaN in Gemma3/EmbeddingGemma when batching mixed-length sequences…
Kabir08 commentedon Apr 19, 2026
I believe the root cause is Sliding window attention + dynamic padding in batched processing → all-padding windows → numerical instability in softmax.
I have added pr fixing the same
vasqu commentedon Apr 20, 2026
Honestly, this is not a
transformersissue itself but a padding side issue. Regardless of the size of the chunk, having only pad input is invalid in itself (i.e. what values would you expect on all padding input --> NaNs, 0s, ...). So this doesn't need a fix by us but you need to force left padding side instead - not sure why right is the default padding side here tbhcc @tomaarsen for ST
tomaarsen commentedon Apr 20, 2026
Many encoders use right padding, and it seems that https://huggingface.co/google/embeddinggemma-300m is not an exception. Left padding is a bit of a newer development from decoders. I'll try and reproduce this myself locally, but if right-padding with sliding window attention breaks if there's more than
window_sizepadding tokens, then that might reasonably be a bug. I'll try and reproduce this in a bit.tomaarsen commentedon Apr 20, 2026
@RiccardoTOTI embeddinggemma-300m its architecture (
gemma3_text) was added in https://github.com/huggingface/transformers/releases/tag/v4.56.0-Embedding-Gemma-preview, but you report being on 4.45.1. I get this error when loading with your exact environment:Could you share a bit more details? Also, on the latest Transformers/Sentence Transformers, I'm unable to get any NaNs even with inputs of the same shape:
RiccardoTOTI commentedon Apr 20, 2026
The pip list | grep transformers output showing 4.45.1 is from the base image before model dependencies are installed. At model load time, requirements.txt upgrades transformers to 4.50.0 into an isolated directory, so the actual runtime version is 4.50.0. The model loads fine on 4.50.0 — gemma3_text is already present there. The NaN occurs in its GPU attention kernel when running on that version
tomaarsen commentedon Apr 20, 2026
embeddinggemma-300m requires the aforementioned version also for the inclusion of the
use_bidirectional_attentionparameter: https://huggingface.co/google/embeddinggemma-300m/blob/main/config.json#L57More details in huggingface/sentence-transformers#3725
RiccardoTOTI commentedon Apr 20, 2026
Thank you — this is a key clarification. Our config.json does include use_bidirectional_attention: true. If that parameter is ignored on 4.50.0, the model was running with causal attention, which would explain the abnormal behaviour. We will upgrade to transformers ≥ 4.56 and re-test. We may need to revisit this issue entirely after the upgrade — the NaN under bidirectional attention on a correct runtime may or may not reproduce.
tomaarsen commentedon Apr 20, 2026
Yes, please give it another try with a newer version and let us know! Good luck.