finnvoorhees commited on
Commit
7945f87
1 Parent(s): 19e58f9

Add conversion script

Browse files
ModernBERT-base-4bit.mlpackage/Data/com.apple.CoreML/model.mlmodel CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:9a9d1c57cce5d2cdb960bbeb3cfd97443a0752e0542ab958eb636c6a63a87774
3
- size 279063
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b0e5498f10d54a0351e27f59884ece8901418a639c1545f130acfd7ed9de1a6e
3
+ size 278896
ModernBERT-base-4bit.mlpackage/Manifest.json CHANGED
@@ -1,18 +1,18 @@
1
  {
2
  "fileFormatVersion": "1.0.0",
3
  "itemInfoEntries": {
4
- "E15EBF2F-BF39-4526-8AB8-51E73AFEC7FD": {
5
  "author": "com.apple.CoreML",
6
  "description": "CoreML Model Specification",
7
  "name": "model.mlmodel",
8
  "path": "com.apple.CoreML/model.mlmodel"
9
  },
10
- "FD5E8367-55E6-413C-8437-713B00D64144": {
11
  "author": "com.apple.CoreML",
12
  "description": "CoreML Model Weights",
13
  "name": "weights",
14
  "path": "com.apple.CoreML/weights"
15
  }
16
  },
17
- "rootModelIdentifier": "E15EBF2F-BF39-4526-8AB8-51E73AFEC7FD"
18
  }
 
1
  {
2
  "fileFormatVersion": "1.0.0",
3
  "itemInfoEntries": {
4
+ "5679C35F-3674-42B5-A1EB-CF36A3309400": {
5
  "author": "com.apple.CoreML",
6
  "description": "CoreML Model Specification",
7
  "name": "model.mlmodel",
8
  "path": "com.apple.CoreML/model.mlmodel"
9
  },
10
+ "F51070FD-D894-4A2E-BEBB-774355701FB7": {
11
  "author": "com.apple.CoreML",
12
  "description": "CoreML Model Weights",
13
  "name": "weights",
14
  "path": "com.apple.CoreML/weights"
15
  }
16
  },
17
+ "rootModelIdentifier": "5679C35F-3674-42B5-A1EB-CF36A3309400"
18
  }
ModernBERT-base.mlpackage/Data/com.apple.CoreML/model.mlmodel CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:67cc80bf6060a2d08059fe98c081fc9ebc34b074252143ca4dfa03d3e4f277df
3
- size 267080
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:117a44931a590725359233dcf82eb1df1a2ce609c39da433cc0c12c613f33ce0
3
+ size 266913
ModernBERT-base.mlpackage/Manifest.json CHANGED
@@ -1,18 +1,18 @@
1
  {
2
  "fileFormatVersion": "1.0.0",
3
  "itemInfoEntries": {
4
- "A373F851-2CE5-43A4-8511-ECFD46CE8A03": {
5
  "author": "com.apple.CoreML",
6
  "description": "CoreML Model Specification",
7
  "name": "model.mlmodel",
8
  "path": "com.apple.CoreML/model.mlmodel"
9
  },
10
- "C8A72ADC-A8C6-48A6-8B70-5F91F4815174": {
11
  "author": "com.apple.CoreML",
12
  "description": "CoreML Model Weights",
13
  "name": "weights",
14
  "path": "com.apple.CoreML/weights"
15
  }
16
  },
17
- "rootModelIdentifier": "A373F851-2CE5-43A4-8511-ECFD46CE8A03"
18
  }
 
1
  {
2
  "fileFormatVersion": "1.0.0",
3
  "itemInfoEntries": {
4
+ "273F953B-8A06-40EB-96D9-D2F027393D23": {
5
  "author": "com.apple.CoreML",
6
  "description": "CoreML Model Specification",
7
  "name": "model.mlmodel",
8
  "path": "com.apple.CoreML/model.mlmodel"
9
  },
10
+ "D92B9527-3316-412B-B7A2-8B412B463912": {
11
  "author": "com.apple.CoreML",
12
  "description": "CoreML Model Weights",
13
  "name": "weights",
14
  "path": "com.apple.CoreML/weights"
15
  }
16
  },
17
+ "rootModelIdentifier": "273F953B-8A06-40EB-96D9-D2F027393D23"
18
  }
