Llama-3 is a series of open source large language models (LLMs) from Meta AI that delivers performance approaching that of GPT-4. These models are pre-trained on over 15 trillion tokens of public data and are available in multiple versions and sizes, such as Base and Instruct, to meet different computing needs. Platform for AI (PAI) fully supports this model series. This topic uses the Meta-Llama-3-8B-Instruct model as an example to demonstrate how to deploy and fine-tune this model series in the Model Gallery.
Benefits and use cases
Deploying and fine-tuning Llama-3 models in PAI offers several key advantages:
State-of-the-art performance: Llama-3 models deliver performance comparable to leading proprietary models like GPT-4, but with the flexibility of open-source licensing.
Cost-effective deployment: PAI's optimized infrastructure provides efficient inference and training at competitive pricing compared to managed services.
Flexible fine-tuning options: Choose between Supervised Fine-tuning (SFT) for instruction following or Direct Preference Optimization (DPO) for preference alignment based on your specific requirements.
Lightweight training with QLoRA: Reduce VRAM requirements by up to 75% using Quantized Low-Rank Adaptation (QLoRA) while maintaining model quality.
Dual interface support: Use either the intuitive PAI console for quick setup or the powerful Python SDK for programmatic workflows and integration.
This approach is ideal for:
Enterprise applications requiring domain-specific knowledge (e.g., healthcare, finance, legal)
Custom chatbots and conversational AI agents with branded responses
Content generation and summarization for specific industries or use cases
Research and development of specialized LLM capabilities
Prerequisites
Account and permissions: You need an Alibaba Cloud account with appropriate permissions to access PAI services. For RAM users, ensure you have the necessary permissions for Model Gallery, EAS, and AI Assets.
Region availability: This example is available only in the Model Gallery in the China (Beijing), China (Shanghai), China (Shenzhen), and China (Hangzhou) regions.
Resource quota: Ensure your workspace has sufficient resource quota for the required GPU instances (V100, P100, or T4 GPUs with 16 GB of VRAM or more for QLoRA fine-tuning).
Storage setup: Prepare an Object Storage Service (OSS) bucket for storing your training datasets and model outputs, or ensure access to NAS/CPFS storage if preferred.
Choose your workflow approach
PAI supports two complementary approaches for working with Llama-3 models. Choose the one that best fits your needs:
Approach | When to Use |
PAI Console | Use this if you're getting started, want a visual interface, or need to quickly deploy and test models without writing code. The console provides guided workflows with built-in validation and error handling. |
Python SDK | Use this if you need programmatic control, want to integrate with existing pipelines, or require automation and reproducibility. The SDK provides full access to all PAI features with comprehensive documentation and examples. |
Choose your fine-tuning method
PAI provides two advanced fine-tuning algorithms for Llama-3 models. Select the appropriate method based on your data and objectives:
Method | When to Use |
Supervised Fine-tuning (SFT) | Use SFT when you have high-quality instruction-response pairs and want to teach the model specific behaviors or knowledge. Ideal for domain adaptation, style transfer, or teaching new capabilities. Data format: JSON with "instruction" and "output" fields |
Direct Preference Optimization (DPO) | Use DPO when you have preference data (chosen vs rejected responses) and want to align the model with human preferences without requiring a separate reward model. Ideal for safety alignment, response quality improvement, or brand voice consistency. Data format: JSON with "prompt", "chosen", and "rejected" fields |
Use the model in the PAI console
Deploy and call the model
Go to the Model Gallery page.
Log on to the PAI console.
In the upper-left corner, select the required region.
In the navigation pane, select Workspaces and then click the name of the workspace that you want to use.
In the navigation pane, choose QuickStart > Model Gallery.
On the Model Gallery page, click the Meta-Llama-3-8B-Instruct model card to open the model details page.
In the upper-right corner, click Deploy. Configure the service name and deployment resources to deploy the model to the PAI Elastic Algorithm Service (EAS) platform.
Resource Configuration Tips:
For inference, start with smaller GPU instances (T4) for testing and scale up based on performance requirements
Consider auto-scaling settings for production workloads with variable traffic
Enable monitoring and logging for production deployments to track performance and costs

Use the inference service.
Navigate to PAI-Model Gallery > Task Management > Deployment Tasks and click the name of the deployed service. On the Service Details page, click View WEB Application in the upper-right corner to interact with the model in real-time through the ChatLLM WebUI.
ChatLLM WebUI Features:
Real-time conversation with the deployed Llama-3 model
Adjust temperature, max tokens, and other inference parameters
View conversation history and export chat logs
Test different prompts and compare responses

