finnvoorhees commited on
Commit
19e58f9
·
1 Parent(s): f0540d3

Auto-generate attention mask

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:9a92e9ca48ff235d69963aae1243cc46012dfafac748c1a28e548c5a43cb032c
3
- size 278858
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9a9d1c57cce5d2cdb960bbeb3cfd97443a0752e0542ab958eb636c6a63a87774
3
+ size 279063
ModernBERT-base-4bit.mlpackage/Manifest.json CHANGED
@@ -1,18 +1,18 @@
1
  {
2
  "fileFormatVersion": "1.0.0",
3
  "itemInfoEntries": {
4
- "9BD93E54-BA9D-470F-B2B6-0C4E6EF68B69": {
5
  "author": "com.apple.CoreML",
6
  "description": "CoreML Model Specification",
7
  "name": "model.mlmodel",
8
  "path": "com.apple.CoreML/model.mlmodel"
9
  },
10
- "9EE76E38-2157-40A9-B3E0-1974022135F2": {
11
  "author": "com.apple.CoreML",
12
  "description": "CoreML Model Weights",
13
  "name": "weights",
14
  "path": "com.apple.CoreML/weights"
15
  }
16
  },
17
- "rootModelIdentifier": "9BD93E54-BA9D-470F-B2B6-0C4E6EF68B69"
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
  }
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:769355987eb544d7ce0e25260e07dcc407b42a7044aa08835e3d434019b6629d
3
- size 266877
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:67cc80bf6060a2d08059fe98c081fc9ebc34b074252143ca4dfa03d3e4f277df
3
+ size 267080
ModernBERT-base.mlpackage/Manifest.json CHANGED
@@ -1,18 +1,18 @@
1
  {
2
  "fileFormatVersion": "1.0.0",
3
  "itemInfoEntries": {
4
- "5DE8F5DD-BC7D-4275-90A0-85F8F3B565E9": {
5
- "author": "com.apple.CoreML",
6
- "description": "CoreML Model Weights",
7
- "name": "weights",
8
- "path": "com.apple.CoreML/weights"
9
- },
10
- "6B85C700-26D7-47DD-9768-DA523F859464": {
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": "6B85C700-26D7-47DD-9768-DA523F859464"
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
  }
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:cc436ae7fd0bc2824bd12f0c0049db5d15234510d9ec73b78fa441d440b455ac
3
- size 350781
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:dbe210fd61ab3ced4e7041c483472e46a3383d4e44849a6d5cdba2c4eea3614b
3
+ size 350974
ModernBERT-large-4bit.mlpackage/Manifest.json CHANGED
@@ -1,18 +1,18 @@
1
  {
2
  "fileFormatVersion": "1.0.0",
3
  "itemInfoEntries": {
4
- "69217F75-A051-4A83-B7B8-3F3A04468AD6": {
5
- "author": "com.apple.CoreML",
6
- "description": "CoreML Model Specification",
7
- "name": "model.mlmodel",
8
- "path": "com.apple.CoreML/model.mlmodel"
9
- },
10
- "D932257A-B814-45A2-B0A2-9CD3B727AF75": {
11
  "author": "com.apple.CoreML",
12
  "description": "CoreML Model Weights",
13
  "name": "weights",
14
  "path": "com.apple.CoreML/weights"
 
 
 
 
 
 
15
  }
16
  },
17
- "rootModelIdentifier": "69217F75-A051-4A83-B7B8-3F3A04468AD6"
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
  }
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:ab69e9172a16cb1a6ed2efd5557c957446df8b6db7bc7cbfd83be23635ba71f6
3
- size 335647
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:420ef83f761a8cc1c6baa280486ea89dd2892bce55a32d471af9a28a27b28c38
3
+ size 335838
ModernBERT-large.mlpackage/Manifest.json CHANGED
@@ -1,18 +1,18 @@
1
  {
2
  "fileFormatVersion": "1.0.0",
3
  "itemInfoEntries": {
4
- "D1999926-212D-4488-9717-A26B486AE1EE": {
5
- "author": "com.apple.CoreML",
6
- "description": "CoreML Model Specification",
7
- "name": "model.mlmodel",
8
- "path": "com.apple.CoreML/model.mlmodel"
9
- },
10
- "E3431F6D-5367-47DA-8179-3E46922823A0": {
11
  "author": "com.apple.CoreML",
12
  "description": "CoreML Model Weights",
13
  "name": "weights",
14
  "path": "com.apple.CoreML/weights"
 
 
 
 
 
 
15
  }
16
  },
17
- "rootModelIdentifier": "D1999926-212D-4488-9717-A26B486AE1EE"
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
  }
README.md CHANGED
@@ -29,24 +29,21 @@ print("Loading…")
29
  let model = try await ModernBERT_base.load()
30
  let tokenizer = try await AutoTokenizer.from(pretrained: "answerdotai/ModernBERT-base")
31
 
 
32
  let tokens = tokenizer(text)
33
  let inputIDs = MLShapedArray(scalars: tokens.map(Int32.init), shape: [1, tokens.count])
34
- let attentionMask = MLShapedArray<Int32>(scalars: tokens.map { _ in 1 }, shape: [1, tokens.count])
35
- let input = ModernBERT_baseInput(
36
- input_ids: inputIDs,
37
- attention_mask: attentionMask
38
- )
39
 
40
  print("Predicting…")
41
  let output = try await model.prediction(input: input)
42
  let logits = output.logitsShapedArray
43
- let maskPosition = tokens.firstIndex(
44
- of: tokenizer.encode(text: "[MASK]", addSpecialTokens: false)[0]
45
- )!
46
- let maskLogits = logits[0, maskPosition]
47
- let maskToken = Int(await MLTensor(maskLogits).argmax().shapedArray(of: Int32.self).scalar!)
48
- let maskText = tokenizer.decode(tokens: [maskToken])
49
 
 
 
 
 
 
 
 
50
  // The capital of Ireland is Dublin.
51
- print(text.replacingOccurrences(of: "[MASK]", with: maskText.trimmingCharacters(in: .whitespaces)))
52
  ```
 
29
  let model = try await ModernBERT_base.load()
30
  let tokenizer = try await AutoTokenizer.from(pretrained: "answerdotai/ModernBERT-base")
31
 
32
+ print("Tokenizing…")
33
  let tokens = tokenizer(text)
34
  let inputIDs = MLShapedArray(scalars: tokens.map(Int32.init), shape: [1, tokens.count])
35
+ let input = ModernBERT_baseInput(input_ids: inputIDs)
 
 
 
 
36
 
37
  print("Predicting…")
38
  let output = try await model.prediction(input: input)
39
  let logits = output.logitsShapedArray
 
 
 
 
 
 
40
 
41
+ print("Decoding…")
42
+ let maskPosition = tokens.firstIndex(of: tokenizer.convertTokenToId("[MASK]")!)!
43
+ let predictedTokenID = await MLTensor(logits[0, maskPosition]).argmax().shapedArray(of: Int32.self).scalar!
44
+ let predictedTokenText = tokenizer.decode(tokens: [Int(predictedTokenID)])
45
+
46
+ print("Result:")
47
+ print(text.replacingOccurrences(of: "[MASK]", with: predictedTokenText.trimmingCharacters(in: .whitespaces)))
48
  // The capital of Ireland is Dublin.
 
49
  ```