Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 159 additions & 0 deletions openseek/competition/LongContext-ICL-Annotation/src/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
# 长上下文ICL自动数据标注方案

本方案基于Qwen3-4B大语言模型,采用In-Context Learning (ICL) 范式完成8个数据集的自动标注任务。

## 环境要求

- Python >= 3.8
- Huawei Ascend 910C GPU
- vLLM推理引擎 (支持Ascend)

## 安装依赖

```bash
pip install -r requirements.txt
```

## 模型准备

下载Qwen3-4B模型到指定路径(默认为 `/root/Qwen3-4B`):

- HuggingFace: https://huggingface.co/Qwen/Qwen3-4B
- ModelScope: https://modelscope.cn/models/Qwen/Qwen3-4B

## 启动vLLM服务

在使用本方案前,需要先启动vLLM推理服务:

```bash
# 使用vLLM Ascend版本启动服务
vllm serve /root/Qwen3-4B \
--port 9010 \
--dtype auto \
--max-model-len 131072 \
--tensor-parallel-size 1 \
--gpu-memory-utilization 0.95 \
--block-size 16 \
--trust-remote-code
```

服务启动后,API地址为:`http://localhost:9010/v1/`

## 数据准备

确保数据集已放置在正确路径:

```
/root/OpenSeek/openseek/competition/LongContext-ICL-Annotation/data/
```

## 运行标注任务

### 单个任务运行

```bash
python3 main.py \
--task_id 1 \
--tokenizer_path /root/Qwen3-4B \
--log_path_prefix ./outputs/ \
--max_input_length 128000
```

参数说明:
- `--task_id`: 任务ID (1-8)
- `--tokenizer_path`: Qwen3-4B模型路径
- `log_path_prefix`: 输出文件路径前缀
- `--max_input_length`: 最大输入长度(默认128000)

### 批量运行所有任务

```bash
#!/bin/bash
OUTPUT_DIR="./outputs"
TOKENIZER_PATH="/root/Qwen3-4B"

for task_id in 1 2 3 4 5 6 7 8; do
python3 main.py \
--task_id $task_id \
--log_path_prefix $OUTPUT_DIR/ \
--tokenizer_path $TOKENIZER_PATH
done
```

### 并行运行(4个任务同时)

```bash
#!/bin/bash
OUTPUT_DIR="./outputs"
TOKENIZER_PATH="/root/Qwen3-4B"
CONCURRENT=4

for task_id in 1 2 3 4 5 6 7 8; do
python3 main.py \
--task_id $task_id \
--log_path_prefix $OUTPUT_DIR/ \
--tokenizer_path $TOKENIZER_PATH > task${task_id}.log 2>&1 &

if [ $((task_id % $CONCURRENT)) -eq 0 ]; then
wait
fi
done
wait
```

## 输出结果

每个任务会生成一个 `.jsonl` 文件,格式如下:

```json
{"test_sample_id": "1", "prediction": "Good Review"}
{"test_sample_id": "2", "prediction": "Bad Review"}
```

## 方案特点

1. **长上下文支持**:使用50个ICL示例,通过YARN RoPE scaling支持最长131,072 tokens
2. **提示工程**:优化的prompt设计,确保输出格式准确(使用<label>标签)
3. **高效推理**:基于vLLM引擎,支持OpenAI兼容API
4. **Ascend优化**:针对华为Ascend NPU进行了专门优化

## 技术方案说明

### 1. 提示策略设计
- 结构化prompt模板,明确角色定义和任务规则
- 使用`<label>`标签确保输出格式统一
- 鼓励内部推理过程,但只输出最终结果

### 2. 上下文构造
- 动态选择ICL示例,基于token长度控制上下文大小
- 使用Qwen3-4B原生tokenizer精确计算token数量
- 最多使用50个示例,平衡性能与上下文长度

