Finetuning on AWS Sagemaker
Finetune and deploy a custom Command-R model
This sample notebook shows you how to finetune and deploy a custom Command-R model using Amazon SageMaker.
Note: This is a reference notebook and it cannot run unless you make changes suggested in the notebook.
Pre-requisites:
- Note: This notebook contains elements which render correctly in Jupyter interface. Open this notebook from an Amazon SageMaker Notebook Instance or Amazon SageMaker Studio.
- Ensure that IAM role used has AmazonSageMakerFullAccess
- To deploy this ML model successfully, ensure that:
- Either your IAM role has these three permissions and you have authority to make AWS Marketplace subscriptions in the AWS account used:
- aws-marketplace:ViewSubscriptions
- aws-marketplace:Unsubscribe
- aws-marketplace:Subscribe
- or your AWS account has a subscription to the packages for Cohere Command R Finetuning. If so, skip step: Subscribe to the finetune algorithm
- Either your IAM role has these three permissions and you have authority to make AWS Marketplace subscriptions in the AWS account used:
Contents:
- Subscribe to the finetune algorithm
- Upload data and finetune Command-R Model
- Create an endpoint for inference with the custom model
- Clean-up
Usage instructions
You can run this notebook one cell at a time (By using Shift+Enter for running a cell).
1. Subscribe to the finetune algorithm
To subscribe to the model algorithm:
- Open the algorithm listing page Cohere Command R Finetuning
- On the AWS Marketplace listing, click on the Continue to Subscribe button.
- On the Subscribe to this software page, review and click on “Accept Offer” if you and your organization agrees with EULA, pricing, and support terms. On the “Configure and launch” page, make sure ARN displayed in your region match with the ARN in the following cell.
The algorithm is available in the list of AWS regions specified below.
2. Upload data and finetune Command-R
Select a path on S3 to store the training and evaluation datasets and update the s3_data_dir below:
Upload sample training data to S3:
Note:
You’ll need your data in a .jsonl file that contains chat-formatted data. Doc
Example:
JSONL:
Note: If eval dataset is absent, we will auto-split the training dataset into training and evaluation datasets with the ratio of 80:20.
Each dataset must contain at least 1 examples. If an evaluation dataset is absent, training dataset must cointain at least 2 examples.
We recommend using a dataset than contains at least 100 examples but a larger dataset is likely to yield high quality finetunes. Be aware that a larger dataset would mean that the time to finetune would also be longer.
Specify a directory on S3 where finetuned models should be stored. Make sure you do not reuse the same directory across multiple runs.
Create Cohere client:
Optional: Define hyperparameters
train_epochs
: Integer. This is the maximum number of training epochs to run for. Defaults to 1
learning_rate
: Float. The initial learning rate to be used during training. Default to 0.0001
train_batch_size
: Integer. The batch size used during training. Defaults to 16 for Command.
-
early_stopping_enabled
: Boolean. Enables early stopping. When set to true, the final model is the best model found based on the validation set. When set to false, the final model is the last model of training. Defaults to true. -
early_stopping_patience
: Integer. Stop training if the loss metric does not improve beyond ‘early_stopping_threshold’ for this many times of evaluation. Defaults to 10
early_stopping_threshold
: Float. How much the loss must improve to prevent early stopping. Defaults to 0.001.
If the algorithm is command-r-0824-ft, you have the option to define:
lora_rank': Integer
. Lora adapter rank. Defaults to 32
Create fine-tuning jobs for the uploaded datasets. Add a field for eval_data
if you have pre-split your dataset and uploaded both training and evaluation datasets to S3. Remember to use p4de for Command-R Finetuning.
The finetuned weights for the above will be store in a tar file {s3_models_dir}/test-finetune.tar.gz
where the file name is the same as the name used during the creation of the finetune.
3. Create an endpoint for inference with the custom model
A. Create an endpoint
The Cohere AWS SDK provides a built-in method for creating an endpoint for inference. This will automatically deploy the model you finetuned earlier.
Note: This is equivalent to creating and deploying a
ModelPackage
in SageMaker’s SDK.
B. Perform real-time inference
Now, you can access all models deployed on the endpoint for inference:
[Optional] Now let’s evaluate our finetuned model using the evaluation dataset.
4. Clean-up
A. Delete the endpoint
After you’ve successfully performed inference, you can delete the deployed endpoint to avoid being charged continuously. This can also be done via the Cohere AWS SDK:
Unsubscribe to the listing (optional)
If you would like to unsubscribe to the model package, follow these steps. Before you cancel the subscription, ensure that you do not have any deployable models created from the model package or using the algorithm. Note - You can find this information by looking at the container name associated with the model.
Steps to unsubscribe to product from AWS Marketplace:
- Navigate to Machine Learning tab on Your Software subscriptions page
- Locate the listing that you want to cancel the subscription for, and then choose Cancel Subscription to cancel the subscription.