mlx-examples/bert
2024-03-13 10:24:21 -07:00
..
weights BERT implementation 2023-12-08 05:14:11 -05:00
convert.py Add llms subdir + update README (#145) 2023-12-20 10:22:25 -08:00
model.py bert encoder inherits from nn.Module now (#571) 2024-03-13 10:24:21 -07:00
README.md docs: added missing imports (#375) 2024-01-25 10:44:53 -08:00
requirements.txt Update to mlx>=0.0.5 2023-12-13 17:48:07 -05:00
test.py Some fixes / cleanup for BERT example (#269) 2024-01-09 08:44:51 -08:00

BERT

An implementation of BERT (Devlin, et al., 2019) within MLX.

Setup

Install the requirements:

pip install -r requirements.txt

Then convert the weights with:

python convert.py \
    --bert-model bert-base-uncased \
    --mlx-model weights/bert-base-uncased.npz

Usage

To use the Bert model in your own code, you can load it with:

import mlx.core as mx
from model import Bert, load_model

model, tokenizer = load_model(
    "bert-base-uncased",
    "weights/bert-base-uncased.npz")

batch = ["This is an example of BERT working on MLX."]
tokens = tokenizer(batch, return_tensors="np", padding=True)
tokens = {key: mx.array(v) for key, v in tokens.items()}

output, pooled = model(**tokens)

The output contains a Batch x Tokens x Dims tensor, representing a vector for every input token. If you want to train anything at a token-level, you'll want to use this.

The pooled contains a Batch x Dims tensor, which is the pooled representation for each input. If you want to train a classification model, you'll want to use this.

Test

You can check the output for the default model (bert-base-uncased) matches the Hugging Face version with:

python test.py