This is an unofficial Core ML conversion of cl-nagoya/ruri-v3-130m (a general-purpose Japanese text-embedding model), for running directly on iOS/macOS. All credit for the original model goes to its authors (Nagoya University / cl-nagoya).
Base model: cl-nagoya/ruri-v3-130m (ModernBERT-Ja, 132M params, hidden=512)
Output: a mean-pooled, L2-normalized sentence embedding (512-dim). You can use a dot product instead of cosine similarity directly.
The model is split into fixed-length buckets per sequence length (Core ML
has trouble tracing variable-length graphs, so this buckets into a few
practical lengths instead). Pad/truncate input_ids / attention_mask to
the matching bucket length.
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.
| File | Seq. length | Precision | Approx. size |
|---|---|---|---|
ruri-v3-130m_seq128_fp16.mlpackage |
128 | fp16 | 265MB |
ruri-v3-130m_seq256_fp16.mlpackage |
256 | fp16 | 265MB |
ruri-v3-130m_seq512_fp16.mlpackage |
512 | fp16 | 265MB |
ruri-v3-130m_seq128_int8.mlpackage |
128 | int8 (weight-only) | 133MB |
ruri-v3-130m_seq256_int8.mlpackage |
256 | int8 (weight-only) | 133MB |
ruri-v3-130m_seq512_int8.mlpackage |
512 | int8 (weight-only) | 133MB |
For short queries/chunks, use seq128; for longer document chunks, use seq256/512. The int8 variant is recommended for mobile apps.
computeUnits = .all).
Steady-state inference is about 5ms per sentence (Apple's own
NLContextualEmbedding is about 9-10ms). The fp16/int8 similarity-score
difference is at most 0.0042, with search-result ranking essentially
unchanged.See REPORT.md for detailed verification results.
This Core ML conversion is used in the iOS app "道程 -Michinori-" (free, iOS 17.0+), which compares a user's input text against Japanese literature (classical works, etc.) to find the semantically closest passages. The app uses this ruri-v3 model for the initial embedding-based candidate search, and japanese-reranker-xsmall-v2-coreml to rerank the results -- both models can be tried out live in the app.
ruri-v3 uses a "1+3 prefix scheme" -- prepend the following prefix to the text before tokenizing, depending on its role (required to match the original model's behavior):
トピック: ("Topic: "): classification/clustering検索クエリ: ("Search query: "): the query side of retrieval検索文書: ("Search document: "): the document side of retrievalSee MANUAL.md for how to add the downloaded model to an Xcode project, and a complete implementation example.
import CoreML
let configuration = MLModelConfiguration()
configuration.computeUnits = .all
let model = try MLModel(
contentsOf: Bundle.main.url(forResource: "ruri-v3-130m_seq128_int8", withExtension: "mlmodelc")!,
configuration: configuration
)
// You'll need a tokenizer separately -- huggingface/swift-transformers'
// Tokenizers, or an equivalent implementation that can load tokenizer.json,
// is recommended.
let inputIDs: MLMultiArray = ... // shape [1, 128], Int32
let attentionMask: MLMultiArray = ... // shape [1, 128], Int32
let input = try MLDictionaryFeatureProvider(dictionary: [
"input_ids": MLFeatureValue(multiArray: inputIDs),
"attention_mask": MLFeatureValue(multiArray: attentionMask),
])
let output = try model.prediction(from: input)
let embedding = output.featureValue(for: "sentence_embedding")!.multiArrayValue!
// A [1, 512] L2-normalized vector (dtype float16; read it as Swift's Float16).
You must use the original model's tokenizer.json as-is (a ModernBERT-Ja /
PLaMo-family tokenizer).
import coremltools as ct
import numpy as np
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("cl-nagoya/ruri-v3-130m")
model = ct.models.MLModel("ruri-v3-130m_seq128_int8.mlpackage")
text = "検索文書: 瑠璃色(るりいろ)は、紫みを帯びた濃い青のことである。"
encoded = tokenizer([text], return_tensors="np", padding="max_length", truncation=True, max_length=128)
output = model.predict({
"input_ids": encoded["input_ids"].astype(np.int32),
"attention_mask": encoded["attention_mask"].astype(np.int32),
})
print(output["sentence_embedding"].shape) # (1, 512)
Generated with the ruri_coreml_convert conversion tool. Steps:
transformers' AutoModel.nn.Module that does mean pooling + L2 normalize.torch.jit.trace (since transformers.masking_utils.and_masks/
or_masks use Tensor.new_ones/new_zeros, which the Core ML converter
doesn't support, these were swapped for a torch.ones/torch.zeros-based
implementation before tracing)..mlpackage with coremltools.convert(..., convert_to="mlprogram").coremltools.optimize.coreml.linear_quantize_weights.Same as the base model, Apache License 2.0. Copyright for the original model belongs to Nagoya University's conversational AI research group (cl-nagoya). This repository only performs a weight-format conversion.
@misc{ruri2024,
title={{Ruri}: {J}apanese {G}eneral {T}ext {E}mbeddings},
author={Hayato Tsukagoshi and Ryohei Sasano},
year={2024},
eprint={2409.07737},
archivePrefix={arXiv},
primaryClass={cs.CL},
}
SECURITY.md for details).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.
ruri-v3-130m_seq512_fp16.mlpackage/Data/com.apple.CoreML/weights/weight.bin 266 MBruri-v3-130m_seq256_fp16.mlpackage/Data/com.apple.CoreML/weights/weight.bin 265 MBruri-v3-130m_seq128_fp16.mlpackage/Data/com.apple.CoreML/weights/weight.bin 264 MBruri-v3-130m_seq512_int8.mlpackage/Data/com.apple.CoreML/weights/weight.bin 134 MBruri-v3-130m_seq256_int8.mlpackage/Data/com.apple.CoreML/weights/weight.bin 133 MBruri-v3-130m_seq128_int8.mlpackage/Data/com.apple.CoreML/weights/weight.bin 133 MBHave the files? Submit a torrent.
Submit a torrentHave the complete files and permission to share them? Replace the example folder path. The script checks the files, makes a torrent and submits its magnet and checksums. Keep your torrent client seeding. Get your community key from your settings, and read the script before running it.
curl -fsSL https://pirateface.co/package.sh | bash -s -- --repo masahiroid/ruri-v3-130m-coreml ./model-foldercl-nagoya/ruri-v3-130m