Giant language fashions (LLMs) have outstanding capabilities. Nonetheless, utilizing them in customer-facing functions typically requires tailoring their responses to align along with your group’s values and model identification. On this publish, we exhibit easy methods to use direct preference optimization (DPO), a way that lets you fine-tune an LLM with human choice knowledge, along with Amazon SageMaker Studio and Amazon SageMaker Floor Fact to align the Meta Llama 3 8B Instruct mannequin responses to your group’s values.
Utilizing SageMaker Studio and SageMaker Floor Fact for DPO
With DPO, you’ll be able to fine-tune an LLM with human choice knowledge resembling scores or rankings in order that it generates outputs that align to end-user expectations. DPO is computationally environment friendly and helps improve a mannequin’s helpfulness, honesty, and harmlessness, divert the LLM from addressing particular topics, and mitigate biases. On this method, you usually begin with deciding on an current or coaching a brand new supervised fine-tuned (SFT) mannequin. You utilize the mannequin to generate responses and also you collect human suggestions on these responses. After that, you utilize this suggestions to carry out DPO fine-tuning and align the mannequin to human preferences.
Whether or not you’re fine-tuning a pre-trained LLM with supervised fine-tuning (SFT) or loading an current fine-tuned mannequin for DPO, you usually want highly effective GPUs. The identical applies throughout DPO fine-tuning. With Amazon SageMaker, you may get began rapidly and experiment quickly by utilizing managed Jupyter notebooks geared up with GPU situations. You may rapidly get began by making a JupyterLab area in SageMaker Studio, the built-in growth surroundings (IDE) purpose-built for machine studying (ML), launch a JupyterLab utility that runs on a GPU occasion.
Orchestrating the end-to-end knowledge assortment workflow and creating an utility for annotators to price or rank mannequin responses for DPO fine-tuning may be time-consuming. SageMaker Floor Fact provides human-in-the-loop capabilities that aid you arrange workflows, handle annotators, and acquire constant, high-quality suggestions.
This publish walks you thru the steps of utilizing DPO to align an SFT mannequin’s responses to the values of a fictional digital financial institution known as Instance Financial institution. Your pocket book runs in a JupyterLab area in SageMaker Studio powered by a single ml.g5.48xlarge occasion (8 A10G GPUs). Optionally, you’ll be able to select to run this pocket book inside a smaller occasion kind resembling ml.g5.12xlarge (4 A10G GPUs) or ml.g6.12xlarge (4 L4 GPUs) with bitsandbytes quantization. You utilize Meta Llama 3 8B Instruct (the Meta Llama 3 instruction tuned mannequin optimized for dialogue use instances from the Hugging Face Hub) to generate responses, SageMaker Floor Fact to gather choice knowledge, and the DPOTrainer from the HuggingFace TRL library for DPO fine-tuning along with Parameter-Efficient Fine-Tuning (PEFT). You additionally deploy the aligned mannequin to a SageMaker endpoint for real-time inference. You should utilize the identical strategy with different fashions.
Resolution overview
The next diagram illustrates the strategy.
The workflow accommodates the next key steps:
- Load the Meta Llama 3 8B Instruct mannequin into SageMaker Studio and generate responses for a curated set of frequent and poisonous questions. The dataset serves because the preliminary benchmark for the mannequin’s efficiency.
- The generated question-answer pairs are saved in Amazon Easy Storage Service (Amazon S3). These might be introduced to the human annotators later to allow them to rank the mannequin responses.
- Create a workflow in SageMaker Floor Fact to collect human choice knowledge for the responses. This includes creating a piece workforce, designing a UI for suggestions assortment, and establishing a labeling job.
- Human annotators work together with the labeling portal to guage and rank the mannequin’s responses based mostly on their alignment to the group’s values.
- The collected knowledge is processed to stick to the
DPOTraineranticipated format. - Utilizing the Hugging Face TRL library and the
DPOTrainer, fine-tune the Llama 3 mannequin utilizing the processed knowledge from the earlier step. - Take a look at the fine-tuned mannequin on a holdout analysis dataset to evaluate its efficiency and confirm it meets the specified requirements.
- Once you’re happy with the mannequin efficiency, you’ll be able to deploy it to a SageMaker endpoint for real-time inference at scale.
Conditions
To run the answer described on this publish, you will need to have an AWS account arrange, together with an AWS Id and Entry Administration (IAM) position that grants you the required permissions to create and entry the answer assets. If you’re new to AWS and haven’t created an account but, consult with Create a standalone AWS account.
To make use of SageMaker Studio, you have to have a SageMaker area arrange with a person profile that has the required permissions to launch the SageMaker Studio utility. In the event you’re new to SageMaker Studio, the Fast Studio setup is the quickest strategy to get began. With a single click on, SageMaker provisions the required area with default presets, together with establishing the person profile, IAM position, IAM authentication, and public web entry. The pocket book related to this publish assumes the usage of an ml.g5.48xlarge occasion kind. To evaluate or improve your quota limits, navigate to the AWS Service Quotas console, select AWS Companies within the navigation pane, select Amazon SageMaker, and consult with the worth for Studio JupyterLab Apps working on ml.g5.48xlarge situations.

