Skip to main content

Use Huggingface Transformer and Tokenizers as Tensorflow Reusable SavedModels.

Project description

TFtftransformers

Converting Hugginface tokenizers to Tensorflow tokenizers. The main reason is to be able to bundle the tokenizer and model into one Reusable SavedModel.


Source Code: https://github.com/Hugging-Face-Supporter/tftokenizers


Example

This is an example of how one can use Huggingface model and tokenizers bundled together as a Reusable SavedModel and yields the same result as using the model and tokenizer from Huggingface 🤗

import tensorflow as tf
from tftokenizer import TFModel
from tftokenizers import TFAutoTokenizer
from transformers import TFAutoModel

# Load base models from Huggingface
model_name = "bert-base-cased"
model = TFAutoModel.from_pretrained(model_name)

# Load converted TF tokenizer
tokenizer = TFAutoTokenizer(model_name)

# Create a TF Reusable SavedModel
custom_model = TFModel(model=model, tokenizer=tokenizer)

# Tokenizer and model can handle `tf.Tensors` or regular strings
tf_string = tf.constant(["Hello from Tensorflow"])
s1 = "SponGE bob SQuarePants is an avenger"
s2 = "Huggingface to Tensorflow tokenizers"
s3 = "Hello, world!"

output = custom_model(tf_string)
output = custom_model([s1, s2, s3])

# You can also pass arguments, similar to Huggingface tokenizers
output = custom_model(
    [s1, s2, s3],
    max_length=512,
    padding="max_length",
)
print(output)

# Save tokenizer
saved_name = "reusable_bert_tf"
tf.saved_model.save(custom_model, saved_name)

# # Load tokenizer
reloaded_model = tf.saved_model.load(saved_name)
output = reloaded_model([s1, s2, s3])
print(output)

Setup

git clone https://github.com/Hugging-Face-Supporter/tftokenizers.git
cd tftokenizers
poetry install
poetry shell

Run

To convert a Huggingface tokenizer to Tensorflow, first choose one from the models or tokenizers from the Huggingface hub to download.

NOTE

Currently only BERT models work with the converter.

Download

First download tokenizers from the hub by name. Either run the bash script do download multiple tokenizers or download a single tokenizer with the python script.

The idea is to eventually only to automatically download and convert

python tftokenizers/download.py -n bert-base-uncased
bash scripts/download_tokenizers.sh

Convert

Convert downloaded tokenizer from Huggingface format to Tensorflow

python tftokenizers/convert.py

Before Commit

make build

WIP

  • Convert a BERT tokenizer from Huggingface to Tensorflow
  • Make a TF Reusabel SavedModel with Tokenizer and Model in the same class. Emulate how the TF Hub example for BERT works.
  • Find methods for identifying the base tokenizer model and map those settings and special tokens to new tokenizers
  • Extend the tokenizers to more tokenizer types and identify them from a huggingface model name
  • Document how others can use the library and document the different stages in the process
  • Convert other tokenizers. Identify limitations
  • Improve the conversion pipeline (s.a. Download and export files if not passed in or available locally)
  • Support encoding of two sentences at a time Ref
  • Allow the tokenizers to be used for Masking (MLM) Ref

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tftokenizers-0.1.1.tar.gz (19.3 kB view details)

Uploaded Source

Built Distribution

tftokenizers-0.1.1-py3-none-any.whl (22.9 kB view details)

Uploaded Python 3

File details

Details for the file tftokenizers-0.1.1.tar.gz.

File metadata

  • Download URL: tftokenizers-0.1.1.tar.gz
  • Upload date:
  • Size: 19.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.11 CPython/3.8.5 Linux/5.15.11-76051511-generic

File hashes

Hashes for tftokenizers-0.1.1.tar.gz
Algorithm Hash digest
SHA256 99f0c37a438228a63bbab91fe3fe9ed55755d0611b04c45f1a86ba270b8e9cc6
MD5 ba5938df7a38599e601c09af686e986c
BLAKE2b-256 f32a20a3e8c35c9f120c840b09ea6f68b635baa8f1893fe85c5cf55033d1f6ca

See more details on using hashes here.

Provenance

File details

Details for the file tftokenizers-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: tftokenizers-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 22.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.11 CPython/3.8.5 Linux/5.15.11-76051511-generic

File hashes

Hashes for tftokenizers-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d1b4fd584d811b8e278715d333b8c5e5740a22f5fec45a6034560cee12f5ca52
MD5 63ba451920ea73dbed306a6e827f75ee
BLAKE2b-256 be3a2906b2d9e2b86736021265d14c34a2a41b176a25f117e9ef36dda8fee2ad

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page