Model weights
Load model weights without Hugging Face or S3
Serving a model requires access to model files, such as model weights. These files are often many gigabytes.
For many models, these files are loaded from Hugging Face. However, model files can come from other sources or be stored directly in the Truss. Model weights and other model data can be:
- Public on Hugging Face (default, example here)
- Private on Hugging Face
- Bundled directly with the Truss
- Public cloud storage like S3
- Private cloud storage like S3
Bundling model weights in Truss
You can bundle model data directly with your model in Truss. To do so, use the Truss’ data
folder to store any necessary files.
Here’s an example of the data
folder for a Truss of Stable Diffusion 2.1.
To access the data in the model, use the self._data_dir
variable in the load()
function of model/model.py
:
Loading public model weights from S3
Bundling multi-gigabyte files with your Truss can be difficult if you have limited local storage and can make deployment slow. Instead, you can store your model weights and other files in cloud storage like S3.
Using files from S3 requires four steps:
- Uploading the content of your data directory to S3
- Setting
external_data
in config.yaml - Removing unneeded files from the
data
directory - Accessing data correctly in the model
Here’s an example of that setup for Stable Diffusion, where we have already uploaded the content of our data/
directory to S3.
First, add the URLs for hosted versions of the large files to config.yaml
:
Each URL matches with a local data path that represents where the model data would be stored if everything was bundled together locally. This is how your model code will know where to look for the data.
Then, get rid of the large files from your data
folder. The Stable Diffusion Truss has the following directory structure after large files are removed:
The code in model/model.py
does not need to be changed and will automatically pull the large files from the provided links.
Loading private model weights from S3
If your model weights are proprietary, you’ll be storing them in a private S3 bucket or similar access-restricted data store. Accessing these model files works exactly the same as above, but first uses secrets to securely authenticate your model with the data store.
First, set the following secrets in config.yaml
. Set the values to null
, only the keys are needed here.
Then, add secrets to your Baseten account for your AWS access key id, secret access key, region, and bucket. This time, use the actual values as they will be securely stored and provided to your model at runtime.
In your model code, authenticate with AWS in the __init__()
function:
You can then use the boto3
package to access your model weights in load()
.
When you’re ready to deploy your model, make sure to pass is_trusted=True
to baseten.deploy()
:
For further details, see docs on using secrets in models.