Skip to content

Conversation

@devin-ai-integration
Copy link

Make sure to read the contributing guidelines before submitting a PR

Summary

This PR introduces the foundational infrastructure for refactoring src/llama-model.cpp (20,498 lines) into a modular architecture. This is Phase 1 only - it creates the scaffolding and patterns but does not yet migrate any existing code.

Link to Devin run: https://app.devin.ai/sessions/a329f05a4cdc4f2eb254bcb7ee3fb2ac
Requested by: Jake Cosme (@jakexcosme)

Changes

New Infrastructure

  • Directory structure under src/llama-model/:

    • architectures/ - Will contain one file per architecture (currently has LLAMA example)
    • interfaces/ - Interface definitions (IModelLoader, IGraphBuilder, ArchitectureBuilder)
    • registry/ - Architecture registration system with singleton pattern
    • base/, loading/, memory/ - Empty directories for future modules
  • Registry Pattern: ArchitectureRegistry with factory-based lazy instantiation

  • Example Implementation: LlamaArchitectureBuilder that demonstrates the pattern (currently just delegates to existing code)

  • Documentation: Comprehensive 518-line REFACTORING-GUIDE.md with examples and migration strategy

Build System

  • Updated src/CMakeLists.txt to use file(GLOB ...) for automatic inclusion of architecture files
  • Updated CODEOWNERS for new directory structure

Backwards Compatibility

All existing APIs unchanged - No modifications to llama_model interface
All 37 tests pass (100% pass rate)
No functional changes - New code is not yet integrated into existing flow

Important Notes for Reviewers

This PR Does NOT:

  • Actually refactor any of the 89+ architecture builders from llama-model.cpp
  • Integrate the new registry system into the existing code paths
  • Change any existing functionality
  • Remove any existing code

This PR DOES:

  • Create the infrastructure and patterns for future refactoring
  • Demonstrate the intended approach with one example (LLAMA)
  • Provide comprehensive documentation for contributors
  • Ensure the build system can handle the new structure

Key Review Points

  1. Architecture Design: Are the interfaces (ArchitectureBuilder, ArchitectureRegistry) well-designed for the intended refactoring?

  2. Documentation Quality: Is docs/development/REFACTORING-GUIDE.md clear and comprehensive enough for contributors to follow?

  3. Build System: The use of file(GLOB ...) is convenient but requires manual CMake re-runs when files are added. Is this acceptable?

  4. Dead Code: The new registry and LLAMA builder are not yet called from anywhere. Is it okay to merge infrastructure before integration?

  5. Empty Directories: Several directories (base/, loading/, memory/) are created but empty. Should these be added in later PRs instead?

  6. Macro Safety: The REGISTER_ARCHITECTURE macro uses __LINE__ for uniqueness. Review for potential edge cases.

Testing

# Build succeeds
cmake -B build -DLLAMA_FATAL_WARNINGS=ON
cmake --build build --config Release -j $(nproc)

# All tests pass
cd build && ctest -L main --timeout 300
# Result: 100% tests passed, 0 tests failed out of 37

Next Steps (Phase 2)

After this PR is merged, Phase 2 will:

  1. Integrate the registry into llama_model::build_graph()
  2. Migrate architectures one at a time from the monolithic file
  3. Add unit tests for each extracted architecture
  4. Gradually deprecate the old switch-statement approach

Checklist

  • Directory structure created
  • Interfaces defined with documentation
  • Registry pattern implemented
  • Example architecture (LLAMA) created
  • Build system updated
  • CODEOWNERS updated
  • Comprehensive documentation written
  • All existing tests pass
  • No breaking changes to public API
  • Unit tests for registry (in gitignored directory, will add in Phase 2)
  • Integration with existing code (Phase 2)
  • Migration of remaining architectures (Phase 2+)

This commit introduces a modular architecture to refactor the monolithic
src/llama-model.cpp (20,498 lines) into a maintainable structure.

Key changes:
- Created src/llama-model/ directory structure with subdirectories:
  - architectures/: One file per architecture (starting with LLAMA)
  - interfaces/: Interface definitions (IModelLoader, IGraphBuilder)
  - registry/: Architecture registration system
  - base/, loading/, memory/: Future modular components

- Implemented ArchitectureRegistry pattern:
  - Singleton registry for architecture builders
  - Factory-based lazy instantiation
  - REGISTER_ARCHITECTURE macro for easy registration

- Created comprehensive documentation:
  - docs/development/REFACTORING-GUIDE.md with migration guide
  - Inline documentation in all new interfaces
  - Examples and common patterns

- Updated build system:
  - Modified src/CMakeLists.txt to include modular sources
  - Uses GLOB to automatically include architecture files

- Updated CODEOWNERS:
  - Added ownership for new modular directories
  - Maintained existing ownership patterns

Backwards compatibility:
- All existing APIs remain unchanged
- Loading flow in src/llama.cpp unchanged
- All 37 existing tests pass (100% pass rate)
- No breaking changes to public API

This is Phase 1 of the refactoring. Future phases will:
- Extract remaining 88+ architectures
- Implement loading/memory modules
- Add comprehensive unit tests
- Remove deprecated code paths

Related: #<issue_number> (if applicable)
Co-Authored-By: Jake Cosme <[email protected]>
@devin-ai-integration
Copy link
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@github-actions github-actions bot added the documentation Improvements or additions to documentation label Nov 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant