Exporting Transformers Models to Core ML: A Step-by-Step Guide

Dec 8, 2020 | Data Science

With the rise of machine learning applications on mobile devices, it’s essential to be able to export large Transformer models to formats that are optimized for these platforms. The Hugging Face library provides a smooth pathway to convert your models to Core ML format, which can then be easily integrated into macOS and iOS applications. In this guide, we will walk you through the process step-by-step.

Why Use Core ML?

Core ML is Apple’s machine learning framework designed for on-device model inference. It allows apps to utilize trained models for quick predictions while minimizing CPU and memory overhead. Whether your target is an iPhone, iPad, or even a Mac, Core ML is the go-to framework for real-time inference of machine learning models. This tutorial focuses on transforming your Transformers models into this efficient format.

When to Use Exporters

If you have a Transformer model implemented in PyTorch, TensorFlow, or JAX, but want to deploy it using Core ML, the Exporters package provides an elegant solution without requiring you to write cumbersome conversion scripts. It is tightly integrated with the Hugging Face Hub, allowing you to manage models seamlessly.

Installation

To get started with the Core ML exportation, you’ll need to clone the repository and install the necessary packages. Here’s how:

  • Clone the repository:
  • bash
    $ git clone https://github.com/huggingface/exporters.git
      
  • Navigate to the directory and install the package:
  • bash
    $ cd exporters
    $ pip install -e .
      

Note: While exporting from Linux is possible, we recommend using macOS for the full validation features.

Exporting a Model to Core ML

Once the setup is complete, you can begin exporting a model to Core ML. Here’s how:

  • First, install additional dependencies:
  • bash
    pip install transformers[coreml]
        
  • Now, you can export a model using the following command:
  • bash
    python -m exporters.coreml --model=distilbert-base-uncased exported
      

The resulting Core ML model file will be saved as Model.mlpackage in the exported directory.

Visualizing the Process: The Box Analogy

Imagine you’re packing a large box (your Transformers model) for a long journey (deployment). Usually, you spend time ensuring everything fits snugly and securely. The Exporters package acts like a professional packing service that knows the best way to fit that large box into your car (Core ML format). Rather than struggling to force it in (manual conversion), you hand it over to the Exporters package to organize and optimize it for a smooth ride.

Troubleshooting: Common Issues and Solutions

Sometimes, exporting a model may not go as planned. Here are some troubleshooting tips:

  • Double-check your arguments: Ensure the task matches the model architecture. For instance, using a feature-extraction task with a model meant for classification will lead to errors.
  • Conversion Errors: Occasionally, certain operations may not be supported by Core ML. If the conversion fails, consider the following options:
    • Implement the missing operator within the Exporters package.
    • Adjust the original model or implement workarounds with stringently defined parameters.
    • Reach out to the community or submit an issue for more tailored help.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Using the Exported Model

After exporting, you can integrate the Core ML model with your application in Xcode. It will automatically generate the necessary Swift class for you to make predictions. Just remember that depending on how you’ve set up your export options, you might need to preprocess the input data yourself.

Conclusion

Exporting Transformer models to Core ML can significantly streamline the deployment process for machine learning applications on almost any Apple device. With this guide, you should be well-equipped to tackle the exporting process, troubleshoot common issues, and harness the power of machine learning in your mobile applications.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox