Files

175 lines
5.5 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# TI Coding Agent Data Prep
这个仓库用于整理 `nvidia/Open-SWE-Traces` 的数据准备流程,目标是把原先分散在 probe 目录里的脚本项目化,形成三个清晰阶段:
1. `probing`:检查数据结构、统计唯一 problem/repo、比较 tokenizer、统计 token。
2. `filtering`:对原始 trajectory 做 hard filter/audit筛掉明显不适合作为 SFT 训练样本的轨迹。
3. `repurposing`:把筛选后的轨迹拆解成阶段化子任务,或导出为 ModelScope-SWIFT / pi-mono 相关格式。
## 目录结构
```text
.
├── data/ # 本地数据目录,不进 git
│ └── Open-SWE-Traces/ # 建议放 nvidia/Open-SWE-Traces clone 或下载结果
├── docs/
│ └── legacy_subproblem_decomposition_README.md
├── runs/ # 所有脚本输出目录,不进 git
├── scripts/
│ ├── probing/ # 探查、统计、tokenizer/token 相关脚本
│ ├── filtering/ # native trace audit / hard filter
│ └── repurposing/ # 子问题拆解、SWIFT 导出、pi-mono 格式转换
├── src/ti_coding_agent_data_prep/
│ └── openswe/ # 共享常量和路径 helper
├── pyproject.toml
├── README.md
└── SKILL.md
```
## 环境部署
建议只使用 repo-local 环境,避免污染共享机器:
```bash
cd /ssd/workspace/yi/ti_coding_agent_data_prep
export http_proxy=http://100.72.0.101:8888
export https_proxy=http://100.72.0.101:8888
export HTTP_PROXY=http://100.72.0.101:8888
export HTTPS_PROXY=http://100.72.0.101:8888
export HF_ENDPOINT=https://hf-mirror.com
uv venv .venv --python 3.10
source .venv/bin/activate
uv pip install -e '.[dev]'
```
如果不用 `uv`,也可以用普通 venv 后执行 `pip install -e '.[dev]'`
## 数据准备
默认所有脚本都从 `data/Open-SWE-Traces` 读取数据。可以把已有数据软链进来:
```bash
cd /ssd/workspace/yi/ti_coding_agent_data_prep
ln -s /ssd/workspace/yi/openswetraces_probe/Open-SWE-Traces data/Open-SWE-Traces
```
## Probing 入口
检查样本结构:
```bash
python scripts/probing/inspect_sample.py
```
统计 unique instance/repo并随机拆 20 条 fine-grained subproblem
```bash
python scripts/probing/analyze_and_decompose.py
```
比较 Qwen tokenizer
```bash
python scripts/probing/compare_qwen_tokenizers.py
```
精确统计 Qwen token 数,输出会持续写入 `runs/native_trace_audit/qwen_exact_token_count.json`
```bash
python scripts/probing/count_qwen_tokens_exact_parallel.py \
--input-root data/Open-SWE-Traces \
--output runs/native_trace_audit/qwen_exact_token_count.json \
--model Qwen/Qwen3-32B \
--workers 12
```
## Filtering 入口
对 native trajectory 做 hard filter/audit
```bash
python scripts/filtering/audit_native_traces.py \
--input-root data/Open-SWE-Traces \
--output-dir runs/native_trace_audit
```
主要 hard filter 覆盖:
- malformed tool call JSON
- tool call/result 对不上
- tool 名或参数被模型输出污染
- final patch 为空或不是合理 diff
- patch 文件和 trajectory 明显不一致
- trajectory 过长
- 重复、无推进的 tool loop
- unresolved 且明显走偏
## Repurposing 入口
把 fine-grained 子问题合并为粗阶段:
```bash
python scripts/repurposing/coarse_decompose.py
```
### 模式 A全量 hard-filter-kept 训练数据
该模式会扫描 Open-SWE-Traces 四个 split 的全部样本,使用
`scripts/filtering/audit_native_traces.py` 中同一套 hard filter 规则,导出所有未触发
hard filter 的样本。按当前数据和规则,预期规模约为 190k 条。
全量导出使用流式 JSONL 写入,避免把约 190k 条长 trajectory 全部放进内存:
```bash
python scripts/repurposing/build_swift_full_kept.py \
--input-root data/Open-SWE-Traces \
--output-dir runs/training_full_kept_swift
```
如果需要同时写 gzip
```bash
python scripts/repurposing/build_swift_full_kept.py \
--input-root data/Open-SWE-Traces \
--output-dir runs/training_full_kept_swift \
--write-gzip
```
### 模式 B5k+500 probe/测试数据
该模式只用于快速训练链路和数据质量 probing不代表全量训练集。
构建 5k ModelScope-SWIFT training probe每个 source config 取 1,250 条 hard-filter-kept 样本:
```bash
python scripts/repurposing/build_swift_training_probe_5k.py
```
构建 500 条 validation split每个 source config 额外随机取 125 条,并排除 5k train 中的
`trajectory_id`
```bash
python scripts/repurposing/build_swift_validation_500.py
```
SWIFT 导出的关键策略:
- MiniMax 样本按 thinking 模式处理,`reasoning_content` 会包成 `<think>...</think>` 放入 assistant content。
- Qwen 样本按 non-thinking 模式处理,不主动加入 reasoning 内容;异常非空 reasoning 会计数。
- `system``user``tool` message 标记为 `loss=false`,只让 assistant 输出参与 loss。
尝试转换到 pi-mono 风格消息:
```bash
python scripts/repurposing/convert_openswe_to_pi_mono.py \
--input-root data/Open-SWE-Traces \
--output-root runs/pi_mono_converted
```
注意pi-mono 转换脚本保留为研究/兼容入口。由于不同 scaffold 的 system prompt、tool schema、tool call 语义不同,最安全的训练路径仍是优先保留 native trace 格式并筛掉坏样本。
## 输出产物
`runs/``data/` 默认不进 git。推荐把所有大文件、parquet、jsonl、token 统计、audit report 都留在这两个目录下。