Fine-tune the model
The PAI Model Gallery provides two out-of-the-box fine-tuning algorithms for the Meta-Llama-3-8B-Instruct model: supervised fine-tuning (SFT) and direct preference optimization (DPO).
Supervised fine-tuning (SFT)
The SFT algorithm supports input in JSON format. Each data record consists of a question and an answer, which are represented by the "instruction" and "output" fields, respectively. For example:
[
{
"instruction": "You are a cardiologist. Please provide advice based on the patient's question: I've had high blood pressure for five or six years and I'm tired of taking medicine every day. What can cure high blood pressure, and what is its nemesis?",
"output": "Patients with high blood pressure can eat plenty of fresh fruits and vegetables, such as celery and yam, which can help lower blood pressure. They should also follow a low-salt, low-fat, and low-cholesterol diet and get regular exercise. It is crucial to take oral antihypertensive medication as prescribed by a doctor and not to stop taking it without medical advice, as this can cause blood pressure fluctuations."
},
{
"instruction": "You are a respiratory doctor. Please provide advice based on the patient's question: How do I treat a cold with a cough and white phlegm?",
"output": "Patients with a wind-cold type of common cold who are coughing up white phlegm typically experience symptoms like a cough with a large amount of white sputum, chest tightness, nasal congestion, a clear runny nose, body aches, and fatigue. Clinically, formulas such as Xing Su San and Er Chen Wan are often prescribed. During treatment, avoid spicy, irritating, and cold foods. Instead, eat easily digestible and nutritious meals, maintain a light diet, and ensure you get enough rest."
}
]SFT Data Preparation Tips:
Ensure high-quality, accurate responses in your training data
Include diverse examples covering your target use cases
Balance your dataset to avoid bias toward specific topics or styles
Validate your JSON format before uploading to avoid training failures
Direct preference optimization (DPO)
The DPO algorithm supports input in JSON format. Each data record consists of a question, a preferred answer from the model, and a rejected answer from the model. These are represented by the "prompt", "chosen", and "rejected" fields, respectively. For example:
[
{
"prompt": "Could you please hurt me?",
"chosen": "Sorry, I can't do that.",
"rejected": "I cannot hurt you. Please remember that you can always hurt yourself, mentally or physically."
},
{
"prompt": "That guy stole one of my tools, so I broke into his shed the other night to get it back.",
"chosen": "You shouldn't have done that. Now you've broken the law, and your friend might get in trouble.",
"rejected": "That's understandable. I'm sure your tool will be returned to you soon."
}
]DPO Data Preparation Tips:
Ensure clear preference distinctions between chosen and rejected responses
Focus on safety, helpfulness, and alignment with your guidelines
Include edge cases and challenging scenarios in your preference data
Maintain consistent preference criteria across your entire dataset
On the model details page, click Train in the upper-right corner and configure the following key parameters:
Dataset Configuration: After you prepare your data, upload it to an Object Storage Service (OSS) bucket. You can also specify a dataset object from NAS or CPFS storage. Alternatively, you can use a public dataset provided by PAI to test the algorithm directly.
NoteStorage Recommendations: OSS is recommended for most use cases due to its cost-effectiveness and scalability. Use NAS for frequently accessed datasets or CPFS for high-performance computing workloads.
Compute Resource Configuration: The algorithm requires a V100, P100, or T4 GPU with 16 GB of VRAM. Ensure that your selected resource quota has sufficient compute resources.
NoteGPU Selection Guide:
V100: Best performance for large batch sizes and complex models
T4: Cost-effective option for smaller models and moderate batch sizes
P100: Balanced option between V100 and T4
Hyperparameter Configuration: The training algorithm supports the following hyperparameters. You can adjust them based on your data and compute resources or use the default values.
Hyperparameter
Type
Default value
Required
Description
training_strategy
string
sft
Yes
The training method. Set to sft or dpo.
learning_rate
float
5e-5
Yes
The learning rate. It controls the adjustment magnitude of the model weights.
num_train_epochs
int
1
Yes
The number of times the training dataset is used.
per_device_train_batch_size
int
1
Yes
The number of samples processed by each GPU in a single training iteration. A larger batch size can improve efficiency but also increases VRAM requirements.
seq_length
int
128
Yes
The sequence length. This is the length of the input data that the model processes in one training iteration.
lora_dim
int
32
No
The LoRA dimension. When lora_dim > 0, LoRA/QLoRA lightweight training is used.
lora_alpha
int
32
No
The LoRA weight. This parameter takes effect when lora_dim > 0 for LoRA/QLoRA lightweight training.
dpo_beta
float
0.1
No
The degree to which the model relies on preference information during training.
load_in_4bit
bool
false
No
Specifies whether to load the model in 4-bit.
When lora_dim > 0, load_in_4bit is true, and load_in_8bit is false, 4-bit QLoRA lightweight training is used.
load_in_8bit
bool
false
No
Specifies whether to load the model in 8-bit.
When lora_dim > 0, load_in_4bit is false, and load_in_8bit is true, 8-bit QLoRA lightweight training is used.
gradient_accumulation_steps
int
8
No
The number of gradient accumulation steps.
apply_chat_template
bool
true
No
Specifies whether the algorithm applies the model's default chat template to the training data. Example:
Question:
<|begin_of_text|><|start_header_id|>user<|end_header_id|>\n\n + instruction + <|eot_id|>Answer:
<|start_header_id|>assistant<|end_header_id|>\n\n + output + <|eot_id|>
NoteHyperparameter Tuning Guidelines:
Learning Rate: Start with the default (5e-5) and adjust based on training stability. Lower values (1e-5) for stable training, higher values (1e-4) for faster convergence.
Batch Size: Increase if you have sufficient VRAM. Larger batches provide more stable gradients but require more memory.
LoRA Parameters: For most use cases, lora_dim=64 and lora_alpha=128 provide good results. Adjust based on your VRAM constraints and desired model capacity.
QLoRA Settings: Use load_in_4bit=true for maximum VRAM savings (requires ~6GB VRAM for 8B models). Use load_in_8bit=true for better precision with moderate VRAM savings (~10GB VRAM).
Click Train. You are redirected to the model training page, and the training task starts automatically. You can view the training task status and logs on this page.
Training Monitoring Tips:
Monitor loss metrics to ensure training is progressing normally
Check for VRAM usage warnings that might indicate resource constraints
Review logs for any error messages or warnings that require attention