Request a rise in quota worth larger than or equal to 1 for experimentation.

Meta Llama 3 8B Instruct is accessible underneath the Llama 3 license. To obtain the mannequin from Hugging Face, you want an entry token. In the event you don’t have already got one, navigate to the Settings web page on the Hugging Face web site to acquire it.
Make it possible for the SageMaker Studio position has the required permissions for SageMaker Floor Fact and Amazon S3 entry. Once you’re working in SageMaker Studio, you’re already utilizing an IAM position, which you’ll want to switch for launching SageMaker Floor Fact labeling jobs. To allow SageMaker Floor Fact performance, you must connect the AWS managed coverage AmazonSageMakerGroundTruthExecution to your SageMaker Studio position. This coverage supplies the important permissions for creating and managing labeling jobs.
For Amazon S3 entry, scoping permissions to particular buckets and actions enhances safety and aligns with greatest practices. This strategy adheres to the precept of least privilege, lowering potential dangers related to overly permissive insurance policies. The next is an instance of a restricted Amazon S3 coverage that grants solely the required permissions:
So as to add these insurance policies to your SageMaker Studio position, full the next steps:
- On the IAM console, discover and select your SageMaker Studio position (it normally begins with
AmazonSageMaker-ExecutionRole-). - On the Permissions tab, select Add permissions after which Connect insurance policies.
- Seek for and fix
AmazonSageMakerGroundTruthExecution. - Create and fix the customized Amazon S3 inline coverage as proven within the previous instance, if wanted.
Bear in mind to comply with the precept of least privilege, granting solely the permissions mandatory to your particular use case. Frequently evaluate your IAM roles and insurance policies to validate their alignment along with your safety necessities. For extra particulars on IAM insurance policies for SageMaker Floor Fact, consult with Use IAM Managed Insurance policies with Floor Fact.
Arrange the pocket book and surroundings
To get began, open SageMaker Studio and create a JupyterLab area. For Occasion, select ml.g5.48xlarge. Run the area, open JupyterLab, and clone the code within the following GitHub repository. You may configure the JupyterLab area to make use of as much as 100 GB in your Amazon Elastic Block Retailer (Amazon EBS) quantity. As well as, the ml.g5 occasion household comes with NVMe SSD native storage, which you should use within the JupyterLab utility. The NVMe occasion retailer listing is mounted to the appliance container in /mnt/sagemaker-nvme. For this publish, you utilize the NVMe storage out there within the ml.g5.48xlarge occasion.
When your area is prepared, clone the GitHub repo and open the pocket book llama3/rlhf-genai-studio/RLHF-with-Llama3-on-Studio-DPO.ipynb, which accommodates the answer code. Within the pop-up, guarantee that the Python 3 kernel is chosen.

