Fine-tuning (Snowflake Cortex)

The Snowflake Cortex Fine-Tuning function offers a way to customize large language models for your specific task. This topic describes how the feature works and how to get started with creating your own fine-tuned model.

Overview

Cortex Fine-Tuning allows users to leverage parameter-efficient fine-tuning (PEFT) to create customized adaptors for use with pre-trained models on more specialized tasks. If you don’t want the high cost of training a large model from scratch but need better latency and results than you’re getting from prompt engineering or even retrieval augmented generation (RAG) methods, fine-tuning an existing large model is an option. Fine-tuning allows you to use examples to adjust the behavior of the model and improve the model’s knowledge of domain-specific tasks.

Cortex Fine-Tuning is a fully managed service that lets you fine-tune popular LLMs using your data, all within Snowflake.

Cortex Fine-Tuning features are provided as a Snowflake Cortex function, FINETUNE, with the following arguments:

  • CREATE: Creates a fine-tuning job with the given training data.

  • SHOW: Lists all the fine-tuning jobs that the current role has access to.

  • DESCRIBE: Describes the progress and status of a particular fine-tuning job.

  • CANCEL: Cancels a given fine-tuning job.

Cost considerations

The Snowflake Cortex Fine-Tuning function incurs compute cost based on the number of tokens used in training. In addition, running the COMPLETE function on a fine-tuned model incurs compute costs based on the number of tokens processed. Refer to the consumption table for each cost in credits per million tokens.

A token is the smallest unit of text processed by the Snowflake Cortex Fine-Tuning function, approximately equal to four characters of text. The equivalence of raw input or output text to tokens can vary by model.

  • For the COMPLETE function, which generates new text in the response, both input and output tokens are counted.

  • Fine-tuning trained tokens are calculated as follows:

    Fine-Tuning trained tokens = number of input tokens  * number of epochs trained
    
    Copy

    Use the FINETUNE ('DESCRIBE') (SNOWFLAKE.CORTEX) to see the number of trained tokens for your fine-tuning job.

Other considerations

  • In addition to the charges incurred for tuning and inference, there are normal storage and warehouse costs for storing the output customized adaptors, as well as for running any SQL commands.

  • Fine-tuning jobs are often long running and are not attached to a worksheet session.

Access control requirements

To run a fine-tuning job, the role that creates the fine-tuning job needs the following privileges:

Privilege

Object

Notes

USAGE

DATABASE

The database that the training (and validation) data are queried from.

(CREATE MODEL and USAGE) or OWNERSHIP

SCHEMA

The schema that the model is saved to.

Additionally, to use the FINETUNE function, the ACCOUNTADMIN role must grant the SNOWFLAKE.CORTEX_USER database role to the user who will call the function. See LLM Functions required privileges topic for details.

Models available to fine-tune

You have the following base models that you can fine-tune. Models available for fine-tuning may be added or removed in the future:

Name

Description

mistral-7b

7 billion parameter large language model from Mistral AI that is ideal for your simplest summarization, structuration, and question answering tasks that need to be done quickly. It offers low latency and high throughput processing for multiple pages of text with its 32K context window.

mixtral-8x7b

A large language model from Mistral AI that is ideal for text generation, classification, and question answering. Mistral models are optimized for low latency with low memory requirements, which translates into higher throughput for enterprise use cases.

llama3-8b

A large language model from Meta that is ideal for tasks that require low to moderate reasoning with better accuracy than the llama2-70b-chat model, like text classification, summarization, and sentiment analysis.

llama3-70b

An LLM from Meta that delivers state of the art performance ideal for chat applications, content creation, and enterprise applications.

How to tune a model

The overall workflow for tuning a model is as follows:

  1. Prepare the training data.

  2. Start the fine-tuning job with the required parameters.

  3. Monitor training job.

Once training is complete, you can use the model name provided by Cortex Fine-Tuning to run inference on your model.

Prepare the training data

The training data must come from a Snowflake table or view and the query result must contain columns named prompt and completion. If your table or view does not contain columns with the required names, use a column alias in your query to name them. This query is given as a parameter to the FINETUNE function. You will get an error if the results do not contain prompt and completion column names.

Note