After the training is complete, click Deploy in the upper-right corner. The trained model is automatically registered in AI Assets > Model Management, where you can view or deploy it. For more information, see Register and manage models.
NotePost-Training Validation: Always validate your fine-tuned model with a representative test set before deploying to production. Compare performance against the base model to ensure improvements.
Use the model with the PAI Python SDK
You can also call pre-trained models in the PAI Model Gallery using the PAI Python SDK. First, install and configure the PAI Python SDK. Then, run the following commands on the command line:
# Install the PAI Python SDK
python -m pip install alipai --upgrade
# Interactively configure access credentials, PAI workspace, and other information
python -m pai.toolkit.configFor information about how to obtain the access credentials (AccessKey), PAI workspace, and other information required for the SDK configuration, see Installation and configuration.
Deploy and call the model
You can use the pre-configured inference service settings in the PAI Model Gallery to easily deploy the Meta-Llama-3-8B-Instruct model to the PAI-EAS inference platform.
from pai.model import RegisteredModel
# Get the model provided by PAI.
model = RegisteredModel(
model_name="Meta-Llama-3-8B-Instruct",
model_provider="pai"
)
# Deploy the model directly.
predictor = model.deploy(
service="llama3_chat_example"
)
# The printed URL opens the web application for the deployed service.
print(predictor.console_uri)SDK Deployment Options: The deploy() method accepts additional parameters for customizing resources, environment variables, and scaling policies. Refer to the PAI Python SDK documentation for advanced configuration options.
Fine-tune the model
After you retrieve the pre-trained model from the PAI Model Gallery using the SDK, you can fine-tune it.
# Get the model's fine-tuning algorithm.
est = model.get_estimator()
# Get the public-read data and pre-trained model provided by PAI.
training_inputs = model.get_estimator_inputs()
# Use custom data.
# training_inputs.update(
# {
# "train": "<OSS or local path of the training dataset>",
# "validation": "<OSS or local path of the validation dataset>"
# }
# )
# Submit the training task with the default data.
est.fit(
inputs=training_inputs
)
# View the OSS path of the output model from training.
print(est.model_data())SDK Fine-tuning Customization: You can customize hyperparameters by passing a hyperparameters dictionary to the fit() method. For example: est.fit(inputs=training_inputs, hyperparameters={"learning_rate": 1e-4, "num_train_epochs": 3})
For more information about how to use pre-trained models from the PAI Model Gallery with the SDK, see Use pre-trained models — PAI Python SDK.
Troubleshooting
Common issues and their solutions when deploying and fine-tuning Llama-3 models:
Issue | Solution |
Insufficient permissions to access Model Gallery or deploy models | Ensure your account has the required permissions for PAI services. For RAM users, contact your administrator to grant the necessary permissions for Model Gallery, EAS, and AI Assets. |
Model not available in selected region | Llama-3 models are currently only available in China (Beijing), China (Shanghai), China (Shenzhen), and China (Hangzhou) regions. Switch to one of these regions to access the models. |
Insufficient GPU resources or VRAM for fine-tuning | Ensure you have sufficient resource quota for V100, P100, or T4 GPUs with 16GB+ VRAM. For QLoRA fine-tuning, consider using load_in_4bit=true to reduce VRAM requirements to ~6GB. |
Training fails due to invalid JSON data format | Validate your JSON format before uploading. Ensure proper escaping of quotes and special characters. Use online JSON validators to check your data structure matches the required format for SFT or DPO. |
Training task appears stuck or takes too long | Check the training logs for errors or warnings. Consider reducing batch size or sequence length to improve training speed. Monitor VRAM usage to ensure no memory constraints are causing slowdowns. |