### 3. 一致性保证
- 统一的prompt模板确保不同任务的输出格式一致
- 多轮对话场景下,使用相同的系统提示和示例选择策略
- 可扩展的架构设计,便于添加新任务

## 测试API连接

可以使用 `api_test.py` 测试vLLM服务是否正常运行:

```bash
python3 api_test.py
```

## 故障排除

1. **内存不足**:减少 `--max-model-len` 或 `--gpu-memory-utilization`
2. **推理速度慢**:增加 `--tensor-parallel-size`(如果有多张GPU)
3. **输出格式错误**:检查prompt中的<label>标签使用是否正确

## 版本信息

- Qwen3-4B
- vLLM 0.13.0+ascend
- Python 3.11.13

## 联系方式

如有问题,请通过比赛官方渠道联系。
41 changes: 25 additions & 16 deletions openseek/competition/LongContext-ICL-Annotation/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@

# from method import build_prompt, select_examples, annotate

from method import build_prompt, select_examples
from method import build_prompt, select_examples, build_prompt_cot

from method import annotate_nvidia as annotate # For Nvidia GPU
# from method import annotate_ascend as annotate # For Huawei Ascend
# from method import annotate_nvidia as annotate # For Nvidia GPU
from method import annotate_ascend as annotate # For Huawei Ascend

TASK_FILES = {
1: './data/openseek-1_closest_integers.json',
2: './data/openseek-2_count_nouns_verbs.json',
3: './data/openseek-3_collatz_conjecture.json',
4: './data/openseek-4_conala_concat_strings.json',
5: './data/openseek-5_semeval_2018_task1_tweet_sadness_detection.json',
6: './data/openseek-6_mnli_same_genre_classification.json',
7: './data/openseek-7_jeopardy_answer_generation_all.json',
8: '../data/openseek-8_kernel_generation.json',
1: '/root/OpenSeek/openseek/competition/LongContext-ICL-Annotation/data/openseek-1_closest_integers.json',
2: '/root/OpenSeek/openseek/competition/LongContext-ICL-Annotation/data/openseek-2_count_nouns_verbs.json',
3: '/root/OpenSeek/openseek/competition/LongContext-ICL-Annotation/data/openseek-3_collatz_conjecture.json',
4: '/root/OpenSeek/openseek/competition/LongContext-ICL-Annotation/data/openseek-4_conala_concat_strings.json',
5: '/root/OpenSeek/openseek/competition/LongContext-ICL-Annotation/data/openseek-5_semeval_2018_task1_tweet_sadness_detection.json',
6: '/root/OpenSeek/openseek/competition/LongContext-ICL-Annotation/data/openseek-6_mnli_same_genre_classification.json',
7: '/root/OpenSeek/openseek/competition/LongContext-ICL-Annotation/data/openseek-7_jeopardy_answer_generation_all.json',
8: '/root/OpenSeek/openseek/competition/LongContext-ICL-Annotation/data/openseek-8_kernel_generation.json',
Comment on lines +13 to +20

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Hardcoding absolute paths like /root/OpenSeek/... makes the code non-portable and prone to failure on other environments. Since the data directory is located relative to the script directory, it is highly recommended to construct these paths dynamically using os.path.

Suggested change
1: '/root/OpenSeek/openseek/competition/LongContext-ICL-Annotation/data/openseek-1_closest_integers.json',
2: '/root/OpenSeek/openseek/competition/LongContext-ICL-Annotation/data/openseek-2_count_nouns_verbs.json',
3: '/root/OpenSeek/openseek/competition/LongContext-ICL-Annotation/data/openseek-3_collatz_conjecture.json',
4: '/root/OpenSeek/openseek/competition/LongContext-ICL-Annotation/data/openseek-4_conala_concat_strings.json',
5: '/root/OpenSeek/openseek/competition/LongContext-ICL-Annotation/data/openseek-5_semeval_2018_task1_tweet_sadness_detection.json',
6: '/root/OpenSeek/openseek/competition/LongContext-ICL-Annotation/data/openseek-6_mnli_same_genre_classification.json',
7: '/root/OpenSeek/openseek/competition/LongContext-ICL-Annotation/data/openseek-7_jeopardy_answer_generation_all.json',
8: '/root/OpenSeek/openseek/competition/LongContext-ICL-Annotation/data/openseek-8_kernel_generation.json',
1: os.path.abspath(os.path.join(os.path.dirname(__file__), '../data/openseek-1_closest_integers.json')),
2: os.path.abspath(os.path.join(os.path.dirname(__file__), '../data/openseek-2_count_nouns_verbs.json')),
3: os.path.abspath(os.path.join(os.path.dirname(__file__), '../data/openseek-3_collatz_conjecture.json')),
4: os.path.abspath(os.path.join(os.path.dirname(__file__), '../data/openseek-4_conala_concat_strings.json')),
5: os.path.abspath(os.path.join(os.path.dirname(__file__), '../data/openseek-5_semeval_2018_task1_tweet_sadness_detection.json')),
6: os.path.abspath(os.path.join(os.path.dirname(__file__), '../data/openseek-6_mnli_same_genre_classification.json')),
7: os.path.abspath(os.path.join(os.path.dirname(__file__), '../data/openseek-7_jeopardy_answer_generation_all.json')),
8: os.path.abspath(os.path.join(os.path.dirname(__file__), '../data/openseek-8_kernel_generation.json'))

}

