Downloading a custom foundation model and setting up storage
To deploy a custom foundation model for inferencing with watsonx.ai, you must upload the model to a cloud object storage. You can use the bucket in the IBM Cloud Object Storage that is associated with your deployment space or an external cloud storage.
Follow these steps to download a custom foundation model from a public repository and then upload it to cloud object storage:
- Download the model from a public repository
- General-purpose models only: Convert the model to the required format
- Time-series models only: Generate the tsfm_config.json file
- Upload the model to cloud object storage
Downloading models from public repositories
You can use public model repositories to download foundation models from. Public model repositories might require you to set up an account before you download models from them. For example, you can use Hugging Face, a public model repository, to download custom foundation models for your use case. You must set up a Hugging Face account to download the model from Hugging Face.
These steps demonstrate how to download a custom foundation model by using a Hugging Face model. Follow these steps to download a custom foundation model by using the Hugging Face command-line interface:
-
Install the
huggingface-cli
package withpip
:pip install -U "huggingface_hub[cli]"
-
Check that the huggingface-cli is correctly set up:
huggingface-cli --help
-
Configure the required environment variables:
export HF_TOKEN="<your Hugging Face token>" export MODEL_NAME="<name of the model>" export MODEL_DIR="<directory to download the model to>"
-
Set up a directory on your local disk to download the model to:
mkdir ${MODEL_DIR}
-
Log in to Hugging Face command-line interface and download the model:
huggingface-cli login --token ${HF_TOKEN} huggingface-cli download ${MODEL_NAME} --local-dir ${MODEL_DIR} --cache-dir ${MODEL_DIR}
Converting a model to the required format
Time-series models don't require any conversion at this stage. If you're downloading a time-series model, skip to the section about Generating the tsfm_config.json file for time-series models.
Before you add a model to object storage, you must make sure that your model is compatible with the Text Generation Inference (TGI) standard and is built with a supported model architecture and model type. For more information, see Planning to deploy a custom foundation model.
If your model was fine-tuned with InstructLab, conversion to the safetensors
format might not be possible:
- Models that were fine-tuned in Linux environment require conversion.
- Models that were fine-tuned on a Mac can't be converted.
- Models that were fine-tuned and saved to the
.gguf
format (in any environment) cannot be converted.
To convert a model that was fine-tuned with InstructLab, use the code in This repository to convert your model.
For all the other models, if your model is not in the safetensors
format and does not contain the tokenizer.json
file, follow these steps to convert your model to the required format. Otherwise, skip to the section
about Setting up cloud object storage and adding the model.
-
Install
podman
Desktop on your local machine. -
Pull the TGIS image:
export TGIS_IMAGE="quay.io/modh/text-generation-inference:rhoai-2.8-58cac74" podman pull ${TGIS_IMAGE}
-
Convert the model:
container_id=$(podman run -itd --privileged -u 0 -v ${MODEL_DIR}:/tmp ${TGIS_IMAGE} tail -f /dev/null) podman exec -it ${container_id} bash -c 'export MODEL_PATH=/tmp ; text-generation-server convert-to-safetensors ${MODEL_PATH} ; text-generation-server convert-to-fast-tokenizer ${MODEL_PATH}'
Generating the tsfm_config.json file for time-series models
General-purpose models don't require the tsfm_config.json
file. If you're downloading a general-purpose model, skip to the section about Setting up cloud object storage and adding the model.
The model directory for time-series models, must contain the tsfm_config.json
file. Time-series models that are hosted on Hugging Face (model_type: tinytimemixer
) may not include this file. If the file is not there
when the model is downloaded and deployed, forecasting will fail. If you don't see this file in the model directory, perform these steps:
Prerequisites:
- To generate the files, Python version 3.9 or higher must be installed in your environment.
- Because package installation is required, if you're operating in an air-gapped environment, you must run the script on a machine that has access to the Internet and then transfer the generated files to your air-gapped environment.
-
Run this code to install two required packages on your local machine:
pip3 install granite-tsfm==0.2.27 pip3 install ibm-cos-sdk
-
Create and run a Python script that creates the missing file:
from tsfm_public import TinyTimeMixerForPrediction from tsfm_public.toolkit.service_util import save_deployment_package # Instantiate the model target_columns = ["total load actual"] zeroshot_model = TinyTimeMixerForPrediction.from_pretrained( "ibm-granite/granite-timeseries-ttm-r2", # Name of the model on Hugging Face num_input_channels=len(target_columns), # tsp.num_input_channels ) save_deployment_package("ttm", zeroshot_model)
The script generates three files:
config.json
,model.safetensors
, andtsfm_config.json
and saves them to thettm
directory. -
Copy the generated
tsfm_config.json
file to the directory where your downloaded model is located.
Uploading the model to cloud object storage
You can upload your model to a bucket in the IBM Cloud Object Storage or any other storage buckets, such as Amazon Simple Storage Service (Amazon S3). Here are some of the cloud object storages offered by IBM:
Uploading the model to IBM Cloud Object Storage by using the IBM Aspera Transfer SDK
Prerequisites:
- Download the IBM Aspera Transfer SDK. Click this link.
- Set these environment variables:
- Path to IBM Aspera Transfer SDK as the
path-to-aspera
- Path to the folder with the model as
path-to-model-folder
- Bucket name to transfer the model to as
bucket-name
- Your cloud object storage API key as
api-key
- Your cloud object storage service instance ID as
cos-service-instance-id
- Your cloud object storage service endpoint as
cos-service-endpoint
- Path to IBM Aspera Transfer SDK as the
Use this script to upload your model to IBM Cloud Object Storage by using the IBM Aspera Transfer SDK:
import grpc
import sys
import time, subprocess
import json
import os.path
import os.environ
import transfer_pb2 as transfer_manager
import transfer_pb2_grpc as transfer_manager_grpc
path_to_aspera = os.environ['path-to-aspera']
sys.path.append(f'{path_to_aspera}/connectors/python')
def start_aspera_daemon():
config_path = f'/{path_to_aspera}/config/asperatransferd.json'
daemon_path = f'/{path_to_aspera}/bin/asperatransferd'
# Start the daemon
process = subprocess.Popen([daemon_path, '--config', config_path])
print(f"Started Aspera Transfer SDK daemon with PID {process.pid}")
time.sleep(5) # Increased wait time for the daemon to start properly
return process
def run():
try:
# create a connection to the transfer manager daemon
client = transfer_manager_grpc.TransferServiceStub(grpc.insecure_channel('localhost:55002'))
# create transfer spec
transfer_spec = {
"session_initiation":{
"icos":{
"bucket": os.environ['bucket-name'],
"api_key": os.environ['cos-api-key'],
"ibm_service_instance_id": os.environ['cos-service-instance-id'],
"ibm_service_endpoint": os.environ['cos-service-endpoint'] # example: https://s3.us-south.cloud-object-storage.appdomain.cloud
}
},
"direction": "send",
"title": "COS Upload",
"assets": {
"destination_root": "/model",
"paths": [
{
"source": os.environ['path-to-model-folder']
}
]
}
}
transfer_spec = json.dumps(transfer_spec)
# create a transfer request
transfer_request = transfer_manager.TransferRequest(
transferType=transfer_manager.FILE_REGULAR,
config=transfer_manager.TransferConfig(),
transferSpec=transfer_spec
)
# send start transfer request to transfer manager daemon
try:
transfer_response = client.StartTransfer(transfer_request)
transfer_id = transfer_response.transferId
print(f"Transfer started with id {transfer_id}")
except grpc.RpcError as e:
print(f"Error starting transfer: {e.code()}: {e.details()}")
return
# monitor transfer status
try:
# Monitor transfer status
while True:
response = client.MonitorTransfers(
transfer_manager.RegistrationRequest(
filters=[transfer_manager.RegistrationFilter(
transferId=[transfer_id]
)]))
for transfer_info in response:
print(f"Transfer info: {transfer_info}")
# Check transfer status in response
status = transfer_info.status
if status == transfer_manager.FAILED or status == transfer_manager.COMPLETED:
print(f"Transfer finished with status {status}")
return
# Wait before polling again
time.sleep(5)
except grpc.RpcError as e:
print(f"Error monitoring transfer: {e.code()}: {e.details()}")
except Exception as e:
print(f"Unexpected error: {str(e)}", file=sys.stderr)
if __name__ == '__main__':
# Start the Aspera Transfer SDK daemon
daemon_process = start_aspera_daemon()
# Run the file transfer
run()
# Optionally, stop the Aspera daemon after transfer
daemon_process.terminate()
print("Aspera Transfer SDK daemon stopped")
Uploading the model to IBM Cloud Object Storage by using tools that are provided by third parties
You can use third-party software to upload your model to IBM Cloud Object Storage.
Follow these example steps to upload your model to IBM Cloud Object Storage by using the Amazon Web Services command-line interface:
-
Install the Amazon Web Services command-line interface with
pip
:pip install awscli
-
Set the required environment variables:
export AWS_ACCESS_KEY_ID="<your access key>" export AWS_SECRET_ACCESS_KEY="<your secret access key>" export ENDPOINT="<s3 endpoint URL>" export BUCKET_NAME="<name of the bucket to upload the model>" MODEL_FOLDER=${MODEL_NAME//\//-} # The name of the created folder is based on model name. export MODEL_FOLDER=${MODEL_FOLDER//./-} # Just in case, we're removing slashes and dots from it.
-
Add the model to the IBM Cloud Object Storage bucket by using the Amazon Web Services command-line interface:
aws --endpoint-url ${ENDPOINT} s3 cp ${MODEL_DIR} s3://${BUCKET_NAME}/${MODEL_FOLDER}/ --recursive --follow-symlinks
Next step
Parent topic: Planning to deploy a custom foundation model