Let’s undergo the pocket book. First, set up the required Python libraries:
The next line units the default path the place you retailer non permanent artifacts to the situation within the NVMe storage:
cache_dir = "/mnt/sagemaker-nvme"
That is native storage, which signifies that your knowledge might be misplaced when the JupyterLab utility is deleted, restarted, or patched. Alternatively, you’ll be able to improve your EBS quantity of your SageMaker Studio area to larger than or equal to 100 GB to supply adequate storage for the Meta Llama 3 base mannequin, PEFT adapter, and new merged fine-tuned mannequin.
Load Meta Llama 3 8B Instruct within the pocket book
After you may have imported the required libraries, you’ll be able to obtain the Meta Llama 3 8B Instruct mannequin and its related tokenizers from Hugging Face:
Acquire preliminary mannequin responses for frequent and poisonous questions
The example_bank_questions.txt file accommodates an inventory of frequent questions acquired by name facilities in monetary organizations mixed with an inventory of poisonous and off-topic questions.
Earlier than you ask the mannequin to generate solutions to those questions, you have to specify the model and core values of Instance Financial institution. You’ll embody these values within the immediate as context later so the mannequin has the suitable data it wants to reply.
Now you’re able to invoke the mannequin. For every query within the file, you assemble a immediate that accommodates the context and the precise query. You ship the immediate to the mannequin 4 occasions to generate 4 completely different outputs and save the ends in the llm_responses.json file.
The next is an instance entry from llm_reponses.json.

Arrange the SageMaker Floor Fact labeling job and human choice knowledge
To fine-tune the mannequin utilizing DPO, you have to collect human choice knowledge for the generated responses. SageMaker Floor Fact helps orchestrate the info assortment course of. It provides customizable labeling workflows and sturdy workforce administration options for rating duties. This part exhibits you easy methods to arrange a SageMaker Floor Fact labeling job and invite a human workforce with requisite experience to evaluate the LLM responses and rank them.
Arrange the workforce
A personal workforce in SageMaker Floor Fact consists of people who’re particularly invited to carry out knowledge labeling duties. These people may be workers or contractors who’ve the required experience to guage the mannequin’s responses. Establishing a non-public workforce helps obtain knowledge safety and high quality by limiting entry to trusted people for knowledge labeling.
For this use case, the workforce consists of the group of people that will rank the mannequin responses. You may arrange a non-public workforce utilizing the SageMaker console by creating a non-public workforce and welcoming members by way of e mail. For detailed directions, consult with Create a Non-public Workforce (Amazon SageMaker Console).
Create the instruction template
With the instruction template, you’ll be able to handle the UI and information human annotators in reviewing mannequin outputs. It wants to obviously current the mannequin responses and supply a simple manner for the annotators to rank them. Right here, you utilize the textual content rating template. This template lets you show the directions for the human reviewer and the prompts with the pregenerated LLM responses. The annotator evaluations the immediate and responses and ranks the latter based mostly on their alignment to the group’s model.
The definition of the template is as follows. The template exhibits a pane on the left with directions from the job requester, a immediate on the prime, and three LLM responses in the principle physique. The precise aspect of the UI is the place the annotator ranks the responses from most to least preferable.
The template is saved domestically in your Studio JupyterLab area EBS quantity as directions.template in a short lived listing. You then add this template file to your designated S3 bucket utilizing s3.upload_file(), putting it within the specified bucket and prefix. This Amazon S3 hosted template might be referenced once you create the SageMaker Floor Fact labeling job, so staff see the proper interface for the textual content rating process.
Preprocess the enter knowledge
Earlier than you create the labeling job, confirm that the enter knowledge matches the format anticipated by SageMaker Floor Fact and is saved as a JSON file in Amazon S3. You should utilize the prompts and responses within the llm_responses.json file to create the manifest file inp-manifest-trank.json. Every row within the manifest file accommodates a JSON object (source-responses pair). The earlier entry now appears like the next code.

