From 68108a9b3ae1aa53af73982ae31833ef4131280c Mon Sep 17 00:00:00 2001 From: HPUhushicheng Date: Sat, 30 May 2026 21:42:28 +0800 Subject: [PATCH] kima 's Technical report and code Signed-off-by: HPUhushicheng --- .../submissions/HPUhushicheng/README.md | 294 ++++++++ .../HPUhushicheng/Technical-report-kima.pdf | Bin 0 -> 183801 bytes .../HPUhushicheng/qwen3_experiment/main.py | 186 +++++ .../HPUhushicheng/qwen3_experiment/method.py | 707 ++++++++++++++++++ .../qwen3_experiment/package_results.sh | 25 + .../qwen3_experiment/requirements.txt | 20 + .../HPUhushicheng/qwen3_experiment/run_all.sh | 32 + 7 files changed, 1264 insertions(+) create mode 100644 openseek/competition/LongContext-ICL-Annotation/submissions/HPUhushicheng/README.md create mode 100644 openseek/competition/LongContext-ICL-Annotation/submissions/HPUhushicheng/Technical-report-kima.pdf create mode 100644 openseek/competition/LongContext-ICL-Annotation/submissions/HPUhushicheng/qwen3_experiment/main.py create mode 100644 openseek/competition/LongContext-ICL-Annotation/submissions/HPUhushicheng/qwen3_experiment/method.py create mode 100644 openseek/competition/LongContext-ICL-Annotation/submissions/HPUhushicheng/qwen3_experiment/package_results.sh create mode 100644 openseek/competition/LongContext-ICL-Annotation/submissions/HPUhushicheng/qwen3_experiment/requirements.txt create mode 100644 openseek/competition/LongContext-ICL-Annotation/submissions/HPUhushicheng/qwen3_experiment/run_all.sh diff --git a/openseek/competition/LongContext-ICL-Annotation/submissions/HPUhushicheng/README.md b/openseek/competition/LongContext-ICL-Annotation/submissions/HPUhushicheng/README.md new file mode 100644 index 00000000..7727281d --- /dev/null +++ b/openseek/competition/LongContext-ICL-Annotation/submissions/HPUhushicheng/README.md @@ -0,0 +1,294 @@ +# 超长上下文场景中LLM自动数据标注挑战赛 + +--- + +## 快速开始 + +### 1. 环境准备 + +```bash +# 创建 conda 环境 +conda create -n qwen3_annotation python=3.10 -y +conda activate qwen3_annotation + +# 安装依赖 +pip install -r requirements.txt +``` + +### 2. 安装 FlagScale(源码安装) + +FlagScale 需从 GitHub 源码安装: + +```bash +git clone https://github.com/flagos-ai/FlagScale.git +cd FlagScale +pip install -e . +cd .. +``` + +验证安装: + +```bash +pip list | grep flagscale +# 应输出: flagscale 1.0.0 +``` + +### 3. 模型权重 + +下载 Qwen3-4B 模型权重至本地目录,例如 `/path/to/Qwen3-4B`: + +``` +Qwen3-4B/ +├── config.json +├── tokenizer.json +├── tokenizer_config.json +├── model-00001-of-00003.safetensors +├── model-00002-of-00003.safetensors +├── model-00003-of-00003.safetensors +└── ... +``` + +### 4. 长文本配置(YaRN RoPE Scaling) + +本方案使用 YaRN rope scaling 扩展上下文窗口至 **128K tokens**。需在 `config.json` 中配置: + +```json +"rope_scaling": { + "rope_type": "yarn", + "factor": 4.0, + "original_max_position_embeddings": 32768 +} +``` + +### 5. 模型部署 + +#### 方式一:使用 FlagScale 启动 + +```bash +cd /path/to/FlagScale +python run.py \ + --config-path /path/to/LongContext-ICL-Annotation/src \ + --config-name llm_config \ + action=run +``` + +#### 方式二:直接使用 vLLM 启动(推荐) + +```bash +# 设置模型路径 +MODEL_PATH=/path/to/Qwen3-4B + +# 启动 vLLM 推理服务 +python -m vllm.entrypoints.openai.api_server \ + --host 0.0.0.0 \ + --port 2026 \ + --model ${MODEL_PATH} \ + --gpu-memory-utilization 0.90 \ + --trust-remote-code \ + --max-model-len 40000 \ + --max-num-batched-tokens 40000 \ + --max-num-seqs 4 \ + --enable-prefix-caching +``` + +参数说明: + +| 参数 | 说明 | 推荐值 | +|------|------|--------| +| `--host` | 监听地址 | 0.0.0.0 | +| `--port` | 服务端口 | 2026 | +| `--gpu-memory-utilization` | GPU 显存利用率 | 0.90 | +| `--max-model-len` | 最大模型输入长度 | 40000 | +| `--max-num-batched-tokens` | 最大批处理 token 数 | 40000 | +| `--max-num-seqs` | 最大并发序列数 | 4 | +| `--enable-prefix-caching` | 启用前缀缓存 | 开启 | + +#### 验证服务 + +```bash +curl http://0.0.0.0:2026/v1/completions \ + -H "Content-Type: application/json" \ + -d '{ + "model": "/path/to/Qwen3-4B", + "prompt": "Hello, who are you?", + "max_tokens": 50 + }' +``` + +#### 停止服务 + +```bash +# 方式一:FlagScale 停止 +cd /path/to/FlagScale +python run.py \ + --config-path /path/to/LongContext-ICL-Annotation/src \ + --config-name llm_config \ + action=stop + +# 方式二:直接 kill vLLM 进程 +pkill -f "vllm.entrypoints.openai.api_server" +``` + +### 6. 运行标注 + +进入 `qwen3_experiment/` 目录后执行: + +#### 单任务运行 + +```bash +cd qwen3_experiment + +# 运行单个任务(例如 Task 1) +python main.py \ + --task_id 1 \ + --max_input_length 128000 \ + --tokenizer_path /path/to/Qwen3-4B \ + --log_path_prefix ./outputs/ \ + --max_examples 100 +``` + +参数说明: + +| 参数 | 说明 | 默认值 | +|------|------|--------| +| `--task_id` | 任务 ID(1-8) | 必填 | +| `--max_input_length` | 最大输入长度(tokens) | 128000 | +| `--tokenizer_path` | Qwen3-4B tokenizer 路径 | /root/autodl-tmp/qwen3-4b | +| `--log_path_prefix` | 输出结果目录 | ./outputs/ | +| `--num_samples` | 多次采样次数(>1 启用投票) | 1 | +| `--max_examples` | 最大 ICL 示例数 | 100 | +| `--no_alignment` | 禁用结构对齐后处理 | False | + +#### 批量运行所有任务 + +```bash +cd qwen3_experiment +bash run_all.sh /path/to/Qwen3-4B ./outputs +``` + +### 7. 打包结果 + +```bash +cd qwen3_experiment +bash package_results.sh ./outputs result.zip +``` + +打包后的 `result.zip` 包含 8 个 JSONL 文件(`openseek-1-v1.jsonl` ~ `openseek-8-v1.jsonl`),每个文件包含 `test_sample_id` 和 `prediction` 两个字段。 + +--- + +## 方案说明 + +### 核心改进 + +本方案针对超长上下文 ICL 数据标注的三个核心问题进行了优化: + +#### 1. 上下文压缩与结构对齐(Context Compression & Structure Alignment) + +提出了一种轻量级的 **上下文压缩与结构对齐** 方法,通过以下技术提升标注质量: + +- **上下文压缩**:将 ICL 示例压缩为紧凑的结构化模板,减少冗余信息对模型推理的干扰 +- **结构对齐**:在模型输出后,通过结构对齐后处理,将原始输出与预计算的结构化模板进行匹配,确保输出格式的一致性和准确性 +- **模板匹配**:利用 TF-IDF 加权结构哈希和输出模式归一化,实现高效的模板匹配 + + +#### 2. 超长上下文下的 Prompt 设计 + +- **任务感知的 Prompt 模板**:为 6 种任务类型(数学推理、语言分析、分类、代码/字符串、开放生成、代码生成)分别设计了定制化的 Prompt 模板 +- **结构化 Prompt 架构**:Role → Task → Instructions → Examples → Input → Output 的分层结构 +- **输出格式约束**:在 Prompt 中明确要求 `