ModernBERT-large-4bit.mlpackage/Data/com.apple.CoreML/model.mlmodel CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:dbe210fd61ab3ced4e7041c483472e46a3383d4e44849a6d5cdba2c4eea3614b
3
- size 350974
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b4907ed377d3c6808a53c2287cad2298b15d25d0c66de2ca49ae81cd3e7e562c
3
+ size 350807
ModernBERT-large-4bit.mlpackage/Manifest.json CHANGED
@@ -1,18 +1,18 @@
1
  {
2
  "fileFormatVersion": "1.0.0",
3
  "itemInfoEntries": {
4
- "5C57D828-673E-436E-ABD8-F8FAF5D3A45E": {
5
- "author": "com.apple.CoreML",
6
- "description": "CoreML Model Weights",
7
- "name": "weights",
8
- "path": "com.apple.CoreML/weights"
9
- },
10
- "BDC69D2B-C6B6-4E19-A9C2-B2333CA42723": {
11
  "author": "com.apple.CoreML",
12
  "description": "CoreML Model Specification",
13
  "name": "model.mlmodel",
14
  "path": "com.apple.CoreML/model.mlmodel"
 
 
 
 
 
 
15
  }
16
  },
17
- "rootModelIdentifier": "BDC69D2B-C6B6-4E19-A9C2-B2333CA42723"
18
  }
 
1
  {
2
  "fileFormatVersion": "1.0.0",
3
  "itemInfoEntries": {
4
+ "2A4DCB67-0E33-4D7D-93B5-415E9AF6EEAB": {
 
 
 
 
 
 
5
  "author": "com.apple.CoreML",
6
  "description": "CoreML Model Specification",
7
  "name": "model.mlmodel",
8
  "path": "com.apple.CoreML/model.mlmodel"
9
+ },
10
+ "EA3A02AD-4E6B-4B0D-B3A2-0F39B5630C04": {
11
+ "author": "com.apple.CoreML",
12
+ "description": "CoreML Model Weights",
13
+ "name": "weights",
14
+ "path": "com.apple.CoreML/weights"
15
  }
16
  },
17
+ "rootModelIdentifier": "2A4DCB67-0E33-4D7D-93B5-415E9AF6EEAB"
18
  }
ModernBERT-large.mlpackage/Data/com.apple.CoreML/model.mlmodel CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:420ef83f761a8cc1c6baa280486ea89dd2892bce55a32d471af9a28a27b28c38
3
- size 335838
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:da340e916321bea2e8bf96121154b0f4bd17cf87fe3e330dc891f5aa702cfab7
3
+ size 335671
ModernBERT-large.mlpackage/Manifest.json CHANGED
@@ -1,18 +1,18 @@
1
  {
2
  "fileFormatVersion": "1.0.0",
3
  "itemInfoEntries": {
4
- "20D16BF3-9CD7-4631-8373-8CA41044B1AE": {
5
- "author": "com.apple.CoreML",
6
- "description": "CoreML Model Weights",
7
- "name": "weights",
8
- "path": "com.apple.CoreML/weights"
9
- },
10
- "652E02D8-970C-42B2-806B-A5B2911B653D": {
11
  "author": "com.apple.CoreML",
12
  "description": "CoreML Model Specification",
13
  "name": "model.mlmodel",
14
  "path": "com.apple.CoreML/model.mlmodel"
 
 
 
 
 
 
15
  }
16
  },
17
- "rootModelIdentifier": "652E02D8-970C-42B2-806B-A5B2911B653D"
18
  }
 
1
  {
2
  "fileFormatVersion": "1.0.0",
3
  "itemInfoEntries": {
4
+ "033ACE82-9893-4FCC-8758-C4A2366AB463": {
 
 
 
 
 
 
5
  "author": "com.apple.CoreML",
6
  "description": "CoreML Model Specification",
7
  "name": "model.mlmodel",
8
  "path": "com.apple.CoreML/model.mlmodel"
9
+ },
10
+ "4B48085F-08D1-45D5-B120-69ED6033119C": {
11
+ "author": "com.apple.CoreML",
12
+ "description": "CoreML Model Weights",
13
+ "name": "weights",
14
+ "path": "com.apple.CoreML/weights"
15
  }
16
  },
17
+ "rootModelIdentifier": "033ACE82-9893-4FCC-8758-C4A2366AB463"
18
  }
