Integrate a library
Last updated
Last updated
The BOINC AI Hub makes hosting and sharing models with the community easy. It supports in the Open Source ecosystem. We are always working on expanding this support to push collaborative Machine Learning forward. The boincai_hub
library plays a key role in this process, allowing any Python script to easily push and load files.
There are four main ways to integrate a library with the Hub:
Push to Hub: implement a method to upload a model to the Hub. This includes the model weights, as well as and any other relevant information or data necessary to run the model (for example, training logs). This method is often called push_to_hub()
.
Download from Hub: implement a method to load a model from the Hub. The method should download the model configuration/weights and load the model. This method is often called from_pretrained
or load_from_hub()
.
Inference API: use our servers to run inference on models supported by your library for free.
Widgets: display a widget on the landing page of your models on the Hub. It allows users to quickly try a model from the browser.
In this guide, we will focus on the first two topics. We will present the two main approaches you can use to integrate a library, with their advantages and drawbacks. Everything is summarized at the end of the guide to help you choose between the two. Please keep in mind that these are only guidelines that you are free to adapt to you requirements.
If you are interested in Inference and Widgets, you can follow . In both cases, you can reach out to us if you are integrating a library with the Hub and want to be listed .
The first approach to integrate a library to the Hub is to actually implement the push_to_hub
and from_pretrained
methods by yourself. This gives you full flexibility on which files you need to upload/download and how to handle inputs specific to your framework. You can refer to the two and guides to learn more about how to do that. This is, for example how the FastAI integration is implemented (see and ).
Implementation can differ between libraries, but the workflow is often similar.
This is how a from_pretrained
method usually look like:
Copied
The push_to_hub
method often requires a bit more complexity to handle repo creation, generate the model card and save weights. A common approach is to save all of these files in a temporary folder, upload it and then delete it.
Copied
While being flexible, this approach has some drawbacks, especially in terms of maintenance. BOINC AI users are often used to additional features when working with boincai_hub
. For example, when loading files from the Hub, it is common to offer parameters like:
token
: to download from a private repo
revision
: to download from a specific branch
cache_dir
: to cache files in a specific directory
force_download
/resume_download
/local_files_only
: to reuse the cache or not
api_endpoint
/proxies
: configure HTTP session
When pushing models, similar parameters are supported:
commit_message
: custom commit message
private
: create a private repo if missing
create_pr
: create a PR instead of pushing to main
branch
: push to a branch instead of the main
branch
allow_patterns
/ignore_patterns
: filter which files to upload
token
api_endpoint
โฆ
All of these parameters can be added to the implementations we saw above and passed to the boincai_hub
methods. However, if a parameter changes or a new feature is added, you will need to update your package. Supporting those parameters also means more documentation to maintain on your side. To see how to mitigate these limitations, letโs jump to our next section class inheritance.
As we saw above, there are two main methods to include in your library to integrate it with the Hub: upload files (push_to_hub
) and download files (from_pretrained
). You can implement those methods by yourself but it comes with caveats. To tackle this, boincai_hub
provides a tool that uses class inheritance. Letโs see how it works!
Implement the private methods:
You are done!
How to use it?
Here is how any user can load/save a PyTorch model from/to the Hub:
Copied
Implementation
First, inherit your class from ModelHubMixin
:
Copied
Implement the _save_pretrained
method:
Copied
Implement the _from_pretrained
method:
Copied
And thatโs it! Your library now enables users to upload and download files to and from the Hub.
Letโs quickly sum up the two approaches we saw with their advantages and drawbacks. The table below is only indicative. Your framework might have some specificities that you need to address. This guide is only here to give guidelines and ideas on how to handle integration. In any case, feel free to contact us if you have any questions!
Integration
Using helpers
User experience
model = load_from_hub(...)
push_to_hub(model, ...)
model = MyModel.from_pretrained(...)
model.push_to_hub(...)
Flexibility
Very flexible. You fully control the implementation.
Less flexible. Your framework must have a model class.
Maintenance
More maintenance to add support for configuration, and new features. Might also require fixing issues reported by users.
Less maintenance as most of the interactions with the Hub are implemented in boincai_hub
.
Documentation / Type annotation
To be written manually.
Partially handled by boincai_hub
.
This is of course only an example. If you are interested in more complex manipulations (delete remote files, upload weights on the fly, persist weights locally, etc.) please refer to the guide.
In a lot of cases, a library already implements its model using a Python class. The class contains the properties of the model and methods to load, run, train, and evaluate it. Our approach is to extend this class to include upload and download features using mixins. A is a class that is meant to extend an existing class with a set of specific features using multiple inheritance. boincai_hub
provides its own mixin, the . The key here is to understand its behavior and how to customize it.
The class implements 3 public methods (push_to_hub
, save_pretrained
and from_pretrained
). Those are the methods that your users will call to load/save models with your library. also defines 2 private methods (_save_pretrained
and _from_pretrained
). Those are the ones you must implement. So to integrate your library, you should:
Make your Model class inherit from .
: method taking as input a path to a directory and saving the model to it. You must write all the logic to dump your model in this method: model card, model weights, configuration files, training logs, and figures. Any relevant information for this model must be handled by this method. are particularly important to describe your model. Check out for more details.
: class method taking as input a model_id
and returning an instantiated model. The method must download the relevant files and load them.
The advantage of using is that once you take care of the serialization/loading of the files, you are ready to go. You donโt need to worry about stuff like repo creation, commits, PRs, or revisions. All of this is handled by the mixin and is available to your users. The Mixin also ensures that public methods are well documented and type annotated.
A good example of what we saw above is , our integration for the PyTorch framework. This is a ready-to-use integration.
The implementation is actually very straightforward, and the full implementation can be found .
Using