Add the structured knowledge to the S3 bucket in order that it may be ingested by SageMaker Floor Fact.
Create the labeling job
Now you’re able to configure and launch the labeling job utilizing the SageMaker API from inside the pocket book. This includes specifying the work workforce, UI template, and knowledge saved within the S3 bucket. By setting acceptable parameters resembling process cut-off dates and the variety of staff per knowledge object, you’ll be able to run jobs effectively and successfully. The next code exhibits easy methods to begin the labeling job:
Because the job is launched, it’s important to observe its progress carefully, ensuring duties are being distributed and accomplished as anticipated.
Collect human suggestions by way of the labeling portal
When the job setup is full, annotators can log in to the labeling portal and begin rating the mannequin responses.

Employees can first seek the advice of the Directions pane to know the duty, then use the principle interface to guage and rank the mannequin’s responses in accordance with the given standards. The next screenshot illustrates the UI.

The human suggestions is collected and saved in an S3 bucket. This suggestions would be the foundation for DPO. With this knowledge, you’ll fine-tune the Meta Llama 3 mannequin and align its responses with the group’s values, bettering its total efficiency.
Align Meta Llama 3 8B Instruct with the DPOTrainer
On this part, we present easy methods to use the choice dataset that you just ready utilizing SageMaker Floor Fact to fine-tune the mannequin utilizing DPO. DPO explicitly optimizes the mannequin’s output based mostly on human evaluations. It aligns the mannequin’s conduct extra carefully with human expectations and improves its efficiency on duties requiring nuanced understanding and contextual appropriateness. By integrating human preferences, DPO enhances the mannequin’s relevance, coherence, and total effectiveness in producing desired responses.
DPO makes it extra easy to preference-tune a mannequin compared to different common methods resembling Proximal Policy Optimization (PPO). DPO eliminates the need for a separate rewards model, thereby avoiding the price related to coaching it. Moreover, DPO requires considerably much less knowledge to realize efficiency corresponding to PPO.
Effective-tuning a language mannequin utilizing DPO consists of two steps:
- Collect a choice dataset with optimistic and unfavourable chosen pairs of technology, given a immediate.
- Maximize the log-likelihood of the DPO loss straight.
To study extra in regards to the DPO algorithm, consult with the next whitepaper.

Anticipated knowledge format
The DPO coach expects a really specific format for the dataset, which accommodates sentence pairs the place one sentence is a selected response and the opposite is a rejected response. That is represented as a Python dictionary with three keys:
- immediate – Consists of the context immediate given to a mannequin at inference time for textual content technology
- chosen – Accommodates the popular generated response to the corresponding immediate
- rejected – Accommodates the response that isn’t most well-liked or shouldn’t be the sampled response for the given immediate
The next perform definition illustrates easy methods to course of the info saved in Amazon S3 to create a DPO dataset utilizing with pattern pairs and a immediate:
Right here is an instance sentence pair:

You cut up the DPO coach dataset into practice and check samples utilizing an 80/20 cut up and tokenize the dataset in preparation for DPO fine-tuning:
Supervised fine-tuning utilizing DPO
Now that the dataset is formatted for the DPO coach, you should use the practice and check datasets ready earlier to provoke the DPO mannequin fine-tuning. Meta Llama 3 8B belongs to a class of small language fashions, however even Meta Llama 3 8B barely suits right into a SageMaker ML occasion like ml.g5.48xlarge in fp16 or fp32, leaving little room for full fine-tuning. You should utilize PEFT with DPO to fine-tune Meta Llama 3 8B’s responses based mostly on human preferences. PEFT is a technique of fine-tuning that focuses on coaching solely a subset of the pre-trained mannequin’s parameters. This strategy includes figuring out crucial parameters for the brand new process and updating solely these parameters throughout coaching. By doing so, PEFT can considerably cut back the computation required for fine-tuning. See the next code:
For a full record of LoraConfig coaching arguments, consult with LoRA. At a excessive stage, you have to initialize the DPOTrainer with the next elements: the mannequin you need to practice, a reference mannequin (ref_model) used to calculate the implicit rewards of the popular and rejected responses, the beta hyperparameter that controls the steadiness between the implicit rewards assigned to the popular and rejected responses, and a dataset containing immediate, chosen, and rejected responses. If ref_model=None, the coach will create a reference mannequin with the identical structure because the enter mannequin to be optimized. See the next code:
When you begin the coaching, you’ll be able to see the standing within the pocket book:

