[PR #15621] fix: nil-guard optional embedding components + use exact GELU for BERT #46484

Open
opened 2026-04-25 01:53:59 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/15621
Author: @CrispStrobe
Created: 4/16/2026
Status: 🔄 Open

Base: mainHead: fix/embedding-nil-guards-gelu


📝 Commits (1)

  • cecdb33 fix: nil-guard optional embedding components + use exact GELU for BERT

📊 Changes

3 files changed (+11 additions, -4 deletions)

View changed files

📝 model/models/bert/embed.go (+2 -1)
📝 model/models/gemma3/embed.go (+3 -1)
📝 model/models/qwen3/model.go (+6 -2)

📄 Description

Summary

Three small, backwards-compatible fixes for embedding models:

  1. Qwen3 QK-norm nil-guard (model/models/qwen3/model.go): Qwen3 attention unconditionally calls QueryNorm.Forward() and KeyNorm.Forward(), causing nil pointer panic for models that don't have QK-norm tensors (e.g. Jina v5 Nano and other Qwen-based models without QK-norm).

  2. Gemma3 Dense nil-guard (model/models/gemma3/embed.go): Gemma3 embed model unconditionally calls dense.Forward(), causing nil pointer panic for models without Dense projection layers (e.g. Harrier-270M).

  3. BERT GELU_ERF (model/models/bert/embed.go): BERT was trained with exact GELU (erf-based), but the implementation uses GELU() which is the tanh approximation. Switching to GELU_ERF() improves cosine similarity vs HuggingFace from 0.996 to 1.000.

Test plan

  • Verified all-MiniLM-L6-v2 Q8_0 embeddings match HuggingFace at cos=0.9998
  • Verified gte-small, arctic-embed-xs match at cos>0.999
  • Jina v5 Nano (Qwen3 without QK-norm) loads and produces embeddings
  • Harrier-270M (Gemma3 without Dense) loads and produces embeddings

Related issue: #15620

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ollama/ollama/pull/15621 **Author:** [@CrispStrobe](https://github.com/CrispStrobe) **Created:** 4/16/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/embedding-nil-guards-gelu` --- ### 📝 Commits (1) - [`cecdb33`](https://github.com/ollama/ollama/commit/cecdb33dc3e98901c6cd143d5052ba4c2ed4ab70) fix: nil-guard optional embedding components + use exact GELU for BERT ### 📊 Changes **3 files changed** (+11 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `model/models/bert/embed.go` (+2 -1) 📝 `model/models/gemma3/embed.go` (+3 -1) 📝 `model/models/qwen3/model.go` (+6 -2) </details> ### 📄 Description ## Summary Three small, backwards-compatible fixes for embedding models: 1. **Qwen3 QK-norm nil-guard** (`model/models/qwen3/model.go`): Qwen3 attention unconditionally calls `QueryNorm.Forward()` and `KeyNorm.Forward()`, causing nil pointer panic for models that don't have QK-norm tensors (e.g. Jina v5 Nano and other Qwen-based models without QK-norm). 2. **Gemma3 Dense nil-guard** (`model/models/gemma3/embed.go`): Gemma3 embed model unconditionally calls `dense.Forward()`, causing nil pointer panic for models without Dense projection layers (e.g. Harrier-270M). 3. **BERT GELU_ERF** (`model/models/bert/embed.go`): BERT was trained with exact GELU (erf-based), but the implementation uses `GELU()` which is the tanh approximation. Switching to `GELU_ERF()` improves cosine similarity vs HuggingFace from 0.996 to 1.000. ## Test plan - [x] Verified all-MiniLM-L6-v2 Q8_0 embeddings match HuggingFace at cos=0.9998 - [x] Verified gte-small, arctic-embed-xs match at cos>0.999 - [x] Jina v5 Nano (Qwen3 without QK-norm) loads and produces embeddings - [x] Harrier-270M (Gemma3 without Dense) loads and produces embeddings Related issue: #15620 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-25 01:53:59 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#46484