Qwen2.5-Coder, also known as CodeQwen, is a series of large language models from Alibaba Cloud that specialize in code-related tasks. The series includes six model sizes (0.5B, 1.5B, 3B, 7B, 14B, and 32B) to meet diverse developer needs. Trained on a massive volume of code data, Qwen2.5-Coder excels in code-centric applications while maintaining strong mathematical and reasoning abilities. This tutorial uses the Qwen2.5-Coder-32B-Instruct model as an example.
Model overview
Qwen2.5-Coder is a series of powerful, code-centric models that support a context length of up to 128K tokens and are compatible with 92 programming languages. These models excel at various code-related tasks, including multilingual code generation, code completion, and code repair. The instruction-tuned variant, Qwen2.5-Coder-Instruct, builds on the base model for enhanced performance on these tasks and excellent generalization.
Key features include:
Multilingual programming capability: Qwen2.5-Coder-Instruct performs exceptionally well on the McEval benchmark, which covers over 40 languages, including niche programming languages.
Code reasoning capability: The model achieves impressive results on the CRUXEval benchmark, showcasing its strong code reasoning abilities. This enhanced reasoning also correlates with improved performance on complex instruction-following tasks.
Mathematical capability: As a foundational discipline for coding, mathematics is another area where the model excels, demonstrating its comprehensive capabilities in STEM fields.
General capabilities: Qwen2.5-Coder-Instruct inherits the powerful general capabilities of the Qwen2.5 base model for stability and broad applicability across a wide range of tasks.
Environment requirements
Supported regions
You can use Model Gallery to run this model in the China (Beijing), China (Shanghai), China (Shenzhen), China (Hangzhou), China (Ulanqab), and Singapore regions.
Resource requirements
Model size | Deployment requirements | Training requirements |
Qwen2.5-Coder-0.5B/1.5B | Minimum: a single P4 card. Recommended: a single GU30, A10, V100, or T4 card. | A GPU with 16 GB or more VRAM, such as a T4, P100, or V100. |
Qwen2.5-Coder-3B/7B | Minimum: a single P100, T4, or V100 (gn6v) card. Recommended: a single GU30 or A10 card. | A GPU with 24 GB or more VRAM, such as an A10 or T4. |
Qwen2.5-Coder-14B | Minimum: a single L20 or GU60 card, or two GU30 cards. Recommended: two GU60 or L20 cards. | A GPU with 32 GB or more VRAM, such as a V100. |
Qwen2.5-Coder-32B | Minimum: two GU60 or L20 cards, or four A10 cards. Recommended: four GU60 or L20 cards, or eight V100-32G cards. | A GPU with 80 GB or more VRAM, such as an A800 or H800. |
Deploy and call the model
Deploy model
Go to the Model Gallery page.
Log on to the PAI console.
In the upper-left corner of the top navigation bar, select a region.
In the left-side navigation pane, click Workspaces and then click the name of the workspace you want to open.
In the left-side navigation pane, choose Quick Start > Model Gallery.
On the Model Gallery page, find and click the Qwen2.5-Coder-32B-Instruct model card to go to the model details page.
In the upper-right corner, click Deploy. Accept the default configuration and click OK to deploy the service. The deployment is successful when the status changes to Running.
This tutorial uses public resources, which are billed on a pay-as-you-go basis. To avoid charges, stop or delete the service when you are finished.
Call model
Get invocation information
On the model details page, click View Call Information to view the public endpoint and token.
In the Invocation Information panel that appears, select the Shared Gateway tab and copy the Public Endpoint and Token.
Call by API
The following example shows how to call the model by using the API:
from openai import OpenAI
# 1. Configure the client.
# Replace <EAS_TOKEN> with the Token from the Invocation Information panel.
openai_api_key = "<EAS_TOKEN>"
# Replace <EAS_ENDPOINT> with the Public Endpoint from the Invocation Information panel.
openai_api_base = "<EAS_ENDPOINT>/v1"
client = OpenAI(
api_key=openai_api_key,
base_url=openai_api_base,
)
# 2. Get the model name.
# For BladeLLM, set model = "". BladeLLM does not require the model parameter and does not support using client.models.list() to get the model name. Set it to an empty string to comply with the mandatory parameter requirement of the OpenAI SDK.
models = client.models.list()
model = models.data[0].id
print(model)
# 3. Initiate a chat completion request.
# Both streaming (stream=True) and non-streaming (stream=False) outputs are supported.
stream = True
chat_completion = client.chat.completions.create(
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "hello"},
],
model=model,
top_p=0.8,
temperature=0.7,
max_tokens=1024,
stream=stream,
)
if stream:
for chunk in chat_completion:
print(chunk.choices[0].delta.content, end="")
else:
result = chat_completion.choices[0].message.content
print(result)For more information about API calls, see Call the Model by Using an API.
Call by web UI
You can also call the model by using a web UI. For detailed instructions, see the model card.
After the model deployment is complete, you can download the client code from the Web Application section of the model card details page and run python webui_client.py --eas_endpoint "<EAS API Endpoint>" --eas_token "<EAS API Token>" to start the local web application for inference. The EAS API Endpoint and EAS API Token can be obtained from the details of the deployed EAS service. Run python webui_client.py -h to view more parameter descriptions.
Model fine-tuning
Model Gallery provides two out-of-the-box fine-tuning algorithms for the Qwen2.5-Coder-32B-Instruct model: supervised fine-tuning (SFT) and direct preference optimization (DPO).
Supervised fine-tuning (SFT)
The SFT training algorithm supports input in JSON format, where each data entry consists of an instruction and an output, represented by the "instruction" and "output" fields, respectively. For example:
[
{
"instruction": "Create a function to calculate the sum of a sequence of integers.",
"output": "# Python code\ndef sum_sequence(sequence):\n sum = 0\n for num in sequence:\n sum += num\n return sum"
},
{
"instruction": "Generate a Python code for crawling a website for a specific type of data.",
"output": "import requests\nimport re\n\ndef crawl_website_for_phone_numbers(website):\n response = requests.get(website)\n phone_numbers = re.findall('\\d{3}-\\d{3}-\\d{4}', response.text)\n return phone_numbers\n \nif __name__ == '__main__':\n print(crawl_website_for_phone_numbers('www.example.com'))"
}
]Direct preference optimization (DPO)
The DPO training algorithm supports input in JSON format. Each data entry consists of a prompt, a chosen response, and a rejected response, which are represented by the "prompt", "chosen", and "rejected" fields, respectively. For example:
[
{
"prompt": "Create a function to calculate the sum of a sequence of integers.",
"chosen": "# Python code\ndef sum_sequence(sequence):\n sum = 0\n for num in sequence:\n sum += num\n return sum",
"rejected": "[x*x for x in [1, 2, 3, 5, 8, 13]]"
},
{
"prompt": "Generate a Python code for crawling a website for a specific type of data.",
"chosen": "import requests\nimport re\n\ndef crawl_website_for_phone_numbers(website):\n response = requests.get(website)\n phone_numbers = re.findall('\\d{3}-\\d{3}-\\d{4}', response.text)\n return phone_numbers\n \nif __name__ == '__main__':\n print(crawl_website_for_phone_numbers('www.example.com'))",
"rejected": "def remove_duplicates(string): \n result = \"\" \n prev = '' \n\n for char in string:\n if char != prev: \n result += char\n prev = char\n return result\n\nresult = remove_duplicates(\"AAABBCCCD\")\nprint(result)"
}
]On the Model Gallery page, click the Qwen2.5-Coder-32B-Instruct model card to go to the model details page.
On the model details page, click Train in the upper-right corner. Configure the following parameters:
Dataset configuration: After preparing your data, upload it to an Object Storage Service (OSS) bucket or select a dataset on NAS or CPFS by specifying a dataset object. You can also use a public dataset provided by PAI to submit a job and test the algorithm.
Computing resource configuration: The algorithm requires a GPU with 80 GB or more VRAM. Ensure that you have sufficient computing resources within your selected resource quota. For the resource specifications required by other model sizes, see Environment requirements.
Hyperparameter configuration: The following table lists the training hyperparameters. You can adjust these values or use the defaults.
Click Train. Model Gallery automatically redirects you to the job details page, where you can monitor the training status and view logs.
The trained model is automatically registered in AI Asset - Model Management, where you can view or deploy the model. For more information, see Register and Manage Models.
Model evaluation
PAI provides a built-in evaluation algorithm for the Qwen2.5-Coder-32B-Instruct model. Use this algorithm to assess the performance of the original model and your fine-tuned versions. For more information, see Model Evaluation and Best Practices for Large Language Model Evaluation.
Model compression
After training, you can quantize (compress) the model before deployment to reduce its storage and computing requirements. For more information, see Model Compression.