← AI Practitioner study path

Applications with foundation models · 11 min

Fine-tuning, reinforcement and distillation in Amazon Bedrock

In this lesson you will learn how supervised fine-tuning, continued pre-training, reinforcement fine-tuning, RLHF and distillation work, what data each one needs and how that data is prepared.

Fine-tuning and its variants are the techniques that change the weights of a , that is, the numbers it learned, after its first training. That first training uses a huge amount of general unlabeled data, where nobody tells the model what the correct answer is and the model derives its own labels from the data. For example, the model reads a sentence with the last word covered and learns to guess it, without a person telling it the answer.

AWS calls that self-supervised learning, and with it the model comes out knowing a little about everything, so these techniques are used to adapt it to a task or a domain, which is a field of work with its own vocabulary. For example, a general model knows what a claim is, but it doesn't know the billing, service and cancellation categories that an insurance company uses.

What is supervised fine-tuning?

Supervised fine-tuning takes a model that already exists and continues training it with labeled data, that is, with input and output pairs. With those pairs, the model learns what output is expected for each type of input, and in the process its parameters are adjusted. For example, the insurance company gives it thousands of claims, each with the category an employee assigned to it, such as "I was charged twice this month" with the billing category.

Supervised fine-tuning in Amazon Bedrock: base model, data in S3, customization job and custom model

In Amazon Bedrock you upload a .jsonl file to and start a customization job, whose result is a custom model that exists only in your account. The base model doesn't change, because Bedrock creates a tuned copy. That copy is used in two ways: with Provisioned Throughput, which is paid by the hour, or with an on-demand deployment, which is paid per .

A .jsonl file is a text file where each line is an example, written as a list of fields with a name and a value. For tasks that are not conversations, such as classifying or summarizing, each line has a prompt field with the input and a completion field with the expected output, while chat models use the message format of the Converse . For example, a line from the insurance company would have the text of the claim in prompt and the word billing in completion.

Types of fine-tuning named on the exam

The three types of fine-tuning in the exam guide: instruction tuning, domain adaptation and transfer learning

The exam guide names three types of fine-tuning:

  • Instruction tuning, which uses prompt and response pairs written as instructions, such as "Classify this claim".
  • Domain adaptation, which gives the model text from an industry so that it handles its jargon and which SageMaker JumpStart recommends when prompt engineering is not enough.
  • Transfer learning, which reuses a model trained on one task for a similar one, with much less data, and which is the idea that all fine-tuning is based on.

The three types differ in what they give the model. For example, the insurance company does instruction tuning if it trains with pairs such as "Classify this claim" and its category, and it does domain adaptation if it gives the model insurance texts so it understands words like deductible or covered loss.

What is continued pre-training?

Continued pre-training continues the provider's pre-training with your own unlabeled documents, such as contracts, medical literature or manuals. The Amazon Nova 2 guide says it is worth it with large volumes, on the order of tens of billions of tokens. After this pre-training, the model needs instruction tuning to be able to use what it learned. For example, a model that read all of the insurance company's policies already knows their vocabulary, but it still has to be taught, with instruction and response pairs, to answer when a customer asks it something.

Which technique uses this data?

0 of 4

Tap the column that matches each dataset.

  1. 8,000 claims, each with its correct category

  2. Ten years of unlabeled legal case files

  3. Programming prompts with no answer, and a function that checks whether the code compiles

  4. Question and answer pairs written as instructions

What is reinforcement fine-tuning?

Reinforcement fine-tuning needs a way to grade responses instead of labeled pairs. In this technique you give the model prompts, the model generates several responses for each one and a reward function gives each response a score, for example 0.2, 0.9 or 0.5 points. With those scores, Bedrock trains the model so that the responses with more points become more likely.

Reinforcement fine-tuning in Amazon Bedrock: the model generates several responses and a reward function grades them

The reward function can be code in or a judge model, which is another model that you ask to do the grading. AWS recommends this technique when you can measure whether a response is good, but getting labeled examples is expensive. For example, if the insurance company wants the model to calculate a claim's refund, a program can check whether the figure matches the policy's rules and give more points to the correct responses, without anyone writing thousands of answers by hand.

cloud path tip: the documentation advises starting with 100 to 200 examples and measuring the base model first, because if it already gets more than 95 % of the reward, you may not need the tuning.

RLHF

RLHF (reinforcement learning from human feedback) applies the same idea with people. The model gives several responses to the same prompt and people choose which one they prefer. Those preferences are used to train a separate reward model, and the model is tuned to aim for the responses that this reward model scores best. For example, the model writes two responses for an upset customer and a person chooses the one that sounds kinder and clearer. With many choices like that, the model learns to answer the way people prefer.

What is distillation?

Distillation transfers what a large, accurate model knows, which is called the teacher, to a smaller, faster and cheaper one, which is called the student, for your use case. To distill, you provide your prompts in a .jsonl file or your application's , and Bedrock generates the teacher's responses and uses them to tune the student. For example, the insurance company already classifies its claims well with a large model, but each response is slow and costly. With distillation, the large model answers its claims and a small model learns from those responses to classify them the same way.

Model distillation in Amazon Bedrock: teacher, generated responses and student

If Bedrock uses its data synthesis techniques to generate the teacher's responses, those calls are charged at the teacher's on-demand rate. The distilled model is yours alone, since only you have access to it, and AWS says it doesn't use your data to train other publicly available models.

Which technique is the right one?

A company uses a large model in Amazon Bedrock that responds well for its use case, but it is slow and expensive. It wants a smaller one with similar quality for that same use case.

How is the data prepared?

The five aspects of training data: curation, labeling, size, representativeness and governance

All of these techniques depend on the data the model is trained with. The exam guide asks you to take care of five aspects of that data, which the AWS generative AI decision guide explains:

  • Curation, which calls for relevant, accurate and consistent data.
  • Labeling, which has to be of good quality and done by experts if needed.
  • Size, which has to provide enough examples, and when there are too few, the dataset is augmented or you start from a pretrained model.
  • Representativeness, which calls for data balanced across categories, because with 900 billing claims and 20 cancellation claims the model will see billing everywhere.
  • Governance, which consists of knowing where the data comes from, whether you can use it and whether it contains personal data.

RLHF also fits into this preparation, because in that technique what gets prepared is human preferences. Those preferences come from people who choose among several responses, so they need the same care as labels. For example, if the people choosing don't know the policies, they may prefer a response that is kind but wrong, and the model learns that mistake.

On the exam

In questions about these techniques, the type of data in the question points to the answer:

  • "Labeled examples" or "input and output pairs", which point to supervised fine-tuning.
  • "Unlabeled domain data", which points to continued pre-training.
  • "Reward function" or "grading responses without labels", which point to reinforcement fine-tuning, and to RLHF when the grading comes from people.
  • "Large model that works but is expensive", which points to distillation.

The Provisioned Throughput page still says you have to purchase it to use a custom model, even though on-demand deployment also exists today. That is why, if a question asks you how to use a custom model, look for Provisioned Throughput among the options. For example, if the question says that a company already tuned its model and wants to use it in its application, the option with Provisioned Throughput is the one that matches that page.

cloud path tip: in an account on the new AWS experience you won't be able to try fine-tuning or reinforcement fine-tuning, because the Bedrock documentation offers them in us-east-1 and us-west-2 and that account stays in the us-east-2 . In a full account you choose the model's Region.

Review question

A law firm has millions of unlabeled documents and wants a foundation model to learn the vocabulary of its field. Which technique applies?

Next