[PR #13661] mlx: implement L2Norm using native mlx_linalg_norm_l2 #45571

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

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/13661
Author: @iamadalek
Created: 1/9/2026
Status: 🔄 Open

Base: mainHead: l2norm-feature


📝 Commits (1)

  • 93f3782 mlx: implement L2Norm using native mlx_linalg_norm_l2

📊 Changes

1 file changed (+22 additions, -2 deletions)

View changed files

📝 x/ml/backend/mlx/mlx.go (+22 -2)

📄 Description

Summary

Implements the L2Norm method for the MLX backend using the native mlx_linalg_norm_l2 function from MLX-C's linalg.h.

Implementation

Uses the native MLX linalg function as suggested by @dhiltgen:

  1. Compute L2 norm along last axis using mlx_linalg_norm_l2
  2. Add epsilon for numerical stability
  3. Divide input by (norm + eps)

Formula: x / (||x||₂ + eps)

Models Unblocked

This enables embedding models that require L2 normalization:

  • BERT / NomicBERT
  • Qwen3 embeddings
  • Gemma3 embeddings
  • Other models using nn.LayerNorm variants

Testing

  • Build verified on M4 Max (macOS 26.2, Xcode 26.2)
  • cmake --preset MLX
  • cmake --build --preset MLX --parallel
  • go build -tags mlx .
  • go test -tags mlx ./x/ml/backend/mlx/...

Follow-up to MLX backend merge (#13648)


🔄 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/13661 **Author:** [@iamadalek](https://github.com/iamadalek) **Created:** 1/9/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `l2norm-feature` --- ### 📝 Commits (1) - [`93f3782`](https://github.com/ollama/ollama/commit/93f3782ce41259a22f9caaeb23d311d7fb8081f7) mlx: implement L2Norm using native mlx_linalg_norm_l2 ### 📊 Changes **1 file changed** (+22 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `x/ml/backend/mlx/mlx.go` (+22 -2) </details> ### 📄 Description ## Summary Implements the `L2Norm` method for the MLX backend using the native `mlx_linalg_norm_l2` function from MLX-C's linalg.h. ## Implementation Uses the native MLX linalg function as suggested by @dhiltgen: 1. Compute L2 norm along last axis using `mlx_linalg_norm_l2` 2. Add epsilon for numerical stability 3. Divide input by (norm + eps) Formula: `x / (||x||₂ + eps)` ## Models Unblocked This enables embedding models that require L2 normalization: - BERT / NomicBERT - Qwen3 embeddings - Gemma3 embeddings - Other models using `nn.LayerNorm` variants ## Testing - Build verified on M4 Max (macOS 26.2, Xcode 26.2) - `cmake --preset MLX` ✓ - `cmake --build --preset MLX --parallel` ✓ - `go build -tags mlx .` ✓ - `go test -tags mlx ./x/ml/backend/mlx/...` ✓ ## Related Follow-up to MLX backend merge (#13648) --- <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:15:01 -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#45571