the-stack-go / stackgo /README.md
s0duku's picture
Add generic end-to-end dataset builder and smoke-tested tooling
370fed4 verified
|
Raw History Blame Contribute Delete
9.22 kB
# stackgo
`stackgo` 是 The Stack v2 代码数据流水线和仓库级上下文样本工具。它把数据获取、星数区间筛选、正文下载、多区间合并导出和样本生成拆成可复现的阶段,并将 `stackbuild` 作为同一项目中的仓库扫描与样本生成库。
## 流程
1. 下载 The Stack v2 的 repository-grouped file ID 索引(不含源码正文)。
2. 从索引物化任意严格星数区间,例如 `star_events_count > 10` 或 `1 < star_events_count < 11`。
3. 从 Software Heritage 按 `blob_id` 下载并 SHA-1 校验正文。
4. 将一个或多个正文下载目录合并成统一布局,并按 `(shard, blob_id)` 去重。
5. 将每个仓库快照传入 `stackbuild`,使用固定 DeepSeek V4 Flash Base tokenizer 和原生 repo/file 边界,生成真实 8K token 预算的 ZSTD Parquet 依赖上下文样本。
星数是索引快照中的 `star_events_count`,不是实时 GitHub star;`> 10` 不包含等于 10。索引没有 commit、issue 或 PR 历史。正文下载遵守 The Stack 和 Software Heritage 的访问条款,并保留原始许可信息。
上游说明:[The Stack v2 IDs](https://huggingface.co/datasets/bigcode/the-stack-v2-train-full-ids) 和 [官方正文下载流程](https://github.com/bigcode-project/the-stack-v2/blob/main/the_stack/2_download_files/get_file_contents.py)。批量访问 Software Heritage 前应遵守其条款并按上游要求联系 `datasets@softwareheritage.org`;匿名 S3 可访问不代表免除条款。完整数据预估约 143 GiB 原始索引长度,建议为 Parquet、状态库和临时文件预留至少约 200 GiB。
## 安装
```bash
cd stackgo
python -m venv .venv
.venv/bin/pip install -e '.[test]'
```
## 获取数据
```bash
stackgo-download-index --output-dir /mnt/data/the-stack-v2-train-full-ids
stackgo-filter-star10 \
--input-dir /mnt/data/the-stack-v2-train-full-ids \
--output-dir /mnt/data/the-stack-v2-train-full-ids-star-gt10
stackgo-download-contents \
--index-dir /mnt/data/the-stack-v2-train-full-ids-star-gt10 \
--output-dir /mnt/data/the-stack-v2-contents-star-gt10 \
--already-filtered --workers 16
stackgo-export \
--source /mnt/data/the-stack-v2-contents-star-gt10 \
--output /mnt/data/the-stack-go
```
任意区间筛选使用 `stackgo-filter-stars`;正文下载器也支持相同的严格上下界:
```bash
stackgo-filter-stars \
--input-dir /mnt/data/the-stack-v2-train-full-ids \
--output-dir /mnt/data/the-stack-v2-train-full-ids-star-2-10 \
--min-stars-exclusive 1 --max-stars-exclusive 11
stackgo-download-contents \
--index-dir /mnt/data/the-stack-v2-train-full-ids-star-2-10 \
--output-dir /mnt/data/the-stack-v2-contents-star-2-10 \
--already-filtered --workers 16
```
### 构建 `star_events_count > 1`
如果已有 `>10` 数据,可增量下载 `2–9` 和精确 `10`,再合并,避免重下 `>10` 正文:
```bash
# 精确 10 星:9 < star_events_count < 11
stackgo-download-contents \
--index-dir /mnt/data/the-stack-v2-train-full-ids \
--output-dir /mnt/data/the-stack-v2-contents-star-10 \
--min-stars-exclusive 9 --max-stars-exclusive 11 --workers 8
stackgo-export \
--source /mnt/data/the-stack-v2-contents-star-gt10 \
--source /mnt/data/the-stack-v2-contents-star-2-9 \
--source /mnt/data/the-stack-v2-contents-star-10 \
--output /mnt/data/the-stack-go-ge2 \
--star-events-count-exclusive 1 \
--expected-repositories 3246224 \
--expected-files 81112563
stackgo-build-samples --root /mnt/data/the-stack-go-ge2 --jobs 4
```
合并导出会检查各来源是否下载完成、星数区间是否连续、仓库是否重复,并校验最终仓库数和文件引用数。`stackgo-export --source` 可重复传入;只有一个来源时仍保持原有的硬链接导出模式。
也可以用一个可恢复入口完成“等待下载、合并校验、样本生成”:
```bash
stackgo-build-ge2 --wait --jobs 4
```
默认读取 `the-stack-v2-contents-star-gt10`、`the-stack-v2-contents-star-2-9` 和 `the-stack-v2-contents-star-10`,输出 `the-stack-go-ge2/`。合并阶段中断后可重跑,已完成且来源身份一致的分片会跳过;样本阶段同样会跳过已完成分片。
### 任意 star 规则端到端构建
`stackgo-build-dataset` 从原始索引开始串联索引下载(缺失时)、区间过滤、正文下载、有限/无限区间导出,并可继续生成样本:
```bash
stackgo-build-dataset \
--min-stars-exclusive 5 \
--max-stars-exclusive 11 \
--output /mnt/data/the-stack-go-stars-6-10 \
--workers 64 \
--with-samples --jobs 32
```
不传 `--max-stars-exclusive` 表示只有严格下界,例如 `--min-stars-exclusive 1` 表示 `>1`。已有索引时可加 `--skip-index-download` 复用 `--index-dir`。冒烟测试可以限制源分片和正文请求:
```bash
stackgo-build-dataset \
--min-stars-exclusive 10000 \
--max-stars-exclusive 20000 \
--output /tmp/stackgo-smoke \
--skip-index-download --max-source-shards 1 --max-files 3 \
--with-samples --jobs 1 --sample-max-repositories 1
```
The index is gated on Hugging Face: accept its dataset terms and configure `HF_TOKEN` or `hf auth login` first. `STACKGO_DATA_ROOT` changes defaults when explicit paths are omitted.
所有步骤都可安全重启。正文下载器按 blob 去重、原子发布 Parquet、记录 SQLite 状态,并验证正文 SHA-1。`--status` 查看状态,`--index-only` 只建立正文下载队列;Software Heritage 暂不可用的对象可在确认来源后使用 `stackgo-recover-contents` 尝试精确 revision 的 GitHub fallback。
`stackgo-export` 将已下载数据拆成两个目录:`repo-meta/<shard>/repositories.parquet` 保存仓库、文件路径、revision 和许可等元数据;`repo-contents/<shard>/*.parquet` 保存 `blob_id`、正文和长度。两者通过 shard 名和 `blob_id` 关联。默认使用硬链接,不额外占用正文空间;跨文件系统时使用 `--mode copy`,或用 `--mode symlink` 建立绝对链接。输出的 `manifest.json` 记录来源 manifest 哈希、布局和统计。
导出目录会自动生成根目录、`repo-meta/` 和 `repo-contents/` 三份 README,说明字段和关联方式,适合直接作为 Hugging Face dataset repository 的数据说明。项目代码本身就在 `the-stack-go/stackgo/` 中维护,不由导出器复制或刷新。
## 生成样本
```python
from stackbuild import BuildConfig, build_samples, write_parquet
samples = build_samples(
repo,
config=BuildConfig(
max_tokens=8192,
target_context=True,
max_targets_per_repo=32,
max_dependency_depth=3,
),
)
write_parquet(samples, "repository-samples.parquet")
```
默认 tokenizer 固定为 `deepseek-ai/DeepSeek-V4-Flash-Base` revision
`8855555deef230a27a21a8d6f294b7b7497759b6`。优先读取同级
`../tokenizers/deepseek-v4-flash-base/`,也可用 `STACKBUILD_TOKENIZER_PATH` 指定本地目录。
全量生成使用本地 tokenizer,不会在扫描中下载模型或 tokenizer:
```bash
python -m stackgo.scripts.build_stack_samples --root /mnt/data/the-stack-go --jobs 8
```
输出为 `samples/train-xxxxx-of-00064.parquet`,使用 ZSTD 压缩和 2048 行 row group。
`manifest.json` 记录 tokenizer revision、SHA-256 和预算;安全重启会跳过配置一致且已完成的分片。
中断的 `.parquet.part` 会重新生成。默认并行构建 8 个源 shard,每处理 100 个仓库打印进度;
内存较小的机器可用 `--jobs 1` 降低并发。
每个仓库在独立的可杀死 runner 中构建。默认单仓库超过 **1800 秒**时,
只终止并降级当前仓库到 regex,其他仓库和后续分片继续优先使用 tree-sitter。
`dependency` 样本覆盖仓库全部文件;`target_context` 样本优先放置直接和传递依赖,再放 README、manifest 和构建文件。二进制、vendor 和 generated 文件默认只保留元数据。完整 API 说明见 [`stackbuild/README.md`](stackbuild/README.md)。
## 读取样本
样本是标准 Parquet 文件,推荐流式读取。仓库提供了可运行示例:
```bash
PYTHONPATH=. python examples/read_samples.py ../samples/train-*.parquet --limit 3
PYTHONPATH=. python examples/read_samples.py ../samples \
--repo owner/project --kind dependency --limit 5
```
PyArrow 流式读取:
```python
import json
import pyarrow.dataset as ds
dataset = ds.dataset("samples", format="parquet")
scanner = dataset.scanner(
columns=["sample_id", "repo_id", "kind", "text", "files", "token_count", "metadata_json"],
filter=ds.field("kind") == "dependency",
batch_size=512,
)
for batch in scanner.to_batches():
for row in batch.to_pylist():
print(row["repo_id"], row["token_count"])
print([item["path"] for item in row["files"]])
print(json.loads(row["metadata_json"]))
```
完整示例见 [`examples/README.md`](examples/README.md),包括 Hugging Face
`datasets` 流式加载、按仓库/kind 过滤和文件 provenance 读取。
## 测试
```bash
PYTHONPATH=. pytest -q
```
数据目录不放在 Git 工作区;用 manifest、revision、过滤条件和生成配置记录每次数据版本。