Refactor Reproducer to Eliminate Code Duplication Using AST-Based Function Extraction #178
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
fix #170
Summary
This PR eliminates duplicate functions between the reproducer template and utility module by implementing an automatic function extraction system using Python's AST (Abstract Syntax Tree) parser. The template file is reduced by 92% (398 → 31 lines) while maintaining full functionality.
Net Impact: 4 files changed, -40 lines of code (443 insertions, 483 deletions)
Problem
The reproducer system had significant code duplication:
example.py(template) andutils.pycontained 8 duplicate functionsutils.pywas missing critical functionality:NoneType,str,floattypesSolution
1. Single Source of Truth
All utility functions now live exclusively in
utils.py. The template uses a placeholder that gets replaced with extracted code during reproducer generation.2. AST-Based Function Extraction
Created
function_extractor.pythat uses Python's AST parser to:3. Enhanced Utility Functions
Fixed and expanded
utils.pywith:NoneType,str,float, etc.)cuda→cuda:0)Changes Overview
Files Modified
function_extractor.pyplaceholder_replacer.pyexample.pyutils.pyDetailed Changes
1. New Module:
function_extractor.py(+122 lines)Purpose: Extract utility functions from source files using AST parsing.
Key Functions:
Extracted Content:
utils.py: 8 functions + 1 constantload_tensor.py: 1 functionAdvantages over inspect-based approach:
2. Enhanced:
utils.py(+320 lines, restructured)Added Functions:
Enhanced Functions:
Impact:
3. Simplified:
example.py(-373 lines, -92%)Before: 398 lines with duplicate function implementations
After: 31 lines with just structure and placeholders
Note:
# noqa: F821comments suppress linter warnings for identifiers that will be injected at generation time.4. Updated:
placeholder_replacer.py(+11 lines)Changes:
Technical Highlights
AST-Based Extraction
Why AST instead of inspect?
Example: Robust Constant Extraction
Before (fragile string parsing):
After (robust AST-based):
Workflow
How reproducer generation works now:
example.py) - Only 31 lines with placeholders{{JSON_FILE_NAME_PLACEHOLDER}}→ JSON filename{{KERNEL_IMPORT_PLACEHOLDER}}→ Kernel import statement{{UTILITY_FUNCTIONS_PLACEHOLDER}}→ Extracted utility code (~14KB){{KERNEL_INVOCATION_PLACEHOLDER}}→ Kernel callfunction_extractor.pyparses source files via ASTTesting & Validation
✅ All Tests Pass
1. Function Extraction Validation
2. Integration Test
3. Code Quality
4. Output Consistency
Benefits
1. Code Maintainability 📈
2. Functional Completeness ✅
3. Advantages Over Previous Approach
utils.pyexample.pyis cleaner and focused on structureutils.pynow feature-complete4. Developer Experience 🎯
utils.py→ Add name to extraction list → DoneBreaking Changes
None. This is a pure refactoring:
Migration Notes
No action required from users. This change is completely transparent:
Future Possibilities
With AST-based extraction, we can now easily:
Extract Classes
Extract Type Aliases
Selective Extraction
_prefix)Cross-File Analysis
Commits
Add function extractor module for reproducer utility functions
Refactor function extractor to use AST parsing
Review Checklist
Conclusion
This PR successfully modernizes the reproducer system by:
The refactoring is complete, tested, and ready for review.
Test Plan