README.md CHANGED
@@ -47,3 +47,20 @@ print("Result:")
47
  print(text.replacingOccurrences(of: "[MASK]", with: predictedTokenText.trimmingCharacters(in: .whitespaces)))
48
  // The capital of Ireland is Dublin.
49
  ```
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  print(text.replacingOccurrences(of: "[MASK]", with: predictedTokenText.trimmingCharacters(in: .whitespaces)))
48
  // The capital of Ireland is Dublin.
49
  ```
50
+
51
+ ### Conversion
52
+
53
+ ```
54
+ uv run https://hf.co/finnvoorhees/ModernBERT-CoreML/raw/main/convert.py
55
+ ```
56
+
57
+ ```
58
+ usage: convert.py [-h] [--model MODEL] [--quantize]
59
+
60
+ Convert ModernBERT to CoreML
61
+
62
+ options:
63
+ -h, --help show this help message and exit
64
+ --model MODEL Model name
65
+ --quantize Linear quantize model
66
+ ```
convert.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # /// script
2
+ # requires-python = ">=3.11"
3
+ # dependencies = [
4
+ # "coremltools",
5
+ # "torch",
6
+ # "transformers",
7
+ # ]
8
+ #
9
+ # [tool.uv.sources]
10
+ # transformers = { git = "https://github.com/huggingface/transformers.git", branch = "main" }
11
+ # ///
12
+
13
+ from transformers import AutoModelForMaskedLM
14
+ import torch
15
+ import coremltools as ct
16
+ import numpy as np
17
+ import argparse
18
+
19
+
20
+ def log(text):
21
+ print(f"\033[92m\033[1m{text}\033[0m")
22
+
23
+
24
+ parser = argparse.ArgumentParser(
25
+ prog="convert.py", description="Convert ModernBERT to CoreML"
26
+ )
27
+ parser.add_argument("--model", type=str, default="ModernBERT-base", help="Model name")
28
+ parser.add_argument("--quantize", action="store_true", help="Linear quantize model")
29
+ args = parser.parse_args()
30
+
31
+
32
+ class Model(torch.nn.Module):
33
+ def __init__(self):
34
+ super().__init__()
35
+ self.model = AutoModelForMaskedLM.from_pretrained(f"answerdotai/{args.model}")
36
+
37
+ def forward(self, input_ids):
38
+ attention_mask = torch.ones_like(input_ids)
39
+ return self.model(input_ids=input_ids, attention_mask=attention_mask).logits
40
+
41
+
42
+ log("Loading model…")
43
+ model = Model().eval()
44
+
45
+ log("Tracing model…")
46
+ example_input = (torch.zeros((1, 1), dtype=torch.int32),)
47
+ traced_model = torch.jit.trace(model, example_input)
48
+
49
+ log("Converting model…")
50
+ input_shape = (
51
+ 1,
52
+ ct.RangeDim(
53
+ lower_bound=1, upper_bound=model.model.config.max_position_embeddings, default=1
54
+ ),
55
+ )
56
+ mlmodel = ct.convert(
57
+ traced_model,
58
+ inputs=[ct.TensorType(name="input_ids", shape=input_shape, dtype=np.int32)],
59
+ outputs=[ct.TensorType(name="logits")],
60
+ minimum_deployment_target=ct.target.macOS15,
61
+ )
62
+
63
+ if args.quantize:
64
+ log("Quantizing model…")
65
+ op_config = ct.optimize.coreml.OpLinearQuantizerConfig(
66
+ mode="linear_symmetric",
67
+ dtype="int4",
68
+ granularity="per_block",
69
+ block_size=32,
70
+ )
71
+ config = ct.optimize.coreml.OptimizationConfig(global_config=op_config)
72
+ mlmodel = ct.optimize.coreml.linear_quantize_weights(mlmodel, config=config)
73
+
74
+ mlmodel.author = "Finn Voorhees"
75
+ mlmodel.short_description = "https://hf.co/finnvoorhees/ModernBERT-CoreML"
76
+
77
+ log("Saving mlmodel…")
78
+ if args.quantize:
79
+ mlmodel.save(f"{args.model}-4bit.mlpackage")
80
+ else:
81
+ mlmodel.save(f"{args.model}.mlpackage")
82
+
83
+ log("Done!")