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.

PyPI version

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
  • Improve the conversion pipeline (s.a. Download and export files if not passed in or available locally)
  • Convert other tokenizers. Identify limitations
  • 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.2.tar.gz (19.4 kB view details)

Uploaded Source

Built Distribution

tftokenizers-0.1.2-py3-none-any.whl (23.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tftokenizers-0.1.2.tar.gz
  • Upload date:
  • Size: 19.4 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.2.tar.gz
Algorithm Hash digest
SHA256 b29ade3d7ac82d0fd495c1d686f4bd462b31bd16553134e5b5dbfc9c93f191ae
MD5 af373c3ec90d6c1365d2fb7637fa4fd2
BLAKE2b-256 682dbe8886183043a51a13bdf4c263aaeca496f358c5e6e4260004d1f73e9cd3

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: tftokenizers-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 23.0 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3fb56cfc56aa7fdc5a9b37e2ffe29ba7a32e32c6abd6ddff6fcf4910ad7c2aaf
MD5 9e6c6eba67c4e6bdf07493c619b2e0c4
BLAKE2b-256 cc2f4933def0a16ca97a48833279564fc125dba395c9f28cf262a7a49ff48647

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