When mannequin fine-tuning is full, save the PEFT adapter mannequin to disk and merge it with the bottom mannequin to create a newly tuned mannequin. You should utilize the saved mannequin for native inference and validation or deploy it as a SageMaker endpoint after you may have gained adequate confidence within the mannequin’s responses.
Consider the fine-tuned mannequin inside a SageMaker Studio pocket book
Earlier than you host your mannequin for inference, confirm that its response optimization aligns with person preferences. You may acquire the mannequin’s response each earlier than and after DPO fine-tuning and examine them aspect by aspect, as proven within the following desk.

The DPO Mannequin Response column signifies the RLHF aligned mannequin’s response post-fine-tuning, and the Rejected Mannequin Response column refers back to the mannequin’s response to the enter immediate previous to fine-tuning.
Deploy the mannequin to a SageMaker endpoint
After you may have gained adequate confidence in your mannequin, you’ll be able to deploy it to a SageMaker endpoint for real-time inference. SageMaker endpoints are absolutely managed and supply auto scaling capabilities. For this publish, we use DJL Serving to host the fine-tuned, DPO-aligned Meta Llama3 8B mannequin. To study extra about internet hosting your LLM utilizing DJL Serving, consult with Deploy giant fashions on Amazon SageMaker utilizing DJLServing and DeepSpeed mannequin parallel inference.
To deploy an LLM straight out of your SageMaker Studio pocket book utilizing DJL Serving, full the next steps:
- Add mannequin weights and different mannequin artifacts to Amazon S3.
- Create a meta-model definition file known as
serving.properties. This definition file dictates how the DJL Serving container is configured for inference.
engine = DeepSpeedpossibility.tensor_parallel_degree = 1possibility.s3url = s3://<MY-TEST-BUCKET>/llama3-dpo-ft/modelweightspossibility.hf_access_token=hf_xx1234
- Create a customized inference file known as
mannequin.py, which defines a customized inference logic:
- Deploy the DPO fine-tuned mannequin as a SageMaker endpoint:
- Invoke the hosted mannequin for inference utilizing the
sageMaker.Predictorclass:
Clear up
After you full your duties within the SageMaker Studio pocket book, keep in mind to cease your JupyterLab workspace to stop incurring extra fees. You are able to do this by selecting Cease subsequent to your JupyterLab area. Moreover, you may have the choice to arrange lifecycle configuration scripts that may robotically shut down assets once they’re not in use.

In the event you deployed the mannequin to a SageMaker endpoint, run the next code on the finish of the pocket book to delete the endpoint:
Conclusion
Amazon SageMaker provides instruments to streamline the method of fine-tuning LLMs to align with human preferences. With SageMaker Studio, you’ll be able to experiment interactively with completely different fashions, questions, and fine-tuning methods. With SageMaker Floor Fact, you’ll be able to arrange workflows, handle groups, and acquire constant, high-quality human suggestions.
On this publish, we confirmed easy methods to improve the efficiency of Meta Llama 3 8B Instruct by fine-tuning it utilizing DPO on knowledge collected with SageMaker Floor Fact. To get began, launch SageMaker Studio and run the pocket book out there within the following GitHub repo. Share your ideas within the feedback part!
Concerning the Authors
Anastasia Tzeveleka is a GenAI/ML Specialist Options Architect at AWS. As a part of her work, she helps clients construct basis fashions and create scalable generative AI and machine studying options utilizing AWS companies.
Pra
nav Murthy is an AI/ML Specialist Options Architect at AWS. He focuses on serving to clients construct, practice, deploy and migrate machine studying (ML) workloads to SageMaker. He beforehand labored within the semiconductor trade creating giant laptop imaginative and prescient (CV) and pure language processing (NLP) fashions to enhance semiconductor processes. In his free time, he enjoys taking part in chess and touring.
Sundar Raghavan is an AI/ML Specialist Options Architect at AWS, serving to clients construct scalable and cost-efficient AI/ML pipelines with Human within the Loop companies. In his free time, Sundar loves touring, sports activities and having fun with outside actions along with his household.