All columns other than the prompt and completion columns will be ignored by the FINETUNE function. Snowflake recommends using a query that selects only the columns you need.

The following code calls the FINETUNE function and uses the SELECT ... AS syntax to set two of the columns in the query result to prompt and completion.

SELECT SNOWFLAKE.CORTEX.FINETUNE(
  'CREATE',
  'my_tuned_model',
  'mistral-7b',
  'SELECT a AS prompt, d AS completion FROM train',
  'SELECT a AS prompt, d AS completion FROM validation'
);
Copy

A prompt is an input to the LLM and completion is the response from the LLM. Your training data should include prompt and completion pairs that show how you want the model to respond to particular prompts.

The following are additional recommendations and requirements regarding your training data for getting optimal performance from fine-tuning.

  • Start with a few hundred examples. Starting with too many examples may increase tuning time drastically with minimal improvement in performance.

  • For each example, you must use only a portion of the allotted context window for the base model you are tuning. Context window is defined in terms of tokens. A token is the smallest unit of text processed by Snowflake Cortex functions, approximately equal to four characters of text.

  • The portion of the context window alloted for prompt and completion for each base model is defined in the following table:

    Model

    Context Window

    Input Context (prompt)

    Output Context (completion)

    mistral-7b

    32k

    28k

    4k

    llama3-8b

    8k

    6k

    2k

    mixtral-8x7b

    32k

    28k

    4k

    llama3-70b

    8k

    6k

    2k

Start the fine-tuning function

Call the SNOWFLAKE.CORTEX.FINETUNE function and pass in ‘CREATE’ as the first argument to start a fine-tuning job.

This example uses the mistral-7b model as the base model to create a job with a model output name of my_tuned_model and training and validation data querying from the my_training_data and my_validation_data tables respectively.

USE DATABASE mydb;
USE SCHEMA myschema;

SELECT SNOWFLAKE.CORTEX.FINETUNE(
  'CREATE',
  'my_tuned_model',
  'mistral-7b',
  'SELECT prompt, completion FROM my_training_data',
  'SELECT prompt, completion FROM my_validation_data'
);
Copy

You can use absolute paths for each of the database objects such as the model or data if you want to use different database and schema for each. The following example shows creating a fine-tuning job with data from mydb2.myschema2 database and schema and saving the fine-tuned model to the mydb.myschema database and schema.

SELECT SNOWFLAKE.CORTEX.FINETUNE(
  'CREATE',
  'mydb.myschema.my_tuned_model',
  'mistral-7b',
  'SELECT prompt, completion FROM mydb2.myschema2.my_training_data',
  'SELECT prompt, completion FROM mydb2.myschema2.my_validation_data'
);
Copy

The SNOWFLAKE.CORTEX.FINETUNE function with ‘CREATE’ as the first argument returns a fine-tuned model ID as the output. Use this ID to get status or job progress using the SNOWFLAKE.CORTEX.FINETUNE function with ‘DESCRIBE’ as the first argument.

Managing fine-tuned models

Fine-tuning jobs are long running, which means they are not tied to a worksheet session. You can check the status of your tuning job using the SNOWFLAKE.CORTEX.FINETUNE function with ‘SHOW’ or ‘DESCRIBE’ as the first argument.

If you no longer need a fine-tuning job, you can use SNOWFLAKE.CORTEX.FINETUNE function with ‘CANCEL’ as the first argument and the job ID as the second argument to terminate it.

Use your fine-tuned model for inference

Use the COMPLETE LLM function with the name your fine-tuned model to make inferences.

This example shows a call to the COMPLETE function with the name of your fine-tuned model.

SELECT SNOWFLAKE.CORTEX.COMPLETE(
  'my_tuned_model',
  'How to fine-tune mistral models'
);
Copy

The following is a snippet of the output from the example call:

Mistral models are a type of deep learning model used for image recognition and classification. Fine-tuning a Mistral model involves adjusting the model's parameters to ...

Limitations and known issues

  • Fine-tuning jobs are listable at the account-level only.

  • The fine-tuning jobs returned from FINETUNE ('SHOW') (SNOWFLAKE.CORTEX) are not permanent and may be garbage collected periodically.

  • If a base model is removed from the Cortex LLM Functions, your fine-tuned model will no longer work.