Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Huggingface Transformers Modular Model Converter

From Leeroopedia
Revision as of 13:06, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Huggingface_Transformers_Modular_Model_Converter.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains Build_System, Code_Generation
Last Updated 2026-02-13 20:00 GMT

Overview

Concrete tool for converting modular model definition files (modular_*.py) into fully standalone modeling files by resolving class inheritance, renaming symbols, and inlining dependencies.

Description

The modular_model_converter.py utility (1930 lines) is the engine behind the "modular model" architecture. It uses libcst for CST-level code transformations. It parses modular files to build dependency graphs between classes, functions, and assignments. For each class that inherits from a parent model's class, it: (1) retrieves the parent class source, (2) applies name replacement transformers to convert parent model names to the new model names (handling CamelCase, lowercase, and uppercase variants), (3) replaces super() calls to reference the correct parent, (4) merges overridden methods while preserving new additions, and (5) resolves all transitive dependencies (imports, helper functions, constants). The ModularFileMapper orchestrates the full conversion, mapping each modular file to one or more output files with correctly transformed imports. Supports parallel processing via multiprocessing.

Usage

Run to generate or update modeling files from their modular definitions. Enforced by CI to ensure generated files stay in sync with modular sources.

Code Reference

Source Location

Signature

class ReplaceNameTransformer(cst.CSTTransformer):
    """CST transformer that replaces model-specific names."""

class ClassDependencyMapper:
    """Builds dependency graphs between classes in a module."""

class ModularFileMapper:
    """Orchestrates conversion from modular to standalone files."""

    def convert(self, modular_file: str) -> Dict[str, str]:
        """Convert a modular file to a dict of {output_file: content}."""

def convert_modular_files(
    files: List[str] = None,
    overwrite: bool = True,
    num_workers: int = 1,
) -> None:
    """Convert all modular files, optionally in parallel."""

Import

python utils/modular_model_converter.py
python utils/modular_model_converter.py --files src/transformers/models/new_model/modular_new_model.py

I/O Contract

Inputs

Name Type Required Description
modular_*.py files Python files Yes Modular model definition files
--files List[str] No Specific modular files to convert (default: all)
--num_workers int No Number of parallel workers

Outputs

Name Type Description
modeling_*.py Python files Generated standalone modeling files
configuration_*.py Python files Generated configuration files

Usage Examples

Converting Modular Models

# Convert all modular files
python utils/modular_model_converter.py

# Convert a specific modular file
python utils/modular_model_converter.py \
    --files src/transformers/models/llama/modular_llama.py

# Parallel conversion
python utils/modular_model_converter.py --num_workers 4

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment