File size: 4,119 Bytes
2d5f501
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
---

pipeline_tag: fill-mask
license: mit
base_model: microsoft/mpnet-base
library_name: zeromodels
language:
- en
tags:
- keras
- zeromodels
- mpnet
- fill-mask
- text-encoder
- arxiv:2004.09297
- pytorch
- jax
- tf
---


## ***See [our collection](https://huggingface.co/collections/zeromodels/mpnet-6ab0702ad43a267bae54ded1) for all versions of MPNet.***

# Run MPNet with Keras 3: JAX, PyTorch, or TensorFlow

[![GitHub](https://img.shields.io/badge/GitHub-ZeroModels-black?logo=github)](https://github.com/IMvision12/ZeroModels) [![Docs](https://img.shields.io/badge/Docs-MPNet-blue)](https://imvision12.github.io/ZeroModels/mpnet/) [![Collection](https://img.shields.io/badge/HF-MPNet%20collection-yellow)](https://huggingface.co/collections/zeromodels/mpnet-6ab0702ad43a267bae54ded1)

# zeromodels/mpnet_base



Paper: [MPNet: Masked and Permuted Pre-training for Language Understanding (arXiv:2004.09297)](https://arxiv.org/abs/2004.09297) · [HF Papers](https://huggingface.co/papers/2004.09297)



MPNet is a bidirectional encoder pre-trained with **masked and permuted** language modeling, unifying BERT's masked-LM objective with XLNet's permuted one. Unlike BERT it has no token-type embeddings, offsets position ids past the padding id, and adds a shared relative position bias to every attention layer.



For more details on the model, please go to the upstream [model card](https://huggingface.co/microsoft/mpnet-base).



Pure-**Keras 3** conversion of [`microsoft/mpnet-base`](https://huggingface.co/microsoft/mpnet-base) for [zeromodels](https://github.com/IMvision12/ZeroModels). One implementation runs unmodified on **TensorFlow / Torch / JAX**.



This is a **fill-mask / encoder** checkpoint (`MPNetMaskedLM`, 12 layers / 768 dim). Task heads load via `hf:` fine-tunes.



## ✨ Quick start (fill-mask)



```python

import os

os.environ["KERAS_BACKEND"] = "torch"  # or "jax" / "tensorflow"

from zeromodels.models.mpnet import MPNetMaskedLM, MPNetTokenizer

mlm = MPNetMaskedLM.from_weights("zeromodels/mpnet_base")
tokenizer = MPNetTokenizer.from_weights("zeromodels/mpnet_base")

inputs = tokenizer("the capital of France is <mask>.")
logits = mlm(inputs)  # (1, L, vocab_size)

mask = int((inputs["input_ids"][0] == tokenizer.mask_token_id).argmax())

print(tokenizer.decode([int(logits[0, mask].argmax())]))

```



Load any MPNet variant the same way with `from_weights("zeromodels/<variant>")`:

| Variant | Hub |
|---|---|
| `mpnet_base` | [`zeromodels/mpnet_base`](https://huggingface.co/zeromodels/mpnet_base) |

## Available classes

Load any of these from this repo with `from_weights("zeromodels/mpnet_base")` (or on the fly via the `hf:` prefix). The pretrained backbone is shared; task heads not stored in this checkpoint start randomly initialized, ready for fine-tuning (or load a `hf:` fine-tune).

| Class | Task |
|---|---|
| `MPNetModel` | Encoder backbone |
| `MPNetMaskedLM` | Masked language modeling (fill-mask) |
| `MPNetSequenceClassify` | Sequence classification |
| `MPNetTokenClassify` | Token classification (NER / POS) |
| `MPNetQnA` | Extractive question answering |
| `MPNetMultipleChoice` | Multiple choice |

```python

from zeromodels.models.mpnet import MPNetSequenceClassify

model = MPNetSequenceClassify.from_weights("zeromodels/mpnet_base")

```

## Tips

- Set `KERAS_BACKEND` **before** importing Keras / zeromodels.
- Prefer `MPNetTokenizer.from_weights(...)` so the WordPiece vocab matches.
- Use `<mask>` (not `[MASK]`); MPNet pairs RoBERTa-style special tokens with a WordPiece vocabulary.
- MPNet takes `input_ids` + `attention_mask` only — there are no `token_type_ids`.
- See [MPNet docs](https://imvision12.github.io/ZeroModels/mpnet/) and [Loading Weights](https://imvision12.github.io/ZeroModels/loading_weights/).
- Community / upstream safetensors still work via the `hf:` prefix, e.g. `MPNetMaskedLM.from_weights("hf:microsoft/mpnet-base")`.

## Special Thanks

A huge thank you to the Microsoft MPNet authors for creating and releasing these models.

License: MIT.