MaxSim Metal

On Apple Silicon, use_kernels=True in Sentence Transformers currently falls back to plain PyTorch on mps. This package provides a fused Metal MaxSim kernel for that path, with inference and training support.

It scores token embeddings from late interaction models such as ColBERT, is packaged for Hugging Face Kernels, and is intended for use with Sentence Transformers.

Credits

The fused MaxSim algorithm comes from Late Interaction Kernels (LIK) by Aurélien Lac and Tony Wu at H Company. It takes the maximum over document tokens during scoring instead of materializing the full token similarity tensor.

The MaxSim and MaxSimPairwise interfaces and PyTorch reference behavior follow the late-interaction-kernels package used by Sentence Transformers on CUDA, which builds on LIK.

The Metal shaders here are a separate implementation. They do not reuse LIK's Triton kernels or port its existing Metal code. I've also proposed a Metal 4 path upstream in hcompai/late-interaction-kernels#116.

This package uses the Apache 2.0 license, as do LIK and the Sentence Transformers package.

What does it compute?

For each query token, MaxSim finds the document token with the highest dot product, then sums those maxima over the query:

MaxSim⁡(Q,D)=∑i∈valid query tokensmax⁡j∈valid document tokensQi⊤Dj \operatorname{MaxSim}(Q, D) = \sum_{i \in \text{valid query tokens}} \max_{j \in \text{valid document tokens}} Q_i^\top D_j

One simdgroup scores each query and document pair with Metal 4 tensor ops, on Apple GPUs running macOS 26. The [Nq, Nd, Lq, Ld] similarity tensor is never written to memory. Both layers support autograd.

Requirements

  • An Apple Silicon Mac, macOS 26 or newer, and PyTorch 2.11 or newer with the mps backend.
  • No Xcode or Metal toolchain is needed. The shader compiles at runtime with torch.mps.compile_shader.

The kernel probes the GPU's cooperative tensor layout on first use. If the layout or shader compilation is unsupported, scoring falls back to PyTorch. Older macOS and PyTorch versions also use the PyTorch path. See Precision and fallbacks.

Tested on an M5 Max (macOS 27.2.1) with PyTorch 2.15 and 2.11, and on an M2 (macOS 26.6.1) with PyTorch 2.15. PyTorch 2.10 and 2.6 use the PyTorch scoring path.

Quickstart

pip install kernels
import torch
import torch.nn.functional as F
from kernels import get_kernel

kernel = get_kernel("m4zar/maxsim-metal", version=0, trust_remote_code=True)

queries = F.normalize(torch.randn(2, 32, 128, device="mps"), dim=-1).half().requires_grad_()
documents = F.normalize(torch.randn(4, 180, 128, device="mps"), dim=-1).half().requires_grad_()
query_mask = torch.ones(2, 32, dtype=torch.bool, device="mps")
document_mask = torch.ones(4, 180, dtype=torch.bool, device="mps")
document_mask[:, 150:] = False

scores = kernel.layers.MaxSim()(queries, documents, query_mask, document_mask)
paired_scores = kernel.layers.MaxSimPairwise()(queries, documents[:2], query_mask, document_mask[:2])

print(scores.shape)  # torch.Size([2, 4])
print(paired_scores.shape)  # torch.Size([2])
scores.sum().backward()

trust_remote_code=True allows the loader to execute Python code from this kernel repository. The first call also compiles the shader and runs the layout probe.

Sentence Transformers

With a Sentence Transformers version that selects this kernel on mps, set use_kernels=True when loading a MultiVectorEncoder:

from sentence_transformers import MultiVectorEncoder

model = MultiVectorEncoder("lightonai/LateOn", device="mps", use_kernels=True)
queries = model.encode_query(["What is the capital of France?"])
documents = model.encode_document(["Paris is the capital of France.", "Berlin is the capital of Germany."])
scores = model.similarity(queries, documents)

Scoring interface

Layer Query shape Document shape Output shape
MaxSim [Nq, Lq, dim] [Nd, Ld, dim] [Nq, Nd]
MaxSimPairwise [B, Lq, dim] [B, Ld, dim] [B]
  • Inputs must share the embedding dimension, device, and dtype.
  • Masks have shape [batch_size, sequence_length], with True for valid tokens and False for padding. Pass None for either mask to include all tokens.
  • Query masks exclude tokens from the sum. Document masks exclude tokens from the maximum. Documents with no valid tokens receive a score of -1e9.
  • The layers use dot products and do not normalize embeddings internally.
  • Scores are returned in torch.float32.

Precision and fallbacks

Supports float16, bfloat16, and float32 inputs. The accelerated path uses half precision with float32 accumulation, so scores and gradients may differ slightly from full-precision PyTorch. Values outside ±65504 overflow when computed in float16.

float32 and bfloat16 inputs are converted to float16 internally, and gradients are returned in the input dtype.

Scoring falls back to PyTorch for other dtypes or devices, empty inputs, non-boolean query masks, execution inside torch.compile, unsupported GPU layouts or shader compilation, and a backward pass under torch.use_deterministic_algorithms(True). Double backward (create_graph=True) is not supported.

Support

Report issues in the Hub discussions. Include your chip, macOS and PyTorch versions, input shapes and dtypes, and a minimal reproducer.

Citation

Please credit the original kernel authors using the upstream citation:

@software{late_interaction_kernels_2026,
  author  = {Lac, Aurélien and Wu, Tony},
  title   = {{late-interaction-kernels}: Fused Triton kernels for late-interaction scoring},
  year    = {2026},
  url     = {https://github.com/hcompai/late-interaction-kernels},
}
Downloads last month
-
kernels
metal
maxsim
colbert
late-interaction
sentence-transformers
apache-2.0
arxiv: 2004.12832
Supported hardwares new
Metal
Apple Silicon Ultra
Apple M5 Ultra
96GB
Apple Silicon Max
Apple M5 Max
36GB
Apple Silicon Pro
Apple M5 Pro
24GB
Apple Silicon
Apple M6
16GB
Apple Silicon
Apple MacBook Neo
8GB
Apple Silicon Ultra
Apple M3 Ultra
96GB
Apple Silicon
Apple M5
16GB
Apple Silicon Max
Apple M4 Max
36GB
Apple Silicon Pro
Apple M4 Pro
24GB
Apple Silicon
Apple M4
16GB
Apple Silicon Ultra
Apple M2 Ultra
64GB
Apple Silicon Max
Apple M3 Max
36GB
Apple Silicon Max
Apple M2 Max
32GB
Apple Silicon Pro
Apple M3 Pro
18GB
Apple Silicon Pro
Apple M2 Pro
16GB
Apple Silicon
Apple M3
8GB
Apple Silicon Ultra
Apple M1 Ultra
16GB
Apple Silicon
Apple M2
8GB
Apple Silicon Max
Apple M1 Max
16GB
Apple Silicon Pro
Apple M1 Pro
16GB
Apple Silicon
Apple M1
8GB
Kernel Builder
32b3dc8