def parser_args():
Expand All @@ -30,7 +30,7 @@ def parser_args():
default='../outputs/',
help='Prefix path to save the evaluation logs.')
parser.add_argument('--tokenizer_path', type=str,
default='/share/project/wuhaiming/spaces/data_agent/OpenSeek-main/openseek/competition/LongContext-ICL-Annotation/src/Qwen3-4B')
default='/root/Qwen3-4B')
args = parser.parse_args()
return args

Expand All @@ -48,7 +48,7 @@ def evaluate(task_id:int,

task_name = task_dict['task_name']
task_description = task_dict['Definition'][0]
icl_examples = task_dict['examples'][:100]
icl_examples = task_dict['examples'][:50]
test_samples = task_dict['test_samples']

version = 1
Expand All @@ -70,7 +70,16 @@ def evaluate(task_id:int,


text2annotate = test_sample['input']
prompt = build_prompt(task_description, text2annotate)

# Use CoT prompt for Task 3 and 4, standard prompt for others (Account 3 strategy)
# Task 3: Collatz conjecture (math reasoning) - CoT helps
# Task 4: String concatenation - CoT significantly helped in Account 2 (+29.2%)
# Task 8: Kernel generation - CoT was harmful in Account 2 (6.0% -> 0.6%)
if task_id in [3, 4]:
prompt = build_prompt_cot(task_description, text2annotate, task_id)
else:
prompt = build_prompt(task_description, text2annotate)

if examples_str is None:
examples_str = select_examples(icl_examples, task_description, text2annotate)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Pass the loaded qwen_tokenizer to select_examples to avoid redundant tokenizer loading and to respect the --tokenizer_path command-line argument.

Suggested change
examples_str = select_examples(icl_examples, task_description, text2annotate)
examples_str = select_examples(icl_examples, task_description, text2annotate, qwen_tokenizer)

input_prompt = prompt.replace("[[EXAMPLES]]\n\n", examples_str+'\n\n')
Expand All @@ -79,9 +88,9 @@ def evaluate(task_id:int,
# if tokenized_input['input_ids'].shape[1] > max_input_length:
# test_record['prediction'] = None
# else:
# prediction = annotate(input_prompt)
# prediction = annotate(input_prompt, task_id)
# test_record['prediction'] = prediction
prediction = annotate(input_prompt)
prediction = annotate(input_prompt, task_id)
test_record['prediction'] = prediction
with open(output_file, 'a') as f:
f.write(json.dumps(test_record)+'\n')
Expand Down
Loading