Skip to content

Commit 5b9c440

Browse files
authored
Merge pull request #2645 from ssalgadodev/patch-50
Create fine-tune-models.md
2 parents d4a8f64 + d6a3160 commit 5b9c440

File tree

3 files changed

+237
-0
lines changed

3 files changed

+237
-0
lines changed
Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
---
2+
title: Fine-tune models using serverless APIs in Azure AI Foundry portal
3+
titleSuffix: Azure AI Foundry
4+
description: Learn how to fine-tune models deployed via serverless APIs in Azure AI Foundry.
5+
manager: scottpolly
6+
ms.service: azure-ai-studio
7+
ms.topic: how-to
8+
ms.date: 01/31/2025
9+
ms.reviewer: rasavage
10+
reviewer: RSavage2
11+
ms.author: ssalgado
12+
author: ssalgadodev
13+
ms.custom: references_regions
14+
---
15+
16+
# Fine-tune models using serverless APIs in Azure AI Foundry
17+
18+
[!INCLUDE [Feature preview](~/reusable-content/ce-skilling/azure/includes/ai-studio/includes/feature-preview.md)]
19+
20+
Azure AI Foundry lets you tailor large language models to your personal datasets by using a process known as *fine-tuning*.
21+
22+
Fine-tuning provides significant value by enabling customization and optimization for specific tasks and applications. It leads to improved performance, cost efficiency, reduced latency, and tailored outputs.
23+
24+
In this article, you learn how to fine-tune models that are deployed via serverless APIs in [Azure AI Foundry](https://ai.azure.com).
25+
26+
27+
## Prerequisites
28+
29+
- An Azure subscription with a valid payment method. Free or trial Azure subscriptions won't work. If you don't have an Azure subscription, create a [paid Azure account](https://azure.microsoft.com/pricing/purchase-options/pay-as-you-go) to begin.
30+
31+
- Access to the [Azure portal](https://portal.azure.com).
32+
33+
- An [Azure AI Foundry project](create-projects.md).
34+
35+
- Azure role-based access controls (Azure RBAC) are used to grant access to operations in Azure AI Foundry portal. To perform the steps in this article, your user account must be assigned the __owner__ or __contributor__ role for the Azure subscription. For more information on permissions, see [Role-based access control in Azure AI Foundry portal](../concepts/rbac-ai-studio.md).
36+
37+
38+
## Find models with fine-tuning support
39+
40+
The AI Foundry model catalog offers fine-tuning support for multiple types of models, including chat completions and text generation. For a list of models that support fine-tuning and the Azure regions of support for fine-tuning, see [region availability for models in serverless API endpoints.](deploy-models-serverless-availability.md).
41+
42+
43+
You can also go to the Azure AI Foundry portal to view all models that contain fine-tuning support:
44+
45+
1. Sign in to [Azure AI Foundry](https://ai.azure.com).
46+
1. If you're not already in your project, select it.
47+
1. Navigate to the model catalog.
48+
1. Select the **Fine-tuning tasks** filter.
49+
50+
:::image type="content" source="../media/how-to/fine-tune/fine-tune-filters.png" alt-text="Screenshot of model catalog finetuning filter options." lightbox="../media/how-to/fine-tune/fine-tune-filters.png":::
51+
52+
1. Select **All** or select a specific task.
53+
54+
55+
56+
## Verify registration of subscription provider
57+
58+
Verify the subscription is registered to the `Microsoft.Network` resource provider.
59+
1. Sign in to the [Azure portal](https://portal.azure.com).
60+
1. Select **Subscriptions** from the left menu.
61+
1. Select the subscription you want to use.
62+
1. Select **Settings** > **Resource providers** from the left menu.
63+
1. Confirm that **Microsoft.Network** is in the list of resource providers. Otherwise add it.
64+
65+
66+
67+
### Prepare data for fine-tuning
68+
69+
Prepare your training and validation data to fine-tune your model. Your training data and validation data sets consist of input and output examples for how you would like the model to perform.
70+
71+
Make sure all your training examples follow the expected format for inference. To fine-tune models effectively, ensure a balanced and diverse dataset by maintaining data balance, including various scenarios, and periodically refining training data to align with real-world expectations. These actions ultimately lead to more accurate and balanced model responses.
72+
73+
> [!TIP]
74+
> Different model types require a different format of training data.
75+
76+
# [Chat completion](#tab/chat-completion)
77+
78+
### Example file format
79+
80+
The supported file type is JSON Lines. Files are uploaded to the default datastore and made available in your project.
81+
82+
```json
83+
{"messages": [{"role": "system", "content": "You are an Xbox customer support agent whose primary goal is to help users with issues they are experiencing with their Xbox devices. You are friendly and concise. You only provide factual answers to queries, and do not provide answers that are not related to Xbox."}, {"role": "user", "content": "Is Xbox better than PlayStation?"}, {"role": "assistant", "content": "I apologize, but I cannot provide personal opinions. My primary job is to assist you with any issues related to your Xbox device. Do you have any Xbox-related issues that need addressing?"}]}
84+
{"messages": [{"role": "system", "content": "You are an Xbox customer support agent whose primary goal is to help users with issues they are experiencing with their Xbox devices. You are friendly and concise. You only provide factual answers to queries, and do not provide answers that are not related to Xbox."}, {"role": "user", "content": "Can I play PC games on my Xbox?"}, {"role": "assistant", "content": "While Xbox consoles don't natively support PC games, they can play an array of Xbox games, many of which are also available on PC. Can I assist you with finding games compatible with your Xbox device?"}]}
85+
{"messages": [{"role": "system", "content": "You are an Xbox customer support agent whose primary goal is to help users with issues they are experiencing with their Xbox devices. You are friendly and concise. You only provide factual answers to queries, and do not provide answers that are not related to Xbox."}, {"role": "user", "content": "I'm having trouble connecting my Xbox to the Wi-Fi."}, {"role": "assistant", "content": "No worries, let's go through the network settings on your Xbox. Can you please tell me what happens when you try to connect it to the Wi-Fi?"}]}
86+
```
87+
88+
# [Text generation](#tab/text-generation)
89+
90+
The training and validation data you use **must** be formatted as a JSON Lines (JSONL) document in which each line represents a single prompt-completion pair.
91+
92+
### Example file format
93+
94+
```json
95+
{"prompt": "<prompt text>", "completion": "<ideal generated text>"}
96+
{"prompt": "<prompt text>", "completion": "<ideal generated text>"}
97+
{"prompt": "<prompt text>", "completion": "<ideal generated text>"}
98+
```
99+
100+
Here are some example datasets on Hugging Face that you can use to fine-tune your model:
101+
- [dair-ai/emotion](https://huggingface.co/datasets/dair-ai/emotion)
102+
103+
:::image type="content" source="../media/how-to/fine-tune/dataset-dair-ai-emotion.png" alt-text="Screenshot of example emotion data on Hugging Face." lightbox="../media/how-to/fine-tune/dataset-dair-ai-emotion.png":::
104+
105+
- [SetFit/mrpc](https://huggingface.co/datasets/SetFit/mrpc)
106+
107+
:::image type="content" source="../media/how-to/fine-tune/dataset-setfit-mrpc.png" alt-text="Screenshot of example Microsoft Research Paraphrase Corpus (MRPC) data on Hugging Face." lightbox="../media/how-to/fine-tune/dataset-setfit-mrpc.png":::
108+
109+
Single text classification requires the training data to include at least two fields such as `text1` and `label`. Text pair classification requires the training data to include at least three fields such as `text1`, `text2`, and `label`.
110+
111+
The supported file type is JSON Lines. Files are uploaded to the default datastore and made available in your project.
112+
113+
---
114+
115+
## Fine-tune a model
116+
117+
1. Choose the model you want to fine-tune from the Azure AI Foundry [model catalog](https://ai.azure.com/explore/models).
118+
119+
1. On the model's **Details** page, select **fine-tune**. Some foundation models support both __Serverless API__ and __Managed compute__, while others support one or the other.
120+
121+
1. If you're presented the options for __Serverless API__ and __Managed compute__, select __Serverless API__ for fine-tuning. This action opens up a wizard that shows information about __pay-as-you-go__ fine-tuning for your model.
122+
123+
> [!NOTE]
124+
> To use a serverless API to fine-tune your model, your project must belong to an available region. Each model has specific region availability. These regions are listed in the fine-tune wizard that opens up. You can also check [region availability](deploy-models-serverless-availability.md) for your chosen model.
125+
126+
1. Select the **Pricing and terms** tab to learn about pricing for the selected model.
127+
1. If you're using a model that's offered through Azure Marketplace, select the link to **Azure Marketplace Terms** from the __Overview__ tab to learn more about the terms of use.
128+
129+
1. If it's your first time fine-tuning the model (for example, Mistral-large-2407) in the project, you must subscribe your project for the particular offering from Azure AI Foundry. This step requires that your account has the Azure subscription permissions and resource group permissions listed in the prerequisites. Each project has its own subscription to the particular Azure AI Studio offering, which allows you to control and monitor spending. Select **Subscribe and fine-tune**.
130+
131+
> [!NOTE]
132+
> Subscribing a project to a particular Azure AI Foundry offering requires that your account has **Contributor** or **Owner** access at the subscription level where the project is created. Alternatively, your user account can be assigned a custom role that has the Azure subscription permissions and resource group permissions listed in the [prerequisites](#prerequisites).
133+
134+
2. Once you sign up the project for the particular Azure AI Foundry offering, subsequent fine-tuning of the _same_ offering in the _same_ project don't require subscribing again. Therefore, you don't need to have the subscription-level permissions for subsequent fine-tune jobs. If this scenario applies to you, select **Continue to fine-tune**.
135+
136+
1. If you're using a Microsoft model (for example, Phi-3.5-mini-instruct), you don't create an Azure Marketplace subscription. Select __Fine-tune__.
137+
138+
1. Enter a name for your fine-tuned model and the optional tags and description.
139+
1. Select training data to fine-tune your model. See [prepare data for fine-tuning](#prepare-data-for-fine-tuning) for more information.
140+
141+
> [!NOTE]
142+
> If you have your training/validation files in a credential-less datastore, you need to allow workspace managed identity access to your datastore in order to proceed with Serverless API fine-tuning with a credential-less storage. On the __Datastore__ page, after selecting __Update authentication__, select the option to use workspace managed identity.
143+
144+
![Use workspace managed identity for data preview and profiling in Azure Machine Learning Foundry.](../media/how-to/fine-tune/phi-3/credentials.png)
145+
146+
1. Select validation data.
147+
1. Specify (optional) task parameters. Task parameters are an optional step and an advanced option. Tuning hyperparameters is essential for optimizing large language models (LLMs) in real-world applications. It allows for improved performance and efficient resource usage. You can choose to keep he default settings or customize parameters like epochs or learning rate.
148+
149+
- __Batch size multiplier__: The batch size to use for training. When set to -1, batch_size is calculated as 0.2% of examples in training set and the max is 256.
150+
- __Learning rate__: The fine-tuning learning rate is the original learning rate used for pretraining multiplied by this multiplier. We recommend experimenting with values between 0.5 and 2. Empirically, we've found that larger learning rates often perform better with larger batch sizes. Must be between 0.0 and 5.0.
151+
- __Epochs__: Number of training epochs. An epoch refers to one full cycle through the data set.
152+
153+
1. Review your selections and select __Submit__ to train your model.
154+
155+
Once your model is fine-tuned, you can deploy it and use it in your own application, in the playground, or in prompt flow. For more information on how to use deployed models, see [How to use Mistral premium chat models](./deploy-models-mistral.md).
156+
157+
158+
---
159+
160+
## Clean up your fine-tuned models
161+
162+
You can delete a fine-tuned model from the fine-tuning model list in [Azure AI Foundry](https://ai.azure.com) or from the model details page. To delete the fine-tuned model from the Fine-tuning page,
163+
164+
1. Select __Fine-tuning__ from the left navigation in your Azure AI Foundry project.
165+
1. Select the __Delete__ button to delete the fine-tuned model.
166+
167+
>[!NOTE]
168+
> You can't delete a custom model if it has an existing deployment. You must first delete your model deployment before you delete your custom model.
169+
170+
## Cost and quota considerations for models deployed as serverless API endpoints
171+
172+
Quota is managed per deployment. Each deployment has a rate limit of 200,000 tokens per minute and 1,000 API requests per minute. However, we currently limit one deployment per model per project. Contact Microsoft Azure Support if the current rate limits aren't sufficient for your scenarios.
173+
174+
#### Cost for Microsoft models
175+
176+
You can find the pricing information on the __Pricing and terms__ tab of the deployment wizard when deploying Microsoft models (such as Phi-3 models) as serverless API endpoints.
177+
178+
#### Cost for non-Microsoft models
179+
180+
Non-Microsoft models deployed as serverless API endpoints are offered through Azure Marketplace and integrated with Azure AI Foundry for use. You can find Azure Marketplace pricing when deploying or fine-tuning these models.
181+
182+
Each time a project subscribes to a given offer from Azure Marketplace, a new resource is created to track the costs associated with its consumption. The same resource is used to track costs associated with inference and fine-tuning; however, multiple meters are available to track each scenario independently.
183+
184+
For more information on how to track costs, see [Monitor costs for models offered through Azure Marketplace](costs-plan-manage.md#monitor-costs-for-models-offered-through-the-azure-marketplace).
185+
186+
:::image type="content" source="../media/deploy-monitor/serverless/costs-model-as-service-cost-details.png" alt-text="A screenshot showing different resources corresponding to different model offers and their associated meters." lightbox="../media/deploy-monitor/serverless/costs-model-as-service-cost-details.png":::
187+
188+
## Sample Notebook
189+
190+
You can use this [sample notebook](https://github.com/Azure/azureml-examples/blob/main/sdk/python/jobs/finetuning/standalone/chat-completion/chat_completion_with_model_as_service.ipynb) to create a standalone fine-tuning job to enhance a model's ability to summarize dialogues between two people using the Samsum dataset. The training data utilized is the ultrachat_200k dataset, which is divided into four splits suitable for supervised fine-tuning (sft) and generation ranking (gen). The notebook employs the available Azure AI models for the chat-completion task (If you would like to use a different model than what's used in the notebook, you can replace the model name). The notebook includes setting up prerequisites, selecting a model to fine-tune, creating training and validation datasets, configuring and submitting the fine-tuning job, and finally, creating a serverless deployment using the fine-tuned model for sample inference.
191+
192+
## Sample CLI
193+
194+
Additionally, you can use this sample CLI to create a standalone fine-tuning job to enhance a model's ability to summarize dialogues between two people using a dataset.
195+
196+
```yaml
197+
type: finetuning
198+
199+
name: "Phi-3-mini-4k-instruct-with-amlcompute"
200+
experiment_name: "Phi-3-mini-4k-instruct-finetuning-experiment"
201+
display_name: "Phi-3-mini-4k-instruct-display-name"
202+
task: chat_completion
203+
model_provider: custom
204+
model:
205+
path: "azureml://registries/azureml/models/Phi-3-mini-4k-instruct/versions/14"
206+
type: mlflow_model
207+
training_data: train.jsonl
208+
validation_data:
209+
path: validation.jsonl
210+
type: uri_file
211+
hyperparameters:
212+
num_train_epochs: "1"
213+
per_device_train_batch_size: "1"
214+
learning_rate: "0.00002"
215+
properties:
216+
my_property: "my_value"
217+
tags:
218+
foo_tag: "bar"
219+
outputs:
220+
registered_model:
221+
name: "Phi-3-mini-4k-instruct-finetuned-model"
222+
type: mlflow_model
223+
```
224+
225+
The training data used is the same as demonstrated in the SDK notebook. The CLI employs the available Azure AI models for the chat-completion task. If you prefer to use a different model than the one in the CLI sample, you can update the arguments, such as 'model path,' accordingly
226+
227+
## Content filtering
228+
229+
Models deployed as a service with pay-as-you-go billing are protected by Azure AI Content Safety. When deployed to real-time endpoints, you can opt out of this capability. With Azure AI content safety enabled, both the prompt and completion pass through an ensemble of classification models aimed at detecting and preventing the output of harmful content. The content filtering system detects and takes action on specific categories of potentially harmful content in both input prompts and output completions. Learn more about [Azure AI Content Safety](../concepts/content-filtering.md).
230+
231+
232+
## Next steps
233+
- [What is Azure AI Foundry?](../what-is-ai-studio.md)
234+
- [Learn more about deploying Mistral models](./deploy-models-mistral.md)
235+
- [Azure AI FAQ article](../faq.yml)
36.7 KB
Loading

articles/ai-studio/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ items:
9898
items:
9999
- name: Fine-tuning overview
100100
href: concepts/fine-tuning-overview.md
101+
- name: Fine-tune with serverless API
102+
href: how-to/fine-tune-serverless.md
101103
- name: Fine-tune with user-managed compute
102104
href: how-to/fine-tune-managed-compute.md
103105
- name: Fine-tune Azure OpenAI models

0 commit comments

Comments
 (0)