Data Selection Principles for Code Language Model Pretraining
When pretraining a language model focused on programming assistance, selecting high-quality code data is crucial for developing capable models. Based on recent research and practices in building code language models, several effective principles and methods have emerged for selecting good code documents from large repositories.
Language and Format Filtering
Language filtering is typically the first step in data selection for code models:
-
Programming Language Selection: Models like DeepSeek-Coder retain only specific programming languages (87 languages in their case) and filter out others to focus on relevant languages[4].
-
File Extension Filtering: A simple but effective approach is filtering for documents that match approved filename extensions (e.g., ".py" for Python)[2].
-
Format-Specific Filtering: Different file formats require different filtering rules:
- For HTML files: Retain those where visible text constitutes at least 20% of the code and is no less than 100 characters[4].
- For JSON and YAML: Keep only files with character count between 50-5000 characters to remove excessively data-heavy files[4].
- For XML files: Filter out files where "<?xml version=" appears in the first 100 characters (except for XSLT)4.
Heuristic Filtering for Code
Heuristics are particularly valuable for efficiently processing large code repositories:
-
Line Length: Filter out files with average line length exceeding 100 characters or maximum line length surpassing 1000 characters[4]. For the ROOTS dataset, researchers filtered for line lengths between 20-1000 characters[2].
-
Alphabetic Character Ratio: Remove files with fewer than 25% alphabetic characters[4]. For ROOTS, they kept files with 15-65% alphabetic characters (a-zA-Z)[2].
-
File Size: Filter source files to keep those between 100 and 200,000 characters[2].
-
Line Variance: Filter out files with highly uniform line lengths (standard deviation of 3 characters or less), which might indicate auto-generated code or non-code content[2].
-
Document Structure: The goal is to constrain the training distribution along dimensions like file length, repetitiveness, and character composition, assuming the evaluation distribution will exhibit similar characteristics[1][2].
Deduplication Methods
Deduplication is essential for removing redundant code that might bias the model:
Data Mix and Composition
Research shows that a balanced mix of code and code-related content produces better models:
-
Complementary Data Types: DeepSeek-Coder used 87% source code, 10% English code-related natural language corpus (from GitHub Markdown and StackExchange), and 3% non-code language corpus[4].
-
Domain-Specific Content: Including documentation, discussions about code (from platforms like StackExchange), and educational materials helps the model understand code concepts and library usage[4].
Advanced Selection Methods
Beyond basic heuristics, more sophisticated approaches can be employed:
-
Classifier-Based Quality Filtering: Training binary classifiers to distinguish between high-quality and low-quality code documents[9].
- For code, this might involve training a classifier on well-structured, documented code vs. poorly written code.
- FastText-based classifiers are efficient for large-scale filtering[9].
-
N-gram Repetition Analysis: Identifying and filtering out code with excessive repetition that might indicate auto-generated content or boilerplate[6][9].
-
Dependency and Structure Analysis: Parsing code to understand its dependency structure can help select well-structured code samples[4].
Preparation Pipeline
A comprehensive pipeline for code data preparation typically includes:
-
Data Crawling: Collecting raw code from public repositories (e.g., GitHub)[4].
-
Rule-Based Filtering: Applying the heuristics mentioned above to filter out low-quality content[4].
-
Dependency Parsing: Analyzing code structure and dependencies[4].
-
Repository-Level Deduplication: Eliminating duplicate repositories[4].
-
Quality Screening: Final filtering based on quality metrics[4].
-
Data Mixing: Combining code with related natural language content in appropriate proportions[4].
Efficiency Considerations
Given the scale of code repositories, efficiency in selection is important:
-
Cascading Approach: Implementing filters in sequence, starting with the most efficient ones[6][9].
-
Batch Processing: Processing multiple documents simultaneously to reduce computation time[6].
-
Targeted Selection: Using loss-based approaches to select data that improves performance on downstream tasks, which can significantly reduce the amount of data needed for training[10].
Evaluation Methods
To validate your selection strategy:
-
Manual Validation: Checking a sample of filtered documents to ensure desirable code is kept and undesirable code is removed[1].
-
Model Training and Evaluation: Training smaller models on different data subsets to compare performance[1][10].
By applying these principles, you can significantly improve the quality of your training data, which will directly impact the performance of your code language model. The most effective approach will likely combine multiple methods tailored to your specific resources and target programming domains.