ruri-v3-310m-coreml
Model Summary
This is an unofficial Core ML conversion of cl-nagoya/ruri-v3-310m (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-310m (ModernBERT-Ja, 315M params, hidden=768)
Output: a mean-pooled, L2-normalized sentence embedding (768-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_maskto 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.
Files
| File | Seq. length | Precision | Approx. size |
|---|---|---|---|
ruri-v3-310m_seq128_fp16.mlpackage |
128 | fp16 | 630MB |
ruri-v3-310m_seq256_fp16.mlpackage |
256 | fp16 | 630MB |
ruri-v3-310m_seq512_fp16.mlpackage |
512 | fp16 | 630MB |
ruri-v3-310m_seq128_int8.mlpackage |
128 | int8 (weight-only) | 316MB |
ruri-v3-310m_seq256_int8.mlpackage |
256 | int8 (weight-only) | 316MB |
ruri-v3-310m_seq512_int8.mlpackage |
512 | int8 (weight-only) | 316MB |
For short queries/chunks, use seq128; for longer document chunks, use seq256/512. The int8 variant is recommended for mobile apps.
Verification
- On a real iPhone 17 Pro (iOS 27): both the seq128 fp16 and int8
variants of ruri-v3-310m run on GPU/Neural Engine (
computeUnits = .all). Steady-state inference is about 5ms per sentence (Apple's ownNLContextualEmbeddingis about 9-10ms). The fp16/int8 similarity-score difference is at most 0.0042, with search-result ranking essentially unchanged. - On Mac (coremltools): all files show cosine similarity 0.9998+ against the original PyTorch output.
See REPORT.md for detailed verification results.
Real-world usage
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.
Prefixes (important)
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):
- Empty string: general semantic similarity
トピック:("Topic: "): classification/clustering検索クエリ:("Search query: "): the query side of retrieval検索文書:("Search document: "): the document side of retrieval
Swift usage example
See 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-310m_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, 768] 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).
Python verification example
import coremltools as ct
import numpy as np
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("cl-nagoya/ruri-v3-310m")
model = ct.models.MLModel("ruri-v3-310m_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, 768)
How it was converted (reproduction steps)
Generated with the ruri_coreml_convert conversion tool. Steps:
- Load the ModernBERT-based ruri-v3 with
transformers'AutoModel. - Wrap it in an
nn.Modulethat does mean pooling + L2 normalize. - Trace with
torch.jit.trace(sincetransformers.masking_utils.and_masks/or_masksuseTensor.new_ones/new_zeros, which the Core ML converter doesn't support, these were swapped for atorch.ones/torch.zeros-based implementation before tracing). - Generate the
.mlpackagewithcoremltools.convert(..., convert_to="mlprogram"). - Generate the int8 variant with
coremltools.optimize.coreml.linear_quantize_weights.
License
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.
Citation
@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},
}
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.
- Downloads last month
- 20
Model tree for masahiroid/ruri-v3-310m-coreml
Base model
sbintuitions/modernbert-ja-310m