stack-v3-train
收藏资源简介:
The Stack v3 是迄今为止最大、最新的开源源代码数据集,直接从 GitHub 爬取,专为预训练具有完整仓库上下文的大语言模型而设计。它是 The Stack v2 的升级版,旨在使代码模型的训练更加开放、可复现和透明。与之前的版本相比,v3 有两项关键改进:一、文件内容直接内嵌在数据集中,无需额外下载;二、数据反映的是 2025 年 8 月 GitHub 的状态,涵盖了约两年新增的开源代码。训练子集(stack-v3-train)包含 15.9 TB 的源代码,涵盖 713 种编程语言,来自 1.73 亿个仓库,约 4.9 万亿个 token。完整版(stack-v3-full)则包含 113.7 TB 的源代码,涵盖 770 种语言,来自 2.24 亿个仓库。数据集分为两个互补的部分:stack-v3-train(经过近重复去除和启发式质量过滤,按仓库分组,文件内容内联,可直接用于训练)和 stack-v3-full(完整未过滤的语料库,托管为 Hugging Face Storage Bucket,分为元数据和内容两部分,保留所有近重复信息及预过滤信号,适合自定义过滤和研究)。数据集结构(以 stack-v3-train 为例):每一行代表一个仓库,包含 repo_path、repo_id、commit_id、github_metadata、num_files、files(每个文件包含 content_id、content、size_bytes、file_path、file_timestamp、language、is_vendor、license_type、detected_licenses)等字段。创建过程包括从 GH Archive 和 Software Heritage 收集仓库列表、爬取快照、语言检测、许可证检测、近重复去除、质量过滤、PII 脱敏等步骤。
The Stack v3 is the largest and most recent open-source code dataset, directly crawled from GitHub, designed for pretraining large language models with full repository context. It is an upgrade of The Stack v2, aiming to make code model training more open, reproducible, and transparent. Compared to previous versions, v3 has two key improvements: first, file contents are embedded directly in the dataset without needing additional downloads; second, the data reflects the state of GitHub as of August 2025, covering approximately two years of newly added open-source code. The training subset (stack-v3-train) contains 15.9 TB of source code covering 713 programming languages from 173 million repositories, approximately 4.9 trillion tokens. The full version (stack-v3-full) contains 113.7 TB of source code covering 770 languages from 224 million repositories. The dataset is divided into two complementary parts: stack-v3-train (filtered with near-deduplication and heuristic quality filters, grouped by repository, with file contents inlined, ready for training) and stack-v3-full (complete unfiltered corpus, hosted on Hugging Face Storage Bucket, split into metadata and content, retaining all near-duplicate information and pre-filter signals, suitable for custom filtering and research). Dataset structure (using stack-v3-train as example): each line represents a repository with fields including repo_path, repo_id, commit_id, github_metadata, num_files, files (each file contains content_id, content, size_bytes, file_path, file_timestamp, language, is_vendor, license_type, detected_licenses). The creation process includes collecting repository lists from GH Archive and Software Heritage, crawling snapshots, language detection, license detection, near-deduplication, quality filtering, PII redaction, etc.
The Stack v3 数据集详情
数据集简介
The Stack v3 是一个大规模、最新的开源代码数据集,直接从 GitHub 爬取,专为代码大语言模型(LLM)的全仓库上下文预训练而设计。它是 The Stack v2 的继任者,致力于使代码模型训练更加开放、可复现和透明。
数据集规模
- 训练子集包含 15.9 TB 源代码,覆盖 713 种编程语言,来自 1.73 亿 个仓库,约 4.9 万亿 tokens。
- 完整语料库(stack-v3-full)包含 113.7 TB 源代码,覆盖 770 种语言,来自 2.24 亿 个仓库。
- 数据快照日期为 2025 年 8 月 7 日,反映了 GitHub 公共仓库的当时状态。
版本对比
| 特性 | The Stack v1 | The Stack v2 | The Stack v3 |
|---|---|---|---|
| 截止时间 | 2022 | 2023 | 2025 |
| 完整规模 | 6.4 TB | 67.5 TB | 113.7 TB |
| 去重后 | 2.9 TB | 32.1 TB | 99.3 TB |
| 训练集 | ~200B tokens | 2.0 TB / ~550B tokens | 15.9 TB / ~4.9T tokens |
| 语言数量 | 358 | 618 | 713 |
数据集发布形式
The Stack v3 以两个互补的数据集发布:
- stack-v3-train(本数据集):近去重、启发式过滤后的代码,按仓库分组,文件内容内联,可直接用于预训练。
- stack-v3-full:完整的原始语料(过滤和近去重前),以 HF Storage Bucket 形式托管,分为元数据部分和内容部分,适合研究和自定义过滤。
数据结构
每个数据行代表一个 仓库,包含以下字段:
repo_path:GitHub 上的owner/name标识。repo_id:GitHub 仓库数字 ID。commit_id:快照对应的默认分支 HEAD 提交。github_metadata:仓库级元数据(分支、提交数、创建时间、是否为 fork、star 数、fork 数、issue 数、PR 数等)。num_files:仓库中的文件数量。files:文件列表,每个文件包含:content_id:文件内容的 SHA-1 哈希。content:UTF-8 文件内容(已进行 PII 脱敏)。size_bytes:内容字节长度。file_path:仓库内文件路径。file_timestamp:文件修改时间(Unix 秒)。language:由 go-enry(GitHub Linguist 的移植版)检测的编程语言。is_vendor:是否为第三方/供应商文件。license_type:permissive或no_license。detected_licenses:由 ScanCode 检测的 SPDX 许可证标识列表。
与 v2 的主要变化
- 文件内容内联:源代码文本直接嵌入数据集,下载完成即可开始训练。
- 数据来源:从 Software Heritage 图(2023)改为直接爬取 GitHub(截止 2025-08-07),新增约两年的开源代码。
- 近去重方式:从按语言去重改为跨语言去重,并通过 Jaccard 验证消除误报。
数据处理流程
- 解码与语言检测:文件解码为 UTF-8,使用 go-enry 检测语言、供应商和生成文件标记。
- 许可证检测:使用 ScanCode 进行文件级许可证检测,并通过目录传播到同级文件,非许可性许可证(non_permissive)的文件被排除。
- 近去重:MinHash-LSH(256 置换、5-gram、最小 5 tokens),相似度 ≥0.7 的候选对被聚类,每个簇保留一个代表文件。
- 质量过滤:移除字母占比过低、行过长、行数过多、含自动生成标记等质量不佳的文件。
- PII 脱敏:使用 StarPII 模型检测并替换邮箱、密钥、姓名、密码和 IP 地址为占位符。
- Jupyter 笔记本处理:剥离输出、内联图像和易变元数据,保持结构化 JSON。
- 仓库分组:按仓库将所有文件分组,支持仓库上下文预训练。
使用方式
python from datasets import load_dataset
完整下载(本地缓存)
ds = load_dataset("HuggingFaceCode/stack-v3-train", split="train", cache_dir="/path/to/local/cache", num_proc=16)
流式加载(推荐快速测试)
ds = load_dataset("HuggingFaceCode/stack-v3-train", split="train", streaming=True)
也可通过 DuckDB 或 Polars 直接查询 Parquet 文件。注意:没有按语言划分的配置,需要自行按 language 字段筛选。
数据局限性
- 热门语言(如 C、JavaScript、Python)在数据中占比过高,小众语言代表性不足。
- 注释中可能包含有害或冒犯性语言。
- 大多数自然语言为英语。
- PII 脱敏并非完美,数据中可能仍残留敏感信息。
- 许可证归属的准确性受 ScanCode 工具限制。
退出机制
开发者可通过 "Am I in The Stack?" Space 检查代码是否被收录,并提交移除请求。已选择退出的仓库会在每次补丁发布前从数据集中移除。





