Views
No views yet
transformers and we advise you to use the latest version of transformers.transformers<4.37.0, you will encounter the following error:KeyError: 'qwen2'apply_chat_template to show you how to load the tokenizer and model and how to generate contents.1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model_name = "Qwen/Qwen2.5-7B-Instruct-1M"
4
5model = AutoModelForCausalLM.from_pretrained(
6 model_name,
7 torch_dtype="auto",
8 device_map="auto"
9)
10tokenizer = AutoTokenizer.from_pretrained(model_name)
11
12prompt = "Give me a short introduction to large language model."
13messages = [
14 {"role": "system", "content": "You are Qwen, created by Alibaba Cloud. You are a helpful assistant."},
15 {"role": "user", "content": prompt}
16]
17text = tokenizer.apply_chat_template(
18 messages,
19 tokenize=False,
20 add_generation_prompt=True
21)
22model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
23
24generated_ids = model.generate(
25 **model_inputs,
26 max_new_tokens=512
27)
28generated_ids = [
29 output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
30]
31
32response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]1git clone -b dev/dual-chunk-attn git@github.com:QwenLM/vllm.git
2cd vllm
3pip install -e . -v1from transformers import AutoTokenizer
2from vllm import LLM, SamplingParams
3
4# Initialize the tokenizer
5tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-7B-Instruct-1M")
6
7# Pass the default decoding hyperparameters of Qwen2.5-7B-Instruct
8# max_tokens is for the maximum length for generation.
9sampling_params = SamplingParams(temperature=0.7, top_p=0.8, repetition_penalty=1.05, max_tokens=512)
10
11# Input the model name or path. See below for parameter explanation (after the example of openai-like server).
12llm = LLM(model="Qwen/Qwen2.5-7B-Instruct-1M",
13 tensor_parallel_size=4,
14 max_model_len=1010000,
15 enable_chunked_prefill=True,
16 max_num_batched_tokens=131072,
17 enforce_eager=True,
18 # quantization="fp8", # Enabling FP8 quantization for model weights can reduce memory usage.
19)
20
21# Prepare your prompts
22prompt = "Tell me something about large language models."
23messages = [
24 {"role": "system", "content": "You are Qwen, created by Alibaba Cloud. You are a helpful assistant."},
25 {"role": "user", "content": prompt}
26]
27text = tokenizer.apply_chat_template(
28 messages,
29 tokenize=False,
30 add_generation_prompt=True
31)
32
33# generate outputs
34outputs = llm.generate([text], sampling_params)
35
36# Print the outputs.
37for output in outputs:
38 prompt = output.prompt
39 generated_text = output.outputs[0].text
40 print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")1vllm serve Qwen/Qwen2.5-7B-Instruct-1M \
2 --tensor-parallel-size 4 \
3 --max-model-len 1010000 \
4 --enable-chunked-prefill --max-num-batched-tokens 131072 \
5 --enforce-eager \
6 --max-num-seqs 1
7
8# --quantization fp8 # Enabling FP8 quantization for model weights can reduce memory usage.--tensor-parallel-size--max-model-len--max-num-batched-tokens--max-num-seqsmax_model_len or increasing the tensor_parallel_size. Alternatively, you can reduce max_num_batched_tokens, although this may significantly slow down inference.gpu_memory_utilization to 0.85 or lower, but be aware that this might reduce the VRAM available for the KV cache.max_model_len.@misc{qwen2.5-1m,
title = {Qwen2.5-1M: Deploy Your Own Qwen with Context Length up to 1M Tokens},
url = {https://qwenlm.github.io/blog/qwen2.5-1m/},
author = {Qwen Team},
month = {January},
year = {2025}
}
@article{qwen2.5,
title={Qwen2.5-1M Technical Report},
author={An Yang and Bowen Yu and Chengyuan Li and Dayiheng Liu and Fei Huang and Haoyan Huang and Jiandong Jiang and Jianhong Tu and Jianwei Zhang and Jingren Zhou and Junyang Lin and Kai Dang and Kexin Yang and Le Yu and Mei Li and Minmin Sun and Qin Zhu and Rui Men and Tao He and Weijia Xu and Wenbiao Yin and Wenyuan Yu and Xiafei Qiu and Xingzhang Ren and Xinlong Yang and Yong Li and Zhiying Xu and Zipeng Zhang},
journal={arXiv preprint arXiv:2501.15383},
year={2025}
}