Views
No views yet




1# Create the conda environment
2conda create -n qwenlongl1 python==3.10
3conda activate qwenlongl1
4
5# Install requirements
6pip3 install -r requirements.txt
7
8# Install verl
9cd verl
10pip3 install -e .
11
12# Install vLLM
13pip3 install vllm==0.7.3
14
15# Install flash-attn
16pip3 install flash-attn --no-build-isolation1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model_name = "Tongyi-Zhiwen/QwenLong-L1-32B"
4
5# load the tokenizer and the model
6tokenizer = AutoTokenizer.from_pretrained(model_name)
7model = AutoModelForCausalLM.from_pretrained(
8 model_name,
9 torch_dtype="auto",
10 device_map="auto"
11)
12
13# prepare the model input
14template = """Please read the following text and answer the question below.
15
16<text>
17$DOC$
18</text>
19
20$Q$
21
22Format your response as follows: "Therefore, the answer is (insert answer here)"."""
23context = "<YOUR_CONTEXT_HERE>"
24question = "<YOUR_QUESTION_HERE>"
25prompt = template.replace('$DOC$', context.strip()).replace('$Q$', question.strip())
26messages = [
27 # {"role": "system", "content": "You are QwenLong-L1, created by Alibaba Tongyi Lab. You are a helpful assistant."}, # Use system prompt to define identity when needed.
28 {"role": "user", "content": prompt}
29]
30text = tokenizer.apply_chat_template(
31 messages,
32 tokenize=False,
33 add_generation_prompt=True
34)
35model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
36
37# conduct text completion
38generated_ids = model.generate(
39 **model_inputs,
40 max_new_tokens=10000,
41 temperature=0.7,
42 top_p=0.95
43)
44output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
45
46# parsing thinking content
47try:
48 # rindex finding 151649 (</think>)
49 index = len(output_ids) - output_ids[::-1].index(151649)
50except ValueError:
51 index = 0
52
53thinking_content = tokenizer.decode(output_ids[:index], skip_special_tokens=True).strip("
54")
55content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("
56")
57
58print("thinking content:", thinking_content)
59print("content:", content)transformers and llama.cpp for local use, vllm and sglang for deployment. In general, there are two approaches to enabling YaRN for supported frameworks:config.json file, add the rope_scaling fields:1{
2 ...,
3 "rope_scaling": {
4 "rope_type": "yarn",
5 "factor": 4.0,
6 "original_max_position_embeddings": 32768
7 }
8}llama.cpp, you need to regenerate the GGUF file after the modification.vllm, you can usevllm serve ... --rope-scaling '{"rope_type":"yarn","factor":4.0,"original_max_position_embeddings":32768}' --max-model-len 131072 sglang, you can usepython -m sglang.launch_server ... --json-model-override-args '{"rope_scaling":{"rope_type":"yarn","factor":4.0,"original_max_position_embeddings":32768}}'llama-server from llama.cpp, you can usellama-server ... --rope-scaling yarn --rope-scale 4 --yarn-orig-ctx 32768[!IMPORTANT] If you encounter the following warningUnrecognized keys in `rope_scaling` for 'rope_type'='yarn': {'original_max_position_embeddings'}please upgradetransformers>=4.51.0.
[!NOTE] All the notable open-source frameworks implement static YaRN, which means the scaling factor remains constant regardless of input length, potentially impacting performance on shorter texts. We advise adding therope_scalingconfiguration only when processing long contexts is required. It is also recommended to modify thefactoras needed. For example, if the typical context length for your application is 65,536 tokens, it would be better to setfactoras 2.0.
[!NOTE] If the average context length does not exceed 32,768 tokens, we do not recommend enabling YaRN in this scenario, as it may potentially degrade model performance.
./datasets/ for training and evaluation.1export CUDA_VISIBLE_DEVICES=0
2
3vllm serve "Qwen/Qwen2.5-1.5B-Instruct" \
4 --host 0.0.0.0 \
5 --port 235471export PROJ_DIR="<YOUR_PROJ_DIR_HERE>"
2export MASTER_IP="<YOUR_MASTER_IP_HERE>" # ray master ip
3export NNODES=4 # total GPU nodes
4export NODE_RANK=${RANK} # rank of current node
5export PORT=6382
6export WANDB_API_KEY="<YOUR_WANDB_API_KEY_HERE>"
7export WANDB_PROJECT="QwenLong-L1"
8export LLM_JUDGE=Y # 'Y': LLM JUDGE, 'N': RULE BASED
9export VLLM_ATTENTION_BACKEND=FLASH_ATTN
10# verifier
11export VERIFIER_PATH="Qwen/Qwen2.5-1.5B-Instruct"
12export VERIFIER_HOST="<YOUR_VERIFIER_HOST_HERE>"
13export VERIFIER_PORT="23547"
14
15ray_start_retry() {
16 while true; do
17 ray start --address="${MASTER_IP}:${PORT}"
18 if [ $? -eq 0 ]; then
19 break
20 fi
21 echo "Failed to connect to master, retrying in 5 seconds..."
22 sleep 5
23 done
24}
25
26check_ray_status() {
27 until ray status >/dev/null 2>&1; do
28 echo "Waiting for Ray cluster to be ready..."
29 sleep 5
30 done
31}
32
33if [ "$RANK" == "0" ]; then
34 echo "Starting HEAD node..."
35 ray start --head --port=${PORT}
36
37 check_ray_status
38 echo "Ray head node started successfully"
39
40else
41 echo "Starting WORKER node..."
42 ray_start_retry
43
44 check_ray_status
45 echo "Successfully joined Ray cluster"
46fi
47
48if [ "$RANK" == "0" ]; then
49 bash ${PROJ_DIR}/scripts/rl_4nodes_dapo.sh 2>&1 | tee ${PROJ_DIR}/logs/rl_log_$(date +%Y%m%d_%H%M%S).txt &
50else
51 sleep 30d
52fi
53
54wait1# Step 1. Serve the model for evaluation
2export CUDA_VISIBLE_DEVICES="0,1,2,3,4,5,6,7"
3MODEL_NAME="QwenLong-L1-32B"
4MODEL_PATH="Tongyi-Zhiwen/QwenLong-L1-32B"
5
6vllm serve ${MODEL_PATH} \
7 --port 23547 \
8 --api-key "token-abc123" \
9 --tensor-parallel-size 8 \
10 --gpu-memory-utilization 0.95 \
11 --max_model_len 131072 \
12 --trust-remote-code
13
14# Step 2. Generate model responses for each dataset
15export SERVE_HOST="<YOUR_SERVE_HOST_HERE>" # e.g., 127.0.0.1
16export SERVE_PORT="23547"
17PROJ_DIR="<YOUR_PROJ_DIR_HERE>"
18DATA="<YOUR_DATA_HERE>" # e.g., docmath, frames, 2wikimqa, hotpotqa, musique, narrativeqa, pasper
19python ${PROJ_DIR}/eval/${DATA}.py \
20 --save_dir "${PROJ_DIR}/eval/results/${DATA}" \
21 --save_file "${MODEL_NAME}" \
22 --model "${MODEL_PATH}" \
23 --tokenizer "${MODEL_PATH}" \
24 --n_proc 16 \
25 --api "openai"
26
27# Step 3. Verify model responses for each dataset
28export VERIFIER_API="<YOUR_API_KEY_HERE>"
29export VERIFIER_URL="https://api.deepseek.com/v1"
30PROJ_DIR="<YOUR_PROJ_DIR_HERE>"
31DATA="<YOUR_DATA_HERE>" # e.g., docmath, frames, 2wikimqa, hotpotqa, musique, narrativeqa, pasper
32python ${PROJ_DIR}/eval/${DATA}_verify.py \
33 --save_dir "${PROJ_DIR}/results/${DATA}" \
34 --save_file \"${MODEL_NAME}\" \
35 --judge_model \"deepseek-chat\" \
36 --batch_size 20| DingTalk | |
|---|---|
![]() | ![]() |
@article{wan2025qwenlongl1,
title={QwenLong-L1: : Towards Long-Context Large Reasoning Models with Reinforcement Learning},
author={Fanqi Wan, Weizhou Shen, Shengyi Liao, Yingcheng Shi, Chenliang Li, Ziyi Yang, Ji Zhang, Fei Huang, Jingren Zhou, Ming Yan},
journal={arXiv preprint arXiv:2505.17667},
year={2025}
}