ruri-v3-130m-coreai
Model Summary
This is an unofficial Core AI conversion of cl-nagoya/ruri-v3-130m (a general-purpose Japanese text-embedding model), Apple's new on-device AI runtime (iOS/macOS 27+, the successor to Core ML). All credit for the original model goes to its authors (Nagoya University / cl-nagoya).
- Live demo: Ruri Atlas — explore Japanese & English sentence embeddings as a 3-D map in your browser (WebGPU), switch embedding models, toggle the reranker, and see where a prompt-injection lands. Nothing is sent to a server.
Validated on both Neural Engine and GPU
This has been rewritten from scratch for Neural Engine. Simply converting
the existing model as-is does not load on the Neural Engine in standard
PyTorch matmul layout -- it requires rewriting to Apple's BC1S
(Batch, Channel, 1, Sequence) layout, Conv2d-based projections, and
explicit per-head attention. This reimplementation faithfully reproduces
ModernBERT-Ja's specific architecture (19 layers, full/sliding-window
attention alternating every 3rd layer, a different RoPE theta per attention
flavor, and a bidirectional sliding-window mask with radius 65).
The bundled ruri_ane.py is needed to understand or reproduce this
conversion in Python (the .aimodel itself can be embedded directly into a
Swift app without it).
Usage (Python, coreai.runtime)
# /// script
# requires-python = ">=3.11"
# dependencies = ["coreai-core==1.0.0b3", "transformers", "torch", "sentencepiece", "protobuf"]
# ///
import asyncio
import numpy as np
import torch
import transformers
from coreai.runtime import AIModel, ComputeUnitKind, NDArray, SpecializationOptions
async def main():
tokenizer = transformers.AutoTokenizer.from_pretrained("cl-nagoya/ruri-v3-130m")
text = "検索文書: 瑠璃色(るりいろ)は、紫みを帯びた濃い青のことである。"
enc = tokenizer(text, return_tensors="pt", padding="max_length", truncation=True, max_length=128)
# ComputeUnitKind.neural_engine() also available
opts = SpecializationOptions.from_preferred_compute_unit_kind(ComputeUnitKind.gpu())
model = await AIModel.load("ruri-v3-130m_float16_seq128.aimodel", specialization_options=opts)
# For longer text, use ruri-v3-130m-ane_float16_seq256.aimodel (+ max_length=256) or seq512 (+ max_length=512)
fn = model.load_function(model.function_names[0])
out = await fn(inputs={
"input_ids": NDArray(enc["input_ids"].to(torch.int32).numpy()),
"attention_mask": NDArray(enc["attention_mask"].to(torch.int32).numpy()),
})
embedding = out["sentence_embedding"].numpy()[0] # (512,) L2-normalized
print(embedding[:5])
asyncio.run(main())
Run with uv run test.py (uv builds a throwaway environment for this
script automatically, from the inline # /// script metadata above).
Three fixed sequence lengths are bundled: 128 tokens
(ruri-v3-130m_float16_seq128.aimodel), 256 tokens
(ruri-v3-130m-ane_float16_seq256.aimodel) and 512 tokens
(ruri-v3-130m-ane_float16_seq512.aimodel) for longer text. Pass a
different seq_len to RuriAneModel(...) in ruri_ane.py to re-export
other buckets.
Prefixes (important)
ruri-v3 uses a "1+3 prefix scheme" -- prepend the appropriate prefix before tokenizing:
- Empty string: general semantic similarity
トピック:("Topic: "): classification/clustering検索クエリ:("Search query: "): the query side of retrieval検索文書:("Search document: "): the document side of retrieval
Accuracy
Compared against the PyTorch fp32 reference (a real Japanese sentence, padded to 24/128 real tokens):
| Target | seq128 | seq256 | seq512 |
|---|---|---|---|
| GPU specialization | 1.0001926 | 1.0002351 | 1.0001947 |
| Neural Engine specialization | 1.0002038 | 1.0002038 | 1.0002571 |
During conversion, Neural Engine execution specifically showed degraded
accuracy (cosine 0.954) due to a subtle bug: nn.Parameter/nn.Conv2d
default to fp32, and .copy_() during weight loading only overwrites
values, not dtype -- fixed by explicitly casting the whole module to fp16
(model.half()) before loading weights. See the code comments, or Pattern
11 of mlx-coreml-conversion-toolkit
for the full writeup.
Specs
| Item | Value |
|---|---|
| Base model | cl-nagoya/ruri-v3-130m (ModernBERT-Ja, 132M params) |
| Precision | float16 |
| Input | fixed 128 / 256 / 512 tokens |
| Framework | Core AI (coreai-torch, Neural-Engine-targeted BC1S implementation in ruri_ane.py) |
| Compute units | GPU, Neural Engine (both validated) |
Notes
- This is a community conversion, not an official release from the Ruri / cl-nagoya team.
- Security audit uses model-audit-lite
(see
SECURITY.mdfor details).
Security
Audited against its upstream with model-audit-lite: weight format, bundled code, and a machine-readable lineage (ML-BOM). Details, checksums and how to reproduce: SECURITY.md.