metadata
license: apache-2.0
language:
- en
base_model:
- answerdotai/ModernBERT-base
- answerdotai/ModernBERT-large
base_model_relation: quantized
tags:
- fill-mask
- masked-lm
- long-context
- modernbert
ModernBERT-CoreML
This repo contains ModernBERT-base and ModernBERT-large converted to CoreML.
Example Usage
import CoreML
import Tokenizers
let text = "The capital of Ireland is [MASK]."
print("Loading…")
let model = try await ModernBERT_base.load()
let tokenizer = try await AutoTokenizer.from(pretrained: "answerdotai/ModernBERT-base")
let tokens = tokenizer(text)
let inputIDs = MLShapedArray(scalars: tokens.map(Int32.init), shape: [1, tokens.count])
let attentionMask = MLShapedArray<Int32>(scalars: tokens.map { _ in 1 }, shape: [1, tokens.count])
let input = ModernBERT_baseInput(
input_ids: inputIDs,
attention_mask: attentionMask
)
print("Predicting…")
let output = try await model.prediction(input: input)
let logits = output.logitsShapedArray
let maskPosition = tokens.firstIndex(
of: tokenizer.encode(text: "[MASK]", addSpecialTokens: false)[0]
)!
let maskLogits = logits[0, maskPosition]
let maskToken = Int(await MLTensor(maskLogits).argmax().shapedArray(of: Int32.self).scalar!)
let maskText = tokenizer.decode(tokens: [maskToken])
// The capital of Ireland is Dublin.
print(text.replacingOccurrences(of: "[MASK]", with: maskText.trimmingCharacters(in: .whitespaces)))