File size: 1,755 Bytes
dbae32f f0540d3 dbae32f 19e58f9 dbae32f 19e58f9 dbae32f 19e58f9 dbae32f 7945f87 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
---
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](https://huggingface.co/answerdotai/ModernBERT-base) and [ModernBERT-large](https://huggingface.co/answerdotai/ModernBERT-large) converted to CoreML.
### Example Usage
```swift
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")
print("Tokenizing…")
let tokens = tokenizer(text)
let inputIDs = MLShapedArray(scalars: tokens.map(Int32.init), shape: [1, tokens.count])
let input = ModernBERT_baseInput(input_ids: inputIDs)
print("Predicting…")
let output = try await model.prediction(input: input)
let logits = output.logitsShapedArray
print("Decoding…")
let maskPosition = tokens.firstIndex(of: tokenizer.convertTokenToId("[MASK]")!)!
let predictedTokenID = await MLTensor(logits[0, maskPosition]).argmax().shapedArray(of: Int32.self).scalar!
let predictedTokenText = tokenizer.decode(tokens: [Int(predictedTokenID)])
print("Result:")
print(text.replacingOccurrences(of: "[MASK]", with: predictedTokenText.trimmingCharacters(in: .whitespaces)))
// The capital of Ireland is Dublin.
```
### Conversion
```
uv run https://hf.co/finnvoorhees/ModernBERT-CoreML/raw/main/convert.py
```
```
usage: convert.py [-h] [--model MODEL] [--quantize]
Convert ModernBERT to CoreML
options:
-h, --help show this help message and exit
--model MODEL Model name
--quantize Linear quantize model
``` |